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