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