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

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

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