Extension:FormelApplet/1.0n

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
FormelApplet
Release status: beta
Implementation Tag
Description Add FormelApplet to MediaWiki pages
Author(s) R. Großmann (Bigvirtalk)
Latest version 1.0n
MediaWiki tested with 1.12.0
(may run with older versions)
License No license specified
Download see below
Hooks used
ParserFirstCallInit
Translate the FormelApplet extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?[edit]

Add a FormelApplet to your MediaWiki page.

The status of this extension has changed from experimental to beta, because FormelApplet now is successfully in use by other wikis. I am waiting for error reports.

Usage[edit]

Add a FormelApplet tag to your MediaWiki page[edit]

The following tag will add a FormelApplet to your MediaWiki page:

Use parameter 'term' to get a FormelApplet for output. Use parameter 'solution' to get an FormelApplet for input.

Examples[edit]

You may pass the term to be rendered directly as a parameter like this:

<formelapplet width="600" height="300" term="ZIP-504b0304140008000800f8587f3600...(many bytes)...010038000000840000000000" />

Alternatively you may put the content of the term parameter into a file "myFile.fas", upload the fas-file to your MediaWiki and pass the filename to the applet using the term parameter like this:

<formelapplet width="600" height="300" term="myFile.fas" />

Use parameter 'solution' to get an FormelApplet for input (passing solution using a *.fas-file).

<formelapplet width="600" height="300" solution="myFile.fas" />

You can also pass other FormelApplet parameters. Example (smaller font size):

<formelapplet width="600" height="300" solution="myFile.fas" RootFontsize="12" />

How to create a term/solution parameter[edit]

see below

Download instructions[edit]

For sake of simplicity, this extension consists of only one file.

Please cut and paste the code found below and place it in $IP/extensions/FormelApplet.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit]

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/FormelApplet.php");

Allow uploads of fas-files[edit]

You will want to allow uploads of FormelApplet-files (*.fas) containing terms or solutions. This can be done by adding the following two lines to LocalSettings.php:

$wgFileExtensions[] = 'fas';
$wgVerifyMimeType = false;

Please tell me if you know how to swich off MimeType checking only for certain file extensions.

Learn more about upload of media files.

Code[edit]

<?php
/**
 * FormelApplet extension
 *
 * @author Rudolf Grossmann
 * @version 1.0n
 */
 
$fa_version = "1.0n";
 
// This MediaWiki extension is based on the Java Applet extension by Phil Trasatti
// see: http://www.mediawiki.org/wiki/Extension:Java_Applet
 
//Avoid unstubbing $wgParser too early on modern (1.12+) MW versions, as per r35980
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
        $wgHooks['ParserFirstCallInit'][] = 'fa_AppletSetup';
} else {
        $wgExtensionFunctions[] = 'fa_AppletSetup';
}
 
$wgExtensionCredits['parserhook'][] = array(
        'name'         => 'FormelApplet',
        'author'       => 'Rudolf Grossmann',
        'url'          => 'http://www.mediawiki.org/wiki/Extension:FormelApplet',
        'description'  => 'Add [http://www.formelapplet.de FormelApplets] to MediaWiki pages.',
        'version'      => $fa_version
);

//check if an external file exists, code from http://www.webdigity.com/index.php/topic,7148
if (! function_exists('url_exists')){
   function url_exists($url) {
        $url = str_replace(" ", "%20", $url);
        $a_url = parse_url($url);
        if (!isset($a_url['port'])) $a_url['port'] = 80;
        $errno = 0;
        $errstr = ''; 
        $timeout = 15;
        if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){
                $fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
                if (!$fid) return false;
                $page = isset($a_url['path'])  ?$a_url['path']:'';
                $page .= isset($a_url['query'])?'?'.$a_url['query']:'';
                fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
                $head = fread($fid, 4096); 
                fclose($fid);
                return preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head);
        } else {
                return false;
        }
   }
}
 
function fa_AppletSetup() {
        global $wgParser;
        $wgParser->setHook( 'formelapplet', 'get_fa_AppletOutput' );
        return true;
}
 
function get_fa_AppletOutput( $input, $args, $parser ) {
        global $wgServer; // URL of the WIKI's server
        global $fa_version; // see line 9 of this file
 
        $appletBinary = "gf03.jar" ;
        $codeBase = "http://www.formelapplet.de/classes/";
 
        $error_message = "no error"; //will be overwritten, if error occurs
        $CRLF = "\r\n";
        $quot='"';
 
        $parameter_array = array("GlobalFontColor", "FontColor", "GlobalOutputColor", "OutputColor", "GlobalCursorColor", "CursorColor");
        $parameter_array = array_merge($parameter_array, array("RootFontsize", "MinFontsize", "x_offset", "global_y_offset"));
        $parameter_array = array_merge($parameter_array, array("y_offset", "AlignRight", "debugpanel", "precision", "condition"));
        $parameter_array = array_merge($parameter_array, array("def0", "def1", "def2", "def3", "def4", "def5", "def6", "def7", "def8", "def9"));
        $parameter_array = array_merge($parameter_array, array("GlobalInputActiveColor", "InputActiveColor", "GlobalOKColor", "OKColor"));
        $parameter_array = array_merge($parameter_array, array("GlobalInputInactiveColor", "InputInactiveColor"));
        $parameter_array = array_merge($parameter_array, array("GlobalWrongColor", "WrongColor", "fontsize", "fontname", "AutoShrink"));
 
        $noJavaText = 'Please <a href="http://java.sun.com/getjava">install Java 1.4</a> (or later) to use this page.';
 
        // Look for required parameters
        if( !isset( $args['width'] )   ||
            !isset( $args['height'] )  ||
            !(isset( $args['solution'] ) || isset( $args['term'] ) ) )
            $error_message = "Missing parameter (width or height or term/solution)";
 
        // the following code is of use, if MediaWiki is installed on a local fileserver (wamp, server2go,...)
        $remoteFormelAppletJarExists = url_exists($codeBase . $appletBinary);
        if (( $args['uselocaljar'] == 'true' ) || ( ! $remoteFormelAppletJarExists)) { 
          // If use of local JAR is wanted (e.g. for testing purposes) or JAR is not available from internet:
          // Retrieve URL of uploaded JAR file.
          $appletBinary = ucfirst($appletBinary); // set first letter of jar-file upper case
          $file = Image::newFromName( $appletBinary ) ; // Get the Image object from the file name
          $fileURL = $file->getURL(); // Get the mediawiki path of the file
          $fileURL = str_replace( $appletBinary, "", $fileURL ); // Strip the filename from the path
          $codeBase = $wgServer . $fileURL; // Attach wiki site path
        }
 
        $output = "<!-- FormelApplet Mediawiki extension " . $fa_version ." by R. Grossmann -->" . $CRLF;  // Output the opening applet tag
 
        // Add code value to tag
        $is_inputapplet=false; //default
        if (isset( $args['solution'] )) {
           // MAYSCRIPT necessary for allowing applet to write Cookies. Cookies necessary for localization.
           $output = $output . "<applet MAYSCRIPT code=".$quot."gut.InputApplet".$quot;
           $solution_or_term = $args['solution'];
           $is_inputapplet = true;
        } else {
           $output = $output . "<applet MAYSCRIPT code=".$quot."gut.OutputApplet".$quot;
           $solution_or_term = $args['term'];
        }

        if (isset( $args['name'] )) {
           $output = $output . " name=" . $quot . htmlspecialchars(strip_tags($args['name'])) . $quot; // Add name value to tag
         }
 
        $output = $output . " codebase=" . $quot . $codeBase . $quot; // Add codebase value to tag
        $output = $output . " width=" . $quot . htmlspecialchars(strip_tags($args['width'])) . $quot; // Add width value to tag
        $output = $output . " height=" . $quot . htmlspecialchars(strip_tags($args['height'])) . $quot; // Add height value to tag
        $output = $output . " archive=" . $quot. $appletBinary . $quot. " >"; // Add archive value to tag
 
        $head = substr($solution_or_term, 0, 4);
        if (strtoupper($head)!='ZIP-') {
          //Magic head "ZIP-" not found. Value of parameter solution/term does not contain ZIP-file but filename.
          $filename = $solution_or_term;
          $image= Image::newFromName( $filename ) ; // Get file as Image object from the file name.
 
          // Get the MediaWiki path of the file
          if (isset( $image )) {
            $fileURL = $image->getURL();
            $pathAndFilename = $wgServer . $fileURL;
            $solution_or_term = file_get_contents($pathAndFilename);
            if (strlen($solution_or_term) < 4)
               $error = "File " . $filename . " not found.";
          } else {
             $error = "File " . $filename . " not found.";
          }
        }
 
 
        if ($is_inputapplet) {
          $output = $output . "<param name=" . $quot . "solution" . $quot;
        } else {
          $output = $output . "<param name=" . $quot . "term" . $quot;
        }
        $output = $output . " value=" . $quot . htmlspecialchars(strip_tags($solution_or_term)) . $quot . ">\n";
 
        // Add code for parameters, if exist!.
        foreach($parameter_array as $parameter) {
          $value = $args[strtolower($parameter)]; //strtolower necessary for $args [   ]
          $value = htmlspecialchars(strip_tags($value));
          if(strlen($value) > 0)
             $output = $output . '<param name="' . $parameter .'" value="' . $value . '">' . $CRLF;
        }
 
        $output = $output . $noJavaText . $CRLF; // Message if Java is not installed
        $output = $output . "</applet>" . $CRLF; // The closing applet tag
 
        // if error occurred, discard applet and output error message
        if ($error_message != "no error") {
          $output = "<p>Error in MediaWiki extension (FormelApplet.php): <em>" . $error_message. "</em></p>" . $CRLF;
        }
 
        // Send the output to the browser
        return $output;
} // missing php end tag to avoid troubles.

See also[edit]