Annotation of loncom/xml/lonxml.pm, revision 1.447
1.2 sakharuk 1: # The LearningOnline Network with CAPA
1.3 sakharuk 2: # XML Parser Module
1.2 sakharuk 3: #
1.447 ! albertel 4: # $Id: lonxml.pm,v 1.446 2007/05/17 21:00:59 albertel Exp $
1.139 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: # Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
29: # TtHfunc and TtMfunc (the "Code") may be compiled and linked into
30: # binary executable programs or libraries distributed by the
31: # Michigan State University (the "Licensee"), but any binaries so
32: # distributed are hereby licensed only for use in the context
33: # of a program or computational system for which the Licensee is the
34: # primary author or distributor, and which performs substantial
35: # additional tasks beyond the translation of (La)TeX into HTML.
36: # The C source of the Code may not be distributed by the Licensee
37: # to any other parties under any circumstances.
38: #
1.316 albertel 39:
1.2 sakharuk 40:
1.4 albertel 41: package Apache::lonxml;
1.33 www 42: use vars
1.410 albertel 43: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace $errorcount $warningcount);
1.1 sakharuk 44: use strict;
1.444 albertel 45: use LONCAPA;
1.167 albertel 46: use HTML::LCParser();
1.161 albertel 47: use HTML::TreeBuilder();
48: use HTML::Entities();
49: use Safe();
50: use Safe::Hole();
51: use Math::Cephes();
52: use Math::Random();
53: use Opcode();
1.271 www 54: use POSIX qw(strftime);
1.339 albertel 55: use Time::HiRes qw( gettimeofday tv_interval );
1.393 albertel 56: use Symbol();
1.266 bowersj2 57:
1.72 albertel 58: sub register {
1.141 albertel 59: my ($space,@taglist) = @_;
60: foreach my $temptag (@taglist) {
61: push(@{ $Apache::lonxml::alltags{$temptag} },$space);
1.72 albertel 62: }
63: }
64:
1.141 albertel 65: sub deregister {
66: my ($space,@taglist) = @_;
67: foreach my $temptag (@taglist) {
68: my $tempspace = $Apache::lonxml::alltags{$temptag}[-1];
69: if ($tempspace eq $space) {
70: pop(@{ $Apache::lonxml::alltags{$temptag} });
71: }
72: }
1.142 albertel 73: #&printalltags();
1.141 albertel 74: }
75:
1.46 www 76: use Apache::Constants qw(:common);
1.161 albertel 77: use Apache::lontexconvert();
78: use Apache::style();
79: use Apache::run();
80: use Apache::londefdef();
81: use Apache::scripttag();
1.285 www 82: use Apache::languagetags();
1.161 albertel 83: use Apache::edit();
1.266 bowersj2 84: use Apache::inputtags();
85: use Apache::outputtags();
1.372 albertel 86: use Apache::lonnet;
1.161 albertel 87: use Apache::File();
88: use Apache::loncommon();
1.198 www 89: use Apache::lonfeedback();
1.200 www 90: use Apache::lonmsg();
1.217 matthew 91: use Apache::loncacc();
1.431 www 92: use Apache::lonmaxima();
1.280 www 93: use Apache::lonlocal;
1.79 www 94:
1.72 albertel 95: #================================================== Main subroutine: xmlparse
96: #debugging control, to turn on debugging modify the correct handler
97: $Apache::lonxml::debug=0;
1.206 albertel 98:
99: # keeps count of the number of warnings and errors generated in a parse
100: $warningcount=0;
101: $errorcount=0;
1.72 albertel 102:
103: #path to the directory containing the file currently being processed
104: @pwd=();
105:
106: #these two are used for capturing a subset of the output for later processing,
107: #don't touch them directly use &startredirection and &endredirection
108: @outputstack = ();
109: $redirection = 0;
110:
111: #controls wheter the <import> tag actually does
112: $import = 1;
113: @extlinks=();
114:
115: # meta mode is a bit weird only some output is to be turned off
116: #<output> tag turns metamode off (defined in londefdef.pm)
117: $metamode = 0;
118:
119: # turns on and of run::evaluate actually derefencing var refs
120: $evaluate = 1;
1.7 albertel 121:
1.74 albertel 122: # data structure for eidt mode, determines what tags can go into what other tags
123: %insertlist=();
1.68 www 124:
1.99 albertel 125: # stores the list of active tag namespaces
1.76 albertel 126: @namespace=();
127:
1.172 albertel 128: # a pointer the the Apache request object
129: $Apache::lonxml::request='';
130:
1.216 sakharuk 131: # a problem number counter, and check on ether it is used
1.237 sakharuk 132: $Apache::lonxml::counter=1;
1.204 albertel 133: $Apache::lonxml::counter_changed=0;
134:
1.212 albertel 135: #internal check on whether to look at style defs
136: $Apache::lonxml::usestyle=1;
1.260 albertel 137:
138: #locations used to store the parameter string for style substitutions
139: $Apache::lonxml::style_values='';
140: $Apache::lonxml::style_end_values='';
1.212 albertel 141:
1.281 albertel 142: #array of ssi calls that need to occur after we are done parsing
143: @Apache::lonxml::ssi_info=();
144:
1.282 albertel 145: #should we do the postag variable interpolation
146: $Apache::lonxml::post_evaluate=1;
147:
1.295 albertel 148: #a header message to emit in the case of any generated warning or errors
149: $Apache::lonxml::warnings_error_header='';
150:
1.396 foxr 151: # Control whether or not LaTeX symbols should be substituted for their
152: # \ style equivalents...this may be turned off e.g. in an verbatim
153: # environment.
154:
155: $Apache::lonxml::substitute_LaTeX_symbols = 1; # Starts out on.
156:
157: sub enable_LaTeX_substitutions {
158: $Apache::lonxml::substitute_LaTeX_symbols = 1;
159: }
160: sub disable_LaTeX_substitutions {
161: $Apache::lonxml::substitute_LaTeX_symbols = 0;
162: }
163:
1.68 www 164: sub xmlend {
1.335 sakharuk 165: my ($target,$parser)=@_;
1.278 www 166: my $mode='xml';
167: my $status='OPEN';
1.368 albertel 168: if ($Apache::lonhomework::parsing_a_problem ||
169: $Apache::lonhomework::parsing_a_task ) {
1.278 www 170: $mode='problem';
171: $status=$Apache::inputtags::status[-1];
172: }
1.362 matthew 173: my $discussion;
1.379 albertel 174: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
175: ['LONCAPA_INTERNAL_no_discussion']);
1.372 albertel 176: if (! exists($env{'form.LONCAPA_INTERNAL_no_discussion'}) ||
177: $env{'form.LONCAPA_INTERNAL_no_discussion'} ne 'true') {
1.362 matthew 178: $discussion=&Apache::lonfeedback::list_discussion($mode,$status);
179: }
1.334 sakharuk 180: if ($target eq 'tex') {
1.335 sakharuk 181: $discussion.='<tex>\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\end{document}</tex>';
1.334 sakharuk 182: &Apache::lonxml::newparser($parser,\$discussion,'');
183: return '';
184: }
1.405 albertel 185:
1.407 albertel 186: return $discussion;
1.119 www 187: }
188:
189: sub tokeninputfield {
1.120 www 190: my $defhost=$Apache::lonnet::perlvar{'lonHostID'};
191: $defhost=~tr/a-z/A-Z/;
1.119 www 192: return (<<ENDINPUTFIELD)
1.226 albertel 193: <script type="text/javascript">
1.120 www 194: function updatetoken() {
195: var comp=new Array;
196: var barcode=unescape(document.tokeninput.barcode.value);
197: comp=barcode.split('*');
198: if (typeof(comp[0])!="undefined") {
199: document.tokeninput.codeone.value=comp[0];
200: }
201: if (typeof(comp[1])!="undefined") {
202: document.tokeninput.codetwo.value=comp[1];
203: }
204: if (typeof(comp[2])!="undefined") {
205: comp[2]=comp[2].toUpperCase();
206: document.tokeninput.codethree.value=comp[2];
207: }
208: document.tokeninput.barcode.value='';
209: }
210: </script>
211: <form method="post" name="tokeninput">
1.119 www 212: <table border="2" bgcolor="#FFFFBB">
213: <tr><th>DocID Checkin</th></tr>
214: <tr><td>
215: <table>
216: <tr>
217: <td>Scan in Barcode</td>
1.120 www 218: <td><input type="text" size="22" name="barcode"
219: onChange="updatetoken()"/></td>
1.119 www 220: </tr>
221: <tr><td><i>or</i> Type in DocID</td>
222: <td>
223: <input type="text" size="5" name="codeone" />
1.120 www 224: <b><font size="+2">*</font></b>
1.119 www 225: <input type="text" size="5" name="codetwo" />
1.120 www 226: <b><font size="+2">*</font></b>
227: <input type="text" size="10" name="codethree" value="$defhost"
228: onChange="this.value=this.value.toUpperCase()" />
1.119 www 229: </td></tr>
230: </table>
231: </td></tr>
232: <tr><td><input type="submit" value="Check in DocID" /></td></tr>
233: </table>
234: </form>
235: ENDINPUTFIELD
1.112 www 236: }
237:
1.116 www 238: sub maketoken {
1.118 www 239: my ($symb,$tuname,$tudom,$tcrsid)=@_;
1.112 www 240: unless ($symb) {
241: $symb=&Apache::lonnet::symbread();
242: }
243: unless ($tuname) {
1.372 albertel 244: $tuname=$env{'user.name'};
245: $tudom=$env{'user.domain'};
246: $tcrsid=$env{'request.course.id'};
1.112 www 247: }
1.116 www 248:
1.118 www 249: return &Apache::lonnet::checkout($symb,$tuname,$tudom,$tcrsid);
250: }
251:
252: sub printtokenheader {
1.133 albertel 253: my ($target,$token,$tsymb,$tcrsid,$tudom,$tuname)=@_;
1.116 www 254: unless ($token) { return ''; }
1.118 www 255:
1.423 albertel 256: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.133 albertel 257: unless ($tsymb) {
258: $tsymb=$symb;
1.118 www 259: }
260: unless ($tuname) {
1.133 albertel 261: $tuname=$name;
262: $tudom=$domain;
263: $tcrsid=$courseid;
1.118 www 264: }
1.114 www 265:
1.390 albertel 266: my $plainname=&Apache::loncommon::plainname($tuname,$tudom);
1.114 www 267:
1.112 www 268: if ($target eq 'web') {
1.145 www 269: my %idhash=&Apache::lonnet::idrget($tudom,($tuname));
1.115 www 270: return
1.221 albertel 271: '<img align="right" src="/cgi-bin/barcode.png?encode='.$token.'" />'.
1.284 www 272: &mt('Checked out for').' '.$plainname.
273: '<br />'.&mt('User').': '.$tuname.' at '.$tudom.
274: '<br />'.&mt('ID').': '.$idhash{$tuname}.
275: '<br />'.&mt('CourseID').': '.$tcrsid.
1.372 albertel 276: '<br />'.&mt('Course').': '.$env{'course.'.$tcrsid.'.description'}.
1.284 www 277: '<br />'.&mt('DocID').': '.$token.
278: '<br />'.&mt('Time').': '.&Apache::lonlocal::locallocaltime().'<hr />';
1.112 www 279: } else {
1.121 albertel 280: return $token;
1.112 www 281: }
1.68 www 282: }
283:
1.48 albertel 284: sub printalltags {
285: my $temp;
286: foreach $temp (sort keys %Apache::lonxml::alltags) {
1.141 albertel 287: &Apache::lonxml::debug("$temp -- ".
288: join(',',@{ $Apache::lonxml::alltags{$temp} }));
1.48 albertel 289: }
290: }
1.31 sakharuk 291:
1.3 sakharuk 292: sub xmlparse {
1.172 albertel 293: my ($request,$target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96 albertel 294:
1.172 albertel 295: &setup_globals($request,$target);
1.232 albertel 296: &Apache::inputtags::initialize_inputtags();
1.370 albertel 297: &Apache::bridgetask::initialize_bridgetask();
1.232 albertel 298: &Apache::outputtags::initialize_outputtags();
299: &Apache::edit::initialize_edit();
1.287 albertel 300: &Apache::londefdef::initialize_londefdef();
1.244 albertel 301:
1.178 www 302: #
303: # do we have a course style file?
304: #
305:
1.372 albertel 306: if ($env{'request.course.id'} && $env{'request.state'} ne 'construct') {
1.178 www 307: my $bodytext=
1.372 albertel 308: $env{'course.'.$env{'request.course.id'}.'.default_xml_style'};
1.178 www 309: if ($bodytext) {
1.337 albertel 310: foreach my $file (split(',',$bodytext)) {
311: my $location=&Apache::lonnet::filelocation('',$file);
312: my $styletext=&Apache::lonnet::getfile($location);
313: if ($styletext ne '-1') {
314: %style_for_target = (%style_for_target,
315: &Apache::style::styleparser($target,$styletext));
316: }
317: }
318: }
1.372 albertel 319: } elsif ($env{'construct.style'} && ($env{'request.state'} eq 'construct')) {
320: my $location=&Apache::lonnet::filelocation('',$env{'construct.style'});
1.291 sakharuk 321: my $styletext=&Apache::lonnet::getfile($location);
322: if ($styletext ne '-1') {
323: %style_for_target = (%style_for_target,
324: &Apache::style::styleparser($target,$styletext));
325: }
1.178 www 326: }
1.255 sakharuk 327: #&printalltags();
1.16 albertel 328: my @pars = ();
1.372 albertel 329: my $pwd=$env{'request.filename'};
1.23 albertel 330: $pwd =~ s:/[^/]*$::;
331: &newparser(\@pars,\$content_file_string,$pwd);
1.24 sakharuk 332:
1.3 sakharuk 333: my $safeeval = new Safe;
1.40 albertel 334: my $safehole = new Safe::Hole;
1.82 ng 335: &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3 sakharuk 336: #-------------------- Redefinition of the target in the case of compound target
337:
338: ($target, my @tenta) = split('&&',$target);
339:
1.150 albertel 340: my @stack = ();
1.3 sakharuk 341: my @parstack = ();
1.358 albertel 342: &initdepth();
343: &init_alarm();
1.101 albertel 344: my $finaloutput = &inner_xmlparse($target,\@stack,\@parstack,\@pars,
1.394 albertel 345: $safeeval,\%style_for_target,1);
1.255 sakharuk 346:
1.425 albertel 347: if (@stack) {
348: &warning("At end of file some tags were still left unclosed, ".
349: '<tt><'.join('></tt>, <tt><',reverse(@stack)).
350: '></tt>');
351: }
1.372 albertel 352: if ($env{'request.uri'}) {
353: &writeallows($env{'request.uri'});
1.125 www 354: }
1.281 albertel 355: &do_registered_ssi();
1.204 albertel 356: if ($Apache::lonxml::counter_changed) { &store_counter() }
1.393 albertel 357:
358: &clean_safespace($safeeval);
359:
1.372 albertel 360: if ($env{'form.return_only_error_and_warning_counts'}) {
1.361 www 361: return "$errorcount:$warningcount";
362: }
1.3 sakharuk 363: return $finaloutput;
1.106 www 364: }
365:
1.191 albertel 366: sub latex_special_symbols {
1.272 albertel 367: my ($string,$where)=@_;
1.396 foxr 368: #
369: # If e.g. in verbatim mode, then don't substitute.
370: # but return original string.
371: #
372: if (!($Apache::lonxml::substitute_LaTeX_symbols)) {
373: return $string;
374: }
1.235 sakharuk 375: if ($where eq 'header') {
1.414 foxr 376: $string =~ s/\\/\$\\backslash\$/g; # \ -> $\backslash$ per LaTex line by line pg 10.
1.311 albertel 377: $string =~ s/(\$|%|\{|\})/\\$1/g;
378: $string=&Apache::lonprintout::character_chart($string);
379: # any & or # leftover should be safe to just escape
380: $string=~s/([^\\])\&/$1\\\&/g;
381: $string=~s/([^\\])\#/$1\\\#/g;
1.415 foxr 382: $string =~ s/_/\\_/g; # _ -> \_
383: $string =~ s/\^/\\\^{}/g; # ^ -> \^{}
1.229 sakharuk 384: } else {
1.312 albertel 385: $string=~s/\\/\\ensuremath{\\backslash}/g;
1.367 albertel 386: $string=~s/\\\%|\%/\\\%/g;
387: $string=~s/\\{|{/\\{/g;
388: $string=~s/\\}|}/\\}/g;
1.378 albertel 389: $string=~s/\\ensuremath\\{\\backslash\\}/\\ensuremath{\\backslash}/g;
1.367 albertel 390: $string=~s/\\\$|\$/\\\$/g;
391: $string=~s/\\\_|\_/\\\_/g;
1.313 albertel 392: $string=~s/([^\\]|^)(\~|\^)/$1\\$2\\strut /g;
1.310 sakharuk 393: $string=~s/(>|<)/\\ensuremath\{$1\}/g; #more or less
1.311 albertel 394: $string=&Apache::lonprintout::character_chart($string);
395: # any & or # leftover should be safe to just escape
1.367 albertel 396: $string=~s/\\\&|\&/\\\&/g;
397: $string=~s/\\\#|\#/\\\#/g;
1.332 sakharuk 398: $string=~s/\|/\$\\mid\$/g;
1.310 sakharuk 399: #single { or } How to escape?
1.229 sakharuk 400: }
1.272 albertel 401: return $string;
1.188 sakharuk 402: }
403:
1.101 albertel 404: sub inner_xmlparse {
1.394 albertel 405: my ($target,$stack,$parstack,$pars,$safeeval,$style_for_target,$start)=@_;
1.101 albertel 406: my $finaloutput = '';
407: my $result;
408: my $token;
1.258 albertel 409: my $dontpop=0;
1.389 albertel 410: my $startredirection = $Apache::lonxml::redirection;
1.101 albertel 411: while ( $#$pars > -1 ) {
412: while ($token = $$pars['-1']->get_token) {
1.261 albertel 413: if (($token->[0] eq 'T') || ($token->[0] eq 'C') ) {
1.101 albertel 414: if ($metamode<1) {
1.190 albertel 415: my $text=$token->[1];
1.193 albertel 416: if ($token->[0] eq 'C' && $target eq 'tex') {
1.239 sakharuk 417: $text = '';
418: # $text = '%'.$text."\n";
1.182 sakharuk 419: }
1.190 albertel 420: $result.=$text;
1.101 albertel 421: }
1.261 albertel 422: } elsif (($token->[0] eq 'D')) {
423: if ($metamode<1 && $target eq 'web') {
424: my $text=$token->[1];
425: $result.=$text;
426: }
1.101 albertel 427: } elsif ($token->[0] eq 'PI') {
1.261 albertel 428: if ($metamode<1 && $target eq 'web') {
1.101 albertel 429: $result=$token->[2];
430: }
431: } elsif ($token->[0] eq 'S') {
1.140 albertel 432: # add tag to stack
1.101 albertel 433: push (@$stack,$token->[1]);
434: # add parameters list to another stack
435: push (@$parstack,&parstring($token));
1.140 albertel 436: &increasedepth($token);
1.212 albertel 437: if ($Apache::lonxml::usestyle &&
438: exists($$style_for_target{$token->[1]})) {
439: $Apache::lonxml::usestyle=0;
440: my $string=$$style_for_target{$token->[1]}.
441: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
442: &Apache::lonxml::newparser($pars,\$string);
1.257 albertel 443: $Apache::lonxml::style_values=$$parstack[-1];
1.259 albertel 444: $Apache::lonxml::style_end_values=$$parstack[-1];
1.101 albertel 445: } else {
446: $result = &callsub("start_$token->[1]", $target, $token, $stack,
447: $parstack, $pars, $safeeval, $style_for_target);
1.140 albertel 448: }
1.101 albertel 449: } elsif ($token->[0] eq 'E') {
1.212 albertel 450: if ($Apache::lonxml::usestyle &&
451: exists($$style_for_target{'/'."$token->[1]"})) {
452: $Apache::lonxml::usestyle=0;
453: my $string=$$style_for_target{'/'.$token->[1]}.
1.258 albertel 454: '<LONCAPA_INTERNAL_TURN_STYLE_ON end="'.$token->[1].'" />';
1.212 albertel 455: &Apache::lonxml::newparser($pars,\$string);
1.259 albertel 456: $Apache::lonxml::style_values=$Apache::lonxml::style_end_values;
457: $Apache::lonxml::style_end_values='';
1.258 albertel 458: $dontpop=1;
1.101 albertel 459: } else {
1.258 albertel 460: #clear out any tags that didn't end
461: while ($token->[1] ne $$stack['-1'] && ($#$stack > -1)) {
462: my $lasttag=$$stack[-1];
1.317 albertel 463: if ($token->[1] =~ /^\Q$lasttag\E$/i) {
1.258 albertel 464: &Apache::lonxml::warning('Using tag </'.$token->[1].'> on line '.$token->[3].' as end tag to <'.$$stack[-1].'>');
465: last;
466: } else {
467: &Apache::lonxml::warning('Found tag </'.$token->[1].'> on line '.$token->[3].' when looking for </'.$$stack[-1].'> in file');
468: &end_tag($stack,$parstack,$token);
469: }
470: }
471: $result = &callsub("end_$token->[1]", $target, $token, $stack,
472: $parstack, $pars,$safeeval, $style_for_target);
1.101 albertel 473: }
474: } else {
475: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
476: }
477: #evaluate variable refs in result
1.282 albertel 478: if ($Apache::lonxml::post_evaluate &&$result ne "") {
1.257 albertel 479: my $extras;
480: if (!$Apache::lonxml::usestyle) {
481: $extras=$Apache::lonxml::style_values;
482: }
1.101 albertel 483: if ( $#$parstack > -1 ) {
1.257 albertel 484: $result=&Apache::run::evaluate($result,$safeeval,$extras.$$parstack[-1]);
1.101 albertel 485: } else {
1.257 albertel 486: $result= &Apache::run::evaluate($result,$safeeval,$extras);
1.101 albertel 487: }
1.163 albertel 488: }
1.282 albertel 489: $Apache::lonxml::post_evaluate=1;
490:
1.190 albertel 491: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.249 albertel 492: #Style file definitions should be correct
1.250 albertel 493: if ($target eq 'tex' && ($Apache::lonxml::usestyle)) {
1.311 albertel 494: $result=&latex_special_symbols($result);
1.249 albertel 495: }
1.190 albertel 496: }
497:
1.169 albertel 498: if ($Apache::lonxml::redirection) {
499: $Apache::lonxml::outputstack['-1'] .= $result;
500: } else {
501: $finaloutput.=$result;
502: }
503: $result = '';
504:
1.258 albertel 505: if ($token->[0] eq 'E' && !$dontpop) {
1.101 albertel 506: &end_tag($stack,$parstack,$token);
507: }
1.258 albertel 508: $dontpop=0;
1.224 albertel 509: }
1.212 albertel 510: if ($#$pars > -1) {
511: pop @$pars;
512: pop @Apache::lonxml::pwd;
513: }
1.101 albertel 514: }
515:
516: # if ($target eq 'meta') {
517: # $finaloutput.=&endredirection;
518: # }
519:
1.394 albertel 520: if ( $start && $target eq 'grade') { &endredirection(); }
1.389 albertel 521: if ( $Apache::lonxml::redirection > $startredirection) {
522: while ($Apache::lonxml::redirection > $startredirection) {
523: $finaloutput .= &endredirection();
1.387 albertel 524: }
525: }
1.101 albertel 526: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
527: $finaloutput=&afterburn($finaloutput);
1.216 sakharuk 528: }
1.101 albertel 529: return $finaloutput;
530: }
1.67 www 531:
1.318 matthew 532: ##
533: ## Looks to see if there is a subroutine defined for this tag. If so, call it,
534: ## otherwise do not call it as we do not know what it is.
535: ##
1.7 albertel 536: sub callsub {
1.84 albertel 537: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 538: my $currentstring='';
1.72 albertel 539: my $nodefault;
1.7 albertel 540: {
1.59 albertel 541: my $sub1;
1.7 albertel 542: no strict 'refs';
1.68 www 543: my $tag=$token->[1];
1.236 www 544: # get utterly rid of extended html tags
545: if ($tag=~/^x\-/i) { return ''; }
1.141 albertel 546: my $space=$Apache::lonxml::alltags{$tag}[-1];
1.68 www 547: if (!$space) {
1.141 albertel 548: $tag=~tr/A-Z/a-z/;
1.68 www 549: $sub=~tr/A-Z/a-z/;
1.141 albertel 550: $space=$Apache::lonxml::alltags{$tag}[-1]
1.68 www 551: }
1.97 albertel 552:
553: my $deleted=0;
554: if (($token->[0] eq 'S') && ($target eq 'modified')) {
555: $deleted=&Apache::edit::handle_delete($space,$target,$token,$tagstack,
556: $parstack,$parser,$safeeval,
557: $style);
558: }
559: if (!$deleted) {
560: if ($space) {
1.220 albertel 561: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode");
1.97 albertel 562: $sub1="$space\:\:$sub";
563: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
564: $parstack,$parser,$safeeval,
565: $style);
566: } else {
1.318 matthew 567: if ($target eq 'tex') {
568: # throw away tag name
569: return '';
570: }
1.220 albertel 571: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode");
1.97 albertel 572: if ($metamode <1) {
573: if (defined($token->[4]) && ($metamode < 1)) {
574: $currentstring = $token->[4];
575: } else {
576: $currentstring = $token->[2];
577: }
1.62 sakharuk 578: }
1.7 albertel 579: }
1.97 albertel 580: # &Apache::lonxml::debug("nodefalt:$nodefault:");
581: if ($currentstring eq '' && $nodefault eq '') {
582: if ($target eq 'edit') {
1.220 albertel 583: #&Apache::lonxml::debug("doing default edit for $token->[1]");
1.97 albertel 584: if ($token->[0] eq 'S') {
585: $currentstring = &Apache::edit::tag_start($target,$token);
586: } elsif ($token->[0] eq 'E') {
587: $currentstring = &Apache::edit::tag_end($target,$token);
588: }
1.441 albertel 589: }
590: }
591: if ($target eq 'modified' && $nodefault eq '') {
592: if ($currentstring eq '') {
593: if ($token->[0] eq 'S') {
594: $currentstring = $token->[4];
595: } elsif ($token->[0] eq 'E') {
596: $currentstring = $token->[2];
597: } else {
598: $currentstring = $token->[2];
599: }
600: }
1.97 albertel 601: if ($token->[0] eq 'S') {
1.447 ! albertel 602: $currentstring.=&Apache::edit::handle_insert();
1.210 www 603: } elsif ($token->[0] eq 'E') {
1.447 ! albertel 604: $currentstring.=&Apache::edit::handle_insertafter($token->[1]);
1.97 albertel 605: }
1.72 albertel 606: }
1.7 albertel 607: }
608: use strict 'refs';
609: }
610: return $currentstring;
1.82 ng 611: }
612:
1.96 albertel 613: sub setup_globals {
1.172 albertel 614: my ($request,$target)=@_;
615: $Apache::lonxml::request=$request;
1.205 www 616: $errorcount=0;
617: $warningcount=0;
1.207 albertel 618: $Apache::lonxml::default_homework_loaded=0;
1.212 albertel 619: $Apache::lonxml::usestyle=1;
1.204 albertel 620: &init_counter();
1.101 albertel 621: @Apache::lonxml::pwd=();
1.124 albertel 622: @Apache::lonxml::extlinks=();
1.281 albertel 623: @Apache::lonxml::ssi_info=();
1.282 albertel 624: $Apache::lonxml::post_evaluate=1;
1.295 albertel 625: $Apache::lonxml::warnings_error_header='';
1.397 albertel 626: $Apache::lonxml::substitute_LaTeX_symbols = 1;
1.96 albertel 627: if ($target eq 'meta') {
628: $Apache::lonxml::redirection = 0;
629: $Apache::lonxml::metamode = 1;
630: $Apache::lonxml::evaluate = 1;
631: $Apache::lonxml::import = 0;
1.129 albertel 632: } elsif ($target eq 'answer') {
633: $Apache::lonxml::redirection = 0;
634: $Apache::lonxml::metamode = 1;
635: $Apache::lonxml::evaluate = 1;
636: $Apache::lonxml::import = 1;
1.96 albertel 637: } elsif ($target eq 'grade') {
1.387 albertel 638: &startredirection(); #ended in inner_xmlparse on exit
1.96 albertel 639: $Apache::lonxml::metamode = 0;
640: $Apache::lonxml::evaluate = 1;
641: $Apache::lonxml::import = 1;
642: } elsif ($target eq 'modified') {
643: $Apache::lonxml::redirection = 0;
644: $Apache::lonxml::metamode = 0;
645: $Apache::lonxml::evaluate = 0;
646: $Apache::lonxml::import = 0;
647: } elsif ($target eq 'edit') {
648: $Apache::lonxml::redirection = 0;
649: $Apache::lonxml::metamode = 0;
650: $Apache::lonxml::evaluate = 0;
651: $Apache::lonxml::import = 0;
1.163 albertel 652: } elsif ($target eq 'analyze') {
653: $Apache::lonxml::redirection = 0;
654: $Apache::lonxml::metamode = 0;
655: $Apache::lonxml::evaluate = 1;
656: $Apache::lonxml::import = 1;
1.96 albertel 657: } else {
658: $Apache::lonxml::redirection = 0;
659: $Apache::lonxml::metamode = 0;
660: $Apache::lonxml::evaluate = 1;
661: $Apache::lonxml::import = 1;
662: }
663: }
664:
1.82 ng 665: sub init_safespace {
666: my ($target,$safeeval,$safehole,$safeinit) = @_;
1.393 albertel 667: $safeeval->deny_only(':dangerous');
668: $safeeval->reval('use Math::Complex;');
1.383 albertel 669: $safeeval->permit_only(":default");
1.82 ng 670: $safeeval->permit("entereval");
671: $safeeval->permit(":base_math");
672: $safeeval->permit("sort");
1.286 albertel 673: $safeeval->permit("time");
1.371 albertel 674: $safeeval->deny("rand");
675: $safeeval->deny("srand");
1.82 ng 676: $safeeval->deny(":base_io");
1.102 albertel 677: $safehole->wrap(\&Apache::scripttag::xmlparse,$safeeval,'&xmlparse');
1.251 albertel 678: $safehole->wrap(\&Apache::outputtags::multipart,$safeeval,'&multipart');
1.82 ng 679: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
1.324 albertel 680: $safehole->wrap(\&Apache::chemresponse::chem_standard_order,$safeeval,
681: '&chem_standard_order');
1.369 albertel 682: $safehole->wrap(\&Apache::response::check_status,$safeeval,'&check_status');
1.324 albertel 683:
1.431 www 684: $safehole->wrap(\&Apache::lonmaxima::maxima_eval,$safeeval,'&maxima_eval');
1.432 www 685: $safehole->wrap(\&Apache::lonmaxima::maxima_check,$safeeval,'&maxima_check');
1.433 albertel 686: $safehole->wrap(\&Apache::lonmaxima::maxima_cas_formula_fix,$safeeval,
687: '&maxima_cas_formula_fix');
688:
689: $safehole->wrap(\&Apache::caparesponse::capa_formula_fix,$safeeval,
690: '&capa_formula_fix');
1.431 www 691:
1.82 ng 692: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
693: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
694: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
695: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
696: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
697: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
698: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
699: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
700: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
701: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
702: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
703: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
704: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
705: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
706: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
707: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
708: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
709: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
710: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.215 albertel 711:
712: $safehole->wrap(\&Math::Cephes::bdtr ,$safeeval,'&bdtr' );
713: $safehole->wrap(\&Math::Cephes::bdtrc ,$safeeval,'&bdtrc' );
714: $safehole->wrap(\&Math::Cephes::bdtri ,$safeeval,'&bdtri' );
715: $safehole->wrap(\&Math::Cephes::btdtr ,$safeeval,'&btdtr' );
716: $safehole->wrap(\&Math::Cephes::chdtr ,$safeeval,'&chdtr' );
717: $safehole->wrap(\&Math::Cephes::chdtrc,$safeeval,'&chdtrc');
718: $safehole->wrap(\&Math::Cephes::chdtri,$safeeval,'&chdtri');
719: $safehole->wrap(\&Math::Cephes::fdtr ,$safeeval,'&fdtr' );
720: $safehole->wrap(\&Math::Cephes::fdtrc ,$safeeval,'&fdtrc' );
721: $safehole->wrap(\&Math::Cephes::fdtri ,$safeeval,'&fdtri' );
722: $safehole->wrap(\&Math::Cephes::gdtr ,$safeeval,'&gdtr' );
723: $safehole->wrap(\&Math::Cephes::gdtrc ,$safeeval,'&gdtrc' );
724: $safehole->wrap(\&Math::Cephes::nbdtr ,$safeeval,'&nbdtr' );
725: $safehole->wrap(\&Math::Cephes::nbdtrc,$safeeval,'&nbdtrc');
726: $safehole->wrap(\&Math::Cephes::nbdtri,$safeeval,'&nbdtri');
727: $safehole->wrap(\&Math::Cephes::ndtr ,$safeeval,'&ndtr' );
728: $safehole->wrap(\&Math::Cephes::ndtri ,$safeeval,'&ndtri' );
729: $safehole->wrap(\&Math::Cephes::pdtr ,$safeeval,'&pdtr' );
730: $safehole->wrap(\&Math::Cephes::pdtrc ,$safeeval,'&pdtrc' );
731: $safehole->wrap(\&Math::Cephes::pdtri ,$safeeval,'&pdtri' );
732: $safehole->wrap(\&Math::Cephes::stdtr ,$safeeval,'&stdtr' );
733: $safehole->wrap(\&Math::Cephes::stdtri,$safeeval,'&stdtri');
734:
1.383 albertel 735: $safehole->wrap(\&Math::Cephes::Matrix::mat,$safeeval,'&mat');
736: $safehole->wrap(\&Math::Cephes::Matrix::new,$safeeval,
737: '&Math::Cephes::Matrix::new');
738: $safehole->wrap(\&Math::Cephes::Matrix::coef,$safeeval,
739: '&Math::Cephes::Matrix::coef');
740: $safehole->wrap(\&Math::Cephes::Matrix::clr,$safeeval,
741: '&Math::Cephes::Matrix::clr');
742: $safehole->wrap(\&Math::Cephes::Matrix::add,$safeeval,
743: '&Math::Cephes::Matrix::add');
744: $safehole->wrap(\&Math::Cephes::Matrix::sub,$safeeval,
745: '&Math::Cephes::Matrix::sub');
746: $safehole->wrap(\&Math::Cephes::Matrix::mul,$safeeval,
747: '&Math::Cephes::Matrix::mul');
748: $safehole->wrap(\&Math::Cephes::Matrix::div,$safeeval,
749: '&Math::Cephes::Matrix::div');
750: $safehole->wrap(\&Math::Cephes::Matrix::inv,$safeeval,
751: '&Math::Cephes::Matrix::inv');
752: $safehole->wrap(\&Math::Cephes::Matrix::transp,$safeeval,
753: '&Math::Cephes::Matrix::transp');
754: $safehole->wrap(\&Math::Cephes::Matrix::simq,$safeeval,
755: '&Math::Cephes::Matrix::simq');
756: $safehole->wrap(\&Math::Cephes::Matrix::mat_to_vec,$safeeval,
757: '&Math::Cephes::Matrix::mat_to_vec');
758: $safehole->wrap(\&Math::Cephes::Matrix::vec_to_mat,$safeeval,
759: '&Math::Cephes::Matrix::vec_to_mat');
760: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
761: '&Math::Cephes::Matrix::check');
762: $safehole->wrap(\&Math::Cephes::Matrix::check,$safeeval,
763: '&Math::Cephes::Matrix::check');
764:
1.215 albertel 765: # $safehole->wrap(\&Math::Cephes::new_fract,$safeeval,'&new_fract');
766: # $safehole->wrap(\&Math::Cephes::radd,$safeeval,'&radd');
767: # $safehole->wrap(\&Math::Cephes::rsub,$safeeval,'&rsub');
768: # $safehole->wrap(\&Math::Cephes::rmul,$safeeval,'&rmul');
769: # $safehole->wrap(\&Math::Cephes::rdiv,$safeeval,'&rdiv');
770: # $safehole->wrap(\&Math::Cephes::euclid,$safeeval,'&euclid');
771:
1.91 ng 772: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
773: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
774: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
775: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
776: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
777: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
778: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
779: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
780: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
781: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
782: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93 ng 783: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91 ng 784: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
785: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
786: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
787: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
788: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
789: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
790: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
791: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
792: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
1.305 albertel 793: $safehole->wrap(\&Apache::lonxml::error,$safeeval,'&LONCAPA_INTERNAL_ERROR');
1.311 albertel 794: $safehole->wrap(\&Apache::lonxml::debug,$safeeval,'&LONCAPA_INTERNAL_DEBUG');
1.420 albertel 795: $safehole->wrap(\&Apache::lonnet::logthis,$safeeval,'&LONCAPA_INTERNAL_LOGTHIS');
796: $safehole->wrap(\&Apache::inputtags::finalizeawards,$safeeval,'&LONCAPA_INTERNAL_FINALIZEAWARDS');
1.322 albertel 797: $safehole->wrap(\&Apache::caparesponse::get_sigrange,$safeeval,'&LONCAPA_INTERNAL_get_sigrange');
1.430 albertel 798: # use Data::Dumper;
799: # $safehole->wrap(\&Data::Dumper::Dumper,$safeeval,'&LONCAPA_INTERNAL_Dumper');
1.82 ng 800: #need to inspect this class of ops
801: # $safeeval->deny(":base_orig");
1.331 albertel 802: $safeeval->permit("require");
1.91 ng 803: $safeinit .= ';$external::target="'.$target.'";';
1.82 ng 804: &Apache::run::run($safeinit,$safeeval);
1.373 albertel 805: &initialize_rndseed($safeeval);
806: }
1.303 albertel 807:
1.393 albertel 808: sub clean_safespace {
809: my ($safeeval) = @_;
810: delete_package_recurse($safeeval->{Root});
811: }
812:
813: sub delete_package_recurse {
814: my ($package) = @_;
815: my @subp;
816: {
817: no strict 'refs';
818: while (my ($key,$val) = each(%{*{"$package\::"}})) {
819: if (!defined($val)) { next; }
820: local (*ENTRY) = $val;
821: if (defined *ENTRY{HASH} && $key =~ /::$/ &&
822: $key ne "main::" && $key ne "<none>::")
823: {
824: my ($p) = $package ne "main" ? "$package\::" : "";
825: ($p .= $key) =~ s/::$//;
826: push(@subp,$p);
827: }
828: }
829: }
830: foreach my $p (@subp) {
831: delete_package_recurse($p);
832: }
833: Symbol::delete_package($package);
834: }
835:
1.373 albertel 836: sub initialize_rndseed {
837: my ($safeeval)=@_;
838: my $rndseed;
1.423 albertel 839: my ($symb,$courseid,$domain,$name) = &Apache::lonnet::whichuser();
1.373 albertel 840: $rndseed=&Apache::lonnet::rndseed($symb,$courseid,$domain,$name);
841: my $safeinit = '$external::randomseed="'.$rndseed.'";';
842: &Apache::lonxml::debug("Setting rndseed to $rndseed");
843: &Apache::run::run($safeinit,$safeeval);
1.207 albertel 844: }
845:
846: sub default_homework_load {
847: my ($safeeval)=@_;
848: &Apache::lonxml::debug('Loading default_homework');
849: my $default=&Apache::lonnet::getfile('/home/httpd/html/res/adm/includes/default_homework.lcpm');
1.241 albertel 850: if ($default eq -1) {
1.207 albertel 851: &Apache::lonxml::error("<b>Unable to find <i>default_homework.lcpm</i></b>");
852: } else {
853: &Apache::run::run($default,$safeeval);
854: $Apache::lonxml::default_homework_loaded=1;
855: }
1.17 albertel 856: }
857:
1.358 albertel 858: {
859: my $alarm_depth;
860: sub init_alarm {
861: alarm(0);
862: $alarm_depth=0;
863: }
864:
865: sub start_alarm {
866: if ($alarm_depth<1) {
867: my $old=alarm($Apache::lonnet::perlvar{'lonScriptTimeout'});
868: if ($old) {
869: &Apache::lonxml::error("Cancelled an alarm of $old, this shouldn't occur.");
870: }
871: }
872: $alarm_depth++;
873: }
874:
875: sub end_alarm {
876: $alarm_depth--;
877: if ($alarm_depth<1) { alarm(0); }
878: }
879: }
1.328 albertel 880: my $metamode_was;
1.55 albertel 881: sub startredirection {
1.328 albertel 882: if (!$Apache::lonxml::redirection) {
883: $metamode_was=$Apache::lonxml::metamode;
884: }
885: $Apache::lonxml::metamode=0;
886: $Apache::lonxml::redirection++;
887: push (@Apache::lonxml::outputstack, '');
1.55 albertel 888: }
889:
890: sub endredirection {
1.328 albertel 891: if (!$Apache::lonxml::redirection) {
1.380 www 892: &Apache::lonxml::error("Endredirection was called before a startredirection, perhaps you have unbalanced tags. Some debugging information:".join ":",caller);
1.328 albertel 893: return '';
894: }
895: $Apache::lonxml::redirection--;
896: if (!$Apache::lonxml::redirection) {
897: $Apache::lonxml::metamode=$metamode_was;
898: }
899: pop @Apache::lonxml::outputstack;
1.97 albertel 900: }
901:
902: sub end_tag {
903: my ($tagstack,$parstack,$token)=@_;
904: pop(@$tagstack);
905: pop(@$parstack);
906: &decreasedepth($token);
1.55 albertel 907: }
908:
1.17 albertel 909: sub initdepth {
910: @Apache::lonxml::depthcounter=();
1.439 albertel 911: undef($Apache::lonxml::last_depth_count);
1.17 albertel 912: }
913:
1.438 albertel 914:
1.339 albertel 915: my @timers;
916: my $lasttime;
1.438 albertel 917: # @Apache::lonxml::depthcounter -> count of tags that exist so
918: # far at each level
1.439 albertel 919: # $Apache::lonxml::last_depth_count -> when ascending, need to
920: # remember the count for the level below the current level (for
921: # example going from 1_2 -> 1 -> 1_3 need to remember the 2 )
1.438 albertel 922:
1.17 albertel 923: sub increasedepth {
1.19 albertel 924: my ($token) = @_;
1.439 albertel 925: push(@Apache::lonxml::depthcounter,$Apache::lonxml::last_depth_count+1);
926: undef($Apache::lonxml::last_depth_count);
1.340 albertel 927: my $time;
928: if ($Apache::lonxml::debug eq "1") {
929: push(@timers,[&gettimeofday()]);
930: $time=&tv_interval($lasttime);
931: $lasttime=[&gettimeofday()];
932: }
1.439 albertel 933: my $spacing=' 'x($#Apache::lonxml::depthcounter);
1.438 albertel 934: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.439 albertel 935: # &Apache::lonxml::debug("s$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $Apache::lonxml::curdepth : $token->[1] : $time");
1.54 albertel 936: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 937: }
938:
939: sub decreasedepth {
1.19 albertel 940: my ($token) = @_;
1.439 albertel 941: if ( $#Apache::lonxml::depthcounter == -1) {
942: &Apache::lonxml::warning(&mt("Missing tags, unable to properly run file."));
1.43 albertel 943: }
1.439 albertel 944: $Apache::lonxml::last_depth_count = pop(@Apache::lonxml::depthcounter);
945:
1.340 albertel 946: my ($timer,$time);
947: if ($Apache::lonxml::debug eq "1") {
948: $timer=pop(@timers);
949: $time=&tv_interval($lasttime);
950: $lasttime=[&gettimeofday()];
951: }
1.439 albertel 952: my $spacing=' 'x($#Apache::lonxml::depthcounter);
953: $Apache::lonxml::curdepth = join('_',@Apache::lonxml::depthcounter);
954: # &Apache::lonxml::debug("e$spacing$Apache::lonxml::depth : $Apache::lonxml::olddepth : $Apache::lonxml::curdepth : $token->[1] : $time : ".&tv_interval($timer));
1.54 albertel 955: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 956: }
1.19 albertel 957:
1.399 albertel 958: sub get_id {
959: my ($parstack,$safeeval)=@_;
960: my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
961: if ($env{'request.state'} eq 'construct' && $id =~ /(\.|_)/) {
962: &error(&mt("IDs are not allowed to contain "<tt>_</tt>" or "<tt>.</tt>""));
963: }
964: if ($id =~ /^\s*$/) { $id = $Apache::lonxml::curdepth; }
965: return $id;
966: }
967:
1.180 albertel 968: sub get_all_text_unbalanced {
1.190 albertel 969: #there is a copy of this in lonpublisher.pm
1.326 albertel 970: my($tag,$pars)= @_;
971: my $token;
972: my $result='';
973: $tag='<'.$tag.'>';
974: while ($token = $$pars[-1]->get_token) {
975: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.386 albertel 976: if ($token->[0] eq 'T' && $token->[2]) {
1.382 albertel 977: $result.='<![CDATA['.$token->[1].']]>';
978: } else {
979: $result.=$token->[1];
980: }
1.326 albertel 981: } elsif ($token->[0] eq 'PI') {
982: $result.=$token->[2];
983: } elsif ($token->[0] eq 'S') {
984: $result.=$token->[4];
985: } elsif ($token->[0] eq 'E') {
986: $result.=$token->[2];
987: }
988: if ($result =~ /\Q$tag\E/is) {
989: ($result,my $redo)=$result =~ /(.*)\Q$tag\E(.*)/is;
990: #&Apache::lonxml::debug('Got a winner with leftovers ::'.$2);
991: #&Apache::lonxml::debug('Result is :'.$1);
992: $redo=$tag.$redo;
993: &Apache::lonxml::newparser($pars,\$redo);
994: last;
995: }
996: }
997: return $result
1.204 albertel 998: }
999:
1.447 ! albertel 1000: =pod
! 1001:
! 1002: For bubble grading mode and exam bubble printing mode, the tracking of
! 1003: the current 'bubble line number' is stored in the %env element
! 1004: 'form.counter', and is modifed and handled by the following routines.
! 1005:
! 1006: The value of it is stored in $Apache:lonxml::counter when live and
! 1007: stored back to env after done.
! 1008:
! 1009: =item &increment_counter($increment);
! 1010:
! 1011: Increments the internal counter environment variable a specified amount
! 1012:
! 1013: Optional Arguments:
! 1014: $increment - amount to increment by (defaults to 1)
! 1015:
! 1016: =cut
! 1017:
1.204 albertel 1018: sub increment_counter {
1.247 albertel 1019: my ($increment) = @_;
1.289 sakharuk 1020: if (defined($increment) && $increment gt 0) {
1021: $Apache::lonxml::counter+=$increment;
1022: } else {
1023: $Apache::lonxml::counter++;
1.247 albertel 1024: }
1.289 sakharuk 1025: $Apache::lonxml::counter_changed=1;
1.204 albertel 1026: }
1027:
1.447 ! albertel 1028: =pod
! 1029:
! 1030: =item &init_counter($increment);
! 1031:
! 1032: Initialize the internal counter environment variable
! 1033:
! 1034: =cut
! 1035:
1.204 albertel 1036: sub init_counter {
1.391 albertel 1037: if ($env{'request.state'} eq 'construct') {
1038: $Apache::lonxml::counter=1;
1039: $Apache::lonxml::counter_changed=1;
1040: } elsif (defined($env{'form.counter'})) {
1.372 albertel 1041: $Apache::lonxml::counter=$env{'form.counter'};
1.247 albertel 1042: $Apache::lonxml::counter_changed=0;
1.237 sakharuk 1043: } else {
1.204 albertel 1044: $Apache::lonxml::counter=1;
1.247 albertel 1045: $Apache::lonxml::counter_changed=1;
1.204 albertel 1046: }
1047: }
1048:
1049: sub store_counter {
1050: &Apache::lonnet::appenv(('form.counter' => $Apache::lonxml::counter));
1.401 albertel 1051: $Apache::lonxml::counter_changed=0;
1.204 albertel 1052: return '';
1.180 albertel 1053: }
1054:
1.398 albertel 1055: {
1056: my $state;
1057: sub clear_problem_counter {
1058: undef($state);
1059: &Apache::lonnet::delenv('form.counter');
1060: &Apache::lonxml::init_counter();
1061: &Apache::lonxml::store_counter();
1062: }
1063:
1064: sub remember_problem_counter {
1.422 albertel 1065: &Apache::lonnet::transfer_profile_to_env(undef,undef,1);
1.398 albertel 1066: $state = $env{'form.counter'};
1067: }
1068:
1069: sub restore_problem_counter {
1070: if (defined($state)) {
1071: &Apache::lonnet::appenv(('form.counter' => $state));
1072: }
1073: }
1.401 albertel 1074: sub get_problem_counter {
1075: if ($Apache::lonxml::counter_changed) { &store_counter() }
1.422 albertel 1076: &Apache::lonnet::transfer_profile_to_env(undef,undef,1);
1.401 albertel 1077: return $env{'form.counter'};
1078: }
1.398 albertel 1079: }
1080:
1.19 albertel 1081: sub get_all_text {
1.270 albertel 1082: my($tag,$pars,$style)= @_;
1083: my $gotfullstack=1;
1084: if (ref($pars) ne 'ARRAY') {
1085: $gotfullstack=0;
1086: $pars=[$pars];
1087: }
1088: if (ref($style) ne 'HASH') {
1089: $style={};
1090: }
1091: my $depth=0;
1092: my $token;
1093: my $result='';
1094: if ( $tag =~ m:^/: ) {
1095: my $tag=substr($tag,1);
1096: #&Apache::lonxml::debug("have:$tag:");
1097: my $top_empty=0;
1098: while (($depth >=0) && ($#$pars > -1) && (!$top_empty)) {
1099: while (($depth >=0) && ($token = $$pars[-1]->get_token)) {
1100: #&Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]:".$#$pars.":".$#Apache::lonxml::pwd);
1101: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
1.382 albertel 1102: if ($token->[2]) {
1103: $result.='<![CDATA['.$token->[1].']]>';
1104: } else {
1105: $result.=$token->[1];
1106: }
1.270 albertel 1107: } elsif ($token->[0] eq 'PI') {
1108: $result.=$token->[2];
1109: } elsif ($token->[0] eq 'S') {
1.316 albertel 1110: if ($token->[1] =~ /^\Q$tag\E$/i) { $depth++; }
1111: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1112: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270 albertel 1113: $result.=$token->[4];
1114: } elsif ($token->[0] eq 'E') {
1.316 albertel 1115: if ( $token->[1] =~ /^\Q$tag\E$/i) { $depth--; }
1.270 albertel 1116: #skip sending back the last end tag
1.283 albertel 1117: if ($depth == 0 && exists($$style{'/'.$token->[1]}) && $Apache::lonxml::usestyle) {
1.270 albertel 1118: my $string=
1119: '<LONCAPA_INTERNAL_TURN_STYLE_OFF end="yes" />'.
1120: $$style{'/'.$token->[1]}.
1121: $token->[2].
1122: '<LONCAPA_INTERNAL_TURN_STYLE_ON />';
1123: &Apache::lonxml::newparser($pars,\$string);
1124: #&Apache::lonxml::debug("reParsing $string");
1125: next;
1126: }
1127: if ($depth > -1) {
1128: $result.=$token->[2];
1129: } else {
1130: $$pars[-1]->unget_token($token);
1131: }
1132: }
1133: }
1134: if (($depth >=0) && ($#$pars == 0) ) { $top_empty=1; }
1135: if (($depth >=0) && ($#$pars > 0) ) {
1136: pop(@$pars);
1137: pop(@Apache::lonxml::pwd);
1138: }
1139: }
1140: if ($top_empty && $depth >= 0) {
1141: #never found the end tag ran out of text, throw error send back blank
1142: &error('Never found end tag for <'.$tag.
1143: '> current string <pre>'.
1.314 albertel 1144: &HTML::Entities::encode($result,'<>&"').
1.270 albertel 1145: '</pre>');
1146: if ($gotfullstack) {
1147: my $newstring='</'.$tag.'>'.$result;
1148: &Apache::lonxml::newparser($pars,\$newstring);
1149: }
1150: $result='';
1151: }
1152: } else {
1153: while ($#$pars > -1) {
1154: while ($token = $$pars[-1]->get_token) {
1155: #&Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
1156: if (($token->[0] eq 'T')||($token->[0] eq 'C')||
1157: ($token->[0] eq 'D')) {
1.382 albertel 1158: if ($token->[2]) {
1159: $result.='<![CDATA['.$token->[1].']]>';
1160: } else {
1161: $result.=$token->[1];
1162: }
1.270 albertel 1163: } elsif ($token->[0] eq 'PI') {
1164: $result.=$token->[2];
1165: } elsif ($token->[0] eq 'S') {
1.316 albertel 1166: if ( $token->[1] =~ /^\Q$tag\E$/i) {
1.270 albertel 1167: $$pars[-1]->unget_token($token); last;
1168: } else {
1169: $result.=$token->[4];
1170: }
1.316 albertel 1171: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_ON$/) { $Apache::lonxml::usestyle=1; }
1172: if ($token->[1] =~ /^LONCAPA_INTERNAL_TURN_STYLE_OFF$/) { $Apache::lonxml::usestyle=0; }
1.270 albertel 1173: } elsif ($token->[0] eq 'E') {
1174: $result.=$token->[2];
1175: }
1176: }
1177: if (($#$pars > 0) ) {
1178: pop(@$pars);
1179: pop(@Apache::lonxml::pwd);
1180: } else { last; }
1181: }
1182: }
1183: #&Apache::lonxml::debug("Exit:$result:");
1184: return $result
1.19 albertel 1185: }
1186:
1.23 albertel 1187: sub newparser {
1188: my ($parser,$contentref,$dir) = @_;
1.167 albertel 1189: push (@$parser,HTML::LCParser->new($contentref));
1.365 albertel 1190: $$parser[-1]->xml_mode(1);
1191: $$parser[-1]->marked_sections(1);
1.23 albertel 1192: if ( $dir eq '' ) {
1193: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
1194: } else {
1195: push (@Apache::lonxml::pwd, $dir);
1196: }
1197: }
1.1 sakharuk 1198:
1.8 albertel 1199: sub parstring {
1.417 albertel 1200: my ($token) = @_;
1201: my (@vars,@values);
1202: foreach my $attr (@{$token->[3]}) {
1203: if ($attr!~/\W/) {
1204: my $val=$token->[2]->{$attr};
1205: $val =~ s/([\%\@\\\"\'])/\\$1/g;
1206: $val =~ s/(\$[^\{a-zA-Z_])/\\$1/g;
1207: $val =~ s/(\$)$/\\$1/;
1208: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1209: push(@vars,"\$$attr");
1210: push(@values,"\"$val\"");
1211: }
1212: }
1213: my $var_init =
1214: (@vars) ? 'my ('.join(',',@vars).') = ('.join(',',@values).');'
1215: : '';
1216: return $var_init;
1.8 albertel 1217: }
1.22 albertel 1218:
1.384 albertel 1219: sub extlink {
1220: my ($res,$exact)=@_;
1221: if (!$exact) {
1222: $res=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$res);
1223: }
1224: push(@Apache::lonxml::extlinks,$res)
1225: }
1226:
1.34 www 1227: sub writeallows {
1.126 www 1228: unless ($#extlinks>=0) { return; }
1.377 albertel 1229: my $thisurl = &Apache::lonnet::clutter(shift);
1.372 albertel 1230: if ($env{'httpref.'.$thisurl}) {
1231: $thisurl=$env{'httpref.'.$thisurl};
1.111 www 1232: }
1.34 www 1233: my $thisdir=$thisurl;
1234: $thisdir=~s/\/[^\/]+$//;
1235: my %httpref=();
1.142 albertel 1236: foreach (@extlinks) {
1.34 www 1237: $httpref{'httpref.'.
1.444 albertel 1238: &Apache::lonnet::hreflocation($thisdir,&unescape($_))}=$thisurl;
1.142 albertel 1239: }
1.126 www 1240: @extlinks=();
1.34 www 1241: &Apache::lonnet::appenv(%httpref);
1242: }
1243:
1.281 albertel 1244: sub register_ssi {
1245: my ($url,%form)=@_;
1246: push (@Apache::lonxml::ssi_info,{'url'=>$url,'form'=>\%form});
1247: return '';
1248: }
1249:
1250: sub do_registered_ssi {
1251: foreach my $info (@Apache::lonxml::ssi_info) {
1252: my %form=%{ $info->{'form'}};
1253: my $url=$info->{'url'};
1254: &Apache::lonnet::ssi($url,%form);
1255: }
1256: }
1.66 www 1257: #
1258: # Afterburner handles anchors, highlights and links
1259: #
1260: sub afterburn {
1261: my $result=shift;
1.154 albertel 1262: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1263: ['highlight','anchor','link']);
1.372 albertel 1264: if ($env{'form.highlight'}) {
1265: foreach (split(/\,/,$env{'form.highlight'})) {
1.66 www 1266: my $anchorname=$_;
1267: my $matchthis=$anchorname;
1268: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1269: $result=~s/(\Q$matchthis\E)/\<font color=\"red\"\>$1\<\/font\>/gs;
1.142 albertel 1270: }
1.66 www 1271: }
1.372 albertel 1272: if ($env{'form.link'}) {
1273: foreach (split(/\,/,$env{'form.link'})) {
1.66 www 1274: my ($anchorname,$linkurl)=split(/\>/,$_);
1275: my $matchthis=$anchorname;
1276: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1277: $result=~s/(\Q$matchthis\E)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
1.142 albertel 1278: }
1.66 www 1279: }
1.372 albertel 1280: if ($env{'form.anchor'}) {
1281: my $anchorname=$env{'form.anchor'};
1.66 www 1282: my $matchthis=$anchorname;
1283: $matchthis=~s/\_+/\\s\+/g;
1.317 albertel 1284: $result=~s/(\Q$matchthis\E)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
1.66 www 1285: $result.=(<<"ENDSCRIPT");
1.226 albertel 1286: <script type="text/javascript">
1.66 www 1287: document.location.hash='$anchorname';
1288: </script>
1289: ENDSCRIPT
1290: }
1291: return $result;
1292: }
1293:
1.79 www 1294: sub storefile {
1295: my ($file,$contents)=@_;
1.290 albertel 1296: &Apache::lonnet::correct_line_ends(\$contents);
1.79 www 1297: if (my $fh=Apache::File->new('>'.$file)) {
1298: print $fh $contents;
1299: $fh->close();
1.271 www 1300: return 1;
1.147 albertel 1301: } else {
1.271 www 1302: &warning("Unable to save file $file");
1303: return 0;
1.79 www 1304: }
1305: }
1306:
1.151 albertel 1307: sub createnewhtml {
1.321 www 1308: my $title=&mt('Title of document goes here');
1309: my $body=&mt('Body of document goes here');
1310: my $filecontents=(<<SIMPLECONTENT);
1.78 www 1311: <html>
1312: <head>
1.321 www 1313: <title>$title</title>
1.78 www 1314: </head>
1315: <body bgcolor="#FFFFFF">
1.321 www 1316: $body
1.78 www 1317: </body>
1318: </html>
1319: SIMPLECONTENT
1.321 www 1320: return $filecontents;
1.151 albertel 1321: }
1322:
1.274 albertel 1323: sub createnewsty {
1324: my $filecontents=(<<SIMPLECONTENT);
1325: <definetag name="">
1326: <render>
1327: <web></web>
1328: <tex></tex>
1329: </render>
1330: </definetag>
1331: SIMPLECONTENT
1332: return $filecontents;
1333: }
1334:
1.147 albertel 1335:
1.151 albertel 1336: sub inserteditinfo {
1.274 albertel 1337: my ($result,$filecontents,$filetype)=@_;
1.314 albertel 1338: $filecontents = &HTML::Entities::encode($filecontents,'<>&"');
1.147 albertel 1339: # my $editheader='<a href="#editsection">Edit below</a><hr />';
1.274 albertel 1340: my $xml_help = '';
1.321 www 1341: my $initialize='';
1.274 albertel 1342: if ($filetype eq 'html') {
1.323 www 1343: my $addbuttons=&Apache::lonhtmlcommon::htmlareaaddbuttons();
1.410 albertel 1344: $initialize=&Apache::lonhtmlcommon::spellheader();
1.347 albertel 1345: if (!&Apache::lonhtmlcommon::htmlareablocked() &&
1346: &Apache::lonhtmlcommon::htmlareabrowser()) {
1347: $initialize.=(<<FULLPAGE);
1.321 www 1348: <script type="text/javascript">
1.323 www 1349: $addbuttons
1350:
1.321 www 1351: HTMLArea.loadPlugin("FullPage");
1352:
1353: function initDocument() {
1.323 www 1354: var editor=new HTMLArea("filecont",config);
1.321 www 1355: editor.registerPlugin(FullPage);
1356: editor.generate();
1357: }
1358: </script>
1359: FULLPAGE
1.347 albertel 1360: } else {
1361: $initialize.=(<<FULLPAGE);
1362: <script type="text/javascript">
1363: $addbuttons
1364: function initDocument() {
1365: }
1366: </script>
1367: FULLPAGE
1368: }
1.321 www 1369: $result=~s/\<body([^\>]*)\>/\<body onload="initDocument()" $1\>/i;
1370: $xml_help=&Apache::loncommon::helpLatexCheatsheet();
1.274 albertel 1371: }
1372: my $cleanbut = '';
1.374 www 1373:
1.254 albertel 1374: my $titledisplay=&display_title();
1.426 banghart 1375: my %lt=&Apache::lonlocal::texthash('st' => 'Save and Edit',
1376: 'vi' => 'Save and View',
1.427 banghart 1377: 'dv' => 'Discard Edits and View',
1.428 banghart 1378: 'un' => 'undo',
1.280 www 1379: 'ed' => 'Edit');
1.161 albertel 1380: my $buttons=(<<BUTTONS);
1.274 albertel 1381: $cleanbut
1.428 banghart 1382: <input type="submit" name="discardview" accesskey="d" value="$lt{'dv'}" />
1383: <input type="submit" name="Undo" accesskey="u" value="$lt{'un'}" /><hr>
1.304 matthew 1384: <input type="submit" name="savethisfile" accesskey="s" value="$lt{'st'}" />
1385: <input type="submit" name="viewmode" accesskey="v" value="$lt{'vi'}" />
1.161 albertel 1386: BUTTONS
1.333 www 1387: $buttons.=&Apache::lonhtmlcommon::spelllink('xmledit','filecont');
1.78 www 1388: my $editfooter=(<<ENDFOOTER);
1.321 www 1389: $initialize
1.78 www 1390: <hr />
1391: <a name="editsection" />
1.333 www 1392: <form method="post" name="xmledit">
1.240 albertel 1393: $xml_help
1.280 www 1394: <input type="hidden" name="editmode" value="$lt{'ed'}" />
1.170 www 1395: $buttons<br />
1.366 albertel 1396: <textarea style="width:100%" cols="80" rows="44" name="filecont" id="filecont">$filecontents</textarea>
1.170 www 1397: <br />$buttons
1.78 www 1398: <br />
1399: </form>
1.254 albertel 1400: $titledisplay
1.321 www 1401: </body>
1.78 www 1402: ENDFOOTER
1.147 albertel 1403: # $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
1.78 www 1404: $result=~s/(\<\/body\>)/$editfooter/is;
1405: return $result;
1406: }
1407:
1.152 albertel 1408: sub get_target {
1.372 albertel 1409: my $viewgrades=&Apache::lonnet::allowed('vgr',$env{'request.course.id'});
1410: if ( $env{'request.state'} eq 'published') {
1411: if ( defined($env{'form.grade_target'})
1.152 albertel 1412: && ($viewgrades == 'F' )) {
1.372 albertel 1413: return ($env{'form.grade_target'});
1414: } elsif (defined($env{'form.grade_target'})) {
1415: if (($env{'form.grade_target'} eq 'web') ||
1416: ($env{'form.grade_target'} eq 'tex') ) {
1417: return $env{'form.grade_target'}
1.153 albertel 1418: } else {
1419: return 'web';
1420: }
1.152 albertel 1421: } else {
1422: return 'web';
1423: }
1.372 albertel 1424: } elsif ($env{'request.state'} eq 'construct') {
1425: if ( defined($env{'form.grade_target'})) {
1426: return ($env{'form.grade_target'});
1.152 albertel 1427: } else {
1428: return 'web';
1429: }
1430: } else {
1431: return 'web';
1432: }
1433: }
1434:
1.24 sakharuk 1435: sub handler {
1.255 sakharuk 1436: my $request=shift;
1437:
1438: my $target=&get_target();
1439:
1.372 albertel 1440: $Apache::lonxml::debug=$env{'user.debug'};
1.255 sakharuk 1441:
1.364 albertel 1442: &Apache::loncommon::content_type($request,'text/html');
1.255 sakharuk 1443: &Apache::loncommon::no_cache($request);
1.372 albertel 1444: if ($env{'request.state'} eq 'published') {
1.363 albertel 1445: $request->set_last_modified(&Apache::lonnet::metadata($request->uri,
1446: 'lastrevisiondate'));
1447: }
1.255 sakharuk 1448: $request->send_http_header;
1449:
1450: return OK if $request->header_only;
1.68 www 1451:
1452:
1.255 sakharuk 1453: my $file=&Apache::lonnet::filelocation("",$request->uri);
1.274 albertel 1454: my $filetype;
1455: if ($file =~ /\.sty$/) {
1456: $filetype='sty';
1457: } else {
1458: $filetype='html';
1459: }
1.78 www 1460: #
1461: # Edit action? Save file.
1462: #
1.428 banghart 1463: if (!($env{'request.state'} eq 'published')) {
1464: if ($env{'form.savethisfile'} || $env{'form.viewmode'} || $env{'form.Undo'}) {
1.429 albertel 1465: my $html_file=&Apache::lonnet::getfile($file);
1466: my $error = &Apache::lonhomework::handle_save_or_undo($request, \$html_file, \$env{'form.filecont'});
1.255 sakharuk 1467: }
1468: }
1469: my %mystyle;
1470: my $result = '';
1471: my $filecontents=&Apache::lonnet::getfile($file);
1472: if ($filecontents eq -1) {
1.402 albertel 1473: my $start_page=&Apache::loncommon::start_page('File Error');
1.412 albertel 1474: my $end_page=&Apache::loncommon::end_page();
1.284 www 1475: my $fnf=&mt('File not found');
1.255 sakharuk 1476: $result=(<<ENDNOTFOUND);
1.402 albertel 1477: $start_page
1.284 www 1478: <b>$fnf: $file</b>
1.402 albertel 1479: $end_page
1.78 www 1480: ENDNOTFOUND
1.343 albertel 1481: $filecontents='';
1.372 albertel 1482: if ($env{'request.state'} ne 'published') {
1.274 albertel 1483: if ($filetype eq 'sty') {
1484: $filecontents=&createnewsty();
1485: } else {
1486: $filecontents=&createnewhtml();
1487: }
1.372 albertel 1488: $env{'form.editmode'}='Edit'; #force edit mode
1.255 sakharuk 1489: }
1490: } else {
1.372 albertel 1491: unless ($env{'request.state'} eq 'published') {
1.343 albertel 1492: if ($filecontents=~/BEGIN LON-CAPA Internal/) {
1.381 www 1493: &Apache::lonxml::error(&mt('This file appears to be a rendering of a LON-CAPA resource. If this is correct, this resource will act very oddly and incorrectly.'));
1.343 albertel 1494: }
1.264 www 1495: #
1496: # we are in construction space, see if edit mode forced
1.385 albertel 1497: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1498: ['editmode']);
1.255 sakharuk 1499: }
1.427 banghart 1500: if (!$env{'form.editmode'} || $env{'form.viewmode'} || $env{'form.discardview'}) {
1.255 sakharuk 1501: $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
1502: '',%mystyle);
1.368 albertel 1503: undef($Apache::lonhomework::parsing_a_task);
1.385 albertel 1504: &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1505: ['rawmode']);
1.395 albertel 1506: if ($env{'form.rawmode'}) { $result = $filecontents; }
1.255 sakharuk 1507: }
1.147 albertel 1508: }
1.255 sakharuk 1509:
1.78 www 1510: #
1511: # Edit action? Insert editing commands
1512: #
1.372 albertel 1513: unless ($env{'request.state'} eq 'published') {
1.427 banghart 1514: if ($env{'form.editmode'} && (!($env{'form.viewmode'})) && (!($env{'form.discardview'})))
1515: {
1.255 sakharuk 1516: my $displayfile=$request->uri;
1517: $displayfile=~s/^\/[^\/]*//;
1.402 albertel 1518: my %options = ();
1519: if ($env{'environment.remote'} ne 'off') {
1520: $options{'bgcolor'} = '#FFFFFF';
1.349 albertel 1521: }
1.402 albertel 1522: my $start_page = &Apache::loncommon::start_page(undef,undef,
1523: \%options);
1524: $result=$start_page.
1.309 albertel 1525: &Apache::lonxml::message_location().'<h3>'.
1526: $displayfile.
1.402 albertel 1527: '</h3>'.&Apache::loncommon::end_page();
1.274 albertel 1528: $result=&inserteditinfo($result,$filecontents,$filetype);
1.255 sakharuk 1529: }
1.147 albertel 1530: }
1.402 albertel 1531: if ($filetype eq 'html') { &writeallows($request->uri); }
1.274 albertel 1532:
1.255 sakharuk 1533:
1.309 albertel 1534: &Apache::lonxml::add_messages(\$result);
1.255 sakharuk 1535: $request->print($result);
1536:
1537: return OK;
1.253 albertel 1538: }
1539:
1540: sub display_title {
1541: my $result;
1.372 albertel 1542: if ($env{'request.state'} eq 'construct') {
1.253 albertel 1543: my $title=&Apache::lonnet::gettitle();
1544: if (!defined($title) || $title eq '') {
1.372 albertel 1545: $title = $env{'request.filename'};
1.253 albertel 1546: $title = substr($title, rindex($title, '/') + 1);
1547: }
1548: $result = "<script type='text/javascript'>top.document.title = '$title - LON-CAPA Construction Space';</script>";
1549: }
1550: return $result;
1.24 sakharuk 1551: }
1.147 albertel 1552:
1.22 albertel 1553: sub debug {
1.298 albertel 1554: if ($Apache::lonxml::debug eq "1") {
1555: $|=1;
1.300 albertel 1556: my $request=$Apache::lonxml::request;
1.388 albertel 1557: if (!$request) {
1558: eval { $request=Apache->request; };
1559: }
1560: if (!$request) {
1561: eval { $request=Apache2::RequestUtil->request; };
1562: }
1.314 albertel 1563: $request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
1.346 albertel 1564: #&Apache::lonnet::logthis($_[0]);
1.298 albertel 1565: }
1.22 albertel 1566: }
1.49 albertel 1567:
1.348 albertel 1568: sub show_error_warn_msg {
1.372 albertel 1569: if ($env{'request.filename'} eq '/home/httpd/html/res/lib/templates/simpleproblem.problem' &&
1570: &Apache::lonnet::allowed('mdc',$env{'request.course.id'})) {
1.351 albertel 1571: return 1;
1572: }
1.348 albertel 1573: return (($Apache::lonxml::debug eq 1) ||
1.372 albertel 1574: ($env{'request.state'} eq 'construct') ||
1.348 albertel 1575: ($Apache::lonhomework::browse eq 'F'
1576: &&
1.372 albertel 1577: $env{'form.show_errors'} eq 'on'));
1.348 albertel 1578: }
1579:
1.22 albertel 1580: sub error {
1.336 albertel 1581: $errorcount++;
1.348 albertel 1582: if ( &show_error_warn_msg() ) {
1.336 albertel 1583: # If printing in construction space, put the error inside <pre></pre>
1584: push(@Apache::lonxml::error_messages,
1585: $Apache::lonxml::warnings_error_header.
1586: "<b>ERROR:</b>".join("<br />\n",@_)."<br />\n");
1587: $Apache::lonxml::warnings_error_header='';
1588: } else {
1589: my $errormsg;
1590: my ($symb)=&Apache::lonnet::symbread();
1591: if ( !$symb ) {
1592: #public or browsers
1593: $errormsg=&mt("An error occured while processing this resource. The author has been notified.");
1.403 albertel 1594: }
1.413 albertel 1595: my $host=$Apache::lonnet::perlvar{'lonHostID'};
1.416 albertel 1596: my $msg = join('<br />',(@_,"The error occurred on host <tt>$host</tt>"));
1.336 albertel 1597: #notify author
1.403 albertel 1598: &Apache::lonmsg::author_res_msg($env{'request.filename'},$msg);
1.336 albertel 1599: #notify course
1.372 albertel 1600: if ( $symb && $env{'request.course.id'} ) {
1.380 www 1601: my $cnum=$env{'course.'.$env{'request.course.id'}.'.num'};
1602: my $cdom=$env{'course.'.$env{'request.course.id'}.'.domain'};
1.440 albertel 1603: my (undef,%users)=&Apache::lonmsg::decide_receiver(undef,0,1,1,1);
1.372 albertel 1604: my $declutter=&Apache::lonnet::declutter($env{'request.filename'});
1.435 raeburn 1605: my $baseurl = &Apache::lonnet::clutter($declutter);
1.336 albertel 1606: my @userlist;
1607: foreach (keys %users) {
1608: my ($user,$domain) = split(/:/, $_);
1609: push(@userlist,"$user\@$domain");
1.380 www 1610: my $key=$declutter.'_'.$user.'_'.$domain;
1611: my %lastnotified=&Apache::lonnet::get('nohist_xmlerrornotifications',
1612: [$key],
1613: $cdom,$cnum);
1614: my $now=time;
1615: if ($now-$lastnotified{$key}>86400) {
1.434 raeburn 1616: my $title = &Apache::lonnet::gettitle($symb);
1617: my $sentmessage;
1.380 www 1618: &Apache::lonmsg::user_normal_msg($user,$domain,
1.435 raeburn 1619: "Error [$title]",$msg,'',$baseurl,'','',
1.434 raeburn 1620: \$sentmessage,$symb,$title,1);
1.380 www 1621: &Apache::lonnet::put('nohist_xmlerrornotifications',
1622: {$key => $now},
1623: $cdom,$cnum);
1624: }
1.336 albertel 1625: }
1.372 albertel 1626: if ($env{'request.role.adv'}) {
1.336 albertel 1627: $errormsg=&mt("An error occured while processing this resource. The course personnel ([_1]) and the author have been notified.",join(', ',@userlist));
1628: } else {
1629: $errormsg=&mt("An error occured while processing this resource. The instructor has been notified.");
1630: }
1631: }
1632: push(@Apache::lonxml::error_messages,"<b>$errormsg</b> <br />");
1.52 albertel 1633: }
1.22 albertel 1634: }
1.49 albertel 1635:
1.22 albertel 1636: sub warning {
1.295 albertel 1637: $warningcount++;
1.261 albertel 1638:
1.372 albertel 1639: if ($env{'form.grade_target'} ne 'tex') {
1.348 albertel 1640: if ( &show_error_warn_msg() ) {
1.309 albertel 1641: push(@Apache::lonxml::warning_messages,
1642: $Apache::lonxml::warnings_error_header.
1643: "<b>W</b>ARNING<b>:</b>".join('<br />',@_)."<br />\n");
1.295 albertel 1644: $Apache::lonxml::warnings_error_header='';
1645: }
1646: }
1.309 albertel 1647: }
1648:
1649: sub info {
1.372 albertel 1650: if ($env{'form.grade_target'} ne 'tex'
1651: && $env{'request.state'} eq 'construct') {
1.309 albertel 1652: push(@Apache::lonxml::info_messages,join('<br />',@_)."<br />\n");
1653: }
1654: }
1655:
1656: sub message_location {
1657: return '__LONCAPA_INTERNAL_MESSAGE_LOCATION__';
1658: }
1659:
1660: sub add_messages {
1661: my ($msg)=@_;
1662: my $result=join(' ',
1663: @Apache::lonxml::info_messages,
1664: @Apache::lonxml::error_messages,
1665: @Apache::lonxml::warning_messages);
1666: undef(@Apache::lonxml::info_messages);
1667: undef(@Apache::lonxml::error_messages);
1668: undef(@Apache::lonxml::warning_messages);
1669: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__/$result/;
1670: $$msg=~s/__LONCAPA_INTERNAL_MESSAGE_LOCATION__//g;
1.83 albertel 1671: }
1672:
1673: sub get_param {
1.213 albertel 1674: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1675: if ( ! $context ) { $context = -1; }
1676: my $args ='';
1677: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297 sakharuk 1678: if ( ! $Apache::lonxml::usestyle ) {
1679: $args=$Apache::lonxml::style_values.$args;
1680: }
1.213 albertel 1681: if ( ! $args ) { return undef; }
1682: if ( $case_insensitive ) {
1.417 albertel 1683: if ($args =~ s/(my (?:.*))(\$\Q$param\E[,\)])/$1.lc($2)/ei) {
1.213 albertel 1684: return &Apache::run::run("{$args;".'return $'.$param.'}',
1685: $safeeval); #'
1686: } else {
1687: return undef;
1688: }
1689: } else {
1.417 albertel 1690: if ( $args =~ /my .*\$\Q$param\E[,\)]/ ) {
1.213 albertel 1691: return &Apache::run::run("{$args;".'return $'.$param.'}',
1692: $safeeval); #'
1693: } else {
1694: return undef;
1695: }
1696: }
1.22 albertel 1697: }
1698:
1.132 albertel 1699: sub get_param_var {
1.213 albertel 1700: my ($param,$parstack,$safeeval,$context,$case_insensitive) = @_;
1.132 albertel 1701: if ( ! $context ) { $context = -1; }
1702: my $args ='';
1703: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
1.297 sakharuk 1704: if ( ! $Apache::lonxml::usestyle ) {
1705: $args=$Apache::lonxml::style_values.$args;
1706: }
1.230 albertel 1707: &Apache::lonxml::debug("Args are $args param is $param");
1.213 albertel 1708: if ($case_insensitive) {
1.419 albertel 1709: if (! ($args=~s/(my (?:.*))(\$\Q$param\E[,\)])/$1.lc($2)/ei)) {
1.213 albertel 1710: return undef;
1711: }
1.419 albertel 1712: } elsif ( $args !~ /my .*\$\Q$param\E[,\)]/ ) { return undef; }
1.132 albertel 1713: my $value=&Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.230 albertel 1714: &Apache::lonxml::debug("first run is $value");
1.341 albertel 1715: if ($value =~ /^[\$\@\%][a-zA-Z_]\w*$/) {
1.230 albertel 1716: &Apache::lonxml::debug("doing second");
1717: my @result=&Apache::run::run("return $value",$safeeval,1);
1718: if (!defined($result[0])) {
1719: return $value
1720: } else {
1721: if (wantarray) { return @result; } else { return $result[0]; }
1722: }
1.132 albertel 1723: } else {
1724: return $value;
1725: }
1726: }
1727:
1.438 albertel 1728: sub register_insert_xml {
1729: my $parser = HTML::LCParser->new($Apache::lonnet::perlvar{'lonTabDir'}
1730: .'/insertlist.xml');
1731: my ($tagnum,$in_help)=(0,0);
1.442 albertel 1732: my @alltags;
1.438 albertel 1733: my $tag;
1734: while (my $token = $parser->get_token()) {
1735: if ($token->[0] eq 'S') {
1736: my $key;
1737: if ($token->[1] eq 'tag') {
1738: $tag = $token->[2]{'name'};
1739: $insertlist{"$tagnum.tag"} = $tag;
1740: $insertlist{"$tag.num"} = $tagnum;
1.442 albertel 1741: push(@alltags,$tag);
1.438 albertel 1742: } elsif ($in_help && $token->[1] eq 'file') {
1743: $key = $tag.'.helpfile';
1744: } elsif ($in_help && $token->[1] eq 'description') {
1745: $key = $tag.'.helpdesc';
1746: } elsif ($token->[1] eq 'description' ||
1747: $token->[1] eq 'color' ||
1748: $token->[1] eq 'show' ) {
1749: $key = $tag.'.'.$token->[1];
1750: } elsif ($token->[1] eq 'insert_sub') {
1751: $key = $tag.'.function';
1752: } elsif ($token->[1] eq 'help') {
1753: $in_help=1;
1754: } elsif ($token->[1] eq 'allow') {
1.442 albertel 1755: $key = $tag.'.allow';
1.438 albertel 1756: }
1757: if (defined($key)) {
1758: $insertlist{$key} = $parser->get_text();
1759: $insertlist{$key} =~ s/(^\s*|\s*$ )//gx;
1760: }
1761: } elsif ($token->[0] eq 'E') {
1762: if ($token->[1] eq 'tag') {
1763: undef($tag);
1764: $tagnum++;
1765: } elsif ($token->[1] eq 'help') {
1766: undef($in_help);
1767: }
1768: }
1769: }
1.442 albertel 1770:
1771: # parse the allows and ignore tags set to <show>no</show>
1772: foreach my $tag (@alltags) {
1.445 albertel 1773: next if (!exists($insertlist{"$tag.allow"}));
1.442 albertel 1774: my $allow = $insertlist{"$tag.allow"};
1775: foreach my $element (split(',',$allow)) {
1776: $element =~ s/(^\s*|\s*$ )//gx;
1.446 albertel 1777: if (!exists($insertlist{"$element.show"})
1778: || $insertlist{"$element.show"} ne 'no') {
1.442 albertel 1779: push(@{ $insertlist{$tag.'.which'} },$element);
1780: }
1781: }
1782: }
1.438 albertel 1783: }
1784:
1785: sub register_insert {
1786: return ®ister_insert_xml(@_);
1787: # &dump_insertlist('2');
1788: }
1789:
1790: sub dump_insertlist {
1791: my ($ext) = @_;
1792: open(XML,">/tmp/insertlist.xml.$ext");
1793: print XML ("<insertlist>");
1794: my $i=0;
1795:
1796: while (exists($insertlist{"$i.tag"})) {
1797: my $tag = $insertlist{"$i.tag"};
1798: print XML ("
1799: \t<tag name=\"$tag\">");
1800: if (defined($insertlist{"$tag.description"})) {
1801: print XML ("
1802: \t\t<description>".$insertlist{"$tag.description"}."</description>");
1803: }
1804: if (defined($insertlist{"$tag.color"})) {
1805: print XML ("
1806: \t\t<color>".$insertlist{"$tag.color"}."</color>");
1807: }
1808: if (defined($insertlist{"$tag.function"})) {
1809: print XML ("
1810: \t\t<insert_sub>".$insertlist{"$tag.function"}."</insert_sub>");
1811: }
1812: if (defined($insertlist{"$tag.show"})
1813: && $insertlist{"$tag.show"} ne 'yes') {
1814: print XML ("
1815: \t\t<show>".$insertlist{"$tag.show"}."</show>");
1816: }
1817: if (defined($insertlist{"$tag.helpfile"})) {
1818: print XML ("
1819: \t\t<help>
1820: \t\t\t<file>".$insertlist{"$tag.helpfile"}."</file>");
1821: if ($insertlist{"$tag.helpdesc"} ne '') {
1822: print XML ("
1823: \t\t\t<description>".$insertlist{"$tag.helpdesc"}."</description>");
1824: }
1825: print XML ("
1826: \t\t</help>");
1827: }
1828: if (defined($insertlist{"$tag.which"})) {
1829: print XML ("
1830: \t\t<allow>".join(',',sort(@{ $insertlist{"$tag.which"} }))."</allow>");
1831: }
1832: print XML ("
1833: \t</tag>");
1834: $i++;
1835: }
1836: print XML ("\n</insertlist>\n");
1837: close(XML);
1838: }
1839:
1.98 albertel 1840: sub description {
1.437 albertel 1841: my ($token)=@_;
1842: my $tag = &get_tag($token);
1843: return $insertlist{$tag.'.description'};
1.268 bowersj2 1844: }
1845:
1846: # Returns a list containing the help file, and the description
1847: sub helpinfo {
1.437 albertel 1848: my ($token)=@_;
1849: my $tag = &get_tag($token);
1850: return ($insertlist{$tag.'.helpfile'}, $insertlist{$tag.'.helpdesc'});
1851: }
1852:
1853: sub get_tag {
1854: my ($token)=@_;
1855: my $tagnum;
1856: my $tag=$token->[1];
1857: foreach my $namespace (reverse(@Apache::lonxml::namespace)) {
1858: my $testtag = $namespace.'::'.$tag;
1859: $tagnum = $insertlist{"$testtag.num"};
1860: last if (defined($tagnum));
1861: }
1862: if (!defined($tagnum)) {
1863: $tagnum = $Apache::lonxml::insertlist{"$tag.num"};
1864: }
1865: return $insertlist{"$tagnum.tag"};
1.98 albertel 1866: }
1.123 albertel 1867:
1.1 sakharuk 1868: 1;
1869: __END__
1.68 www 1870:
1871:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>