MediaWiki

From Jmol
Jump to navigation Jump to search

Description

MediaWiki is an open source wiki engine licensed under the GNU General Public License. It is used by many websites, including this one.

The Jmol Mediawiki Extension enables the use of the Jmol applet inside of Mediawiki pages. This simple page demonstrates how to use the extension.

Note: The Jmol Mediawiki Extension is currently under development. It will go under several changes, different versions will probably be incompatible between each other

Installation

Download Jmol and extract all the files in a temporary directory.

Create the directory Jmol in $mediawiki/extensions, and copy the following files in it:

  • COPYRIGHT.txt
  • Jmol.js
  • JmolApplet_i18n.jar
  • JmolApplet0.jar
  • JmolApplet1.jar
  • JmolApplet2.jar
  • JmolApplet3.jar
  • JmolApplet4.jar
  • JmolApplet5.jar
  • JmolApplet6.jar
  • LICENSE.txt
  • README.txt

Add the following line to the end of LocalSettings.php:

require_once('extensions/Jmol/JmolExtension.php');

Copy the following code and paste it into extensions/Jmol/JmolExtension.php:

<?php
$wgExtensionFunctions[] = "wfJmolExtension";
$currentTag = "";
$appletContents = "";
$appletScript = "";
$appletName = "";
$appletSize = "";
$appletColor = "";
$appletText = "";

function wfJmolExtension() {
  global $wgParser;
  $wgParser->setHook( "jmol", "renderJmol" );
  $wgParser->setHook( "jmolButton", "renderJmolButton" );
}

function startElement( $parser, $name, $attrs ) {
  global $currentTag;

  $currentTag = $name;
}

function endElement( $parser, $name ) {
  global $currentTag;

  $currentTag = "";
}

function characterData( $parser, $data ) {
  global $currentTag;
  global $appletContents;
  global $appletScript;
  global $appletName;
  global $appletSize;
  global $appletColor;
  global $appletText;

  switch ($currentTag) {
  case "CONTENTS":
    $data = trim($data);
    if ($data != "") {
      $appletContents = $appletContents . "\n" . $data;
    }
    break;
  case "SCRIPT":
    $appletScript = $data;
    break;
  case "NAME":
    $appletName = $data;
    break;
  case "SIZE":
    $appletSize = $data;
    break;
  case "COLOR":
    $appletColor = $data;
    break;
  case "TEXT":
    $appletText = $data;
    break;
  default:
    break;
  }
}

function renderJmol( $input ) {
  global $appletContents;
  global $appletScript;
  global $appletName;
  global $appletSize;
  global $appletColor;

  resetValues();
  $xmlParser = xml_parser_create();
  xml_set_element_handler($xmlParser, "startElement", "endElement");
  xml_set_character_data_handler($xmlParser, "characterData");
  $input = "<jmolApplet>$input<jmolApplet>";
  if (!xml_parse($xmlParser, $input)) {
    die(sprintf(
      "XML error: %s at line %d",
      xml_error_string(xml_get_error_code($xmlParser)),
      xml_get_current_line_number($xmlParser)));
  }
  xml_parser_free($xmlParser);
  $appletContents = trim($appletContents);
  $appletContents = preg_replace("/\t/", " ", $appletContents);
  $appletContents = preg_replace("/\n/", "\\n'+\n'", $appletContents);
  $output = $output .
    "<script src='/extensions/Jmol/Jmol.js'></script>\n".
    "<script>\n" .
    "jmolInitialize('/extensions/Jmol');\n".
    "jmolCheckBrowser('popup', '/extensions/Jmol/browsercheck', 'onClick');\n".
    "jmolSetAppletColor('$appletColor');\n".
    "jmolAppletInline($appletSize, \n'$appletContents', \n'$appletScript', \n'$appletName');\n" .
    "jmolBr();\n" .
    "</script>\n"
    ;
  return $output;
}

function renderJmolButton( $input ) {
  global $appletScript;
  global $appletName;
  global $appletText;

  resetValues();
  $xmlParser = xml_parser_create();
  xml_set_element_handler($xmlParser, "startElement", "endElement");
  xml_set_character_data_handler($xmlParser, "characterData");
  $input = "<jmolApplet>$input<jmolApplet>";
  if (!xml_parse($xmlParser, $input)) {
    die(sprintf(
      "XML error: %s at line %d",
      xml_error_string(xml_get_error_code($xmlParser)),
      xml_get_current_line_number($xmlParser)));
  }
  xml_parser_free($xmlParser);
  $output = $output .
    "<script>\n" .
    "jmolSetTarget('$appletName');\n" .
    "jmolButton('$appletScript', '$appletText');\n" .
    "</script>\n"
    ;
  return $output;
}

function resetValues() {
  global $currentTag;
  global $appletContents;
  global $appletScript;
  global $appletName;
  global $appletSize;
  global $appletColor;

  $currentTag = "";
  $appletContents = "";
  $appletScript = "set spin Y 10; spin on";
  $appletName = "FahWiki";
  $appletSize = "400";
  $appletColor = "black";
  $appletText = "";
}

?>