File:  [LON-CAPA] / loncom / metadata_database / searchcat.pl
Revision 1.85: download - view: text, annotated - select for diffs
Sun Jun 19 04:28:08 2016 UTC (7 years, 10 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Course Editor has "Standard Problem" item (Grading tab) for creation
  of new problem in user's Authoring Space, or in a Course "Authoring" Space.
- Course Editor has "Import from Course Resources" item (Import tab) to
  import published content from Course "Authoring" Space.
- Course "Authoring" Space
  - default.rights -- course-only access
  - quota is shared with content uploaded directly to course
  - content only browsable in course context
  - metadata not included in searchable meatdata MySQL table
CVs: ----------------------------------------------------------------------

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

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