Extension:FormelApplet

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
FormelApplet

Release status: beta

Implementation Tag
Description Add FormelApplet to MediaWiki pages
Author(s) R. Großmann (BigvirTalk)
Last version 1.2a
MediaWiki tested with 1.12.0
(may run with older versions)
License No license specified
Download see below
Hooks used
ParserFirstCallInit

Check usage (experimental)

Contents

[edit] What can this extension do?

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.

[edit] Usage

[edit] Add a FormelApplet tag to your MediaWiki page

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

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

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

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

[edit] Download instructions

Go to the $IP/extensions directory and create a subdirectory named FormelApplet. Please cut and paste the code found below and place it in $IP/extensions/FormelApplet/FormelApplet.php.

If you want to use the parameter useLocalJar="true", download the JAR file gf03.jar from here [[1]] and copy it to $IP/extensions/FormelApplet/gf03.jar.

This is comfortable, if you use a locally installed Wiki (using XAMPP, for instance) without connection to the internet.

Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

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

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

[edit] Code

<?php
/**
 * FormelApplet extension
 *
 * @author Rudolf Grossmann
 * @version 1.2a
 */
 
$fa_version = "1.2a";
 
// 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
);
 
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
 
        $error_message = "no error"; //will be overwritten, if error occurs
        $debug = 'Debug: ';
        $CRLF = "\r\n";
        $quot='"';
        $appletBinary = "gf03.jar" ;
        $codeBase = "http://www.formelapplet.de/classes/";
 
        // Special parameters, not for parameter (name - value) tags. Use lowercase for sake of comparison!
        $special_parameters = array('width', 'height', 'solution', 'term', 'uselocaljar', 'substimage', 'name', 'debug');
        $noJavaText = 'Please <a href="http://java.sun.com/getjava">install Java</a> to use this page.';        
 
        //Look for parameter 'useLocalJar'. Will be overwritten with 'true', if parameter 'filename is used'
        $useLocalJar = isset($args['uselocaljar']) ? $args['uselocaljar'] : '';
        $printDebug = isset($args['debug']) ? $args['debug'] : 'false';
 
        // Look for required parameters width, height,  term/solution
        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,...)
        if ( $args['uselocaljar'] == 'true' ) { 
         // If use of local JAR is wanted (e.g. for testing purposes)
         // The following line is code from http://code.activestate.com/recipes/576595/   "A more reliable DOCUMENT_ROOT"
         $docroot = realpath((getenv('DOCUMENT_ROOT') && ereg('^'.preg_quote(realpath(getenv('DOCUMENT_ROOT'))), realpath(__FILE__))) ? getenv('DOCUMENT_ROOT') : str_replace(dirname(@$_SERVER['PHP_SELF']), '', str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__))));
         $delta = substr(dirname(__FILE__), strlen($docroot));
         $codeBase = $wgServer . $delta;
         # replace backslash by slash
         $codeBase=str_replace('\\','/',$codeBase); 
         # add slash at ending
         if (substr($codeBase, strlen($codeBase)-1) != '/') {
           $codeBase = $codeBase . "/";
         }
       }
 
        $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;
          $debug .= '<p>Parameter solution/term contains filename: ' . $filename . '</p>' . $CRLF;
          $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 ($error_message == 'no error') {
        // Assemble the applet tag
            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  non-special parameters
            foreach($args as $par_name => $par_value) {
              if (! in_array(strtolower($par_name), $special_parameters)){
                $parameter = htmlspecialchars(strip_tags($par_name));
                $value = htmlspecialchars(strip_tags($par_value));
                $debug .= '<p>Allgemein: ' . $par_name . ' => ' . $par_value . '</p>' . $CRLF;
                if(strlen($value) > 0) {
                   $output .= '<param name="' . $parameter .'" value="' . $value . '">' . $CRLF;
                }
              }
            }
            $output .= '<param name="java_arguments" value="-Djnlp.packEnabled=true">'. $CRLF; // Use packed JAR if available
            // Close applet tag
            $output .= $noJavaText . $CRLF; // Message if Java is not installed
            $output .= "</applet>" . $CRLF; // The closing applet tag
        } else {
          $output = "<p>Error in MediaWiki extension (FormelApplet.php): <em>" . $error_message. "</em></p>" . $CRLF;
        }
        if ($printDebug == 'true') {
           $output .= '<p>' . $debug . '</p>' . $CRLF;
        }
        // Send the output to the browser
        return $output;
} // missing php end tag to avoid troubles.

[edit] Change from 1.1b to 1.2a

  • Function url_exists() was deleted, because it seemed to be not reliable in some cases. If gf03.jar is not found at www.formelapplet.de, the browser (or the Java plugin) will generate an error message. (In version 1.1b the error message was generated by the PHP code of the extension.)

[edit] See also

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox