Annotation of loncom/metadata_database/searchcat.pl, revision 1.80
1.1 harris41 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # searchcat.pl "Search Catalog" batch script
1.16 harris41 4: #
1.80 ! raeburn 5: # $Id: searchcat.pl,v 1.79 2013/07/25 19:11:10 raeburn Exp $
1.16 harris41 6: #
7: # Copyright Michigan State University Board of Trustees
8: #
1.29 albertel 9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
1.16 harris41 10: #
1.29 albertel 11: # LON-CAPA is free software; you can redistribute it and/or modify
1.16 harris41 12: # it under the terms of the GNU General Public License as published by
13: # the Free Software Foundation; either version 2 of the License, or
14: # (at your option) any later version.
15: #
1.29 albertel 16: # LON-CAPA is distributed in the hope that it will be useful,
1.16 harris41 17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: # GNU General Public License for more details.
20: #
21: # You should have received a copy of the GNU General Public License
1.29 albertel 22: # along with LON-CAPA; if not, write to the Free Software
1.16 harris41 23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: #
25: # /home/httpd/html/adm/gpl.txt
26: #
1.29 albertel 27: # http://www.lon-capa.org/
1.16 harris41 28: #
29: ###
1.33 matthew 30:
1.32 www 31: =pod
1.1 harris41 32:
1.32 www 33: =head1 NAME
34:
35: B<searchcat.pl> - put authoritative filesystem data into sql database.
36:
37: =head1 SYNOPSIS
38:
39: Ordinarily this script is to be called from a loncapa cron job
40: (CVS source location: F<loncapa/loncom/cron/loncapa>; typical
41: filesystem installation location: F</etc/cron.d/loncapa>).
42:
43: Here is the cron job entry.
44:
45: C<# Repopulate and refresh the metadata database used for the search catalog.>
46: C<10 1 * * 7 www /home/httpd/perl/searchcat.pl>
47:
48: This script only allows itself to be run as the user C<www>.
49:
50: =head1 DESCRIPTION
51:
52: This script goes through a loncapa resource directory and gathers metadata.
53: The metadata is entered into a SQL database.
54:
55: This script also does general database maintenance such as reformatting
56: the C<loncapa:metadata> table if it is deprecated.
57:
58: This script evaluates dynamic metadata from the authors'
1.48 www 59: F<nohist_resevaldata.db> database file in order to store it in MySQL.
1.32 www 60:
61: This script is playing an increasingly important role for a loncapa
62: library server. The proper operation of this script is critical for a smooth
63: and correct user experience.
64:
65: =cut
1.1 harris41 66:
1.45 www 67: use strict;
1.55 matthew 68: use DBI;
1.17 harris41 69: use lib '/home/httpd/lib/perl/';
1.55 matthew 70: use LONCAPA::lonmetadata;
1.76 albertel 71: use LONCAPA;
1.56 matthew 72: use Getopt::Long;
1.1 harris41 73: use IO::File;
74: use HTML::TokeParser;
1.21 www 75: use GDBM_File;
1.24 www 76: use POSIX qw(strftime mktime);
1.80 ! raeburn 77: use Mail::Send;
1.56 matthew 78:
1.63 matthew 79: use Apache::lonnet();
1.62 matthew 80:
1.55 matthew 81: use File::Find;
1.1 harris41 82:
1.56 matthew 83: #
84: # Set up configuration options
1.63 matthew 85: my ($simulate,$oneuser,$help,$verbose,$logfile,$debug);
1.56 matthew 86: GetOptions (
87: 'help' => \$help,
88: 'simulate' => \$simulate,
89: 'only=s' => \$oneuser,
90: 'verbose=s' => \$verbose,
91: 'debug' => \$debug,
92: );
93:
94: if ($help) {
95: print <<"ENDHELP";
96: $0
97: Rebuild and update the LON-CAPA metadata database.
98: Options:
99: -help Print this help
100: -simulate Do not modify the database.
101: -only=user Only compute for the given user. Implies -simulate
102: -verbose=val Sets logging level, val must be a number
103: -debug Turns on debugging output
104: ENDHELP
105: exit 0;
106: }
107:
108: if (! defined($debug)) {
109: $debug = 0;
110: }
111:
112: if (! defined($verbose)) {
113: $verbose = 0;
114: }
115:
116: if (defined($oneuser)) {
117: $simulate=1;
118: }
119:
1.55 matthew 120: ##
121: ## Use variables for table names so we can test this routine a little easier
1.69 raeburn 122: my %oldnames = (
123: 'metadata' => 'metadata',
124: 'portfolio' => 'portfolio_metadata',
125: 'access' => 'portfolio_access',
126: 'addedfields' => 'portfolio_addedfields',
1.78 raeburn 127: 'allusers' => 'allusers',
1.69 raeburn 128: );
129:
130: my %newnames;
131: # new table names - append pid to have unique temporary tables
132: foreach my $key (keys(%oldnames)) {
133: $newnames{$key} = 'new'.$oldnames{$key}.$$;
134: }
1.45 www 135:
1.55 matthew 136: #
137: # Only run if machine is a library server
1.63 matthew 138: exit if ($Apache::lonnet::perlvar{'lonRole'} ne 'library');
1.78 raeburn 139: my $hostid = $Apache::lonnet::perlvar{'lonHostID'};
140:
1.55 matthew 141: #
142: # Make sure this process is running from user=www
143: my $wwwid=getpwnam('www');
144: if ($wwwid!=$<) {
1.63 matthew 145: my $emailto="$Apache::lonnet::perlvar{'lonAdmEMail'},$Apache::lonnet::perlvar{'lonSysEMail'}";
146: my $subj="LON: $Apache::lonnet::perlvar{'lonHostID'} User ID mismatch";
1.55 matthew 147: system("echo 'User ID mismatch. searchcat.pl must be run as user www.' |\
1.63 matthew 148: mail -s '$subj' $emailto > /dev/null");
1.55 matthew 149: exit 1;
150: }
151: #
152: # Let people know we are running
1.63 matthew 153: open(LOG,'>>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/logs/searchcat.log');
1.56 matthew 154: &log(0,'==== Searchcat Run '.localtime()."====");
1.57 matthew 155:
156:
1.56 matthew 157: if ($debug) {
158: &log(0,'simulating') if ($simulate);
159: &log(0,'only processing user '.$oneuser) if ($oneuser);
160: &log(0,'verbosity level = '.$verbose);
161: }
1.55 matthew 162: #
163: # Connect to database
164: my $dbh;
1.63 matthew 165: if (! ($dbh = DBI->connect("DBI:mysql:loncapa","www",$Apache::lonnet::perlvar{'lonSqlAccess'},
1.55 matthew 166: { RaiseError =>0,PrintError=>0}))) {
1.56 matthew 167: &log(0,"Cannot connect to database!");
1.55 matthew 168: die "MySQL Error: Cannot connect to database!\n";
169: }
170: # This can return an error and still be okay, so we do not bother checking.
171: # (perhaps it should be more robust and check for specific errors)
1.69 raeburn 172: foreach my $key (keys(%newnames)) {
173: if ($newnames{$key} ne '') {
174: $dbh->do('DROP TABLE IF EXISTS '.$newnames{$key});
175: }
176: }
177:
1.55 matthew 178: #
1.77 raeburn 179: # Create the new metadata, portfolio and allusers tables
1.69 raeburn 180: foreach my $key (keys(%newnames)) {
181: if ($newnames{$key} ne '') {
182: my $request =
183: &LONCAPA::lonmetadata::create_metadata_storage($newnames{$key},$oldnames{$key});
184: $dbh->do($request);
185: if ($dbh->err) {
186: $dbh->disconnect();
187: &log(0,"MySQL Error Create: ".$dbh->errstr);
188: die $dbh->errstr;
189: }
190: }
1.55 matthew 191: }
1.69 raeburn 192:
1.55 matthew 193: #
194: # find out which users we need to examine
1.63 matthew 195: my @domains = sort(&Apache::lonnet::current_machine_domains());
196: &log(9,'domains ="'.join('","',@domains).'"');
1.62 matthew 197:
198: foreach my $dom (@domains) {
199: &log(9,'domain = '.$dom);
1.63 matthew 200: opendir(RESOURCES,"$Apache::lonnet::perlvar{'lonDocRoot'}/res/$dom");
1.62 matthew 201: my @homeusers =
202: grep {
1.63 matthew 203: &ishome("$Apache::lonnet::perlvar{'lonDocRoot'}/res/$dom/$_");
1.62 matthew 204: } grep {
205: !/^\.\.?$/;
206: } readdir(RESOURCES);
207: closedir RESOURCES;
208: &log(5,'users = '.$dom.':'.join(',',@homeusers));
209: #
210: if ($oneuser) {
211: @homeusers=($oneuser);
212: }
1.80 ! raeburn 213:
1.62 matthew 214: #
215: # Loop through the users
216: foreach my $user (@homeusers) {
217: &log(0,"=== User: ".$user);
218: &process_dynamic_metadata($user,$dom);
219: #
220: # Use File::Find to get the files we need to read/modify
221: find(
222: {preprocess => \&only_meta_files,
223: #wanted => \&print_filename,
224: #wanted => \&log_metadata,
225: wanted => \&process_meta_file,
1.66 albertel 226: no_chdir => 1,
1.63 matthew 227: }, join('/',($Apache::lonnet::perlvar{'lonDocRoot'},'res',$dom,$user)) );
1.62 matthew 228: }
1.77 raeburn 229: # Search for all users and public portfolio files
1.78 raeburn 230: my (%allusers,%portusers,%courses);
1.69 raeburn 231: if ($oneuser) {
232: %portusers = (
233: $oneuser => '',
234: );
1.77 raeburn 235: %allusers = (
236: $oneuser => '',
237: );
1.78 raeburn 238: %courses = &courseiddump($dom,'.',1,'.','.',$oneuser,undef,
239: undef,'.');
1.69 raeburn 240: } else {
1.78 raeburn 241: # get courseIDs for domain on current machine
242: %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.');
1.69 raeburn 243: my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
1.77 raeburn 244: &descend_tree($dom,$dir,0,\%portusers,\%allusers);
1.69 raeburn 245: }
246: foreach my $uname (keys(%portusers)) {
247: my $urlstart = '/uploaded/'.$dom.'/'.$uname;
248: my $pathstart = &propath($dom,$uname).'/userfiles';
1.78 raeburn 249: my $is_course = '';
250: if (exists($courses{$dom.'_'.$uname})) {
251: $is_course = 1;
252: }
1.69 raeburn 253: my $curr_perm = &Apache::lonnet::get_portfile_permissions($dom,$uname);
254: my %access = &Apache::lonnet::get_access_controls($curr_perm);
1.75 raeburn 255: foreach my $file (keys(%access)) {
1.69 raeburn 256: my ($group,$url,$fullpath);
257: if ($is_course) {
258: ($group, my ($path)) = ($file =~ /^(\w+)(\/.+)$/);
1.72 raeburn 259: $fullpath = $pathstart.'/groups/'.$group.'/portfolio'.$path;
1.69 raeburn 260: $url = $urlstart.'/groups/'.$group.'/portfolio'.$path;
261: } else {
262: $fullpath = $pathstart.'/portfolio'.$file;
1.72 raeburn 263: $url = $urlstart.'/portfolio'.$file;
1.69 raeburn 264: }
265: if (ref($access{$file}) eq 'HASH') {
1.75 raeburn 266: my %portaccesslog =
267: &LONCAPA::lonmetadata::process_portfolio_access_data($dbh,
268: $simulate,\%newnames,$url,$fullpath,$access{$file});
269: &portfolio_logging(%portaccesslog);
1.69 raeburn 270: }
1.75 raeburn 271: my %portmetalog = &LONCAPA::lonmetadata::process_portfolio_metadata($dbh,$simulate,\%newnames,$url,$fullpath,$is_course,$dom,$uname,$group);
272: &portfolio_logging(%portmetalog);
1.69 raeburn 273: }
274: }
1.79 raeburn 275: my (%names_by_id,,%ids_by_name,%idstodelete,%idstoadd,%duplicates);
276: unless ($simulate || $oneuser) {
277: my $idshashref;
278: $idshashref = &tie_domain_hash($dom, "ids", &GDBM_WRCREAT());
279: if (ref($idshashref) eq 'HASH') {
280: %names_by_id = %{$idshashref};
281: while (my ($id,$uname) = each(%{$idshashref}) ) {
282: $id = &unescape($id);
283: $uname = &unescape($uname);
284: $names_by_id{$id} = $uname;
285: push(@{$ids_by_name{$uname}},$id);
286: }
287: &untie_domain_hash($idshashref);
288: }
289: }
1.77 raeburn 290: # Update allusers
291: foreach my $uname (keys(%allusers)) {
1.78 raeburn 292: next if (exists($courses{$dom.'_'.$uname}));
1.77 raeburn 293: my %userdata =
294: &Apache::lonnet::get('environment',['firstname','lastname',
295: 'middlename','generation','id','permanentemail'],$dom,$uname);
1.79 raeburn 296: unless ($simulate || $oneuser) {
297: my $addid;
298: if ($userdata{'id'} ne '') {
299: $addid = $userdata{'id'};
300: $addid=~tr/A-Z/a-z/;
301: }
302: if (exists($ids_by_name{$uname})) {
303: if (ref($ids_by_name{$uname}) eq 'ARRAY') {
304: if (scalar(@{$ids_by_name{$uname}}) > 1) {
305: &log(0,"Multiple employee/student IDs found in ids.db for $uname:$dom -- ".join(', ',@{$ids_by_name{$uname}}));
306: }
307: foreach my $id (@{$ids_by_name{$uname}}) {
308: if ($id eq $userdata{'id'}) {
309: undef($addid);
310: } else {
311: $idstodelete{$id} = $uname;
312: }
313: }
314: }
315: }
316: if ($addid ne '') {
317: if (exists($idstoadd{$addid})) {
318: push(@{$duplicates{$addid}},$uname);
319: } else {
320: $idstoadd{$addid} = $uname;
321: }
322: }
323: }
324:
1.77 raeburn 325: $userdata{'username'} = $uname;
326: $userdata{'domain'} = $dom;
327: my %alluserslog =
328: &LONCAPA::lonmetadata::process_allusers_data($dbh,$simulate,
329: \%newnames,$uname,$dom,\%userdata);
330: foreach my $item (keys(%alluserslog)) {
331: &log(0,$alluserslog{$item});
332: }
333: }
1.79 raeburn 334: unless ($simulate || $oneuser) {
335: if (keys(%idstodelete) > 0) {
336: my %resulthash = &Apache::lonnet::iddel($dom,\%idstodelete,$hostid);
337: if ($resulthash{$hostid} eq 'ok') {
338: foreach my $id (sort(keys(%idstodelete))) {
339: &log(0,"Record deleted from ids.db for $dom -- $id => ".$idstodelete{$id});
340: }
341: } else {
342: &log(0,"Error: '$resulthash{$hostid}' occurred when attempting to delete records from ids.db for $dom");
343: }
344: }
345: if (keys(%idstoadd) > 0) {
1.80 ! raeburn 346: my $idmessage = '';
! 347: my %newids;
! 348: foreach my $addid (sort(keys(%idstoadd))) {
! 349: if ((exists($names_by_id{$addid})) && ($names_by_id{$addid} ne $idstoadd{$addid}) && !($idstodelete{$addid})) {
! 350: &log(0,"Two usernames associated with a single ID $addid in domain: $dom: $names_by_id{$addid} (current) and $idstoadd{$addid}\n");
! 351: $idmessage .= "$addid,$names_by_id{$addid},$idstoadd{$addid}\n";
! 352: } else {
! 353: $newids{$addid} = $idstoadd{$addid};
! 354: }
! 355: }
! 356: if (keys(%newids) > 0) {
! 357: my $putresult = &Apache::lonnet::put_dom('ids',\%idstoadd,$dom,$hostid);
! 358: if ($putresult eq 'ok') {
! 359: foreach my $id (sort(keys(%idstoadd))) {
! 360: &log(0,"Record added to ids.db for $dom -- $id => ".$idstoadd{$id});
! 361: }
! 362: } else {
! 363: &log(0,"Error: '$putresult' occurred when attempting to add records to ids.db for $dom");
! 364: }
! 365: }
! 366: if ($idmessage) {
! 367: my $to = &Apache::loncommon::build_recipient_list(undef,'idconflictsmail',$dom);
! 368: if ($to ne '') {
! 369: my $msg = new Mail::Send;
! 370: $msg->to($to);
! 371: $msg->subject('LON-CAPA studentIDs conflict');
! 372: my $lonhost = $Apache::lonnet::perlvar{'lonHostID'};
! 373: my $hostname = &Apache::lonnet::hostname($lonhost);
! 374: my $replytoaddress = 'do-not-reply@'.$hostname;
! 375: $msg->add('Reply-to',$replytoaddress);
! 376: $msg->add('From',"www@$hostname");
! 377: $msg->add('Content-type','text/plain; charset=UTF-8');
! 378: if (my $fh = $msg->open()) {
! 379: print $fh
! 380: 'The following IDs are used for more than one user in your domain:'."\n".
! 381: 'Each row contains: Student/Employee ID, Current username in ids.db file, '.
! 382: 'Additional username'."\n\n".
! 383: $idmessage;
! 384: $fh->close;
! 385: }
1.79 raeburn 386: }
387: }
388: }
389: if (keys(%duplicates) > 0) {
390: foreach my $id (sort(keys(%duplicates))) {
391: &log(0,"Duplicate IDs found for entries to add to ids.db in $dom -- $id => $idstodelete{$id}");
392: }
393: }
394: }
1.55 matthew 395: }
1.69 raeburn 396:
1.55 matthew 397: #
1.69 raeburn 398: # Rename the tables
1.56 matthew 399: if (! $simulate) {
1.69 raeburn 400: foreach my $key (keys(%oldnames)) {
401: if (($oldnames{$key} ne '') && ($newnames{$key} ne '')) {
402: $dbh->do('DROP TABLE IF EXISTS '.$oldnames{$key});
403: if (! $dbh->do('RENAME TABLE '.$newnames{$key}.' TO '.$oldnames{$key})) {
404: &log(0,"MySQL Error Rename: ".$dbh->errstr);
405: die $dbh->errstr;
406: } else {
407: &log(1,"MySQL table rename successful for $key.");
408: }
409: }
1.56 matthew 410: }
1.55 matthew 411: }
412: if (! $dbh->disconnect) {
1.56 matthew 413: &log(0,"MySQL Error Disconnect: ".$dbh->errstr);
1.55 matthew 414: die $dbh->errstr;
415: }
416: ##
417: ## Finished!
1.56 matthew 418: &log(0,"==== Searchcat completed ".localtime()." ====");
1.55 matthew 419: close(LOG);
1.21 www 420:
1.55 matthew 421: &write_type_count();
422: &write_copyright_count();
1.36 www 423:
1.55 matthew 424: exit 0;
1.28 harris41 425:
1.56 matthew 426: ##
427: ## Status logging routine. Inputs: $level, $message
428: ##
429: ## $level 0 should be used for normal output and error messages
430: ##
431: ## $message does not need to end with \n. In the case of errors
432: ## the message should contain as much information as possible to
433: ## help in diagnosing the problem.
434: ##
435: sub log {
436: my ($level,$message)=@_;
437: $level = 0 if (! defined($level));
438: if ($verbose >= $level) {
439: print LOG $message.$/;
440: }
441: }
442:
1.75 raeburn 443: sub portfolio_logging {
444: my (%portlog) = @_;
445: foreach my $key (keys(%portlog)) {
446: if (ref($portlog{$key}) eq 'HASH') {
447: foreach my $item (keys(%{$portlog{$key}})) {
448: &log(0,$portlog{$key}{$item});
449: }
450: }
451: }
452: }
453:
1.69 raeburn 454: sub descend_tree {
1.77 raeburn 455: my ($dom,$dir,$depth,$allportusers,$alldomusers) = @_;
1.69 raeburn 456: if (-d $dir) {
457: opendir(DIR,$dir);
458: my @contents = grep(!/^\./,readdir(DIR));
459: closedir(DIR);
460: $depth ++;
461: foreach my $item (@contents) {
462: if ($depth < 4) {
1.77 raeburn 463: &descend_tree($dom,$dir.'/'.$item,$depth,$allportusers,$alldomusers);
1.69 raeburn 464: } else {
465: if (-e $dir.'/'.$item.'/file_permissions.db') {
1.78 raeburn 466: $$allportusers{$item} = '';
1.77 raeburn 467: }
1.78 raeburn 468: if (-e $dir.'/'.$item.'/passwd') {
1.69 raeburn 469: $$alldomusers{$item} = '';
470: }
471: }
472: }
473: }
474: }
475:
1.55 matthew 476: ########################################################
477: ########################################################
478: ### ###
479: ### File::Find support routines ###
480: ### ###
481: ########################################################
482: ########################################################
483: ##
484: ## &only_meta_files
485: ##
486: ## Called by File::Find.
487: ## Takes a list of files/directories in and returns a list of files/directories
488: ## to search.
489: sub only_meta_files {
490: my @PossibleFiles = @_;
491: my @ChosenFiles;
492: foreach my $file (@PossibleFiles) {
493: if ( ($file =~ /\.meta$/ && # Ends in meta
494: $file !~ /\.\d+\.[^\.]+\.meta$/ # is not for a prior version
1.67 albertel 495: ) || (-d $File::Find::dir."/".$file )) { # directories are okay
1.55 matthew 496: # but we do not want /. or /..
497: push(@ChosenFiles,$file);
498: }
1.38 www 499: }
1.55 matthew 500: return @ChosenFiles;
1.38 www 501: }
502:
1.55 matthew 503: ##
504: ##
505: ## Debugging routines, use these for 'wanted' in the File::Find call
506: ##
507: sub print_filename {
508: my ($file) = $_;
509: my $fullfilename = $File::Find::name;
1.56 matthew 510: if ($debug) {
511: if (-d $file) {
512: &log(5," Got directory ".$fullfilename);
513: } else {
514: &log(5," Got file ".$fullfilename);
515: }
1.38 www 516: }
1.55 matthew 517: $_=$file;
1.38 www 518: }
1.28 harris41 519:
1.55 matthew 520: sub log_metadata {
521: my ($file) = $_;
522: my $fullfilename = $File::Find::name;
523: return if (-d $fullfilename); # No need to do anything here for directories
1.56 matthew 524: if ($debug) {
525: &log(6,$fullfilename);
1.69 raeburn 526: my $ref = &metadata($fullfilename);
1.56 matthew 527: if (! defined($ref)) {
528: &log(6," No data");
529: return;
530: }
531: while (my($key,$value) = each(%$ref)) {
532: &log(6," ".$key." => ".$value);
533: }
534: &count_copyright($ref->{'copyright'});
1.55 matthew 535: }
536: $_=$file;
1.31 harris41 537: }
1.21 www 538:
1.55 matthew 539: ##
540: ## process_meta_file
541: ## Called by File::Find.
542: ## Only input is the filename in $_.
543: sub process_meta_file {
544: my ($file) = $_;
1.56 matthew 545: my $filename = $File::Find::name; # full filename
1.55 matthew 546: return if (-d $filename); # No need to do anything here for directories
547: #
1.56 matthew 548: &log(3,$filename) if ($debug);
1.55 matthew 549: #
1.69 raeburn 550: my $ref = &metadata($filename);
1.55 matthew 551: #
552: # $url is the original file url, not the metadata file
1.61 matthew 553: my $target = $filename;
554: $target =~ s/\.meta$//;
555: my $url='/res/'.&declutter($target);
1.56 matthew 556: &log(3," ".$url) if ($debug);
1.55 matthew 557: #
558: # Ignore some files based on their metadata
559: if ($ref->{'obsolete'}) {
1.56 matthew 560: &log(3,"obsolete") if ($debug);
1.55 matthew 561: return;
562: }
563: &count_copyright($ref->{'copyright'});
564: if ($ref->{'copyright'} eq 'private') {
1.56 matthew 565: &log(3,"private") if ($debug);
1.55 matthew 566: return;
567: }
568: #
569: # Find the dynamic metadata
570: my %dyn;
571: if ($url=~ m:/default$:) {
572: $url=~ s:/default$:/:;
1.56 matthew 573: &log(3,"Skipping dynamic data") if ($debug);
1.55 matthew 574: } else {
1.56 matthew 575: &log(3,"Retrieving dynamic data") if ($debug);
576: %dyn=&get_dynamic_metadata($url);
1.55 matthew 577: &count_type($url);
578: }
1.75 raeburn 579: &LONCAPA::lonmetadata::getfiledates($ref,$target);
1.55 matthew 580: #
581: my %Data = (
582: %$ref,
583: %dyn,
584: 'url'=>$url,
585: 'version'=>'current');
1.56 matthew 586: if (! $simulate) {
1.69 raeburn 587: my ($count,$err) =
588: &LONCAPA::lonmetadata::store_metadata($dbh,$newnames{'metadata'},
589: 'metadata',\%Data);
1.56 matthew 590: if ($err) {
591: &log(0,"MySQL Error Insert: ".$err);
592: }
593: if ($count < 1) {
594: &log(0,"Unable to insert record into MySQL database for $url");
595: }
1.55 matthew 596: }
597: #
598: # Reset $_ before leaving
599: $_ = $file;
600: }
601:
602: ########################################################
603: ########################################################
604: ### ###
605: ### &metadata($uri) ###
606: ### Retrieve metadata for the given file ###
607: ### ###
608: ########################################################
609: ########################################################
610: sub metadata {
1.69 raeburn 611: my ($uri) = @_;
1.55 matthew 612: my %metacache=();
613: $uri=&declutter($uri);
614: my $filename=$uri;
615: $uri=~s/\.meta$//;
616: $uri='';
617: if ($filename !~ /\.meta$/) {
618: $filename.='.meta';
619: }
1.75 raeburn 620: my $metastring =
621: &LONCAPA::lonmetadata::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$filename);
1.55 matthew 622: return undef if (! defined($metastring));
623: my $parser=HTML::TokeParser->new(\$metastring);
624: my $token;
625: while ($token=$parser->get_token) {
626: if ($token->[0] eq 'S') {
627: my $entry=$token->[1];
628: my $unikey=$entry;
629: if (defined($token->[2]->{'part'})) {
630: $unikey.='_'.$token->[2]->{'part'};
631: }
632: if (defined($token->[2]->{'name'})) {
633: $unikey.='_'.$token->[2]->{'name'};
634: }
635: if ($metacache{$uri.'keys'}) {
636: $metacache{$uri.'keys'}.=','.$unikey;
637: } else {
638: $metacache{$uri.'keys'}=$unikey;
639: }
640: foreach ( @{$token->[3]}) {
641: $metacache{$uri.''.$unikey.'.'.$_}=$token->[2]->{$_};
1.69 raeburn 642: }
1.55 matthew 643: if (! ($metacache{$uri.''.$unikey}=$parser->get_text('/'.$entry))){
644: $metacache{$uri.''.$unikey} =
645: $metacache{$uri.''.$unikey.'.default'};
646: }
647: } # End of ($token->[0] eq 'S')
648: }
649: return \%metacache;
1.31 harris41 650: }
1.28 harris41 651:
1.55 matthew 652: ########################################################
653: ########################################################
654: ### ###
655: ### Dynamic Metadata ###
656: ### ###
657: ########################################################
658: ########################################################
1.56 matthew 659: ##
1.58 www 660: ## Dynamic metadata description (incomplete)
661: ##
662: ## For a full description of all fields,
663: ## see LONCAPA::lonmetadata
1.56 matthew 664: ##
665: ## Field Type
666: ##-----------------------------------------------------------
667: ## count integer
668: ## course integer
1.58 www 669: ## course_list comma separated list of course ids
1.56 matthew 670: ## avetries real
1.58 www 671: ## avetries_list comma separated list of real numbers
1.56 matthew 672: ## stdno real
1.58 www 673: ## stdno_list comma separated list of real numbers
1.56 matthew 674: ## usage integer
1.58 www 675: ## usage_list comma separated list of resources
1.56 matthew 676: ## goto scalar
1.58 www 677: ## goto_list comma separated list of resources
1.56 matthew 678: ## comefrom scalar
1.58 www 679: ## comefrom_list comma separated list of resources
1.56 matthew 680: ## difficulty real
1.58 www 681: ## difficulty_list comma separated list of real numbers
1.56 matthew 682: ## sequsage scalar
1.58 www 683: ## sequsage_list comma separated list of resources
1.56 matthew 684: ## clear real
685: ## technical real
686: ## correct real
687: ## helpful real
688: ## depth real
689: ## comments html of all the comments made
690: ##
691: {
692:
693: my %DynamicData;
694: my %Counts;
695:
696: sub process_dynamic_metadata {
697: my ($user,$dom) = @_;
698: undef(%DynamicData);
699: undef(%Counts);
700: #
701: my $prodir = &propath($dom,$user);
1.55 matthew 702: #
1.56 matthew 703: # Read in the dynamic metadata
1.55 matthew 704: my %evaldata;
705: if (! tie(%evaldata,'GDBM_File',
706: $prodir.'/nohist_resevaldata.db',&GDBM_READER(),0640)) {
1.56 matthew 707: return 0;
1.55 matthew 708: }
1.56 matthew 709: #
1.57 matthew 710: %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
1.55 matthew 711: untie(%evaldata);
1.62 matthew 712: $DynamicData{'domain'} = $dom;
1.64 albertel 713: #print('user = '.$user.' domain = '.$dom.$/);
1.56 matthew 714: #
715: # Read in the access count data
716: &log(7,'Reading access count data') if ($debug);
717: my %countdata;
718: if (! tie(%countdata,'GDBM_File',
719: $prodir.'/nohist_accesscount.db',&GDBM_READER(),0640)) {
720: return 0;
721: }
722: while (my ($key,$count) = each(%countdata)) {
723: next if ($key !~ /^$dom/);
724: $key = &unescape($key);
725: &log(8,' Count '.$key.' = '.$count) if ($debug);
726: $Counts{$key}=$count;
727: }
728: untie(%countdata);
729: if ($debug) {
730: &log(7,scalar(keys(%Counts)).
731: " Counts read for ".$user."@".$dom);
732: &log(7,scalar(keys(%DynamicData)).
733: " Dynamic metadata read for ".$user."@".$dom);
734: }
735: #
736: return 1;
737: }
738:
739: sub get_dynamic_metadata {
740: my ($url) = @_;
741: $url =~ s:^/res/::;
1.57 matthew 742: my %data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
743: \%DynamicData);
1.56 matthew 744: # find the count
745: $data{'count'} = $Counts{$url};
746: #
747: # Log the dynamic metadata
748: if ($debug) {
749: while (my($k,$v)=each(%data)) {
750: &log(8," ".$k." => ".$v);
751: }
1.44 www 752: }
1.56 matthew 753: return %data;
1.30 www 754: }
1.28 harris41 755:
1.56 matthew 756: } # End of %DynamicData and %Counts scope
757:
1.55 matthew 758: ########################################################
759: ########################################################
760: ### ###
761: ### Counts ###
762: ### ###
763: ########################################################
764: ########################################################
765: {
1.1 harris41 766:
1.55 matthew 767: my %countext;
1.15 harris41 768:
1.55 matthew 769: sub count_type {
770: my $file=shift;
771: $file=~/\.(\w+)$/;
772: my $ext=lc($1);
773: $countext{$ext}++;
1.31 harris41 774: }
1.1 harris41 775:
1.55 matthew 776: sub write_type_count {
777: open(RESCOUNT,'>/home/httpd/html/lon-status/rescount.txt');
778: while (my ($extension,$count) = each(%countext)) {
779: print RESCOUNT $extension.'='.$count.'&';
1.47 www 780: }
1.55 matthew 781: print RESCOUNT 'time='.time."\n";
782: close(RESCOUNT);
1.31 harris41 783: }
1.27 www 784:
1.55 matthew 785: } # end of scope for %countext
1.34 matthew 786:
1.55 matthew 787: {
1.34 matthew 788:
1.55 matthew 789: my %copyrights;
1.44 www 790:
1.55 matthew 791: sub count_copyright {
792: $copyrights{@_[0]}++;
1.31 harris41 793: }
1.33 matthew 794:
1.55 matthew 795: sub write_copyright_count {
796: open(COPYCOUNT,'>/home/httpd/html/lon-status/copyrightcount.txt');
797: while (my ($copyright,$count) = each(%copyrights)) {
798: print COPYCOUNT $copyright.'='.$count.'&';
1.31 harris41 799: }
1.55 matthew 800: print COPYCOUNT 'time='.time."\n";
801: close(COPYCOUNT);
1.31 harris41 802: }
1.28 harris41 803:
1.55 matthew 804: } # end of scope for %copyrights
1.28 harris41 805:
1.55 matthew 806: ########################################################
807: ########################################################
808: ### ###
809: ### Miscellanous Utility Routines ###
810: ### ###
811: ########################################################
812: ########################################################
813: ##
814: ## &ishome($username)
815: ## Returns 1 if $username is a LON-CAPA author, 0 otherwise
816: ## (copied from lond, modification of the return value)
1.31 harris41 817: sub ishome {
818: my $author=shift;
1.76 albertel 819: $author=~s{/home/httpd/html/res/([^/]*)/([^/]*).*}{$1/$2};
1.31 harris41 820: my ($udom,$uname)=split(/\//,$author);
821: my $proname=propath($udom,$uname);
822: if (-e $proname) {
823: return 1;
824: } else {
825: return 0;
826: }
827: }
1.28 harris41 828:
1.55 matthew 829: ##
830: ## &declutter($filename)
831: ## Given a filename, returns a url for the filename.
832: sub declutter {
833: my $thisfn=shift;
1.63 matthew 834: $thisfn=~s/^$Apache::lonnet::perlvar{'lonDocRoot'}//;
1.55 matthew 835: $thisfn=~s/^\///;
836: $thisfn=~s/^res\///;
837: return $thisfn;
1.31 harris41 838: }
1.28 harris41 839:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>