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

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

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