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