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