Annotation of loncom/publisher/lonpublisher.pm, revision 1.156

1.1       www         1: # The LearningOnline Network with CAPA
                      2: # Publication Handler
1.54      albertel    3: #
1.156   ! www         4: # $Id: lonpublisher.pm,v 1.155 2003/12/27 16:58:36 www Exp $
1.54      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.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: 
                     69: Authors can only write-access the C</~authorname/> space. They can
                     70: copy resources into the resource area through the publication step,
                     71: and move them back through a recover step. Authors do not have direct
                     72: write-access to their resource space.
                     73: 
                     74: During the publication step, several events will be
                     75: triggered. Metadata is gathered, where a wizard manages default
                     76: entries on a hierarchical per-directory base: The wizard imports the
                     77: metadata (including access privileges and royalty information) from
                     78: the most recent published resource in the current directory, and if
                     79: that is not available, from the next directory above, etc. The Network
                     80: keeps all previous versions of a resource and makes them available by
                     81: an explicit version number, which is inserted between the file name
                     82: and extension, for example C<foo.2.html>, while the most recent
                     83: version does not carry a version number (C<foo.html>). Servers
                     84: subscribing to a changed resource are notified that a new version is
                     85: available.
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: =over 4
                    106: 
                    107: =cut
                    108: 
                    109: ######################################################################
                    110: ######################################################################
                    111: 
                    112: 
1.1       www       113: package Apache::lonpublisher;
                    114: 
1.65      harris41  115: # ------------------------------------------------- modules used by this module
1.1       www       116: use strict;
                    117: use Apache::File;
1.13      www       118: use File::Copy;
1.2       www       119: use Apache::Constants qw(:common :http :methods);
1.76      albertel  120: use HTML::LCParser;
1.4       www       121: use Apache::lonxml;
1.27      www       122: use Apache::loncacc;
1.24      harris41  123: use DBI;
1.65      harris41  124: use Apache::lonnet();
                    125: use Apache::loncommon();
1.89      matthew   126: use Apache::lonmysql;
1.134     www       127: use Apache::lonlocal;
1.145     albertel  128: use Apache::loncfile;
1.156   ! www       129: use Apache::lonmeta;
1.105     www       130: use vars qw(%metadatafields %metadatakeys);
1.2       www       131: 
1.3       www       132: my %addid;
1.5       www       133: my %nokey;
1.10      www       134: 
1.12      www       135: my $docroot;
                    136: 
1.27      www       137: my $cuname;
                    138: my $cudom;
                    139: 
1.90      matthew   140: =pod
                    141: 
1.94      harris41  142: =item B<metaeval>
                    143: 
                    144: Evaluates a string that contains metadata.  This subroutine
                    145: stores values inside I<%metadatafields> and I<%metadatakeys>.
                    146: The hash key is a I<$unikey> corresponding to a unique id
                    147: that is descriptive of the parser location inside the XML tree.
                    148: 
                    149: Parameters:
                    150: 
                    151: =over 4
1.90      matthew   152: 
1.94      harris41  153: =item I<$metastring>
                    154: 
                    155: A string that contains metadata.
                    156: 
                    157: =back
                    158: 
                    159: Returns:
                    160: 
                    161: nothing
1.90      matthew   162: 
                    163: =cut
                    164: 
                    165: #########################################
                    166: #########################################
1.144     www       167: #
                    168: # Modifies global %metadatafields %metadatakeys 
                    169: #
                    170: 
1.7       www       171: sub metaeval {
1.140     albertel  172:     my ($metastring,$prefix)=@_;
1.7       www       173:    
1.139     albertel  174:     my $parser=HTML::LCParser->new(\$metastring);
                    175:     my $token;
                    176:     while ($token=$parser->get_token) {
                    177: 	if ($token->[0] eq 'S') {
                    178: 	    my $entry=$token->[1];
                    179: 	    my $unikey=$entry;
                    180: 	    if (defined($token->[2]->{'package'})) { 
                    181: 		$unikey.='_package_'.$token->[2]->{'package'};
                    182: 	    } 
                    183: 	    if (defined($token->[2]->{'part'})) { 
                    184: 		$unikey.='_'.$token->[2]->{'part'}; 
                    185: 	    }
                    186: 	    if (defined($token->[2]->{'id'})) { 
                    187: 		$unikey.='_'.$token->[2]->{'id'};
                    188: 	    } 
                    189: 	    if (defined($token->[2]->{'name'})) { 
                    190: 		$unikey.='_'.$token->[2]->{'name'}; 
                    191: 	    }
                    192: 	    foreach (@{$token->[3]}) {
                    193: 		$metadatafields{$unikey.'.'.$_}=$token->[2]->{$_};
                    194: 		if ($metadatakeys{$unikey}) {
                    195: 		    $metadatakeys{$unikey}.=','.$_;
                    196: 		} else {
                    197: 		    $metadatakeys{$unikey}=$_;
                    198: 		}
                    199: 	    }
1.140     albertel  200: 	    my $newentry=$parser->get_text('/'.$entry);
                    201: 	    if ($entry eq 'customdistributionfile') {
                    202: 		$newentry=~s/^\s*//;
                    203: 		if ($newentry !~m|^/res|) { $newentry=$prefix.$newentry; }
                    204: 	    }
1.149     www       205: # actually store
                    206: 	    $metadatafields{$unikey}=$newentry;
1.139     albertel  207: 	}
                    208:     }
1.7       www       209: }
                    210: 
1.90      matthew   211: #########################################
                    212: #########################################
                    213: 
                    214: =pod
                    215: 
1.94      harris41  216: =item B<metaread>
1.90      matthew   217: 
                    218: Read a metadata file
                    219: 
1.94      harris41  220: Parameters:
                    221: 
                    222: =over
                    223: 
                    224: =item I<$logfile>
                    225: 
                    226: File output stream to output errors and warnings to.
                    227: 
                    228: =item I<$fn>
                    229: 
                    230: File name (including path).
                    231: 
                    232: =back
                    233: 
                    234: Returns:
                    235: 
                    236: =over 4
                    237: 
                    238: =item Scalar string (if successful)
                    239: 
                    240: XHTML text that indicates successful reading of the metadata.
                    241: 
                    242: =back
                    243: 
1.90      matthew   244: =cut
                    245: 
                    246: #########################################
                    247: #########################################
1.7       www       248: sub metaread {
1.140     albertel  249:     my ($logfile,$fn,$prefix)=@_;
1.7       www       250:     unless (-e $fn) {
1.94      harris41  251: 	print($logfile 'No file '.$fn."\n");
1.146     sakharuk  252:         return '<br /><b>'.&mt('No file').':</b> <tt>'.
1.145     albertel  253: 	    &Apache::loncfile::display($fn).'</tt>';
1.7       www       254:     }
1.94      harris41  255:     print($logfile 'Processing '.$fn."\n");
1.7       www       256:     my $metastring;
                    257:     {
1.140     albertel  258: 	my $metafh=Apache::File->new($fn);
                    259: 	$metastring=join('',<$metafh>);
1.7       www       260:     }
1.140     albertel  261:     &metaeval($metastring,$prefix);
1.147     sakharuk  262:     return '<br /><b>'.&mt('Processed file').':</b> <tt>'.
1.145     albertel  263: 	&Apache::loncfile::display($fn).'</tt>';
1.7       www       264: }
1.12      www       265: 
1.90      matthew   266: #########################################
                    267: #########################################
                    268: 
1.101     www       269: sub coursedependencies {
                    270:     my $url=&Apache::lonnet::declutter(shift);
                    271:     $url=~s/\.meta$//;
                    272:     my ($adomain,$aauthor)=($url=~/^(\w+)\/(\w+)\//);
                    273:     my $regexp=$url;
                    274:     $regexp=~s/(\W)/\\$1/g;
                    275:     $regexp='___'.$regexp.'___course';
                    276:     my %evaldata=&Apache::lonnet::dump('nohist_resevaldata',$adomain,
                    277: 				       $aauthor,$regexp);
                    278:     my %courses=();
                    279:     foreach (keys %evaldata) {
                    280: 	if ($_=~/^([a-zA-Z0-9]+_[a-zA-Z0-9]+)___.+___course$/) {
                    281: 	    $courses{$1}=1;
                    282:         }
                    283:     }
                    284:     return %courses;
                    285: }
                    286: #########################################
                    287: #########################################
                    288: 
                    289: 
1.90      matthew   290: =pod
                    291: 
1.94      harris41  292: =item Form-field-generating subroutines.
                    293: 
                    294: For input parameters, these subroutines take in values
                    295: such as I<$name>, I<$value> and other form field metadata.
                    296: The output (scalar string that is returned) is an XHTML
                    297: string which presents the form field (foreseeably inside
                    298: <form></form> tags).
1.90      matthew   299: 
                    300: =over 4
                    301: 
1.94      harris41  302: =item B<textfield>
1.90      matthew   303: 
1.94      harris41  304: =item B<hiddenfield>
1.90      matthew   305: 
1.94      harris41  306: =item B<selectbox>
1.90      matthew   307: 
                    308: =back
                    309: 
                    310: =cut
                    311: 
                    312: #########################################
                    313: #########################################
1.8       www       314: sub textfield {
1.10      www       315:     my ($title,$name,$value)=@_;
1.141     www       316:     $value=~s/^\s+//gs;
                    317:     $value=~s/\s+$//gs;
                    318:     $value=~s/\s+/ /gs;
1.134     www       319:     $title=&mt($title);
1.123     albertel  320:     my $uctitle=uc($title);
                    321:     return "\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
                    322:            "</b></font></p><br />".
1.94      harris41  323:            '<input type="text" name="'.$name.'" size=80 value="'.$value.'" />';
1.11      www       324: }
                    325: 
                    326: sub hiddenfield {
                    327:     my ($name,$value)=@_;
1.94      harris41  328:     return "\n".'<input type="hidden" name="'.$name.'" value="'.$value.'" />';
1.8       www       329: }
                    330: 
1.9       www       331: sub selectbox {
1.65      harris41  332:     my ($title,$name,$value,$functionref,@idlist)=@_;
1.134     www       333:     $title=&mt($title);
1.65      harris41  334:     my $uctitle=uc($title);
1.123     albertel  335:     $value=(split(/\s*,\s*/,$value))[-1];
1.65      harris41  336:     my $selout="\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
1.123     albertel  337: 	'</b></font></p><br /><select name="'.$name.'">';
1.65      harris41  338:     foreach (@idlist) {
                    339:         $selout.='<option value=\''.$_.'\'';
                    340:         if ($_ eq $value) {
                    341: 	    $selout.=' selected>'.&{$functionref}($_).'</option>';
                    342: 	}
                    343:         else {$selout.='>'.&{$functionref}($_).'</option>';}
                    344:     }
1.10      www       345:     return $selout.'</select>';
1.9       www       346: }
                    347: 
1.90      matthew   348: #########################################
                    349: #########################################
                    350: 
                    351: =pod
                    352: 
1.94      harris41  353: =item B<urlfixup>
1.90      matthew   354: 
                    355: Fix up a url?  First step of publication
1.12      www       356: 
1.90      matthew   357: =cut
                    358: 
                    359: #########################################
                    360: #########################################
1.34      www       361: sub urlfixup {
1.35      www       362:     my ($url,$target)=@_;
1.39      www       363:     unless ($url) { return ''; }
1.68      albertel  364:     #javascript code needs no fixing
                    365:     if ($url =~ /^javascript:/i) { return $url; }
1.69      albertel  366:     if ($url =~ /^mailto:/i) { return $url; }
1.68      albertel  367:     #internal document links need no fixing
                    368:     if ($url =~ /^\#/) { return $url; } 
1.35      www       369:     my ($host)=($url=~/(?:http\:\/\/)*([^\/]+)/);
1.65      harris41  370:     foreach (values %Apache::lonnet::hostname) {
1.35      www       371: 	if ($_ eq $host) {
                    372: 	    $url=~s/^http\:\/\///;
                    373:             $url=~s/^$host//;
                    374:         }
1.65      harris41  375:     }
1.40      www       376:     if ($url=~/^http\:\/\//) { return $url; }
1.35      www       377:     $url=~s/\~$cuname/res\/$cudom\/$cuname/;
1.71      www       378:     return $url;
                    379: }
                    380: 
1.90      matthew   381: #########################################
                    382: #########################################
                    383: 
                    384: =pod
                    385: 
1.94      harris41  386: =item B<absoluteurl>
1.90      matthew   387: 
1.94      harris41  388: Currently undocumented.
1.90      matthew   389: 
                    390: =cut
1.71      www       391: 
1.90      matthew   392: #########################################
                    393: #########################################
1.71      www       394: sub absoluteurl {
                    395:     my ($url,$target)=@_;
                    396:     unless ($url) { return ''; }
1.35      www       397:     if ($target) {
                    398: 	$target=~s/\/[^\/]+$//;
                    399:        $url=&Apache::lonnet::hreflocation($target,$url);
                    400:     }
                    401:     return $url;
1.34      www       402: }
                    403: 
1.90      matthew   404: #########################################
                    405: #########################################
                    406: 
                    407: =pod
                    408: 
1.94      harris41  409: =item B<set_allow>
1.90      matthew   410: 
                    411: Currently undocumented    
                    412: 
                    413: =cut
                    414: 
                    415: #########################################
                    416: #########################################
1.81      albertel  417: sub set_allow {
                    418:     my ($allow,$logfile,$target,$tag,$oldurl)=@_;
                    419:     my $newurl=&urlfixup($oldurl,$target);
                    420:     my $return_url=$oldurl;
                    421:     print $logfile 'GUYURL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
                    422:     if ($newurl ne $oldurl) {
                    423: 	$return_url=$newurl;
                    424: 	print $logfile 'URL: '.$tag.':'.$oldurl.' - '.$newurl."\n";
                    425:     }
                    426:     if (($newurl !~ /^javascript:/i) &&
                    427: 	($newurl !~ /^mailto:/i) &&
                    428: 	($newurl !~ /^http:/i) &&
                    429: 	($newurl !~ /^\#/)) {
                    430: 	$$allow{&absoluteurl($newurl,$target)}=1;
                    431:     }
                    432:     return $return_url
                    433: }
                    434: 
1.90      matthew   435: #########################################
                    436: #########################################
                    437: 
                    438: =pod
                    439: 
1.94      harris41  440: =item B<get_subscribed_hosts>
1.90      matthew   441: 
                    442: Currently undocumented    
                    443: 
                    444: =cut
                    445: 
                    446: #########################################
                    447: #########################################
1.85      albertel  448: sub get_subscribed_hosts {
                    449:     my ($target)=@_;
                    450:     my @subscribed;
                    451:     my $filename;
                    452:     $target=~/(.*)\/([^\/]+)$/;
                    453:     my $srcf=$2;
                    454:     opendir(DIR,$1);
                    455:     while ($filename=readdir(DIR)) {
1.118     albertel  456: 	if ($filename=~/\Q$srcf\E\.(\w+)$/) {
1.85      albertel  457: 	    my $subhost=$1;
1.98      www       458: 	    if (($subhost ne 'meta' && $subhost ne 'subscription') &&
                    459:                 ($subhost ne $Apache::lonnet::perlvar{'lonHostID'})) {
1.85      albertel  460: 		push(@subscribed,$subhost);
                    461: 	    }
                    462: 	}
                    463:     }
                    464:     closedir(DIR);
                    465:     my $sh;
                    466:     if ( $sh=Apache::File->new("$target.subscription") ) {
                    467: 	&Apache::lonnet::logthis("opened $target.subscription");
                    468: 	while (my $subline=<$sh>) {
                    469: 	    &Apache::lonnet::logthis("Trying $subline");
1.98      www       470: 	    if ($subline =~ /(^\w+):/) { 
                    471:                 if ($1 ne $Apache::lonnet::perlvar{'lonHostID'}) { 
                    472:                    push(@subscribed,$1);
                    473: 	        }
                    474:             } else {
1.85      albertel  475: 		&Apache::lonnet::logthis("No Match for $subline");
                    476: 	    }
                    477: 	}
                    478:     } else {
1.94      harris41  479: 	&Apache::lonnet::logthis("Unable to open $target.subscription");
1.85      albertel  480:     }
                    481:     return @subscribed;
                    482: }
                    483: 
1.86      albertel  484: 
1.90      matthew   485: #########################################
                    486: #########################################
                    487: 
                    488: =pod
                    489: 
1.94      harris41  490: =item B<get_max_ids_indices>
1.90      matthew   491: 
                    492: Currently undocumented    
                    493: 
                    494: =cut
                    495: 
                    496: #########################################
                    497: #########################################
1.86      albertel  498: sub get_max_ids_indices {
                    499:     my ($content)=@_;
                    500:     my $maxindex=10;
                    501:     my $maxid=10;
                    502:     my $needsfixup=0;
1.106     albertel  503:     my $duplicateids=0;
                    504: 
                    505:     my %allids;
                    506:     my %duplicatedids;
1.86      albertel  507: 
                    508:     my $parser=HTML::LCParser->new($content);
                    509:     my $token;
                    510:     while ($token=$parser->get_token) {
                    511: 	if ($token->[0] eq 'S') {
                    512: 	    my $counter;
                    513: 	    if ($counter=$addid{$token->[1]}) {
                    514: 		if ($counter eq 'id') {
                    515: 		    if (defined($token->[2]->{'id'})) {
                    516: 			$maxid=($token->[2]->{'id'}>$maxid)?$token->[2]->{'id'}:$maxid;
1.106     albertel  517: 			if (exists($allids{$token->[2]->{'id'}})) {
                    518: 			    $duplicateids=1;
                    519: 			    $duplicatedids{$token->[2]->{'id'}}=1;
                    520: 			} else {
                    521: 			    $allids{$token->[2]->{'id'}}=1;
                    522: 			}
1.86      albertel  523: 		    } else {
                    524: 			$needsfixup=1;
                    525: 		    }
                    526: 		} else {
                    527: 		    if (defined($token->[2]->{'index'})) {
                    528: 			$maxindex=($token->[2]->{'index'}>$maxindex)?$token->[2]->{'index'}:$maxindex;
                    529: 		    } else {
                    530: 			$needsfixup=1;
                    531: 		    }
                    532: 		}
                    533: 	    }
                    534: 	}
                    535:     }
1.106     albertel  536:     return ($needsfixup,$maxid,$maxindex,$duplicateids,
                    537: 	    (keys(%duplicatedids)));
1.86      albertel  538: }
                    539: 
1.90      matthew   540: #########################################
                    541: #########################################
                    542: 
                    543: =pod
                    544: 
1.94      harris41  545: =item B<get_all_text_unbalanced>
1.90      matthew   546: 
                    547: Currently undocumented    
                    548: 
                    549: =cut
                    550: 
                    551: #########################################
                    552: #########################################
1.87      albertel  553: sub get_all_text_unbalanced {
                    554:     #there is a copy of this in lonxml.pm
                    555:     my($tag,$pars)= @_;
                    556:     my $token;
                    557:     my $result='';
                    558:     $tag='<'.$tag.'>';
                    559:     while ($token = $$pars[-1]->get_token) {
                    560: 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
                    561: 	    $result.=$token->[1];
                    562: 	} elsif ($token->[0] eq 'PI') {
                    563: 	    $result.=$token->[2];
                    564: 	} elsif ($token->[0] eq 'S') {
                    565: 	    $result.=$token->[4];
                    566: 	} elsif ($token->[0] eq 'E')  {
                    567: 	    $result.=$token->[2];
                    568: 	}
1.107     albertel  569: 	if ($result =~ /(.*)\Q$tag\E(.*)/s) {
1.88      albertel  570: 	    #&Apache::lonnet::logthis('Got a winner with leftovers ::'.$2);
                    571: 	    #&Apache::lonnet::logthis('Result is :'.$1);
1.87      albertel  572: 	    $result=$1;
                    573: 	    my $redo=$tag.$2;
                    574: 	    push (@$pars,HTML::LCParser->new(\$redo));
                    575: 	    $$pars[-1]->xml_mode('1');
                    576: 	    last;
                    577: 	}
                    578:     }
                    579:     return $result
                    580: }
                    581: 
1.90      matthew   582: #########################################
                    583: #########################################
                    584: 
                    585: =pod
                    586: 
1.94      harris41  587: =item B<fix_ids_and_indices>
1.90      matthew   588: 
                    589: Currently undocumented    
                    590: 
                    591: =cut
                    592: 
                    593: #########################################
                    594: #########################################
1.87      albertel  595: #Arguably this should all be done as a lonnet::ssi instead
1.86      albertel  596: sub fix_ids_and_indices {
                    597:     my ($logfile,$source,$target)=@_;
                    598: 
                    599:     my %allow;
                    600:     my $content;
                    601:     {
                    602: 	my $org=Apache::File->new($source);
                    603: 	$content=join('',<$org>);
                    604:     }
                    605: 
1.106     albertel  606:     my ($needsfixup,$maxid,$maxindex,$duplicateids,@duplicatedids)=
                    607: 	&get_max_ids_indices(\$content);
1.86      albertel  608: 
1.106     albertel  609:     print $logfile ("Got $needsfixup,$maxid,$maxindex,$duplicateids--".
                    610: 			   join(', ',@duplicatedids));
                    611:     if ($duplicateids) {
                    612: 	print $logfile "Duplicate ID(s) exist, ".join(', ',@duplicatedids)."\n";
1.147     sakharuk  613: 	my $outstring='<font color="red">'.&mt('Unable to publish file, it contains duplicated ID(s), ID(s) need to be unique. The duplicated ID(s) are').': '.join(', ',@duplicatedids).'</font>';
1.106     albertel  614: 	return ($outstring,1);
                    615:     }
1.86      albertel  616:     if ($needsfixup) {
                    617: 	print $logfile "Needs ID and/or index fixup\n".
                    618: 	    "Max ID   : $maxid (min 10)\n".
                    619:                 "Max Index: $maxindex (min 10)\n";
                    620:     }
                    621:     my $outstring='';
                    622:     my @parser;
                    623:     $parser[0]=HTML::LCParser->new(\$content);
                    624:     $parser[-1]->xml_mode(1);
                    625:     my $token;
                    626:     while (@parser) {
                    627: 	while ($token=$parser[-1]->get_token) {
                    628: 	    if ($token->[0] eq 'S') {
                    629: 		my $counter;
                    630: 		my $tag=$token->[1];
                    631: 		my $lctag=lc($tag);
                    632: 		if ($lctag eq 'allow') {
                    633: 		    $allow{$token->[2]->{'src'}}=1;
                    634: 		    next;
                    635: 		}
                    636: 		my %parms=%{$token->[2]};
                    637: 		$counter=$addid{$tag};
                    638: 		if (!$counter) { $counter=$addid{$lctag}; }
                    639: 		if ($counter) {
                    640: 		    if ($counter eq 'id') {
                    641: 			unless (defined($parms{'id'})) {
                    642: 			    $maxid++;
                    643: 			    $parms{'id'}=$maxid;
                    644: 			    print $logfile 'ID: '.$tag.':'.$maxid."\n";
                    645: 			}
                    646: 		    } elsif ($counter eq 'index') {
                    647: 			unless (defined($parms{'index'})) {
                    648: 			    $maxindex++;
                    649: 			    $parms{'index'}=$maxindex;
                    650: 			    print $logfile 'Index: '.$tag.':'.$maxindex."\n";
                    651: 			}
                    652: 		    }
                    653: 		}
                    654: 		foreach my $type ('src','href','background','bgimg') {
                    655: 		    foreach my $key (keys(%parms)) {
                    656: 			if ($key =~ /^$type$/i) {
                    657: 			    $parms{$key}=&set_allow(\%allow,$logfile,
                    658: 						    $target,$tag,
                    659: 						    $parms{$key});
                    660: 			}
                    661: 		    }
                    662: 		}
                    663: 		# probably a <randomlabel> image type <label>
1.135     albertel  664: 		# or a <image> tag inside <imageresponse>
                    665: 		if (($lctag eq 'label' && defined($parms{'description'}))
                    666: 		    ||
                    667: 		    ($lctag eq 'image')) {
1.86      albertel  668: 		    my $next_token=$parser[-1]->get_token();
                    669: 		    if ($next_token->[0] eq 'T') {
                    670: 			$next_token->[1]=&set_allow(\%allow,$logfile,
                    671: 						    $target,$tag,
                    672: 						    $next_token->[1]);
                    673: 		    }
                    674: 		    $parser[-1]->unget_token($next_token);
                    675: 		}
                    676: 		if ($lctag eq 'applet') {
                    677: 		    my $codebase='';
1.148     albertel  678: 		    my $havecodebase=0;
                    679: 		    foreach my $key (keys(%parms)) {
                    680: 			if (lc($key) eq 'codebase') { 
                    681: 			    $codebase=$parms{$key};
                    682: 			    $havecodebase=1; 
                    683: 			}
                    684: 		    }
                    685: 		    if ($havecodebase) {
                    686: 			my $oldcodebase=$codebase;
1.86      albertel  687: 			unless ($oldcodebase=~/\/$/) {
                    688: 			    $oldcodebase.='/';
                    689: 			}
                    690: 			$codebase=&urlfixup($oldcodebase,$target);
                    691: 			$codebase=~s/\/$//;    
                    692: 			if ($codebase ne $oldcodebase) {
                    693: 			    $parms{'codebase'}=$codebase;
                    694: 			    print $logfile 'URL codebase: '.$tag.':'.
                    695: 				$oldcodebase.' - '.
                    696: 				    $codebase."\n";
                    697: 			}
                    698: 			$allow{&absoluteurl($codebase,$target).'/*'}=1;
                    699: 		    } else {
1.148     albertel  700: 			foreach my $key (keys(%parms)) {
                    701: 			    if ($key =~ /(archive|code|object)/i) {
                    702: 				my $oldurl=$parms{$key};
1.86      albertel  703: 				my $newurl=&urlfixup($oldurl,$target);
                    704: 				$newurl=~s/\/[^\/]+$/\/\*/;
1.148     albertel  705: 				print $logfile 'Allow: applet '.lc($key).':'.
                    706: 				    $oldurl.' allows '.$newurl."\n";
1.86      albertel  707: 				$allow{&absoluteurl($newurl,$target)}=1;
                    708: 			    }
                    709: 			}
                    710: 		    }
                    711: 		}
                    712: 		my $newparmstring='';
                    713: 		my $endtag='';
                    714: 		foreach (keys %parms) {
                    715: 		    if ($_ eq '/') {
                    716: 			$endtag=' /';
                    717: 		    } else { 
                    718: 			my $quote=($parms{$_}=~/\"/?"'":'"');
                    719: 			$newparmstring.=' '.$_.'='.$quote.$parms{$_}.$quote;
                    720: 		    }
                    721: 		}
                    722: 		if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
                    723: 		$outstring.='<'.$tag.$newparmstring.$endtag.'>';
1.130     albertel  724: 		if ($lctag eq 'm' || $lctag eq 'script' 
1.131     albertel  725:                     || $lctag eq 'display' || $lctag eq 'tex') {
1.130     albertel  726: 		    $outstring.=&get_all_text_unbalanced('/'.$lctag,\@parser);
1.87      albertel  727: 		}
1.86      albertel  728: 	    } elsif ($token->[0] eq 'E') {
                    729: 		if ($token->[2]) {
                    730: 		    unless ($token->[1] eq 'allow') {
                    731: 			$outstring.='</'.$token->[1].'>';
                    732: 		    }
                    733: 		}
                    734: 	    } else {
                    735: 		$outstring.=$token->[1];
                    736: 	    }
                    737: 	}
                    738: 	pop(@parser);
                    739:     }
                    740: 
                    741:     if ($needsfixup) {
                    742: 	print $logfile "End of ID and/or index fixup\n".
                    743: 	    "Max ID   : $maxid (min 10)\n".
                    744: 		"Max Index: $maxindex (min 10)\n";
                    745:     } else {
                    746: 	print $logfile "Does not need ID and/or index fixup\n";
                    747:     }
                    748: 
1.106     albertel  749:     return ($outstring,0,%allow);
1.86      albertel  750: }
                    751: 
1.89      matthew   752: #########################################
                    753: #########################################
                    754: 
                    755: =pod
                    756: 
1.94      harris41  757: =item B<store_metadata>
1.89      matthew   758: 
                    759: Store the metadata in the metadata table in the loncapa database.
                    760: Uses lonmysql to access the database.
                    761: 
                    762: Inputs: \%metadata
                    763: 
                    764: Returns: (error,status).  error is undef on success, status is undef on error.
                    765: 
                    766: =cut
                    767: 
                    768: #########################################
                    769: #########################################
                    770: sub store_metadata {
1.151     www       771:     my %metadata = @_;
1.89      matthew   772:     my $error;
                    773:     # Determine if the table exists
                    774:     my $status = &Apache::lonmysql::check_table('metadata');
                    775:     if (! defined($status)) {
                    776:         $error='<font color="red">WARNING: Cannot connect to '.
                    777:             'database!</font>';
                    778:         &Apache::lonnet::logthis($error);
                    779:         return ($error,undef);
                    780:     }
                    781:     if ($status == 0) {
                    782:         # It would be nice to actually create the table....
                    783:         $error ='<font color="red">WARNING: The metadata table does not '.
                    784:             'exist in the LON-CAPA database.</font>';
                    785:         &Apache::lonnet::logthis($error);
                    786:         return ($error,undef);
                    787:     }
1.152     www       788:     if (($metadata{'obsolete'}) || ($metadata{'copyright'} eq 'priv') ||
                    789: 	($metadata{'copyright'} eq 'custom')) {
                    790: # remove this entry
                    791: 	$status=&Apache::lonmysql::remove_from_table
                    792: 	    ('metadata','url',$metadata{'url'});
                    793:     } else {
                    794: # store new data
1.155     www       795: # adjust some values to metadatadatabase (e.g., "usage" is a reserved word)
1.152     www       796: 	$metadata{'creationdate'}=
                    797: 	    &Apache::lonmysql::sqltime($metadata{'creationdate'}); 
                    798: 	$metadata{'lastrevisiondate'}=
1.155     www       799: 	    &Apache::lonmysql::sqltime($metadata{'lastrevisiondate'});
                    800: 	$metadata{'sequsage'}=$metadata{'usage'};
                    801: 	$metadata{'sequsage_list'}=$metadata{'usage_list'};
                    802: 	my %newmetadata=();
                    803: # see if we have old entries
                    804: 	my @oldmeta=&Apache::lonmysql::get_rows('metadata',
                    805: 						"url LIKE BINARY '".
                    806: 						$metadata{'url'}."'");
                    807: 	if ($#oldmeta==0) {
1.156   ! www       808: # yes, there is one old entry, transfer to newmetadata
        !           809: 	    %newmetadata=&Apache::lonmeta::metadata_col_to_hash(@{$oldmeta[0]});
1.155     www       810: # remove old entry
                    811: 	    $status=&Apache::lonmysql::remove_from_table
                    812: 		('metadata','url',$metadata{'url'});
                    813: 	} elsif ($#oldmeta>0) {
                    814: # more than one entry fit - how did that happen?
                    815: 	    $error='<font color="red">Error occured retrieving old values in '.
                    816: 	      'metadata table in LON-CAPA database: '.$#oldmeta.
                    817: 	      ' matches</font>';
                    818: 	    &Apache::lonnet::logthis($error);
                    819: 	    return ($error,undef);
                    820: 	}
                    821: # store new data on top of it
                    822: 	foreach (keys %metadata) {
                    823: 	    $newmetadata{$_}=$metadata{$_};
                    824: 	}
                    825: 	$status = &Apache::lonmysql::store_row('metadata',\%newmetadata);
1.152     www       826:     }
1.89      matthew   827:     if (! defined($status)) {
                    828:         $error='<font color="red">Error occured storing new values in '.
                    829:             'metadata table in LON-CAPA database</font>';
                    830:         &Apache::lonnet::logthis($error);
                    831:         return ($error,undef);
                    832:     }
                    833:     return (undef,$status);
                    834: }
                    835: 
1.142     www       836: 
                    837: # ============================================== Parse file itself for metadata
1.144     www       838: #
                    839: # parses a file with target meta, sets global %metadatafields %metadatakeys 
1.142     www       840: 
                    841: sub parseformeta {
                    842:     my ($source,$style)=@_;
1.143     www       843:     my $allmeta='';
1.142     www       844:     if (($style eq 'ssi') || ($style eq 'prv')) {
                    845: 	my $dir=$source;
                    846: 	$dir=~s-/[^/]*$--;
                    847: 	my $file=$source;
                    848: 	$file=(split('/',$file))[-1];
                    849:         $source=&Apache::lonnet::hreflocation($dir,$file);
1.143     www       850: 	$allmeta=&Apache::lonnet::ssi_body($source,('grade_target' => 'meta'));
1.142     www       851:         &metaeval($allmeta);
                    852:     }
1.143     www       853:     return $allmeta;
1.142     www       854: }
                    855: 
1.90      matthew   856: #########################################
                    857: #########################################
                    858: 
                    859: =pod
                    860: 
1.94      harris41  861: =item B<publish>
                    862: 
                    863: This is the workhorse function of this module.  This subroutine generates
                    864: backup copies, performs any automatic processing (prior to publication,
                    865: especially for rat and ssi files),
1.90      matthew   866: 
1.113     albertel  867: Returns a 2 element array, the first is the string to be shown to the
                    868: user, the second is an error code, either 1 (an error occured) or 0
                    869: (no error occurred)
                    870: 
1.94      harris41  871: I<Additional documentation needed.>
1.90      matthew   872: 
                    873: =cut
                    874: 
                    875: #########################################
                    876: #########################################
1.2       www       877: sub publish {
1.50      www       878: 
1.97      www       879:     my ($source,$target,$style,$batch)=@_;
1.2       www       880:     my $logfile;
1.4       www       881:     my $scrout='';
1.23      www       882:     my $allmeta='';
                    883:     my $content='';
1.36      www       884:     my %allow=();
1.4       www       885: 
1.2       www       886:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1.147     sakharuk  887: 	return ('<font color="red">'.&mt('No write permission to user directory, FAIL').'</font>',1);
1.2       www       888:     }
                    889:     print $logfile 
1.125     www       890: "\n\n================= Publish ".localtime()." Phase One  ================\n".$ENV{'user.name'}.'@'.$ENV{'user.domain'}."\n";
1.2       www       891: 
1.119     www       892:     if (($style eq 'ssi') || ($style eq 'rat') || ($style eq 'prv')) {
1.3       www       893: # ------------------------------------------------------- This needs processing
1.4       www       894: 
                    895: # ----------------------------------------------------------------- Backup Copy
1.3       www       896: 	my $copyfile=$source.'.save';
1.13      www       897:         if (copy($source,$copyfile)) {
1.3       www       898: 	    print $logfile "Copied original file to ".$copyfile."\n";
                    899:         } else {
1.13      www       900: 	    print $logfile "Unable to write backup ".$copyfile.':'.$!."\n";
1.114     albertel  901: 	    return ("<font color=\"red\">Failed to write backup copy, $!,FAIL</font>",1);
1.3       www       902:         }
1.4       www       903: # ------------------------------------------------------------- IDs and indices
1.86      albertel  904: 	
1.106     albertel  905: 	my ($outstring,$error);
                    906: 	($outstring,$error,%allow)=&fix_ids_and_indices($logfile,$source,
                    907: 							$target);
1.113     albertel  908: 	if ($error) { return ($outstring,$error); }
1.36      www       909: # ------------------------------------------------------------ Construct Allows
1.62      www       910:     
1.146     sakharuk  911: 	$scrout.='<h3>'.&mt('Dependencies').'</h3>';
1.62      www       912:         my $allowstr='';
1.73      albertel  913:         foreach (sort(keys(%allow))) {
1.59      www       914: 	   my $thisdep=$_;
1.73      albertel  915: 	   if ($thisdep !~ /[^\s]/) { next; }
1.62      www       916:            unless ($style eq 'rat') { 
                    917:               $allowstr.="\n".'<allow src="'.$thisdep.'" />';
                    918: 	   }
1.120     albertel  919:            $scrout.='<br />';
1.59      www       920:            unless ($thisdep=~/\*/) {
                    921: 	       $scrout.='<a href="'.$thisdep.'">';
1.44      www       922:            }
1.59      www       923:            $scrout.='<tt>'.$thisdep.'</tt>';
                    924:            unless ($thisdep=~/\*/) {
1.44      www       925: 	       $scrout.='</a>';
1.59      www       926:                if (
                    927:        &Apache::lonnet::getfile($Apache::lonnet::perlvar{'lonDocRoot'}.'/'.
                    928:                                             $thisdep.'.meta') eq '-1') {
1.147     sakharuk  929: 		   $scrout.= ' - <font color="red">'.&mt('Currently not available').
1.94      harris41  930: 		       '</font>';
1.59      www       931:                } else {
                    932:                    my %temphash=(&Apache::lonnet::declutter($target).'___'.
                    933:                              &Apache::lonnet::declutter($thisdep).'___usage'
                    934:                                  => time);
                    935:                    $thisdep=~/^\/res\/(\w+)\/(\w+)\//;
                    936:                    if ((defined($1)) && (defined($2))) {
1.92      albertel  937:                       &Apache::lonnet::put('nohist_resevaldata',\%temphash,
                    938: 					   $1,$2);
1.59      www       939: 		   }
                    940: 	       }
1.44      www       941:            }
1.65      harris41  942:         }
1.83      www       943:         $outstring=~s/\n*(\<\/[^\>]+\>)\s*$/$allowstr\n$1\n/s;
1.62      www       944: 
1.136     www       945: ### FIXME: is this really what we want?
1.137     albertel  946: # I dont' think so, to will corrupt any UTF-8 resources at least, 
                    947: # and any encoding other than ISO-8859-1 will probably break
1.76      albertel  948: 	#Encode any High ASCII characters
1.137     albertel  949: 	#$outstring=&HTML::Entities::encode($outstring,"\200-\377");
1.94      harris41  950: # ------------------------------------------------------------- Write modified.
1.37      www       951: 
1.4       www       952:         {
                    953:           my $org;
                    954:           unless ($org=Apache::File->new('>'.$source)) {
                    955:              print $logfile "No write permit to $source\n";
1.136     www       956:              return ('<font color="red">'.&mt('No write permission to').
                    957: 		     ' '.$source.
                    958: 		     ', '.&mt('FAIL').'</font>',1);
1.4       www       959: 	  }
1.94      harris41  960:           print($org $outstring);
1.4       www       961:         }
                    962: 	  $content=$outstring;
1.34      www       963: 
1.37      www       964:     }
1.94      harris41  965: # -------------------------------------------- Initial step done, now metadata.
1.7       www       966: 
1.94      harris41  967: # --------------------------------------- Storage for metadata keys and fields.
1.144     www       968: # these are globals
                    969: #
1.8       www       970:      %metadatafields=();
                    971:      %metadatakeys=();
                    972:      
                    973:      my %oldparmstores=();
1.44      www       974:      
1.97      www       975:     unless ($batch) {
1.136     www       976:      $scrout.='<h3>'.&mt('Metadata Information').' ' .
1.84      bowersj2  977:        Apache::loncommon::help_open_topic("Metadata_Description")
                    978:        . '</h3>';
1.97      www       979:     }
1.7       www       980: 
                    981: # ------------------------------------------------ First, check out environment
1.8       www       982:      unless (-e $source.'.meta') {
1.7       www       983:         $metadatafields{'author'}=$ENV{'environment.firstname'}.' '.
                    984: 	                          $ENV{'environment.middlename'}.' '.
                    985: 		                  $ENV{'environment.lastname'}.' '.
                    986: 		                  $ENV{'environment.generation'};
1.8       www       987:         $metadatafields{'author'}=~s/\s+/ /g;
                    988:         $metadatafields{'author'}=~s/\s+$//;
1.27      www       989:         $metadatafields{'owner'}=$cuname.'@'.$cudom;
1.7       www       990: 
                    991: # ------------------------------------------------ Check out directory hierachy
                    992: 
                    993:         my $thisdisfn=$source;
1.122     albertel  994:         $thisdisfn=~s/^\/home\/\Q$cuname\E\///;
1.7       www       995: 
                    996:         my @urlparts=split(/\//,$thisdisfn);
                    997:         $#urlparts--;
                    998: 
1.27      www       999:         my $currentpath='/home/'.$cuname.'/';
1.7       www      1000: 
1.140     albertel 1001: 	my $prefix='../'x($#urlparts);
1.65      harris41 1002:         foreach (@urlparts) {
1.7       www      1003: 	    $currentpath.=$_.'/';
1.140     albertel 1004:             $scrout.=&metaread($logfile,$currentpath.'default.meta',$prefix);
                   1005: 	    $prefix=~s|^\.\./||;
1.65      harris41 1006:         }
1.149     www      1007: # ----------------------------------------------------------- Parse file itself
                   1008: # read %metadatafields from file itself
                   1009:  
                   1010: 	$allmeta=&parseformeta($source,$style);
1.7       www      1011: 
                   1012: # ------------------- Clear out parameters and stores (there should not be any)
                   1013: 
1.65      harris41 1014:         foreach (keys %metadatafields) {
1.7       www      1015: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
                   1016: 		delete $metadatafields{$_};
                   1017:             }
1.65      harris41 1018:         }
1.7       www      1019: 
1.8       www      1020:     } else {
1.7       www      1021: # ---------------------- Read previous metafile, remember parameters and stores
                   1022: 
                   1023:         $scrout.=&metaread($logfile,$source.'.meta');
                   1024: 
1.65      harris41 1025:         foreach (keys %metadatafields) {
1.7       www      1026: 	    if (($_=~/^parameter/) || ($_=~/^stores/)) {
                   1027:                 $oldparmstores{$_}=1;
                   1028: 		delete $metadatafields{$_};
                   1029:             }
1.65      harris41 1030:         }
1.142     www      1031: # ------------------------------------------ See if anything new in file itself
                   1032:  
1.144     www      1033: 	$allmeta=&parseformeta($source,$style);
                   1034:    }
1.7       www      1035: 
1.144     www      1036:        
1.7       www      1037: # ---------------- Find and document discrepancies in the parameters and stores
                   1038: 
1.116     albertel 1039:     my $chparms='';
                   1040:     foreach (sort keys %metadatafields) {
                   1041: 	if (($_=~/^parameter/) || ($_=~/^stores/)) {
                   1042: 	    unless ($_=~/\.\w+$/) { 
                   1043: 		unless ($oldparmstores{$_}) {
                   1044: 		    print $logfile 'New: '.$_."\n";
                   1045: 		    $chparms.=$_.' ';
                   1046: 		}
                   1047: 	    }
                   1048: 	}
                   1049:     }
                   1050:     if ($chparms) {
1.136     www      1051: 	$scrout.='<p><b>'.&mt('New parameters or stored values').
                   1052: 	    ':</b> '.$chparms.'</p>';
1.116     albertel 1053:     }
1.7       www      1054: 
1.116     albertel 1055:     $chparms='';
                   1056:     foreach (sort keys %oldparmstores) {
                   1057: 	if (($_=~/^parameter/) || ($_=~/^stores/)) {
                   1058: 	    unless (($metadatafields{$_.'.name'}) ||
                   1059: 		    ($metadatafields{$_.'.package'}) || ($_=~/\.\w+$/)) {
                   1060: 		print $logfile 'Obsolete: '.$_."\n";
                   1061: 		$chparms.=$_.' ';
                   1062: 	    }
                   1063: 	}
                   1064:     }
                   1065:     if ($chparms) {
1.136     www      1066: 	$scrout.='<p><b>'.&mt('Obsolete parameters or stored values').':</b> '.
1.144     www      1067: 	    $chparms.'</p><h1><font color="red">'.&mt('Warning!').
                   1068: 	    '</font></h1><p><font color="red" size="+1">'.
                   1069: 	    &mt('If this resource is in active use, student performance data from the previous version may become inaccessible.').'</font></p><hr />';
1.116     albertel 1070:     }
1.37      www      1071: 
1.8       www      1072: # ------------------------------------------------------- Now have all metadata
1.5       www      1073: 
1.116     albertel 1074:     my %keywords=();
1.97      www      1075:         
1.116     albertel 1076:     if (length($content)<500000) {
                   1077: 	my $textonly=$content;
                   1078: 	$textonly=~s/\<script[^\<]+\<\/script\>//g;
                   1079: 	$textonly=~s/\<m\>[^\<]+\<\/m\>//g;
                   1080: 	$textonly=~s/\<[^\>]*\>//g;
                   1081: 	$textonly=~tr/A-Z/a-z/;
                   1082: 	$textonly=~s/[\$\&][a-z]\w*//g;
                   1083: 	$textonly=~s/[^a-z\s]//g;
                   1084: 	
                   1085: 	foreach ($textonly=~m/(\w+)/g) {
                   1086: 	    unless ($nokey{$_}) {
                   1087: 		$keywords{$_}=1;
                   1088: 	    } 
                   1089: 	}
                   1090:     }
1.97      www      1091: 
                   1092:             
1.116     albertel 1093:     foreach (split(/\W+/,$metadatafields{'keywords'})) {
                   1094: 	$keywords{$_}=1;
                   1095:     }
1.97      www      1096: # --------------------------------------------------- Now we also have keywords
                   1097: # =============================================================================
                   1098: # INTERACTIVE MODE
                   1099: #
1.116     albertel 1100:     unless ($batch) {
1.8       www      1101:         $scrout.=
1.116     albertel 1102: 	    '<form name="pubform" action="/adm/publish" method="post">'.
1.146     sakharuk 1103:             '<p><input type="submit" value="'.&mt('Finalize Publication').'" /></p>'.
1.116     albertel 1104:             &hiddenfield('phase','two').
                   1105:             &hiddenfield('filename',$ENV{'form.filename'}).
                   1106: 	    &hiddenfield('allmeta',&Apache::lonnet::escape($allmeta)).
                   1107:             &hiddenfield('dependencies',join(',',keys %allow)).
                   1108:             &textfield('Title','title',$metadatafields{'title'}).
                   1109:             &textfield('Author(s)','author',$metadatafields{'author'}).
                   1110: 	    &textfield('Subject','subject',$metadatafields{'subject'});
1.5       www      1111: 
                   1112: # --------------------------------------------------- Scan content for keywords
1.7       www      1113: 
1.84      bowersj2 1114:         my $keywords_help = Apache::loncommon::help_open_topic("Publishing_Keywords");
1.146     sakharuk 1115:         my $KEYWORDS=&mt('KEYWORDS');
                   1116: 	my $CheckAll=&mt('check all');
                   1117: 	my $UncheckAll=&mt('uncheck all');
1.77      matthew  1118: 	my $keywordout=<<"END";
                   1119: <script>
1.116     albertel 1120: function checkAll(field) {
1.77      matthew  1121:     for (i = 0; i < field.length; i++)
                   1122:         field[i].checked = true ;
                   1123: }
                   1124: 
1.116     albertel 1125: function uncheckAll(field) {
1.77      matthew  1126:     for (i = 0; i < field.length; i++)
                   1127:         field[i].checked = false ;
                   1128: }
                   1129: </script>
1.146     sakharuk 1130: <p><font color="#800000" face="helvetica"><b>$KEYWORDS:</b></font>
1.123     albertel 1131:  $keywords_help</b>
1.146     sakharuk 1132: <input type="button" value="$CheckAll" onclick="javascript:checkAll(document.pubform.keywords)" /> 
                   1133: <input type="button" value="$UncheckAll" onclick="javascript:uncheckAll(document.pubform.keywords)" /> 
1.120     albertel 1134: </p>
1.77      matthew  1135: <br />
1.117     albertel 1136: END
1.120     albertel 1137: 	$keywordout.='<table border="2"><tr>';
1.116     albertel 1138: 	my $colcount=0;
                   1139: 
                   1140: 	foreach (sort keys %keywords) {
1.120     albertel 1141: 	    $keywordout.='<td><input type="checkbox" name="keywords" value="'.$_.'"';
1.116     albertel 1142: 	    if ($metadatafields{'keywords'}) {
1.122     albertel 1143: 		if ($metadatafields{'keywords'}=~/\Q$_\E/) {
1.120     albertel 1144: 		    $keywordout.=' checked="on"';
1.116     albertel 1145: 		}
                   1146: 	    } elsif (&Apache::loncommon::keyword($_)) {
1.120     albertel 1147: 		$keywordout.=' checked="on"';
1.116     albertel 1148: 	    }
1.120     albertel 1149: 	    $keywordout.=' />'.$_.'</td>';
1.116     albertel 1150: 	    if ($colcount>10) {
                   1151: 		$keywordout.="</tr><tr>\n";
                   1152: 		$colcount=0;
                   1153: 	    }
                   1154: 	    $colcount++;
                   1155: 	}
                   1156: 
1.51      www      1157: 	$keywordout.='</tr></table>';
                   1158: 
1.116     albertel 1159: 	$scrout.=$keywordout;
1.9       www      1160: 
1.116     albertel 1161: 	$scrout.=&textfield('Additional Keywords','addkey','');
1.12      www      1162: 
1.116     albertel 1163: 	$scrout.=&textfield('Notes','notes',$metadatafields{'notes'});
1.9       www      1164: 
1.116     albertel 1165: 	$scrout.=
1.146     sakharuk 1166: 	    "\n<p><font color=\"#800000\" face=\"helvetica\"><b>".&mt('ABSTRACT').":".
1.123     albertel 1167: 	    "</b></font></p><br />".
                   1168: 	    '<textarea cols="80" rows="5" name="abstract">'.
1.120     albertel 1169: 	    $metadatafields{'abstract'}.'</textarea></p>';
1.9       www      1170: 
1.11      www      1171: 	$source=~/\.(\w+)$/;
1.150     www      1172: 
                   1173: 
                   1174: 	$scrout.=
                   1175: 	    "\n<p><font color=\"#800000\" face=\"helvetica\"><b>".
                   1176: 	    uc(&mt('Lowest Grade Level:')).
                   1177:            "</b></font></p><br />".
                   1178: 	   &Apache::loncommon::select_level_form($metadatafields{'lowestgradelevel'},'lowestgradelevel').
                   1179: 	    "\n<p><font color=\"#800000\" face=\"helvetica\"><b>".
                   1180: 	    uc(&mt('Highest Grade Level:')).
                   1181:            "</b></font></p><br />".
                   1182: 	   &Apache::loncommon::select_level_form($metadatafields{'highestgradelevel'},'highestgradelevel').
                   1183:            &textfield('Standards','standards',$metadatafields{'standards'});
                   1184: 
                   1185: 
                   1186: 
1.11      www      1187: 
                   1188: 	$scrout.=&hiddenfield('mime',$1);
                   1189: 
1.123     albertel 1190: 	my $defaultlanguage=$metadatafields{'language'};
                   1191: 	$defaultlanguage =~ s/\s*notset\s*//g;
                   1192: 	$defaultlanguage =~ s/^,\s*//g;
                   1193: 	$defaultlanguage =~ s/,\s*$//g;
                   1194: 
1.116     albertel 1195: 	$scrout.=&selectbox('Language','language',
1.123     albertel 1196: 			    $defaultlanguage,
1.70      harris41 1197: 			    \&Apache::loncommon::languagedescription,
1.65      harris41 1198: 			    (&Apache::loncommon::languageids),
1.116     albertel 1199: 			   );
1.11      www      1200: 
1.116     albertel 1201: 	unless ($metadatafields{'creationdate'}) {
1.11      www      1202: 	    $metadatafields{'creationdate'}=time;
1.116     albertel 1203: 	}
                   1204: 	$scrout.=&hiddenfield('creationdate',
1.153     www      1205: 			      &Apache::lonmysql::unsqltime($metadatafields{'creationdate'}));
1.116     albertel 1206: 
                   1207: 	$scrout.=&hiddenfield('lastrevisiondate',time);
1.11      www      1208: 
                   1209: 
1.10      www      1210: 	$scrout.=&textfield('Publisher/Owner','owner',
1.116     albertel 1211: 			    $metadatafields{'owner'});
1.84      bowersj2 1212: 
1.94      harris41 1213: # -------------------------------------------------- Correct copyright for rat.
1.121     www      1214:         my $defaultoption=$metadatafields{'copyright'};
                   1215:         unless ($defaultoption) { $defaultoption='default'; }
1.116     albertel 1216: 	unless ($style eq 'prv') {
                   1217: 	    if ($style eq 'rat') {
                   1218: 		if ($metadatafields{'copyright'} eq 'public') { 
                   1219: 		    delete $metadatafields{'copyright'};
1.121     www      1220:                     $defaultoption='default';
1.116     albertel 1221: 		}
                   1222: 		$scrout.=&selectbox('Copyright/Distribution','copyright',
1.121     www      1223: 				    $defaultoption,
1.116     albertel 1224: 				    \&Apache::loncommon::copyrightdescription,
                   1225: 				    (grep !/^public$/,(&Apache::loncommon::copyrightids)));
                   1226: 	    } else {
                   1227: 		$scrout.=&selectbox('Copyright/Distribution','copyright',
1.121     www      1228: 				    $defaultoption,
1.116     albertel 1229: 				    \&Apache::loncommon::copyrightdescription,
                   1230: 				    (&Apache::loncommon::copyrightids));
                   1231: 	    }
                   1232:     
                   1233: 	    my $copyright_help =
                   1234: 		Apache::loncommon::help_open_topic('Publishing_Copyright');
                   1235: 	    $scrout =~ s/DISTRIBUTION:/'DISTRIBUTION: ' . $copyright_help/ge;
                   1236: 	    $scrout.=&textfield('Custom Distribution File','customdistributionfile',
                   1237: 				$metadatafields{'customdistributionfile'}).
                   1238: 				    $copyright_help;
1.136     www      1239: 	    my $uctitle=uc(&mt('Obsolete'));
                   1240:             $scrout.=
                   1241: 		"\n<p><font color=\"#800000\" face=\"helvetica\"><b>$uctitle:".
                   1242: 		'</b></font> <input type="checkbox" name="obsolete" ';
                   1243: 	    if ($metadatafields{'obsolete'}) {
                   1244: 		$scrout.=' checked="1" ';
                   1245: 	    }
                   1246: 	    $scrout.='/ ></p>'.
                   1247: 		&textfield('Suggested Replacement for Obsolete File',
                   1248: 				    'obsoletereplacement',
                   1249: 				    $metadatafields{'obsoletereplacement'});
1.116     albertel 1250: 	} else {
                   1251: 	    $scrout.=&hiddenfield('copyright','private');
1.65      harris41 1252: 	}
1.136     www      1253: 	return ($scrout.'<p><input type="submit" value="'.
                   1254: 		&mt('Finalize Publication').'" /></p></form>',0);
1.97      www      1255: # =============================================================================
                   1256: # BATCH MODE
                   1257: #
1.116     albertel 1258:     } else {
1.97      www      1259: # Transfer metadata directly to environment for stage 2
1.116     albertel 1260: 	foreach (keys %metadatafields) {
                   1261: 	    $ENV{'form.'.$_}=$metadatafields{$_};
                   1262: 	}
                   1263: 	$ENV{'form.addkey'}='';
                   1264: 	$ENV{'form.keywords'}='';
                   1265: 	foreach (keys %keywords) {
                   1266: 	    if ($metadatafields{'keywords'}) {
1.122     albertel 1267: 		if ($metadatafields{'keywords'}=~/\Q$_\E/) { 
1.116     albertel 1268: 		    $ENV{'form.keywords'}.=$_.','; 
                   1269: 		}
                   1270: 	    } elsif (&Apache::loncommon::keyword($_)) {
                   1271: 		$ENV{'form.keywords'}.=$_.',';
                   1272: 	    }
                   1273: 	}
                   1274: 	$ENV{'form.keywords'}=~s/\,$//;
                   1275: 	unless ($ENV{'form.creationdate'}) { $ENV{'form.creationdate'}=time; }
                   1276: 	$ENV{'form.lastrevisiondate'}=time;
                   1277: 	if ((($style eq 'rat') && ($ENV{'form.copyright'} eq 'public')) ||
                   1278: 	    (!$ENV{'form.copyright'})) { 
                   1279: 	    $ENV{'form.copyright'}='default';
                   1280: 	}
                   1281: 	$ENV{'form.allmeta'}=&Apache::lonnet::escape($allmeta);
                   1282: 	return ($scrout,0);
1.97      www      1283:     }
1.2       www      1284: }
1.1       www      1285: 
1.90      matthew  1286: #########################################
                   1287: #########################################
                   1288: 
                   1289: =pod 
                   1290: 
1.94      harris41 1291: =item B<phasetwo>
1.90      matthew  1292: 
                   1293: Render second interface showing status of publication steps.
                   1294: This is publication step two.
                   1295: 
1.94      harris41 1296: Parameters:
                   1297: 
                   1298: =over 4
                   1299: 
                   1300: =item I<$source>
                   1301: 
                   1302: =item I<$target>
                   1303: 
                   1304: =item I<$style>
                   1305: 
                   1306: =item I<$distarget>
                   1307: 
                   1308: =back
                   1309: 
                   1310: Returns:
                   1311: 
                   1312: =over 4
                   1313: 
                   1314: =item Scalar string
                   1315: 
                   1316: String contains status (errors and warnings) and information associated with
1.100     matthew  1317: the server's attempts at publication.     
1.94      harris41 1318: 
1.90      matthew  1319: =cut
1.12      www      1320: 
1.100     matthew  1321: #'stupid emacs
1.90      matthew  1322: #########################################
                   1323: #########################################
1.11      www      1324: sub phasetwo {
                   1325: 
1.100     matthew  1326:     my ($r,$source,$target,$style,$distarget,$batch)=@_;
1.102     www      1327:     $source=~s/\/+/\//g;
                   1328:     $target=~s/\/+/\//g;
1.109     www      1329: 
                   1330:     if ($target=~/\_\_\_/) {
1.110     www      1331: 	$r->print(
1.136     www      1332:  '<font color="red">'.&mt('Unsupported character combination').
                   1333: 		  ' "<tt>___</tt>" '.&mt('in filename, FAIL').'</font>');
1.110     www      1334:         return 0;
1.109     www      1335:     }
1.102     www      1336:     $distarget=~s/\/+/\//g;
1.11      www      1337:     my $logfile;
                   1338:     unless ($logfile=Apache::File->new('>>'.$source.'.log')) {
1.110     www      1339: 	$r->print(
1.136     www      1340:         '<font color="red">'.
                   1341: 		&mt('No write permission to user directory, FAIL').'</font>');
1.110     www      1342:         return 0;
1.11      www      1343:     }
                   1344:     print $logfile 
1.125     www      1345:         "\n================= Publish ".localtime()." Phase Two  ================\n".$ENV{'user.name'}.'@'.$ENV{'user.domain'}."\n";
1.100     matthew  1346:     
                   1347:     %metadatafields=();
                   1348:     %metadatakeys=();
                   1349:     
                   1350:     &metaeval(&Apache::lonnet::unescape($ENV{'form.allmeta'}));
                   1351:     
                   1352:     $metadatafields{'title'}=$ENV{'form.title'};
                   1353:     $metadatafields{'author'}=$ENV{'form.author'};
                   1354:     $metadatafields{'subject'}=$ENV{'form.subject'};
                   1355:     $metadatafields{'notes'}=$ENV{'form.notes'};
                   1356:     $metadatafields{'abstract'}=$ENV{'form.abstract'};
                   1357:     $metadatafields{'mime'}=$ENV{'form.mime'};
                   1358:     $metadatafields{'language'}=$ENV{'form.language'};
1.103     www      1359:     $metadatafields{'creationdate'}=$ENV{'form.creationdate'};
                   1360:     $metadatafields{'lastrevisiondate'}=$ENV{'form.lastrevisiondate'};
1.100     matthew  1361:     $metadatafields{'owner'}=$ENV{'form.owner'};
                   1362:     $metadatafields{'copyright'}=$ENV{'form.copyright'};
1.152     www      1363:     $metadatafields{'standards'}=$ENV{'form.standards'};
                   1364:     $metadatafields{'lowestgradelevel'}=$ENV{'form.lowestgradelevel'};
                   1365:     $metadatafields{'highestgradelevel'}=$ENV{'form.highestgradelevel'};
1.115     www      1366:     $metadatafields{'customdistributionfile'}=
                   1367:                                  $ENV{'form.customdistributionfile'};
1.138     www      1368:     $metadatafields{'obsolete'}=$ENV{'form.obsolete'};
                   1369:     $metadatafields{'obsoletereplacement'}=
                   1370: 	                        $ENV{'form.obsoletereplacement'};
1.100     matthew  1371:     $metadatafields{'dependencies'}=$ENV{'form.dependencies'};
1.154     www      1372:     $metadatafields{'modifyinguser'}=$ENV{'user.name'}.'@'.
                   1373: 	                                 $ENV{'user.domain'};
                   1374:     $metadatafields{'authorspace'}=$cuname.'@'.$cudom;
1.100     matthew  1375:     
                   1376:     my $allkeywords=$ENV{'form.addkey'};
                   1377:     if (exists($ENV{'form.keywords'})) {
                   1378:         if (ref($ENV{'form.keywords'})) {
                   1379:             $allkeywords .= ','.join(',',@{$ENV{'form.keywords'}});
                   1380:         } else {
                   1381:             $allkeywords .= ','.$ENV{'form.keywords'};
                   1382:         }
                   1383:     }
                   1384:     $allkeywords=~s/\W+/\,/;
                   1385:     $allkeywords=~s/^\,//;
                   1386:     $metadatafields{'keywords'}=$allkeywords;
                   1387:     
1.149     www      1388: # check if custom distribution file is specified
                   1389:     if ($metadatafields{'copyright'} eq 'custom') {
                   1390: 	my $file=$metadatafields{'customdistributionfile'};
                   1391: 	unless ($file=~/\.rights$/) {
                   1392:             return 
                   1393:                 '<font color="red">'.&mt('No valid custom distribution rights file specified, FAIL').
                   1394: 		'</font>';
                   1395:         }
                   1396:     }
1.100     matthew  1397:     {
                   1398:         print $logfile "\nWrite metadata file for ".$source;
                   1399:         my $mfh;
                   1400:         unless ($mfh=Apache::File->new('>'.$source.'.meta')) {
                   1401:             return 
1.136     www      1402:                 '<font color="red">'.&mt('Could not write metadata, FAIL').
                   1403: 		'</font>';
1.100     matthew  1404:         }
                   1405:         foreach (sort keys %metadatafields) {
                   1406:             unless ($_=~/\./) {
                   1407:                 my $unikey=$_;
                   1408:                 $unikey=~/^([A-Za-z]+)/;
                   1409:                 my $tag=$1;
                   1410:                 $tag=~tr/A-Z/a-z/;
                   1411:                 print $mfh "\n\<$tag";
                   1412:                 foreach (split(/\,/,$metadatakeys{$unikey})) {
                   1413:                     my $value=$metadatafields{$unikey.'.'.$_};
                   1414:                     $value=~s/\"/\'\'/g;
                   1415:                     print $mfh ' '.$_.'="'.$value.'"';
                   1416:                 }
                   1417:                 print $mfh '>'.
                   1418:                     &HTML::Entities::encode($metadatafields{$unikey})
                   1419:                         .'</'.$tag.'>';
                   1420:             }
                   1421:         }
1.136     www      1422:         $r->print('<p>'.&mt('Wrote Metadata').'</p>');
1.100     matthew  1423:         print $logfile "\nWrote metadata";
                   1424:     }
                   1425:     
                   1426: # -------------------------------- Synchronize entry with SQL metadata database
1.12      www      1427: 
1.89      matthew  1428:     $metadatafields{'url'} = $distarget;
                   1429:     $metadatafields{'version'} = 'current';
1.152     www      1430: 
                   1431:     my ($error,$success) = &store_metadata(%metadatafields);
                   1432:     if ($success) {
                   1433: 	$r->print('<p>'.&mt('Synchronized SQL metadata database').'</p>');
                   1434: 	print $logfile "\nSynchronized SQL metadata database";
1.89      matthew  1435:     } else {
1.152     www      1436: 	$r->print($error);
                   1437: 	print $logfile "\n".$error;
1.24      harris41 1438:     }
1.152     www      1439: 
1.12      www      1440: # ----------------------------------------------------------- Copy old versions
                   1441:    
1.100     matthew  1442:     if (-e $target) {
                   1443:         my $filename;
                   1444:         my $maxversion=0;
                   1445:         $target=~/(.*)\/([^\/]+)\.(\w+)$/;
                   1446:         my $srcf=$2;
                   1447:         my $srct=$3;
                   1448:         my $srcd=$1;
                   1449:         unless ($srcd=~/^\/home\/httpd\/html\/res/) {
                   1450:             print $logfile "\nPANIC: Target dir is ".$srcd;
1.114     albertel 1451:             return "<font color=\"red\">Invalid target directory, FAIL</font>";
1.100     matthew  1452:         }
                   1453:         opendir(DIR,$srcd);
                   1454:         while ($filename=readdir(DIR)) {
                   1455:             if (-l $srcd.'/'.$filename) {
                   1456:                 unlink($srcd.'/'.$filename);
                   1457:                 unlink($srcd.'/'.$filename.'.meta');
                   1458:             } else {
1.118     albertel 1459:                 if ($filename=~/\Q$srcf\E\.(\d+)\.\Q$srct\E$/) {
1.100     matthew  1460:                     $maxversion=($1>$maxversion)?$1:$maxversion;
                   1461:                 }
                   1462:             }
                   1463:         }
                   1464:         closedir(DIR);
                   1465:         $maxversion++;
1.120     albertel 1466:         $r->print('<p>Creating old version '.$maxversion.'</p>');
1.125     www      1467:         print $logfile "\nCreating old version ".$maxversion."\n";
1.100     matthew  1468:         
                   1469:         my $copyfile=$srcd.'/'.$srcf.'.'.$maxversion.'.'.$srct;
                   1470:         
1.13      www      1471:         if (copy($target,$copyfile)) {
1.12      www      1472: 	    print $logfile "Copied old target to ".$copyfile."\n";
1.136     www      1473:             $r->print('<p>'.&mt('Copied old target file').'</p>');
1.12      www      1474:         } else {
1.13      www      1475: 	    print $logfile "Unable to write ".$copyfile.':'.$!."\n";
1.136     www      1476:             return "<font color=\"red\">".&mt('Failed to copy old target').
                   1477: 		", $!, ".&mt('FAIL')."</font>";
1.12      www      1478:         }
1.100     matthew  1479:         
1.12      www      1480: # --------------------------------------------------------------- Copy Metadata
                   1481: 
                   1482: 	$copyfile=$copyfile.'.meta';
1.100     matthew  1483:         
1.13      www      1484:         if (copy($target.'.meta',$copyfile)) {
1.14      www      1485: 	    print $logfile "Copied old target metadata to ".$copyfile."\n";
1.136     www      1486:             $r->print('<p>'.&mt('Copied old metadata').'</p>')
1.12      www      1487:         } else {
1.13      www      1488: 	    print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
1.14      www      1489:             if (-e $target.'.meta') {
1.100     matthew  1490:                 return 
1.136     www      1491:                     "<font color=\"red\">".
                   1492: &mt('Failed to write old metadata copy').", $!, ".&mt('FAIL')."</font>";
1.14      www      1493: 	    }
1.12      www      1494:         }
1.100     matthew  1495:         
                   1496:         
                   1497:     } else {
1.138     www      1498:         $r->print('<p>'.&mt('Initial version').'</p>');
1.100     matthew  1499:         print $logfile "\nInitial version";
                   1500:     }
1.12      www      1501: 
                   1502: # ---------------------------------------------------------------- Write Source
1.100     matthew  1503:     my $copyfile=$target;
                   1504:     
                   1505:     my @parts=split(/\//,$copyfile);
                   1506:     my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
                   1507:     
                   1508:     my $count;
                   1509:     for ($count=5;$count<$#parts;$count++) {
                   1510:         $path.="/$parts[$count]";
                   1511:         if ((-e $path)!=1) {
                   1512:             print $logfile "\nCreating directory ".$path;
1.136     www      1513:             $r->print('<p>'.&mt('Created directory').' '.$parts[$count].'</p>');
1.100     matthew  1514:             mkdir($path,0777);
1.12      www      1515:         }
1.100     matthew  1516:     }
                   1517:     
                   1518:     if (copy($source,$copyfile)) {
                   1519:         print $logfile "\nCopied original source to ".$copyfile."\n";
1.136     www      1520:         $r->print('<p>'.&mt('Copied source file').'</p>');
1.100     matthew  1521:     } else {
                   1522:         print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
1.136     www      1523:         return "<font color=\"red\">".
                   1524: 	    &mt('Failed to copy source').", $!, ".&mt('FAIL')."</font>";
1.100     matthew  1525:     }
                   1526:     
1.12      www      1527: # --------------------------------------------------------------- Copy Metadata
                   1528: 
1.100     matthew  1529:     $copyfile=$copyfile.'.meta';
                   1530:     
                   1531:     if (copy($source.'.meta',$copyfile)) {
                   1532:         print $logfile "\nCopied original metadata to ".$copyfile."\n";
1.136     www      1533:         $r->print('<p>'.&mt('Copied metadata').'</p>');
1.100     matthew  1534:     } else {
                   1535:         print $logfile "\nUnable to write metadata ".$copyfile.':'.$!."\n";
                   1536:         return 
1.136     www      1537:             "<font color=\"red\">".&mt('Failed to write metadata copy').", $!, ".&mt('FAIL')."</font>";
1.100     matthew  1538:     }
                   1539:     $r->rflush;
1.12      www      1540: # --------------------------------------------------- Send update notifications
                   1541: 
1.85      albertel 1542:     my @subscribed=&get_subscribed_hosts($target);
                   1543:     foreach my $subhost (@subscribed) {
1.136     www      1544: 	$r->print('<p>'.&mt('Notifying host').' '.$subhost.':');$r->rflush;
1.85      albertel 1545: 	print $logfile "\nNotifying host ".$subhost.':';
                   1546: 	my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
1.120     albertel 1547: 	$r->print($reply.'</p><br />');$r->rflush;
1.85      albertel 1548: 	print $logfile $reply;
1.20      www      1549:     }
1.100     matthew  1550:     
1.20      www      1551: # ---------------------------------------- Send update notifications, meta only
                   1552: 
1.85      albertel 1553:     my @subscribedmeta=&get_subscribed_hosts("$target.meta");
                   1554:     foreach my $subhost (@subscribedmeta) {
1.136     www      1555: 	$r->print('<p>'.
                   1556: &mt('Notifying host for metadata only').' '.$subhost.':');$r->rflush;
1.85      albertel 1557: 	print $logfile "\nNotifying host for metadata only ".$subhost.':';
                   1558: 	my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
                   1559: 					    $subhost);
1.120     albertel 1560: 	$r->print($reply.'</p><br />');$r->rflush;
1.85      albertel 1561: 	print $logfile $reply;
1.12      www      1562:     }
1.100     matthew  1563:     
1.101     www      1564: # --------------------------------------------------- Notify subscribed courses
                   1565:     my %courses=&coursedependencies($target);
                   1566:     my $now=time;
                   1567:     foreach (keys %courses) {
1.136     www      1568: 	$r->print('<p>'.&mt('Notifying course').' '.$_.':');$r->rflush;
1.101     www      1569: 	print $logfile "\nNotifying host ".$_.':';
                   1570:         my ($cdom,$cname)=split(/\_/,$_);
                   1571: 	my $reply=&Apache::lonnet::cput
                   1572:                   ('versionupdate',{$target => $now},$cdom,$cname);
1.120     albertel 1573: 	$r->print($reply.'</p><br />');$r->rflush;
1.101     www      1574: 	print $logfile $reply;
                   1575:     }
1.12      www      1576: # ------------------------------------------------ Provide link to new resource
1.100     matthew  1577:     unless ($batch) {
                   1578:         my $thisdistarget=$target;
1.122     albertel 1579:         $thisdistarget=~s/^\Q$docroot\E//;
1.100     matthew  1580:         
                   1581:         my $thissrc=$source;
                   1582:         $thissrc=~s/^\/home\/(\w+)\/public_html/\/priv\/$1/;
                   1583:         
                   1584:         my $thissrcdir=$thissrc;
                   1585:         $thissrcdir=~s/\/[^\/]+$/\//;
                   1586:         
                   1587:         
                   1588:         $r->print(
1.120     albertel 1589:            '<hr /><a href="'.$thisdistarget.'"><font size="+2">'.
1.138     www      1590:            &mt('View Published Version').'</font></a>'.
                   1591:            '<p><a href="'.$thissrc.'"><font size=+2>'.
                   1592: 		  &mt('Back to Source').'</font></a></p>'.
1.100     matthew  1593:            '<p><a href="'.$thissrcdir.
1.138     www      1594:                    '"><font size="+2">'.
                   1595: 		  &mt('Back to Source Directory').'</font></a></p>');
1.100     matthew  1596:     }
1.149     www      1597:     return '<p><font color="green">'.&mt('Done').'</font></p>';
1.11      www      1598: }
                   1599: 
1.95      www      1600: #########################################
                   1601: 
                   1602: sub batchpublish {
1.97      www      1603:     my ($r,$srcfile,$targetfile)=@_;
1.132     albertel 1604:     #publication pollutes %ENV with form.* values
                   1605:     my %oldENV=%ENV;
1.102     www      1606:     $srcfile=~s/\/+/\//g;
                   1607:     $targetfile=~s/\/+/\//g;
1.95      www      1608:     my $thisdisfn=$srcfile;
                   1609:     $thisdisfn=~s/\/home\/korte\/public_html\///;
                   1610:     $srcfile=~s/\/+/\//g;
1.96      www      1611: 
1.97      www      1612:     my $docroot=$r->dir_config('lonDocRoot');
                   1613:     my $thisdistarget=$targetfile;
1.122     albertel 1614:     $thisdistarget=~s/^\Q$docroot\E//;
1.97      www      1615: 
1.96      www      1616: 
1.139     albertel 1617:     %metadatafields=();
                   1618:     %metadatakeys=();
                   1619:     $srcfile=~/\.(\w+)$/;
                   1620:     my $thistype=$1;
1.97      www      1621: 
                   1622: 
1.139     albertel 1623:     my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
1.96      www      1624:      
1.138     www      1625:     $r->print('<h2>'.&mt('Publishing').' <tt>'.$thisdisfn.'</tt></h2>');
1.97      www      1626: 
                   1627: # phase one takes
                   1628: #  my ($source,$target,$style,$batch)=@_;
1.113     albertel 1629:     my ($outstring,$error)=&publish($srcfile,$targetfile,$thisembstyle,1);
                   1630:     $r->print('<p>'.$outstring.'</p>');
1.96      www      1631: # phase two takes
                   1632: # my ($source,$target,$style,$distarget,batch)=@_;
1.97      www      1633: # $ENV{'form.allmeta'},$ENV{'form.title'},$ENV{'form.author'},...
1.113     albertel 1634:     if (!$error) {
                   1635: 	$r->print('<p>');
                   1636: 	&phasetwo($r,$srcfile,$targetfile,$thisembstyle,$thisdistarget,1);
                   1637: 	$r->print('</p>');
                   1638:     }
1.132     albertel 1639:     %ENV=%oldENV;
1.97      www      1640:     return '';
1.95      www      1641: }
1.1       www      1642: 
1.90      matthew  1643: #########################################
1.95      www      1644: 
                   1645: sub publishdirectory {
                   1646:     my ($r,$fn,$thisdisfn)=@_;
1.102     www      1647:     $fn=~s/\/+/\//g;
                   1648:     $thisdisfn=~s/\/+/\//g;
1.96      www      1649:     my $resdir=
1.139     albertel 1650: 	$Apache::lonnet::perlvar{'lonDocRoot'}.'/res/'.$cudom.'/'.$cuname.'/'.
                   1651: 	$thisdisfn;
1.156   ! www      1652:     $r->print('<h1>'.&mt('Directory').' <tt>'.$thisdisfn.'</tt></h1>'.
        !          1653: 	      &mt('Target').': <tt>'.$resdir.'</tt><br />');
1.139     albertel 1654: 
                   1655:     my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
                   1656: 
                   1657:     opendir(DIR,$fn);
                   1658:     my @files=sort(readdir(DIR));
                   1659:     foreach my $filename (@files) {
                   1660: 	my ($cdev,$cino,$cmode,$cnlink,
1.95      www      1661:             $cuid,$cgid,$crdev,$csize,
                   1662:             $catime,$cmtime,$cctime,
                   1663:             $cblksize,$cblocks)=stat($fn.'/'.$filename);
                   1664: 
1.139     albertel 1665: 	my $extension='';
                   1666: 	if ($filename=~/\.(\w+)$/) { $extension=$1; }
                   1667: 	if ($cmode&$dirptr) {
                   1668: 	    if (($filename!~/^\./) && ($ENV{'form.pubrec'})) {
                   1669: 		&publishdirectory($r,$fn.'/'.$filename,$thisdisfn.'/'.$filename);
                   1670: 	    }
                   1671: 	} elsif ((&Apache::loncommon::fileembstyle($extension) ne 'hdn') &&
                   1672: 		 ($filename!~/^[\#\.]/) && ($filename!~/\~$/)) {
1.96      www      1673: # find out publication status and/or exiting metadata
1.139     albertel 1674: 	    my $publishthis=0;
                   1675: 	    if (-e $resdir.'/'.$filename) {
1.96      www      1676: 	        my ($rdev,$rino,$rmode,$rnlink,
1.139     albertel 1677: 		    $ruid,$rgid,$rrdev,$rsize,
                   1678: 		    $ratime,$rmtime,$rctime,
                   1679: 		    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
1.124     www      1680: 	        if (($rmtime<$cmtime) || ($ENV{'form.forcerepub'})) {
1.96      www      1681: # previously published, modified now
                   1682: 		    $publishthis=1;
                   1683:                 }
1.139     albertel 1684: 	    } else {
1.96      www      1685: # never published
1.139     albertel 1686: 		$publishthis=1;
                   1687: 	    }
                   1688: 	    if ($publishthis) {
1.97      www      1689:                 &batchpublish($r,$fn.'/'.$filename,$resdir.'/'.$filename);
1.139     albertel 1690: 	    } else {
1.156   ! www      1691: 		$r->print('<br />'.&mt('Skipping').' '.$filename.'<br />');
1.139     albertel 1692: 	    }
                   1693: 	    $r->rflush();
                   1694: 	}
                   1695:     }
                   1696:     closedir(DIR);
1.95      www      1697: }
1.90      matthew  1698: #########################################
                   1699: 
                   1700: =pod
                   1701: 
1.94      harris41 1702: =item B<handler>
1.90      matthew  1703: 
                   1704: A basic outline of the handler subroutine follows.
                   1705: 
                   1706: =over 4
                   1707: 
1.94      harris41 1708: =item *
                   1709: 
                   1710: Get query string for limited number of parameters.
                   1711: 
                   1712: =item *
                   1713: 
                   1714: Check filename.
                   1715: 
                   1716: =item *
                   1717: 
                   1718: File is there and owned, init lookup tables.
                   1719: 
                   1720: =item *
1.90      matthew  1721: 
1.94      harris41 1722: Start page output.
1.90      matthew  1723: 
1.94      harris41 1724: =item *
1.90      matthew  1725: 
1.94      harris41 1726: Evaluate individual file, and then output information.
1.90      matthew  1727: 
1.94      harris41 1728: =item *
1.90      matthew  1729: 
1.94      harris41 1730: Publishing from $thisfn to $thistarget with $thisembstyle.
1.90      matthew  1731: 
                   1732: =back
                   1733: 
                   1734: =cut
                   1735: 
                   1736: #########################################
                   1737: #########################################
1.1       www      1738: sub handler {
1.139     albertel 1739:     my $r=shift;
1.2       www      1740: 
1.139     albertel 1741:     if ($r->header_only) {
                   1742: 	&Apache::loncommon::content_type($r,'text/html');
                   1743: 	$r->send_http_header;
                   1744: 	return OK;
                   1745:     }
1.2       www      1746: 
1.43      www      1747: # Get query string for limited number of parameters
                   1748: 
1.80      matthew  1749:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                   1750:                                             ['filename']);
1.43      www      1751: 
1.2       www      1752: # -------------------------------------------------------------- Check filename
                   1753: 
1.139     albertel 1754:     my $fn=&Apache::lonnet::unescape($ENV{'form.filename'});
1.2       www      1755: 
1.27      www      1756:   
1.139     albertel 1757:     unless ($fn) { 
                   1758: 	$r->log_reason($cuname.' at '.$cudom.
                   1759: 		       ' trying to publish empty filename', $r->filename); 
                   1760: 	return HTTP_NOT_FOUND;
                   1761:     } 
                   1762: 
                   1763:     ($cuname,$cudom)=
                   1764: 	&Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
                   1765:     unless (($cuname) && ($cudom)) {
                   1766: 	$r->log_reason($cuname.' at '.$cudom.
                   1767: 		       ' trying to publish file '.$ENV{'form.filename'}.
                   1768: 		       ' ('.$fn.') - not authorized', 
                   1769: 		       $r->filename); 
                   1770: 	return HTTP_NOT_ACCEPTABLE;
                   1771:     }
                   1772: 
                   1773:     unless (&Apache::lonnet::homeserver($cuname,$cudom) 
                   1774: 	    eq $r->dir_config('lonHostID')) {
                   1775: 	$r->log_reason($cuname.' at '.$cudom.
                   1776: 		       ' trying to publish file '.$ENV{'form.filename'}.
                   1777: 		       ' ('.$fn.') - not homeserver ('.
                   1778: 		       &Apache::lonnet::homeserver($cuname,$cudom).')', 
                   1779: 		       $r->filename); 
                   1780: 	return HTTP_NOT_ACCEPTABLE;
                   1781:     }
                   1782: 
                   1783:     $fn=~s/^http\:\/\/[^\/]+//;
                   1784:     $fn=~s/^\/\~(\w+)/\/home\/$1\/public_html/;
                   1785: 
                   1786:     my $targetdir='';
                   1787:     $docroot=$r->dir_config('lonDocRoot'); 
                   1788:     if ($1 ne $cuname) {
                   1789: 	$r->log_reason($cuname.' at '.$cudom.
                   1790: 		       ' trying to publish unowned file '.
                   1791: 		       $ENV{'form.filename'}.' ('.$fn.')', 
                   1792: 		       $r->filename); 
                   1793: 	return HTTP_NOT_ACCEPTABLE;
                   1794:     } else {
                   1795: 	$targetdir=$docroot.'/res/'.$cudom;
                   1796:     }
1.2       www      1797:                                  
                   1798:   
1.139     albertel 1799:     unless (-e $fn) { 
                   1800: 	$r->log_reason($cuname.' at '.$cudom.
                   1801: 		       ' trying to publish non-existing file '.
                   1802: 		       $ENV{'form.filename'}.' ('.$fn.')', 
                   1803: 		       $r->filename); 
                   1804: 	return HTTP_NOT_FOUND;
                   1805:     } 
1.2       www      1806: 
1.139     albertel 1807:     unless ($ENV{'form.phase'} eq 'two') {
1.11      www      1808: 
1.94      harris41 1809: # -------------------------------- File is there and owned, init lookup tables.
1.2       www      1810: 
1.139     albertel 1811: 	%addid=();
                   1812: 
                   1813: 	{
                   1814: 	    my $fh=Apache::File->new($r->dir_config('lonTabDir').'/addid.tab');
                   1815: 	    while (<$fh>=~/(\w+)\s+(\w+)/) {
                   1816: 		$addid{$1}=$2;
                   1817: 	    }
                   1818: 	}
1.3       www      1819: 
1.139     albertel 1820: 	%nokey=();
1.11      www      1821: 
1.139     albertel 1822: 	{
                   1823: 	    my $fh=Apache::File->new($r->dir_config('lonIncludes').'/un_keyword.tab');
                   1824: 	    while (<$fh>) {
                   1825: 		my $word=$_;
                   1826: 		chomp($word);
                   1827: 		$nokey{$word}=1;
                   1828: 	    }
                   1829: 	}
                   1830: 
                   1831:     }
1.11      www      1832: 
1.94      harris41 1833: # ---------------------------------------------------------- Start page output.
1.2       www      1834: 
1.139     albertel 1835:     &Apache::loncommon::content_type($r,'text/html');
                   1836:     $r->send_http_header;
1.1       www      1837: 
1.139     albertel 1838:     $r->print('<html><head><title>LON-CAPA Publishing</title></head>');
                   1839:     $r->print(&Apache::loncommon::bodytag('Resource Publication'));
1.101     www      1840: 
                   1841: 
1.139     albertel 1842:     my $thisfn=$fn;
1.95      www      1843: 
1.139     albertel 1844:     my $thistarget=$thisfn;
1.95      www      1845:       
1.139     albertel 1846:     $thistarget=~s/^\/home/$targetdir/;
                   1847:     $thistarget=~s/\/public\_html//;
1.95      www      1848: 
1.139     albertel 1849:     my $thisdistarget=$thistarget;
                   1850:     $thisdistarget=~s/^\Q$docroot\E//;
1.95      www      1851: 
1.139     albertel 1852:     my $thisdisfn=$thisfn;
                   1853:     $thisdisfn=~s/^\/home\/\Q$cuname\E\/public_html\///;
1.95      www      1854: 
1.139     albertel 1855:     if ($fn=~/\/$/) {
1.95      www      1856: # -------------------------------------------------------- This is a directory
1.139     albertel 1857: 	&publishdirectory($r,$fn,$thisdisfn);
                   1858: 	$r->print('<hr><font size="+2">'.&mt('Done').'</font><br><a href="/priv/'
                   1859: 		  .$cuname.'/'.$thisdisfn
                   1860: 		  .'">'.&mt('Return to Directory').'</a>');
1.128     www      1861: 
1.95      www      1862: 
1.139     albertel 1863:     } else {
1.94      harris41 1864: # ---------------------- Evaluate individual file, and then output information.
1.139     albertel 1865: 	$thisfn=~/\.(\w+)$/;
                   1866: 	my $thistype=$1;
                   1867: 	my $thisembstyle=&Apache::loncommon::fileembstyle($thistype);
                   1868: 	$r->print('<h2>'.&mt('Publishing').' '.
                   1869: 		  &Apache::loncommon::filedescription($thistype).' <tt>');
1.2       www      1870: 
1.139     albertel 1871: 	$r->print(<<ENDCAPTION);
1.129     www      1872: <a href='javascript:void(window.open("/~$cuname/$thisdisfn","cat","height=300,width=500,scrollbars=1,resizable=1,menubar=0,location=1"))'>
                   1873: $thisdisfn</a>
                   1874: ENDCAPTION
1.139     albertel 1875:         $r->print('</tt></h2><b>'.&mt('Target').':</b> <tt>'.
                   1876: 		  $thisdistarget.'</tt><br />');
1.27      www      1877:    
1.139     albertel 1878: 	if (($cuname ne $ENV{'user.name'})||($cudom ne $ENV{'user.domain'})) {
                   1879: 	    $r->print('<h3><font color="red">'.&mt('Co-Author').': '.
                   1880: 		      $cuname.&mt(' at ').$cudom.'</font></h3>');
                   1881: 	}
1.26      www      1882: 
1.139     albertel 1883: 	if (&Apache::loncommon::fileembstyle($thistype) eq 'ssi') {
                   1884: 	    $r->print(<<ENDDIFF);
1.129     www      1885: <br />
1.136     www      1886: <a href='javascript:void(window.open("/adm/diff?filename=/~$cuname/$thisdisfn&versiontwo=priv","cat","height=300,width=500,scrollbars=1,resizable=1,menubar=0,location=1"))'>
1.129     www      1887: ENDDIFF
1.139     albertel 1888:             $r->print(&mt('Diffs with Current Version').'</a><br />');
                   1889: 	}
1.11      www      1890:   
1.94      harris41 1891: # ------------------ Publishing from $thisfn to $thistarget with $thisembstyle.
1.2       www      1892: 
1.139     albertel 1893: 	unless ($ENV{'form.phase'} eq 'two') {
                   1894: 	    my ($outstring,$error)=&publish($thisfn,$thistarget,$thisembstyle);
                   1895: 	    $r->print('<hr />'.$outstring);
                   1896: 	} else {
1.149     www      1897: 	    $r->print('<hr />'.
                   1898: 	    &phasetwo($r,$thisfn,$thistarget,$thisembstyle,$thisdistarget)); 
1.139     albertel 1899: 	}
                   1900:     }
                   1901:     $r->print('</body></html>');
1.15      www      1902: 
1.139     albertel 1903:     return OK;
1.1       www      1904: }
                   1905: 
                   1906: 1;
                   1907: __END__
                   1908: 
1.89      matthew  1909: =pod
1.126     bowersj2 1910: 
                   1911: =back
1.66      harris41 1912: 
                   1913: =back
                   1914: 
1.89      matthew  1915: =cut
1.66      harris41 1916: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>