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

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

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