[system] / trunk / pg / lib / PGalias.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/PGalias.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6851 - (download) (as text) (annotate)
Sat Jun 11 17:17:39 2011 UTC (8 years, 5 months ago) by gage
File size: 33562 byte(s)
?	added some warning messages to Course Admin if permissions on DATA, log and html directories are not set correctly.
?	added refreshEquations(1) to PG to force all equation images to be recalculated.

?added AddToTexPreamble($str ) to PG  to allow short macro definitions such as    \newcommand{\myVec}[#1]{\vec{#1}}
	?	this works in images mode and in hardcopy mode.  It does not work in jsMath mode (but fails gracefully).  MathJax also fails, not quite so gracefully.


Added some additional POD documentation


    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/lib/PGalias.pm,v 1.6 2010/05/15 18:41:23 gage Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 package PGresource;
   18 use strict;
   19 use Exporter;
   20 use PGcore;
   21 
   22 sub new {
   23   my $class = shift;
   24   my $aux_file_name = shift;  #pointer to auxiliary fle
   25   my $self = {
   26     type        =>  'png', # gif eps pdf html pg (macro: pl) (applets: java js fla geogebra (ggb) )
   27     path    =>  { content => undef,       # file path to resource
   28               is_complete=>0,
   29               is_accessible => 0,
   30             },
   31     url     =>  { content => undef,       # url path to resource
   32               is_complete=>0,
   33               is_accessible => 0,
   34             },
   35     return_uri  =>  '',
   36     recorded_uri => '',
   37     convert      => { needed  => 0,
   38               from_type => undef,
   39               from_path => undef,
   40               to_type => undef,
   41               to_path => undef,
   42             },
   43     copy_link   =>  { type => undef,  # copy or link or ??
   44               link_to_path => undef,
   45               copy_to_path => undef,
   46             },
   47     cache_info  =>  {},
   48     unique_id   =>  undef,
   49   };
   50   bless $self, $class;
   51   # $self->initialize;
   52   # $self->check_parameters;
   53   return $self;
   54 }
   55 
   56 package PGalias;
   57 use strict;
   58 use Exporter;
   59 use PGcore;
   60 #use WeBWorK::PG::IO;
   61 
   62 our @ISA =  qw ( PGcore  );  # look up features in PGcore -- in this case we want the environment.
   63 
   64 =head2
   65 
   66 # new
   67 #   Create one alias object per question (and per PGcore object)
   68 #   Check that information is intact
   69 #   Construct unique id stubs -- the id stub is for this PGalias object which is
   70 #        attached to all the resource files (except equations) for this question.
   71 #   Keep list of external links
   72 
   73 =cut
   74 
   75 sub new {
   76   my $class = shift;
   77   my $envir = shift;  #pointer to environment hash
   78   warn "PGlias must be called with an environment" unless ref($envir) eq 'HASH';
   79   my $self = {
   80     envir   =>  $envir,
   81     searchList  =>  [{url=>'foo',dir=>'.'}],   # for subclasses -> list of url/directories to search
   82     resourceList => {},
   83 
   84   };
   85   bless $self, $class;
   86   $self->initialize;
   87   $self->check_parameters;
   88   return $self;
   89 }
   90 
   91 # methods
   92 #     make_alias   -- outputs url and does what needs to be done
   93 #     normalize paths (remove extra precursors to the path)
   94 #     search directories for item
   95 #     make_links   -- in those cases where links need to be made
   96 #     create_files  -- e.g. when printing hardcopy
   97 #     dispatcher -- decides what needs to be done based on displayMode and file type
   98 #     alias_for_html
   99 #     alias_for_image_in_html   image includes gif, png, jpg, swf, svg, flv?? ogg??, js
  100 #     alias_for_image_in_tex
  101 
  102 
  103 sub initialize {
  104   my $self = shift;
  105   my $envir = $self->{envir};
  106   # warn "envir-- ", join(" ", %$envir);
  107   $self->{fileName}            = $envir->{probFileName};
  108   $self->{htmlDirectory}       = $envir->{htmlDirectory};
  109   $self->{htmlURL}             = $envir->{htmlURL};
  110   $self->{tempDirectory}       = $envir->{tempDirectory};
  111   $self->{templateDirectory}   = $envir->{templateDirectory};
  112   $self->{tempURL}             = $envir->{tempURL};
  113   $self->{studentLogin}        = $envir->{studentLogin};
  114   $self->{psvn}          = $envir->{psvn};
  115   $self->{setNumber}           = $envir->{setNumber};
  116   $self->{probNum}             = $envir->{probNum};
  117   $self->{displayMode}         = $envir->{displayMode};
  118   $self->{externalGif2EpsPath} = $envir->{externalGif2EpsPath};
  119   $self->{externalPng2EpsPath} = $envir->{externalPng2EpsPath};
  120 
  121   $self->{appletPath} = $self->{envir}->{pgDirectories}->{appletPath};
  122   #
  123   #  Find auxiliary files even when the main file is in tempates/tmpEdit
  124   #
  125   $self->{fileName} =~ s!(^|/)tmpEdit/!$1!;
  126 
  127   $self->{ext}      = "";
  128 
  129   # create uniqeID stub    "gif/uniqIDstub-filePath"
  130         #  Make file names work in Library Browser when the images in several
  131         #  files have the same names.
  132         my $libFix = "";
  133         if ($self->{setNumber} eq "Undefined_Set") {
  134           $libFix = $self->{fileName};
  135           $libFix =~ s!.*/!!, $libFix =~ s!\.pg(\..*)?$!!;
  136           $libFix =~ s![^a-zA-Z0-9._-]!!g;
  137           $libFix .= '-';
  138         }
  139   my $uniqIDstub = join("-",
  140                  $self->{studentLogin},
  141                  $self->{psvn},
  142                  'set'.$self->{setNumber},
  143                  'prob'.$self->{probNum},
  144                  $libFix,
  145   );
  146   $uniqIDstub =~ tr/@.,/___/;  # replace @ . and , by _ since they don't work well in file names
  147   $self->{uniqIDstub} = $uniqIDstub;
  148 
  149 }
  150 
  151 sub check_parameters {
  152   my $self = shift;
  153 
  154   # problem specific data
  155   warn "The path to the current problem file template is not defined."     unless $self->{fileName};
  156   warn "The current studentLogin is not defined "                          unless $self->{studentLogin};
  157   warn "The current problem set number is not defined"                     if $self->{setNumber} eq ""; # allow for sets equal to 0
  158   warn "The current problem number is not defined"                         if $self->{probNum} eq "";
  159   warn "The current problem set version number (psvn) is not defined"      unless defined($self->{psvn});
  160   warn "The displayMode is not defined"                                    unless $self->{displayMode};
  161 
  162   # required macros
  163 # warn "The macro &surePathToTmpFile can't be found"                    unless defined(&{$self->surePathToTmpFile()} );
  164 # warn "The macro &convertPath can't be found"                          unless defined(&{$self->convertPath()});
  165 # warn "The macro &directoryFromPath can't be found"                    unless defined(&{$self->directoryFromPath()});
  166 #    warn $self->surePathToTmpFile("foo");
  167   # warn "The webwork server does not have permission to execute the gif2eps script at  ${externalGif2EpsPath}." unless ( -x "${externalGif2EpsPath}" );
  168   # warn "The webwork server does not have permission to execute the png2eps script at ${externalPng2EpsPath}." unless ( -x "${externalPng2EpsPath}" );
  169 
  170   # required directory addresses (and URL address)
  171   warn "htmlDirectory is not defined." unless $self->{htmlDirectory};
  172   warn "htmlURL is not defined." unless $self->{htmlURL};
  173   warn "tempURL is not defined." unless $self->{tempURL};
  174 }
  175 
  176 sub make_alias {
  177     my $self = shift;
  178     # input is a path to the original auxiliary file
  179     my $aux_file_path = shift @_;
  180     my $resource_alias = new PGresource($aux_file_path);  # just call it alias? FIXME -- not in use yet.
  181 
  182   warn "Empty string used as input into the function alias" unless $aux_file_path;
  183 
  184   my $displayMode         = $self->{displayMode};
  185     my $fileName            = $self->{fileName};    # name of .pg file
  186   my $envir               = $self->{envir};
  187   my $htmlDirectory       = $envir->{htmlDirectory};
  188   my $htmlURL             = $envir->{htmlURL};
  189   my $tempDirectory       = $envir->{tempDirectory};
  190   my $tempURL             = $envir->{tempURL};
  191   my $studentLogin        = $envir->{studentLogin};
  192   my $psvn          = $envir->{psvn};
  193   my $setNumber           = $envir->{setNumber};
  194   my $probNum             = $envir->{probNum};
  195     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  196     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  197 
  198     my $templateDirectory   = $self->{templateDirectory};
  199 
  200   # $adr_output is a url in HTML and Latex2HTML modes
  201   # and a complete path in TEX mode.
  202   my $adr_output;
  203   my $ext;
  204 
  205 #######################################################################
  206   # determine file type
  207   # determine display mode
  208   # dispatch
  209 #######################################################################
  210   # determine extension, if there is one
  211   # if extension exists, strip and use the value for $ext
  212   # files without extensions are considered to be picture files:
  213 
  214 
  215   if ($aux_file_path =~ s/\.([^\.]*)$// ) {
  216     $ext = $1;
  217   } else {
  218     warn "This file name $aux_file_path did not have an extension.<BR> " .
  219          "Every file name used as an argument to alias must have an extension.<BR> " .
  220          "The permissable extensions are .gif, .png, and .html .<BR>";
  221     $ext  = "gif";
  222   }
  223 
  224 
  225   # in order to facilitate maintenance of this macro the routines for handling
  226   # different file types are defined separately.  This involves some redundancy
  227   # in the code but it makes it easier to define special handling for a new file
  228   # type, (but harder to change the behavior for all of the file types at once
  229   # (sigh)  ).
  230 
  231   if ($ext eq 'html') {
  232      $adr_output = $self->alias_for_html($aux_file_path)
  233   } elsif ($ext eq 'gif') {
  234     if ( $displayMode eq 'HTML_MathJax'||
  235          $displayMode eq 'HTML_dpng'||
  236          $displayMode eq 'HTML' ||
  237          $displayMode eq 'HTML_tth'||
  238          $displayMode eq 'HTML_asciimath'||
  239          $displayMode eq 'HTML_LaTeXMathML'||
  240          $displayMode eq 'HTML_jsMath'||
  241          $displayMode eq 'HTML_img') {
  242       ################################################################################
  243       # .gif FILES in HTML; HTML_tth; HTML_dpng; HTML_img; and Latex2HTML modes
  244       ################################################################################
  245       $adr_output=$self->alias_for_gif_in_html_mode($aux_file_path);
  246 
  247     } elsif ($displayMode eq 'TeX') {
  248       ################################################################################
  249       # .gif FILES in TeX mode
  250       ################################################################################
  251             $adr_output=$self->alias_for_gif_in_tex_mode($aux_file_path);
  252 
  253     } else {
  254       die "Error in alias: dangerousMacros.pl: unrecognizable displayMode = $displayMode";
  255     }
  256   } elsif ($ext eq 'png') {
  257     if ( $displayMode eq 'HTML_MathJax'||
  258          $displayMode eq 'HTML_dpng'||
  259          $displayMode eq 'HTML' ||
  260          $displayMode eq 'HTML_tth'||
  261          $displayMode eq 'HTML_asciimath'||
  262          $displayMode eq 'HTML_LaTeXMathML'||
  263          $displayMode eq 'HTML_jsMath'||
  264          $displayMode eq 'HTML_img' )  {
  265         $adr_output = $self->alias_for_png_in_html_mode($aux_file_path);
  266     } elsif ($displayMode eq 'TeX') {
  267       $adr_output = $self->alias_for_png_in_tex_mode($aux_file_path);
  268 
  269     } else {
  270       warn  "Error in alias: dangerousMacros.pl","unrecognizable displayMode = $displayMode","";
  271     }
  272   } else { # $ext is not recognized
  273     ################################################################################
  274     # FILES  with unrecognized file extensions in any display modes
  275     ################################################################################
  276 
  277     warn "Error in the macro alias. Alias does not understand how to process files with extension $ext.  (Path ot problem file is  $fileName) ";
  278   }
  279 
  280   warn "The macro alias was unable to form a URL for some auxiliary file used in this problem." unless $adr_output;
  281   return $adr_output;
  282 }
  283 
  284 
  285 
  286 sub alias_for_html {
  287   my $self = shift;
  288   my $aux_file_path = shift;
  289     # warn "aux_file for html $aux_file_path";
  290   my $envir               = $self->{envir};
  291   my $fileName            = $self->{fileName};
  292   my $htmlDirectory       = $envir->{htmlDirectory};
  293   my $htmlURL             = $envir->{htmlURL};
  294   my $tempDirectory       = $envir->{tempDirectory};
  295   my $tempURL             = $envir->{tempURL};
  296   my $studentLogin        = $envir->{studentLogin};
  297   my $psvn          = $envir->{psvn};
  298   my $setNumber           = $envir->{setNumber};
  299   my $probNum             = $envir->{probNum};
  300   my $displayMode         = $envir->{displayMode};
  301     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  302     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  303 
  304     my $templateDirectory   = $self->{templateDirectory};
  305 
  306 
  307   # $adr_output is a url in HTML and Latex2HTML modes
  308   # and a complete path in TEX mode.
  309   my $adr_output;
  310   my $ext                 =   "html";
  311     ################################################################################
  312     # .html FILES in HTML, HTML_tth, HTML_dpng, HTML_img, etc. and Latex2HTML mode
  313     ################################################################################
  314 
  315     # No changes are made for auxiliary files in the
  316     # ${Global::htmlDirectory} subtree.
  317     if ( $aux_file_path =~ m|^$tempDirectory| ) {
  318       $adr_output = $aux_file_path,
  319       $adr_output =~ s|$tempDirectory|$tempURL/|,
  320       $adr_output .= ".$ext",
  321     } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  322       $adr_output = $aux_file_path,
  323       $adr_output =~ s|$htmlDirectory|$htmlURL|,
  324       $adr_output .= ".$ext",
  325     } else {
  326       # HTML files not in the htmlDirectory are assumed under live under the
  327       # templateDirectory in the same directory as the problem.
  328       # Create an alias file (link) in the directory html/tmp/html which
  329       # points to the original file and return the URL of this alias.
  330       # Create all of the subdirectories of html/tmp/html which are needed
  331       # using sure file to path.
  332 
  333       # $fileName is obtained from environment for PGeval
  334       # it gives the  full path to the current problem
  335       my $filePath = $self->directoryFromPath($fileName);
  336       my $htmlFileSource = $self->convertPath("$templateDirectory${filePath}$aux_file_path.html");
  337       my $link = "html/".$self->{uniqIDstub}."-$aux_file_path.$ext";
  338       my $linkPath = $self->surePathToTmpFile($link);
  339       $adr_output = "${tempURL}$link";
  340       if (-e $htmlFileSource) {
  341         if (-e $linkPath) {
  342           unlink($linkPath) || warn "Unable to unlink alias file at |$linkPath|";
  343           # destroy the old link.
  344         }
  345         symlink( $htmlFileSource, $linkPath)
  346               || warn "The macro alias cannot create a link from |$linkPath|  to |$htmlFileSource| <BR>" ;
  347       } else {
  348         warn("The macro alias cannot find an HTML file at: |$htmlFileSource|");
  349       }
  350     }
  351   $adr_output;
  352 }
  353 
  354 
  355 sub alias_for_gif_in_html_mode {
  356   my $self = shift;
  357   my $aux_file_path = shift;
  358 #    warn "entering alias_for_gif_in_html_mode $aux_file_path";
  359 
  360   my $envir               = $self->{envir};
  361   my $fileName            = $self->{fileName};
  362   my $htmlDirectory       = $envir->{htmlDirectory};
  363   my $htmlURL             = $envir->{htmlURL};
  364   my $tempDirectory       = $envir->{tempDirectory};
  365   my $tempURL             = $envir->{tempURL};
  366   my $studentLogin        = $envir->{studentLogin};
  367   my $psvn          = $envir->{psvn};
  368   my $setNumber           = $envir->{setNumber};
  369   my $probNum             = $envir->{probNum};
  370   my $displayMode         = $envir->{displayMode};
  371     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  372     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  373 
  374     my $templateDirectory   = $self->{templateDirectory};
  375 
  376 
  377   # $adr_output is a url in HTML and Latex2HTML modes
  378   # and a complete path in TEX mode.
  379   my $adr_output;
  380   my $ext                 = "gif";
  381 
  382       ################################################################################
  383       # .gif FILES in HTML, HTML_tth, HTML_dpng, HTML_img, and Latex2HTML modes
  384       ################################################################################
  385 
  386       #warn "tempDirectory is $tempDirectory";
  387       #warn "file Path for auxiliary file is $aux_file_path";
  388 
  389       # No changes are made for auxiliary files in the htmlDirectory or in the tempDirectory subtree.
  390       if ( $aux_file_path =~ m|^$tempDirectory| ) {
  391         $adr_output = $aux_file_path;
  392         $adr_output =~ s|$tempDirectory|$tempURL|;
  393         $adr_output .= ".$ext";
  394         #warn "adress out is $adr_output",
  395       } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  396         $adr_output = $aux_file_path;
  397         $adr_output =~ s|$htmlDirectory|$htmlURL|;
  398         $adr_output .= ".$ext";
  399       } else {
  400         # files not in the htmlDirectory sub tree are assumed to live under the templateDirectory
  401         # subtree in the same directory as the problem.
  402 
  403         # For a gif file the alias macro creates an alias under the html/images directory
  404         # which points to the gif file in the problem directory.
  405         # All of the subdirectories of html/tmp/gif which are needed are also created.
  406       #warn "fileName is $fileName   $self";
  407         my $filePath = ( $self->directoryFromPath($fileName) );
  408           #warn "filePath is $filePath";
  409         # $fileName is obtained from environment for PGeval
  410         # it gives the full path to the current problem
  411         my $gifSourceFile = $self->convertPath("$templateDirectory${filePath}$aux_file_path.gif");
  412         #my $link = "gif/$studentLogin-$psvn-set$setNumber-prob$probNum-$aux_file_path.$ext";
  413        #warn "fileName is $fileName filePath is $filePath gifSourceFile is $gifSourceFile";
  414 
  415         #  Make file names work in Library Browser when the images in several
  416         #  files have the same names.
  417         my $libFix = "";
  418         if ($setNumber eq "Undefined_Set") {
  419           $libFix = $fileName;
  420           $libFix =~ s!.*/!!, $libFix =~ s!\.pg(\..*)?$!!;
  421           $libFix =~ s![^a-zA-Z0-9._-]!!g;
  422           $libFix .= '-';
  423         }
  424 
  425         # my $link = "gif/$setNumber-prob$probNum-$libFix$aux_file_path.$ext";
  426           my $uniqIDstub = $self->{uniqIDstub};
  427         my $link = "gif/${uniqIDstub}-$aux_file_path.$ext";
  428         my $linkPath = $self->surePathToTmpFile($link);
  429         $adr_output = "${tempURL}$link";
  430         #warn "linkPath is $linkPath";
  431         #warn "adr_output is $adr_output";
  432         if (-e $gifSourceFile) {
  433           if (-e $linkPath) {
  434             unlink($linkPath) || warn "Unable to unlink old alias file at $linkPath";
  435           }
  436           symlink($gifSourceFile, $linkPath)
  437             || warn "The macro alias cannot create a link from |$linkPath|  to |$gifSourceFile| <BR>" ;
  438         } else {
  439           warn("The macro alias cannot find a GIF file at: |$gifSourceFile|");
  440         }
  441       }
  442     $adr_output;
  443 }
  444 
  445 sub alias_for_gif_in_tex_mode {
  446   my $self = shift;
  447   my $aux_file_path = shift;
  448 
  449   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  450   my $htmlDirectory       = $envir->{htmlDirectory};
  451   my $htmlURL             = $envir->{htmlURL};
  452   my $tempDirectory       = $envir->{tempDirectory};
  453   my $tempURL             = $envir->{tempURL};
  454   my $studentLogin        = $envir->{studentLogin};
  455   my $psvn          = $envir->{psvn};
  456   my $setNumber           = $envir->{setNumber};
  457   my $probNum             = $envir->{probNum};
  458   my $displayMode         = $envir->{displayMode};
  459     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  460     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  461 
  462     my $templateDirectory   = $self->{templateDirectory};
  463 
  464 
  465   # $adr_output is a url in HTML and Latex2HTML modes
  466   # and a complete path in TEX mode.
  467   my $adr_output;
  468   my $ext                 = "gif";
  469       ################################################################################
  470       # .gif FILES in TeX mode
  471       ################################################################################
  472 
  473             $setNumber =~ s/\./_/g;  ## extra dots confuse latex's graphics package
  474       if ($envir->{texDisposition} eq "pdf") {
  475         # We're going to create PDF files with our TeX (using pdflatex); so we
  476         # need images in PNG format.
  477 
  478         my $gifFilePath;
  479 
  480         if ($aux_file_path =~ m/^$htmlDirectory/ or $aux_file_path =~ m/^$tempDirectory/) {
  481           # we've got a full pathname to a file
  482           $gifFilePath = "$aux_file_path.gif";
  483         } else {
  484           # we assume the file is in the same directory as the problem source file
  485           my $dir = $self->directoryFromPath($fileName);
  486           $gifFilePath = "$templateDirectory${dir}$aux_file_path.gif";
  487         }
  488 
  489         my $gifFileName = $self->fileFromPath($gifFilePath);
  490         $gifFileName =~ /^(.*)\.gif$/;
  491           my $pngFilePath = $self->surePathToTmpFile("${tempDirectory}png/$setNumber-$probNum-$1.png");
  492         my $command = $envir->{externalGif2PngPath};
  493         my $returnCode = system "cat $gifFilePath | $command > $pngFilePath";
  494       #warn "FILE path $pngFilePath  exists =", -e $pngFilePath;
  495         if ($returnCode or not -e $pngFilePath) {
  496           warn "returnCode $returnCode: failed to convert $gifFilePath to $pngFilePath using gif->png with $command: $!";
  497         }
  498 
  499         $adr_output = $pngFilePath;
  500       } else {
  501         # Since we're not creating PDF files; we're probably just using a plain
  502         # vanilla latex. Hence; we need EPS images.
  503 
  504         ################################################################################
  505         # This is statement used below is system dependent.
  506         # Notice that the range of colors is restricted when converting to postscript to keep the files small
  507         # "cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmtops -noturn > $adr_output"
  508         # "cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn > $adr_output"
  509         ################################################################################
  510         if ($aux_file_path =~  m|^$htmlDirectory|  or $aux_file_path =~  m|^$tempDirectory|)  {
  511           # To serve an eps file copy an eps version of the gif file to the subdirectory of eps/
  512           my $linkPath = $self->directoryFromPath($fileName);
  513 
  514           my $gifSourceFile = "$aux_file_path.gif";
  515           my $gifFileName = $self->fileFromPath($gifSourceFile);
  516           $adr_output = $self->surePathToTmpFile("$tempDirectory/eps/$studentLogin-$psvn-$gifFileName.eps") ;
  517 
  518           if (-e $gifSourceFile) {
  519             #system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output")
  520             system("cat $gifSourceFile | ${externalGif2EpsPath} > $adr_output" )
  521               && die "Unable to create eps file:\n |$adr_output| from file\n |$gifSourceFile|\n in problem $probNum " .
  522                      "using the system dependent script\n |${externalGif2EpsPath}| \n";
  523           } else {
  524             die "|$gifSourceFile| cannot be found.  Problem number: |$probNum|";
  525           }
  526         } else {
  527           # To serve an eps file copy an eps version of the gif file to  a subdirectory of eps/
  528           my $filePath = $self->directoryFromPath($fileName);
  529           my $gifSourceFile = "${templateDirectory}${filePath}$aux_file_path.gif";
  530           #print "content-type: text/plain \n\nfileName = $fileName and aux_file_path =$aux_file_path<BR>";
  531           $adr_output = $self->surePathToTmpFile("eps/$studentLogin-$psvn-set$setNumber-prob$probNum-$aux_file_path.eps");
  532 
  533           if (-e $gifSourceFile) {
  534             #system("cat $gifSourceFile  | /usr/math/bin/giftopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  535             #warn "Unable to create eps file: |$adr_output|\n from file\n |$gifSourceFile|\n in problem $probNum";
  536             #warn "Help ${:externalGif2EpsPath}" unless -x "${main::externalGif2EpsPath}";
  537             system("cat $gifSourceFile | ${externalGif2EpsPath} > $adr_output" )
  538               && die "Unable to create eps file:\n |$adr_output| from file\n |$gifSourceFile|\n in problem $probNum " .
  539                      "using the system dependent commands \n |${externalGif2EpsPath}| \n ";
  540           }  else {
  541             die "|$gifSourceFile| cannot be found.  Problem number: |$probNum|";
  542           }
  543         }
  544       }
  545   $adr_output;
  546 
  547 }
  548 sub alias_for_png_in_html_mode {
  549   my $self = shift;
  550   my $aux_file_path = shift;
  551 
  552   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  553   my $htmlDirectory       = $envir->{htmlDirectory};
  554   my $htmlURL             = $envir->{htmlURL};
  555   my $tempDirectory       = $envir->{tempDirectory};
  556   my $tempURL             = $envir->{tempURL};
  557   my $studentLogin        = $envir->{studentLogin};
  558   my $psvn          = $envir->{psvn};
  559   my $setNumber           = $envir->{setNumber};
  560   my $probNum             = $envir->{probNum};
  561   my $displayMode         = $envir->{displayMode};
  562     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  563     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  564 
  565     my $templateDirectory   = $self->{templateDirectory};
  566 
  567 
  568   # $adr_output is a url in HTML and Latex2HTML modes
  569   # and a complete path in TEX mode.
  570   my $adr_output;
  571   my $ext                 = "png";
  572       ################################################################################
  573       # .png FILES in HTML; HTML_tth; HTML_dpng; HTML_img; etc. and Latex2HTML modes
  574       ################################################################################
  575 
  576       #warn "tempDirectory is $tempDirectory";
  577       #warn "file Path for auxiliary file is $aux_file_path";
  578 
  579       # No changes are made for auxiliary files in the htmlDirectory or in the tempDirectory subtree.
  580       if ( $aux_file_path =~ m|^$tempDirectory| ) {
  581       $adr_output = $aux_file_path;
  582         $adr_output =~ s|$tempDirectory|$tempURL|;
  583         $adr_output .= ".$ext";
  584         #warn "adress out is $adr_output";
  585       } elsif ($aux_file_path =~ m|^$htmlDirectory| ) {
  586         $adr_output = $aux_file_path;
  587         $adr_output =~ s|$htmlDirectory|$htmlURL|;
  588         $adr_output .= ".$ext";
  589       } else {
  590         # files not in the htmlDirectory sub tree are assumed to live under the templateDirectory
  591         # subtree in the same directory as the problem.
  592 
  593         # For a png file the alias macro creates an alias under the html/images directory
  594         # which points to the png file in the problem directory.
  595         # All of the subdirectories of html/tmp/gif which are needed are also created.
  596         my $filePath = $self->directoryFromPath($fileName);
  597 
  598         # $fileName is obtained from environment for PGeval
  599         # it gives the full path to the current problem
  600         my $pngSourceFile = $self->convertPath("$templateDirectory${filePath}$aux_file_path.png");
  601         my $uniqIDstub = $self->{uniqIDstub};
  602         my $link = "gif/${uniqIDstub}-$aux_file_path.$ext";
  603         my $linkPath = $self->surePathToTmpFile($link);
  604         $adr_output = "${tempURL}$link";
  605         #warn "linkPath is $linkPath";
  606         #warn "adr_output is $adr_output";
  607         if (-e $pngSourceFile) {
  608           if (-e $linkPath) {
  609             unlink($linkPath) || warn "Unable to unlink old alias file at $linkPath";
  610           }
  611           symlink($pngSourceFile, $linkPath)
  612           || warn "The macro alias cannot create a link from |$linkPath|  to |$pngSourceFile| <BR>" ;
  613         } else {
  614           warn("The macro alias cannot find a PNG file at: |$pngSourceFile|");
  615         }
  616       }
  617   $adr_output;
  618 
  619 }
  620 
  621 sub alias_for_png_in_tex_mode {
  622 
  623     my $self = shift;
  624   my $aux_file_path = shift;
  625 
  626   my $envir               = $self->{envir};   my $fileName            = $envir->{fileName};
  627   my $htmlDirectory       = $envir->{htmlDirectory};
  628   my $htmlURL             = $envir->{htmlURL};
  629   my $tempDirectory       = $envir->{tempDirectory};
  630   my $tempURL             = $envir->{tempURL};
  631   my $studentLogin        = $envir->{studentLogin};
  632   my $psvn          = $envir->{psvn};
  633   my $setNumber           = $envir->{setNumber};
  634   my $probNum             = $envir->{probNum};
  635   my $displayMode         = $envir->{displayMode};
  636     my $externalGif2EpsPath = $envir->{externalGif2EpsPath};
  637     my $externalPng2EpsPath = $envir->{externalPng2EpsPath};
  638 
  639     my $templateDirectory   = $self->{templateDirectory};
  640 
  641 
  642   # $adr_output is a url in HTML and Latex2HTML modes
  643   # and a complete path in TEX mode.
  644   my $adr_output;
  645   my $ext                 = "png";
  646             ################################################################################
  647       # .png FILES in TeX mode
  648       ################################################################################
  649 
  650             $setNumber =~ s/\./_/g;  ## extra dots confuse latex's graphics package
  651       if ($envir->{texDisposition} eq "pdf") {
  652         # We're going to create PDF files with our TeX (using pdflatex); so we
  653         # need images in PNG format. what luck! they're already in PDF format!
  654 
  655         my $pngFilePath;
  656 
  657         if ($aux_file_path =~ m/^$htmlDirectory/ or $aux_file_path =~ m/^$tempDirectory/) {
  658           # we've got a full pathname to a file
  659           $pngFilePath = "$aux_file_path.png";
  660         } else {
  661           # we assume the file is in the same directory as the problem source file
  662           my $dir = $self->directoryFromPath($fileName);
  663           $pngFilePath = "$templateDirectory${dir}$aux_file_path.png";
  664         }
  665 
  666         $adr_output = $pngFilePath;
  667       } else {
  668         # Since we're not creating PDF files; we're probably just using a plain
  669         # vanilla latex. Hence; we need EPS images.
  670 
  671         ################################################################################
  672         # This is statement used below is system dependent.
  673         # Notice that the range of colors is restricted when converting to postscript to keep the files small
  674         # "cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmtops -noturn > $adr_output"
  675         # "cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn > $adr_output"
  676         ################################################################################
  677 
  678         if ($aux_file_path =~  m|^$htmlDirectory|  or $aux_file_path =~  m|^$tempDirectory|)  {
  679           # To serve an eps file copy an eps version of the png file to the subdirectory of eps/
  680           my $linkPath = $self->directoryFromPath($fileName);
  681 
  682           my $pngSourceFile = "$aux_file_path.png";
  683           my $pngFileName = fileFromPath($pngSourceFile);
  684           $adr_output = $self->surePathToTmpFile("$tempDirectory/eps/$studentLogin-$psvn-$pngFileName.eps") ;
  685 
  686           if (-e $pngSourceFile) {
  687             #system("cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output")
  688             system("cat $pngSourceFile | ${externalPng2EpsPath} > $adr_output" )
  689               && die "Unable to create eps file:\n |$adr_output| from file\n |$pngSourceFile|\n in problem $probNum " .
  690                      "using the system dependent commands\n |${externalPng2EpsPath}| \n";
  691           } else {
  692             die "|$pngSourceFile| cannot be found.  Problem number: |$probNum|";
  693           }
  694         } else {
  695           # To serve an eps file copy an eps version of the png file to  a subdirectory of eps/
  696           my $filePath = $self->directoryFromPath($fileName);
  697           my $pngSourceFile = "${templateDirectory}${filePath}$aux_file_path.png";
  698           #print "content-type: text/plain \n\nfileName = $fileName and aux_file_path =$aux_file_path<BR>";
  699           $adr_output = $self->surePathToTmpFile("eps/$studentLogin-$psvn-set$setNumber-prob$probNum-$aux_file_path.eps") ;
  700           if (-e $pngSourceFile) {
  701             #system("cat $pngSourceFile  | /usr/math/bin/pngtopnm | /usr/math/bin/pnmdepth 1 | /usr/math/bin/pnmtops -noturn>$adr_output") &&
  702             #warn "Unable to create eps file: |$adr_output|\n from file\n |$pngSourceFile|\n in problem $probNum";
  703             #warn "Help ${externalPng2EpsPath}" unless -x "${externalPng2EpsPath}";
  704             system("cat $pngSourceFile | ${externalPng2EpsPath} > $adr_output" )
  705               && die "Unable to create eps file:\n |$adr_output| from file\n |$pngSourceFile|\n in problem $probNum " .
  706                      "using the system dependent commands\n |${externalPng2EpsPath}| \n ";
  707           } else {
  708             die "|$pngSourceFile| cannot be found.  Problem number: |$probNum|";
  709           }
  710         }
  711       }
  712   $adr_output;
  713 
  714 }
  715 
  716 ################################################
  717 
  718 # More resource search macros
  719 
  720 ################################################
  721 
  722 #
  723 #  Look for a macro file in the directories specified in the macros path
  724 #
  725 
  726 # ^variable my $macrosPath
  727 our ($macrosPath,
  728     # ^variable my $pwd
  729     $pwd,
  730     # ^variable my $appletPath
  731     $appletPath,
  732     # ^variable my $server_root_url
  733     $server_root_url,
  734   # ^variable my $templateDirectory
  735   $templateDirectory,
  736   # ^variable my $scriptDirectory
  737   $scriptDirectory,
  738   # ^variable my $externalTTHPath
  739   $externalTTHPath,
  740   );
  741 
  742 # ^function findMacroFile
  743 # ^uses $macrosPath
  744 # ^uses $pwd
  745 sub findMacroFile {
  746   my $self   = shift;
  747   my $fileName = shift;
  748   my $filePath;
  749   foreach my $dir (@{$macrosPath}) {
  750     $filePath = "$dir/$fileName";
  751     $filePath =~ s!^\.\.?/!$pwd/!;
  752     return $filePath if (-r $filePath);
  753   }
  754   return;  # no file found
  755 }
  756 
  757 # ^function check_url
  758 # ^uses %envir
  759 sub check_url {
  760   my $self = shift;
  761   my $url  = shift;
  762   my $OK_CONSTANT = "200 OK";
  763   return undef if $url =~ /;/;   # make sure we can't get a second command in the url
  764   #FIXME -- check for other exploits of the system call
  765   #FIXME -- ALARM feature so that the response cannot be held up for too long.
  766   #FIXME doesn't seem to work with relative addresses.
  767   #FIXME  Can we get the machine name of the server?
  768 
  769    my $check_url_command = $self->{envir}->{externalCheckUrl};
  770    my $response = `$check_url_command $url`;
  771   return ($response =~ /^$OK_CONSTANT/) ? 1 : 0;
  772 }
  773 
  774 # ^variable our %appletCodebaseLocations
  775 
  776 # ^function findAppletCodebase
  777 # ^uses %appletCodebaseLocations
  778 # ^uses $appletPath
  779 # ^uses $server_root_url
  780 # ^uses check_url
  781 
  782 our %appletCodebaseLocations = ();   # cache for found applets (lasts until the child exits
  783 sub findAppletCodebase {
  784   my $self     = shift;
  785   my $fileName = shift;  # probably the name of a jar file
  786   $server_root_url=$self->envir("server_root_url");
  787   #check cache first
  788   if (defined($appletCodebaseLocations{$fileName})
  789         and $appletCodebaseLocations{$fileName} =~/\S/  )
  790   {
  791       return $appletCodebaseLocations{$fileName}; # return if found in cache
  792   }
  793   my $appletPath = $self->{appletPath};
  794   foreach my $appletLocation (@{$appletPath}) {
  795     if ($appletLocation =~ m|^/|) {
  796       $appletLocation = "$server_root_url$appletLocation";
  797     }
  798     my $url = "$appletLocation/$fileName";
  799 
  800     if ($self->check_url($url)) {
  801         $appletCodebaseLocations{$fileName} = $appletLocation; #update cache
  802       return $appletLocation   # return codebase part of url
  803     }
  804   }
  805   warn "findAppletCodebase Error: $fileName not found after searching ". join(",  ", @{$appletPath} );
  806   return "";
  807 }
  808 
  809 
  810 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9