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