Annotation of loncom/metadata_database/searchcat.pl, revision 1.62

1.1       harris41    1: #!/usr/bin/perl
                      2: # The LearningOnline Network
                      3: # searchcat.pl "Search Catalog" batch script
1.16      harris41    4: #
1.62    ! matthew     5: # $Id: searchcat.pl,v 1.61 2005/03/09 18:22:19 matthew Exp $
1.16      harris41    6: #
                      7: # Copyright Michigan State University Board of Trustees
                      8: #
1.29      albertel    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
1.16      harris41   10: #
1.29      albertel   11: # LON-CAPA is free software; you can redistribute it and/or modify
1.16      harris41   12: # it under the terms of the GNU General Public License as published by
                     13: # the Free Software Foundation; either version 2 of the License, or
                     14: # (at your option) any later version.
                     15: #
1.29      albertel   16: # LON-CAPA is distributed in the hope that it will be useful,
1.16      harris41   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     19: # GNU General Public License for more details.
                     20: #
                     21: # You should have received a copy of the GNU General Public License
1.29      albertel   22: # along with LON-CAPA; if not, write to the Free Software
1.16      harris41   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     24: #
                     25: # /home/httpd/html/adm/gpl.txt
                     26: #
1.29      albertel   27: # http://www.lon-capa.org/
1.16      harris41   28: #
                     29: ###
1.33      matthew    30: 
1.32      www        31: =pod
1.1       harris41   32: 
1.32      www        33: =head1 NAME
                     34: 
                     35: B<searchcat.pl> - put authoritative filesystem data into sql database.
                     36: 
                     37: =head1 SYNOPSIS
                     38: 
                     39: Ordinarily this script is to be called from a loncapa cron job
                     40: (CVS source location: F<loncapa/loncom/cron/loncapa>; typical
                     41: filesystem installation location: F</etc/cron.d/loncapa>).
                     42: 
                     43: Here is the cron job entry.
                     44: 
                     45: C<# Repopulate and refresh the metadata database used for the search catalog.>
                     46: C<10 1 * * 7    www    /home/httpd/perl/searchcat.pl>
                     47: 
                     48: This script only allows itself to be run as the user C<www>.
                     49: 
                     50: =head1 DESCRIPTION
                     51: 
                     52: This script goes through a loncapa resource directory and gathers metadata.
                     53: The metadata is entered into a SQL database.
                     54: 
                     55: This script also does general database maintenance such as reformatting
                     56: the C<loncapa:metadata> table if it is deprecated.
                     57: 
                     58: This script evaluates dynamic metadata from the authors'
1.48      www        59: F<nohist_resevaldata.db> database file in order to store it in MySQL.
1.32      www        60: 
                     61: This script is playing an increasingly important role for a loncapa
                     62: library server.  The proper operation of this script is critical for a smooth
                     63: and correct user experience.
                     64: 
                     65: =cut
1.1       harris41   66: 
1.45      www        67: use strict;
                     68: 
1.55      matthew    69: use DBI;
1.17      harris41   70: use lib '/home/httpd/lib/perl/';
                     71: use LONCAPA::Configuration;
1.55      matthew    72: use LONCAPA::lonmetadata;
1.17      harris41   73: 
1.56      matthew    74: use Getopt::Long;
1.1       harris41   75: use IO::File;
                     76: use HTML::TokeParser;
1.21      www        77: use GDBM_File;
1.24      www        78: use POSIX qw(strftime mktime);
1.56      matthew    79: 
1.62    ! matthew    80: use Sys::Hostname;
        !            81: 
1.55      matthew    82: use File::Find;
1.1       harris41   83: 
1.56      matthew    84: #
                     85: # Set up configuration options
1.62    ! matthew    86: my ($simulate,$oneuser,$help,$verbose,$logfile,$debug,$multidom);
1.56      matthew    87: GetOptions (
                     88:             'help'     => \$help,
                     89:             'simulate' => \$simulate,
                     90:             'only=s'   => \$oneuser,
                     91:             'verbose=s'  => \$verbose,
                     92:             'debug' => \$debug,
1.62    ! matthew    93:             'multi_domain'  => \$multidom,
1.56      matthew    94:             );
                     95: 
                     96: if ($help) {
                     97:     print <<"ENDHELP";
                     98: $0
                     99: Rebuild and update the LON-CAPA metadata database. 
                    100: Options:
                    101:     -help          Print this help
                    102:     -simulate      Do not modify the database.
                    103:     -only=user     Only compute for the given user.  Implies -simulate   
                    104:     -verbose=val   Sets logging level, val must be a number
                    105:     -debug         Turns on debugging output
1.62    ! matthew   106:     -multi_domain  Parse the hosts.tab file domain(s) to use.
1.56      matthew   107: ENDHELP
                    108:     exit 0;
                    109: }
                    110: 
                    111: if (! defined($debug)) {
                    112:     $debug = 0;
                    113: }
                    114: 
                    115: if (! defined($verbose)) {
                    116:     $verbose = 0;
                    117: }
                    118: 
                    119: if (defined($oneuser)) {
                    120:     $simulate=1;
                    121: }
                    122: 
1.55      matthew   123: ##
                    124: ## Use variables for table names so we can test this routine a little easier
                    125: my $oldname = 'metadata';
1.59      matthew   126: my $newname = 'newmetadata'.$$; # append pid to have unique temporary table
1.45      www       127: 
1.55      matthew   128: #
                    129: # Read loncapa_apache.conf and loncapa.conf
                    130: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
                    131: my %perlvar=%{$perlvarref};
                    132: undef $perlvarref;
                    133: delete $perlvar{'lonReceipt'}; # remove since sensitive (really?) & not needed
                    134: #
                    135: # Only run if machine is a library server
                    136: exit if ($perlvar{'lonRole'} ne 'library');
                    137: #
                    138: #  Make sure this process is running from user=www
                    139: my $wwwid=getpwnam('www');
                    140: if ($wwwid!=$<) {
                    141:     my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
                    142:     my $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
                    143:     system("echo 'User ID mismatch. searchcat.pl must be run as user www.' |\
                    144:  mailto $emailto -s '$subj' > /dev/null");
                    145:     exit 1;
                    146: }
                    147: #
                    148: # Let people know we are running
1.60      matthew   149: open(LOG,'>>'.$perlvar{'lonDaemons'}.'/logs/searchcat.log');
1.56      matthew   150: &log(0,'==== Searchcat Run '.localtime()."====");
1.57      matthew   151: 
                    152: 
1.56      matthew   153: if ($debug) {
                    154:     &log(0,'simulating') if ($simulate);
                    155:     &log(0,'only processing user '.$oneuser) if ($oneuser);
                    156:     &log(0,'verbosity level = '.$verbose);
                    157: }
1.55      matthew   158: #
                    159: # Connect to database
                    160: my $dbh;
                    161: if (! ($dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},
                    162:                           { RaiseError =>0,PrintError=>0}))) {
1.56      matthew   163:     &log(0,"Cannot connect to database!");
1.55      matthew   164:     die "MySQL Error: Cannot connect to database!\n";
                    165: }
                    166: # This can return an error and still be okay, so we do not bother checking.
                    167: # (perhaps it should be more robust and check for specific errors)
                    168: $dbh->do('DROP TABLE IF EXISTS '.$newname);
                    169: #
                    170: # Create the new table
                    171: my $request = &LONCAPA::lonmetadata::create_metadata_storage($newname);
                    172: $dbh->do($request);
                    173: if ($dbh->err) {
                    174:     $dbh->disconnect();
1.56      matthew   175:     &log(0,"MySQL Error Create: ".$dbh->errstr);
1.55      matthew   176:     die $dbh->errstr;
                    177: }
                    178: #
                    179: # find out which users we need to examine
1.62    ! matthew   180: my @domains;
        !           181: if (defined($multidom)) {
        !           182:     &log(1,'====multi domain setup====');
        !           183:     # Peek into the hosts.tab and look for matches of our hostname
        !           184:     my $host = hostname();
        !           185:     &log(9,'hostname = "'.$host.'"');
        !           186:     open(HOSTFILE,$perlvar{'lonTabDir'}.'/hosts.tab') || 
        !           187:         die ("Unable to determine domain(s) of multi-domain server");
        !           188:     my %domains;
        !           189:     while (<HOSTFILE>) {
        !           190:         next if (/^\#/);
        !           191:         next if (!/:\Q$host\E/);
        !           192:         &log(9,$_);
        !           193:         $domains{(split(':',$_))[1]}++;
        !           194:     }
        !           195:     close HOSTFILE;
        !           196:     @domains = sort(keys(%domains));
        !           197:     &log(9,join(',',@domains));
        !           198:     if (! scalar(@domains)) {
        !           199:         die ("Unable to find any domains in the hosts.tab that match ".$host);
        !           200:     }
        !           201: } else {
        !           202:     push(@domains,$perlvar{'lonDefDomain'});
        !           203: }
        !           204: 
        !           205: foreach my $dom (@domains) {
        !           206:     &log(9,'domain = '.$dom);
        !           207:     opendir(RESOURCES,"$perlvar{'lonDocRoot'}/res/$dom");
        !           208:     my @homeusers = 
        !           209:         grep {
        !           210:             &ishome("$perlvar{'lonDocRoot'}/res/$dom/$_");
        !           211:         } grep { 
        !           212:             !/^\.\.?$/;
        !           213:         } readdir(RESOURCES);
        !           214:     closedir RESOURCES;
        !           215:     &log(5,'users = '.$dom.':'.join(',',@homeusers));
        !           216:     #
        !           217:     if ($oneuser) {
        !           218:         @homeusers=($oneuser);
        !           219:     }
        !           220:     #
        !           221:     # Loop through the users
        !           222:     foreach my $user (@homeusers) {
        !           223:         &log(0,"=== User: ".$user);
        !           224:         &process_dynamic_metadata($user,$dom);
        !           225:         #
        !           226:         # Use File::Find to get the files we need to read/modify
        !           227:         find(
        !           228:              {preprocess => \&only_meta_files,
        !           229:               #wanted     => \&print_filename,
        !           230:               #wanted     => \&log_metadata,
        !           231:               wanted     => \&process_meta_file,
        !           232:              }, join('/',($perlvar{'lonDocRoot'},'res',$dom,$user)) );
        !           233:     }
1.55      matthew   234: }
                    235: #
                    236: # Rename the table
1.56      matthew   237: if (! $simulate) {
                    238:     $dbh->do('DROP TABLE IF EXISTS '.$oldname);
                    239:     if (! $dbh->do('RENAME TABLE '.$newname.' TO '.$oldname)) {
                    240:         &log(0,"MySQL Error Rename: ".$dbh->errstr);
                    241:         die $dbh->errstr;
                    242:     } else {
                    243:         &log(1,"MySQL table rename successful.");
                    244:     }
1.55      matthew   245: }
                    246: if (! $dbh->disconnect) {
1.56      matthew   247:     &log(0,"MySQL Error Disconnect: ".$dbh->errstr);
1.55      matthew   248:     die $dbh->errstr;
                    249: }
                    250: ##
                    251: ## Finished!
1.56      matthew   252: &log(0,"==== Searchcat completed ".localtime()." ====");
1.55      matthew   253: close(LOG);
1.21      www       254: 
1.55      matthew   255: &write_type_count();
                    256: &write_copyright_count();
1.36      www       257: 
1.55      matthew   258: exit 0;
1.28      harris41  259: 
1.56      matthew   260: ##
                    261: ## Status logging routine.  Inputs: $level, $message
                    262: ## 
                    263: ## $level 0 should be used for normal output and error messages
                    264: ##
                    265: ## $message does not need to end with \n.  In the case of errors
                    266: ## the message should contain as much information as possible to
                    267: ## help in diagnosing the problem.
                    268: ##
                    269: sub log {
                    270:     my ($level,$message)=@_;
                    271:     $level = 0 if (! defined($level));
                    272:     if ($verbose >= $level) {
                    273:         print LOG $message.$/;
                    274:     }
                    275: }
                    276: 
1.55      matthew   277: ########################################################
                    278: ########################################################
                    279: ###                                                  ###
                    280: ###          File::Find support routines             ###
                    281: ###                                                  ###
                    282: ########################################################
                    283: ########################################################
                    284: ##
                    285: ## &only_meta_files
                    286: ##
                    287: ## Called by File::Find.
                    288: ## Takes a list of files/directories in and returns a list of files/directories
                    289: ## to search.
                    290: sub only_meta_files {
                    291:     my @PossibleFiles = @_;
                    292:     my @ChosenFiles;
                    293:     foreach my $file (@PossibleFiles) {
                    294:         if ( ($file =~ /\.meta$/ &&            # Ends in meta
                    295:               $file !~ /\.\d+\.[^\.]+\.meta$/  # is not for a prior version
                    296:              ) || (-d $file )) { # directories are okay
                    297:                  # but we do not want /. or /..
                    298:             push(@ChosenFiles,$file);
                    299:         }
1.38      www       300:     }
1.55      matthew   301:     return @ChosenFiles;
1.38      www       302: }
                    303: 
1.55      matthew   304: ##
                    305: ##
                    306: ## Debugging routines, use these for 'wanted' in the File::Find call
                    307: ##
                    308: sub print_filename {
                    309:     my ($file) = $_;
                    310:     my $fullfilename = $File::Find::name;
1.56      matthew   311:     if ($debug) {
                    312:         if (-d $file) {
                    313:             &log(5," Got directory ".$fullfilename);
                    314:         } else {
                    315:             &log(5," Got file ".$fullfilename);
                    316:         }
1.38      www       317:     }
1.55      matthew   318:     $_=$file;
1.38      www       319: }
1.28      harris41  320: 
1.55      matthew   321: sub log_metadata {
                    322:     my ($file) = $_;
                    323:     my $fullfilename = $File::Find::name;
                    324:     return if (-d $fullfilename); # No need to do anything here for directories
1.56      matthew   325:     if ($debug) {
                    326:         &log(6,$fullfilename);
                    327:         my $ref=&metadata($fullfilename);
                    328:         if (! defined($ref)) {
                    329:             &log(6,"    No data");
                    330:             return;
                    331:         }
                    332:         while (my($key,$value) = each(%$ref)) {
                    333:             &log(6,"    ".$key." => ".$value);
                    334:         }
                    335:         &count_copyright($ref->{'copyright'});
1.55      matthew   336:     }
                    337:     $_=$file;
1.31      harris41  338: }
1.21      www       339: 
1.55      matthew   340: ##
                    341: ## process_meta_file
                    342: ##   Called by File::Find. 
                    343: ##   Only input is the filename in $_.  
                    344: sub process_meta_file {
                    345:     my ($file) = $_;
1.56      matthew   346:     my $filename = $File::Find::name; # full filename
1.55      matthew   347:     return if (-d $filename); # No need to do anything here for directories
                    348:     #
1.56      matthew   349:     &log(3,$filename) if ($debug);
1.55      matthew   350:     #
                    351:     my $ref=&metadata($filename);
                    352:     #
                    353:     # $url is the original file url, not the metadata file
1.61      matthew   354:     my $target = $filename;
                    355:     $target =~ s/\.meta$//;
                    356:     my $url='/res/'.&declutter($target);
1.56      matthew   357:     &log(3,"    ".$url) if ($debug);
1.55      matthew   358:     #
                    359:     # Ignore some files based on their metadata
                    360:     if ($ref->{'obsolete'}) { 
1.56      matthew   361:         &log(3,"obsolete") if ($debug);
1.55      matthew   362:         return; 
                    363:     }
                    364:     &count_copyright($ref->{'copyright'});
                    365:     if ($ref->{'copyright'} eq 'private') { 
1.56      matthew   366:         &log(3,"private") if ($debug);
1.55      matthew   367:         return; 
                    368:     }
                    369:     #
                    370:     # Find the dynamic metadata
                    371:     my %dyn;
                    372:     if ($url=~ m:/default$:) {
                    373:         $url=~ s:/default$:/:;
1.56      matthew   374:         &log(3,"Skipping dynamic data") if ($debug);
1.55      matthew   375:     } else {
1.56      matthew   376:         &log(3,"Retrieving dynamic data") if ($debug);
                    377:         %dyn=&get_dynamic_metadata($url);
1.55      matthew   378:         &count_type($url);
                    379:     }
                    380:     #
1.61      matthew   381:     if (! defined($ref->{'creationdate'}) ||
                    382:         $ref->{'creationdate'} =~ /^\s*$/) {
                    383:         $ref->{'creationdate'} = (stat($target))[9];
                    384:     }
                    385:     if (! defined($ref->{'lastrevisiondate'}) ||
                    386:         $ref->{'lastrevisiondate'} =~ /^\s*$/) {
                    387:         $ref->{'lastrevisiondate'} = (stat($target))[9];
                    388:     }
1.55      matthew   389:     $ref->{'creationdate'}     = &sqltime($ref->{'creationdate'});
                    390:     $ref->{'lastrevisiondate'} = &sqltime($ref->{'lastrevisiondate'});
                    391:     my %Data = (
                    392:                 %$ref,
                    393:                 %dyn,
                    394:                 'url'=>$url,
                    395:                 'version'=>'current');
1.56      matthew   396:     if (! $simulate) {
                    397:         my ($count,$err) = &LONCAPA::lonmetadata::store_metadata($dbh,$newname,
                    398:                                                                  \%Data);
                    399:         if ($err) {
                    400:             &log(0,"MySQL Error Insert: ".$err);
                    401:         }
                    402:         if ($count < 1) {
                    403:             &log(0,"Unable to insert record into MySQL database for $url");
                    404:         }
1.55      matthew   405:     }
                    406:     #
                    407:     # Reset $_ before leaving
                    408:     $_ = $file;
                    409: }
                    410: 
                    411: ########################################################
                    412: ########################################################
                    413: ###                                                  ###
                    414: ###  &metadata($uri)                                 ###
                    415: ###   Retrieve metadata for the given file           ###
                    416: ###                                                  ###
                    417: ########################################################
                    418: ########################################################
                    419: sub metadata {
                    420:     my ($uri)=@_;
                    421:     my %metacache=();
                    422:     $uri=&declutter($uri);
                    423:     my $filename=$uri;
                    424:     $uri=~s/\.meta$//;
                    425:     $uri='';
                    426:     if ($filename !~ /\.meta$/) { 
                    427:         $filename.='.meta';
                    428:     }
                    429:     my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
                    430:     return undef if (! defined($metastring));
                    431:     my $parser=HTML::TokeParser->new(\$metastring);
                    432:     my $token;
                    433:     while ($token=$parser->get_token) {
                    434:         if ($token->[0] eq 'S') {
                    435:             my $entry=$token->[1];
                    436:             my $unikey=$entry;
                    437:             if (defined($token->[2]->{'part'})) { 
                    438:                 $unikey.='_'.$token->[2]->{'part'}; 
                    439:             }
                    440:             if (defined($token->[2]->{'name'})) { 
                    441:                 $unikey.='_'.$token->[2]->{'name'}; 
                    442:             }
                    443:             if ($metacache{$uri.'keys'}) {
                    444:                 $metacache{$uri.'keys'}.=','.$unikey;
                    445:             } else {
                    446:                 $metacache{$uri.'keys'}=$unikey;
                    447:             }
                    448:             foreach ( @{$token->[3]}) {
                    449:                 $metacache{$uri.''.$unikey.'.'.$_}=$token->[2]->{$_};
                    450:             } 
                    451:             if (! ($metacache{$uri.''.$unikey}=$parser->get_text('/'.$entry))){
                    452:                 $metacache{$uri.''.$unikey} = 
                    453:                     $metacache{$uri.''.$unikey.'.default'};
                    454:             }
                    455:         } # End of ($token->[0] eq 'S')
                    456:     }
                    457:     return \%metacache;
1.31      harris41  458: }
1.28      harris41  459: 
1.55      matthew   460: ##
                    461: ## &getfile($filename)
                    462: ##   Slurps up an entire file into a scalar.  
                    463: ##   Returns undef if the file does not exist
                    464: sub getfile {
                    465:     my $file = shift();
                    466:     if (! -e $file ) { 
                    467:         return undef; 
                    468:     }
                    469:     my $fh=IO::File->new($file);
                    470:     my $contents = '';
                    471:     while (<$fh>) { 
                    472:         $contents .= $_;
                    473:     }
                    474:     return $contents;
                    475: }
1.28      harris41  476: 
1.55      matthew   477: ########################################################
                    478: ########################################################
                    479: ###                                                  ###
                    480: ###    Dynamic Metadata                              ###
                    481: ###                                                  ###
                    482: ########################################################
                    483: ########################################################
1.56      matthew   484: ##
1.58      www       485: ## Dynamic metadata description (incomplete)
                    486: ##
                    487: ## For a full description of all fields,
                    488: ## see LONCAPA::lonmetadata
1.56      matthew   489: ##
                    490: ##   Field             Type
                    491: ##-----------------------------------------------------------
                    492: ##   count             integer
                    493: ##   course            integer
1.58      www       494: ##   course_list       comma separated list of course ids
1.56      matthew   495: ##   avetries          real                                
1.58      www       496: ##   avetries_list     comma separated list of real numbers
1.56      matthew   497: ##   stdno             real
1.58      www       498: ##   stdno_list        comma separated list of real numbers
1.56      matthew   499: ##   usage             integer   
1.58      www       500: ##   usage_list        comma separated list of resources
1.56      matthew   501: ##   goto              scalar
1.58      www       502: ##   goto_list         comma separated list of resources
1.56      matthew   503: ##   comefrom          scalar
1.58      www       504: ##   comefrom_list     comma separated list of resources
1.56      matthew   505: ##   difficulty        real
1.58      www       506: ##   difficulty_list   comma separated list of real numbers
1.56      matthew   507: ##   sequsage          scalar
1.58      www       508: ##   sequsage_list     comma separated list of resources
1.56      matthew   509: ##   clear             real
                    510: ##   technical         real
                    511: ##   correct           real
                    512: ##   helpful           real
                    513: ##   depth             real
                    514: ##   comments          html of all the comments made
                    515: ##
                    516: {
                    517: 
                    518: my %DynamicData;
                    519: my %Counts;
                    520: 
                    521: sub process_dynamic_metadata {
                    522:     my ($user,$dom) = @_;
                    523:     undef(%DynamicData);
                    524:     undef(%Counts);
                    525:     #
                    526:     my $prodir = &propath($dom,$user);
1.55      matthew   527:     #
1.56      matthew   528:     # Read in the dynamic metadata
1.55      matthew   529:     my %evaldata;
                    530:     if (! tie(%evaldata,'GDBM_File',
                    531:               $prodir.'/nohist_resevaldata.db',&GDBM_READER(),0640)) {
1.56      matthew   532:         return 0;
1.55      matthew   533:     }
1.56      matthew   534:     #
1.57      matthew   535:     %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
1.55      matthew   536:     untie(%evaldata);
1.62    ! matthew   537:     $DynamicData{'domain'} = $dom;
        !           538:     print('user = '.$user.' domain = '.$dom.$/);
1.56      matthew   539:     #
                    540:     # Read in the access count data
                    541:     &log(7,'Reading access count data') if ($debug);
                    542:     my %countdata;
                    543:     if (! tie(%countdata,'GDBM_File',
                    544:               $prodir.'/nohist_accesscount.db',&GDBM_READER(),0640)) {
                    545:         return 0;
                    546:     }
                    547:     while (my ($key,$count) = each(%countdata)) {
                    548:         next if ($key !~ /^$dom/);
                    549:         $key = &unescape($key);
                    550:         &log(8,'    Count '.$key.' = '.$count) if ($debug);
                    551:         $Counts{$key}=$count;
                    552:     }
                    553:     untie(%countdata);
                    554:     if ($debug) {
                    555:         &log(7,scalar(keys(%Counts)).
                    556:              " Counts read for ".$user."@".$dom);
                    557:         &log(7,scalar(keys(%DynamicData)).
                    558:              " Dynamic metadata read for ".$user."@".$dom);
                    559:     }
                    560:     #
                    561:     return 1;
                    562: }
                    563: 
                    564: sub get_dynamic_metadata {
                    565:     my ($url) = @_;
                    566:     $url =~ s:^/res/::;
1.57      matthew   567:     my %data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
                    568:                                                                \%DynamicData);
1.56      matthew   569:     # find the count
                    570:     $data{'count'} = $Counts{$url};
                    571:     #
                    572:     # Log the dynamic metadata
                    573:     if ($debug) {
                    574:         while (my($k,$v)=each(%data)) {
                    575:             &log(8,"    ".$k." => ".$v);
                    576:         }
1.44      www       577:     }
1.56      matthew   578:     return %data;
1.30      www       579: }
1.28      harris41  580: 
1.56      matthew   581: } # End of %DynamicData and %Counts scope
                    582: 
1.55      matthew   583: ########################################################
                    584: ########################################################
                    585: ###                                                  ###
                    586: ###   Counts                                         ###
                    587: ###                                                  ###
                    588: ########################################################
                    589: ########################################################
                    590: {
1.1       harris41  591: 
1.55      matthew   592: my %countext;
1.15      harris41  593: 
1.55      matthew   594: sub count_type {
                    595:     my $file=shift;
                    596:     $file=~/\.(\w+)$/;
                    597:     my $ext=lc($1);
                    598:     $countext{$ext}++;
1.31      harris41  599: }
1.1       harris41  600: 
1.55      matthew   601: sub write_type_count {
                    602:     open(RESCOUNT,'>/home/httpd/html/lon-status/rescount.txt');
                    603:     while (my ($extension,$count) = each(%countext)) {
                    604: 	print RESCOUNT $extension.'='.$count.'&';
1.47      www       605:     }
1.55      matthew   606:     print RESCOUNT 'time='.time."\n";
                    607:     close(RESCOUNT);
1.31      harris41  608: }
1.27      www       609: 
1.55      matthew   610: } # end of scope for %countext
1.34      matthew   611: 
1.55      matthew   612: {
1.34      matthew   613: 
1.55      matthew   614: my %copyrights;
1.44      www       615: 
1.55      matthew   616: sub count_copyright {
                    617:     $copyrights{@_[0]}++;
1.31      harris41  618: }
1.33      matthew   619: 
1.55      matthew   620: sub write_copyright_count {
                    621:     open(COPYCOUNT,'>/home/httpd/html/lon-status/copyrightcount.txt');
                    622:     while (my ($copyright,$count) = each(%copyrights)) {
                    623: 	print COPYCOUNT $copyright.'='.$count.'&';
1.31      harris41  624:     }
1.55      matthew   625:     print COPYCOUNT 'time='.time."\n";
                    626:     close(COPYCOUNT);
1.31      harris41  627: }
1.28      harris41  628: 
1.55      matthew   629: } # end of scope for %copyrights
1.28      harris41  630: 
1.55      matthew   631: ########################################################
                    632: ########################################################
                    633: ###                                                  ###
                    634: ###   Miscellanous Utility Routines                  ###
                    635: ###                                                  ###
                    636: ########################################################
                    637: ########################################################
                    638: ##
                    639: ## &ishome($username)
                    640: ##   Returns 1 if $username is a LON-CAPA author, 0 otherwise
                    641: ##   (copied from lond, modification of the return value)
1.31      harris41  642: sub ishome {
                    643:     my $author=shift;
                    644:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                    645:     my ($udom,$uname)=split(/\//,$author);
                    646:     my $proname=propath($udom,$uname);
                    647:     if (-e $proname) {
                    648: 	return 1;
                    649:     } else {
                    650:         return 0;
                    651:     }
                    652: }
1.28      harris41  653: 
1.55      matthew   654: ##
                    655: ## &propath($udom,$uname)
                    656: ##   Returns the path to the users LON-CAPA directory
                    657: ##   (copied from lond)
1.31      harris41  658: sub propath {
                    659:     my ($udom,$uname)=@_;
                    660:     $udom=~s/\W//g;
                    661:     $uname=~s/\W//g;
                    662:     my $subdir=$uname.'__';
                    663:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                    664:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
                    665:     return $proname;
                    666: } 
1.28      harris41  667: 
1.55      matthew   668: ##
                    669: ## &sqltime($timestamp)
                    670: ##
                    671: ## Convert perl $timestamp to MySQL time.  MySQL expects YYYY-MM-DD HH:MM:SS
                    672: ##
1.31      harris41  673: sub sqltime {
1.55      matthew   674:     my ($time) = @_;
                    675:     my $mysqltime;
                    676:     if ($time =~ 
                    677:         /(\d+)-(\d+)-(\d+) # YYYY-MM-DD
                    678:         \s                 # a space
                    679:         (\d+):(\d+):(\d+)  # HH:MM::SS
                    680:         /x ) { 
                    681:         # Some of the .meta files have the time in mysql
                    682:         # format already, so just make sure they are 0 padded and
                    683:         # pass them back.
                    684:         $mysqltime = sprintf('%04d-%02d-%02d %02d:%02d:%02d',
                    685:                              $1,$2,$3,$4,$5,$6);
                    686:     } elsif ($time =~ /^\d+$/) {
                    687:         my @TimeData = gmtime($time);
                    688:         # Alter the month to be 1-12 instead of 0-11
                    689:         $TimeData[4]++;
                    690:         # Alter the year to be from 0 instead of from 1900
                    691:         $TimeData[5]+=1900;
                    692:         $mysqltime = sprintf('%04d-%02d-%02d %02d:%02d:%02d',
                    693:                              @TimeData[5,4,3,2,1,0]);
1.56      matthew   694:     } elsif (! defined($time) || $time == 0) {
                    695:         $mysqltime = 0;
1.55      matthew   696:     } else {
1.56      matthew   697:         &log(0,"    sqltime:Unable to decode time ".$time);
1.55      matthew   698:         $mysqltime = 0;
                    699:     }
                    700:     return $mysqltime;
1.31      harris41  701: }
1.28      harris41  702: 
1.55      matthew   703: ##
                    704: ## &declutter($filename)
                    705: ##   Given a filename, returns a url for the filename.
                    706: sub declutter {
                    707:     my $thisfn=shift;
                    708:     $thisfn=~s/^$perlvar{'lonDocRoot'}//;
                    709:     $thisfn=~s/^\///;
                    710:     $thisfn=~s/^res\///;
                    711:     return $thisfn;
1.31      harris41  712: }
1.28      harris41  713: 
1.55      matthew   714: ##
                    715: ## Escape / Unescape special characters
                    716: sub unescape {
                    717:     my $str=shift;
                    718:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
                    719:     return $str;
1.31      harris41  720: }
1.28      harris41  721: 
1.55      matthew   722: sub escape {
                    723:     my $str=shift;
                    724:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
                    725:     return $str;
1.45      www       726: }

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