Extension:GeoGebra/2.0p

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
GeoGebra
Release status: stable
Implementation Tag
Description Add GeoGebra applet to MediaWiki pages
Author(s) R. Großmann (Bigvirtalk)
Latest version 2.0p
MediaWiki tested with 1.12.0
(may run with older versions)
License No license specified
Download see below
Translate the GeoGebra extension if it is available at translatewiki.net
Check usage and version matrix.

Version 2.0p stable[edit]

This version has been tested at four wikis. Latest version see Extension:GeoGebra.

What can this extension do?[edit]

Add a GeoGebra applet to your MediaWiki page.

GeoGebra applets use Java technology to combine an interactive geometry environment with the ability to directly enter equations and coordinates making it very useful in math education and math explorations.

Usage[edit]

Allow uploads of ggb-files[edit]

You will have to allow uploads of GeoGebra-files (*.ggb). This can be done by adding the following two lines to LocalSettings.php:

$wgFileExtensions[] = 'ggb';
$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.

Add a GeoGebra Applet tag to your MediaWiki page[edit]

The following tag will add a GeoGebra applet to your MediaWiki page:

<ggb_applet height="300" width="600" filename="MyFile.ggb" />

You can also pass other GeoGebra applet parameters. Example:

<ggb_applet height="200" width="400" showMenuBar="true" showResetIcon="true" filename="MyFile.ggb" />

Usage of useLocalJar = true/false[edit]

By default, useLocalJar = "false". That means, geogebra.jar is fetched from www.geogebra.at.

If geogebra.jar is not found at www.geogebra.at or if useLocalJar = "true" is set explicitly, geogebra.jar will be searched in the same directory as GeoGebra.php.

Example:

<ggb_applet height="200" width="400" showMenuBar="true" showResetIcon="true" filename="MyFile.ggb" useLocalJar = "true" />

Download instructions[edit]

Please cut and paste the code found below and place it in $IP/extensions/GeoGebra/GeoGebra.php.

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

Upload the following files of your GeoGebra installation (maybe in \programs\GeoGebra) to $IP/extensions/GeoGebra.

  • geogebra.jar
  • geogebra_cas.jar
  • geogebra_export.jar
  • geogebra_gui.jar
  • geogebra_main.jar
  • geogebra_properties.jar
  • lax.jar
  • license.txt
  • gpl-2.0.txt
  • cc-by-nc-nd-3.0.txt

Installation[edit]

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

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

Code[edit]

<?php
/**
 * GeoGebra extension
 *
 * @author Rudolf Grossmann
 * @version 2.0p
 */

$ggb_version = "2.0p";

// 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'][] = 'ggb_AppletSetup';
} else {
        $wgExtensionFunctions[] = 'ggb_AppletSetup';
}

$wgExtensionCredits['parserhook'][] = array(
        'name'         => 'GeoGebra',
        'author'       => 'Rudolf Grossmann',
        'url'          => 'http://www.mediawiki.org/wiki/Extension:GeoGebra',
        'description'  => 'Add [http://www.geogebra.at GeoGebra] applets to MediaWiki pages.',
        'version'      => $ggb_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 ggb_AppletSetup() {
        global $wgParser;
        $wgParser->setHook( 'ggb_applet', 'get_ggbAppletOutput' );
        return true;
}

function get_ggbAppletOutput( $input, $args, $parser ) {
        global $wgServer; // URL of the WIKI's server
        global $ggb_version; // see line 9 of this file

        $error_message = "no error"; //will be overwritten, if error occurs
        $CRLF = "\r\n";
        $quot='"';

        $parameter_array = array("type", "framePossible", "bgcolor", "borderColor", "enableRightClick", "enableShiftDragZoom", "showMenuBar");
        $parameter_array = array_merge($parameter_array, array('showToolBar', 'showToolBarHelp', 'customToolBar','showAlgebraInput'));
        $parameter_array = array_merge($parameter_array, array('showResetIcon', 'language', 'country'));

        $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['filename'] ) )
            $error_message = "Missing parameter (width or height or filename).";

        $appletBinary = "geogebra.jar" ;
        $codeBase = "http://www.geogebra.at/webstart/"; // default
        $filenotfound = false;
        if (url_exists($codeBase . $appletBinary) == 'false'){
          $filenotfound = true;
        }
        
        if ($filenotfound == true || $args['uselocaljar'] == 'true' ){
          # 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, -1) != '/') {
            $codeBase = $codeBase . "/";
          }
          $filenotfound = false;  
          if (url_exists($codeBase . $appletBinary) == 'false'){
            $filenotfound = true;
          }
        }
        if ($filenotfound){
          $error_message = "File " . $codeBase . $appletBinary . " not found.";
        }
        
        $output = "<!-- GeoGebra Applet MediaWiki extension " . $ggb_version . " by R. Grossmann -->" . $CRLF;
        $output = $output . '<applet code="geogebra.GeoGebraApplet"'; // Add code value to tag
        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

        // retrieve URL of *.ggb file
        $ggbBinary = htmlspecialchars(strip_tags($args['filename']));
        $ggbFile = Image::newFromName($ggbBinary);
        if (!($ggbFile->exists()))
        {
          $error_message = "File " . $ggbBinary . " not found.";
        }
        else
        {
          $ggbURL = $ggbFile->getURL();
          // if URL doesn't start with slash, add starting slash.
          if (substr($ggbURL, 0, 1) != '/'){
                  $ggbURL = '/' . $ggbURL;
          }
          $ggbURL = $wgServer . $ggbURL;
        }

        // Add URL of *.ggb file to tag
        $output = $output . '<param name="filename" value="' . $ggbURL . '">' . $CRLF;

        // 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 (GeoGebra.php): <em>" . $error_message. "</em></p>" . $CRLF;
        }

        // Send the output to the browser
        return $output;
} // missing php end tag to avoid troubles.

Known problems[edit]

  • There was a problem with the Opera browser. Opera showed Applet crashed instead of the GeoGebra applet. This problem occurred only if useLocalJar="true" was used and only geogebra.jar was installed without the rest of the geogebra*.jar files. Please inform me, if you are using Opera, and an error occurs.
  • There is a problem retrieving the URL of a GGB file uploaded to the MediaWiki file repository. This concerns only one specific installation of the GeoGebra extension; the others work fine. Maybe this is an interference with another extension.

Changes from 1.0u to 2.0p[edit]

If geogebra.jar is not found at www.geogebra.at, or if using useLocalJar="true", GeoGebra extension looks for geogebra.jar at another place:

Version 1.0u looked for geogebra.jar in the file repository of the wiki. This caused problems, when geogebra.jar changed to be only a starter for other geogebra*.jar files, e.g. geogebra_main.jar, geogebra_gui.jar etc.

Version 2.0p now looks for geogebra.jar in the /extension/GeoGebra subdirectory of the wiki. So you have to upload all geogebra*.jar files using FTP.

A minor change was necessary, because $_SERVER['DOCUMENT_ROOT'] did not work correctly.

See also[edit]

Other languages: English