Annotation of loncom/metadata_database/searchcat.pl, revision 1.22
1.1 harris41 1: #!/usr/bin/perl
2: # The LearningOnline Network
3: # searchcat.pl "Search Catalog" batch script
1.16 harris41 4: #
1.22 ! www 5: # $Id: searchcat.pl,v 1.21 2002/10/08 15:09:36 www 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.21 www 47: use GDBM_File;
1.1 harris41 48:
49: my @metalist;
1.21 www 50:
51:
52: # ----------------------------------------------------- Un-Escape Special Chars
53:
54: sub unescape {
55: my $str=shift;
56: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
57: return $str;
58: }
59:
1.22 ! www 60: # -------------------------------------------------------- Escape Special Chars
! 61:
! 62: sub escape {
! 63: my $str=shift;
! 64: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
! 65: return $str;
! 66: }
! 67:
1.21 www 68:
69: # ------------------------------------------- Code to evaluate dynamic metadata
70:
71: sub dynamicmeta {
72: #
73: #
74: # Do nothing for now ...
75: #
76: #
77: return;
78: #
79: # ..., but stuff below already works
80: #
81: my $url=&declutter(shift);
82: $url=~s/\.meta$//;
83: my %returnhash=();
84: my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
85: my $prodir=&propath($adomain,$aauthor);
86: if (tie(%evaldata,'GDBM_File',
1.22 ! www 87: $prodir.'/nohist_resevaldata.db',&GDBM_WRCREAT,0640)) {
1.21 www 88: my %sum=();
89: my %cnt=();
90: my %listitems=('count' => 'add',
91: 'course' => 'add',
92: 'avetries' => 'avg',
93: 'stdno' => 'add',
94: 'difficulty' => 'avg',
95: 'clear' => 'avg',
96: 'technical' => 'avg',
97: 'helpful' => 'avg',
98: 'correct' => 'avg',
99: 'depth' => 'avg',
100: 'comments' => 'app',
101: 'usage' => 'cnt'
102: );
103: my $regexp=$url;
104: $regexp=~s/(\W)/\\$1/g;
105: $regexp='___'.$regexp.'___([a-z]+)$';
106: foreach (keys %evaldata) {
107: my $key=&unescape($_);
108: if ($key=~/$regexp/) {
1.22 ! www 109: my $ctype=$1;
! 110: if (defined($cnt{$ctype})) {
! 111: $cnt{$ctype}++;
! 112: } else {
! 113: $cnt{$ctype}=1;
! 114: }
! 115: unless ($listitems{$ctype} eq 'app') {
! 116: if (defined($sum{$ctype})) {
! 117: $sum{$ctype}+=$evaldata{$_};
! 118: } else {
! 119: $sum{$ctype}=$evaldata{$_};
! 120: }
! 121: } else {
! 122: if (defined($sum{$ctype})) {
! 123: if ($evaldata{$_}) {
! 124: $sum{$ctype}.='<hr>'.$evaldata{$_};
1.21 www 125: }
1.22 ! www 126: } else {
! 127: $sum{$ctype}=''.$evaldata{$_};
! 128: }
! 129: }
! 130: if ($ctype eq 'count') {
! 131: delete($evaldata{$_});
! 132: }
! 133: }
! 134: }
! 135: foreach (keys %cnt) {
! 136: if ($listitems{$_} eq 'avg') {
! 137: $returnhash{$_}=int(($sum{$_}/$cnt{$_})*100.0+0.5)/100.0;
! 138: } elsif ($listitems{$_} eq 'cnt') {
! 139: $returnhash{$_}=$cnt{$_};
! 140: } else {
! 141: $returnhash{$_}=$sum{$_};
! 142: }
! 143: }
! 144: if ($returnhash{'count'}) {
! 145: my $newkey=$$.'_'.time.'_searchcat___'.&escape($url).'___count';
! 146: $evaldata{$newkey}=$returnhash{'count'};
1.21 www 147: }
148: untie(%evaldata);
149: }
150: return %returnhash;
151: }
152:
1.1 harris41 153: # ----------------- Code to enable 'find' subroutine listing of the .meta files
154: require "find.pl";
155: sub wanted {
156: (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
157: -f _ &&
1.10 harris41 158: /^.*\.meta$/ && !/^.+\.\d+\.[^\.]+\.meta$/ &&
1.1 harris41 159: push(@metalist,"$dir/$_");
160: }
161:
1.18 matthew 162: # --------------- Read loncapa_apache.conf and loncapa.conf and get variables
1.20 harris41 163: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1.17 harris41 164: my %perlvar=%{$perlvarref};
165: undef $perlvarref; # remove since sensitive and not needed
166: delete $perlvar{'lonReceipt'}; # remove since sensitive and not needed
1.15 harris41 167:
168: # ------------------------------------- Only run if machine is a library server
169: exit unless $perlvar{'lonRole'} eq 'library';
1.1 harris41 170:
1.3 harris41 171: my $dbh;
1.1 harris41 172: # ------------------------------------- Make sure that database can be accessed
173: {
174: unless (
175: $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
176: ) {
177: print "Cannot connect to database!\n";
178: exit;
179: }
1.19 matthew 180: my $make_metadata_table = "CREATE TABLE IF NOT EXISTS metadata (".
181: "title TEXT, author TEXT, subject TEXT, url TEXT, keywords TEXT, ".
182: "version TEXT, notes TEXT, abstract TEXT, mime TEXT, language TEXT, ".
183: "creationdate DATETIME, lastrevisiondate DATETIME, owner TEXT, ".
184: "copyright TEXT, FULLTEXT idx_title (title), ".
185: "FULLTEXT idx_author (author), FULLTEXT idx_subject (subject), ".
186: "FULLTEXT idx_url (url), FULLTEXT idx_keywords (keywords), ".
187: "FULLTEXT idx_version (version), FULLTEXT idx_notes (notes), ".
188: "FULLTEXT idx_abstract (abstract), FULLTEXT idx_mime (mime), ".
189: "FULLTEXT idx_language (language), FULLTEXT idx_owner (owner), ".
190: "FULLTEXT idx_copyright (copyright)) TYPE=MYISAM";
191: # It would sure be nice to have some logging mechanism.
192: $dbh->do($make_metadata_table);
1.1 harris41 193: }
194:
195: # ------------------------------------------------------------- get .meta files
1.2 harris41 196: opendir(RESOURCES,"$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}");
197: my @homeusers=grep
198: {&ishome("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$_")}
199: grep {!/^\.\.?$/} readdir(RESOURCES);
200: closedir RESOURCES;
201: foreach my $user (@homeusers) {
202: &find("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$user");
203: }
1.1 harris41 204:
205: # -- process each file to get metadata and put into search catalog SQL database
1.9 harris41 206: # Also, check to see if already there.
1.11 harris41 207: # I could just delete (without searching first), but this works for now.
1.1 harris41 208: foreach my $m (@metalist) {
209: my $ref=&metadata($m);
1.11 harris41 210: my $m2='/res/'.&declutter($m);
1.12 harris41 211: $m2=~s/\.meta$//;
1.21 www 212: &dynamicmeta($m2);
1.12 harris41 213: my $q2="select * from metadata where url like binary '$m2'";
1.9 harris41 214: my $sth = $dbh->prepare($q2);
215: $sth->execute();
216: my $r1=$sth->fetchall_arrayref;
217: if (@$r1) {
1.12 harris41 218: $sth=$dbh->prepare("delete from metadata where url like binary '$m2'");
1.9 harris41 219: $sth->execute();
220: }
221: $sth=$dbh->prepare('insert into metadata values ('.
1.8 harris41 222: '"'.delete($ref->{'title'}).'"'.','.
223: '"'.delete($ref->{'author'}).'"'.','.
224: '"'.delete($ref->{'subject'}).'"'.','.
1.12 harris41 225: '"'.$m2.'"'.','.
1.8 harris41 226: '"'.delete($ref->{'keywords'}).'"'.','.
1.9 harris41 227: '"'.'current'.'"'.','.
1.8 harris41 228: '"'.delete($ref->{'notes'}).'"'.','.
229: '"'.delete($ref->{'abstract'}).'"'.','.
230: '"'.delete($ref->{'mime'}).'"'.','.
231: '"'.delete($ref->{'language'}).'"'.','.
1.13 harris41 232: '"'.sqltime(delete($ref->{'creationdate'})).'"'.','.
233: '"'.sqltime(delete($ref->{'lastrevisiondate'})).'"'.','.
1.8 harris41 234: '"'.delete($ref->{'owner'}).'"'.','.
235: '"'.delete($ref->{'copyright'}).'"'.')');
1.1 harris41 236: $sth->execute();
237: }
238:
239: # ----------------------------------------------------------- Clean up database
240: # Need to, perhaps, remove stale SQL database records.
241: # ... not yet implemented
242:
243: # --------------------------------------------------- Close database connection
244: $dbh->disconnect;
245:
246: # ---------------------------------------------------------------- Get metadata
247: # significantly altered from subroutine present in lonnet
248: sub metadata {
249: my ($uri,$what)=@_;
250: my %metacache;
251: $uri=&declutter($uri);
252: my $filename=$uri;
253: $uri=~s/\.meta$//;
254: $uri='';
255: unless ($metacache{$uri.'keys'}) {
256: unless ($filename=~/\.meta$/) { $filename.='.meta'; }
257: my $metastring=&getfile($perlvar{'lonDocRoot'}.'/res/'.$filename);
258: my $parser=HTML::TokeParser->new(\$metastring);
259: my $token;
260: while ($token=$parser->get_token) {
261: if ($token->[0] eq 'S') {
262: my $entry=$token->[1];
263: my $unikey=$entry;
264: if (defined($token->[2]->{'part'})) {
265: $unikey.='_'.$token->[2]->{'part'};
266: }
267: if (defined($token->[2]->{'name'})) {
268: $unikey.='_'.$token->[2]->{'name'};
269: }
270: if ($metacache{$uri.'keys'}) {
271: $metacache{$uri.'keys'}.=','.$unikey;
272: } else {
273: $metacache{$uri.'keys'}=$unikey;
274: }
275: map {
276: $metacache{$uri.''.$unikey.'.'.$_}=$token->[2]->{$_};
277: } @{$token->[3]};
278: unless (
279: $metacache{$uri.''.$unikey}=$parser->get_text('/'.$entry)
280: ) { $metacache{$uri.''.$unikey}=
281: $metacache{$uri.''.$unikey.'.default'};
282: }
283: }
284: }
285: }
286: return \%metacache;
287: }
288:
289: # ------------------------------------------------------------ Serves up a file
290: # returns either the contents of the file or a -1
291: sub getfile {
292: my $file=shift;
293: if (! -e $file ) { return -1; };
294: my $fh=IO::File->new($file);
295: my $a='';
296: while (<$fh>) { $a .=$_; }
297: return $a
298: }
299:
300: # ------------------------------------------------------------- Declutters URLs
301: sub declutter {
302: my $thisfn=shift;
303: $thisfn=~s/^$perlvar{'lonDocRoot'}//;
304: $thisfn=~s/^\///;
305: $thisfn=~s/^res\///;
306: return $thisfn;
307: }
1.2 harris41 308:
309: # --------------------------------------- Is this the home server of an author?
310: # (copied from lond, modification of the return value)
311: sub ishome {
312: my $author=shift;
313: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
314: my ($udom,$uname)=split(/\//,$author);
315: my $proname=propath($udom,$uname);
316: if (-e $proname) {
317: return 1;
318: } else {
319: return 0;
320: }
321: }
322:
323: # -------------------------------------------- Return path to profile directory
324: # (copied from lond)
325: sub propath {
326: my ($udom,$uname)=@_;
327: $udom=~s/\W//g;
328: $uname=~s/\W//g;
329: my $subdir=$uname.'__';
330: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
331: my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
332: return $proname;
333: }
1.13 harris41 334:
335: # ---------------------------- convert 'time' format into a datetime sql format
336: sub sqltime {
337: my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
338: localtime(@_[0]);
1.14 harris41 339: $mon++; $year+=1900;
1.13 harris41 340: return "$year-$mon-$mday $hour:$min:$sec";
341: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>