File:  [LON-CAPA] / loncom / metadata_database / searchcat.pl
Revision 1.59: download - view: text, annotated - select for diffs
Tue Jun 22 14:16:08 2004 UTC (19 years, 10 months ago) by matthew
Branches: MAIN
CVS tags: version_1_2_X, version_1_2_1, version_1_2_0, version_1_1_99_5, version_1_1_99_4, version_1_1_99_3, version_1_1_99_2, version_1_1_99_1, HEAD
Append PID to temporary table name to prevent collisions when multiple
searchcat run simultaniously.

    1: #!/usr/bin/perl
    2: # The LearningOnline Network
    3: # searchcat.pl "Search Catalog" batch script
    4: #
    5: # $Id: searchcat.pl,v 1.59 2004/06/22 14:16:08 matthew 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: 
   69: use DBI;
   70: use lib '/home/httpd/lib/perl/';
   71: use LONCAPA::Configuration;
   72: use LONCAPA::lonmetadata;
   73: 
   74: use Getopt::Long;
   75: use IO::File;
   76: use HTML::TokeParser;
   77: use GDBM_File;
   78: use POSIX qw(strftime mktime);
   79: 
   80: use File::Find;
   81: 
   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: 
  119: ##
  120: ## Use variables for table names so we can test this routine a little easier
  121: my $oldname = 'metadata';
  122: my $newname = 'newmetadata'.$$; # append pid to have unique temporary table
  123: 
  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');
  146: &log(0,'==== Searchcat Run '.localtime()."====");
  147: 
  148: 
  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: }
  154: #
  155: # Connect to database
  156: my $dbh;
  157: if (! ($dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},
  158:                           { RaiseError =>0,PrintError=>0}))) {
  159:     &log(0,"Cannot connect to database!");
  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();
  171:     &log(0,"MySQL Error Create: ".$dbh->errstr);
  172:     die $dbh->errstr;
  173: }
  174: #
  175: # find out which users we need to examine
  176: my $dom = $perlvar{'lonDefDomain'};
  177: opendir(RESOURCES,"$perlvar{'lonDocRoot'}/res/$dom");
  178: my @homeusers = 
  179:     grep {
  180:         &ishome("$perlvar{'lonDocRoot'}/res/$dom/$_");
  181:     } grep { 
  182:         !/^\.\.?$/;
  183:     } readdir(RESOURCES);
  184: closedir RESOURCES;
  185: #
  186: if ($oneuser) {
  187:     @homeusers=($oneuser);
  188: }
  189: #
  190: # Loop through the users
  191: foreach my $user (@homeusers) {
  192:     &log(0,"=== User: ".$user);
  193:     &process_dynamic_metadata($user,$dom);
  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
  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:     }
  214: }
  215: 
  216: if (! $dbh->disconnect) {
  217:     &log(0,"MySQL Error Disconnect: ".$dbh->errstr);
  218:     die $dbh->errstr;
  219: }
  220: ##
  221: ## Finished!
  222: &log(0,"==== Searchcat completed ".localtime()." ====");
  223: close(LOG);
  224: 
  225: &write_type_count();
  226: &write_copyright_count();
  227: 
  228: exit 0;
  229: 
  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: 
  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:         }
  270:     }
  271:     return @ChosenFiles;
  272: }
  273: 
  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;
  281:     if ($debug) {
  282:         if (-d $file) {
  283:             &log(5," Got directory ".$fullfilename);
  284:         } else {
  285:             &log(5," Got file ".$fullfilename);
  286:         }
  287:     }
  288:     $_=$file;
  289: }
  290: 
  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
  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'});
  306:     }
  307:     $_=$file;
  308: }
  309: 
  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) = $_;
  316:     my $filename = $File::Find::name; # full filename
  317:     return if (-d $filename); # No need to do anything here for directories
  318:     #
  319:     &log(3,$filename) if ($debug);
  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$//;
  326:     &log(3,"    ".$url) if ($debug);
  327:     #
  328:     # Ignore some files based on their metadata
  329:     if ($ref->{'obsolete'}) { 
  330:         &log(3,"obsolete") if ($debug);
  331:         return; 
  332:     }
  333:     &count_copyright($ref->{'copyright'});
  334:     if ($ref->{'copyright'} eq 'private') { 
  335:         &log(3,"private") if ($debug);
  336:         return; 
  337:     }
  338:     #
  339:     # Find the dynamic metadata
  340:     my %dyn;
  341:     if ($url=~ m:/default$:) {
  342:         $url=~ s:/default$:/:;
  343:         &log(3,"Skipping dynamic data") if ($debug);
  344:     } else {
  345:         &log(3,"Retrieving dynamic data") if ($debug);
  346:         %dyn=&get_dynamic_metadata($url);
  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');
  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:         }
  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;
  419: }
  420: 
  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: }
  437: 
  438: ########################################################
  439: ########################################################
  440: ###                                                  ###
  441: ###    Dynamic Metadata                              ###
  442: ###                                                  ###
  443: ########################################################
  444: ########################################################
  445: ##
  446: ## Dynamic metadata description (incomplete)
  447: ##
  448: ## For a full description of all fields,
  449: ## see LONCAPA::lonmetadata
  450: ##
  451: ##   Field             Type
  452: ##-----------------------------------------------------------
  453: ##   count             integer
  454: ##   course            integer
  455: ##   course_list       comma separated list of course ids
  456: ##   avetries          real                                
  457: ##   avetries_list     comma separated list of real numbers
  458: ##   stdno             real
  459: ##   stdno_list        comma separated list of real numbers
  460: ##   usage             integer   
  461: ##   usage_list        comma separated list of resources
  462: ##   goto              scalar
  463: ##   goto_list         comma separated list of resources
  464: ##   comefrom          scalar
  465: ##   comefrom_list     comma separated list of resources
  466: ##   difficulty        real
  467: ##   difficulty_list   comma separated list of real numbers
  468: ##   sequsage          scalar
  469: ##   sequsage_list     comma separated list of resources
  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);
  488:     #
  489:     # Read in the dynamic metadata
  490:     my %evaldata;
  491:     if (! tie(%evaldata,'GDBM_File',
  492:               $prodir.'/nohist_resevaldata.db',&GDBM_READER(),0640)) {
  493:         return 0;
  494:     }
  495:     #
  496:     %DynamicData = &LONCAPA::lonmetadata::process_reseval_data(\%evaldata);
  497:     untie(%evaldata);
  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:     }
  530:     my %data = &LONCAPA::lonmetadata::process_dynamic_metadata($url,
  531:                                                                \%DynamicData);
  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:         }
  540:     }
  541:     return %data;
  542: }
  543: 
  544: } # End of %DynamicData and %Counts scope
  545: 
  546: ########################################################
  547: ########################################################
  548: ###                                                  ###
  549: ###   Counts                                         ###
  550: ###                                                  ###
  551: ########################################################
  552: ########################################################
  553: {
  554: 
  555: my %countext;
  556: 
  557: sub count_type {
  558:     my $file=shift;
  559:     $file=~/\.(\w+)$/;
  560:     my $ext=lc($1);
  561:     $countext{$ext}++;
  562: }
  563: 
  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.'&';
  568:     }
  569:     print RESCOUNT 'time='.time."\n";
  570:     close(RESCOUNT);
  571: }
  572: 
  573: } # end of scope for %countext
  574: 
  575: {
  576: 
  577: my %copyrights;
  578: 
  579: sub count_copyright {
  580:     $copyrights{@_[0]}++;
  581: }
  582: 
  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.'&';
  587:     }
  588:     print COPYCOUNT 'time='.time."\n";
  589:     close(COPYCOUNT);
  590: }
  591: 
  592: } # end of scope for %copyrights
  593: 
  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)
  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: }
  616: 
  617: ##
  618: ## &propath($udom,$uname)
  619: ##   Returns the path to the users LON-CAPA directory
  620: ##   (copied from lond)
  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: } 
  630: 
  631: ##
  632: ## &sqltime($timestamp)
  633: ##
  634: ## Convert perl $timestamp to MySQL time.  MySQL expects YYYY-MM-DD HH:MM:SS
  635: ##
  636: sub sqltime {
  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]);
  657:     } elsif (! defined($time) || $time == 0) {
  658:         $mysqltime = 0;
  659:     } else {
  660:         &log(0,"    sqltime:Unable to decode time ".$time);
  661:         $mysqltime = 0;
  662:     }
  663:     return $mysqltime;
  664: }
  665: 
  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;
  675: }
  676: 
  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;
  683: }
  684: 
  685: sub escape {
  686:     my $str=shift;
  687:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  688:     return $str;
  689: }

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