Annotation of loncom/publisher/lonpublisher.pm, revision 1.69
1.1 www 1: # The LearningOnline Network with CAPA
2: # Publication Handler
1.54 albertel 3: #
1.69 ! albertel 4: # $Id: lonpublisher.pm,v 1.68 2002/01/08 21:14:53 albertel Exp $
1.54 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.1 www 28: #
29: # (TeX Content Handler
30: #
31: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
32: #
1.15 www 33: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
1.20 www 34: # 03/23 Guy Albertelli
1.23 www 35: # 03/24,03/29,04/03 Gerd Kortemeyer
1.24 harris41 36: # 04/16/2001 Scott Harrison
1.27 www 37: # 05/03,05/05,05/07 Gerd Kortemeyer
1.30 harris41 38: # 05/28/2001 Scott Harrison
1.51 www 39: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 Gerd Kortemeyer
1.58 www 40: # 12/04,12/05 Guy Albertelli
41: # 12/05 Gerd Kortemeyer
1.62 www 42: # 12/05 Guy Albertelli
1.64 www 43: # 12/06,12/07 Gerd Kortemeyer
1.66 harris41 44: # 12/15,12/16 Scott Harrison
1.67 www 45: # 12/25 Gerd Kortemeyer
1.65 harris41 46: #
47: ###
48:
49: ###############################################################################
50: ## ##
51: ## ORGANIZATION OF THIS PERL MODULE ##
52: ## ##
53: ## 1. Modules used by this module ##
54: ## 2. Various subroutines ##
55: ## 3. Publication Step One ##
56: ## 4. Phase Two ##
57: ## 5. Main Handler ##
58: ## ##
59: ###############################################################################
1.1 www 60:
61: package Apache::lonpublisher;
62:
1.65 harris41 63: # ------------------------------------------------- modules used by this module
1.1 www 64: use strict;
65: use Apache::File;
1.13 www 66: use File::Copy;
1.2 www 67: use Apache::Constants qw(:common :http :methods);
68: use HTML::TokeParser;
1.4 www 69: use Apache::lonxml;
1.17 albertel 70: use Apache::lonhomework;
1.27 www 71: use Apache::loncacc;
1.24 harris41 72: use DBI;
1.65 harris41 73: use Apache::lonnet();
74: use Apache::loncommon();
1.2 www 75:
1.3 www 76: my %addid;
1.5 www 77: my %nokey;
1.10 www 78:
1.7 www 79: my %metadatafields;
80: my %metadatakeys;
81:
1.12 www 82: my $docroot;
83:
1.27 www 84: my $cuname;
85: my $cudom;
86:
1.12 www 87: # ----------------------------------------------- Evaluate string with metadata
1.7 www 88: sub metaeval {
89: my $metastring=shift;
90:
91: my $parser=HTML::TokeParser->new(\$metastring);
92: my $token;
93: while ($token=$parser->get_token) {
94: if ($token->[0] eq 'S') {
95: my $entry=$token->[1];
96: my $unikey=$entry;
1.32 www 97: if (defined($token->[2]->{'package'})) {
98: $unikey.='_package_'.$token->[2]->{'package'};
99: }
1.7 www 100: if (defined($token->[2]->{'part'})) {
101: $unikey.='_'.$token->[2]->{'part'};
102: }
1.32 www 103: if (defined($token->[2]->{'id'})) {
1.49 www 104: $unikey.='_'.$token->[2]->{'id'};
1.32 www 105: }
1.7 www 106: if (defined($token->[2]->{'name'})) {
107: $unikey.='_'.$token->[2]->{'name'};
108: }
1.65 harris41 109: foreach (@{$token->[3]}) {
1.7 www 110: $metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
111: if ($metadatakeys{$unikey}) {
112: $metadatakeys{$unikey}.=','.$_;
113: } else {
114: $metadatakeys{$unikey}=$_;
115: }
1.65 harris41 116: }
1.7 www 117: if ($metadatafields{$unikey}) {
1.8 www 118: my $newentry=$parser->get_text('/'.$entry);
1.41 www 119: unless (($metadatafields{$unikey}=~/$newentry/) ||
120: ($newentry eq '')) {
1.8 www 121: $metadatafields{$unikey}.=', '.$newentry;
122: }
1.7 www 123: } else {
124: $metadatafields{$unikey}=$parser->get_text('/'.$entry);
125: }
126: }
127: }
128: }
129:
1.12 www 130: # -------------------------------------------------------- Read a metadata file
1.7 www 131: sub metaread {
132: my ($logfile,$fn)=@_;
133: unless (-e $fn) {
134: print $logfile 'No file '.$fn."\n";
135: return '<br><b>No file:</b> <tt>'.$fn.'</tt>';
136: }
137: print $logfile 'Processing '.$fn."\n";
138: my $metastring;
139: {
140: my $metafh=Apache::File->new($fn);
141: $metastring=join('',<$metafh>);
142: }
143: &metaeval($metastring);
144: return '<br><b>Processed file:</b> <tt>'.$fn.'</tt>';
145: }
146:
1.25 harris41 147: # ---------------------------- convert 'time' format into a datetime sql format
148: sub sqltime {
149: my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
150: localtime(@_[0]);
151: $mon++; $year+=1900;
152: return "$year-$mon-$mday $hour:$min:$sec";
153: }
154:
1.12 www 155: # --------------------------------------------------------- Various form fields
156:
1.8 www 157: sub textfield {
1.10 www 158: my ($title,$name,$value)=@_;
1.8 www 159: return "\n<p><b>$title:</b><br>".
1.11 www 160: '<input type=text name="'.$name.'" size=80 value="'.$value.'">';
161: }
162:
163: sub hiddenfield {
164: my ($name,$value)=@_;
165: return "\n".'<input type=hidden name="'.$name.'" value="'.$value.'">';
1.8 www 166: }
167:
1.9 www 168: sub selectbox {
1.65 harris41 169: my ($title,$name,$value,$functionref,@idlist)=@_;
170: my $uctitle=uc($title);
171: my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
172: "</b></font><br />".'<select name="'.$name.'">';
173: foreach (@idlist) {
174: $selout.='<option value=\''.$_.'\'';
175: if ($_ eq $value) {
176: $selout.=' selected>'.&{$functionref}($_).'</option>';
177: }
178: else {$selout.='>'.&{$functionref}($_).'</option>';}
179: }
1.10 www 180: return $selout.'</select>';
1.9 www 181: }
182:
1.12 www 183: # -------------------------------------------------------- Publication Step One
184:
1.34 www 185: sub urlfixup {
1.35 www 186: my ($url,$target)=@_;
1.39 www 187: unless ($url) { return ''; }
1.68 albertel 188: #javascript code needs no fixing
189: if ($url =~ /^javascript:/i) { return $url; }
1.69 ! albertel 190: if ($url =~ /^mailto:/i) { return $url; }
1.68 albertel 191: #internal document links need no fixing
192: if ($url =~ /^\#/) { return $url; }
1.35 www 193: my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
1.65 harris41 194: foreach (values %Apache::lonnet::hostname) {
1.35 www 195: if ($_ eq $host) {
196: $url=~s/^http\:\/\///;
197: $url=~s/^$host//;
198: }
1.65 harris41 199: }
1.40 www 200: if ($url=~/^http\:\/\//) { return $url; }
1.35 www 201: $url=~s/\~$cuname/res\/$cudom\/$cuname/;
202: if ($target) {
203: $target=~s/\/[^\/]+$//;
204: $url=&Apache::lonnet::hreflocation($target,$url);
205: }
206: return $url;
1.34 www 207: }
208:
1.2 www 209: sub publish {
1.50 www 210:
1.2 www 211: my ($source,$target,$style)=@_;
212: my $logfile;
1.4 www 213: my $scrout='';
1.23 www 214: my $allmeta='';
215: my $content='';
1.36 www 216: my %allow=();
217: undef %allow;
1.4 www 218:
1.2 www 219: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1.7 www 220: return
221: '<font color=red>No write permission to user directory, FAIL</font>';
1.2 www 222: }
223: print $logfile
1.11 www 224: "\n\n================= Publish ".localtime()." Phase One ================\n";
1.2 www 225:
1.3 www 226: if (($style eq 'ssi') || ($style eq 'rat')) {
227: # ------------------------------------------------------- This needs processing
1.4 www 228:
229: # ----------------------------------------------------------------- Backup Copy
1.3 www 230: my $copyfile=$source.'.save';
1.13 www 231: if (copy($source,$copyfile)) {
1.3 www 232: print $logfile "Copied original file to ".$copyfile."\n";
233: } else {
1.13 www 234: print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
235: return "<font color=red>Failed to write backup copy, $!,FAIL</font>";
1.3 www 236: }
1.4 www 237: # ------------------------------------------------------------- IDs and indices
238:
239: my $maxindex=10;
240: my $maxid=10;
1.23 www 241:
1.4 www 242: my $needsfixup=0;
243:
244: {
245: my $org=Apache::File->new($source);
246: $content=join('',<$org>);
247: }
248: {
249: my $parser=HTML::TokeParser->new(\$content);
250: my $token;
251: while ($token=$parser->get_token) {
252: if ($token->[0] eq 'S') {
253: my $counter;
254: if ($counter=$addid{$token->[1]}) {
255: if ($counter eq 'id') {
256: if (defined($token->[2]->{'id'})) {
257: $maxid=
258: ($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
259: } else {
260: $needsfixup=1;
261: }
262: } else {
263: if (defined($token->[2]->{'index'})) {
264: $maxindex=
265: ($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
266: } else {
267: $needsfixup=1;
268: }
269: }
270: }
271: }
272: }
273: }
274: if ($needsfixup) {
275: print $logfile "Needs ID and/or index fixup\n".
276: "Max ID : $maxid (min 10)\n".
277: "Max Index: $maxindex (min 10)\n";
1.34 www 278: }
1.4 www 279: my $outstring='';
280: my $parser=HTML::TokeParser->new(\$content);
1.53 albertel 281: $parser->xml_mode(1);
1.4 www 282: my $token;
283: while ($token=$parser->get_token) {
284: if ($token->[0] eq 'S') {
1.34 www 285: my $counter;
286: my $tag=$token->[1];
1.56 albertel 287: my $lctag=lc($tag);
1.53 albertel 288: unless ($lctag eq 'allow') {
1.34 www 289: my %parms=%{$token->[2]};
1.53 albertel 290: $counter=$addid{$tag};
291: if (!$counter) { $counter=$addid{$lctag}; }
292: if ($counter) {
1.4 www 293: if ($counter eq 'id') {
1.34 www 294: unless (defined($parms{'id'})) {
1.4 www 295: $maxid++;
1.34 www 296: $parms{'id'}=$maxid;
297: print $logfile 'ID: '.$tag.':'.$maxid."\n";
1.4 www 298: }
1.34 www 299: } elsif ($counter eq 'index') {
300: unless (defined($parms{'index'})) {
1.4 www 301: $maxindex++;
1.34 www 302: $parms{'index'}=$maxindex;
303: print $logfile 'Index: '.$tag.':'.$maxindex."\n";
1.4 www 304: }
305: }
1.34 www 306: }
307:
1.65 harris41 308: foreach ('src','href','background') {
1.34 www 309: if (defined($parms{$_})) {
310: my $oldurl=$parms{$_};
1.35 www 311: my $newurl=&urlfixup($oldurl,$target);
1.34 www 312: if ($newurl ne $oldurl) {
313: $parms{$_}=$newurl;
314: print $logfile 'URL: '.$tag.':'.$oldurl.' - '.
315: $newurl."\n";
316: }
1.36 www 317: $allow{$newurl}=1;
1.34 www 318: }
1.65 harris41 319: }
1.38 www 320:
1.53 albertel 321: if ($lctag eq 'applet') {
1.38 www 322: my $codebase='';
323: if (defined($parms{'codebase'})) {
324: my $oldcodebase=$parms{'codebase'};
325: unless ($oldcodebase=~/\/$/) {
326: $oldcodebase.='/';
327: }
328: $codebase=&urlfixup($oldcodebase,$target);
329: $codebase=~s/\/$//;
330: if ($codebase ne $oldcodebase) {
331: $parms{'codebase'}=$codebase;
332: print $logfile 'URL codebase: '.$tag.':'.
333: $oldcodebase.' - '.
334: $codebase."\n";
335: }
336: $allow{$codebase.'/*'}=1;
337: } else {
1.65 harris41 338: foreach ('archive','code','object') {
1.38 www 339: if (defined($parms{$_})) {
340: my $oldurl=$parms{$_};
341: my $newurl=&urlfixup($oldurl,$target);
342: $newurl=~s/\/[^\/]+$/\/\*/;
343: print $logfile 'Allow: applet '.$_.':'.
344: $oldurl.' allows '.
345: $newurl."\n";
346: $allow{$newurl}=1;
347: }
1.65 harris41 348: }
1.38 www 349: }
350: }
1.34 www 351:
352: my $newparmstring='';
353: my $endtag='';
1.65 harris41 354: foreach (keys %parms) {
1.34 www 355: if ($_ eq '/') {
356: $endtag=' /';
357: } else {
358: my $quote=($parms{$_}=~/\"/?"'":'"');
359: $newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
360: }
1.65 harris41 361: }
1.57 albertel 362: if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
1.34 www 363: $outstring.='<'.$tag.$newparmstring.$endtag.'>';
1.36 www 364: } else {
365: $allow{$token->[2]->{'src'}}=1;
366: }
1.4 www 367: } elsif ($token->[0] eq 'E') {
1.57 albertel 368: if ($token->[2]) {
1.34 www 369: unless ($token->[1] eq 'allow') {
1.41 www 370: $outstring.='</'.$token->[1].'>';
1.34 www 371: }
1.57 albertel 372: }
1.4 www 373: } else {
374: $outstring.=$token->[1];
375: }
376: }
1.36 www 377: # ------------------------------------------------------------ Construct Allows
1.62 www 378:
1.44 www 379: $scrout.='<h3>Dependencies</h3>';
1.62 www 380: my $allowstr='';
1.65 harris41 381: foreach (keys %allow) {
1.59 www 382: my $thisdep=$_;
1.62 www 383: unless ($style eq 'rat') {
384: $allowstr.="\n".'<allow src="'.$thisdep.'" />';
385: }
1.44 www 386: $scrout.='<br>';
1.59 www 387: unless ($thisdep=~/\*/) {
388: $scrout.='<a href="'.$thisdep.'">';
1.44 www 389: }
1.59 www 390: $scrout.='<tt>'.$thisdep.'</tt>';
391: unless ($thisdep=~/\*/) {
1.44 www 392: $scrout.='</a>';
1.59 www 393: if (
394: &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
395: $thisdep.'.meta') eq '-1') {
1.58 www 396: $scrout.=
397: ' - <font color=red>Currently not available</font>';
1.59 www 398: } else {
399: my %temphash=(&Apache::lonnet::declutter($target).'___'.
400: &Apache::lonnet::declutter($thisdep).'___usage'
401: => time);
402: $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
403: if ((defined($1)) && (defined($2))) {
404: &Apache::lonnet::put('resevaldata',\%temphash,$1,$2);
405: }
406: }
1.44 www 407: }
1.65 harris41 408: }
1.36 www 409: $outstring=~s/(\<\/[^\>]+\>\s*)$/$allowstr$1/s;
1.62 www 410:
1.37 www 411: # ------------------------------------------------------------- Write modified
412:
1.4 www 413: {
414: my $org;
415: unless ($org=Apache::File->new('>'.$source)) {
416: print $logfile "No write permit to $source\n";
1.7 www 417: return
418: "<font color=red>No write permission to $source, FAIL</font>";
1.4 www 419: }
420: print $org $outstring;
421: }
422: $content=$outstring;
1.34 www 423:
424: if ($needsfixup) {
1.4 www 425: print $logfile "End of ID and/or index fixup\n".
426: "Max ID : $maxid (min 10)\n".
427: "Max Index: $maxindex (min 10)\n";
428: } else {
429: print $logfile "Does not need ID and/or index fixup\n";
430: }
1.37 www 431: }
1.7 www 432: # --------------------------------------------- Initial step done, now metadata
433:
434: # ---------------------------------------- Storage for metadata keys and fields
435:
1.8 www 436: %metadatafields=();
437: %metadatakeys=();
438:
439: my %oldparmstores=();
1.44 www 440:
441: $scrout.='<h3>Metadata Information</h3>';
1.7 www 442:
443: # ------------------------------------------------ First, check out environment
1.8 www 444: unless (-e $source.'.meta') {
1.7 www 445: $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
446: $ENV{'environment.middlename'}.' '.
447: $ENV{'environment.lastname'}.' '.
448: $ENV{'environment.generation'};
1.8 www 449: $metadatafields{'author'}=~s/\s+/ /g;
450: $metadatafields{'author'}=~s/\s+$//;
1.27 www 451: $metadatafields{'owner'}=$cuname.'@'.$cudom;
1.7 www 452:
453: # ------------------------------------------------ Check out directory hierachy
454:
455: my $thisdisfn=$source;
1.27 www 456: $thisdisfn=~s/^\/home\/$cuname\///;
1.7 www 457:
458: my @urlparts=split(/\//,$thisdisfn);
459: $#urlparts--;
460:
1.27 www 461: my $currentpath='/home/'.$cuname.'/';
1.7 www 462:
1.65 harris41 463: foreach (@urlparts) {
1.7 www 464: $currentpath.=$_.'/';
465: $scrout.=&metaread($logfile,$currentpath.'default.meta');
1.65 harris41 466: }
1.7 www 467:
468: # ------------------- Clear out parameters and stores (there should not be any)
469:
1.65 harris41 470: foreach (keys %metadatafields) {
1.7 www 471: if (($_=~/^parameter/) || ($_=~/^stores/)) {
472: delete $metadatafields{$_};
473: }
1.65 harris41 474: }
1.7 www 475:
1.8 www 476: } else {
1.7 www 477: # ---------------------- Read previous metafile, remember parameters and stores
478:
479: $scrout.=&metaread($logfile,$source.'.meta');
480:
1.65 harris41 481: foreach (keys %metadatafields) {
1.7 www 482: if (($_=~/^parameter/) || ($_=~/^stores/)) {
483: $oldparmstores{$_}=1;
484: delete $metadatafields{$_};
485: }
1.65 harris41 486: }
1.7 www 487:
1.8 www 488: }
1.7 www 489:
1.4 www 490: # -------------------------------------------------- Parse content for metadata
1.37 www 491: if ($style eq 'ssi') {
1.42 www 492: my $oldenv=$ENV{'request.uri'};
493:
494: $ENV{'request.uri'}=$target;
1.23 www 495: $allmeta=Apache::lonxml::xmlparse('meta',$content);
1.42 www 496: $ENV{'request.uri'}=$oldenv;
1.32 www 497:
1.19 albertel 498: &metaeval($allmeta);
1.37 www 499: }
1.7 www 500: # ---------------- Find and document discrepancies in the parameters and stores
501:
502: my $chparms='';
1.65 harris41 503: foreach (sort keys %metadatafields) {
1.7 www 504: if (($_=~/^parameter/) || ($_=~/^stores/)) {
505: unless ($_=~/\.\w+$/) {
506: unless ($oldparmstores{$_}) {
507: print $logfile 'New: '.$_."\n";
508: $chparms.=$_.' ';
509: }
510: }
511: }
1.65 harris41 512: }
1.7 www 513: if ($chparms) {
514: $scrout.='<p><b>New parameters or stored values:</b> '.
515: $chparms;
516: }
517:
518: my $chparms='';
1.65 harris41 519: foreach (sort keys %oldparmstores) {
1.7 www 520: if (($_=~/^parameter/) || ($_=~/^stores/)) {
1.33 www 521: unless (($metadatafields{$_.'.name'}) ||
522: ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
1.7 www 523: print $logfile 'Obsolete: '.$_."\n";
524: $chparms.=$_.' ';
525: }
526: }
1.65 harris41 527: }
1.7 www 528: if ($chparms) {
529: $scrout.='<p><b>Obsolete parameters or stored values:</b> '.
530: $chparms;
531: }
1.37 www 532:
1.8 www 533: # ------------------------------------------------------- Now have all metadata
1.5 www 534:
1.8 www 535: $scrout.=
536: '<form action="/adm/publish" method="post">'.
1.63 albertel 537: '<p><input type="submit" value="Finalize Publication" /></p>'.
1.11 www 538: &hiddenfield('phase','two').
539: &hiddenfield('filename',$ENV{'form.filename'}).
540: &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
1.58 www 541: &hiddenfield('dependencies',join(',',keys %allow)).
1.10 www 542: &textfield('Title','title',$metadatafields{'title'}).
543: &textfield('Author(s)','author',$metadatafields{'author'}).
544: &textfield('Subject','subject',$metadatafields{'subject'});
1.5 www 545:
546: # --------------------------------------------------- Scan content for keywords
1.7 www 547:
1.8 www 548: my $keywordout='<p><b>Keywords:</b><br><table border=2><tr>';
1.7 www 549: my $colcount=0;
1.67 www 550: my %keywords=();
1.7 www 551:
1.52 albertel 552: if (length($content)<500000) {
1.5 www 553: my $textonly=$content;
554: $textonly=~s/\<script[^\<]+\<\/script\>//g;
555: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
556: $textonly=~s/\<[^\>]*\>//g;
557: $textonly=~tr/A-Z/a-z/;
558: $textonly=~s/[\$\&][a-z]\w*//g;
559: $textonly=~s/[^a-z\s]//g;
560:
1.65 harris41 561: foreach ($textonly=~m/(\w+)/g) {
1.50 www 562: unless ($nokey{$_}) {
563: $keywords{$_}=1;
564: }
1.65 harris41 565: }
1.67 www 566: }
1.5 www 567:
1.67 www 568:
1.65 harris41 569: foreach (split(/\W+/,$metadatafields{'keywords'})) {
1.12 www 570: $keywords{$_}=1;
1.65 harris41 571: }
1.5 www 572:
1.65 harris41 573: foreach (sort keys %keywords) {
1.12 www 574: $keywordout.='<td><input type=checkbox name="key.'.$_.'"';
1.67 www 575: if ($metadatafields{'keywords'}) {
576: if ($metadatafields{'keywords'}=~/$_/) {
577: $keywordout.=' checked';
578: }
579: } elsif (&Apache::loncommon::keyword($_)) {
580: $keywordout.=' checked';
581: }
1.8 www 582: $keywordout.='>'.$_.'</td>';
1.7 www 583: if ($colcount>10) {
584: $keywordout.="</tr><tr>\n";
585: $colcount=0;
586: }
1.50 www 587: $colcount++;
1.65 harris41 588: }
1.50 www 589:
1.51 www 590: $keywordout.='</tr></table>';
591:
592: $scrout.=$keywordout;
1.9 www 593:
1.12 www 594: $scrout.=&textfield('Additional Keywords','addkey','');
595:
1.10 www 596: $scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1.9 www 597:
598: $scrout.=
599: '<p><b>Abstract:</b><br><textarea cols=80 rows=5 name=abstract>'.
600: $metadatafields{'abstract'}.'</textarea>';
601:
1.11 www 602: $source=~/\.(\w+)$/;
603:
604: $scrout.=&hiddenfield('mime',$1);
605:
1.10 www 606: $scrout.=&selectbox('Language','language',
1.65 harris41 607: $metadatafields{'language'},
608: \&{Apache::loncommon::languagedescription},
609: (&Apache::loncommon::languageids),
610: );
1.11 www 611:
612: unless ($metadatafields{'creationdate'}) {
613: $metadatafields{'creationdate'}=time;
614: }
615: $scrout.=&hiddenfield('creationdate',$metadatafields{'creationdate'});
616:
617: $scrout.=&hiddenfield('lastrevisiondate',time);
618:
1.9 www 619:
1.10 www 620: $scrout.=&textfield('Publisher/Owner','owner',
621: $metadatafields{'owner'});
1.45 www 622: # --------------------------------------------------- Correct copyright for rat
623: if ($style eq 'rat') {
1.65 harris41 624: if ($metadatafields{'copyright'} eq 'public') {
625: delete $metadatafields{'copyright'};
626: }
627: $scrout.=&selectbox('Copyright/Distribution','copyright',
628: $metadatafields{'copyright'},
629: \&{Apache::loncommon::copyrightdescription},
630: (grep !/^public$/,(&Apache::loncommon::copyrightids)));
631: }
632: else {
1.10 www 633: $scrout.=&selectbox('Copyright/Distribution','copyright',
1.65 harris41 634: $metadatafields{'copyright'},
635: \&{Apache::loncommon::copyrightdescription},
636: (&Apache::loncommon::copyrightids));
637: }
1.8 www 638: return $scrout.
1.63 albertel 639: '<p><input type="submit" value="Finalize Publication" /></p></form>';
1.2 www 640: }
1.1 www 641:
1.12 www 642: # -------------------------------------------------------- Publication Step Two
643:
1.11 www 644: sub phasetwo {
645:
1.24 harris41 646: my ($source,$target,$style,$distarget)=@_;
1.11 www 647: my $logfile;
648: my $scrout='';
649:
650: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
651: return
652: '<font color=red>No write permission to user directory, FAIL</font>';
653: }
654: print $logfile
655: "\n================= Publish ".localtime()." Phase Two ================\n";
656:
657: %metadatafields=();
658: %metadatakeys=();
659:
660: &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
661:
662: $metadatafields{'title'}=$ENV{'form.title'};
663: $metadatafields{'author'}=$ENV{'form.author'};
664: $metadatafields{'subject'}=$ENV{'form.subject'};
665: $metadatafields{'notes'}=$ENV{'form.notes'};
666: $metadatafields{'abstract'}=$ENV{'form.abstract'};
667: $metadatafields{'mime'}=$ENV{'form.mime'};
668: $metadatafields{'language'}=$ENV{'form.language'};
669: $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
670: $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
671: $metadatafields{'owner'}=$ENV{'form.owner'};
672: $metadatafields{'copyright'}=$ENV{'form.copyright'};
1.60 www 673: $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
1.12 www 674:
675: my $allkeywords=$ENV{'form.addkey'};
1.65 harris41 676: foreach (keys %ENV) {
1.12 www 677: if ($_=~/^form\.key\.(\w+)/) {
678: $allkeywords.=','.$1;
679: }
1.65 harris41 680: }
1.12 www 681: $allkeywords=~s/\W+/\,/;
682: $allkeywords=~s/^\,//;
683: $metadatafields{'keywords'}=$allkeywords;
684:
685: {
686: print $logfile "\nWrite metadata file for ".$source;
687: my $mfh;
688: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
689: return
690: '<font color=red>Could not write metadata, FAIL</font>';
1.65 harris41 691: }
692: foreach (sort keys %metadatafields) {
1.12 www 693: unless ($_=~/\./) {
694: my $unikey=$_;
695: $unikey=~/^([A-Za-z]+)/;
696: my $tag=$1;
697: $tag=~tr/A-Z/a-z/;
698: print $mfh "\n\<$tag";
1.65 harris41 699: foreach (split(/\,/,$metadatakeys{$unikey})) {
1.12 www 700: my $value=$metadatafields{$unikey.'.'.$_};
701: $value=~s/\"/\'\'/g;
702: print $mfh ' '.$_.'="'.$value.'"';
1.65 harris41 703: }
1.12 www 704: print $mfh '>'.$metadatafields{$unikey}.'</'.$tag.'>';
705: }
1.65 harris41 706: }
1.12 www 707: $scrout.='<p>Wrote Metadata';
708: print $logfile "\nWrote metadata";
709: }
710:
1.24 harris41 711: # -------------------------------- Synchronize entry with SQL metadata database
1.64 www 712: my $warning;
713:
714: unless ($metadatafields{'copyright'} eq 'priv') {
1.25 harris41 715:
1.24 harris41 716: my $dbh;
717: {
718: unless (
1.64 www 719: $dbh = DBI->connect("DBI:mysql:loncapa","www",
720: $Apache::lonnet::perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
1.24 harris41 721: ) {
1.29 harris41 722: $warning='<font color=red>WARNING: Cannot connect to '.
723: 'database!</font>';
724: }
725: else {
726: my %sqldatafields;
727: $sqldatafields{'url'}=$distarget;
728: my $sth=$dbh->prepare(
729: 'delete from metadata where url like binary'.
730: '"'.$sqldatafields{'url'}.'"');
731: $sth->execute();
1.65 harris41 732: foreach ('title','author','subject','keywords','notes','abstract',
1.29 harris41 733: 'mime','language','creationdate','lastrevisiondate','owner',
1.65 harris41 734: 'copyright') {
735: my $field=$metadatafields{$_}; $field=~s/\"/\'\'/g;
736: $sqldatafields{$_}=$field;
737: }
1.29 harris41 738:
739: $sth=$dbh->prepare('insert into metadata values ('.
740: '"'.delete($sqldatafields{'title'}).'"'.','.
741: '"'.delete($sqldatafields{'author'}).'"'.','.
742: '"'.delete($sqldatafields{'subject'}).'"'.','.
743: '"'.delete($sqldatafields{'url'}).'"'.','.
744: '"'.delete($sqldatafields{'keywords'}).'"'.','.
745: '"'.'current'.'"'.','.
746: '"'.delete($sqldatafields{'notes'}).'"'.','.
747: '"'.delete($sqldatafields{'abstract'}).'"'.','.
748: '"'.delete($sqldatafields{'mime'}).'"'.','.
749: '"'.delete($sqldatafields{'language'}).'"'.','.
750: '"'.
751: sqltime(delete($sqldatafields{'creationdate'}))
752: .'"'.','.
753: '"'.
754: sqltime(delete(
755: $sqldatafields{'lastrevisiondate'})).'"'.','.
756: '"'.delete($sqldatafields{'owner'}).'"'.','.
757: '"'.delete(
758: $sqldatafields{'copyright'}).'"'.')');
759: $sth->execute();
760: $dbh->disconnect;
761: $scrout.='<p>Synchronized SQL metadata database';
762: print $logfile "\nSynchronized SQL metadata database";
1.24 harris41 763: }
764: }
765:
1.64 www 766: } else {
767: $scrout.='<p>Private Publication - did not synchronize database';
1.66 harris41 768: print $logfile "\nPrivate: Did not synchronize data into ".
769: "SQL metadata database";
1.64 www 770: }
1.12 www 771: # ----------------------------------------------------------- Copy old versions
772:
773: if (-e $target) {
774: my $filename;
775: my $maxversion=0;
776: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
777: my $srcf=$2;
778: my $srct=$3;
779: my $srcd=$1;
780: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
781: print $logfile "\nPANIC: Target dir is ".$srcd;
782: return "<font color=red>Invalid target directory, FAIL</font>";
783: }
784: opendir(DIR,$srcd);
785: while ($filename=readdir(DIR)) {
786: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
787: $maxversion=($1>$maxversion)?$1:$maxversion;
788: }
789: }
790: closedir(DIR);
791: $maxversion++;
792: $scrout.='<p>Creating old version '.$maxversion;
793: print $logfile "\nCreating old version ".$maxversion;
794:
795: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
796:
1.13 www 797: if (copy($target,$copyfile)) {
1.12 www 798: print $logfile "Copied old target to ".$copyfile."\n";
799: $scrout.='<p>Copied old target file';
800: } else {
1.13 www 801: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
802: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
1.12 www 803: }
804:
805: # --------------------------------------------------------------- Copy Metadata
806:
807: $copyfile=$copyfile.'.meta';
1.13 www 808:
809: if (copy($target.'.meta',$copyfile)) {
1.14 www 810: print $logfile "Copied old target metadata to ".$copyfile."\n";
1.12 www 811: $scrout.='<p>Copied old metadata';
812: } else {
1.13 www 813: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14 www 814: if (-e $target.'.meta') {
815: return
1.13 www 816: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
1.14 www 817: }
1.12 www 818: }
1.11 www 819:
820:
1.12 www 821: } else {
822: $scrout.='<p>Initial version';
823: print $logfile "\nInitial version";
824: }
825:
826: # ---------------------------------------------------------------- Write Source
827: my $copyfile=$target;
828:
829: my @parts=split(/\//,$copyfile);
830: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
831:
832: my $count;
833: for ($count=5;$count<$#parts;$count++) {
834: $path.="/$parts[$count]";
835: if ((-e $path)!=1) {
836: print $logfile "\nCreating directory ".$path;
837: $scrout.='<p>Created directory '.$parts[$count];
838: mkdir($path,0777);
839: }
840: }
841:
1.13 www 842: if (copy($source,$copyfile)) {
1.12 www 843: print $logfile "Copied original source to ".$copyfile."\n";
844: $scrout.='<p>Copied source file';
845: } else {
1.13 www 846: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
847: return "<font color=red>Failed to copy source, $!, FAIL</font>";
1.12 www 848: }
849:
850: # --------------------------------------------------------------- Copy Metadata
851:
1.13 www 852: $copyfile=$copyfile.'.meta';
853:
854: if (copy($source.'.meta',$copyfile)) {
1.12 www 855: print $logfile "Copied original metadata to ".$copyfile."\n";
856: $scrout.='<p>Copied metadata';
857: } else {
1.13 www 858: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.12 www 859: return
1.13 www 860: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
1.12 www 861: }
862:
863: # --------------------------------------------------- Send update notifications
864:
865: {
866:
867: my $filename;
868:
869: $target=~/(.*)\/([^\/]+)$/;
870: my $srcf=$2;
871: opendir(DIR,$1);
872: while ($filename=readdir(DIR)) {
873: if ($filename=~/$srcf\.(\w+)$/) {
874: my $subhost=$1;
875: if ($subhost ne 'meta') {
876: $scrout.='<p>Notifying host '.$subhost.':';
877: print $logfile "\nNotifying host '.$subhost.':'";
878: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1.20 www 879: $scrout.=$reply;
880: print $logfile $reply;
881: }
882: }
883: }
884: closedir(DIR);
885:
886: }
887:
888: # ---------------------------------------- Send update notifications, meta only
889:
890: {
891:
892: my $filename;
893:
894: $target=~/(.*)\/([^\/]+)$/;
895: my $srcf=$2.'.meta';
896: opendir(DIR,$1);
897: while ($filename=readdir(DIR)) {
898: if ($filename=~/$srcf\.(\w+)$/) {
899: my $subhost=$1;
900: if ($subhost ne 'meta') {
901: $scrout.=
902: '<p>Notifying host for metadata only '.$subhost.':';
903: print $logfile
904: "\nNotifying host for metadata only '.$subhost.':'";
905: my $reply=&Apache::lonnet::critical(
906: 'update:'.$target.'.meta',$subhost);
1.12 www 907: $scrout.=$reply;
908: print $logfile $reply;
909: }
910: }
911: }
912: closedir(DIR);
913:
914: }
915:
916: # ------------------------------------------------ Provide link to new resource
917:
918: my $thisdistarget=$target;
919: $thisdistarget=~s/^$docroot//;
920:
1.22 www 921: my $thissrc=$source;
922: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
923:
924: my $thissrcdir=$thissrc;
925: $thissrcdir=~s/\/[^\/]+$/\//;
926:
927:
1.29 harris41 928: return $warning.$scrout.
1.22 www 929: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
930: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
931: '<p><a href="'.$thissrcdir.
932: '"><font size=+2>Back to Source Directory</font></a>';
933:
1.11 www 934: }
935:
1.1 www 936: # ================================================================ Main Handler
937:
938: sub handler {
939: my $r=shift;
1.2 www 940:
941: if ($r->header_only) {
942: $r->content_type('text/html');
943: $r->send_http_header;
944: return OK;
945: }
946:
1.43 www 947: # Get query string for limited number of parameters
948:
1.65 harris41 949: foreach (split(/&/,$ENV{'QUERY_STRING'})) {
1.43 www 950: my ($name, $value) = split(/=/,$_);
951: $value =~ tr/+/ /;
952: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
953: if ($name eq 'filename') {
954: unless ($ENV{'form.'.$name}) {
955: $ENV{'form.'.$name}=$value;
956: }
957: }
1.65 harris41 958: }
1.43 www 959:
960:
1.2 www 961: # -------------------------------------------------------------- Check filename
962:
963: my $fn=$ENV{'form.filename'};
964:
1.27 www 965:
1.2 www 966: unless ($fn) {
1.27 www 967: $r->log_reason($cuname.' at '.$cudom.
1.2 www 968: ' trying to publish empty filename', $r->filename);
969: return HTTP_NOT_FOUND;
970: }
1.4 www 971:
1.31 www 972: ($cuname,$cudom)=
973: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
974: unless (($cuname) && ($cudom)) {
1.27 www 975: $r->log_reason($cuname.' at '.$cudom.
1.4 www 976: ' trying to publish file '.$ENV{'form.filename'}.
1.27 www 977: ' ('.$fn.') - not authorized',
978: $r->filename);
979: return HTTP_NOT_ACCEPTABLE;
980: }
981:
982: unless (&Apache::lonnet::homeserver($cuname,$cudom)
983: eq $r->dir_config('lonHostID')) {
984: $r->log_reason($cuname.' at '.$cudom.
985: ' trying to publish file '.$ENV{'form.filename'}.
986: ' ('.$fn.') - not homeserver ('.
987: &Apache::lonnet::homeserver($cuname,$cudom).')',
1.4 www 988: $r->filename);
989: return HTTP_NOT_ACCEPTABLE;
990: }
1.2 www 991:
1.43 www 992: $fn=~s/^http\:\/\/[^\/]+//;
993: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1.2 www 994:
995: my $targetdir='';
1.12 www 996: $docroot=$r->dir_config('lonDocRoot');
1.27 www 997: if ($1 ne $cuname) {
998: $r->log_reason($cuname.' at '.$cudom.
1.2 www 999: ' trying to publish unowned file '.$ENV{'form.filename'}.
1000: ' ('.$fn.')',
1001: $r->filename);
1002: return HTTP_NOT_ACCEPTABLE;
1003: } else {
1.27 www 1004: $targetdir=$docroot.'/res/'.$cudom;
1.2 www 1005: }
1006:
1007:
1008: unless (-e $fn) {
1.27 www 1009: $r->log_reason($cuname.' at '.$cudom.
1.2 www 1010: ' trying to publish non-existing file '.$ENV{'form.filename'}.
1011: ' ('.$fn.')',
1012: $r->filename);
1013: return HTTP_NOT_FOUND;
1014: }
1015:
1.11 www 1016: unless ($ENV{'form.phase'} eq 'two') {
1017:
1.2 www 1018: # --------------------------------- File is there and owned, init lookup tables
1019:
1.3 www 1020: %addid=();
1021:
1022: {
1023: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
1024: while (<$fh>=~/(\w+)\s+(\w+)/) {
1025: $addid{$1}=$2;
1026: }
1.5 www 1027: }
1028:
1029: %nokey=();
1030:
1031: {
1032: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
1.65 harris41 1033: while (<$fh>) {
1.5 www 1034: my $word=$_;
1035: chomp($word);
1036: $nokey{$word}=1;
1.65 harris41 1037: }
1.3 www 1038: }
1.11 www 1039:
1040: }
1041:
1.2 www 1042: # ----------------------------------------------------------- Start page output
1043:
1.1 www 1044: $r->content_type('text/html');
1045: $r->send_http_header;
1046:
1047: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1.15 www 1048: $r->print(
1049: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.2 www 1050: my $thisfn=$fn;
1051:
1052: # ------------------------------------------------------------- Individual file
1053: {
1054: $thisfn=~/\.(\w+)$/;
1055: my $thistype=$1;
1.65 harris41 1056: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1.2 www 1057:
1058: my $thistarget=$thisfn;
1059:
1060: $thistarget=~s/^\/home/$targetdir/;
1061: $thistarget=~s/\/public\_html//;
1062:
1063: my $thisdistarget=$thistarget;
1064: $thisdistarget=~s/^$docroot//;
1065:
1066: my $thisdisfn=$thisfn;
1.27 www 1067: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1.2 www 1068:
1069: $r->print('<h2>Publishing '.
1.66 harris41 1070: &Apache::loncommon::filedescription($thistype).' <tt>'.
1.2 www 1071: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1.27 www 1072:
1073: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1074: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
1075: '</font></h3>');
1076: }
1.26 www 1077:
1.65 harris41 1078: if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
1.28 www 1079: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
1080: $thisdisfn.
1.26 www 1081: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
1082: }
1.11 www 1083:
1.2 www 1084: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
1085:
1.11 www 1086: unless ($ENV{'form.phase'} eq 'two') {
1.27 www 1087: $r->print(
1088: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1.11 www 1089: } else {
1.27 www 1090: $r->print(
1091: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1.11 www 1092: }
1.2 www 1093:
1.11 www 1094: }
1.1 www 1095: $r->print('</body></html>');
1.15 www 1096:
1.1 www 1097: return OK;
1098: }
1099:
1100: 1;
1101: __END__
1102:
1.66 harris41 1103: =head1 NAME
1.1 www 1104:
1.66 harris41 1105: Apache::lonpublisher - Publication Handler
1.1 www 1106:
1.66 harris41 1107: =head1 SYNOPSIS
1.1 www 1108:
1.66 harris41 1109: Invoked by /etc/httpd/conf/srm.conf:
1.1 www 1110:
1.66 harris41 1111: <Location /adm/publish>
1112: PerlAccessHandler Apache::lonacc
1113: SetHandler perl-script
1114: PerlHandler Apache::lonpublisher
1115: ErrorDocument 403 /adm/login
1116: ErrorDocument 404 /adm/notfound.html
1117: ErrorDocument 406 /adm/unauthorized.html
1118: ErrorDocument 500 /adm/errorhandler
1119: </Location>
1.1 www 1120:
1.66 harris41 1121: =head1 INTRODUCTION
1.1 www 1122:
1.66 harris41 1123: This module publishes a file. This involves gathering metadata,
1124: versioning the file, copying file from construction space to
1125: publication space, and copying metadata from construction space
1126: to publication space.
1127:
1128: This is part of the LearningOnline Network with CAPA project
1129: described at http://www.lon-capa.org.
1130:
1131: =head1 HANDLER SUBROUTINE
1132:
1133: This routine is called by Apache and mod_perl.
1134:
1135: =over 4
1136:
1137: =item *
1138:
1139: Get query string for limited number of parameters
1140:
1141: =item *
1142:
1143: Check filename
1144:
1145: =item *
1146:
1147: File is there and owned, init lookup tables
1148:
1149: =item *
1150:
1151: Start page output
1152:
1153: =item *
1154:
1155: Individual file
1156:
1157: =item *
1158:
1159: publish from $thisfn to $thistarget with $thisembstyle
1160:
1161: =back
1162:
1163: =head1 OTHER SUBROUTINES
1164:
1165: =over 4
1166:
1167: =item *
1168:
1169: metaeval() : Evaluate string with metadata
1170:
1171: =item *
1172:
1173: metaread() : Read a metadata file
1174:
1175: =item *
1176:
1177: sqltime() : convert 'time' format into a datetime sql format
1178:
1179: =item *
1180:
1181: textfield() : form field
1182:
1183: =item *
1184:
1185: hiddenfield() : form field
1186:
1187: =item *
1188:
1189: selectbox() : form field
1190:
1191: =item *
1192:
1193: urlfixup() : fixup URL (Publication Step One)
1194:
1195: =item *
1196:
1197: publish() : publish (Publication Step One)
1198:
1199: =item *
1200:
1201: phasetwo() : render second interface showing status of publication steps
1202: (Publication Step Two)
1203:
1204: =back
1205:
1206: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>