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