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