Annotation of loncom/publisher/lonpublisher.pm, revision 1.65
1.1 www 1: # The LearningOnline Network with CAPA
2: # Publication Handler
1.54 albertel 3: #
1.65 ! harris41 4: # $Id: lonpublisher.pm,v 1.64 2001/12/07 22:37:56 www Exp $
1.54 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
27: #
1.1 www 28: #
29: # (TeX Content Handler
30: #
31: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
32: #
1.15 www 33: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
1.20 www 34: # 03/23 Guy Albertelli
1.23 www 35: # 03/24,03/29,04/03 Gerd Kortemeyer
1.24 harris41 36: # 04/16/2001 Scott Harrison
1.27 www 37: # 05/03,05/05,05/07 Gerd Kortemeyer
1.30 harris41 38: # 05/28/2001 Scott Harrison
1.51 www 39: # 06/23,08/07,08/11,8/13,8/17,8/18,8/24,9/26,10/16 Gerd Kortemeyer
1.58 www 40: # 12/04,12/05 Guy Albertelli
41: # 12/05 Gerd Kortemeyer
1.62 www 42: # 12/05 Guy Albertelli
1.64 www 43: # 12/06,12/07 Gerd Kortemeyer
1.65 ! harris41 44: # 12/15 Scott Harrison
! 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';
760: print $logfile "\nPrivate: Did not ynchronized SQL metadata database";
761: }
1.12 www 762: # ----------------------------------------------------------- Copy old versions
763:
764: if (-e $target) {
765: my $filename;
766: my $maxversion=0;
767: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
768: my $srcf=$2;
769: my $srct=$3;
770: my $srcd=$1;
771: unless ($srcd=~/^\/home\/httpd\/html\/res/) {
772: print $logfile "\nPANIC: Target dir is ".$srcd;
773: return "<font color=red>Invalid target directory, FAIL</font>";
774: }
775: opendir(DIR,$srcd);
776: while ($filename=readdir(DIR)) {
777: if ($filename=~/$srcf\.(\d+)\.$srct$/) {
778: $maxversion=($1>$maxversion)?$1:$maxversion;
779: }
780: }
781: closedir(DIR);
782: $maxversion++;
783: $scrout.='<p>Creating old version '.$maxversion;
784: print $logfile "\nCreating old version ".$maxversion;
785:
786: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
787:
1.13 www 788: if (copy($target,$copyfile)) {
1.12 www 789: print $logfile "Copied old target to ".$copyfile."\n";
790: $scrout.='<p>Copied old target file';
791: } else {
1.13 www 792: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
793: return "<font color=red>Failed to copy old target, $!, FAIL</font>";
1.12 www 794: }
795:
796: # --------------------------------------------------------------- Copy Metadata
797:
798: $copyfile=$copyfile.'.meta';
1.13 www 799:
800: if (copy($target.'.meta',$copyfile)) {
1.14 www 801: print $logfile "Copied old target metadata to ".$copyfile."\n";
1.12 www 802: $scrout.='<p>Copied old metadata';
803: } else {
1.13 www 804: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14 www 805: if (-e $target.'.meta') {
806: return
1.13 www 807: "<font color=red>Failed to write old metadata copy, $!, FAIL</font>";
1.14 www 808: }
1.12 www 809: }
1.11 www 810:
811:
1.12 www 812: } else {
813: $scrout.='<p>Initial version';
814: print $logfile "\nInitial version";
815: }
816:
817: # ---------------------------------------------------------------- Write Source
818: my $copyfile=$target;
819:
820: my @parts=split(/\//,$copyfile);
821: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
822:
823: my $count;
824: for ($count=5;$count<$#parts;$count++) {
825: $path.="/$parts[$count]";
826: if ((-e $path)!=1) {
827: print $logfile "\nCreating directory ".$path;
828: $scrout.='<p>Created directory '.$parts[$count];
829: mkdir($path,0777);
830: }
831: }
832:
1.13 www 833: if (copy($source,$copyfile)) {
1.12 www 834: print $logfile "Copied original source to ".$copyfile."\n";
835: $scrout.='<p>Copied source file';
836: } else {
1.13 www 837: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
838: return "<font color=red>Failed to copy source, $!, FAIL</font>";
1.12 www 839: }
840:
841: # --------------------------------------------------------------- Copy Metadata
842:
1.13 www 843: $copyfile=$copyfile.'.meta';
844:
845: if (copy($source.'.meta',$copyfile)) {
1.12 www 846: print $logfile "Copied original metadata to ".$copyfile."\n";
847: $scrout.='<p>Copied metadata';
848: } else {
1.13 www 849: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.12 www 850: return
1.13 www 851: "<font color=red>Failed to write metadata copy, $!, FAIL</font>";
1.12 www 852: }
853:
854: # --------------------------------------------------- Send update notifications
855:
856: {
857:
858: my $filename;
859:
860: $target=~/(.*)\/([^\/]+)$/;
861: my $srcf=$2;
862: opendir(DIR,$1);
863: while ($filename=readdir(DIR)) {
864: if ($filename=~/$srcf\.(\w+)$/) {
865: my $subhost=$1;
866: if ($subhost ne 'meta') {
867: $scrout.='<p>Notifying host '.$subhost.':';
868: print $logfile "\nNotifying host '.$subhost.':'";
869: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1.20 www 870: $scrout.=$reply;
871: print $logfile $reply;
872: }
873: }
874: }
875: closedir(DIR);
876:
877: }
878:
879: # ---------------------------------------- Send update notifications, meta only
880:
881: {
882:
883: my $filename;
884:
885: $target=~/(.*)\/([^\/]+)$/;
886: my $srcf=$2.'.meta';
887: opendir(DIR,$1);
888: while ($filename=readdir(DIR)) {
889: if ($filename=~/$srcf\.(\w+)$/) {
890: my $subhost=$1;
891: if ($subhost ne 'meta') {
892: $scrout.=
893: '<p>Notifying host for metadata only '.$subhost.':';
894: print $logfile
895: "\nNotifying host for metadata only '.$subhost.':'";
896: my $reply=&Apache::lonnet::critical(
897: 'update:'.$target.'.meta',$subhost);
1.12 www 898: $scrout.=$reply;
899: print $logfile $reply;
900: }
901: }
902: }
903: closedir(DIR);
904:
905: }
906:
907: # ------------------------------------------------ Provide link to new resource
908:
909: my $thisdistarget=$target;
910: $thisdistarget=~s/^$docroot//;
911:
1.22 www 912: my $thissrc=$source;
913: $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
914:
915: my $thissrcdir=$thissrc;
916: $thissrcdir=~s/\/[^\/]+$/\//;
917:
918:
1.29 harris41 919: return $warning.$scrout.
1.22 www 920: '<hr><a href="'.$thisdistarget.'"><font size=+2>View Target</font></a>'.
921: '<p><a href="'.$thissrc.'"><font size=+2>Back to Source</font></a>'.
922: '<p><a href="'.$thissrcdir.
923: '"><font size=+2>Back to Source Directory</font></a>';
924:
1.11 www 925: }
926:
1.1 www 927: # ================================================================ Main Handler
928:
929: sub handler {
930: my $r=shift;
1.2 www 931:
932: if ($r->header_only) {
933: $r->content_type('text/html');
934: $r->send_http_header;
935: return OK;
936: }
937:
1.43 www 938: # Get query string for limited number of parameters
939:
1.65 ! harris41 940: foreach (split(/&/,$ENV{'QUERY_STRING'})) {
1.43 www 941: my ($name, $value) = split(/=/,$_);
942: $value =~ tr/+/ /;
943: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
944: if ($name eq 'filename') {
945: unless ($ENV{'form.'.$name}) {
946: $ENV{'form.'.$name}=$value;
947: }
948: }
1.65 ! harris41 949: }
1.43 www 950:
951:
1.2 www 952: # -------------------------------------------------------------- Check filename
953:
954: my $fn=$ENV{'form.filename'};
955:
1.27 www 956:
1.2 www 957: unless ($fn) {
1.27 www 958: $r->log_reason($cuname.' at '.$cudom.
1.2 www 959: ' trying to publish empty filename', $r->filename);
960: return HTTP_NOT_FOUND;
961: }
1.4 www 962:
1.31 www 963: ($cuname,$cudom)=
964: &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
965: unless (($cuname) && ($cudom)) {
1.27 www 966: $r->log_reason($cuname.' at '.$cudom.
1.4 www 967: ' trying to publish file '.$ENV{'form.filename'}.
1.27 www 968: ' ('.$fn.') - not authorized',
969: $r->filename);
970: return HTTP_NOT_ACCEPTABLE;
971: }
972:
973: unless (&Apache::lonnet::homeserver($cuname,$cudom)
974: eq $r->dir_config('lonHostID')) {
975: $r->log_reason($cuname.' at '.$cudom.
976: ' trying to publish file '.$ENV{'form.filename'}.
977: ' ('.$fn.') - not homeserver ('.
978: &Apache::lonnet::homeserver($cuname,$cudom).')',
1.4 www 979: $r->filename);
980: return HTTP_NOT_ACCEPTABLE;
981: }
1.2 www 982:
1.43 www 983: $fn=~s/^http\:\/\/[^\/]+//;
984: $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
1.2 www 985:
986: my $targetdir='';
1.12 www 987: $docroot=$r->dir_config('lonDocRoot');
1.27 www 988: if ($1 ne $cuname) {
989: $r->log_reason($cuname.' at '.$cudom.
1.2 www 990: ' trying to publish unowned file '.$ENV{'form.filename'}.
991: ' ('.$fn.')',
992: $r->filename);
993: return HTTP_NOT_ACCEPTABLE;
994: } else {
1.27 www 995: $targetdir=$docroot.'/res/'.$cudom;
1.2 www 996: }
997:
998:
999: unless (-e $fn) {
1.27 www 1000: $r->log_reason($cuname.' at '.$cudom.
1.2 www 1001: ' trying to publish non-existing file '.$ENV{'form.filename'}.
1002: ' ('.$fn.')',
1003: $r->filename);
1004: return HTTP_NOT_FOUND;
1005: }
1006:
1.11 www 1007: unless ($ENV{'form.phase'} eq 'two') {
1008:
1.2 www 1009: # --------------------------------- File is there and owned, init lookup tables
1010:
1.3 www 1011: %addid=();
1012:
1013: {
1014: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
1015: while (<$fh>=~/(\w+)\s+(\w+)/) {
1016: $addid{$1}=$2;
1017: }
1.5 www 1018: }
1019:
1020: %nokey=();
1021:
1022: {
1023: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
1.65 ! harris41 1024: while (<$fh>) {
1.5 www 1025: my $word=$_;
1026: chomp($word);
1027: $nokey{$word}=1;
1.65 ! harris41 1028: }
1.3 www 1029: }
1.11 www 1030:
1031: }
1032:
1.2 www 1033: # ----------------------------------------------------------- Start page output
1034:
1.1 www 1035: $r->content_type('text/html');
1036: $r->send_http_header;
1037:
1038: $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
1.15 www 1039: $r->print(
1040: '<body bgcolor="#FFFFFF"><img align=right src=/adm/lonIcons/lonlogos.gif>');
1.2 www 1041: my $thisfn=$fn;
1042:
1043: # ------------------------------------------------------------- Individual file
1044: {
1045: $thisfn=~/\.(\w+)$/;
1046: my $thistype=$1;
1.65 ! harris41 1047: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1.2 www 1048:
1049: my $thistarget=$thisfn;
1050:
1051: $thistarget=~s/^\/home/$targetdir/;
1052: $thistarget=~s/\/public\_html//;
1053:
1054: my $thisdistarget=$thistarget;
1055: $thisdistarget=~s/^$docroot//;
1056:
1057: my $thisdisfn=$thisfn;
1.27 www 1058: $thisdisfn=~s/^\/home\/$cuname\/public_html\///;
1.2 www 1059:
1060: $r->print('<h2>Publishing '.
1061: &Apache::lonnet::filedescription($thistype).' <tt>'.
1062: $thisdisfn.'</tt></h2><b>Target:</b> <tt>'.$thisdistarget.'</tt><p>');
1.27 www 1063:
1064: if (($cuname ne $ENV{'user.name'}) || ($cudom ne $ENV{'user.domain'})) {
1065: $r->print('<h3><font color=red>Co-Author: '.$cuname.' at '.$cudom.
1066: '</font></h3>');
1067: }
1.26 www 1068:
1.65 ! harris41 1069: if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
1.28 www 1070: $r->print('<br><a href="/adm/diff?filename=/~'.$cuname.'/'.
1071: $thisdisfn.
1.26 www 1072: '&versionone=priv" target=cat>Diffs with Current Version</a><p>');
1073: }
1.11 www 1074:
1.2 www 1075: # ------------ We are publishing from $thisfn to $thistarget with $thisembstyle
1076:
1.11 www 1077: unless ($ENV{'form.phase'} eq 'two') {
1.27 www 1078: $r->print(
1079: '<hr>'.&publish($thisfn,$thistarget,$thisembstyle));
1.11 www 1080: } else {
1.27 www 1081: $r->print(
1082: '<hr>'.&phasetwo($thisfn,$thistarget,$thisembstyle,$thisdistarget));
1.11 www 1083: }
1.2 www 1084:
1.11 www 1085: }
1.1 www 1086: $r->print('</body></html>');
1.15 www 1087:
1.1 www 1088: return OK;
1089: }
1090:
1091: 1;
1092: __END__
1093:
1094:
1095:
1096:
1097:
1098:
1099:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>