Annotation of loncom/publisher/loncleanup.pm, revision 1.18
1.1 www 1: # The LearningOnline Network with CAPA
2: # Handler to cleanup XML files
3: #
1.18 ! bisitz 4: # $Id: loncleanup.pm,v 1.17 2012/10/29 17:38:55 raeburn Exp $
1.1 www 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: ###
30:
31: package Apache::loncleanup;
32:
33: use strict;
34: use Apache::File;
35: use File::Copy;
36: use Apache::Constants qw(:common :http :methods);
37: use Apache::loncommon();
1.11 bisitz 38: use Apache::lonhtmlcommon();
1.1 www 39: use Apache::lonlocal;
40: use Apache::lonnet;
1.8 www 41: use lib '/home/httpd/lib/perl/';
42: use LONCAPA;
43:
1.1 www 44:
45: sub latextrans {
46: my $symbolfont=shift;
47: my %latexsymb=(
48: '±' => '\pm',
49: '´' => '\times',
50: '¸' => '\div',
51: 'Ò' => '(R)',
52: 'Ó' => '\copy',
53: 'Ø' => '\neg',
54: 'â' => '(R)',
55: 'ã' => '\copy',
56: '¦' => 'f',
57: 'A' => '\Alpha',
58: 'B' => '\Beta',
59: 'G' => '\Gamma',
60: 'D' => '\Delta',
61: 'E' => '\Epsilon',
62: 'Z' => '\Zeta',
63: 'H' => '\Eta',
64: 'Q' => '\Theta',
65: 'I' => '\Iota',
66: 'K' => '\Kappa',
67: 'L' => '\Lambda',
68: 'M' => '\Mu',
69: 'N' => '\Nu',
70: 'X' => '\Xi',
71: 'O' => '\Omicron',
72: 'P' => '\Pi',
73: 'R' => '\Rho',
74: 'S' => '\Sigma',
75: 'T' => '\Tau',
76: 'U' => 'Y',
77: 'F' => '\Phi',
78: 'C' => '\Chi',
79: 'Y' => '\Psi',
80: 'W' => '\Omega',
81: 'a' => '\alpha',
82: 'b' => '\beta',
83: 'g' => '\gamma',
84: 'd' => '\delta',
85: 'e' => '\epsilon',
86: 'z' => '\zeta',
87: 'h' => '\eta',
88: 'q' => '\theta',
89: 'i' => '\iota',
90: 'k' => '\kappa',
91: 'l' => '\lambda',
92: 'm' => '\mu',
93: 'n' => '\nu',
94: 'x' => '\xi',
95: 'o' => '\omicron',
96: 'p' => '\pi',
97: 'r' => '\rho',
98: 'V' => '\sigmaf',
99: 's' => '\sigma',
100: 't' => '\tau',
101: 'u' => '\upsilon',
102: 'f' => '\phi',
103: 'c' => '\chi',
104: 'y' => '\psi',
105: 'w' => '\omega',
106: 'J' => '\vartheta',
107: 'j' => '\varphi',
108: 'v' => '\varpi',
109: '¡' => '\Upsilon',
110: '¢' => "'",
111: '¤' => '/',
112: '²' => '"',
113: '¼' => '\ldots',
114: 'À' => '\aleph',
115: 'Á' => '\Im',
116: 'Â' => '\Re',
117: 'Ã' => '\wp',
118: 'Ô' => '^{TM}',
119: 'ä' => '^{TM}',
120: 'ð' => 'EUR',
121: '«' => '\leftrightarrow',
122: '¬' => '\leftarrow',
123: '­' => '\uparrow',
124: '®' => '\rightarrow',
125: '¯' => '\downarraw',
126: '¿' => '\hookleftarrow',
127: 'Û' => '\Leftrightarrow',
128: 'Ü' => '\Leftarrow',
129: 'Ý' => '\Uparrow',
130: 'Þ' => '\Rightarrow',
131: 'ß' => '\Downarrow',
132: '"' => '\forall',
133: '$' => '\exists',
134: ''' => '\ni',
135: '*' => '\ast',
136: '-' => '-',
137: '@' => '\cong',
138: '\' => '\therefore',
139: '^' => '\perp',
140: '~' => '\sim',
141: '£' => '\leq',
142: '¥' => '\infty',
143: '³' => '\geq',
144: 'µ' => '\propto',
145: '¶' => '\partial',
146: '·' => '\cdot',
147: '¹' => '\not=',
148: 'º' => '\equiv',
149: '»' => '\approx',
150: 'Ä' => '\otimes',
151: 'Å' => '\oplus',
152: 'Æ' => '\emptyset',
153: 'Ç' => '\cap',
154: 'È' => '\cup',
155: 'É' => '\supset',
156: 'Ê' => '\supseteq',
157: 'Ë' => '\not\subset',
158: 'Ì' => '\subset',
159: 'Í' => '\subseteq',
160: 'Î' => '\in',
161: 'Ï' => '\not\in',
162: 'Ð' => '\angle',
163: 'Ñ' => '\nabla',
164: 'Õ' => '\prod',
165: 'Ö' => '\surd',
166: '×' => '\cdot',
167: 'Ù' => '\wedge',
168: 'Ú' => '\wee',
169: 'å' => '\sum',
170: 'ò' => '\int',
171: 'á' => '\langle',
172: 'ñ' => '\rangle',
173: 'à' => '\diamondsuit',
174: '§' => '\clubsuit',
175: '¨' => '\diamondsuit',
176: '©' => '\heartsuit',
177: 'ª' => '\spadesuit'
178: );
179: my $output='';
180: my $char='';
181: my $entitymode=0;
182: for (my $i=0; $i<length($symbolfont); $i++) {
183: my $newchar=substr($symbolfont,$i,1);
184: $char.=$newchar;
185: if ($newchar eq '&') { $entitymode=1; }
186: if (($entitymode) && ($newchar ne ';')) { next; }
187: my $latex=$latexsymb{$char};
188: if ($latex) {
189: $output.=$latex;
190: } else {
191: $output.=$char;
192: }
193: $char='';
194: $entitymode=0;
195: }
196: return $output;
197: }
198:
199: sub insidetrans {
200: my @args=@_;
201: return '<font'.$args[0].$args[1].'><m>$'.&latextrans($args[2]).'$</m>';
202: }
203:
204: sub symbolfontreplace {
205: my $text=shift;
206: my @fragments=split(/\<\/font\>/si,$text);
207: for (my $i=0; $i<=$#fragments;$i++) {
208: $fragments[$i]=~s/\<font([^\>]*)\s+face=[\"\']*symbol[\"\']*([^\>]*)\>(.*)$/&insidetrans($1,$2,$3)/gsie;
209: }
210: return join('</font>',@fragments);
211: }
212:
213: sub htmlclean {
1.2 www 214: my ($raw,$full,$blocklinefeed,$blockemptytags,$blocklowercasing,$blockdesymboling)=@_;
1.1 www 215: # Take care of CRLF etc
1.2 www 216: unless ($blocklinefeed) {
217: $raw=~s/\r\f/\n/gs; $raw=~s/\f\r/\n/gs;
218: $raw=~s/\r\n/\n/gs; $raw=~s/\n\r/\n/gs;
219: $raw=~s/\f/\n/gs; $raw=~s/\r/\n/gs;
220: $raw=~s/\&\#10\;/\n/gs; $raw=~s/\&\#13\;/\n/gs;
221: }
1.1 www 222: # Generate empty tags, remove wrong end tags
1.2 www 223: unless ($blockemptytags) {
1.6 www 224: $raw=~s/\<(br|hr|img|meta|embed|allow|basefont)([^\>]*?)\>/\<$1$2 \/\>/gis;
225: $raw=~s/\<\/(br|hr|img|meta|embed|allow|basefont)\>//gis;
226: $raw=~s/\/ \/\>/\/\>/gs;
1.2 www 227: unless ($full) {
228: $raw=~s/\<[\/]*(body|head|html)\>//gis;
229: }
1.1 www 230: }
231: # Make standard tags lowercase
1.2 www 232: unless ($blocklowercasing) {
233: foreach ('html','body','head','meta','h1','h2','h3','h4','b','i','m',
234: 'table','tr','td','th','p','br','hr','img','embed','font',
235: 'a','strong','center','title','basefont','li','ol','ul',
236: 'input','select','form','option','script','pre') {
237: $raw=~s/\<$_\s*\>/\<$_\>/gis;
238: $raw=~s/\<\/$_\s*\>/<\/$_\>/gis;
239: $raw=~s/\<$_\s([^\>]*)\>/<$_ $1\>/gis;
240: }
241: }
242: # Replace <font face="symbol">
243: unless ($blockdesymboling) {
244: $raw=&symbolfontreplace($raw);
1.1 www 245: }
246: return $raw;
247: }
248:
249: sub phaseone {
1.2 www 250: my ($r,$fn,$uname,$udom)=@_;
1.11 bisitz 251: $r->print(
252: &Apache::lonhtmlcommon::start_pick_box()
253: .&Apache::lonhtmlcommon::row_title(&mt('Select actions to attempt'))
1.18 ! bisitz 254: .'<label>'
1.11 bisitz 255: .'<input type="checkbox" name="linefeed" checked="checked" /> '
256: .&mt('Linefeeds, formfeeds, and carriage returns')
1.18 ! bisitz 257: .'</label><br />'
! 258: .'<label>'
1.11 bisitz 259: .'<input type="checkbox" name="empty" checked="checked" /> '
260: .&mt('Empty tags')
1.18 ! bisitz 261: .'</label><br />'
! 262: .'<label>'
1.11 bisitz 263: .'<input type="checkbox" name="lower" checked="checked" /> '
264: .&mt('Lower casing')
1.18 ! bisitz 265: .'</label><br />'
! 266: .'<label>'
1.11 bisitz 267: .'<input type="checkbox" name="symbol" checked="checked" /> '
268: .&mt('Symbol font')
1.18 ! bisitz 269: .'</label>'
1.11 bisitz 270: .&Apache::lonhtmlcommon::row_closure(1)
271: .&Apache::lonhtmlcommon::end_pick_box()
272: );
273:
274: $r->print(
275: '<input type="hidden" name="phase" value="two" />'
276: .'<p>'
277: .'<input type="submit" value="'.&mt('Next').'" />'
278: .'</p>'
279: );
1.1 www 280: }
281:
282: sub phasetwo {
1.11 bisitz 283: # Check original file
1.2 www 284: my ($r,$fn,$uname,$udom)=@_;
1.4 www 285: my $text='';
1.14 raeburn 286: my $londocroot = $r->dir_config('lonDocRoot');
287: if (open(IN,"<$londocroot/priv/$udom/$uname".$fn)) {
288: while (my $line=<IN>) {
289: $text.=$line;
290: }
291: close(IN);
1.4 www 292: }
1.18 ! bisitz 293: # Check if any selection was made
! 294: if ($env{'form.linefeed'} ne 'on' &&
! 295: $env{'form.empty'} ne 'on' &&
! 296: $env{'form.lower'} ne 'on' &&
! 297: $env{'form.symbol'} ne 'on') {
! 298: $r->print(
! 299: '<p class="LC_warning">'
! 300: .&mt('Please select at least one option.')
! 301: .'</p>'
! 302: .'<p><a href="javascript:history.back();">'.&mt('Back').'</p>'
! 303: );
! 304: return;
! 305: }
! 306:
1.14 raeburn 307: my $uri="/priv/$udom/$uname".$fn;
1.4 www 308: my $result=&Apache::lonnet::ssi_body($uri,
309: ('grade_target'=>'web',
310: 'return_only_error_and_warning_counts' => 1));
311: my ($errorcount,$warningcount)=split(':',$result);
1.11 bisitz 312:
313: # Display results for original file
314: $r->print(
315: &Apache::lonhtmlcommon::start_pick_box()
316: .&Apache::lonhtmlcommon::row_title(&mt('Original file'))
317: .&Apache::lonhtmlcommon::confirm_success(
318: &mt('[quant,_1,error]',$errorcount), $errorcount)
319: .'<br />'
320: .&Apache::lonhtmlcommon::confirm_success(
321: &mt('[quant,_1,warning]',$warningcount), $warningcount)
322: .&Apache::lonhtmlcommon::row_closure()
323: );
324:
325: # Clean up file
1.4 www 326: $text=&htmlclean($text,1,
327: ($env{'form.linefeed'} ne 'on'),
328: ($env{'form.empty'} ne 'on'),
329: ($env{'form.lower'} ne 'on'),
330: ($env{'form.symbol'} ne 'on'));
331: my ($main,$ext)=($fn=~/^(.*)\.(\w+)/);
332: my $newfn=$main.'_Auto_Cleaned_Up.'.$ext;
1.14 raeburn 333: if (open(OUT,">$londocroot/priv/$udom/$uname".$newfn)) {
334: print OUT $text;
335: close(OUT);
336: }
337: my $newuri="/priv/$udom/$uname".$newfn;
1.4 www 338: $result=&Apache::lonnet::ssi_body($newuri,
1.11 bisitz 339: ('grade_target'=>'web',
340: 'return_only_error_and_warning_counts' => 1));
1.4 www 341: ($errorcount,$warningcount)=split(':',$result);
1.11 bisitz 342:
343: # Display results for cleaned up file
344: $r->print(
345: &Apache::lonhtmlcommon::row_title(&mt('Cleaned up file'))
346: .&Apache::lonhtmlcommon::confirm_success(
347: &mt('[quant,_1,error]',$errorcount), $errorcount)
348: .'<br />'
349: .&Apache::lonhtmlcommon::confirm_success(
350: &mt('[quant,_1,warning]',$warningcount), $warningcount)
351: .&Apache::lonhtmlcommon::row_closure()
352: );
353:
354: # Display actions
355: $r->print(
356: &Apache::lonhtmlcommon::row_title(&mt('Actions'))
357: .'<ul>'
358: .'<li><a href="'.$newuri.'" target="prev">'
359: .&mt('Open (and edit) cleaned up file in new window')
360: .'</a></li>'
1.18 ! bisitz 361: .'<li><a href="'
! 362: .&HTML::Entities::encode(
! 363: '/adm/diff?filename='.&escape($uri)
! 364: .'&versionone=priv&filetwo='.&escape($newuri))
! 365: .'" target="prev">'
1.11 bisitz 366: .&mt('Show diffs in new window')
367: .'</a></li>'
368: .'</ul>'
369: .&Apache::lonhtmlcommon::row_closure(1)
370: .&Apache::lonhtmlcommon::end_pick_box()
371: .'<p>'
372: .'<input type="hidden" name="phase" value="three" />'
373: .'<input type="submit" name="accept" value="'
374: .&mt('Clean Up').'" />'
375: .' <input type="submit" name="reject" value="'
376: .&mt('Cancel').'" />'
377: .'</p>'
378: );
1.2 www 379: }
380:
381: sub phasethree {
382: my ($r,$fn,$uname,$udom)=@_;
1.14 raeburn 383: my $old=$r->dir_config('lonDocRoot')."/priv/$udom/$uname".$fn;
1.4 www 384: my ($main,$ext)=($fn=~/^(.*)\.(\w+)/);
385: my $newfn=$main.'_Auto_Cleaned_Up.'.$ext;
1.14 raeburn 386: my $new=$r->dir_config('lonDocRoot')."/priv/$udom/$uname".$newfn;
1.4 www 387: if ($env{'form.accept'}) {
1.18 ! bisitz 388: $r->print(
1.11 bisitz 389: '<p class="LC_info">'
1.18 ! bisitz 390: .&mt('Accepting changes...')
1.11 bisitz 391: .'</p>'
392: );
1.4 www 393: move($new,$old);
394: } else {
1.18 ! bisitz 395: $r->print(
1.11 bisitz 396: '<p class="LC_info">'
1.18 ! bisitz 397: .&mt('Rejecting changes...')
1.11 bisitz 398: .'</p>'
1.18 ! bisitz 399: );
1.4 www 400: unlink($new);
401: }
1.18 ! bisitz 402: $r->print(
! 403: '<p>'
! 404: .&Apache::lonhtmlcommon::confirm_success(&mt('Done')));
! 405: '</p>'
1.1 www 406: }
407:
408: # ---------------------------------------------------------------- Main Handler
409: sub handler {
410:
1.3 albertel 411: my $r=shift;
1.4 www 412: my $fn='';
1.1 www 413:
414: # Get query string for limited number of parameters
415:
1.3 albertel 416: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
417: ['filename']);
1.1 www 418:
1.3 albertel 419: if ($env{'form.filename'}) {
420: $fn=$env{'form.filename'};
1.15 raeburn 421: $fn=~s{^https?\://[^/]+}{};
1.3 albertel 422: } else {
423: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
424: ' unspecified filename for cleanup', $r->filename);
425: return HTTP_NOT_FOUND;
426: }
427:
428: unless ($fn) {
429: $r->log_reason($env{'user.name'}.' at '.$env{'user.domain'}.
430: ' trying to cleanup non-existing file', $r->filename);
431: return HTTP_NOT_FOUND;
432: }
1.1 www 433:
434: # ----------------------------------------------------------- Start page output
1.3 albertel 435: my $uname;
436: my $udom;
437:
1.17 raeburn 438: ($uname,$udom)=&Apache::lonnet::constructaccess($fn);
1.3 albertel 439: unless (($uname) && ($udom)) {
440: $r->log_reason($uname.' at '.$udom.
441: ' trying to cleanup file '.$env{'form.filename'}.
442: ' ('.$fn.') - not authorized',
443: $r->filename);
444: return HTTP_NOT_ACCEPTABLE;
445: }
446:
447: &Apache::loncommon::content_type($r,'text/html');
448: $r->send_http_header;
449:
1.11 bisitz 450: # Breadcrumbs
1.16 raeburn 451: my $brcrum = [{'href' => &Apache::loncommon::authorspace($fn),
1.11 bisitz 452: 'text' => 'Construction Space'},
453: {'href' => '',
454: 'text' => 'Cleanup XML Document'}];
455:
1.16 raeburn 456: $fn=~s{^/priv/$LONCAPA::domain_re/$LONCAPA::username_re}{};
457:
1.11 bisitz 458: $r->print(&Apache::loncommon::start_page('Cleanup XML Document',
459: undef,
460: {'bread_crumbs' => $brcrum,}));
1.4 www 461: $r->print('<h2>'.$fn.'</h2>'.
462: '<form action="/adm/cleanup" method="post">'.
463: '<input type="hidden" name="filename" value="'.$env{'form.filename'}.'" />');
464: unless ($fn=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
1.18 ! bisitz 465: $r->print('<p class="LC_warning">'.&mt('Cannot cleanup this filetype').'</p>');
1.3 albertel 466: } else {
1.4 www 467: if ($env{'form.phase'} eq 'three') {
468: &phasethree($r,$fn,$uname,$udom);
469: } elsif ($env{'form.phase'} eq 'two') {
470: &phasetwo($r,$fn,$uname,$udom);
471: } else {
472: &phaseone($r,$fn,$uname,$udom);
473: }
1.3 albertel 474: }
1.4 www 475: my $dir=$fn;
1.14 raeburn 476: $dir=~s{[^/]+$}{};
1.11 bisitz 477: $r->print(
478: '</form>'
1.18 ! bisitz 479: .&Apache::lonhtmlcommon::actionbox(
! 480: ['<a href="/priv/'.$udom.'/'.$uname.$fn.'">'.
! 481: &mt('Back to Source File').'</a>',
! 482: '<a href="/priv/'.$udom.'/'.$uname.$dir.'">'.
! 483: &mt('Back to Source Directory').'</a>'])
1.11 bisitz 484: .&Apache::loncommon::end_page()
485: );
486:
1.3 albertel 487: return OK;
1.1 www 488: }
489:
490: 1;
491: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>