File:  [LON-CAPA] / loncom / metadata_database / searchcat.pl
Revision 1.17: download - view: text, annotated - select for diffs
Sat May 11 21:28:20 2002 UTC (22 years, 1 month ago) by harris41
Branches: MAIN
CVS tags: HEAD
using LONCAPA::Configuration::read_conf
BUG 129

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

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