File:  [LON-CAPA] / loncom / metadata_database / searchcat.pl
Revision 1.84: download - view: text, annotated - select for diffs
Sun Jan 31 21:25:49 2016 UTC (8 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Score upload form supports identification of a user based on clicker ID,
  for Course Coordinators who prefer not to use LON-CAPA's in-built
  "Process Clicker" utility.
- clickers.db file on a library server contains key = value pairs, where key
  is (escaped) clicker ID, and value is (escaped) comma-separated list of
  usernames who registered that particular clicker ID.
- bi-nightly run of searchcat.pl will update clickers.db file.

    1: #!/usr/bin/perl
    2: # The LearningOnline Network
    3: # searchcat.pl "Search Catalog" batch script
    4: #
    5: # $Id: searchcat.pl,v 1.84 2016/01/31 21:25:49 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   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: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   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
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: ###
   30: 
   31: =pod
   32: 
   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'
   59: F<nohist_resevaldata.db> database file in order to store it in MySQL.
   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
   66: 
   67: use strict;
   68: use DBI;
   69: use lib '/home/httpd/lib/perl/';
   70: use LONCAPA::lonmetadata;
   71: use LONCAPA;
   72: use Getopt::Long;
   73: use IO::File;
   74: use HTML::TokeParser;
   75: use GDBM_File;
   76: use POSIX qw(strftime mktime);
   77: use Mail::Send;
   78: use Apache::loncommon();
   79: 
   80: use Apache::lonnet();
   81: 
   82: use File::Find;
   83: 
   84: #
   85: # Set up configuration options
   86: my ($simulate,$oneuser,$help,$verbose,$logfile,$debug);
   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: 
  121: ##
  122: ## Use variables for table names so we can test this routine a little easier
  123: my %oldnames = (
  124:                  'metadata'    => 'metadata',
  125:                  'portfolio'   => 'portfolio_metadata',
  126:                  'access'      => 'portfolio_access',
  127:                  'addedfields' => 'portfolio_addedfields',
  128:                  'allusers'    => 'allusers',
  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: }
  136: 
  137: #
  138: # Only run if machine is a library server
  139: exit if ($Apache::lonnet::perlvar{'lonRole'} ne 'library');
  140: my $hostid = $Apache::lonnet::perlvar{'lonHostID'};
  141: 
  142: #
  143: #  Make sure this process is running from user=www
  144: my $wwwid=getpwnam('www');
  145: if ($wwwid!=$<) {
  146:     my $emailto="$Apache::lonnet::perlvar{'lonAdmEMail'},$Apache::lonnet::perlvar{'lonSysEMail'}";
  147:     my $subj="LON: $Apache::lonnet::perlvar{'lonHostID'} User ID mismatch";
  148:     system("echo 'User ID mismatch. searchcat.pl must be run as user www.' |\
  149:  mail -s '$subj' $emailto > /dev/null");
  150:     exit 1;
  151: }
  152: #
  153: # Let people know we are running
  154: open(LOG,'>>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/logs/searchcat.log');
  155: &log(0,'==== Searchcat Run '.localtime()."====");
  156: 
  157: 
  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: }
  163: #
  164: # Connect to database
  165: my $dbh;
  166: if (! ($dbh = DBI->connect("DBI:mysql:loncapa","www",$Apache::lonnet::perlvar{'lonSqlAccess'},
  167:                           { RaiseError =>0,PrintError=>0}))) {
  168:     &log(0,"Cannot connect to database!");
  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)
  173: foreach my $key (keys(%newnames)) {
  174:     if ($newnames{$key} ne '') {
  175:         $dbh->do('DROP TABLE IF EXISTS '.$newnames{$key});
  176:     }
  177: }
  178: 
  179: #
  180: # Create the new metadata, portfolio and allusers tables
  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:     }
  192: }
  193: 
  194: #
  195: # find out which users we need to examine
  196: my @domains = sort(&Apache::lonnet::current_machine_domains());
  197: &log(9,'domains ="'.join('","',@domains).'"');
  198: 
  199: foreach my $dom (@domains) {
  200:     &log(9,'domain = '.$dom);
  201:     opendir(RESOURCES,"$Apache::lonnet::perlvar{'lonDocRoot'}/res/$dom");
  202:     my @homeusers = 
  203:         grep {
  204:             &ishome("$Apache::lonnet::perlvar{'lonDocRoot'}/res/$dom/$_");
  205:         } grep { 
  206:             !/^\.\.?$/;
  207:         } readdir(RESOURCES);
  208:     closedir RESOURCES;
  209:     &log(5,'users = '.$dom.':'.join(',',@homeusers));
  210:     #
  211:     if ($oneuser) {
  212:         @homeusers=($oneuser);
  213:     }
  214: 
  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,
  227:               no_chdir   => 1,
  228:              }, join('/',($Apache::lonnet::perlvar{'lonDocRoot'},'res',$dom,$user)) );
  229:     }
  230:     # Search for all users and public portfolio files
  231:     my (%allusers,%portusers,%courses);
  232:     if ($oneuser) {
  233:         %portusers = (
  234:                         $oneuser => '',
  235:                        );
  236:         %allusers = (
  237:                         $oneuser => '',
  238:                        );
  239:         %courses = &courseiddump($dom,'.',1,'.','.',$oneuser,undef,
  240:                                  undef,'.');
  241:     } else {
  242:         # get courseIDs for domain on current machine
  243:         %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.');
  244:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
  245:         &descend_tree($dom,$dir,0,\%portusers,\%allusers);
  246:     }
  247:     foreach my $uname (keys(%portusers)) {
  248:         my $urlstart = '/uploaded/'.$dom.'/'.$uname;
  249:         my $pathstart = &propath($dom,$uname).'/userfiles';
  250:         my $is_course = '';
  251:         if (exists($courses{$dom.'_'.$uname})) {
  252:             $is_course = 1;
  253:         }
  254:         my $curr_perm = &Apache::lonnet::get_portfile_permissions($dom,$uname);
  255:         my %access = &Apache::lonnet::get_access_controls($curr_perm);
  256:         foreach my $file (keys(%access)) {
  257:             my ($group,$url,$fullpath);
  258:             if ($is_course) {
  259:                 ($group, my ($path)) = ($file =~ /^(\w+)(\/.+)$/);
  260:                 $fullpath = $pathstart.'/groups/'.$group.'/portfolio'.$path;
  261:                 $url = $urlstart.'/groups/'.$group.'/portfolio'.$path;
  262:             } else {
  263:                 $fullpath = $pathstart.'/portfolio'.$file;
  264:                 $url = $urlstart.'/portfolio'.$file;
  265:             }
  266:             if (ref($access{$file}) eq 'HASH') {
  267:                 my %portaccesslog = 
  268:                     &LONCAPA::lonmetadata::process_portfolio_access_data($dbh,
  269:                            $simulate,\%newnames,$url,$fullpath,$access{$file});
  270:                 &portfolio_logging(%portaccesslog);
  271:             }
  272:             my %portmetalog = &LONCAPA::lonmetadata::process_portfolio_metadata($dbh,$simulate,\%newnames,$url,$fullpath,$is_course,$dom,$uname,$group);
  273:             &portfolio_logging(%portmetalog);
  274:         }
  275:     }
  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:                  );
  301:     unless ($simulate || $oneuser) {
  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);
  320:             }
  321:         }
  322:     }
  323:     # Update allusers
  324:     foreach my $uname (keys(%allusers)) {
  325:         next if (exists($courses{$dom.'_'.$uname}));
  326:         my %userdata = 
  327:             &Apache::lonnet::get('environment',['firstname','lastname',
  328:                 'middlename','generation','id','permanentemail','clickers'],
  329:                                  $dom,$uname);
  330:         unless ($simulate || $oneuser) {
  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:                             }
  358:                         }
  359:                     }
  360:                 }
  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:                     }
  373:                 }
  374:             }
  375:         }
  376: 
  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:     }
  386:     unless ($simulate || $oneuser) {
  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});
  397:                 }
  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:                     }
  402:                 } else {
  403:                     &log(0,"Error: '$resulthash{$hostid}' occurred when attempting to delete records from $namespace{$key}.db for $dom");
  404:                 }
  405:             }
  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:                         }
  417:                     }
  418:                 } else {
  419:                     foreach my $addid (sort(keys(%{$idstoadd{$key}}))) {
  420:                         $idstoadd{$key}{$addid} =~ s/,$//;
  421:                         $newids{$addid} = $idstoadd{$key}{$addid};
  422:                     }
  423:                 }
  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:                         }
  459:                     }
  460:                 }
  461:             }
  462:         }
  463:         if (keys(%duplicates) > 0) {
  464:             foreach my $id (sort(keys(%duplicates))) {
  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:                 }
  468:             }
  469:         }
  470:     }
  471: }
  472: 
  473: #
  474: # Rename the tables
  475: if (! $simulate) {
  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:         }
  486:     }
  487: }
  488: if (! $dbh->disconnect) {
  489:     &log(0,"MySQL Error Disconnect: ".$dbh->errstr);
  490:     die $dbh->errstr;
  491: }
  492: ##
  493: ## Finished!
  494: &log(0,"==== Searchcat completed ".localtime()." ====");
  495: close(LOG);
  496: 
  497: &write_type_count();
  498: &write_copyright_count();
  499: 
  500: exit 0;
  501: 
  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: 
  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: 
  530: sub descend_tree {
  531:     my ($dom,$dir,$depth,$allportusers,$alldomusers) = @_;
  532:     if (-d $dir) {
  533:         opendir(DIR,$dir);
  534:         my @contents = grep(!/^\./,readdir(DIR));
  535:         closedir(DIR);
  536:         $depth ++;
  537:         foreach my $item (@contents) {
  538:             if (($depth < 4) && (length($item) == 1)) {
  539:                 &descend_tree($dom,$dir.'/'.$item,$depth,$allportusers,$alldomusers);
  540:             } else {
  541:                 if (-e $dir.'/'.$item.'/file_permissions.db') {
  542:                     $$allportusers{$item} = '';
  543:                 }
  544:                 if (-e $dir.'/'.$item.'/passwd') {
  545:                     $$alldomusers{$item} = '';
  546:                 }
  547:             }       
  548:         }
  549:     } 
  550: }
  551: 
  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
  571:              ) || (-d $File::Find::dir."/".$file )) { # directories are okay
  572:                  # but we do not want /. or /..
  573:             push(@ChosenFiles,$file);
  574:         }
  575:     }
  576:     return @ChosenFiles;
  577: }
  578: 
  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;
  586:     if ($debug) {
  587:         if (-d $file) {
  588:             &log(5," Got directory ".$fullfilename);
  589:         } else {
  590:             &log(5," Got file ".$fullfilename);
  591:         }
  592:     }
  593:     $_=$file;
  594: }
  595: 
  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
  600:     if ($debug) {
  601:         &log(6,$fullfilename);
  602:         my $ref = &metadata($fullfilename);
  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'});
  611:     }
  612:     $_=$file;
  613: }
  614: 
  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) = $_;
  621:     my $filename = $File::Find::name; # full filename
  622:     return if (-d $filename); # No need to do anything here for directories
  623:     #
  624:     &log(3,$filename) if ($debug);
  625:     #
  626:     my $ref = &metadata($filename);
  627:     #
  628:     # $url is the original file url, not the metadata file
  629:     my $target = $filename;
  630:     $target =~ s/\.meta$//;
  631:     my $url='/res/'.&declutter($target);
  632:     &log(3,"    ".$url) if ($debug);
  633:     #
  634:     # Ignore some files based on their metadata
  635:     if ($ref->{'obsolete'}) { 
  636:         &log(3,"obsolete") if ($debug);
  637:         return; 
  638:     }
  639:     &count_copyright($ref->{'copyright'});
  640:     if ($ref->{'copyright'} eq 'private') { 
  641:         &log(3,"private") if ($debug);
  642:         return; 
  643:     }
  644:     #
  645:     # Find the dynamic metadata
  646:     my %dyn;
  647:     if ($url=~ m:/default$:) {
  648:         $url=~ s:/default$:/:;
  649:         &log(3,"Skipping dynamic data") if ($debug);
  650:     } else {
  651:         &log(3,"Retrieving dynamic data") if ($debug);
  652:         %dyn=&get_dynamic_metadata($url);
  653:         &count_type($url);
  654:     }
  655:     &LONCAPA::lonmetadata::getfiledates($ref,$target);
  656:     #
  657:     my %Data = (
  658:                 %$ref,
  659:                 %dyn,
  660:                 'url'=>$url,
  661:                 'version'=>'current');
  662:     if (! $simulate) {
  663:         my ($count,$err) = 
  664:           &LONCAPA::lonmetadata::store_metadata($dbh,$newnames{'metadata'},
  665:                                                 'metadata',\%Data);
  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:         }
  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 {
  687:     my ($uri) = @_;
  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:     }
  696:     my $metastring = 
  697:         &LONCAPA::lonmetadata::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$filename);
  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]->{$_};
  718:             }
  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;
  726: }
  727: 
  728: ########################################################
  729: ########################################################
  730: ###                                                  ###
  731: ###    Dynamic Metadata                              ###
  732: ###                                                  ###
  733: ########################################################
  734: ########################################################
  735: ##
  736: ## Dynamic metadata description (incomplete)
  737: ##
  738: ## For a full description of all fields,
  739: ## see LONCAPA::lonmetadata
  740: ##
  741: ##   Field             Type
  742: ##-----------------------------------------------------------
  743: ##   count             integer
  744: ##   course            integer
  745: ##   course_list       comma separated list of course ids
  746: ##   avetries          real                                
  747: ##   avetries_list     comma separated list of real numbers
  748: ##   stdno             real
  749: ##   stdno_list        comma separated list of real numbers
  750: ##   usage             integer   
  751: ##   usage_list        comma separated list of resources
  752: ##   goto              scalar
  753: ##   goto_list         comma separated list of resources
  754: ##   comefrom          scalar
  755: ##   comefrom_list     comma separated list of resources
  756: ##   difficulty        real
  757: ##   difficulty_list   comma separated list of real numbers
  758: ##   sequsage          scalar
  759: ##   sequsage_list     comma separated list of resources
  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);
  778:     #
  779:     # Read in the dynamic metadata
  780:     my %evaldata;
  781:     if (! tie(%evaldata,'GDBM_File',
  782:               $prodir.'/nohist_resevaldata.db',&GDBM_READER(),0640)) {
  783:         return 0;
  784:     }
  785:     #
  786:     %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
  787:     untie(%evaldata);
  788:     $DynamicData{'domain'} = $dom;
  789:     #print('user = '.$user.' domain = '.$dom.$/);
  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/::;
  818:     my %data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
  819:                                                                \%DynamicData);
  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:         }
  828:     }
  829:     return %data;
  830: }
  831: 
  832: } # End of %DynamicData and %Counts scope
  833: 
  834: ########################################################
  835: ########################################################
  836: ###                                                  ###
  837: ###   Counts                                         ###
  838: ###                                                  ###
  839: ########################################################
  840: ########################################################
  841: {
  842: 
  843: my %countext;
  844: 
  845: sub count_type {
  846:     my $file=shift;
  847:     $file=~/\.(\w+)$/;
  848:     my $ext=lc($1);
  849:     $countext{$ext}++;
  850: }
  851: 
  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.'&';
  856:     }
  857:     print RESCOUNT 'time='.time."\n";
  858:     close(RESCOUNT);
  859: }
  860: 
  861: } # end of scope for %countext
  862: 
  863: {
  864: 
  865: my %copyrights;
  866: 
  867: sub count_copyright {
  868:     $copyrights{@_[0]}++;
  869: }
  870: 
  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.'&';
  875:     }
  876:     print COPYCOUNT 'time='.time."\n";
  877:     close(COPYCOUNT);
  878: }
  879: 
  880: } # end of scope for %copyrights
  881: 
  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)
  893: sub ishome {
  894:     my $author=shift;
  895:     $author=~s{/home/httpd/html/res/([^/]*)/([^/]*).*}{$1/$2};
  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: }
  904: 
  905: ##
  906: ## &declutter($filename)
  907: ##   Given a filename, returns a url for the filename.
  908: sub declutter {
  909:     my $thisfn=shift;
  910:     $thisfn=~s/^$Apache::lonnet::perlvar{'lonDocRoot'}//;
  911:     $thisfn=~s/^\///;
  912:     $thisfn=~s/^res\///;
  913:     return $thisfn;
  914: }
  915: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>