Jmol MediaWiki Extension

From FaHWiki
Jump to: navigation, search

Jmol MediaWiki Extension is a piece of php code that runs whenever the Wiki renderer finds the jmol tags. With it it is possible to show a Jmol Applet in a Wiki page.

As it is now it only accepts Folding@Home proteins.

Using it is very simple. Place the tags in a Wiki page. Inside the tags put the contents of the current.xyz file corresponding to the protein you want it to show.

Open the current.xyz file in a text editor, copy everything and paste inside the tags:

<jmol><contents>
contents of the current.xyz file
</contents></jmol>

Bulding an extension is explained here:

http://meta.wikimedia.org/wiki/Writing_your_own_MediaWiki_extension

This is the second version, written by NicoV, with a XML parser. With the new XML parser it is possible to pass parameters to the Jmol Applet. To see how this page examples were set open the page for edition but do not save it. Copy to the sandbox to experiment.

The parameters that can be passed to the Jmol Applet are the following:

<jmol>
<contents>contents of the current.xyz file</contents>
<script>script to execute</script>
<name>applet name</name>
<size>applet size</size>
<color>applet background color</color>
</jmol>

In addition, you can also create buttons to send scripts to the applet.

<jmolButton>
<script>script to execute</script>
<text>text of the button<text>
<name>applet name</name>
</jmolButton>

The source code of the extension (an updated description and version can be found here) :

<?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 = 
    "<script src='/extensions/jmol/Jmol.js' type='text/javascript'></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 = 
    "<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 = "";
}

?>

Example with buttons


<jmolButton><script>spin on</script><name>FahWiki</name><text>Start spinning</text></jmolButton><jmolButton><script>spin off</script><name>FahWiki</name><text>Stop spinning</text></jmolButton>

NicoV 19 June 2005

Personal tools