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