Annotation of loncom/xml/lonxml.pm, revision 1.96
1.2 sakharuk 1: # The LearningOnline Network with CAPA
1.3 sakharuk 2: # XML Parser Module
1.2 sakharuk 3: #
1.3 sakharuk 4: # last modified 06/26/00 by Alexander Sakharuk
1.33 www 5: # 11/6 Gerd Kortemeyer
1.45 www 6: # 6/1/1 Gerd Kortemeyer
1.56 albertel 7: # 2/21,3/13 Guy
1.68 www 8: # 3/29,5/4 Gerd Kortemeyer
1.73 harris41 9: # 5/10 Scott Harrison
1.78 www 10: # 5/26 Gerd Kortemeyer
1.80 harris41 11: # 5/27 H. K. Ng
1.89 www 12: # 6/2,6/3,6/8,6/9 Gerd Kortemeyer
1.93 ng 13: # 6/12,6/13 H. K. Ng
1.95 www 14: # 6/16 Gerd Kortemeyer
1.2 sakharuk 15:
1.4 albertel 16: package Apache::lonxml;
1.33 www 17: use vars
1.76 albertel 18: qw(@pwd @outputstack $redirection $import @extlinks $metamode $evaluate %insertlist @namespace);
1.1 sakharuk 19: use strict;
20: use HTML::TokeParser;
1.3 sakharuk 21: use Safe;
1.40 albertel 22: use Safe::Hole;
1.81 ng 23: use Math::Cephes qw(:trigs :hypers :bessels erf erfc);
1.91 ng 24: use Math::Random qw(:all);
1.13 albertel 25: use Opcode;
1.72 albertel 26:
27: sub register {
28: my $space;
29: my @taglist;
30: my $temptag;
31: ($space,@taglist) = @_;
32: foreach $temptag (@taglist) {
33: $Apache::lonxml::alltags{$temptag}=$space;
34: }
35: }
36:
1.46 www 37: use Apache::Constants qw(:common);
1.71 www 38: use Apache::lontexconvert;
1.72 albertel 39: use Apache::style;
40: use Apache::run;
41: use Apache::londefdef;
42: use Apache::scripttag;
43: use Apache::edit;
1.79 www 44: use Apache::lonnet;
45: use Apache::File;
46:
1.72 albertel 47: #================================================== Main subroutine: xmlparse
48: #debugging control, to turn on debugging modify the correct handler
49: $Apache::lonxml::debug=0;
50:
51: #path to the directory containing the file currently being processed
52: @pwd=();
53:
54: #these two are used for capturing a subset of the output for later processing,
55: #don't touch them directly use &startredirection and &endredirection
56: @outputstack = ();
57: $redirection = 0;
58:
59: #controls wheter the <import> tag actually does
60: $import = 1;
61: @extlinks=();
62:
63: # meta mode is a bit weird only some output is to be turned off
64: #<output> tag turns metamode off (defined in londefdef.pm)
65: $metamode = 0;
66:
67: # turns on and of run::evaluate actually derefencing var refs
68: $evaluate = 1;
1.7 albertel 69:
1.74 albertel 70: # data structure for eidt mode, determines what tags can go into what other tags
71: %insertlist=();
1.68 www 72:
1.76 albertel 73: #stores the list of active tag namespaces
74: @namespace=();
75:
1.68 www 76: sub xmlbegin {
77: my $output='';
78: if ($ENV{'browser.mathml'}) {
79: $output='<?xml version="1.0"?>'
80: .'<?xml-stylesheet type="text/css" href="/adm/MathML/mathml.css"?>'
81: .'<!DOCTYPE html SYSTEM "/adm/MathML/mathml.dtd" '
82: .'[<!ENTITY mathns "http://www.w3.org/1998/Math/MathML">]>'
83: .'<html xmlns:math="http://www.w3.org/1998/Math/MathML" '
84: .'xmlns="http://www.w3.org/TR/REC-html40">';
85: } else {
86: $output='<html>';
87: }
88: return $output;
89: }
90:
91: sub xmlend {
92: return '</html>';
93: }
94:
1.70 www 95: sub fontsettings() {
96: my $headerstring='';
97: if (($ENV{'browser.os'} eq 'mac') && (!$ENV{'browser.mathml'})) {
98: $headerstring.=
99: '<meta Content-Type="text/html; charset=x-mac-roman">';
100: }
101: return $headerstring;
102: }
103:
1.68 www 104: sub registerurl {
1.86 www 105: if ($ENV{'REQUEST_URI'}!~/^\/(res\/)*adm\//) {
1.87 www 106: my $hwkadd='';
107: if ($ENV{'REQUEST_URI'}=~/\.(problem|exam|quiz|assess|survey|form)$/) {
108: if (&Apache::lonnet::allowed('vgr',$ENV{'request.course.id'})) {
109: $hwkadd.=(<<ENDSUBM);
110: menu.switchbutton
111: (7,1,'subm.gif','view sub','missions',
1.88 www 112: 'gocmd("/adm/grades","submission")');
1.87 www 113: ENDSUBM
114: }
115: if (&Apache::lonnet::allowed('mgr',$ENV{'request.course.id'})) {
116: $hwkadd.=(<<ENDGRDS);
117: menu.switchbutton
118: (7,2,'pgrd.gif','problem','grades',
1.88 www 119: 'gocmd("/adm/grades","viewgrades")');
1.87 www 120: ENDGRDS
121: }
122: if (&Apache::lonnet::allowed('opa',$ENV{'request.course.id'})) {
123: $hwkadd.=(<<ENDPARM);
124: menu.switchbutton
125: (7,3,'pparm.gif','problem','parms',
1.88 www 126: 'gocmd("/adm/parmset","set")');
1.87 www 127: ENDPARM
128: }
129: }
1.86 www 130: return (<<ENDREGTHIS);
1.87 www 131:
1.68 www 132: <script language="JavaScript">
1.71 www 133: // BEGIN LON-CAPA Internal
1.86 www 134:
1.69 www 135: function LONCAPAreg() {
136: menu=window.open("","LONCAPAmenu");
1.86 www 137: menu.clearTimeout(menu.menucltim);
1.69 www 138: menu.currentURL=window.location.pathname;
139: menu.currentStale=0;
1.85 www 140: menu.clearbut(3,1);
141: menu.switchbutton
142: (8,1,'eval.gif','evaluate','this','gopost("/adm/evaluate",currentURL)');
143: menu.switchbutton
144: (8,2,'fdbk.gif','feedback','on this','gopost("/adm/feedback",currentURL)');
145: menu.switchbutton
146: (8,3,'prt.gif','prepare','printout','gopost("/adm/printout",currentURL)');
147: menu.switchbutton
148: (2,1,'back.gif','backward','','gopost("/adm/flip","back:"+currentURL)');
149: menu.switchbutton
150: (2,3,'forw.gif','forward','','gopost("/adm/flip","forward:"+currentURL)');
1.94 www 151: menu.switchbutton
152: (9,1,'sbkm.gif','set','bookmark','set_bookmark()');
153: menu.switchbutton
154: (9,2,'vbkm.gif','view','bookmark','edit_bookmarks()');
155: menu.switchbutton
1.95 www 156: (9,3,'anot.gif','anno-','tations','annotate()');
1.87 www 157: $hwkadd
1.69 www 158: }
1.86 www 159:
1.69 www 160: function LONCAPAstale() {
1.86 www 161: menu=window.open("","LONCAPAmenu");
162: menu.currentStale=1;
163: menu.switchbutton
164: (3,1,'reload.gif','return','location','go(currentURL)');
1.90 www 165: menu.clearbut(7,1);
166: menu.clearbut(7,2);
167: menu.clearbut(7,3);
1.86 www 168: menu.menucltim=menu.setTimeout(
1.94 www 169: 'clearbut(2,1);clearbut(2,3);clearbut(8,1);clearbut(8,2);clearbut(8,3);'+
170: 'clearbut(9,1);clearbut(9,2);clearbut(9,3);',
1.86 www 171: 2000);
172:
1.87 www 173: }
1.86 www 174:
175: // END LON-CAPA Internal
176: </script>
177: ENDREGTHIS
178:
179: } else {
180: return (<<ENDDONOTREGTHIS);
181:
182: <script language="JavaScript">
183: // BEGIN LON-CAPA Internal
184:
185: function LONCAPAreg() {
1.69 www 186: menu=window.open("","LONCAPAmenu");
187: menu.currentStale=1;
1.85 www 188: menu.clearbut(2,1);
189: menu.clearbut(2,3);
190: menu.clearbut(8,1);
191: menu.clearbut(8,2);
192: menu.clearbut(8,3);
1.86 www 193: if (menu.currentURL) {
194: menu.switchbutton
195: (3,1,'reload.gif','return','location','go(currentURL)');
196: } else {
197: menu.clearbut(3,1);
198: }
199: }
200:
201: function LONCAPAstale() {
1.68 www 202: }
1.86 www 203:
1.71 www 204: // END LON-CAPA Internal
1.68 www 205: </script>
1.86 www 206: ENDDONOTREGTHIS
207:
208: }
1.69 www 209: }
210:
211: sub loadevents() {
212: return 'LONCAPAreg();';
213: }
214:
215: sub unloadevents() {
216: return 'LONCAPAstale();';
1.68 www 217: }
218:
1.48 albertel 219: sub printalltags {
220: my $temp;
221: foreach $temp (sort keys %Apache::lonxml::alltags) {
1.64 albertel 222: &Apache::lonxml::debug("$temp -- $Apache::lonxml::alltags{$temp}");
1.48 albertel 223: }
224: }
1.31 sakharuk 225:
1.3 sakharuk 226: sub xmlparse {
1.18 albertel 227: my ($target,$content_file_string,$safeinit,%style_for_target) = @_;
1.96 ! albertel 228:
! 229: &setup_globals($target);
1.48 albertel 230: #&printalltags();
1.16 albertel 231: my @pars = ();
1.23 albertel 232: @Apache::lonxml::pwd=();
233: my $pwd=$ENV{'request.filename'};
234: $pwd =~ s:/[^/]*$::;
235: &newparser(\@pars,\$content_file_string,$pwd);
1.3 sakharuk 236: my $currentstring = '';
237: my $finaloutput = '';
238: my $newarg = '';
1.16 albertel 239: my $result;
1.24 sakharuk 240:
1.3 sakharuk 241: my $safeeval = new Safe;
1.40 albertel 242: my $safehole = new Safe::Hole;
1.82 ng 243: &init_safespace($target,$safeeval,$safehole,$safeinit);
1.3 sakharuk 244: #-------------------- Redefinition of the target in the case of compound target
245:
246: ($target, my @tenta) = split('&&',$target);
247:
248: my @stack = ();
249: my @parstack = ();
1.17 albertel 250: &initdepth;
1.3 sakharuk 251: my $token;
1.16 albertel 252: while ( $#pars > -1 ) {
253: while ($token = $pars[$#pars]->get_token) {
1.57 albertel 254: if (($token->[0] eq 'T') || ($token->[0] eq 'C') || ($token->[0] eq 'D') ) {
1.61 albertel 255: if ($metamode<1) { $result=$token->[1]; }
1.57 albertel 256: } elsif ($token->[0] eq 'PI') {
1.61 albertel 257: if ($metamode<1) { $result=$token->[2]; }
1.16 albertel 258: } elsif ($token->[0] eq 'S') {
259: # add tag to stack
260: push (@stack,$token->[1]);
261: # add parameters list to another stack
262: push (@parstack,&parstring($token));
1.19 albertel 263: &increasedepth($token);
1.16 albertel 264: if (exists $style_for_target{$token->[1]}) {
1.61 albertel 265: if ($Apache::lonxml::redirection) {
1.55 albertel 266: $Apache::lonxml::outputstack['-1'] .=
267: &recurse($style_for_target{$token->[1]},$target,$safeeval,
268: \%style_for_target,@parstack);
1.41 albertel 269: } else {
1.55 albertel 270: $finaloutput .= &recurse($style_for_target{$token->[1]},$target,
271: $safeeval,\%style_for_target,@parstack);
1.41 albertel 272: }
1.16 albertel 273: } else {
1.84 albertel 274: $result = &callsub("start_$token->[1]", $target, $token, \@stack,
275: \@parstack, \@pars, $safeeval, \%style_for_target);
1.16 albertel 276: }
277: } elsif ($token->[0] eq 'E') {
278: #clear out any tags that didn't end
1.55 albertel 279: while ($token->[1] ne $stack[$#stack] && ($#stack > -1)) {
280: &Apache::lonxml::warning("Unbalanced tags in resource $stack['-1']");
1.43 albertel 281: pop @stack;pop @parstack;&decreasedepth($token);
282: }
1.16 albertel 283:
284: if (exists $style_for_target{'/'."$token->[1]"}) {
1.61 albertel 285: if ($Apache::lonxml::redirection) {
1.55 albertel 286: $Apache::lonxml::outputstack['-1'] .=
287: &recurse($style_for_target{'/'."$token->[1]"},
288: $target,$safeeval,\%style_for_target,@parstack);
289: } else {
290: $finaloutput .= &recurse($style_for_target{'/'."$token->[1]"},
291: $target,$safeeval,\%style_for_target,
292: @parstack);
293: }
1.59 albertel 294:
1.16 albertel 295: } else {
1.84 albertel 296: $result = &callsub("end_$token->[1]", $target, $token, \@stack,
297: \@parstack, \@pars,$safeeval, \%style_for_target);
1.13 albertel 298: }
1.57 albertel 299: } else {
300: &Apache::lonxml::error("Unknown token event :$token->[0]:$token->[1]:");
1.16 albertel 301: }
1.55 albertel 302: #evaluate variable refs in result
1.25 sakharuk 303: if ($result ne "") {
1.24 sakharuk 304: if ( $#parstack > -1 ) {
1.55 albertel 305: if ($Apache::lonxml::redirection) {
306: $Apache::lonxml::outputstack['-1'] .=
307: &Apache::run::evaluate($result,$safeeval,$parstack[$#parstack]);
308: } else {
309: $finaloutput .= &Apache::run::evaluate($result,$safeeval,
310: $parstack[$#parstack]);
311: }
1.16 albertel 312: } else {
313: $finaloutput .= &Apache::run::evaluate($result,$safeeval,'');
1.13 albertel 314: }
1.16 albertel 315: $result = '';
1.55 albertel 316: }
317: if ($token->[0] eq 'E') {
318: pop @stack;pop @parstack;&decreasedepth($token);
1.2 sakharuk 319: }
1.5 albertel 320: }
1.16 albertel 321: pop @pars;
1.23 albertel 322: pop @Apache::lonxml::pwd;
1.3 sakharuk 323: }
1.24 sakharuk 324:
1.59 albertel 325: # if ($target eq 'meta') {
326: # $finaloutput.=&endredirection;
327: # }
1.67 www 328:
329: if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
330: $finaloutput=&afterburn($finaloutput);
331: }
332:
1.3 sakharuk 333: return $finaloutput;
1.15 albertel 334: }
335:
1.67 www 336:
1.15 albertel 337: sub recurse {
338:
339: my @innerstack = ();
340: my @innerparstack = ();
341: my ($newarg,$target,$safeeval,$style_for_target,@parstack) = @_;
1.16 albertel 342: my @pat = ();
1.23 albertel 343: &newparser(\@pat,\$newarg);
1.15 albertel 344: my $tokenpat;
345: my $partstring = '';
346: my $output='';
1.16 albertel 347: my $decls='';
348: while ( $#pat > -1 ) {
349: while ($tokenpat = $pat[$#pat]->get_token) {
1.57 albertel 350: if (($tokenpat->[0] eq 'T') || ($tokenpat->[0] eq 'C') || ($tokenpat->[0] eq 'D') ) {
1.61 albertel 351: if ($metamode<1) { $partstring=$tokenpat->[1]; }
1.57 albertel 352: } elsif ($tokenpat->[0] eq 'PI') {
1.61 albertel 353: if ($metamode<1) { $partstring=$tokenpat->[2]; }
1.16 albertel 354: } elsif ($tokenpat->[0] eq 'S') {
355: push (@innerstack,$tokenpat->[1]);
356: push (@innerparstack,&parstring($tokenpat));
1.19 albertel 357: &increasedepth($tokenpat);
1.84 albertel 358: $partstring = &callsub("start_$tokenpat->[1]", $target, $tokenpat,
359: \@innerstack, \@innerparstack, \@pat,
360: $safeeval, $style_for_target);
1.16 albertel 361: } elsif ($tokenpat->[0] eq 'E') {
362: #clear out any tags that didn't end
363: while ($tokenpat->[1] ne $innerstack[$#innerstack]
1.43 albertel 364: && ($#innerstack > -1)) {
1.49 albertel 365: &Apache::lonxml::warning("Unbalanced tags in resource $innerstack['-1']");
1.43 albertel 366: pop @innerstack;pop @innerparstack;&decreasedepth($tokenpat);
367: }
1.84 albertel 368: $partstring = &callsub("end_$tokenpat->[1]", $target, $tokenpat,
369: \@innerstack, \@innerparstack, \@pat,
370: $safeeval, $style_for_target);
1.57 albertel 371: } else {
372: &Apache::lonxml::error("Unknown token event :$tokenpat->[0]:$tokenpat->[1]:");
1.16 albertel 373: }
374: #pass both the variable to the style tag, and the tag we
375: #are processing inside the <definedtag>
376: if ( $partstring ne "" ) {
377: if ( $#parstack > -1 ) {
378: if ( $#innerparstack > -1 ) {
379: $decls= $parstack[$#parstack].$innerparstack[$#innerparstack];
380: } else {
381: $decls= $parstack[$#parstack];
382: }
383: } else {
384: if ( $#innerparstack > -1 ) {
385: $decls=$innerparstack[$#innerparstack];
386: } else {
387: $decls='';
388: }
389: }
390: $output .= &Apache::run::evaluate($partstring,$safeeval,$decls);
391: $partstring = '';
392: }
1.17 albertel 393: if ($tokenpat->[0] eq 'E') { pop @innerstack;pop @innerparstack;
1.19 albertel 394: &decreasedepth($tokenpat);}
1.15 albertel 395: }
1.16 albertel 396: pop @pat;
1.23 albertel 397: pop @Apache::lonxml::pwd;
1.15 albertel 398: }
399: return $output;
1.7 albertel 400: }
401:
402: sub callsub {
1.84 albertel 403: my ($sub,$target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.7 albertel 404: my $currentstring='';
1.72 albertel 405: my $nodefault;
1.7 albertel 406: {
1.59 albertel 407: my $sub1;
1.7 albertel 408: no strict 'refs';
1.68 www 409: my $tag=$token->[1];
410: my $space=$Apache::lonxml::alltags{$tag};
411: if (!$space) {
412: $tag=~tr/A-Z/a-z/;
413: $sub=~tr/A-Z/a-z/;
414: $space=$Apache::lonxml::alltags{$tag}
415: }
416: if ($space) {
1.72 albertel 417: #&Apache::lonxml::debug("Calling sub $sub in $space $metamode<br />\n");
1.24 sakharuk 418: $sub1="$space\:\:$sub";
1.17 albertel 419: $Apache::lonxml::curdepth=join('_',@Apache::lonxml::depthcounter);
1.84 albertel 420: ($currentstring,$nodefault) = &$sub1($target,$token,$tagstack,
421: $parstack,$parser,$safeeval,
422: $style);
1.7 albertel 423: } else {
1.72 albertel 424: #&Apache::lonxml::debug("NOT Calling sub $sub in $space $metamode<br />\n");
1.62 sakharuk 425: if ($metamode <1) {
426: if (defined($token->[4]) && ($metamode < 1)) {
1.72 albertel 427: $currentstring = $token->[4];
1.62 sakharuk 428: } else {
1.72 albertel 429: $currentstring = $token->[2];
1.62 sakharuk 430: }
1.7 albertel 431: }
1.59 albertel 432: }
1.83 albertel 433: # &Apache::lonxml::debug("nodefalt:$nodefault:");
1.72 albertel 434: if ($currentstring eq '' && $nodefault eq '') {
435: if ($target eq 'edit') {
1.74 albertel 436: &Apache::lonxml::debug("doing default edit for $token->[1]");
1.72 albertel 437: if ($token->[0] eq 'S') {
1.74 albertel 438: $currentstring = &Apache::edit::tag_start($target,$token);
1.72 albertel 439: } elsif ($token->[0] eq 'E') {
1.74 albertel 440: $currentstring = &Apache::edit::tag_end($target,$token);
1.72 albertel 441: }
442: } elsif ($target eq 'modified') {
443: if ($token->[0] eq 'S') {
444: $currentstring = $token->[4];
1.76 albertel 445: $currentstring.=&Apache::edit::handle_insert();
1.72 albertel 446: } else {
447: $currentstring = $token->[2];
448: }
449: }
1.7 albertel 450: }
451: use strict 'refs';
452: }
453: return $currentstring;
1.82 ng 454: }
455:
1.96 ! albertel 456: sub setup_globals {
! 457: my ($target)=@_;
! 458: if ($target eq 'meta') {
! 459: $Apache::lonxml::redirection = 0;
! 460: $Apache::lonxml::metamode = 1;
! 461: $Apache::lonxml::evaluate = 1;
! 462: $Apache::lonxml::import = 0;
! 463: } elsif ($target eq 'grade') {
! 464: &startredirection;
! 465: $Apache::lonxml::metamode = 0;
! 466: $Apache::lonxml::evaluate = 1;
! 467: $Apache::lonxml::import = 1;
! 468: } elsif ($target eq 'modified') {
! 469: $Apache::lonxml::redirection = 0;
! 470: $Apache::lonxml::metamode = 0;
! 471: $Apache::lonxml::evaluate = 0;
! 472: $Apache::lonxml::import = 0;
! 473: } elsif ($target eq 'edit') {
! 474: $Apache::lonxml::redirection = 0;
! 475: $Apache::lonxml::metamode = 0;
! 476: $Apache::lonxml::evaluate = 0;
! 477: $Apache::lonxml::import = 0;
! 478: } else {
! 479: $Apache::lonxml::redirection = 0;
! 480: $Apache::lonxml::metamode = 0;
! 481: $Apache::lonxml::evaluate = 1;
! 482: $Apache::lonxml::import = 1;
! 483: }
! 484: }
! 485:
1.82 ng 486: sub init_safespace {
487: my ($target,$safeeval,$safehole,$safeinit) = @_;
488: $safeeval->permit("entereval");
489: $safeeval->permit(":base_math");
490: $safeeval->permit("sort");
491: $safeeval->deny(":base_io");
492: $safehole->wrap(\&Apache::lonnet::EXT,$safeeval,'&EXT');
493:
494: $safehole->wrap(\&Math::Cephes::asin,$safeeval,'&asin');
495: $safehole->wrap(\&Math::Cephes::acos,$safeeval,'&acos');
496: $safehole->wrap(\&Math::Cephes::atan,$safeeval,'&atan');
497: $safehole->wrap(\&Math::Cephes::sinh,$safeeval,'&sinh');
498: $safehole->wrap(\&Math::Cephes::cosh,$safeeval,'&cosh');
499: $safehole->wrap(\&Math::Cephes::tanh,$safeeval,'&tanh');
500: $safehole->wrap(\&Math::Cephes::asinh,$safeeval,'&asinh');
501: $safehole->wrap(\&Math::Cephes::acosh,$safeeval,'&acosh');
502: $safehole->wrap(\&Math::Cephes::atanh,$safeeval,'&atanh');
503: $safehole->wrap(\&Math::Cephes::erf,$safeeval,'&erf');
504: $safehole->wrap(\&Math::Cephes::erfc,$safeeval,'&erfc');
505: $safehole->wrap(\&Math::Cephes::j0,$safeeval,'&j0');
506: $safehole->wrap(\&Math::Cephes::j1,$safeeval,'&j1');
507: $safehole->wrap(\&Math::Cephes::jn,$safeeval,'&jn');
508: $safehole->wrap(\&Math::Cephes::jv,$safeeval,'&jv');
509: $safehole->wrap(\&Math::Cephes::y0,$safeeval,'&y0');
510: $safehole->wrap(\&Math::Cephes::y1,$safeeval,'&y1');
511: $safehole->wrap(\&Math::Cephes::yn,$safeeval,'&yn');
512: $safehole->wrap(\&Math::Cephes::yv,$safeeval,'&yv');
1.91 ng 513: $safehole->wrap(\&Math::Random::random_beta,$safeeval,'&math_random_beta');
514: $safehole->wrap(\&Math::Random::random_chi_square,$safeeval,'&math_random_chi_square');
515: $safehole->wrap(\&Math::Random::random_exponential,$safeeval,'&math_random_exponential');
516: $safehole->wrap(\&Math::Random::random_f,$safeeval,'&math_random_f');
517: $safehole->wrap(\&Math::Random::random_gamma,$safeeval,'&math_random_gamma');
518: $safehole->wrap(\&Math::Random::random_multivariate_normal,$safeeval,'&math_random_multivariate_normal');
519: $safehole->wrap(\&Math::Random::random_multinomial,$safeeval,'&math_random_multinomial');
520: $safehole->wrap(\&Math::Random::random_noncentral_chi_square,$safeeval,'&math_random_noncentral_chi_square');
521: $safehole->wrap(\&Math::Random::random_noncentral_f,$safeeval,'&math_random_noncentral_f');
522: $safehole->wrap(\&Math::Random::random_normal,$safeeval,'&math_random_normal');
523: $safehole->wrap(\&Math::Random::random_permutation,$safeeval,'&math_random_permutation');
1.93 ng 524: $safehole->wrap(\&Math::Random::random_permuted_index,$safeeval,'&math_random_permuted_index');
1.91 ng 525: $safehole->wrap(\&Math::Random::random_uniform,$safeeval,'&math_random_uniform');
526: $safehole->wrap(\&Math::Random::random_poisson,$safeeval,'&math_random_poisson');
527: $safehole->wrap(\&Math::Random::random_uniform_integer,$safeeval,'&math_random_uniform_integer');
528: $safehole->wrap(\&Math::Random::random_negative_binomial,$safeeval,'&math_random_negative_binomial');
529: $safehole->wrap(\&Math::Random::random_binomial,$safeeval,'&math_random_binomial');
530: $safehole->wrap(\&Math::Random::random_seed_from_phrase,$safeeval,'&random_seed_from_phrase');
531: $safehole->wrap(\&Math::Random::random_set_seed_from_phrase,$safeeval,'&random_set_seed_from_phrase');
532: $safehole->wrap(\&Math::Random::random_get_seed,$safeeval,'&random_get_seed');
533: $safehole->wrap(\&Math::Random::random_set_seed,$safeeval,'&random_set_seed');
534:
1.82 ng 535: #need to inspect this class of ops
536: # $safeeval->deny(":base_orig");
1.91 ng 537: $safeinit .= ';$external::target="'.$target.'";';
1.82 ng 538: $safeinit .= ';$external::randomseed='.&Apache::lonnet::rndseed().';';
539: &Apache::run::run($safeinit,$safeeval);
1.17 albertel 540: }
541:
1.55 albertel 542: sub startredirection {
543: $Apache::lonxml::redirection++;
544: push (@Apache::lonxml::outputstack, '');
545: }
546:
547: sub endredirection {
548: if (!$Apache::lonxml::redirection) {
1.72 albertel 549: &Apache::lonxml::error("Endredirection was called, before a startredirection, perhaps you have unbalanced tags. Some debuging information:".join ":",caller);
1.55 albertel 550: return '';
551: }
552: $Apache::lonxml::redirection--;
553: pop @Apache::lonxml::outputstack;
554: }
555:
1.17 albertel 556: sub initdepth {
557: @Apache::lonxml::depthcounter=();
558: $Apache::lonxml::depth=-1;
559: $Apache::lonxml::olddepth=-1;
560: }
561:
562: sub increasedepth {
1.19 albertel 563: my ($token) = @_;
1.17 albertel 564: $Apache::lonxml::depth++;
565: $Apache::lonxml::depthcounter[$Apache::lonxml::depth]++;
566: if ($Apache::lonxml::depthcounter[$Apache::lonxml::depth]==1) {
567: $Apache::lonxml::olddepth=$Apache::lonxml::depth;
568: }
1.42 albertel 569: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64 albertel 570: &Apache::lonxml::debug("s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n");
1.54 albertel 571: #print "<br />s $Apache::lonxml::depth : $Apache::lonxml::olddepth : $curdepth : $token->[1]\n";
1.17 albertel 572: }
573:
574: sub decreasedepth {
1.19 albertel 575: my ($token) = @_;
1.17 albertel 576: $Apache::lonxml::depth--;
1.36 albertel 577: if ($Apache::lonxml::depth<$Apache::lonxml::olddepth-1) {
578: $#Apache::lonxml::depthcounter--;
579: $Apache::lonxml::olddepth=$Apache::lonxml::depth+1;
580: }
1.43 albertel 581: if ( $Apache::lonxml::depth < -1) {
1.49 albertel 582: &Apache::lonxml::warning("Unbalanced tags in resource");
1.43 albertel 583: $Apache::lonxml::depth='-1';
584: }
1.42 albertel 585: my $curdepth=join('_',@Apache::lonxml::depthcounter);
1.64 albertel 586: &Apache::lonxml::debug("e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n");
1.54 albertel 587: #print "<br />e $Apache::lonxml::depth : $Apache::lonxml::olddepth : $token->[1] : $curdepth\n";
1.1 sakharuk 588: }
1.19 albertel 589:
590: sub get_all_text {
591:
592: my($tag,$pars)= @_;
593: my $depth=0;
594: my $token;
595: my $result='';
1.57 albertel 596: if ( $tag =~ m:^/: ) {
597: my $tag=substr($tag,1);
598: # &Apache::lonxml::debug("have:$tag:");
599: while (($depth >=0) && ($token = $pars->get_token)) {
600: # &Apache::lonxml::debug("e token:$token->[0]:$depth:$token->[1]");
601: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
602: $result.=$token->[1];
603: } elsif ($token->[0] eq 'PI') {
604: $result.=$token->[2];
605: } elsif ($token->[0] eq 'S') {
606: if ($token->[1] eq $tag) { $depth++; }
607: $result.=$token->[4];
608: } elsif ($token->[0] eq 'E') {
609: if ( $token->[1] eq $tag) { $depth--; }
610: #skip sending back the last end tag
611: if ($depth > -1) { $result.=$token->[2]; } else {
612: $pars->unget_token($token);
613: }
614: }
615: }
616: } else {
617: while ($token = $pars->get_token) {
618: # &Apache::lonxml::debug("s token:$token->[0]:$depth:$token->[1]");
619: if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
620: $result.=$token->[1];
621: } elsif ($token->[0] eq 'PI') {
622: $result.=$token->[2];
623: } elsif ($token->[0] eq 'S') {
624: if ( $token->[1] eq $tag) {
625: $pars->unget_token($token); last;
626: } else {
627: $result.=$token->[4];
628: }
629: } elsif ($token->[0] eq 'E') {
630: $result.=$token->[2];
1.36 albertel 631: }
1.19 albertel 632: }
633: }
1.49 albertel 634: # &Apache::lonxml::debug("Exit:$result:");
1.19 albertel 635: return $result
636: }
637:
1.23 albertel 638: sub newparser {
639: my ($parser,$contentref,$dir) = @_;
640: push (@$parser,HTML::TokeParser->new($contentref));
1.56 albertel 641: $$parser['-1']->xml_mode('1');
1.23 albertel 642: if ( $dir eq '' ) {
643: push (@Apache::lonxml::pwd, $Apache::lonxml::pwd[$#Apache::lonxml::pwd]);
644: } else {
645: push (@Apache::lonxml::pwd, $dir);
646: }
647: # &Apache::lonxml::debug("pwd:$#Apache::lonxml::pwd");
648: # &Apache::lonxml::debug("pwd:$Apache::lonxml::pwd[$#Apache::lonxml::pwd]");
649: }
1.1 sakharuk 650:
1.8 albertel 651: sub parstring {
652: my ($token) = @_;
653: my $temp='';
1.20 albertel 654: map {
1.35 www 655: unless ($_=~/\W/) {
1.42 albertel 656: my $val=$token->[2]->{$_};
1.53 albertel 657: $val =~ s/([\%\@\\])/\\$1/g;
1.51 albertel 658: #if ($val =~ m/^[\%\@]/) { $val="\\".$val; }
1.42 albertel 659: $temp .= "my \$$_=\"$val\";"
1.20 albertel 660: }
661: } @{$token->[3]};
1.8 albertel 662: return $temp;
663: }
1.22 albertel 664:
1.34 www 665: sub writeallows {
666: my $thisurl='/res/'.&Apache::lonnet::declutter(shift);
667: my $thisdir=$thisurl;
668: $thisdir=~s/\/[^\/]+$//;
669: my %httpref=();
670: map {
671: $httpref{'httpref.'.
672: &Apache::lonnet::hreflocation($thisdir,$_)}=$thisurl; } @extlinks;
673: &Apache::lonnet::appenv(%httpref);
674: }
675:
1.66 www 676: #
677: # Afterburner handles anchors, highlights and links
678: #
679: sub afterburn {
680: my $result=shift;
681: map {
682: my ($name, $value) = split(/=/,$_);
683: $value =~ tr/+/ /;
684: $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
685: if (($name eq 'highlight')||($name eq 'anchor')||($name eq 'link')) {
686: unless ($ENV{'form.'.$name}) {
687: $ENV{'form.'.$name}=$value;
688: }
689: }
690: } (split(/&/,$ENV{'QUERY_STRING'}));
691: if ($ENV{'form.highlight'}) {
692: map {
693: my $anchorname=$_;
694: my $matchthis=$anchorname;
695: $matchthis=~s/\_+/\\s\+/g;
696: $result=~s/($matchthis)/\<font color=\"red\"\>$1\<\/font\>/gs;
697: } split(/\,/,$ENV{'form.highlight'});
698: }
699: if ($ENV{'form.link'}) {
700: map {
701: my ($anchorname,$linkurl)=split(/\>/,$_);
702: my $matchthis=$anchorname;
703: $matchthis=~s/\_+/\\s\+/g;
704: $result=~s/($matchthis)/\<a href=\"$linkurl\"\>$1\<\/a\>/gs;
705: } split(/\,/,$ENV{'form.link'});
706: }
707: if ($ENV{'form.anchor'}) {
708: my $anchorname=$ENV{'form.anchor'};
709: my $matchthis=$anchorname;
710: $matchthis=~s/\_+/\\s\+/g;
711: $result=~s/($matchthis)/\<a name=\"$anchorname\"\>$1\<\/a\>/s;
712: $result.=(<<"ENDSCRIPT");
713: <script>
714: document.location.hash='$anchorname';
715: </script>
716: ENDSCRIPT
717: }
718: return $result;
719: }
720:
1.79 www 721: sub storefile {
722: my ($file,$contents)=@_;
723: if (my $fh=Apache::File->new('>'.$file)) {
724: print $fh $contents;
725: $fh->close();
726: }
727: }
728:
1.78 www 729: sub inserteditinfo {
730: my ($result,$filecontents)=@_;
731: unless ($filecontents) {
732: $filecontents=(<<SIMPLECONTENT);
733: <html>
734: <head>
735: <title>
736: Title of Document Goes Here
737: </title>
738: </head>
739: <body bgcolor="#FFFFFF">
740:
741: Body of Document Goes Here
742:
743: </body>
744: </html>
745: SIMPLECONTENT
746: }
747: my $editheader='<a href="#editsection">Edit below</a><hr />';
748: my $editfooter=(<<ENDFOOTER);
749: <hr />
750: <a name="editsection" />
751: <form method="post">
752: <textarea cols="80" rows="40" name="filecont">$filecontents</textarea>
753: <br />
754: <input type="submit" name="savethisfile" value="Save this file" />
755: </form>
756: ENDFOOTER
757: $result=~s/(\<body[^\>]*\>)/$1$editheader/is;
758: $result=~s/(\<\/body\>)/$editfooter/is;
759: return $result;
760: }
761:
1.24 sakharuk 762: sub handler {
763: my $request=shift;
1.68 www 764:
1.64 albertel 765: my $target='web';
1.68 www 766:
1.65 albertel 767: $Apache::lonxml::debug=0;
1.68 www 768:
1.25 sakharuk 769: if ($ENV{'browser.mathml'}) {
1.27 albertel 770: $request->content_type('text/xml');
771: } else {
772: $request->content_type('text/html');
1.25 sakharuk 773: }
1.64 albertel 774:
1.27 albertel 775: $request->send_http_header;
1.64 albertel 776:
1.45 www 777: return OK if $request->header_only;
1.27 albertel 778:
1.79 www 779:
780: my $file=&Apache::lonnet::filelocation("",$request->uri);
1.78 www 781: #
782: # Edit action? Save file.
783: #
784: unless ($ENV{'request.state'} eq 'published') {
785: if ($ENV{'form.savethisfile'}) {
1.79 www 786: &storefile($file,$ENV{'form.filecont'});
1.78 www 787: }
788: }
1.24 sakharuk 789: my %mystyle;
1.50 albertel 790: my $result = '';
791: my $filecontents=&Apache::lonnet::getfile($file);
792: if ($filecontents == -1) {
1.78 www 793: $result=(<<ENDNOTFOUND);
794: <html>
795: <head>
796: <title>File not found</title>
797: </head>
798: <body bgcolor="#FFFFFF">
799: <b>File not found: $file</b>
800: </body>
801: </html>
802: ENDNOTFOUND
1.50 albertel 803: $filecontents='';
804: } else {
805: $result = &Apache::lonxml::xmlparse($target,$filecontents,'',%mystyle);
1.78 www 806: }
807:
808: #
809: # Edit action? Insert editing commands
810: #
811: unless ($ENV{'request.state'} eq 'published') {
812: $result=&inserteditinfo($result,$filecontents);
1.66 www 813: }
1.50 albertel 814:
1.67 www 815: $request->print($result);
1.64 albertel 816:
1.34 www 817: writeallows($request->uri);
1.45 www 818: return OK;
1.24 sakharuk 819: }
820:
1.22 albertel 821: sub debug {
822: if ($Apache::lonxml::debug eq 1) {
1.96 ! albertel 823: print("DEBUG:".$_[0]."<br />\n");
1.22 albertel 824: }
825: }
1.49 albertel 826:
1.22 albertel 827: sub error {
1.74 albertel 828: if (($Apache::lonxml::debug eq 1) || ($ENV{'request.state'} eq 'construct') ) {
1.55 albertel 829: print "<b>ERROR:</b>".$_[0]."<br />\n";
1.52 albertel 830: } else {
831: print "<b>An Error occured while processing this resource. The instructor has been notified.</b> <br />";
832: #notify author
833: &Apache::lonmsg::author_res_msg($ENV{'request.filename'},$_[0]);
834: #notify course
835: if ( $ENV{'request.course.id'} ) {
836: my $users=$ENV{'course.'.$ENV{'request.course.id'}.'.comment.email'};
837: foreach my $user (split /\,/, $users) {
838: ($user,my $domain) = split /:/, $user;
1.54 albertel 839: &Apache::lonmsg::user_normal_msg($user,$domain,"Error in $ENV{'request.filename'}",$_[0]);
1.52 albertel 840: }
841: }
1.74 albertel 842:
1.52 albertel 843: #FIXME probably shouldn't have me get everything forever.
1.54 albertel 844: &Apache::lonmsg::user_normal_msg('albertel','msu',"Error in $ENV{'request.filename'}",$_[0]);
1.74 albertel 845: #&Apache::lonmsg::user_normal_msg('albertel','103',"Error in $ENV{'request.filename'}",$_[0]);
1.52 albertel 846: }
1.22 albertel 847: }
1.49 albertel 848:
1.22 albertel 849: sub warning {
1.73 harris41 850: if ($ENV{'request.state'} eq 'construct') {
1.55 albertel 851: print "<b>W</b>ARNING<b>:</b>".$_[0]."<br />\n";
1.73 harris41 852: }
1.83 albertel 853: }
854:
855: sub get_param {
856: my ($param,$parstack,$safeeval,$context) = @_;
857: if ( ! $context ) { $context = -1; }
858: my $args ='';
859: if ( $#$parstack > (-2-$context) ) { $args=$$parstack[$context]; }
860: return &Apache::run::run("{$args;".'return $'.$param.'}',$safeeval); #'
1.22 albertel 861: }
862:
1.74 albertel 863: sub register_insert {
1.75 albertel 864: my @data = split /\n/, &Apache::lonnet::getfile('/home/httpd/lonTabs/insertlist.tab');
1.74 albertel 865: my $i;
1.76 albertel 866: my $tagnum=0;
1.74 albertel 867: my @order;
868: for ($i=0;$i < $#data; $i++) {
869: my $line = $data[$i];
870: if ( $line =~ /^\#/ || $line =~ /^\s*\n/) { next; }
871: if ( $line =~ /TABLE/ ) { last; }
1.92 albertel 872: my ($tag,$descrip,$color,$function,$show) = split(/,/, $line);
1.76 albertel 873: $insertlist{"$tagnum.tag"} = $tag;
874: $insertlist{"$tagnum.description"} = $descrip;
1.92 albertel 875: $insertlist{"$tagnum.color"} = $color;
1.76 albertel 876: $insertlist{"$tagnum.function"} = $function;
877: $insertlist{"$tagnum.show"}= $show;
1.92 albertel 878: $insertlist{"$tag.num"}=$tagnum;
1.76 albertel 879: $tagnum++;
1.74 albertel 880: }
1.76 albertel 881: $i++; #skipping TABLE line
882: $tagnum = 0;
1.74 albertel 883: for (;$i < $#data;$i++) {
884: my $line = $data[$i];
1.76 albertel 885: my ($mnemonic,@which) = split(/ +/,$line);
886: my $tag = $insertlist{"$tagnum.tag"};
1.74 albertel 887: for (my $j=0;$j <$#which;$j++) {
888: if ( $which[$j] eq 'Y' ) {
1.76 albertel 889: if ($insertlist{"$j.show"} ne 'no') {
890: push(@{ $insertlist{"$tag.which"} },$j);
891: }
1.74 albertel 892: }
893: }
1.76 albertel 894: $tagnum++;
1.74 albertel 895: }
896: }
1.1 sakharuk 897: 1;
898: __END__
1.68 www 899:
900:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>