[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 6261 - (download) (as text) (annotate)
Sat May 15 18:41:23 2010 UTC (9 years, 6 months ago) by gage
File size: 32595 byte(s)
added fixes to PGalias.pm and PGcore.pm related to using $self-> in contexts
where the binding was not as expected (e.g.  in 'blah'. $self->{foobar} .'blah' )

Other minor fixes and improvements.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9