File:  [LON-CAPA] / loncom / metadata_database / searchcat.pl
Revision 1.80: download - view: text, annotated - select for diffs
Mon Aug 12 16:52:00 2013 UTC (10 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Domain Configuration (Contacts)
  - Enable/Disable e-mail sent to LON-CAPA when:
    (a) error form is submitted
    (b) domain defaults (authentication type or language) are changed.
  - E-mail recipient can be specified when bi-nightly searchcat.pl detects
    situation where a student/employeeID is used for more than one user in
    server's domain.

    1: #!/usr/bin/perl
    2: # The LearningOnline Network
    3: # searchcat.pl "Search Catalog" batch script
    4: #
    5: # $Id: searchcat.pl,v 1.80 2013/08/12 16:52:00 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: 
   79: use Apache::lonnet();
   80: 
   81: use File::Find;
   82: 
   83: #
   84: # Set up configuration options
   85: my ($simulate,$oneuser,$help,$verbose,$logfile,$debug);
   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: 
  120: ##
  121: ## Use variables for table names so we can test this routine a little easier
  122: my %oldnames = (
  123:                  'metadata'    => 'metadata',
  124:                  'portfolio'   => 'portfolio_metadata',
  125:                  'access'      => 'portfolio_access',
  126:                  'addedfields' => 'portfolio_addedfields',
  127:                  'allusers'    => 'allusers',
  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: }
  135: 
  136: #
  137: # Only run if machine is a library server
  138: exit if ($Apache::lonnet::perlvar{'lonRole'} ne 'library');
  139: my $hostid = $Apache::lonnet::perlvar{'lonHostID'};
  140: 
  141: #
  142: #  Make sure this process is running from user=www
  143: my $wwwid=getpwnam('www');
  144: if ($wwwid!=$<) {
  145:     my $emailto="$Apache::lonnet::perlvar{'lonAdmEMail'},$Apache::lonnet::perlvar{'lonSysEMail'}";
  146:     my $subj="LON: $Apache::lonnet::perlvar{'lonHostID'} User ID mismatch";
  147:     system("echo 'User ID mismatch. searchcat.pl must be run as user www.' |\
  148:  mail -s '$subj' $emailto > /dev/null");
  149:     exit 1;
  150: }
  151: #
  152: # Let people know we are running
  153: open(LOG,'>>'.$Apache::lonnet::perlvar{'lonDaemons'}.'/logs/searchcat.log');
  154: &log(0,'==== Searchcat Run '.localtime()."====");
  155: 
  156: 
  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: }
  162: #
  163: # Connect to database
  164: my $dbh;
  165: if (! ($dbh = DBI->connect("DBI:mysql:loncapa","www",$Apache::lonnet::perlvar{'lonSqlAccess'},
  166:                           { RaiseError =>0,PrintError=>0}))) {
  167:     &log(0,"Cannot connect to database!");
  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)
  172: foreach my $key (keys(%newnames)) {
  173:     if ($newnames{$key} ne '') {
  174:         $dbh->do('DROP TABLE IF EXISTS '.$newnames{$key});
  175:     }
  176: }
  177: 
  178: #
  179: # Create the new metadata, portfolio and allusers tables
  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:     }
  191: }
  192: 
  193: #
  194: # find out which users we need to examine
  195: my @domains = sort(&Apache::lonnet::current_machine_domains());
  196: &log(9,'domains ="'.join('","',@domains).'"');
  197: 
  198: foreach my $dom (@domains) {
  199:     &log(9,'domain = '.$dom);
  200:     opendir(RESOURCES,"$Apache::lonnet::perlvar{'lonDocRoot'}/res/$dom");
  201:     my @homeusers = 
  202:         grep {
  203:             &ishome("$Apache::lonnet::perlvar{'lonDocRoot'}/res/$dom/$_");
  204:         } grep { 
  205:             !/^\.\.?$/;
  206:         } readdir(RESOURCES);
  207:     closedir RESOURCES;
  208:     &log(5,'users = '.$dom.':'.join(',',@homeusers));
  209:     #
  210:     if ($oneuser) {
  211:         @homeusers=($oneuser);
  212:     }
  213: 
  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,
  226:               no_chdir   => 1,
  227:              }, join('/',($Apache::lonnet::perlvar{'lonDocRoot'},'res',$dom,$user)) );
  228:     }
  229:     # Search for all users and public portfolio files
  230:     my (%allusers,%portusers,%courses);
  231:     if ($oneuser) {
  232:         %portusers = (
  233:                         $oneuser => '',
  234:                        );
  235:         %allusers = (
  236:                         $oneuser => '',
  237:                        );
  238:         %courses = &courseiddump($dom,'.',1,'.','.',$oneuser,undef,
  239:                                  undef,'.');
  240:     } else {
  241:         # get courseIDs for domain on current machine
  242:         %courses=&Apache::lonnet::courseiddump($dom,'.',1,'.','.','.',1,[$hostid],'.');
  243:         my $dir = $Apache::lonnet::perlvar{lonUsersDir}.'/'.$dom;
  244:         &descend_tree($dom,$dir,0,\%portusers,\%allusers);
  245:     }
  246:     foreach my $uname (keys(%portusers)) {
  247:         my $urlstart = '/uploaded/'.$dom.'/'.$uname;
  248:         my $pathstart = &propath($dom,$uname).'/userfiles';
  249:         my $is_course = '';
  250:         if (exists($courses{$dom.'_'.$uname})) {
  251:             $is_course = 1;
  252:         }
  253:         my $curr_perm = &Apache::lonnet::get_portfile_permissions($dom,$uname);
  254:         my %access = &Apache::lonnet::get_access_controls($curr_perm);
  255:         foreach my $file (keys(%access)) {
  256:             my ($group,$url,$fullpath);
  257:             if ($is_course) {
  258:                 ($group, my ($path)) = ($file =~ /^(\w+)(\/.+)$/);
  259:                 $fullpath = $pathstart.'/groups/'.$group.'/portfolio'.$path;
  260:                 $url = $urlstart.'/groups/'.$group.'/portfolio'.$path;
  261:             } else {
  262:                 $fullpath = $pathstart.'/portfolio'.$file;
  263:                 $url = $urlstart.'/portfolio'.$file;
  264:             }
  265:             if (ref($access{$file}) eq 'HASH') {
  266:                 my %portaccesslog = 
  267:                     &LONCAPA::lonmetadata::process_portfolio_access_data($dbh,
  268:                            $simulate,\%newnames,$url,$fullpath,$access{$file});
  269:                 &portfolio_logging(%portaccesslog);
  270:             }
  271:             my %portmetalog = &LONCAPA::lonmetadata::process_portfolio_metadata($dbh,$simulate,\%newnames,$url,$fullpath,$is_course,$dom,$uname,$group);
  272:             &portfolio_logging(%portmetalog);
  273:         }
  274:     }
  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:     }
  290:     # Update allusers
  291:     foreach my $uname (keys(%allusers)) {
  292:         next if (exists($courses{$dom.'_'.$uname}));
  293:         my %userdata = 
  294:             &Apache::lonnet::get('environment',['firstname','lastname',
  295:                 'middlename','generation','id','permanentemail'],$dom,$uname);
  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:         
  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:     }
  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) {
  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:                     }
  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:     }
  395: }
  396: 
  397: #
  398: # Rename the tables
  399: if (! $simulate) {
  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:         }
  410:     }
  411: }
  412: if (! $dbh->disconnect) {
  413:     &log(0,"MySQL Error Disconnect: ".$dbh->errstr);
  414:     die $dbh->errstr;
  415: }
  416: ##
  417: ## Finished!
  418: &log(0,"==== Searchcat completed ".localtime()." ====");
  419: close(LOG);
  420: 
  421: &write_type_count();
  422: &write_copyright_count();
  423: 
  424: exit 0;
  425: 
  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: 
  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: 
  454: sub descend_tree {
  455:     my ($dom,$dir,$depth,$allportusers,$alldomusers) = @_;
  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) {
  463:                 &descend_tree($dom,$dir.'/'.$item,$depth,$allportusers,$alldomusers);
  464:             } else {
  465:                 if (-e $dir.'/'.$item.'/file_permissions.db') {
  466:                     $$allportusers{$item} = '';
  467:                 }
  468:                 if (-e $dir.'/'.$item.'/passwd') {
  469:                     $$alldomusers{$item} = '';
  470:                 }
  471:             }       
  472:         }
  473:     } 
  474: }
  475: 
  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
  495:              ) || (-d $File::Find::dir."/".$file )) { # directories are okay
  496:                  # but we do not want /. or /..
  497:             push(@ChosenFiles,$file);
  498:         }
  499:     }
  500:     return @ChosenFiles;
  501: }
  502: 
  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;
  510:     if ($debug) {
  511:         if (-d $file) {
  512:             &log(5," Got directory ".$fullfilename);
  513:         } else {
  514:             &log(5," Got file ".$fullfilename);
  515:         }
  516:     }
  517:     $_=$file;
  518: }
  519: 
  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
  524:     if ($debug) {
  525:         &log(6,$fullfilename);
  526:         my $ref = &metadata($fullfilename);
  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'});
  535:     }
  536:     $_=$file;
  537: }
  538: 
  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) = $_;
  545:     my $filename = $File::Find::name; # full filename
  546:     return if (-d $filename); # No need to do anything here for directories
  547:     #
  548:     &log(3,$filename) if ($debug);
  549:     #
  550:     my $ref = &metadata($filename);
  551:     #
  552:     # $url is the original file url, not the metadata file
  553:     my $target = $filename;
  554:     $target =~ s/\.meta$//;
  555:     my $url='/res/'.&declutter($target);
  556:     &log(3,"    ".$url) if ($debug);
  557:     #
  558:     # Ignore some files based on their metadata
  559:     if ($ref->{'obsolete'}) { 
  560:         &log(3,"obsolete") if ($debug);
  561:         return; 
  562:     }
  563:     &count_copyright($ref->{'copyright'});
  564:     if ($ref->{'copyright'} eq 'private') { 
  565:         &log(3,"private") if ($debug);
  566:         return; 
  567:     }
  568:     #
  569:     # Find the dynamic metadata
  570:     my %dyn;
  571:     if ($url=~ m:/default$:) {
  572:         $url=~ s:/default$:/:;
  573:         &log(3,"Skipping dynamic data") if ($debug);
  574:     } else {
  575:         &log(3,"Retrieving dynamic data") if ($debug);
  576:         %dyn=&get_dynamic_metadata($url);
  577:         &count_type($url);
  578:     }
  579:     &LONCAPA::lonmetadata::getfiledates($ref,$target);
  580:     #
  581:     my %Data = (
  582:                 %$ref,
  583:                 %dyn,
  584:                 'url'=>$url,
  585:                 'version'=>'current');
  586:     if (! $simulate) {
  587:         my ($count,$err) = 
  588:           &LONCAPA::lonmetadata::store_metadata($dbh,$newnames{'metadata'},
  589:                                                 'metadata',\%Data);
  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:         }
  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 {
  611:     my ($uri) = @_;
  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:     }
  620:     my $metastring = 
  621:         &LONCAPA::lonmetadata::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$filename);
  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]->{$_};
  642:             }
  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;
  650: }
  651: 
  652: ########################################################
  653: ########################################################
  654: ###                                                  ###
  655: ###    Dynamic Metadata                              ###
  656: ###                                                  ###
  657: ########################################################
  658: ########################################################
  659: ##
  660: ## Dynamic metadata description (incomplete)
  661: ##
  662: ## For a full description of all fields,
  663: ## see LONCAPA::lonmetadata
  664: ##
  665: ##   Field             Type
  666: ##-----------------------------------------------------------
  667: ##   count             integer
  668: ##   course            integer
  669: ##   course_list       comma separated list of course ids
  670: ##   avetries          real                                
  671: ##   avetries_list     comma separated list of real numbers
  672: ##   stdno             real
  673: ##   stdno_list        comma separated list of real numbers
  674: ##   usage             integer   
  675: ##   usage_list        comma separated list of resources
  676: ##   goto              scalar
  677: ##   goto_list         comma separated list of resources
  678: ##   comefrom          scalar
  679: ##   comefrom_list     comma separated list of resources
  680: ##   difficulty        real
  681: ##   difficulty_list   comma separated list of real numbers
  682: ##   sequsage          scalar
  683: ##   sequsage_list     comma separated list of resources
  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);
  702:     #
  703:     # Read in the dynamic metadata
  704:     my %evaldata;
  705:     if (! tie(%evaldata,'GDBM_File',
  706:               $prodir.'/nohist_resevaldata.db',&GDBM_READER(),0640)) {
  707:         return 0;
  708:     }
  709:     #
  710:     %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
  711:     untie(%evaldata);
  712:     $DynamicData{'domain'} = $dom;
  713:     #print('user = '.$user.' domain = '.$dom.$/);
  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/::;
  742:     my %data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
  743:                                                                \%DynamicData);
  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:         }
  752:     }
  753:     return %data;
  754: }
  755: 
  756: } # End of %DynamicData and %Counts scope
  757: 
  758: ########################################################
  759: ########################################################
  760: ###                                                  ###
  761: ###   Counts                                         ###
  762: ###                                                  ###
  763: ########################################################
  764: ########################################################
  765: {
  766: 
  767: my %countext;
  768: 
  769: sub count_type {
  770:     my $file=shift;
  771:     $file=~/\.(\w+)$/;
  772:     my $ext=lc($1);
  773:     $countext{$ext}++;
  774: }
  775: 
  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.'&';
  780:     }
  781:     print RESCOUNT 'time='.time."\n";
  782:     close(RESCOUNT);
  783: }
  784: 
  785: } # end of scope for %countext
  786: 
  787: {
  788: 
  789: my %copyrights;
  790: 
  791: sub count_copyright {
  792:     $copyrights{@_[0]}++;
  793: }
  794: 
  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.'&';
  799:     }
  800:     print COPYCOUNT 'time='.time."\n";
  801:     close(COPYCOUNT);
  802: }
  803: 
  804: } # end of scope for %copyrights
  805: 
  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)
  817: sub ishome {
  818:     my $author=shift;
  819:     $author=~s{/home/httpd/html/res/([^/]*)/([^/]*).*}{$1/$2};
  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: }
  828: 
  829: ##
  830: ## &declutter($filename)
  831: ##   Given a filename, returns a url for the filename.
  832: sub declutter {
  833:     my $thisfn=shift;
  834:     $thisfn=~s/^$Apache::lonnet::perlvar{'lonDocRoot'}//;
  835:     $thisfn=~s/^\///;
  836:     $thisfn=~s/^res\///;
  837:     return $thisfn;
  838: }
  839: 

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