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