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

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

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