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

1.1       harris41    1: #!/usr/bin/perl
                      2: # The LearningOnline Network
                      3: # searchcat.pl "Search Catalog" batch script
1.16      harris41    4: #
1.18    ! matthew     5: # $Id: searchcat.pl,v 1.17 2002/05/11 21:28:20 harris41 Exp $
1.16      harris41    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: # YEAR=2001
1.17      harris41   30: # 04/14/2001, 04/16/2001 Scott Harrison
1.16      harris41   31: #
1.17      harris41   32: # YEAR=2002
                     33: # 05/11/2002 Scott Harrison
1.16      harris41   34: #
                     35: ###
1.1       harris41   36: 
                     37: # This script goes through a LON-CAPA resource
                     38: # directory and gathers metadata.
                     39: # The metadata is entered into a SQL database.
                     40: 
1.17      harris41   41: use lib '/home/httpd/lib/perl/';
                     42: use LONCAPA::Configuration;
                     43: 
1.1       harris41   44: use IO::File;
                     45: use HTML::TokeParser;
1.6       harris41   46: use DBI;
1.1       harris41   47: 
                     48: my @metalist;
                     49: # ----------------- Code to enable 'find' subroutine listing of the .meta files
                     50: require "find.pl";
                     51: sub wanted {
                     52:     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
                     53:     -f _ &&
1.10      harris41   54:     /^.*\.meta$/ && !/^.+\.\d+\.[^\.]+\.meta$/ &&
1.1       harris41   55:     push(@metalist,"$dir/$_");
                     56: }
                     57: 
1.18    ! matthew    58: # ---------------  Read loncapa_apache.conf and loncapa.conf and get variables
        !            59: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa_apache.conf',
        !            60:                                                  'loncapa.conf');
1.17      harris41   61: my %perlvar=%{$perlvarref};
                     62: undef $perlvarref; # remove since sensitive and not needed
                     63: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
1.15      harris41   64: 
                     65: # ------------------------------------- Only run if machine is a library server
                     66: exit unless $perlvar{'lonRole'} eq 'library';
1.1       harris41   67: 
1.3       harris41   68: my $dbh;
1.1       harris41   69: # ------------------------------------- Make sure that database can be accessed
                     70: {
                     71:     unless (
                     72: 	    $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
                     73: 	    ) { 
                     74: 	print "Cannot connect to database!\n";
                     75: 	exit;
                     76:     }
                     77: }
                     78: 
                     79: # ------------------------------------------------------------- get .meta files
1.2       harris41   80: opendir(RESOURCES,"$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}");
                     81: my @homeusers=grep
                     82:           {&ishome("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$_")}
                     83:           grep {!/^\.\.?$/} readdir(RESOURCES);
                     84: closedir RESOURCES;
                     85: foreach my $user (@homeusers) {
                     86:     &find("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$user");
                     87: }
1.1       harris41   88: 
                     89: # -- process each file to get metadata and put into search catalog SQL database
1.9       harris41   90: # Also, check to see if already there.
1.11      harris41   91: # I could just delete (without searching first), but this works for now.
1.1       harris41   92: foreach my $m (@metalist) {
                     93:     my $ref=&metadata($m);
1.11      harris41   94:     my $m2='/res/'.&declutter($m);
1.12      harris41   95:     $m2=~s/\.meta$//;
                     96:     my $q2="select * from metadata where url like binary '$m2'";
1.9       harris41   97:     my $sth = $dbh->prepare($q2);
                     98:     $sth->execute();
                     99:     my $r1=$sth->fetchall_arrayref;
                    100:     if (@$r1) {
1.12      harris41  101: 	$sth=$dbh->prepare("delete from metadata where url like binary '$m2'");
1.9       harris41  102:         $sth->execute();
                    103:     }
                    104:     $sth=$dbh->prepare('insert into metadata values ('.
1.8       harris41  105: 			  '"'.delete($ref->{'title'}).'"'.','.
                    106: 			  '"'.delete($ref->{'author'}).'"'.','.
                    107: 			  '"'.delete($ref->{'subject'}).'"'.','.
1.12      harris41  108: 			  '"'.$m2.'"'.','.
1.8       harris41  109: 			  '"'.delete($ref->{'keywords'}).'"'.','.
1.9       harris41  110: 			  '"'.'current'.'"'.','.
1.8       harris41  111: 			  '"'.delete($ref->{'notes'}).'"'.','.
                    112: 			  '"'.delete($ref->{'abstract'}).'"'.','.
                    113: 			  '"'.delete($ref->{'mime'}).'"'.','.
                    114: 			  '"'.delete($ref->{'language'}).'"'.','.
1.13      harris41  115: 			  '"'.sqltime(delete($ref->{'creationdate'})).'"'.','.
                    116: 			  '"'.sqltime(delete($ref->{'lastrevisiondate'})).'"'.','.
1.8       harris41  117: 			  '"'.delete($ref->{'owner'}).'"'.','.
                    118: 			  '"'.delete($ref->{'copyright'}).'"'.')');
1.1       harris41  119:     $sth->execute();
                    120: }
                    121: 
                    122: # ----------------------------------------------------------- Clean up database
                    123: # Need to, perhaps, remove stale SQL database records.
                    124: # ... not yet implemented
                    125: 
                    126: # --------------------------------------------------- Close database connection
                    127: $dbh->disconnect;
                    128: 
                    129: # ---------------------------------------------------------------- Get metadata
                    130: # significantly altered from subroutine present in lonnet
                    131: sub metadata {
                    132:     my ($uri,$what)=@_;
                    133:     my %metacache;
                    134:     $uri=&declutter($uri);
                    135:     my $filename=$uri;
                    136:     $uri=~s/\.meta$//;
                    137:     $uri='';
                    138:     unless ($metacache{$uri.'keys'}) {
                    139:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
                    140: 	my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
                    141:         my $parser=HTML::TokeParser->new(\$metastring);
                    142:         my $token;
                    143:         while ($token=$parser->get_token) {
                    144:            if ($token->[0] eq 'S') {
                    145: 	      my $entry=$token->[1];
                    146:               my $unikey=$entry;
                    147:               if (defined($token->[2]->{'part'})) { 
                    148:                  $unikey.='_'.$token->[2]->{'part'}; 
                    149: 	      }
                    150:               if (defined($token->[2]->{'name'})) { 
                    151:                  $unikey.='_'.$token->[2]->{'name'}; 
                    152: 	      }
                    153:               if ($metacache{$uri.'keys'}) {
                    154:                  $metacache{$uri.'keys'}.=','.$unikey;
                    155:               } else {
                    156:                  $metacache{$uri.'keys'}=$unikey;
                    157: 	      }
                    158:               map {
                    159: 		  $metacache{$uri.''.$unikey.'.'.$_}=$token->[2]->{$_};
                    160:               } @{$token->[3]};
                    161:               unless (
                    162:                  $metacache{$uri.''.$unikey}=$parser->get_text('/'.$entry)
                    163: 		      ) { $metacache{$uri.''.$unikey}=
                    164: 			      $metacache{$uri.''.$unikey.'.default'};
                    165: 		      }
                    166:           }
                    167:        }
                    168:     }
                    169:     return \%metacache;
                    170: }
                    171: 
                    172: # ------------------------------------------------------------ Serves up a file
                    173: # returns either the contents of the file or a -1
                    174: sub getfile {
                    175:   my $file=shift;
                    176:   if (! -e $file ) { return -1; };
                    177:   my $fh=IO::File->new($file);
                    178:   my $a='';
                    179:   while (<$fh>) { $a .=$_; }
                    180:   return $a
                    181: }
                    182: 
                    183: # ------------------------------------------------------------- Declutters URLs
                    184: sub declutter {
                    185:     my $thisfn=shift;
                    186:     $thisfn=~s/^$perlvar{'lonDocRoot'}//;
                    187:     $thisfn=~s/^\///;
                    188:     $thisfn=~s/^res\///;
                    189:     return $thisfn;
                    190: }
1.2       harris41  191: 
                    192: # --------------------------------------- Is this the home server of an author?
                    193: # (copied from lond, modification of the return value)
                    194: sub ishome {
                    195:     my $author=shift;
                    196:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                    197:     my ($udom,$uname)=split(/\//,$author);
                    198:     my $proname=propath($udom,$uname);
                    199:     if (-e $proname) {
                    200: 	return 1;
                    201:     } else {
                    202:         return 0;
                    203:     }
                    204: }
                    205: 
                    206: # -------------------------------------------- Return path to profile directory
                    207: # (copied from lond)
                    208: sub propath {
                    209:     my ($udom,$uname)=@_;
                    210:     $udom=~s/\W//g;
                    211:     $uname=~s/\W//g;
                    212:     my $subdir=$uname.'__';
                    213:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                    214:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
                    215:     return $proname;
                    216: } 
1.13      harris41  217: 
                    218: # ---------------------------- convert 'time' format into a datetime sql format
                    219: sub sqltime {
                    220:     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                    221: 	localtime(@_[0]);
1.14      harris41  222:     $mon++; $year+=1900;
1.13      harris41  223:     return "$year-$mon-$mday $hour:$min:$sec";
                    224: }

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