--- loncom/publisher/loncleanup.pm 2005/05/28 02:18:03 1.2 +++ loncom/publisher/loncleanup.pm 2006/12/20 22:41:08 1.9 @@ -1,7 +1,7 @@ # The LearningOnline Network with CAPA # Handler to cleanup XML files # -# $Id: loncleanup.pm,v 1.2 2005/05/28 02:18:03 www Exp $ +# $Id: loncleanup.pm,v 1.9 2006/12/20 22:41:08 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -38,6 +38,9 @@ use Apache::loncacc; use Apache::loncommon(); use Apache::lonlocal; use Apache::lonnet; +use lib '/home/httpd/lib/perl/'; +use LONCAPA; + sub latextrans { my $symbolfont=shift; @@ -218,8 +221,9 @@ sub htmlclean { } # Generate empty tags, remove wrong end tags unless ($blockemptytags) { - $raw=~s/\<(br|hr|img|meta|allow|basefont)([^\>\/]*?)\>/\<$1$2 \/\>/gis; - $raw=~s/\<\/(br|hr|img|meta|allow|basefont)\>//gis; + $raw=~s/\<(br|hr|img|meta|embed|allow|basefont)([^\>]*?)\>/\<$1$2 \/\>/gis; + $raw=~s/\<\/(br|hr|img|meta|embed|allow|basefont)\>//gis; + $raw=~s/\/ \/\>/\/\>/gs; unless ($full) { $raw=~s/\<[\/]*(body|head|html)\>//gis; } @@ -244,75 +248,147 @@ sub htmlclean { sub phaseone { my ($r,$fn,$uname,$udom)=@_; + $r->print(&mt('Select actions to attempt:'). + '
'. + &mt('Linefeeds, formfeeds, and carriage returns'). + '
'. + &mt('Empty tags'). + '
'. + &mt('Lower casing'). + '
'. + &mt('Symbol font'). + ''. + '

'); } sub phasetwo { my ($r,$fn,$uname,$udom)=@_; + open(IN,'/home/'.$uname.'/public_html/'.$fn); + my $text=''; + while (my $line=) { + $text.=$line; + } + close(IN); + my $uri='/~'.$uname.$fn; + my $result=&Apache::lonnet::ssi_body($uri, + ('grade_target'=>'web', + 'return_only_error_and_warning_counts' => 1)); + my ($errorcount,$warningcount)=split(':',$result); + $r->print(&mt('Original file').': '. + $errorcount.' '.&mt('error(s)').', '. + $warningcount.' '.&mt('warning(s)')); + $text=&htmlclean($text,1, + ($env{'form.linefeed'} ne 'on'), + ($env{'form.empty'} ne 'on'), + ($env{'form.lower'} ne 'on'), + ($env{'form.symbol'} ne 'on')); + my ($main,$ext)=($fn=~/^(.*)\.(\w+)/); + my $newfn=$main.'_Auto_Cleaned_Up.'.$ext; + open(OUT,'>/home/'.$uname.'/public_html'.$newfn); + print OUT $text; + close(OUT); + my $newuri='/~'.$uname.$newfn; + $result=&Apache::lonnet::ssi_body($newuri, + ('grade_target'=>'web', + 'return_only_error_and_warning_counts' => 1)); + ($errorcount,$warningcount)=split(':',$result); + $r->print('
'.&mt('Cleaned up file').': '. + $errorcount.' '.&mt('error(s)').', '. + $warningcount.' '.&mt('warning(s)'). + '
'. + &mt('Open (and edit) cleaned up file in new window').''. + '
'. + &mt('Show diffs in new window').'
'. + ''. + ''. + '' + ); } sub phasethree { my ($r,$fn,$uname,$udom)=@_; + my $old='/home/'.$uname.'/public_html/'.$fn; + my ($main,$ext)=($fn=~/^(.*)\.(\w+)/); + my $newfn=$main.'_Auto_Cleaned_Up.'.$ext; + my $new='/home/'.$uname.'/public_html'.$newfn; + if ($env{'form.accept'}) { + $r->print(&mt('Accepting changes')); + move($new,$old); + } else { + $r->print(&mt('Rejeting changes')); + unlink($new); + } } # ---------------------------------------------------------------- Main Handler sub handler { - my $r=shift; - + my $r=shift; + my $fn=''; # Get query string for limited number of parameters - &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, - ['filename']); + &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'}, + ['filename']); - if ($env{'form.filename'}) { - $fn=$env{'form.filename'}; - $fn=~s/^http\:\/\/[^\/]+//; - } else { - $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}. - ' unspecified filename for cleanup', $r->filename); - return HTTP_NOT_FOUND; - } - - unless ($fn) { - $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}. - ' trying to cleanup non-existing file', $r->filename); - return HTTP_NOT_FOUND; - } + if ($env{'form.filename'}) { + $fn=$env{'form.filename'}; + $fn=~s/^http\:\/\/[^\/]+//; + } else { + $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}. + ' unspecified filename for cleanup', $r->filename); + return HTTP_NOT_FOUND; + } + + unless ($fn) { + $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}. + ' trying to cleanup non-existing file', $r->filename); + return HTTP_NOT_FOUND; + } # ----------------------------------------------------------- Start page output - my $uname; - my $udom; + my $uname; + my $udom; - ($uname,$udom)= - &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain')); - unless (($uname) && ($udom)) { - $r->log_reason($uname.' at '.$udom. - ' trying to cleanup file '.$env{'form.filename'}. - ' ('.$fn.') - not authorized', - $r->filename); - return HTTP_NOT_ACCEPTABLE; - } - - $fn=~s/\/\~(\w+)//; - - &Apache::loncommon::content_type($r,'text/html'); - $r->send_http_header; - - $r->print('LON-CAPA Construction Space'); - - $r->print(&Apache::loncommon::bodytag('Cleanup XML Document')); - - if ($env{'form.phase'} eq 'three') { - &phasethree($r,$fn,$uname,$udom); - } elsif ($env{'form.phase'} eq 'two') { - &phasetwo($r,$fn,$uname,$udom); - } else { - &phaseone($r,$fn,$uname,$udom); - } + ($uname,$udom)= + &Apache::loncacc::constructaccess($fn,$r->dir_config('lonDefDomain')); + unless (($uname) && ($udom)) { + $r->log_reason($uname.' at '.$udom. + ' trying to cleanup file '.$env{'form.filename'}. + ' ('.$fn.') - not authorized', + $r->filename); + return HTTP_NOT_ACCEPTABLE; + } - $r->print(''); - return OK; + $fn=~s{/~($LONCAPA::username_re)}{}; + + &Apache::loncommon::content_type($r,'text/html'); + $r->send_http_header; + + $r->print(&Apache::loncommon::start_page('Cleanup XML Document')); + $r->print('

'.$fn.'

'. + '
'. + ''); + unless ($fn=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) { + $r->print(&mt('Cannot cleanup this filetype')); + } else { + if ($env{'form.phase'} eq 'three') { + &phasethree($r,$fn,$uname,$udom); + } elsif ($env{'form.phase'} eq 'two') { + &phasetwo($r,$fn,$uname,$udom); + } else { + &phaseone($r,$fn,$uname,$udom); + } + } + my $dir=$fn; + $dir=~s/\/[^\/]+$/\//; + $r->print('
'. + '
'.&mt('Back to Source File').''. + '
'.&mt('Back to Source Directory').''. + &Apache::loncommon::end_page()); + return OK; } 1;