1: # The LearningOnline Network with CAPA
2: # Publication Handler
3: #
4: # $Id: lonpublisher.pm,v 1.295 2016/03/22 16:41:10 raeburn Exp $
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: #
28: ###
29:
30: ###############################################################################
31: ## ##
32: ## ORGANIZATION OF THIS PERL MODULE ##
33: ## ##
34: ## 1. Modules used by this module ##
35: ## 2. Various subroutines ##
36: ## 3. Publication Step One ##
37: ## 4. Phase Two ##
38: ## 5. Main Handler ##
39: ## ##
40: ###############################################################################
41:
42:
43: ######################################################################
44: ######################################################################
45:
46: =pod
47:
48: =head1 NAME
49:
50: lonpublisher - LON-CAPA publishing handler
51:
52: =head1 SYNOPSIS
53:
54: B<lonpublisher> is used by B<mod_perl> inside B<Apache>. This is the
55: invocation by F<loncapa_apache.conf>:
56:
57: <Location /adm/publish>
58: PerlAccessHandler Apache::lonacc
59: SetHandler perl-script
60: PerlHandler Apache::lonpublisher
61: ErrorDocument 403 /adm/login
62: ErrorDocument 404 /adm/notfound.html
63: ErrorDocument 406 /adm/unauthorized.html
64: ErrorDocument 500 /adm/errorhandler
65: </Location>
66:
67: =head1 OVERVIEW
68:
69: Authors can only write-access the C</priv/domain/authorname/> space.
70: They can copy resources into the resource area through the
71: publication step, and move them back through a recover step.
72: Authors do not have direct write-access to their resource space.
73:
74: During the publication step, several events will be
75: triggered. Metadata is gathered, where a wizard manages default
76: entries on a hierarchical per-directory base: The wizard imports the
77: metadata (including access privileges and royalty information) from
78: the most recent published resource in the current directory, and if
79: that is not available, from the next directory above, etc. The Network
80: keeps all previous versions of a resource and makes them available by
81: an explicit version number, which is inserted between the file name
82: and extension, for example C<foo.2.html>, while the most recent
83: version does not carry a version number (C<foo.html>). Servers
84: subscribing to a changed resource are notified that a new version is
85: available.
86:
87: =head1 DESCRIPTION
88:
89: B<lonpublisher> takes the proper steps to add resources to the LON-CAPA
90: digital library. This includes updating the metadata table in the
91: LON-CAPA database.
92:
93: B<lonpublisher> is many things to many people.
94:
95: This module publishes a file. This involves gathering metadata,
96: versioning the file, copying file from construction space to
97: publication space, and copying metadata from construction space
98: to publication space.
99:
100: =head2 SUBROUTINES
101:
102: Many of the undocumented subroutines implement various magical
103: parsing shortcuts.
104:
105: =cut
106:
107: ######################################################################
108: ######################################################################
109:
110:
111: package Apache::lonpublisher;
112:
113: # ------------------------------------------------- modules used by this module
114: use strict;
115: use Apache::File;
116: use File::Copy;
117: use Apache::Constants qw(:common :http :methods);
118: use HTML::LCParser;
119: use HTML::Entities;
120: use Encode::Encoder;
121: use Apache::lonxml;
122: use DBI;
123: use Apache::lonnet;
124: use Apache::loncommon();
125: use Apache::lonhtmlcommon;
126: use Apache::lonmysql;
127: use Apache::lonlocal;
128: use Apache::loncfile;
129: use LONCAPA::lonmetadata;
130: use Apache::lonmsg;
131: use vars qw(%metadatafields %metadatakeys);
132: use LONCAPA qw(:DEFAULT :match);
133:
134:
135: my %addid;
136: my %nokey;
137:
138: my $docroot;
139:
140: my $cuname;
141: my $cudom;
142:
143: my $registered_cleanup;
144: my $modified_urls;
145:
146: my $lock;
147:
148: =pod
149:
150: =over 4
151:
152: =item B<metaeval>
153:
154: Evaluates a string that contains metadata. This subroutine
155: stores values inside I<%metadatafields> and I<%metadatakeys>.
156: The hash key is a I<$unikey> corresponding to a unique id
157: that is descriptive of the parser location inside the XML tree.
158:
159: Parameters:
160:
161: =over 4
162:
163: =item I<$metastring>
164:
165: A string that contains metadata.
166:
167: =back
168:
169: Returns:
170:
171: nothing
172:
173: =cut
174:
175: #########################################
176: #########################################
177: #
178: # Modifies global %metadatafields %metadatakeys
179: #
180:
181: sub metaeval {
182: my ($metastring,$prefix)=@_;
183:
184: my $parser=HTML::LCParser->new(\$metastring);
185: my $token;
186: while ($token=$parser->get_token) {
187: if ($token->[0] eq 'S') {
188: my $entry=$token->[1];
189: my $unikey=$entry;
190: next if ($entry =~ m/^(?:parameter|stores)_/);
191: if (defined($token->[2]->{'package'})) {
192: $unikey.="\0package\0".$token->[2]->{'package'};
193: }
194: if (defined($token->[2]->{'part'})) {
195: $unikey.="\0".$token->[2]->{'part'};
196: }
197: if (defined($token->[2]->{'id'})) {
198: $unikey.="\0".$token->[2]->{'id'};
199: }
200: if (defined($token->[2]->{'name'})) {
201: $unikey.="\0".$token->[2]->{'name'};
202: }
203: foreach my $item (@{$token->[3]}) {
204: $metadatafields{$unikey.'.'.$item}=$token->[2]->{$item};
205: if ($metadatakeys{$unikey}) {
206: $metadatakeys{$unikey}.=','.$item;
207: } else {
208: $metadatakeys{$unikey}=$item;
209: }
210: }
211: my $newentry=$parser->get_text('/'.$entry);
212: if (($entry eq 'customdistributionfile') ||
213: ($entry eq 'sourcerights')) {
214: $newentry=~s/^\s*//;
215: if ($newentry !~m|^/res|) { $newentry=$prefix.$newentry; }
216: }
217: # actually store
218: if ( $entry eq 'rule' && exists($metadatafields{$unikey})) {
219: $metadatafields{$unikey}.=','.$newentry;
220: } else {
221: $metadatafields{$unikey}=$newentry;
222: }
223: }
224: }
225: }
226:
227: #########################################
228: #########################################
229:
230: =pod
231:
232: =item B<metaread>
233:
234: Read a metadata file
235:
236: Parameters:
237:
238: =over
239:
240: =item I<$logfile>
241:
242: File output stream to output errors and warnings to.
243:
244: =item I<$fn>
245:
246: File name (including path).
247:
248: =back
249:
250: Returns:
251:
252: =over 4
253:
254: =item Scalar string (if successful)
255:
256: XHTML text that indicates successful reading of the metadata.
257:
258: =back
259:
260: =cut
261:
262: #########################################
263: #########################################
264: sub metaread {
265: my ($logfile,$fn,$prefix)=@_;
266: unless (-e $fn) {
267: print($logfile 'No file '.$fn."\n");
268: return '<p class="LC_warning">'
269: .&mt('No file: [_1]',&Apache::loncfile::display($fn))
270: .'</p>';
271: }
272: print($logfile 'Processing '.$fn."\n");
273: my $metastring;
274: {
275: my $metafh=Apache::File->new($fn);
276: $metastring=join('',<$metafh>);
277: }
278: &metaeval($metastring,$prefix);
279: return '<p class="LC_info">'
280: .&mt('Processed file: [_1]',&Apache::loncfile::display($fn))
281: .'</p>';
282: }
283:
284: #########################################
285: #########################################
286:
287: sub coursedependencies {
288: my $url=&Apache::lonnet::declutter(shift);
289: $url=~s/\.meta$//;
290: my ($adomain,$aauthor)=($url=~ m{^($match_domain)/($match_username)/});
291: my $regexp=quotemeta($url);
292: $regexp='___'.$regexp.'___course';
293: my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
294: $aauthor,$regexp);
295: my %courses=();
296: foreach my $item (keys(%evaldata)) {
297: if ($item=~/^([a-zA-Z0-9]+_[a-zA-Z0-9]+)___.+___course$/) {
298: $courses{$1}=1;
299: }
300: }
301: return %courses;
302: }
303: #########################################
304: #########################################
305:
306:
307: =pod
308:
309: =item Form-field-generating subroutines.
310:
311: For input parameters, these subroutines take in values
312: such as I<$name>, I<$value> and other form field metadata.
313: The output (scalar string that is returned) is an XHTML
314: string which presents the form field (foreseeably inside
315: <form></form> tags).
316:
317: =over 4
318:
319: =item B<textfield>
320:
321: =item B<text_with_browse_field>
322:
323: =item B<hiddenfield>
324:
325: =item B<checkbox>
326:
327: =item B<selectbox>
328:
329: =back
330:
331: =cut
332:
333: #########################################
334: #########################################
335: sub textfield {
336: my ($title,$name,$value,$noline)=@_;
337: $value=~s/^\s+//gs;
338: $value=~s/\s+$//gs;
339: $value=~s/\s+/ /gs;
340: $title=&mt($title);
341: $env{'form.'.$name}=$value;
342: return "\n".&Apache::lonhtmlcommon::row_title($title)
343: .'<input type="text" name="'.$name.'" size="80" value="'.$value.'" />'
344: .&Apache::lonhtmlcommon::row_closure($noline);
345: }
346:
347: sub text_with_browse_field {
348: my ($title,$name,$value,$restriction,$noline)=@_;
349: $value=~s/^\s+//gs;
350: $value=~s/\s+$//gs;
351: $value=~s/\s+/ /gs;
352: $title=&mt($title);
353: $env{'form.'.$name}=$value;
354: return "\n".&Apache::lonhtmlcommon::row_title($title)
355: .'<input type="text" name="'.$name.'" size="80" value="'.$value.'" />'
356: .'<br />'
357: .'<a href="javascript:openbrowser(\'pubform\',\''.$name.'\',\''.$restriction.'\');">'
358: .&mt('Select')
359: .'</a> '
360: .'<a href="javascript:opensearcher(\'pubform\',\''.$name.'\');">'
361: .&mt('Search')
362: .'</a>'
363: .&Apache::lonhtmlcommon::row_closure($noline);
364: }
365:
366: sub hiddenfield {
367: my ($name,$value)=@_;
368: $env{'form.'.$name}=$value;
369: return "\n".'<input type="hidden" name="'.$name.'" value="'.$value.'" />';
370: }
371:
372: sub checkbox {
373: my ($name,$text)=@_;
374: return "\n<br /><label><input type='checkbox' name='$name' /> ".
375: &mt($text)."</label>";
376: }
377:
378: sub selectbox {
379: my ($title,$name,$value,$functionref,@idlist)=@_;
380: $title=&mt($title);
381: $value=(split(/\s*,\s*/,$value))[-1];
382: if (defined($value)) {
383: $env{'form.'.$name}=$value;
384: } else {
385: $env{'form.'.$name}=$idlist[0];
386: }
387: my $selout="\n".&Apache::lonhtmlcommon::row_title($title)
388: .'<select name="'.$name.'">';
389: foreach my $id (@idlist) {
390: $selout.='<option value="'.$id.'"';
391: if ($id eq $value) {
392: $selout.=' selected="selected"';
393: }
394: $selout.='>'.&{$functionref}($id).'</option>';
395: }
396: $selout.='</select>'.&Apache::lonhtmlcommon::row_closure();
397: return $selout;
398: }
399:
400: sub select_level_form {
401: my ($value,$name)=@_;
402: $env{'form.'.$name}=$value;
403: if (!defined($value)) { $env{'form.'.$name}=0; }
404: return &Apache::loncommon::select_level_form($value,$name);
405: }
406:
407: sub common_access {
408: my ($name,$text,$options)=@_;
409: return unless (ref($options) eq 'ARRAY');
410: my $formname = 'pubdirpref';
411: my $chkname = 'common'.$name;
412: my $chkid = 'LC_'.$chkname;
413: my $divid = $chkid.'div';
414: my $customdivid = 'LC_customfile';
415: my $selname = $chkname.'select';
416: my $selid = $chkid.'select';
417: my $selonchange;
418: if ($name eq 'dist') {
419: $selonchange = ' onchange="showHideCustom(this,'."'$customdivid'".');"';
420: }
421: my %lt = &Apache::lonlocal::texthash(
422: 'default' => 'System wide - can be used for any courses system wide',
423: 'domain' => 'Domain only - use limited to courses in the domai',
424: 'custom' => 'Customized right of use ...',
425: 'public' => 'Public - no authentication or authorization required for use',
426: 'closed' => 'Closed - XML source is closed to everyone',
427: 'open' => 'Open - XML source is open to people who want to use it',
428: 'sel' => 'Select',
429: );
430: my $output = <<"END";
431: <br />
432: <span class="LC_nobreak">
433: <label>
434: <input type="checkbox" name="commonaccess" value="$name" id="$chkid"
435: onclick="showHideAccess(this,'$divid');" />
436: $text</label></span>
437: <div id="$divid" style="padding:0;clear:both;margin:0;border:0;display:none">
438: <select name="$selname" id="$selid" $selonchange>
439: <option value="" selected="selected">$lt{'sel'}</option>
440: END
441: foreach my $val (@{$options}) {
442: $output .= '<option value="'.$val.'">'.$lt{$val}.'</option>'."\n";
443: }
444: $output .= '
445: </select>';
446: if ($name eq 'dist') {
447: $output .= <<"END";
448: <div id="$customdivid" style="padding:0;clear:both;margin:0;border:0;display:none">
449: <input type="text" name="commoncustomrights" size="60" value="" />
450: <a href="javascript:openbrowser('$formname','commoncustomrights','rights');">
451: $lt{'sel'}</a></div>
452: END
453: }
454: $output .= '
455: </div>
456: ';
457: }
458:
459: #########################################
460: #########################################
461:
462: =pod
463:
464: =item B<urlfixup>
465:
466: Fix up a url? First step of publication
467:
468: =cut
469:
470: #########################################
471: #########################################
472: sub urlfixup {
473: my ($url,$target)=@_;
474: unless ($url) { return ''; }
475: #javascript code needs no fixing
476: if ($url =~ /^javascript:/i) { return $url; }
477: if ($url =~ /^mailto:/i) { return $url; }
478: #internal document links need no fixing
479: if ($url =~ /^\#/) { return $url; }
480: my ($host)=($url=~m{(?:(?:http|https|ftp)://)*([^/]+)});
481: my @lonids = &Apache::lonnet::machine_ids($host);
482: if (@lonids) {
483: $url=~s{^(?:http|https|ftp)://}{};
484: $url=~s/^\Q$host\E//;
485: }
486: if ($url=~m{^(?:http|https|ftp)://}) { return $url; }
487: $url=~s{\Q~$cuname\E}{res/$cudom/$cuname};
488: return $url;
489: }
490:
491: #########################################
492: #########################################
493:
494: =pod
495:
496: =item B<absoluteurl>
497:
498: Currently undocumented.
499:
500: =cut
501:
502: #########################################
503: #########################################
504: sub absoluteurl {
505: my ($url,$target)=@_;
506: unless ($url) { return ''; }
507: if ($target) {
508: $target=~s/\/[^\/]+$//;
509: $url=&Apache::lonnet::hreflocation($target,$url);
510: }
511: return $url;
512: }
513:
514: #########################################
515: #########################################
516:
517: =pod
518:
519: =item B<set_allow>
520:
521: Currently undocumented
522:
523: =cut
524:
525: #########################################
526: #########################################
527: sub set_allow {
528: my ($allow,$logfile,$target,$tag,$oldurl,$type)=@_;
529: my $newurl=&urlfixup($oldurl,$target);
530: my $return_url=$oldurl;
531: print $logfile 'GUYURL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
532: if ($newurl ne $oldurl) {
533: $return_url=$newurl;
534: print $logfile 'URL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
535: }
536: if (($newurl !~ /^javascript:/i) &&
537: ($newurl !~ /^mailto:/i) &&
538: ($newurl !~ /^(?:http|https|ftp):/i) &&
539: ($newurl !~ /^\#/)) {
540: if (($type eq 'src') || ($type eq 'href')) {
541: if ($newurl =~ /^([^?]+)\?[^?]*$/) {
542: $newurl = $1;
543: }
544: }
545: $$allow{&absoluteurl($newurl,$target)}=1;
546: }
547: return $return_url;
548: }
549:
550: #########################################
551: #########################################
552:
553: =pod
554:
555: =item B<get_subscribed_hosts>
556:
557: Currently undocumented
558:
559: =cut
560:
561: #########################################
562: #########################################
563: sub get_subscribed_hosts {
564: my ($target)=@_;
565: my @subscribed;
566: my $filename;
567: $target=~/(.*)\/([^\/]+)$/;
568: my $srcf=$2;
569: opendir(DIR,$1);
570: # cycle through listed files, subscriptions used to exist
571: # as "filename.lonid"
572: while ($filename=readdir(DIR)) {
573: if ($filename=~/\Q$srcf\E\.($match_lonid)$/) {
574: my $subhost=$1;
575: if (($subhost ne 'meta'
576: && $subhost ne 'subscription'
577: && $subhost ne 'meta.subscription'
578: && $subhost ne 'tmp') &&
579: ($subhost ne $Apache::lonnet::perlvar{'lonHostID'})) {
580: push(@subscribed,$subhost);
581: }
582: }
583: }
584: closedir(DIR);
585: my $sh;
586: if ( $sh=Apache::File->new("$target.subscription") ) {
587: while (my $subline=<$sh>) {
588: if ($subline =~ /^($match_lonid):/) {
589: if ($1 ne $Apache::lonnet::perlvar{'lonHostID'}) {
590: push(@subscribed,$1);
591: }
592: }
593: }
594: }
595: return @subscribed;
596: }
597:
598:
599: #########################################
600: #########################################
601:
602: =pod
603:
604: =item B<get_max_ids_indices>
605:
606: Currently undocumented
607:
608: =cut
609:
610: #########################################
611: #########################################
612: sub get_max_ids_indices {
613: my ($content)=@_;
614: my $maxindex=10;
615: my $maxid=10;
616: my $needsfixup=0;
617: my $duplicateids=0;
618:
619: my %allids;
620: my %duplicatedids;
621:
622: my $parser=HTML::LCParser->new($content);
623: $parser->xml_mode(1);
624: my $token;
625: while ($token=$parser->get_token) {
626: if ($token->[0] eq 'S') {
627: my $counter;
628: if ($counter=$addid{$token->[1]}) {
629: if ($counter eq 'id') {
630: if (defined($token->[2]->{'id'}) &&
631: $token->[2]->{'id'} !~ /^\s*$/) {
632: $maxid=($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
633: if (exists($allids{$token->[2]->{'id'}})) {
634: $duplicateids=1;
635: $duplicatedids{$token->[2]->{'id'}}=1;
636: } else {
637: $allids{$token->[2]->{'id'}}=1;
638: }
639: } else {
640: $needsfixup=1;
641: }
642: } else {
643: if (defined($token->[2]->{'index'}) &&
644: $token->[2]->{'index'} !~ /^\s*$/) {
645: $maxindex=($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
646: } else {
647: $needsfixup=1;
648: }
649: }
650: }
651: }
652: }
653: return ($needsfixup,$maxid,$maxindex,$duplicateids,
654: (keys(%duplicatedids)));
655: }
656:
657: #########################################
658: #########################################
659:
660: =pod
661:
662: =item B<get_all_text_unbalanced>
663:
664: Currently undocumented
665:
666: =cut
667:
668: #########################################
669: #########################################
670: sub get_all_text_unbalanced {
671: #there is a copy of this in lonxml.pm
672: my($tag,$pars)= @_;
673: my $token;
674: my $result='';
675: $tag='<'.$tag.'>';
676: while ($token = $$pars[-1]->get_token) {
677: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
678: $result.=$token->[1];
679: } elsif ($token->[0] eq 'PI') {
680: $result.=$token->[2];
681: } elsif ($token->[0] eq 'S') {
682: $result.=$token->[4];
683: } elsif ($token->[0] eq 'E') {
684: $result.=$token->[2];
685: }
686: if ($result =~ /\Q$tag\E/s) {
687: ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
688: #&Apache::lonnet::logthis('Got a winner with leftovers ::'.$2);
689: #&Apache::lonnet::logthis('Result is :'.$1);
690: $redo=$tag.$redo;
691: push (@$pars,HTML::LCParser->new(\$redo));
692: $$pars[-1]->xml_mode('1');
693: last;
694: }
695: }
696: return $result
697: }
698:
699: #########################################
700: #########################################
701:
702: =pod
703:
704: =item B<fix_ids_and_indices>
705:
706: Currently undocumented
707:
708: =cut
709:
710: #########################################
711: #########################################
712: #Arguably this should all be done as a lonnet::ssi instead
713: sub fix_ids_and_indices {
714: my ($logfile,$source,$target)=@_;
715:
716: my %allow;
717: my $content;
718: {
719: my $org=Apache::File->new($source);
720: $content=join('',<$org>);
721: }
722:
723: my ($needsfixup,$maxid,$maxindex,$duplicateids,@duplicatedids)=
724: &get_max_ids_indices(\$content);
725:
726: print $logfile ("Got $needsfixup,$maxid,$maxindex,$duplicateids--".
727: join(', ',@duplicatedids));
728: if ($duplicateids) {
729: print $logfile "Duplicate ID(s) exist, ".join(', ',@duplicatedids)."\n";
730: my $outstring='<span class="LC_error">'.&mt('Unable to publish file, it contains duplicated ID(s), ID(s) need to be unique. The duplicated ID(s) are').': '.join(', ',@duplicatedids).'</span>';
731: return ($outstring,1);
732: }
733: if ($needsfixup) {
734: print $logfile "Needs ID and/or index fixup\n".
735: "Max ID : $maxid (min 10)\n".
736: "Max Index: $maxindex (min 10)\n";
737: }
738: my $outstring='';
739: my $responsecounter=1;
740: my @parser;
741: $parser[0]=HTML::LCParser->new(\$content);
742: $parser[-1]->xml_mode(1);
743: my $token;
744: while (@parser) {
745: while ($token=$parser[-1]->get_token) {
746: if ($token->[0] eq 'S') {
747: my $counter;
748: my $tag=$token->[1];
749: my $lctag=lc($tag);
750: if ($lctag eq 'allow') {
751: $allow{$token->[2]->{'src'}}=1;
752: next;
753: }
754: if ($lctag eq 'base') { next; }
755: if (($lctag eq 'part') || ($lctag eq 'problem')) {
756: $responsecounter=0;
757: }
758: if ($lctag=~/response$/) { $responsecounter++; }
759: if ($lctag eq 'import') { $responsecounter++; }
760: my %parms=%{$token->[2]};
761: $counter=$addid{$tag};
762: if (!$counter) { $counter=$addid{$lctag}; }
763: if ($counter) {
764: if ($counter eq 'id') {
765: unless (defined($parms{'id'}) &&
766: $parms{'id'}!~/^\s*$/) {
767: $maxid++;
768: $parms{'id'}=$maxid;
769: print $logfile 'ID(new) : '.$tag.':'.$maxid."\n";
770: } else {
771: print $logfile 'ID(kept): '.$tag.':'.$parms{'id'}."\n";
772: }
773: } elsif ($counter eq 'index') {
774: unless (defined($parms{'index'}) &&
775: $parms{'index'}!~/^\s*$/) {
776: $maxindex++;
777: $parms{'index'}=$maxindex;
778: print $logfile 'Index: '.$tag.':'.$maxindex."\n";
779: }
780: }
781: }
782: unless ($parms{'type'} eq 'zombie') {
783: foreach my $type ('src','href','background','bgimg') {
784: foreach my $key (keys(%parms)) {
785: if ($key =~ /^$type$/i) {
786: next if (($lctag eq 'img') && ($type eq 'src') &&
787: ($parms{$key} =~ m{^data\:image/gif;base64,}));
788: $parms{$key}=&set_allow(\%allow,$logfile,
789: $target,$tag,
790: $parms{$key},$type);
791: }
792: }
793: }
794: }
795: # probably a <randomlabel> image type <label>
796: # or a <image> tag inside <imageresponse>
797: if (($lctag eq 'label' && defined($parms{'description'}))
798: ||
799: ($lctag eq 'image')) {
800: my $next_token=$parser[-1]->get_token();
801: if ($next_token->[0] eq 'T') {
802: $next_token->[1] =~ s/[\n\r\f]+//g;
803: $next_token->[1]=&set_allow(\%allow,$logfile,
804: $target,$tag,
805: $next_token->[1]);
806: }
807: $parser[-1]->unget_token($next_token);
808: }
809: if ($lctag eq 'applet') {
810: my $codebase='';
811: my $havecodebase=0;
812: foreach my $key (keys(%parms)) {
813: if (lc($key) eq 'codebase') {
814: $codebase=$parms{$key};
815: $havecodebase=1;
816: }
817: }
818: if ($havecodebase) {
819: my $oldcodebase=$codebase;
820: unless ($oldcodebase=~/\/$/) {
821: $oldcodebase.='/';
822: }
823: $codebase=&urlfixup($oldcodebase,$target);
824: $codebase=~s/\/$//;
825: if ($codebase ne $oldcodebase) {
826: $parms{'codebase'}=$codebase;
827: print $logfile 'URL codebase: '.$tag.':'.
828: $oldcodebase.' - '.
829: $codebase."\n";
830: }
831: $allow{&absoluteurl($codebase,$target).'/*'}=1;
832: } else {
833: foreach my $key (keys(%parms)) {
834: if ($key =~ /(archive|code|object)/i) {
835: my $oldurl=$parms{$key};
836: my $newurl=&urlfixup($oldurl,$target);
837: $newurl=~s/\/[^\/]+$/\/\*/;
838: print $logfile 'Allow: applet '.lc($key).':'.
839: $oldurl.' allows '.$newurl."\n";
840: $allow{&absoluteurl($newurl,$target)}=1;
841: }
842: }
843: }
844: }
845: my $newparmstring='';
846: my $endtag='';
847: foreach my $parkey (keys(%parms)) {
848: if ($parkey eq '/') {
849: $endtag=' /';
850: } else {
851: my $quote=($parms{$parkey}=~/\"/?"'":'"');
852: $newparmstring.=' '.$parkey.'='.$quote.$parms{$parkey}.$quote;
853: }
854: }
855: if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
856: $outstring.='<'.$tag.$newparmstring.$endtag.'>';
857: if ($lctag eq 'm' || $lctag eq 'answer' || $lctag eq 'display' ||
858: $lctag eq 'tex') {
859: $outstring.=&get_all_text_unbalanced('/'.$lctag,\@parser);
860: } elsif ($lctag eq 'script') {
861: if ($parms{'type'} eq 'loncapa/perl') {
862: $outstring.=&get_all_text_unbalanced('/'.$lctag,\@parser);
863: } else {
864: my $script = &get_all_text_unbalanced('/'.$lctag,\@parser);
865: if ($script =~ m{\.set\w+(Src|Swf)\(["']}i) {
866: my @srcs = split(/\.set/i,$script);
867: if (scalar(@srcs) > 1) {
868: foreach my $item (@srcs) {
869: if ($item =~ m{^(FlashPlayerSwf|MediaSrc|XMPSrc|ConfigurationSrc|PosterImageSrc)\((['"])(?:(?!\2).)+\2\)}is) {
870: my $srctype = $1;
871: my $quote = $2;
872: my ($url) = ($item =~ m{^\Q$srctype($quote\E([^$quote]+)\Q$quote)\E});
873: $url = &urlfixup($url);
874: unless ($url=~m{^(?:http|https|ftp)://}) {
875: $allow{&absoluteurl($url,$target)}=1;
876: if ($srctype eq 'ConfigurationSrc') {
877: if ($url =~ m{^(.+/)configuration_express\.xml$}) {
878: #
879: # Camtasia 8.1: express_show/spritesheet.png needed, and included in zip archive.
880: # Not referenced directly in <main>.html or <main>_player.html files,
881: # so add this file to %allow (where <main> is name user gave to file/archive).
882: #
883: my $spritesheet = $1.'express_show/spritesheet.png';
884: $allow{&absoluteurl($spritesheet,$target)}=1;
885:
886: #
887: # Camtasia 8.4: skins/express_show/spritesheet.min.css needed, and included in zip archive.
888: # Not referenced directly in <main>.html or <main>_player.html files,
889: # so add this file to %allow (where <main> is name user gave to file/archive).
890: #
891: my $spritecss = $1.'express_show/spritesheet.min.css';
892: $allow{&absoluteurl($spritecss,$target)}=1;
893: }
894: } elsif ($srctype eq 'PosterImageSrc') {
895: if ($url =~ m{^(.+)_First_Frame\.png$}) {
896: my $prefix = $1;
897: #
898: # Camtasia 8.1: <main>_Thumbnails.png needed, and included in zip archive.
899: # Not referenced directly in <main>.html or <main>_player.html files,
900: # so add this file to %allow (where <main> is name user gave to file/archive).
901: #
902: my $thumbnail = $prefix.'_Thumbnails.png';
903: $allow{&absoluteurl($thumbnail,$target)}=1;
904: }
905: }
906: }
907: }
908: }
909: }
910: }
911: if ($script =~ m{\.addMediaSrc\((["'])((?!\1).+)\1\);}) {
912: my $src = $2;
913: if ($src) {
914: my $url = &urlfixup($src);
915: unless ($url=~m{^(?:http|https|ftp)://}) {
916: $allow{&absoluteurl($url,$target)}=1;
917: }
918: }
919: }
920: if ($script =~ /\(document,\s*(['"])script\1,\s*\[([^\]]+)\]\);/s) {
921: my $scriptslist = $2;
922: my @srcs = split(/\s*,\s*/,$scriptslist);
923: foreach my $src (@srcs) {
924: if ($src =~ /(["'])(?:(?!\1).)+\.js\1/) {
925: my $quote = $1;
926: my ($url) = ($src =~ m/\Q$quote\E([^$quote]+)\Q$quote\E/);
927: $url = &urlfixup($url);
928: unless ($url=~m{^(?:http|https|ftp)://}) {
929: $allow{&absoluteurl($url,$target)}=1;
930: }
931: }
932: }
933: }
934: if ($script =~ m{loadScript\(\s*(['"])((?:(?!\1).)+\.js)\1,\s*function}is) {
935: my $src = $2;
936: if ($src) {
937: my $url = &urlfixup($src);
938: unless ($url=~m{^(?:http|https|ftp)://}) {
939: $allow{&absoluteurl($url,$target)}=1;
940: }
941: }
942: }
943: $outstring .= $script;
944: }
945: }
946: } elsif ($token->[0] eq 'E') {
947: if ($token->[2]) {
948: unless ($token->[1] eq 'allow') {
949: $outstring.='</'.$token->[1].'>';
950: }
951: }
952: if ((($token->[1] eq 'part') || ($token->[1] eq 'problem'))
953: && (!$responsecounter)) {
954: my $outstring='<span class="LC_error">'.&mt('Found [_1] without responses. This resource cannot be published.',$token->[1]).'</span>';
955: return ($outstring,1);
956: }
957: } else {
958: $outstring.=$token->[1];
959: }
960: }
961: pop(@parser);
962: }
963:
964: if ($needsfixup) {
965: print $logfile "End of ID and/or index fixup\n".
966: "Max ID : $maxid (min 10)\n".
967: "Max Index: $maxindex (min 10)\n";
968: } else {
969: print $logfile "Does not need ID and/or index fixup\n";
970: }
971:
972: return ($outstring,0,%allow);
973: }
974:
975: #########################################
976: #########################################
977:
978: =pod
979:
980: =item B<store_metadata>
981:
982: Store the metadata in the metadata table in the loncapa database.
983: Uses lonmysql to access the database.
984:
985: Inputs: \%metadata
986:
987: Returns: (error,status). error is undef on success, status is undef on error.
988:
989: =cut
990:
991: #########################################
992: #########################################
993: sub store_metadata {
994: my %metadata = @_;
995: my $error;
996: # Determine if the table exists
997: my $status = &Apache::lonmysql::check_table('metadata');
998: if (! defined($status)) {
999: $error='<span class="LC_error">'
1000: .&mt('WARNING: Cannot connect to database!')
1001: .'</span>';
1002: &Apache::lonnet::logthis($error);
1003: return ($error,undef);
1004: }
1005: if ($status == 0) {
1006: # It would be nice to actually create the table....
1007: $error ='<span class="LC_error">'
1008: .&mt('WARNING: The metadata table does not exist in the LON-CAPA database!')
1009: .'</span>';
1010: &Apache::lonnet::logthis($error);
1011: return ($error,undef);
1012: }
1013: my $dbh = &Apache::lonmysql::get_dbh();
1014: if (($metadata{'obsolete'}) || ($metadata{'copyright'} eq 'priv')) {
1015: # remove this entry
1016: my $delitem = 'url = '.$dbh->quote($metadata{'url'});
1017: $status = &LONCAPA::lonmetadata::delete_metadata($dbh,undef,$delitem);
1018:
1019: } else {
1020: $status = &LONCAPA::lonmetadata::update_metadata($dbh,undef,undef,
1021: \%metadata);
1022: }
1023: if (defined($status) && $status ne '') {
1024: $error='<span class="LC_error">'
1025: .&mt('Error occurred saving new values in metadata table in LON-CAPA database!')
1026: .'</span>';
1027: &Apache::lonnet::logthis($error);
1028: &Apache::lonnet::logthis($status);
1029: return ($error,undef);
1030: }
1031: return (undef,'success');
1032: }
1033:
1034:
1035: # ========================================== Parse file for errors and warnings
1036:
1037: sub checkonthis {
1038: my ($r,$source)=@_;
1039: my $uri=&Apache::lonnet::hreflocation($source);
1040: $uri=~s/\/$//;
1041: my $result=&Apache::lonnet::ssi_body($uri,
1042: ('grade_target'=>'web',
1043: 'return_only_error_and_warning_counts' => 1));
1044: my ($errorcount,$warningcount)=split(':',$result);
1045: if (($errorcount) || ($warningcount)) {
1046: $r->print('<h3>'.&mt('Warnings and Errors').'</h3>');
1047: $r->print('<tt>'.$uri.'</tt>:');
1048: $r->print('<ul>');
1049: if ($warningcount) {
1050: $r->print('<li><div class="LC_warning">'
1051: .&mt('[quant,_1,warning]',$warningcount)
1052: .'</div></li>');
1053: }
1054: if ($errorcount) {
1055: $r->print('<li><div class="LC_error">'
1056: .&mt('[quant,_1,error]',$errorcount)
1057: .' <img src="/adm/lonMisc/bomb.gif" />'
1058: .'</div></li>');
1059: }
1060: $r->print('</ul>');
1061: } else {
1062: #$r->print('<font color="green">'.&mt('ok').'</font>');
1063: }
1064: $r->rflush();
1065: return ($warningcount,$errorcount);
1066: }
1067:
1068: # ============================================== Parse file itself for metadata
1069: #
1070: # parses a file with target meta, sets global %metadatafields %metadatakeys
1071:
1072: sub parseformeta {
1073: my ($source,$style)=@_;
1074: my $allmeta='';
1075: if (($style eq 'ssi') || ($style eq 'prv')) {
1076: my $dir=$source;
1077: $dir=~s-/[^/]*$--;
1078: my $file=$source;
1079: $file=(split('/',$file))[-1];
1080: $source=&Apache::lonnet::hreflocation($dir,$file);
1081: $allmeta=&Apache::lonnet::ssi_body($source,('grade_target' => 'meta'));
1082: &metaeval($allmeta);
1083: }
1084: return $allmeta;
1085: }
1086:
1087: #########################################
1088: #########################################
1089:
1090: =pod
1091:
1092: =item B<publish>
1093:
1094: This is the workhorse function of this module. This subroutine generates
1095: backup copies, performs any automatic processing (prior to publication,
1096: especially for rat and ssi files),
1097:
1098: Returns a 2 element array, the first is the string to be shown to the
1099: user, the second is an error code, either 1 (an error occurred) or 0
1100: (no error occurred)
1101:
1102: I<Additional documentation needed.>
1103:
1104: =cut
1105:
1106: #########################################
1107: #########################################
1108: sub publish {
1109:
1110: my ($source,$target,$style,$batch)=@_;
1111: my $logfile;
1112: my $scrout='';
1113: my $allmeta='';
1114: my $content='';
1115: my %allow=();
1116:
1117: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1118: return ('<span class="LC_error">'.&mt('No write permission to user directory, FAIL').'</span>',1);
1119: }
1120: print $logfile
1121: "\n\n================= Publish ".localtime()." Phase One ================\n".$env{'user.name'}.':'.$env{'user.domain'}."\n";
1122:
1123: if (($style eq 'ssi') || ($style eq 'rat') || ($style eq 'prv')) {
1124: # ------------------------------------------------------- This needs processing
1125:
1126: # ----------------------------------------------------------------- Backup Copy
1127: my $copyfile=$source.'.save';
1128: if (copy($source,$copyfile)) {
1129: print $logfile "Copied original file to ".$copyfile."\n";
1130: } else {
1131: print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
1132: return ("<span class=\"LC_error\">".&mt("Failed to write backup copy, [_1], FAIL",$1)."</span>",1);
1133: }
1134: # ------------------------------------------------------------- IDs and indices
1135:
1136: my ($outstring,$error);
1137: ($outstring,$error,%allow)=&fix_ids_and_indices($logfile,$source,
1138: $target);
1139: if ($error) { return ($outstring,$error); }
1140: # ------------------------------------------------------------ Construct Allows
1141:
1142: my $outdep=''; # Collect dependencies output data
1143: my $allowstr='';
1144: foreach my $thisdep (sort(keys(%allow))) {
1145: if ($thisdep !~ /[^\s]/) { next; }
1146: if ($thisdep =~/\$/) {
1147: $outdep.='<div class="LC_warning">'
1148: .&mt('The resource depends on another resource with variable filename, i.e., [_1].','<tt>'.$thisdep.'</tt>').'<br />'
1149: .&mt('You likely need to explicitly allow access to all possible dependencies using the [_1]-tag.','<tt><allow></tt>')
1150: ."</div>\n";
1151: }
1152: unless ($style eq 'rat') {
1153: $allowstr.="\n".'<allow src="'.$thisdep.'" />';
1154: }
1155: $outdep.='<div>';
1156: if ($thisdep!~/[\*\$]/ && $thisdep!~m|^/adm/|) {
1157: $outdep.='<a href="'.$thisdep.'">';
1158: }
1159: $outdep.='<tt>'.$thisdep.'</tt>';
1160: if ($thisdep!~/[\*\$]/ && $thisdep!~m|^/adm/|) {
1161: $outdep.='</a>';
1162: if (
1163: &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
1164: $thisdep.'.meta') eq '-1') {
1165: $outdep.= ' - <span class="LC_error">'.&mt('Currently not available').
1166: '</span>';
1167: } else {
1168: #
1169: # Store the fact that the dependency has been used by the target file
1170: # Unfortunately, usage is erroneously named sequsage in lonmeta.pm
1171: # The translation happens in lonmetadata.pm
1172: #
1173: my %temphash=(&Apache::lonnet::declutter($target).'___'.
1174: &Apache::lonnet::declutter($thisdep).'___usage'
1175: => time);
1176: $thisdep=~m{^/res/($match_domain)/($match_username)/};
1177: if ((defined($1)) && (defined($2))) {
1178: &Apache::lonnet::put('nohist_resevaldata',\%temphash,
1179: $1,$2);
1180: }
1181: }
1182: }
1183: $outdep.='</div><br />';
1184: }
1185:
1186: if ($outdep) {
1187: $scrout.='<h3>'.&mt('Dependencies').'</h3>'
1188: .$outdep
1189: }
1190: $outstring=~s/\n*(\<\/[^\>]+\>[^<]*)$/$allowstr\n$1\n/s;
1191:
1192: # ------------------------------------------------------------- Write modified.
1193:
1194: {
1195: my $org;
1196: unless ($org=Apache::File->new('>'.$source)) {
1197: print $logfile "No write permit to $source\n";
1198: return ('<span class="LC_error">'.&mt('No write permission to').
1199: ' '.$source.
1200: ', '.&mt('FAIL').'</span>',1);
1201: }
1202: print($org $outstring);
1203: }
1204: $content=$outstring;
1205:
1206: }
1207: # -------------------------------------------- Initial step done, now metadata.
1208:
1209: # --------------------------------------- Storage for metadata keys and fields.
1210: # these are globals
1211: #
1212: %metadatafields=();
1213: %metadatakeys=();
1214:
1215: my %oldparmstores=();
1216:
1217: unless ($batch) {
1218: $scrout.='<h3>'.&mt('Metadata').' ' .
1219: &Apache::loncommon::help_open_topic("Metadata_Description")
1220: . '</h3>';
1221: }
1222:
1223: # ------------------------------------------------ First, check out environment
1224: if ((!(-e $source.'.meta')) || ($env{'form.forceoverride'})) {
1225: $metadatafields{'author'}=$env{'environment.firstname'}.' '.
1226: $env{'environment.middlename'}.' '.
1227: $env{'environment.lastname'}.' '.
1228: $env{'environment.generation'};
1229: $metadatafields{'author'}=~s/\s+/ /g;
1230: $metadatafields{'author'}=~s/\s+$//;
1231: $metadatafields{'owner'}=$cuname.':'.$cudom;
1232:
1233: # ------------------------------------------------ Check out directory hierachy
1234:
1235: my $thisdisfn=$source;
1236:
1237: $thisdisfn=~s/^\Q$docroot\E\/priv\/\Q$cudom\E\/\Q$cuname\E\///;
1238: my @urlparts=('.',split(/\//,$thisdisfn));
1239: $#urlparts--;
1240:
1241: my $currentpath=$docroot.'/priv/'.$cudom.'/'.$cuname.'/';
1242:
1243: my $prefix='../'x($#urlparts);
1244: foreach my $subdir (@urlparts) {
1245: $currentpath.=$subdir.'/';
1246: $scrout.=&metaread($logfile,$currentpath.'default.meta',$prefix);
1247: $prefix=~s|^\.\./||;
1248: }
1249:
1250: # ----------------------------------------------------------- Parse file itself
1251: # read %metadatafields from file itself
1252:
1253: $allmeta=&parseformeta($source,$style);
1254:
1255: # ------------------- Clear out parameters and stores (there should not be any)
1256:
1257: foreach my $field (keys(%metadatafields)) {
1258: if (($field=~/^parameter/) || ($field=~/^stores/)) {
1259: delete $metadatafields{$field};
1260: }
1261: }
1262:
1263: } else {
1264: # ---------------------- Read previous metafile, remember parameters and stores
1265:
1266: $scrout.=&metaread($logfile,$source.'.meta');
1267:
1268: foreach my $field (keys(%metadatafields)) {
1269: if (($field=~/^parameter/) || ($field=~/^stores/)) {
1270: $oldparmstores{$field}=1;
1271: delete $metadatafields{$field};
1272: }
1273: }
1274: # ------------------------------------------------------------- Save some stuff
1275: my %savemeta=();
1276: if ($metadatafields{'title'}) { $savemeta{'title'}=$metadatafields{'title'}; }
1277: # ------------------------------------------ See if anything new in file itself
1278:
1279: $allmeta=&parseformeta($source,$style);
1280: # ----------------------------------------------------------- Restore the stuff
1281: foreach my $item (keys(%savemeta)) {
1282: $metadatafields{$item}=$savemeta{$item};
1283: }
1284: }
1285:
1286:
1287: # ---------------- Find and document discrepancies in the parameters and stores
1288:
1289: my $chparms='';
1290: foreach my $field (sort(keys(%metadatafields))) {
1291: if (($field=~/^parameter/) || ($field=~/^stores/)) {
1292: unless ($field=~/\.\w+$/) {
1293: unless ($oldparmstores{$field}) {
1294: my $disp_key = $field;
1295: $disp_key =~ tr/\0/_/;
1296: print $logfile ('New: '.$disp_key."\n");
1297: $chparms .= $disp_key.' ';
1298: }
1299: }
1300: }
1301: }
1302: if ($chparms) {
1303: $scrout.='<p><b>'.&mt('New parameters or saved values').
1304: ':</b> '.$chparms.'</p>';
1305: }
1306:
1307: $chparms='';
1308: foreach my $olditem (sort(keys(%oldparmstores))) {
1309: if (($olditem=~/^parameter/) || ($olditem=~/^stores/)) {
1310: unless (($metadatafields{$olditem.'.name'}) ||
1311: ($metadatafields{$olditem.'.package'}) || ($olditem=~/\.\w+$/)) {
1312: my $disp_key = $olditem;
1313: $disp_key =~ tr/\0/_/;
1314: print $logfile ('Obsolete: '.$disp_key."\n");
1315: $chparms.=$disp_key.' ';
1316: }
1317: }
1318: }
1319: if ($chparms) {
1320: $scrout.='<p><b>'.&mt('Obsolete parameters or saved values').':</b> '
1321: .$chparms.'</p>'
1322: .'<p class="LC_warning"><b>'.&mt('Warning!').'</b><br />'
1323: .&mt('If this resource is in active use, student performance data from the previous version may become inaccessible.')
1324: .'</p><hr />';
1325: }
1326: if ($metadatafields{'copyright'} eq 'priv') {
1327: $scrout.='<p class="LC_warning"><b>'.&mt('Warning!').'</b><br />'
1328: .&mt('Copyright/distribution option "Private" is no longer supported. Select another option from below. Consider "Custom Rights" for maximum control over the usage of your resource.')
1329: .'</p><hr />';
1330: }
1331:
1332: # ------------------------------------------------------- Now have all metadata
1333:
1334: my %keywords=();
1335:
1336: if (length($content)<500000) {
1337: my $textonly=$content;
1338: $textonly=~s/\<script[^\<]+\<\/script\>//g;
1339: $textonly=~s/\<m\>[^\<]+\<\/m\>//g;
1340: $textonly=~s/\<[^\>]*\>//g;
1341:
1342: #this is a work simplification for german authors for present
1343: $textonly=HTML::Entities::decode($textonly); #decode HTML-character
1344: $textonly=Encode::Encoder::encode('utf8', $textonly); #encode to perl internal unicode
1345: $textonly=~tr/A-ZÜÄÖ/a-züäö/; #add lowercase rule for german "Umlaute"
1346: $textonly=~s/[\$\&][a-z]\w*//g;
1347: $textonly=~s/[^a-z^ü^ä^ö^ß\s]//g; #dont delete german "Umlaute"
1348:
1349: foreach ($textonly=~m/[^\s]+/g) { #match all but whitespaces
1350: unless ($nokey{$_}) {
1351: $keywords{$_}=1;
1352: }
1353: }
1354:
1355:
1356: }
1357:
1358: foreach my $addkey (split(/[\"\'\,\;]/,$metadatafields{'keywords'})) {
1359: $addkey=~s/\s+/ /g;
1360: $addkey=~s/^\s//;
1361: $addkey=~s/\s$//;
1362: if ($addkey=~/\w/) {
1363: $keywords{$addkey}=1;
1364: }
1365: }
1366: # --------------------------------------------------- Now we also have keywords
1367: # =============================================================================
1368: # interactive mode html goes into $intr_scrout
1369: # batch mode throws away this HTML
1370: # additionally all of the field functions have a by product of setting
1371: # $env{'from.'..} so that it can be used by the phase two handler in
1372: # batch mode
1373:
1374: my $intr_scrout.='<br />'
1375: .'<form name="pubform" action="/adm/publish" method="post">';
1376: unless ($env{'form.makeobsolete'}) {
1377: $intr_scrout.='<p class="LC_warning">'
1378: .&mt('Searching for your resource will be based on the following metadata. Please provide as much data as possible.')
1379: .'</p>'
1380: .'<p><input type="submit" value="'
1381: .&mt('Finalize Publication')
1382: .'" /> <a href="'.&Apache::loncfile::url($source).'">'.&mt('Cancel').'</a></p>';
1383: }
1384: $intr_scrout.=&Apache::lonhtmlcommon::start_pick_box();
1385: $intr_scrout.=
1386: &hiddenfield('phase','two').
1387: &hiddenfield('filename',$env{'form.filename'}).
1388: &hiddenfield('allmeta',&escape($allmeta)).
1389: &hiddenfield('dependencies',join(',',keys(%allow)));
1390: unless ($env{'form.makeobsolete'}) {
1391: $intr_scrout.=
1392: &textfield('Title','title',$metadatafields{'title'}).
1393: &textfield('Author(s)','author',$metadatafields{'author'}).
1394: &textfield('Subject','subject',$metadatafields{'subject'});
1395: # --------------------------------------------------- Scan content for keywords
1396:
1397: my $keywords_help = &Apache::loncommon::help_open_topic("Publishing_Keywords");
1398: my $keywordout=<<"END";
1399: <script>
1400: function checkAll(field) {
1401: for (i = 0; i < field.length; i++)
1402: field[i].checked = true ;
1403: }
1404:
1405: function uncheckAll(field) {
1406: for (i = 0; i < field.length; i++)
1407: field[i].checked = false ;
1408: }
1409: </script>
1410: END
1411: $keywordout.="\n".&Apache::lonhtmlcommon::row_title(&mt('Keywords'))
1412: .$keywords_help
1413: .'<input type="button" value="'.&mt('check all').'" onclick="javascript:checkAll(document.pubform.keywords)" />'
1414: .'<input type="button" value="'.&mt('uncheck all').'" onclick="javascript:uncheckAll(document.pubform.keywords)" />'
1415: .'</p><br />'
1416: .&Apache::loncommon::start_data_table();
1417: my $cols_per_row = 10;
1418: my $colcount=0;
1419: my $wordcount=0;
1420: my $numkeywords = scalar(keys(%keywords));
1421:
1422: foreach my $word (sort(keys(%keywords))) {
1423: if ($colcount == 0) {
1424: $keywordout .= &Apache::loncommon::start_data_table_row();
1425: }
1426: $colcount++;
1427: $wordcount++;
1428: if (($wordcount == $numkeywords) && ($colcount < $cols_per_row)) {
1429: my $colspan = 1+$cols_per_row-$colcount;
1430: $keywordout .= '<td colspan="'.$colspan.'">';
1431: } else {
1432: $keywordout .= '<td>';
1433: }
1434: $keywordout.='<label><input type="checkbox" name="keywords" value="'.$word.'"';
1435: if ($metadatafields{'keywords'}) {
1436: if ($metadatafields{'keywords'}=~/\Q$word\E/) {
1437: $keywordout.=' checked="checked"';
1438: $env{'form.keywords'}.=$word.',';
1439: }
1440: } elsif (&Apache::loncommon::keyword($word)) {
1441: $keywordout.=' checked="checked"';
1442: $env{'form.keywords'}.=$word.',';
1443: }
1444: $keywordout.=' />'.$word.'</label></td>';
1445: if ($colcount == $cols_per_row) {
1446: $keywordout.=&Apache::loncommon::end_data_table_row();
1447: $colcount=0;
1448: }
1449: }
1450: if ($colcount > 0) {
1451: $keywordout .= &Apache::loncommon::end_data_table_row();
1452: }
1453:
1454: $env{'form.keywords'}=~s/\,$//;
1455:
1456: $keywordout.=&Apache::loncommon::end_data_table_row()
1457: .&Apache::loncommon::end_data_table()
1458: .&Apache::lonhtmlcommon::row_closure();
1459:
1460: $intr_scrout.=$keywordout;
1461:
1462: $intr_scrout.=&textfield('Additional Keywords','addkey','');
1463:
1464: $intr_scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1465:
1466: $intr_scrout.="\n".&Apache::lonhtmlcommon::row_title(&mt('Abstract'))
1467: .'<textarea cols="80" rows="5" name="abstract">'
1468: .$metadatafields{'abstract'}
1469: .'</textarea>'
1470: .&Apache::lonhtmlcommon::row_closure();
1471:
1472: $source=~/\.(\w+)$/;
1473:
1474: $intr_scrout.="\n".&Apache::lonhtmlcommon::row_title(&mt('Grade Levels'))
1475: .&mt('Lowest Grade Level:').' '
1476: .&select_level_form($metadatafields{'lowestgradelevel'},'lowestgradelevel')
1477: # .&Apache::lonhtmlcommon::row_closure();
1478: # $intr_scrout.="\n".&Apache::lonhtmlcommon::row_title(&mt('Highest Grade Level'))
1479: .' '.&mt('Highest Grade Level:').' '
1480: .&select_level_form($metadatafields{'highestgradelevel'},'highestgradelevel')
1481: .&Apache::lonhtmlcommon::row_closure();
1482:
1483: $intr_scrout.=&textfield('Standards','standards',$metadatafields{'standards'});
1484:
1485: $intr_scrout.=&hiddenfield('mime',$1);
1486:
1487: my $defaultlanguage=$metadatafields{'language'};
1488: $defaultlanguage =~ s/\s*notset\s*//g;
1489: $defaultlanguage =~ s/^,\s*//g;
1490: $defaultlanguage =~ s/,\s*$//g;
1491:
1492: $intr_scrout.=&selectbox('Language','language',
1493: $defaultlanguage,
1494: \&Apache::loncommon::languagedescription,
1495: (&Apache::loncommon::languageids),
1496: );
1497:
1498: unless ($metadatafields{'creationdate'}) {
1499: $metadatafields{'creationdate'}=time;
1500: }
1501: $intr_scrout.=&hiddenfield('creationdate',
1502: &Apache::lonmysql::unsqltime($metadatafields{'creationdate'}));
1503:
1504: $intr_scrout.=&hiddenfield('lastrevisiondate',time);
1505:
1506: my $pubowner_last;
1507: if ($style eq 'prv') {
1508: $pubowner_last = 1;
1509: }
1510: $intr_scrout.=&textfield('Publisher/Owner','owner',
1511: $metadatafields{'owner'},$pubowner_last);
1512:
1513: # ---------------------------------------------- Retrofix for unused copyright
1514: if ($metadatafields{'copyright'} eq 'free') {
1515: $metadatafields{'copyright'}='default';
1516: $metadatafields{'sourceavail'}='open';
1517: }
1518: if ($metadatafields{'copyright'} eq 'priv') {
1519: $metadatafields{'copyright'}='domain';
1520: }
1521: # ------------------------------------------------ Dial in reasonable defaults
1522: my $defaultoption=$metadatafields{'copyright'};
1523: unless ($defaultoption) { $defaultoption='default'; }
1524: my $defaultsourceoption=$metadatafields{'sourceavail'};
1525: unless ($defaultsourceoption) { $defaultsourceoption='closed'; }
1526: unless ($style eq 'prv') {
1527: # -------------------------------------------------- Correct copyright for rat.
1528: if ($style eq 'rat') {
1529: # -------------------------------------- Retrofix for non-applicable copyright
1530: if ($metadatafields{'copyright'} eq 'public') {
1531: delete $metadatafields{'copyright'};
1532: $defaultoption='default';
1533: }
1534: $intr_scrout.=&selectbox('Copyright/Distribution','copyright',
1535: $defaultoption,
1536: \&Apache::loncommon::copyrightdescription,
1537: (grep !/^(public|priv)$/,(&Apache::loncommon::copyrightids)));
1538: } else {
1539: $intr_scrout.=&selectbox('Copyright/Distribution','copyright',
1540: $defaultoption,
1541: \&Apache::loncommon::copyrightdescription,
1542: (grep !/^priv$/,(&Apache::loncommon::copyrightids)));
1543: }
1544: my $copyright_help =
1545: &Apache::loncommon::help_open_topic('Publishing_Copyright');
1546: my $replace=&mt('Copyright/Distribution:');
1547: $intr_scrout =~ s/$replace/$replace.' '.$copyright_help/ge;
1548:
1549: $intr_scrout.=&text_with_browse_field('Custom Distribution File','customdistributionfile',$metadatafields{'customdistributionfile'},'rights');
1550: $intr_scrout.=&selectbox('Source Distribution','sourceavail',
1551: $defaultsourceoption,
1552: \&Apache::loncommon::source_copyrightdescription,
1553: (&Apache::loncommon::source_copyrightids));
1554: # $intr_scrout.=&text_with_browse_field('Source Custom Distribution File','sourcerights',$metadatafields{'sourcerights'},'rights');
1555: my $uctitle=&mt('Obsolete');
1556: my $obsolete_checked=($metadatafields{'obsolete'})?' checked="checked"':'';
1557: $intr_scrout.="\n".&Apache::lonhtmlcommon::row_title($uctitle)
1558: .'<input type="checkbox" name="obsolete"'.$obsolete_checked.' />'
1559: .&Apache::lonhtmlcommon::row_closure(1);
1560: $intr_scrout.=&text_with_browse_field('Suggested Replacement for Obsolete File',
1561: 'obsoletereplacement',
1562: $metadatafields{'obsoletereplacement'},'',1);
1563: } else {
1564: $intr_scrout.=&hiddenfield('copyright','private');
1565: }
1566: } else {
1567: $intr_scrout.=
1568: &hiddenfield('title',$metadatafields{'title'}).
1569: &hiddenfield('author',$metadatafields{'author'}).
1570: &hiddenfield('subject',$metadatafields{'subject'}).
1571: &hiddenfield('keywords',$metadatafields{'keywords'}).
1572: &hiddenfield('abstract',$metadatafields{'abstract'}).
1573: &hiddenfield('notes',$metadatafields{'notes'}).
1574: &hiddenfield('mime',$metadatafields{'mime'}).
1575: &hiddenfield('creationdate',$metadatafields{'creationdate'}).
1576: &hiddenfield('lastrevisiondate',time).
1577: &hiddenfield('owner',$metadatafields{'owner'}).
1578: &hiddenfield('lowestgradelevel',$metadatafields{'lowestgradelevel'}).
1579: &hiddenfield('standards',$metadatafields{'standards'}).
1580: &hiddenfield('highestgradelevel',$metadatafields{'highestgradelevel'}).
1581: &hiddenfield('language',$metadatafields{'language'}).
1582: &hiddenfield('copyright',$metadatafields{'copyright'}).
1583: &hiddenfield('sourceavail',$metadatafields{'sourceavail'}).
1584: &hiddenfield('customdistributionfile',$metadatafields{'customdistributionfile'}).
1585: &hiddenfield('obsolete',1).
1586: &text_with_browse_field('Suggested Replacement for Obsolete File',
1587: 'obsoletereplacement',
1588: $metadatafields{'obsoletereplacement'},'',1);
1589: }
1590: if (!$batch) {
1591: $scrout.=$intr_scrout
1592: .&Apache::lonhtmlcommon::end_pick_box()
1593: .'<p><input type="submit" value="'
1594: .&mt($env{'form.makeobsolete'}?'Make Obsolete':'Finalize Publication')
1595: .'" /></p>'
1596: .'</form>';
1597: }
1598: return($scrout,0);
1599: }
1600:
1601: #########################################
1602: #########################################
1603:
1604: =pod
1605:
1606: =item B<phasetwo>
1607:
1608: Render second interface showing status of publication steps.
1609: This is publication step two.
1610:
1611: Parameters:
1612:
1613: =over 4
1614:
1615: =item I<$source>
1616:
1617: =item I<$target>
1618:
1619: =item I<$style>
1620:
1621: =item I<$distarget>
1622:
1623: =back
1624:
1625: Returns:
1626:
1627: =over 4
1628:
1629: =item integer
1630:
1631: 0: fail
1632: 1: success
1633:
1634: =back
1635:
1636: =cut
1637:
1638: #'stupid emacs
1639: #########################################
1640: #########################################
1641: sub phasetwo {
1642:
1643: my ($r,$source,$target,$style,$distarget,$batch)=@_;
1644: $source=~s/\/+/\//g;
1645: $target=~s/\/+/\//g;
1646: #
1647: # Unless trying to get rid of something, check name validity
1648: #
1649: unless ($env{'form.obsolete'}) {
1650: if ($target=~/(\_\_\_|\&\&\&|\:\:\:)/) {
1651: $r->print('<span class="LC_error">'.
1652: &mt('Unsupported character combination [_1] in filename, FAIL.',"<tt>'.$1.'</tt>").
1653: '</span>');
1654: return 0;
1655: }
1656: unless ($target=~/\.(\w+)$/) {
1657: $r->print('<span class="LC_error">'.&mt('No valid extension found in filename, FAIL').'</span>');
1658: return 0;
1659: }
1660: if ($target=~/\.(\d+)\.(\w+)$/) {
1661: $r->print('<span class="LC_error">'.&mt('Filename of resource contains internal version number. Cannot publish such resources, FAIL').'</span>');
1662: return 0;
1663: }
1664: }
1665:
1666: #
1667: # End name check
1668: #
1669: $distarget=~s/\/+/\//g;
1670: my $logfile;
1671: unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1672: $r->print(
1673: '<span class="LC_error">'.
1674: &mt('No write permission to user directory, FAIL').'</span>');
1675: return 0;
1676: }
1677:
1678: if ($source =~ /\.rights$/) {
1679: $r->print('<p><span class="LC_warning">'.&mt('Warning: It can take up to 1 hour for rights changes to fully propagate.').'</span></p>');
1680: }
1681:
1682: print $logfile
1683: "\n================= Publish ".localtime()." Phase Two ================\n".$env{'user.name'}.':'.$env{'user.domain'}."\n";
1684:
1685: %metadatafields=();
1686: %metadatakeys=();
1687:
1688: &metaeval(&unescape($env{'form.allmeta'}));
1689:
1690: if ($batch) {
1691: my %commonaccess;
1692: map { $commonaccess{$_} = 1; } &Apache::loncommon::get_env_multiple('form.commonaccess');
1693: if ($commonaccess{'dist'}) {
1694: unless ($style eq 'prv') {
1695: if ($env{'form.commondistselect'} eq 'custom') {
1696: unless ($source =~ /\.rights$/) {
1697: if ($env{'form.commoncustomrights'} =~ m{^/res/.+\.rights$}) {
1698: $env{'form.customdistributionfile'} = $env{'form.commoncustomrights'};
1699: $env{'form.copyright'} = $env{'form.commondistselect'};
1700: }
1701: }
1702: } elsif ($env{'form.commondistselect'} =~ /^default|domain|public$/) {
1703: $env{'form.copyright'} = $env{'form.commondistselect'};
1704: }
1705: }
1706: }
1707: unless ($style eq 'prv') {
1708: if ($commonaccess{'source'}) {
1709: if (($env{'form.commonsourceselect'} eq 'open') || ($env{'form.commonsourceselect'} eq 'closed')) {
1710: $env{'form.sourceavail'} = $env{'form.commonsourceselect'};
1711: }
1712: }
1713: }
1714: }
1715:
1716: $metadatafields{'title'}=$env{'form.title'};
1717: $metadatafields{'author'}=$env{'form.author'};
1718: $metadatafields{'subject'}=$env{'form.subject'};
1719: $metadatafields{'notes'}=$env{'form.notes'};
1720: $metadatafields{'abstract'}=$env{'form.abstract'};
1721: $metadatafields{'mime'}=$env{'form.mime'};
1722: $metadatafields{'language'}=$env{'form.language'};
1723: $metadatafields{'creationdate'}=$env{'form.creationdate'};
1724: $metadatafields{'lastrevisiondate'}=$env{'form.lastrevisiondate'};
1725: $metadatafields{'owner'}=$env{'form.owner'};
1726: $metadatafields{'copyright'}=$env{'form.copyright'};
1727: $metadatafields{'standards'}=$env{'form.standards'};
1728: $metadatafields{'lowestgradelevel'}=$env{'form.lowestgradelevel'};
1729: $metadatafields{'highestgradelevel'}=$env{'form.highestgradelevel'};
1730: $metadatafields{'customdistributionfile'}=
1731: $env{'form.customdistributionfile'};
1732: $metadatafields{'sourceavail'}=$env{'form.sourceavail'};
1733: $metadatafields{'obsolete'}=$env{'form.obsolete'};
1734: $metadatafields{'obsoletereplacement'}=
1735: $env{'form.obsoletereplacement'};
1736: $metadatafields{'dependencies'}=$env{'form.dependencies'};
1737: $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
1738: $env{'user.domain'};
1739: $metadatafields{'authorspace'}=$cuname.':'.$cudom;
1740: $metadatafields{'domain'}=$cudom;
1741:
1742: my $allkeywords=$env{'form.addkey'};
1743: if (exists($env{'form.keywords'})) {
1744: if (ref($env{'form.keywords'})) {
1745: $allkeywords .= ','.join(',',@{$env{'form.keywords'}});
1746: } else {
1747: $allkeywords .= ','.$env{'form.keywords'};
1748: }
1749: }
1750: $allkeywords=~s/[\"\']//g;
1751: $allkeywords=~s/\s*[\;\,]\s*/\,/g;
1752: $allkeywords=~s/\s+/ /g;
1753: $allkeywords=~s/^[ \,]//;
1754: $allkeywords=~s/[ \,]$//;
1755: $metadatafields{'keywords'}=$allkeywords;
1756:
1757: # check if custom distribution file is specified
1758: if ($metadatafields{'copyright'} eq 'custom') {
1759: my $file=$metadatafields{'customdistributionfile'};
1760: unless ($file=~/\.rights$/) {
1761: $r->print(
1762: '<span class="LC_error">'.&mt('No valid custom distribution rights file specified, FAIL').
1763: '</span>');
1764: return 0;
1765: }
1766: }
1767: {
1768: print $logfile "\nWrite metadata file for ".$source;
1769: my $mfh;
1770: unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
1771: $r->print(
1772: '<span class="LC_error">'.&mt('Could not write metadata, FAIL').
1773: '</span>');
1774: return 0;
1775: }
1776: foreach my $field (sort(keys(%metadatafields))) {
1777: unless ($field=~/\./) {
1778: my $unikey=$field;
1779: $unikey=~/^([A-Za-z]+)/;
1780: my $tag=$1;
1781: $tag=~tr/A-Z/a-z/;
1782: print $mfh "\n\<$tag";
1783: foreach my $item (split(/\,/,$metadatakeys{$unikey})) {
1784: my $value=$metadatafields{$unikey.'.'.$item};
1785: $value=~s/\"/\'\'/g;
1786: print $mfh ' '.$item.'="'.$value.'"';
1787: }
1788: print $mfh '>'.
1789: &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
1790: .'</'.$tag.'>';
1791: }
1792: }
1793: $r->print('<p>'.&mt('Wrote Metadata').'</p>');
1794: print $logfile "\nWrote metadata";
1795: }
1796:
1797: # -------------------------------- Synchronize entry with SQL metadata database
1798:
1799: $metadatafields{'url'} = $distarget;
1800: $metadatafields{'version'} = 'current';
1801:
1802: my ($error,$success) = &store_metadata(%metadatafields);
1803: if ($success) {
1804: $r->print('<p>'.&mt('Synchronized SQL metadata database').'</p>');
1805: print $logfile "\nSynchronized SQL metadata database";
1806: } else {
1807: $r->print($error);
1808: print $logfile "\n".$error;
1809: }
1810: # --------------------------------------------- Delete author resource messages
1811: my $delresult=&Apache::lonmsg::del_url_author_res_msg($target);
1812: $r->print('<p>'.&mt('Removing error messages:').' '.$delresult.'</p>');
1813: print $logfile "\nRemoving error messages: $delresult";
1814: # ----------------------------------------------------------- Copy old versions
1815:
1816: if (-e $target) {
1817: my $filename;
1818: my $maxversion=0;
1819: $target=~/(.*)\/([^\/]+)\.(\w+)$/;
1820: my $srcf=$2;
1821: my $srct=$3;
1822: my $srcd=$1;
1823: my $docroot = $Apache::lonnet::perlvar{'lonDocRoot'};
1824: unless ($srcd=~/^\Q$docroot\E\/res/) {
1825: print $logfile "\nPANIC: Target dir is ".$srcd;
1826: $r->print(
1827: "<span class=\"LC_error\">".&mt('Invalid target directory, FAIL')."</span>");
1828: return 0;
1829: }
1830: opendir(DIR,$srcd);
1831: while ($filename=readdir(DIR)) {
1832: if (-l $srcd.'/'.$filename) {
1833: unlink($srcd.'/'.$filename);
1834: unlink($srcd.'/'.$filename.'.meta');
1835: } else {
1836: if ($filename=~/^\Q$srcf\E\.(\d+)\.\Q$srct\E$/) {
1837: $maxversion=($1>$maxversion)?$1:$maxversion;
1838: }
1839: }
1840: }
1841: closedir(DIR);
1842: $maxversion++;
1843: $r->print('<p>'.&mt('Creating old version [_1]',$maxversion).'</p>');
1844: print $logfile "\nCreating old version ".$maxversion."\n";
1845:
1846: my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
1847:
1848: if (copy($target,$copyfile)) {
1849: print $logfile "Copied old target to ".$copyfile."\n";
1850: $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Copied old target file')));
1851: } else {
1852: print $logfile "Unable to write ".$copyfile.':'.$!."\n";
1853: $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Failed to copy old target').", $!",1));
1854: return 0;
1855: }
1856:
1857: # --------------------------------------------------------------- Copy Metadata
1858:
1859: $copyfile=$copyfile.'.meta';
1860:
1861: if (copy($target.'.meta',$copyfile)) {
1862: print $logfile "Copied old target metadata to ".$copyfile."\n";
1863: $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Copied old metadata')));
1864: } else {
1865: print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1866: if (-e $target.'.meta') {
1867: $r->print(&Apache::lonhtmlcommon::confirm_success(
1868: &mt('Failed to write old metadata copy').", $!",1));
1869: return 0;
1870: }
1871: }
1872:
1873:
1874: } else {
1875: $r->print('<p>'.&mt('Initial version').'</p>');
1876: print $logfile "\nInitial version";
1877: }
1878:
1879: # ---------------------------------------------------------------- Write Source
1880: my $copyfile=$target;
1881:
1882: my @parts=split(/\//,$copyfile);
1883: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
1884:
1885: my $count;
1886: for ($count=5;$count<$#parts;$count++) {
1887: $path.="/$parts[$count]";
1888: if ((-e $path)!=1) {
1889: print $logfile "\nCreating directory ".$path;
1890: mkdir($path,0777);
1891: $r->print('<p>'
1892: .&mt('Created directory [_1]'
1893: ,'<span class="LC_filename">'.$parts[$count].'</span>')
1894: .'</p>'
1895: );
1896: }
1897: }
1898:
1899: if (copy($source,$copyfile)) {
1900: print $logfile "\nCopied original source to ".$copyfile."\n";
1901: $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Copied source file')));
1902: } else {
1903: print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
1904: $r->print(&Apache::lonhtmlcommon::confirm_success(
1905: &mt('Failed to copy source').", $!",1));
1906: return 0;
1907: }
1908:
1909: # ---------------------------------------------- Delete local tmp-preview files
1910: unlink($copyfile.'.tmp');
1911: # --------------------------------------------------------------- Copy Metadata
1912:
1913: $copyfile=$copyfile.'.meta';
1914:
1915: if (copy($source.'.meta',$copyfile)) {
1916: print $logfile "\nCopied original metadata to ".$copyfile."\n";
1917: $r->print(&Apache::lonhtmlcommon::confirm_success(&mt('Copied metadata')));
1918: } else {
1919: print $logfile "\nUnable to write metadata ".$copyfile.':'.$!."\n";
1920: $r->print(&Apache::lonhtmlcommon::confirm_success(
1921: &mt('Failed to write metadata copy').", $!",1));
1922: return 0;
1923: }
1924: $r->rflush;
1925:
1926: # ------------------------------------------------------------- Trigger updates
1927: push(@{$modified_urls},[$target,$source]);
1928: unless ($registered_cleanup) {
1929: my $handlers = $r->get_handlers('PerlCleanupHandler');
1930: $r->set_handlers('PerlCleanupHandler' => [\¬ify,@{$handlers}]);
1931: $registered_cleanup=1;
1932: }
1933:
1934: # ---------------------------------------------------------- Clear local caches
1935: my $thisdistarget=$target;
1936: $thisdistarget=~s/^\Q$docroot\E//;
1937: &Apache::lonnet::devalidate_cache_new('resversion',$target);
1938: &Apache::lonnet::devalidate_cache_new('meta',
1939: &Apache::lonnet::declutter($thisdistarget));
1940:
1941: # ------------------------------------------------------------- Everything done
1942: $logfile->close();
1943: $r->print('<p class="LC_success">'.&mt('Done').'</p>');
1944:
1945: # ------------------------------------------------ Provide link to new resource
1946: unless ($batch) {
1947:
1948: my $thissrc=&Apache::loncfile::url($source);
1949: my $thissrcdir=$thissrc;
1950: $thissrcdir=~s/\/[^\/]+$/\//;
1951:
1952: $r->print(
1953: &Apache::lonhtmlcommon::actionbox([
1954: '<a href="'.$thisdistarget.'">'.
1955: &mt('View Published Version').
1956: '</a>',
1957: '<a href="'.$thissrc.'">'.
1958: &mt('Back to Source').
1959: '</a>',
1960: '<a href="'.$thissrcdir.'">'.
1961: &mt('Back to Source Directory').
1962: '</a>'])
1963: );
1964: }
1965: return 1;
1966: }
1967:
1968: # =============================================================== Notifications
1969: sub notify {
1970: # --------------------------------------------------- Send update notifications
1971: foreach my $targetsource (@{$modified_urls}){
1972: my ($target,$source)=@{$targetsource};
1973: my $logfile=Apache::File->new('>>'.$source.'.log');
1974: print $logfile "\nCleanup phase: Notifications\n";
1975: my @subscribed=&get_subscribed_hosts($target);
1976: foreach my $subhost (@subscribed) {
1977: print $logfile "\nNotifying host ".$subhost.':';
1978: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1979: print $logfile $reply;
1980: }
1981: # ---------------------------------------- Send update notifications, meta only
1982: my @subscribedmeta=&get_subscribed_hosts("$target.meta");
1983: foreach my $subhost (@subscribedmeta) {
1984: print $logfile "\nNotifying host for metadata only ".$subhost.':';
1985: my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
1986: $subhost);
1987: print $logfile $reply;
1988: }
1989: # --------------------------------------------------- Notify subscribed courses
1990: my %courses=&coursedependencies($target);
1991: my $now=time;
1992: foreach my $course (keys(%courses)) {
1993: print $logfile "\nNotifying course ".$course.':';
1994: my ($cdom,$cname)=split(/\_/,$course);
1995: my $reply=&Apache::lonnet::cput
1996: ('versionupdate',{$target => $now},$cdom,$cname);
1997: print $logfile $reply;
1998: }
1999: print $logfile "\n============ Done ============\n";
2000: $logfile->close();
2001: }
2002: if ($lock) { &Apache::lonnet::remove_lock($lock); }
2003: return OK;
2004: }
2005:
2006: #########################################
2007:
2008: sub batchpublish {
2009: my ($r,$srcfile,$targetfile)=@_;
2010: #publication pollutes %env with form.* values
2011: my %oldenv=%env;
2012: $srcfile=~s/\/+/\//g;
2013: $targetfile=~s/\/+/\//g;
2014: $srcfile=~s/\/+/\//g;
2015:
2016: my $docroot=$r->dir_config('lonDocRoot');
2017: my $thisdistarget=$targetfile;
2018: $thisdistarget=~s/^\Q$docroot\E//;
2019:
2020:
2021: %metadatafields=();
2022: %metadatakeys=();
2023: $srcfile=~/\.(\w+)$/;
2024: my $thistype=$1;
2025:
2026:
2027: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
2028:
2029: $r->print('<h2>'
2030: .&mt('Publishing [_1]',&Apache::loncfile::display($srcfile))
2031: .'</h2>'
2032: );
2033:
2034: # phase one takes
2035: # my ($source,$target,$style,$batch)=@_;
2036: my ($outstring,$error)=&publish($srcfile,$targetfile,$thisembstyle,1);
2037: $r->print('<p>'.$outstring.'</p>');
2038: # phase two takes
2039: # my ($source,$target,$style,$distarget,batch)=@_;
2040: # $env{'form.allmeta'},$env{'form.title'},$env{'form.author'},...
2041: if (!$error) {
2042: $r->print('<p>');
2043: &phasetwo($r,$srcfile,$targetfile,$thisembstyle,$thisdistarget,1);
2044: $r->print('</p>');
2045: }
2046: %env=%oldenv;
2047: return '';
2048: }
2049:
2050: #########################################
2051:
2052: sub publishdirectory {
2053: my ($r,$fn,$thisdisfn)=@_;
2054: $fn=~s/\/+/\//g;
2055: $thisdisfn=~s/\/+/\//g;
2056: my $thisdisresdir=$thisdisfn;
2057: $thisdisresdir=~s/^\/priv\//\/res\//;
2058: my $resdir = $r->dir_config('lonDocRoot').$thisdisresdir;
2059: $r->print('<form name="pubdirpref" method="post" action="">'
2060: .&Apache::lonhtmlcommon::start_pick_box()
2061: .&Apache::lonhtmlcommon::row_title(&mt('Directory'))
2062: .'<span class="LC_filename">'.$thisdisfn.'</span>'
2063: .&Apache::lonhtmlcommon::row_closure()
2064: .&Apache::lonhtmlcommon::row_title(&mt('Target'))
2065: .'<span class="LC_filename">'.$thisdisresdir.'</span>'
2066: );
2067:
2068: my $dirptr=16384; # Mask indicating a directory in stat.cmode.
2069: unless ($env{'form.phase'} eq 'two') {
2070: # ask user what they want
2071: $r->print(&Apache::lonhtmlcommon::row_closure()
2072: .&Apache::lonhtmlcommon::row_title(&mt('Options'))
2073: );
2074: $r->print(&hiddenfield('phase','two').
2075: &hiddenfield('filename',$env{'form.filename'}).
2076: &checkbox('pubrec','include subdirectories').
2077: &checkbox('forcerepub','force republication of previously published files').
2078: &checkbox('obsolete','make file(s) obsolete').
2079: &checkbox('forceoverride','force directory level metadata over existing').
2080: &common_access('dist',&mt('apply common copyright/distribution'),
2081: ['default','domain','custom']).
2082: &common_access('source',&mt('apply common source availability'),
2083: ['closed','open'])
2084: );
2085: $r->print(&Apache::lonhtmlcommon::row_closure(1)
2086: .&Apache::lonhtmlcommon::end_pick_box()
2087: .'<br /><input type="submit" value="'.&mt('Publish Directory').'" /></form>'
2088: );
2089: $lock=0;
2090: } else {
2091: $r->print(&Apache::lonhtmlcommon::row_closure(1)
2092: .&Apache::lonhtmlcommon::end_pick_box()
2093: );
2094: unless ($lock) { $lock=&Apache::lonnet::set_lock(&mt('Publishing [_1]',$fn)); }
2095: # actually publish things
2096: opendir(DIR,$fn);
2097: my @files=sort(readdir(DIR));
2098: foreach my $filename (@files) {
2099: my ($cdev,$cino,$cmode,$cnlink,
2100: $cuid,$cgid,$crdev,$csize,
2101: $catime,$cmtime,$cctime,
2102: $cblksize,$cblocks)=stat($fn.'/'.$filename);
2103:
2104: my $extension='';
2105: if ($filename=~/\.(\w+)$/) { $extension=$1; }
2106: if ($cmode&$dirptr) {
2107: if (($filename!~/^\./) && ($env{'form.pubrec'})) {
2108: &publishdirectory($r,$fn.'/'.$filename,$thisdisfn.'/'.$filename);
2109: }
2110: } elsif ((&Apache::loncommon::fileembstyle($extension) ne 'hdn') &&
2111: ($filename!~/^[\#\.]/) && ($filename!~/\~$/)) {
2112: # find out publication status and/or exiting metadata
2113: my $publishthis=0;
2114: if (-e $resdir.'/'.$filename) {
2115: my ($rdev,$rino,$rmode,$rnlink,
2116: $ruid,$rgid,$rrdev,$rsize,
2117: $ratime,$rmtime,$rctime,
2118: $rblksize,$rblocks)=stat($resdir.'/'.$filename);
2119: if (($rmtime<$cmtime) || ($env{'form.forcerepub'})) {
2120: # previously published, modified now
2121: $publishthis=1;
2122: }
2123: my $meta_cmtime = (stat($fn.'/'.$filename.'.meta'))[9];
2124: my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
2125: if ( $meta_rmtime<$meta_cmtime ) {
2126: $publishthis=1;
2127: }
2128: } else {
2129: # never published
2130: $publishthis=1;
2131: }
2132:
2133: if ($publishthis) {
2134: &batchpublish($r,$fn.'/'.$filename,$resdir.'/'.$filename);
2135: } else {
2136: $r->print('<br />'.&mt('Skipping').' '.$filename.'<br />');
2137: }
2138: $r->rflush();
2139: }
2140: }
2141: closedir(DIR);
2142: }
2143: }
2144:
2145: #########################################
2146: # publish a default.meta file
2147:
2148: sub defaultmetapublish {
2149: my ($r,$fn,$cuname,$cudom)=@_;
2150: unless (-e $fn) {
2151: return HTTP_NOT_FOUND;
2152: }
2153: my $target=$fn;
2154: $target=~s/^\Q$Apache::lonnet::perlvar{'lonDocRoot'}\E\/priv\//\Q$Apache::lonnet::perlvar{'lonDocRoot'}\E\/res\//;
2155:
2156:
2157: &Apache::loncommon::content_type($r,'text/html');
2158: $r->send_http_header;
2159:
2160: $r->print(&Apache::loncommon::start_page('Metadata Publication'));
2161:
2162: # ---------------------------------------------------------------- Write Source
2163: my $copyfile=$target;
2164:
2165: my @parts=split(/\//,$copyfile);
2166: my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
2167:
2168: my $count;
2169: for ($count=5;$count<$#parts;$count++) {
2170: $path.="/$parts[$count]";
2171: if ((-e $path)!=1) {
2172: mkdir($path,0777);
2173: $r->print('<p>'
2174: .&mt('Created directory [_1]'
2175: ,'<span class="LC_filename">'.$parts[$count].'</span>')
2176: .'</p>'
2177: );
2178: }
2179: }
2180:
2181: if (copy($fn,$copyfile)) {
2182: $r->print('<p>'.&mt('Copied source file').'</p>');
2183: } else {
2184: return "<span class=\"LC_error\">".
2185: &mt('Failed to copy source').", $!, ".&mt('FAIL')."</span>";
2186: }
2187:
2188: # --------------------------------------------------- Send update notifications
2189:
2190: my @subscribed=&get_subscribed_hosts($target);
2191: foreach my $subhost (@subscribed) {
2192: $r->print('<p>'.&mt('Notifying host').' '.$subhost.':');$r->rflush;
2193: my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
2194: $r->print($reply.'</p><br />');$r->rflush;
2195: }
2196: # ------------------------------------------------------------------- Link back
2197: $r->print("<a href='".&Apache::loncfile::display($fn)."'>".&mt('Back to Metadata').'</a>');
2198: $r->print(&Apache::loncommon::end_page());
2199: return OK;
2200: }
2201: #########################################
2202:
2203: =pod
2204:
2205: =item B<handler>
2206:
2207: A basic outline of the handler subroutine follows.
2208:
2209: =over 4
2210:
2211: =item *
2212:
2213: Get query string for limited number of parameters.
2214:
2215: =item *
2216:
2217: Check filename.
2218:
2219: =item *
2220:
2221: File is there and owned, init lookup tables.
2222:
2223: =item *
2224:
2225: Start page output.
2226:
2227: =item *
2228:
2229: Evaluate individual file, and then output information.
2230:
2231: =item *
2232:
2233: Publishing from $thisfn to $thistarget with $thisembstyle.
2234:
2235: =back
2236:
2237: =cut
2238:
2239: #########################################
2240: #########################################
2241: sub handler {
2242: my $r=shift;
2243:
2244: if ($r->header_only) {
2245: &Apache::loncommon::content_type($r,'text/html');
2246: $r->send_http_header;
2247: return OK;
2248: }
2249:
2250: # Get query string for limited number of parameters
2251:
2252: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
2253: ['filename']);
2254:
2255: # -------------------------------------- Flag and buffer for registered cleanup
2256: $registered_cleanup=0;
2257: @{$modified_urls}=();
2258: # -------------------------------------------------------------- Check filename
2259:
2260: my $fn=&unescape($env{'form.filename'});
2261: ($cuname,$cudom)=&Apache::lonnet::constructaccess($fn);
2262: # ----------------------------------------------------- Do we have permissions?
2263: unless (($cuname) && ($cudom)) {
2264: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
2265: ' trying to publish file '.$env{'form.filename'}.
2266: ' - not authorized',
2267: $r->filename);
2268: return HTTP_NOT_ACCEPTABLE;
2269: }
2270: # ----------------------------------------------------------------- Get docroot
2271: $docroot=$r->dir_config('lonDocRoot');
2272:
2273:
2274: # special publication: default.meta file
2275: if ($fn=~/\/default.meta$/) {
2276: return &defaultmetapublish($r,$fn,$cuname,$cudom);
2277: }
2278: $fn=~s/\.meta$//;
2279:
2280: # sanity test on the filename
2281:
2282: unless ($fn) {
2283: $r->log_reason($cuname.' at '.$cudom.
2284: ' trying to publish empty filename', $r->filename);
2285: return HTTP_NOT_FOUND;
2286: }
2287:
2288: unless (-e $docroot.$fn) {
2289: $r->log_reason($cuname.' at '.$cudom.
2290: ' trying to publish non-existing file '.
2291: $env{'form.filename'}.' ('.$fn.')',
2292: $r->filename);
2293: return HTTP_NOT_FOUND;
2294: }
2295:
2296: # -------------------------------- File is there and owned, init lookup tables.
2297:
2298: %addid=();
2299:
2300: {
2301: my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
2302: while (<$fh>=~/(\w+)\s+(\w+)/) {
2303: $addid{$1}=$2;
2304: }
2305: }
2306:
2307: %nokey=();
2308:
2309: {
2310: my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
2311: while (<$fh>) {
2312: my $word=$_;
2313: chomp($word);
2314: $nokey{$word}=1;
2315: }
2316: }
2317:
2318: # ---------------------------------------------------------- Start page output.
2319:
2320: &Apache::loncommon::content_type($r,'text/html');
2321: $r->send_http_header;
2322:
2323: # Breadcrumbs
2324: &Apache::lonhtmlcommon::clear_breadcrumbs();
2325: &Apache::lonhtmlcommon::add_breadcrumb({
2326: 'text' => 'Authoring Space',
2327: 'href' => &Apache::loncommon::authorspace($fn),
2328: });
2329: &Apache::lonhtmlcommon::add_breadcrumb({
2330: 'text' => 'Resource Publication',
2331: 'href' => '',
2332: });
2333:
2334: my $js='<script type="text/javascript">'.
2335: &Apache::loncommon::browser_and_searcher_javascript().
2336: '</script>';
2337: my $startargs = {};
2338: if ($fn=~/\/$/) {
2339: unless ($env{'form.phase'} eq 'two') {
2340: $startargs->{'add_entries'} = { onload => 'javascript:setDefaultAccess();' };
2341: $js .= <<"END";
2342: <script type="text/javascript">
2343: // <![CDATA[
2344: function showHideAccess(caller,div) {
2345: if (document.getElementById(div)) {
2346: if (caller.checked) {
2347: document.getElementById(div).style.display='inline-block';
2348: } else {
2349: document.getElementById(div).style.display='none';
2350: }
2351: }
2352: }
2353:
2354: function showHideCustom(caller,divid) {
2355: if (document.getElementById(divid)) {
2356: if (caller.options[caller.selectedIndex].value == 'custom') {
2357: document.getElementById(divid).style.display="inline-block";
2358: } else {
2359: document.getElementById(divid).style.display="none";
2360: }
2361: }
2362: }
2363: function setDefaultAccess() {
2364: var chkids = Array('LC_commondist','LC_commonsource');
2365: for (var i=0; i<chkids.length; i++) {
2366: if (document.getElementById(chkids[i])) {
2367: document.getElementById(chkids[i]).checked = false;
2368: }
2369: if (document.getElementById(chkids[i]+'select')) {
2370: document.getElementById(chkids[i]+'select').selectedIndex = 0;
2371: }
2372: if (document.getElementById(chkids[i]+'div')) {
2373: document.getElementById(chkids[i]+'div').style.display = 'none';
2374: }
2375: }
2376: }
2377: // ]]>
2378: </script>
2379:
2380: END
2381: }
2382: }
2383: $r->print(&Apache::loncommon::start_page('Resource Publication',$js,$startargs)
2384: .&Apache::lonhtmlcommon::breadcrumbs()
2385: .&Apache::loncommon::head_subbox(
2386: &Apache::loncommon::CSTR_pageheader($docroot.$fn))
2387: );
2388:
2389: my $thisdisfn=&HTML::Entities::encode($fn,'<>&"');
2390: my $thistarget=$fn;
2391: $thistarget=~s/^\/priv\//\/res\//;
2392: my $thisdistarget=&HTML::Entities::encode($thistarget,'<>&"');
2393:
2394: if ($fn=~/\/$/) {
2395: # -------------------------------------------------------- This is a directory
2396: &publishdirectory($r,$docroot.$fn,$thisdisfn);
2397: $r->print(
2398: '<br /><br />'.
2399: &Apache::lonhtmlcommon::actionbox([
2400: '<a href="'.$thisdisfn.'">'.&mt('Return to Directory').'</a>']));
2401: } else {
2402: # ---------------------- Evaluate individual file, and then output information.
2403: $fn=~/\.(\w+)$/;
2404: my $thistype=$1;
2405: my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
2406: if ($thistype eq 'page') { $thisembstyle = 'rat'; }
2407:
2408: $r->print('<h2>'
2409: .&mt('Publishing [_1]'
2410: ,'<span class="LC_filename">'.$thisdisfn.'</span>')
2411: .'</h2>'
2412: );
2413:
2414: $r->print('<h3>'.&mt('Resource Details').'</h3>');
2415:
2416: $r->print(&Apache::lonhtmlcommon::start_pick_box());
2417:
2418: $r->print(&Apache::lonhtmlcommon::row_title(&mt('Type'))
2419: .&Apache::loncommon::filedescription($thistype)
2420: .&Apache::lonhtmlcommon::row_closure()
2421: );
2422:
2423: $r->print(&Apache::lonhtmlcommon::row_title(&mt('Link to Resource'))
2424: .'<tt>'
2425: );
2426: $r->print(<<ENDCAPTION);
2427: <a href='javascript:void(window.open("$thisdisfn","cat","height=300,width=500,scrollbars=1,resizable=1,menubar=0,location=1"))'>
2428: $thisdisfn</a>
2429: ENDCAPTION
2430: $r->print('</tt>'
2431: .&Apache::lonhtmlcommon::row_closure()
2432: );
2433:
2434: $r->print(&Apache::lonhtmlcommon::row_title(&mt('Target'))
2435: .'<tt>'.$thisdistarget.'</tt>'
2436: );
2437: if (($cuname ne $env{'user.name'})||($cudom ne $env{'user.domain'})) {
2438: $r->print(&Apache::lonhtmlcommon::row_closure()
2439: .&Apache::lonhtmlcommon::row_title(&mt('Co-Author'))
2440: .'<span class="LC_warning">'
2441: .&Apache::loncommon::plainname($cuname,$cudom) .' ('.$cuname.':'.$cudom.')'
2442: .'</span>'
2443: );
2444: }
2445:
2446: if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
2447: $r->print(&Apache::lonhtmlcommon::row_closure()
2448: .&Apache::lonhtmlcommon::row_title(&mt('Diffs')));
2449: $r->print(<<ENDDIFF);
2450: <a href='javascript:void(window.open("/adm/diff?filename=$thisdisfn&versiontwo=priv","cat","height=300,width=500,scrollbars=1,resizable=1,menubar=0,location=1"))'>
2451: ENDDIFF
2452: $r->print(&mt('Diffs with Current Version').'</a>');
2453: }
2454:
2455: $r->print(&Apache::lonhtmlcommon::row_closure(1)
2456: .&Apache::lonhtmlcommon::end_pick_box()
2457: );
2458:
2459: # ---------------------- Publishing from $fn to $thistarget with $thisembstyle.
2460:
2461: unless ($env{'form.phase'} eq 'two') {
2462: # ---------------------------------------------------------- Parse for problems
2463: my ($warningcount,$errorcount);
2464: if ($thisembstyle eq 'ssi') {
2465: ($warningcount,$errorcount)=&checkonthis($r,$fn);
2466: }
2467: unless ($errorcount) {
2468: my ($outstring,$error)=
2469: &publish($docroot.$fn,$docroot.$thistarget,$thisembstyle);
2470: $r->print($outstring);
2471: } else {
2472: $r->print('<h3 class="LC_error">'.
2473: &mt('The document contains errors and cannot be published.').
2474: '</h3>');
2475: }
2476: } else {
2477: &phasetwo($r,$docroot.$fn,$docroot.$thistarget,$thisembstyle,$thisdistarget);
2478: }
2479: }
2480: $r->print(&Apache::loncommon::end_page());
2481:
2482: return OK;
2483: }
2484:
2485: 1;
2486: __END__
2487:
2488: =pod
2489:
2490: =back
2491:
2492: =cut
2493:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>