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

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

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