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

1.1       harris41    1: #!/usr/bin/perl
                      2: # The LearningOnline Network
                      3: # searchcat.pl "Search Catalog" batch script
1.16      harris41    4: #
1.19    ! matthew     5: # $Id: searchcat.pl,v 1.18 2002/05/17 14:03:04 matthew 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:     }
1.19    ! matthew    77:     my $make_metadata_table = "CREATE TABLE IF NOT EXISTS metadata (".
        !            78:         "title TEXT, author TEXT, subject TEXT, url TEXT, keywords TEXT, ".
        !            79:         "version TEXT, notes TEXT, abstract TEXT, mime TEXT, language TEXT, ".
        !            80:         "creationdate DATETIME, lastrevisiondate DATETIME, owner TEXT, ".
        !            81:         "copyright TEXT, FULLTEXT idx_title (title), ".
        !            82:         "FULLTEXT idx_author (author), FULLTEXT idx_subject (subject), ".
        !            83:         "FULLTEXT idx_url (url), FULLTEXT idx_keywords (keywords), ".
        !            84:         "FULLTEXT idx_version (version), FULLTEXT idx_notes (notes), ".
        !            85:         "FULLTEXT idx_abstract (abstract), FULLTEXT idx_mime (mime), ".
        !            86:         "FULLTEXT idx_language (language), FULLTEXT idx_owner (owner), ".
        !            87:         "FULLTEXT idx_copyright (copyright)) TYPE=MYISAM";
        !            88:     # It would sure be nice to have some logging mechanism.
        !            89:     $dbh->do($make_metadata_table);
1.1       harris41   90: }
                     91: 
                     92: # ------------------------------------------------------------- get .meta files
1.2       harris41   93: opendir(RESOURCES,"$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}");
                     94: my @homeusers=grep
                     95:           {&ishome("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$_")}
                     96:           grep {!/^\.\.?$/} readdir(RESOURCES);
                     97: closedir RESOURCES;
                     98: foreach my $user (@homeusers) {
                     99:     &find("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$user");
                    100: }
1.1       harris41  101: 
                    102: # -- process each file to get metadata and put into search catalog SQL database
1.9       harris41  103: # Also, check to see if already there.
1.11      harris41  104: # I could just delete (without searching first), but this works for now.
1.1       harris41  105: foreach my $m (@metalist) {
                    106:     my $ref=&metadata($m);
1.11      harris41  107:     my $m2='/res/'.&declutter($m);
1.12      harris41  108:     $m2=~s/\.meta$//;
                    109:     my $q2="select * from metadata where url like binary '$m2'";
1.9       harris41  110:     my $sth = $dbh->prepare($q2);
                    111:     $sth->execute();
                    112:     my $r1=$sth->fetchall_arrayref;
                    113:     if (@$r1) {
1.12      harris41  114: 	$sth=$dbh->prepare("delete from metadata where url like binary '$m2'");
1.9       harris41  115:         $sth->execute();
                    116:     }
                    117:     $sth=$dbh->prepare('insert into metadata values ('.
1.8       harris41  118: 			  '"'.delete($ref->{'title'}).'"'.','.
                    119: 			  '"'.delete($ref->{'author'}).'"'.','.
                    120: 			  '"'.delete($ref->{'subject'}).'"'.','.
1.12      harris41  121: 			  '"'.$m2.'"'.','.
1.8       harris41  122: 			  '"'.delete($ref->{'keywords'}).'"'.','.
1.9       harris41  123: 			  '"'.'current'.'"'.','.
1.8       harris41  124: 			  '"'.delete($ref->{'notes'}).'"'.','.
                    125: 			  '"'.delete($ref->{'abstract'}).'"'.','.
                    126: 			  '"'.delete($ref->{'mime'}).'"'.','.
                    127: 			  '"'.delete($ref->{'language'}).'"'.','.
1.13      harris41  128: 			  '"'.sqltime(delete($ref->{'creationdate'})).'"'.','.
                    129: 			  '"'.sqltime(delete($ref->{'lastrevisiondate'})).'"'.','.
1.8       harris41  130: 			  '"'.delete($ref->{'owner'}).'"'.','.
                    131: 			  '"'.delete($ref->{'copyright'}).'"'.')');
1.1       harris41  132:     $sth->execute();
                    133: }
                    134: 
                    135: # ----------------------------------------------------------- Clean up database
                    136: # Need to, perhaps, remove stale SQL database records.
                    137: # ... not yet implemented
                    138: 
                    139: # --------------------------------------------------- Close database connection
                    140: $dbh->disconnect;
                    141: 
                    142: # ---------------------------------------------------------------- Get metadata
                    143: # significantly altered from subroutine present in lonnet
                    144: sub metadata {
                    145:     my ($uri,$what)=@_;
                    146:     my %metacache;
                    147:     $uri=&declutter($uri);
                    148:     my $filename=$uri;
                    149:     $uri=~s/\.meta$//;
                    150:     $uri='';
                    151:     unless ($metacache{$uri.'keys'}) {
                    152:         unless ($filename=~/\.meta$/) { $filename.='.meta'; }
                    153: 	my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
                    154:         my $parser=HTML::TokeParser->new(\$metastring);
                    155:         my $token;
                    156:         while ($token=$parser->get_token) {
                    157:            if ($token->[0] eq 'S') {
                    158: 	      my $entry=$token->[1];
                    159:               my $unikey=$entry;
                    160:               if (defined($token->[2]->{'part'})) { 
                    161:                  $unikey.='_'.$token->[2]->{'part'}; 
                    162: 	      }
                    163:               if (defined($token->[2]->{'name'})) { 
                    164:                  $unikey.='_'.$token->[2]->{'name'}; 
                    165: 	      }
                    166:               if ($metacache{$uri.'keys'}) {
                    167:                  $metacache{$uri.'keys'}.=','.$unikey;
                    168:               } else {
                    169:                  $metacache{$uri.'keys'}=$unikey;
                    170: 	      }
                    171:               map {
                    172: 		  $metacache{$uri.''.$unikey.'.'.$_}=$token->[2]->{$_};
                    173:               } @{$token->[3]};
                    174:               unless (
                    175:                  $metacache{$uri.''.$unikey}=$parser->get_text('/'.$entry)
                    176: 		      ) { $metacache{$uri.''.$unikey}=
                    177: 			      $metacache{$uri.''.$unikey.'.default'};
                    178: 		      }
                    179:           }
                    180:        }
                    181:     }
                    182:     return \%metacache;
                    183: }
                    184: 
                    185: # ------------------------------------------------------------ Serves up a file
                    186: # returns either the contents of the file or a -1
                    187: sub getfile {
                    188:   my $file=shift;
                    189:   if (! -e $file ) { return -1; };
                    190:   my $fh=IO::File->new($file);
                    191:   my $a='';
                    192:   while (<$fh>) { $a .=$_; }
                    193:   return $a
                    194: }
                    195: 
                    196: # ------------------------------------------------------------- Declutters URLs
                    197: sub declutter {
                    198:     my $thisfn=shift;
                    199:     $thisfn=~s/^$perlvar{'lonDocRoot'}//;
                    200:     $thisfn=~s/^\///;
                    201:     $thisfn=~s/^res\///;
                    202:     return $thisfn;
                    203: }
1.2       harris41  204: 
                    205: # --------------------------------------- Is this the home server of an author?
                    206: # (copied from lond, modification of the return value)
                    207: sub ishome {
                    208:     my $author=shift;
                    209:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
                    210:     my ($udom,$uname)=split(/\//,$author);
                    211:     my $proname=propath($udom,$uname);
                    212:     if (-e $proname) {
                    213: 	return 1;
                    214:     } else {
                    215:         return 0;
                    216:     }
                    217: }
                    218: 
                    219: # -------------------------------------------- Return path to profile directory
                    220: # (copied from lond)
                    221: sub propath {
                    222:     my ($udom,$uname)=@_;
                    223:     $udom=~s/\W//g;
                    224:     $uname=~s/\W//g;
                    225:     my $subdir=$uname.'__';
                    226:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
                    227:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
                    228:     return $proname;
                    229: } 
1.13      harris41  230: 
                    231: # ---------------------------- convert 'time' format into a datetime sql format
                    232: sub sqltime {
                    233:     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                    234: 	localtime(@_[0]);
1.14      harris41  235:     $mon++; $year+=1900;
1.13      harris41  236:     return "$year-$mon-$mday $hour:$min:$sec";
                    237: }

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