File:  [LON-CAPA] / loncom / publisher / lonretrieve.pm
Revision 1.20: download - view: text, annotated - select for diffs
Sat Oct 19 06:34:15 2002 UTC (21 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: version_0_6_2, version_0_6, HEAD
- reversed the diff sense, it always seemed backwards to me

    1: # The LearningOnline Network with CAPA
    2: # Handler to retrieve an old version of a file
    3: #
    4: # $Id: lonretrieve.pm,v 1.20 2002/10/19 06:34:15 albertel Exp $
    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: #
   28: #
   29: # (Publication Handler
   30: # 
   31: # (TeX Content Handler
   32: #
   33: # YEAR=2000
   34: # 05/29/00,05/30,10/11 Gerd Kortemeyer)
   35: #
   36: # 11/28,11/29,11/30,12/01,12/02,12/04,12/23 Gerd Kortemeyer
   37: # YEAR=2001
   38: # 03/23 Guy Albertelli
   39: # 03/24,03/29 Gerd Kortemeyer)
   40: #
   41: # 03/31,04/03,05/02,05/09,06/23,08/20 Gerd Kortemeyer
   42: # 12/16 Scott Harrison
   43: #
   44: ###
   45: 
   46: package Apache::lonretrieve;
   47: 
   48: use strict;
   49: use Apache::File;
   50: use File::Copy;
   51: use Apache::Constants qw(:common :http :methods);
   52: use Apache::loncacc;
   53: use Apache::loncommon();
   54: 
   55: # ------------------------------------ Interface for selecting previous version
   56: sub phaseone {
   57:     my ($r,$fn,$uname,$udom)=@_;
   58:     my $docroot=$r->dir_config('lonDocRoot');
   59: 
   60:     my $urldir='/res/'.$udom.'/'.$uname.$fn;
   61:     $urldir=~s/\/[^\/]+$/\//;
   62: 
   63:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
   64:     my $resdir=$resfn;
   65:     $resdir=~s/\/[^\/]+$/\//;
   66: 
   67:     $fn=~/\/([^\/]+)\.(\w+)$/;
   68:     my $main=$1;
   69:     my $suffix=$2;
   70: 
   71:     if (-e $resfn) {  
   72:     $r->print('<form action=/adm/retrieve method=post>'.
   73: 	      '<input type=hidden name=filename value="/~'.$uname.$fn.'">'.
   74:               '<input type=hidden name=phase value=two>'.
   75:               '<table border=2><tr><th>Select</th><th>Version</th>'.
   76:               '<th>Became this version on ...</th>'.
   77:               '<th>Metadata</th></tr>');
   78:     my $filename;
   79:     opendir(DIR,$resdir);
   80:     while ($filename=readdir(DIR)) {
   81:         if ($filename=~/^$main\.(\d+)\.$suffix$/) {
   82: 	   my $version=$1;
   83:            my ($rdev,$rino,$rmode,$rnlink,
   84:                 $ruid,$rgid,$rrdev,$rsize,
   85:                 $ratime,$rmtime,$rctime,
   86:                 $rblksize,$rblocks)=stat($resdir.'/'.$filename);
   87:            $r->print('<tr><td><input type=radio name=version value="'.
   88:                      $version.'"></td><th>'.$version.'</th><td>'.
   89:                      localtime($rmtime).'</td><td>'.
   90:                      '<a href="'.$urldir.$filename.'.meta" target=cat>'.
   91:                      'Metadata Version '.$version.'</a>');
   92:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
   93:                $r->print(
   94:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
   95:                         $uname.$fn.
   96:                         '&versiontwo=priv&versiontwo='.$version.
   97:                         '">Diffs with Version '.$version.'</a>');
   98:            }
   99:            $r->print('</a></td></tr>');
  100:         }
  101:     }
  102:     closedir(DIR);
  103:     my ($rdev,$rino,$rmode,$rnlink,
  104:         $ruid,$rgid,$rrdev,$rsize,
  105:         $ratime,$rmtime,$rctime,
  106:         $rblksize,$rblocks)=stat($resfn);
  107:     $r->print('<tr><td><input type=radio name=version value="new"></td>'.
  108:               '<th>Current</th><td>'.localtime($rmtime).
  109:            '</td><td><a href="'.$urldir.$main.'.'.$suffix.'.meta" target=cat>'.
  110:               'Metadata current version</a>');           
  111:            if (&Apache::loncommon::fileembstyle($suffix) eq 'ssi') {
  112:                $r->print(
  113:                     '&nbsp;&nbsp;<a target=cat href="/adm/diff?filename=/~'.
  114:                         $uname.$fn.
  115:                         '&versiontwo=priv'.
  116:                         '">Diffs with current Version</a>');
  117:            }
  118:            $r->print('</td></tr></table><p>'.
  119:            '<font size=+1 color=red>Retrieval of an old version will '.
  120:            'overwrite the file currently in construction space</font><p>'.
  121:            '<input type=submit value="Retrieve version"></form>');
  122: } else {
  123:     $r->print('<h3>No previous versions published.</h3>');
  124: }
  125: }
  126: 
  127: # ---------------------------------- Interface for presenting specified version
  128: sub phasetwo {
  129:     my ($r,$fn,$uname,$udom)=@_;
  130:     if ($ENV{'form.version'}) {
  131:         my $version=$ENV{'form.version'};
  132: 	if ($version eq 'new') {
  133: 	    $r->print('<h3>Retrieving current (most recent) version</h3>');
  134:         } else {
  135:             $r->print('<h3>Retrieving old version '.$version.'</h3>');
  136:         }
  137:         my $logfile;
  138:         my $ctarget='/home/'.$uname.'/public_html'.$fn;
  139:         my $vfn=$fn;
  140:         if ($version ne 'new') {
  141: 	    $vfn=~s/\.(\w+)$/\.$version\.$1/;
  142:         }
  143:         my $csource=$r->dir_config('lonDocRoot').'/res/'.$udom.'/'.$uname.$vfn;
  144:         unless ($logfile=Apache::File->new('>>'.$ctarget.'.log')) {
  145: 	  $r->print(
  146:          '<font color=red>No write permission to user directory, FAIL</font>');
  147:         }
  148:         print $logfile 
  149: "\n\n================= Retrieve ".localtime()." ================\n".
  150: "Version: $version\nSource: $csource\nTarget: $ctarget\n";
  151:         $r->print('<p>Copying file: ');
  152:         if (copy($csource,$ctarget)) {
  153: 	    $r->print('ok<p>');
  154:             print $logfile "Copied sucessfully.\n\n";
  155:         } else {
  156:             my $error=$!;
  157: 	    $r->print('fail, '.$error.'<p>');
  158:             print $logfile "Copy failed: $error\n\n";
  159:         }
  160:         $r->print('<font size=+2><a href="/priv/'.$uname.$fn.
  161:                   '">Back to '.$fn.'</a></font>'); 
  162:     } else {
  163:        $r->print(
  164:    '<font size=+1 color=red>Please pick a version to retrieve</font><p>');
  165:        &phaseone($r,$fn,$uname,$udom);
  166:     }
  167: }
  168: 
  169: # ---------------------------------------------------------------- Main Handler
  170: sub handler {
  171: 
  172:   my $r=shift;
  173: 
  174:   my $fn;
  175: 
  176: 
  177: # Get query string for limited number of parameters
  178: 
  179:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  180: 					  ['filename']);
  181: 
  182:   if ($ENV{'form.filename'}) {
  183:       $fn=$ENV{'form.filename'};
  184:       $fn=~s/^http\:\/\/[^\/]+//;
  185:   } else {
  186:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  187:          ' unspecified filename for retrieval', $r->filename); 
  188:      return HTTP_NOT_FOUND;
  189:   }
  190: 
  191:   unless ($fn) { 
  192:      $r->log_reason($ENV{'user.name'}.' at '.$ENV{'user.domain'}.
  193:          ' trying to retrieve non-existing file', $r->filename); 
  194:      return HTTP_NOT_FOUND;
  195:   } 
  196: 
  197: # ----------------------------------------------------------- Start page output
  198:   my $uname;
  199:   my $udom;
  200: 
  201:   ($uname,$udom)=
  202:     &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain'));
  203:   unless (($uname) && ($udom)) {
  204:      $r->log_reason($uname.' at '.$udom.
  205:          ' trying to publish file '.$ENV{'form.filename'}.
  206:          ' ('.$fn.') - not authorized', 
  207:          $r->filename); 
  208:      return HTTP_NOT_ACCEPTABLE;
  209:   }
  210: 
  211:   $fn=~s/\/\~(\w+)//;
  212: 
  213:   $r->content_type('text/html');
  214:   $r->send_http_header;
  215: 
  216:   $r->print('<html><head><title>LON-CAPA Construction Space</title></head>');
  217: 
  218:   $r->print(&Apache::loncommon::bodytag('Retrieve Published Resources'));
  219: 
  220:   
  221:   $r->print('<h1>Retrieve previous versions of <tt>'.$fn.'</tt></h1>');
  222:   
  223:   if (($uname ne $ENV{'user.name'}) || ($udom ne $ENV{'user.domain'})) {
  224:           $r->print('<h3><font color=red>Co-Author: '.$uname.' at '.$udom.
  225:                '</font></h3>');
  226:   }
  227: 
  228: 
  229:   if ($ENV{'form.phase'} eq 'two') {
  230:       &phasetwo($r,$fn,$uname,$udom);
  231:   } else {
  232:       &phaseone($r,$fn,$uname,$udom);
  233:   }
  234: 
  235:   $r->print('</body></html>');
  236:   return OK;  
  237: }
  238: 
  239: 1;
  240: __END__
  241: 
  242: =head1 NAME
  243: 
  244: Apache::lonretrieve - retrieves an old version of a file
  245: 
  246: =head1 SYNOPSIS
  247: 
  248: Invoked by /etc/httpd/conf/srm.conf:
  249: 
  250:  <Location /adm/retrieve>
  251:  PerlAccessHandler       Apache::lonacc
  252:  SetHandler perl-script
  253:  PerlHandler Apache::lonretrieve
  254:  ErrorDocument     403 /adm/login
  255:  ErrorDocument     404 /adm/notfound.html
  256:  ErrorDocument     406 /adm/unauthorized.html
  257:  ErrorDocument	  500 /adm/errorhandler
  258:  </Location>
  259: 
  260: =head1 INTRODUCTION
  261: 
  262: This module retrieves an old published version of a file.
  263: 
  264: This is part of the LearningOnline Network with CAPA project
  265: described at http://www.lon-capa.org.
  266: 
  267: =head1 HANDLER SUBROUTINE
  268: 
  269: This routine is called by Apache and mod_perl.
  270: 
  271: =over 4
  272: 
  273: =item *
  274: 
  275: Get query string for limited number of parameters
  276: 
  277: =item *
  278: 
  279: Start page output
  280: 
  281: =item *
  282: 
  283: print phase relevant output
  284: 
  285: =item *
  286: 
  287: (phase one is to select version; phase two retrieves version)
  288: 
  289: =back
  290: 
  291: =head1 OTHER SUBROUTINES
  292: 
  293: =over 4
  294: 
  295: =item *
  296: 
  297: phaseone() : Interface for selecting previous version.
  298: 
  299: =item *
  300: 
  301: phasetwo() : Interface for presenting specified version.
  302: 
  303: =back
  304: 
  305: =cut

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