File:  [LON-CAPA] / loncom / metadata_database / searchcat.pl
Revision 1.66: download - view: text, annotated - select for diffs
Fri Jan 27 15:53:49 2006 UTC (18 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: version_2_1_2, HEAD
- don't do the chdir since we use the full filename anyway (faster this way)

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

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