Difference between revisions of "MediaWiki"

From Jmol
Jump to navigation Jump to search
m (Links)
(New format for jmol extension)
Line 25: Line 25:
  
 
  <jmol>
 
  <jmol>
   <contents>contents of the current.xyz file</contents>
+
   <jmolApplet>
  <script>script to execute</script>
+
    <onlineContents>contents of the current.xyz file</onlineContents>
  <name>applet name</name>
+
    <script>script to execute</script>
  <size>applet size</size>
+
    <name>applet name</name>
  <color>applet background color</color>
+
    <size>applet size</size>
 +
    <color>applet background color</color>
 +
  </jmolApplet>
 
  &lt;/jmol&gt;
 
  &lt;/jmol&gt;
  
 
To add buttons to send scripts to the applet, just add the following:
 
To add buttons to send scripts to the applet, just add the following:
  
  &lt;jmolButton&gt;
+
  &lt;jmol&gt;
   <script>script to execute</script>
+
   <jmolButton>
  <text>text of the button</text>
+
    <script>script to execute</script>
  <name>applet name</name>
+
    <text>text of the button</text>
  &lt;/jmolButton&gt;
+
    <name>applet name</name>
 +
  </jmolButton>
 +
  &lt;/jmol&gt;
  
 +
Applets and buttons can be combined in a single &lt;jmol&gt; tag.
  
 
= Installation =
 
= Installation =
Line 49: Line 54:
 
* <tt>Jmol.js</tt>
 
* <tt>Jmol.js</tt>
 
* <tt>JmolApplet_i18n.jar</tt>
 
* <tt>JmolApplet_i18n.jar</tt>
* <tt>JmolApplet0.jar</tt>
+
* <tt>JmolApplet0.jar</tt> to  <tt>JmolApplet6.jar</tt>
* <tt>JmolApplet1.jar</tt>
 
* <tt>JmolApplet2.jar</tt>
 
* <tt>JmolApplet3.jar</tt>
 
* <tt>JmolApplet4.jar</tt>
 
* <tt>JmolApplet5.jar</tt>
 
* <tt>JmolApplet6.jar</tt>
 
 
* <tt>LICENSE.txt</tt>
 
* <tt>LICENSE.txt</tt>
 
* <tt>README.txt</tt>
 
* <tt>README.txt</tt>
Line 65: Line 64:
  
 
  <pre><?php
 
  <pre><?php
$wgExtensionFunctions[] = "wfJmolExtension";
 
$currentTag = "";
 
$appletContents = "";
 
$appletScript = "";
 
$appletName = "";
 
$appletSize = "";
 
$appletColor = "";
 
$appletText = "";
 
  
function wfJmolExtension() {
+
/*
  global $wgParser;
+
* Copyright (C) 2006 Nicolas Vervelle,  The Jmol Development Team
   $wgParser->setHook( "jmol", "renderJmol" );
+
*
   $wgParser->setHook( "jmolButton", "renderJmolButton" );
+
* Contact: nico@jmol.org, jmol-developers@lists.sf.net
}
+
*
 +
*  This library is free software; you can redistribute it and/or
 +
*  modify it under the terms of the GNU Lesser General Public
 +
*  License as published by the Free Software Foundation; either
 +
*  version 2.1 of the License, or (at your option) any later version.
 +
*
 +
*  This library is distributed in the hope that it will be useful,
 +
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
 +
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 +
*  Lesser General Public License for more details.
 +
*
 +
*  You should have received a copy of the GNU Lesser General Public
 +
*  License along with this library; if not, write to the Free Software
 +
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 +
*/
 +
 +
if (defined('MEDIAWIKI')) {
 +
 
 +
/* Class containing all the code for the Jmol Extension */
 +
class JmolExtension {
 +
   var $mOutput, $mDepth;
 +
   var $mCurrentObject, $mCurrentTag;
  
function startElement( $parser, $name, $attrs ) {
+
  var $mValOnlineContents;
   global $currentTag;
+
  var $mValScript;
 +
  var $mValName;
 +
  var $mValSize;
 +
  var $mValColor;
 +
   var $mValText;
  
   $currentTag = $name;
+
   function JmolExtension() {
}
+
    $this->mOutput = "";
 +
    $this->mDepth = 0;
 +
    $this->resetValues();
 +
  }
 +
 
 +
  function renderJmol($input) {
 +
    $this->mOutput = "";
 +
    $this->mDepth = 0;
 +
    $xmlParser = xml_parser_create();
 +
    xml_set_object($xmlParser, &$this);
 +
    xml_set_element_handler($xmlParser, "startElement", "endElement");
 +
    xml_set_character_data_handler($xmlParser, "characterData");
 +
    $input = "<jmol>$input<jmol>";
 +
    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);
 +
    return $this->mOutput;
 +
  }
  
function endElement( $parser, $name ) {
+
  function renderJmolApplet() {
  global $currentTag;
+
    $this->mValOnlineContents = trim($this->mValOnlineContents);
 +
    $this->mValOnlineContents = preg_replace("/\t/", " ", $this->mValOnlineContents);
 +
    $this->mValOnlineContents = preg_replace("/\n/", "\\n'+\n'", $this->mValOnlineContents);
 +
    $this->mOutput .=
 +
      "<script src='/extensions/Jmol/Jmol.js'></script>\n".
 +
      "<script>\n".
 +
      "jmolInitialize('/extensions/Jmol');\n".
 +
      "jmolCheckBrowser('popup', '/extensions/Jmol/browsercheck', 'onClick');\n".
 +
      "jmolSetAppletColor('".$this->mValColor."');\n".
 +
      "jmolAppletInline(".$this->mValSize.", \n'".
 +
                          $this->mValOnlineContents."', \n'".
 +
                          $this->mValScript."', \n'".
 +
                          $this->mValName."');\n".
 +
      "jmolBr();\n".
 +
      "</script>\n";
 +
  }
  
   $currentTag = "";
+
   function renderJmolButton() {
}
+
    $this->mOutput .=
 +
      "<script>\n".
 +
      "jmolSetTarget('".$this->mValName."');\n".
 +
      "jmolButton('".$this->mValScript."', '".$this->mValText."');\n" .
 +
      "</script>\n";
 +
  }
  
function characterData( $parser, $data ) {
+
  function startElement($parser, $name, $attrs) {
  global $currentTag;
+
    $this->mDepth += 1;
  global $appletContents;
+
    switch ($this->mDepth) {
  global $appletScript;
+
    case 1:
  global $appletName;
+
      // JMOL tag itself
  global $appletSize;
+
      $this->resetValues();
  global $appletColor;
+
      break;
   global $appletText;
+
    case 2:
 +
      // The interesting tags
 +
      $this->resetValues();
 +
      $this->mCurrentObject = $name;
 +
      break;
 +
    case 3:
 +
      // Details of the interesting tags;
 +
      $this->mCurrentTag = $name;
 +
    }
 +
   }
  
   switch ($currentTag) {
+
   function endElement($parser, $name) {
  case "CONTENTS":
+
    switch ($this->mDepth) {
    $data = trim($data);
+
    case 1:
     if ($data != "") {
+
      // JMOL tag itself
       $appletContents = $appletContents . "\n" . $data;
+
      $this->resetValues();
 +
      break;
 +
     case 2:
 +
      // The interesting tags
 +
      switch ($this->mCurrentObject) {
 +
      case "JMOLAPPLET":
 +
        $this->renderJmolApplet();
 +
        break;
 +
      case "JMOLBUTTON":
 +
        $this->renderJmolButton();
 +
        break;
 +
      }
 +
      $this->resetValues();
 +
      break;
 +
    case 3:
 +
      // Details of the interesting tags
 +
       $this->mCurrentTag = "";
 +
      break;
 
     }
 
     }
    break;
+
     $this->mDepth -= 1;
  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 ) {
+
  function characterData($parser, $data) {
  global $appletContents;
+
    switch ($this->mDepth) {
  global $appletScript;
+
    case 3:
  global $appletName;
+
      // Details of the interesting tags
  global $appletSize;
+
      switch ($this->mCurrentTag) {
  global $appletColor;
+
      case "ONLINECONTENTS":
 +
        $data = trim($data);
 +
        if ($data != "") {
 +
          $this->mValOnlineContents .= "\n" . $data;
 +
        }
 +
        break;
 +
      case "SCRIPT":
 +
        $this->mValScript = $data;
 +
        break;
 +
      case "NAME":
 +
        $this->mValName = $data;
 +
        break;
 +
      case "SIZE":
 +
        $this->mValSize = $data;
 +
        break;
 +
      case "COLOR":
 +
        $this->mValColor = $data;
 +
        break;
 +
      case "TEXT":
 +
        $this->mValText = $data;
 +
        break;
 +
      }
 +
      break;
 +
    }
 +
  }
  
   resetValues();
+
   function resetValues() {
  $xmlParser = xml_parser_create();
+
    $this->mCurrentObject = "";
  xml_set_element_handler($xmlParser, "startElement", "endElement");
+
    $this->mCurrentTag = "";
  xml_set_character_data_handler($xmlParser, "characterData");
+
    $this->mValOnlineContents = "";
  $input = "<jmolApplet>$input<jmolApplet>";
+
    $this->mValScript = "";
  if (!xml_parse($xmlParser, $input)) {
+
    $this->mValName = "JmolWiki";
     die(sprintf(
+
    $this->mValSize = "400";
      "XML error: %s at line %d",
+
    $this->mValColor = "black";
      xml_error_string(xml_get_error_code($xmlParser)),
+
     $this->mValText = "";
      xml_get_current_line_number($xmlParser)));
 
 
   }
 
   }
  xml_parser_free($xmlParser);
+
};
  $appletContents = trim($appletContents);
+
 
  $appletContents = preg_replace("/\t/", " ", $appletContents);
+
$wgExtensionCredits['other'][] = array(
   $appletContents = preg_replace("/\n/", "\\n'+\n'", $appletContents);
+
   'name'        => 'Jmol Extension for MediaWiki',
   $output = $output .
+
   'description' => 'adds the possibility to include Jmol applets in MediaWiki.',
    "<script src='/extensions/Jmol/Jmol.js'></script>\n".
+
  'author'     => 'Nicolas Vervelle, Jmol Development Team'
    "<script>\n" .
+
);
    "jmolInitialize('/extensions/Jmol');\n".
+
 
    "jmolCheckBrowser('popup', '/extensions/Jmol/browsercheck', 'onClick');\n".
+
$wgJmolExtension = new JmolExtension;
    "jmolSetAppletColor('$appletColor');\n".
+
 
    "jmolAppletInline($appletSize, \n'$appletContents', \n'$appletScript', \n'$appletName');\n" .
+
/* Test to modify between Dev and Release versions */
    "jmolBr();\n" .
+
if (true) {
    "</script>\n"
+
   $wgExtensionFunctions[] = "wfJmolExtension";
    ;
 
   return $output;
 
}
 
  
function renderJmolButton( $input ) {
+
  function wfJmolExtension() {
  global $appletScript;
+
    global $wgParser;
  global $appletName;
+
    $wgParser->setHook( "jmol", "wfRenderJmol" );
   global $appletText;
+
   }
  
   resetValues();
+
   function wfRenderJmol($input) {
  $xmlParser = xml_parser_create();
+
    global $wgJmolExtension;
  xml_set_element_handler($xmlParser, "startElement", "endElement");
+
    return $wgJmolExtension->renderJmol($input);
  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);
+
} else {
   $output = $output .
+
   $wgExtensionFunctions[] = "wfJmolDevExtension";
    "<script>\n" .
 
    "jmolSetTarget('$appletName');\n" .
 
    "jmolButton('$appletScript', '$appletText');\n" .
 
    "</script>\n"
 
    ;
 
  return $output;
 
}
 
  
function resetValues() {
+
  function wfJmolDevExtension() {
  global $currentTag;
+
    global $wgParser;
  global $appletContents;
+
    $wgParser->setHook( "jmolDev", "wfRenderJmolDev" );
  global $appletScript;
+
   }
  global $appletName;
 
  global $appletSize;
 
   global $appletColor;
 
  
   $currentTag = "";
+
   function wfRenderJmolDev($input) {
  $appletContents = "";
+
    global $wgJmolExtension;
  $appletScript = "set spin Y 10; spin on";
+
    return $wgJmolExtension->renderJmol($input);
  $appletName = "FahWiki";
+
   }
   $appletSize = "400";
 
  $appletColor = "black";
 
  $appletText = "";
 
 
}
 
}
  
 +
}
 
?></pre>
 
?></pre>
  

Revision as of 21:34, 10 April 2006

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


Wikis using the Jmol Extension

If you know a Wiki using the Jmol MediaWiki Extension, please add it in the following list:


How to use the Jmol Extension

Once the Jmol Extension is installed in the Wiki, you can start adding Jmol applets and controls.

This following documentation is subject to change since the Jmol Extension is currently under development

To add a Jmol applet to a MediaWiki page, just add the following:

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

To add buttons to send scripts to the applet, just add the following:

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

Applets and buttons can be combined in a single <jmol> tag.

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 to 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

/*
 * Copyright (C) 2006 Nicolas Vervelle,  The Jmol Development Team
 *
 * Contact: nico@jmol.org, jmol-developers@lists.sf.net
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
if (defined('MEDIAWIKI')) {

/* Class containing all the code for the Jmol Extension */
class JmolExtension {
  var $mOutput, $mDepth;
  var $mCurrentObject, $mCurrentTag;

  var $mValOnlineContents;
  var $mValScript;
  var $mValName;
  var $mValSize;
  var $mValColor;
  var $mValText;

  function JmolExtension() {
    $this->mOutput = "";
    $this->mDepth = 0;
    $this->resetValues();
  }

  function renderJmol($input) {
    $this->mOutput = "";
    $this->mDepth = 0;
    $xmlParser = xml_parser_create();
    xml_set_object($xmlParser, &$this);
    xml_set_element_handler($xmlParser, "startElement", "endElement");
    xml_set_character_data_handler($xmlParser, "characterData");
    $input = "<jmol>$input<jmol>";
    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);
    return $this->mOutput;
  }

  function renderJmolApplet() {
    $this->mValOnlineContents = trim($this->mValOnlineContents);
    $this->mValOnlineContents = preg_replace("/\t/", " ", $this->mValOnlineContents);
    $this->mValOnlineContents = preg_replace("/\n/", "\\n'+\n'", $this->mValOnlineContents);
    $this->mOutput .=
      "<script src='/extensions/Jmol/Jmol.js'></script>\n".
      "<script>\n".
      "jmolInitialize('/extensions/Jmol');\n".
      "jmolCheckBrowser('popup', '/extensions/Jmol/browsercheck', 'onClick');\n".
      "jmolSetAppletColor('".$this->mValColor."');\n".
      "jmolAppletInline(".$this->mValSize.", \n'".
                          $this->mValOnlineContents."', \n'".
                          $this->mValScript."', \n'".
                          $this->mValName."');\n".
      "jmolBr();\n".
      "</script>\n";
  }

  function renderJmolButton() {
    $this->mOutput .=
      "<script>\n".
      "jmolSetTarget('".$this->mValName."');\n".
      "jmolButton('".$this->mValScript."', '".$this->mValText."');\n" .
      "</script>\n";
  }

  function startElement($parser, $name, $attrs) {
    $this->mDepth += 1;
    switch ($this->mDepth) {
    case 1:
      // JMOL tag itself
      $this->resetValues();
      break;
    case 2:
      // The interesting tags
      $this->resetValues();
      $this->mCurrentObject = $name;
      break;
    case 3:
      // Details of the interesting tags;
      $this->mCurrentTag = $name;
    }
  }

  function endElement($parser, $name) {
    switch ($this->mDepth) {
    case 1:
      // JMOL tag itself
      $this->resetValues();
      break;
    case 2:
      // The interesting tags
      switch ($this->mCurrentObject) {
      case "JMOLAPPLET":
        $this->renderJmolApplet();
        break;
      case "JMOLBUTTON":
        $this->renderJmolButton();
        break;
      }
      $this->resetValues();
      break;
    case 3:
      // Details of the interesting tags
      $this->mCurrentTag = "";
      break;
    }
    $this->mDepth -= 1;
  }

  function characterData($parser, $data) {
    switch ($this->mDepth) {
    case 3:
      // Details of the interesting tags
      switch ($this->mCurrentTag) {
      case "ONLINECONTENTS":
        $data = trim($data);
        if ($data != "") {
          $this->mValOnlineContents .= "\n" . $data;
        }
        break;
      case "SCRIPT":
        $this->mValScript = $data;
        break;
      case "NAME":
        $this->mValName = $data;
        break;
      case "SIZE":
        $this->mValSize = $data;
        break;
      case "COLOR":
        $this->mValColor = $data;
        break;
      case "TEXT":
        $this->mValText = $data;
        break;
      }
      break;
    }
  }

  function resetValues() {
    $this->mCurrentObject = "";
    $this->mCurrentTag = "";
    $this->mValOnlineContents = "";
    $this->mValScript = "";
    $this->mValName = "JmolWiki";
    $this->mValSize = "400";
    $this->mValColor = "black";
    $this->mValText = "";
  }
};

$wgExtensionCredits['other'][] = array(
  'name'        => 'Jmol Extension for MediaWiki',
  'description' => 'adds the possibility to include Jmol applets in MediaWiki.',
  'author'      => 'Nicolas Vervelle, Jmol Development Team'
);

$wgJmolExtension = new JmolExtension;

/* Test to modify between Dev and Release versions */
if (true) {
  $wgExtensionFunctions[] = "wfJmolExtension";

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

  function wfRenderJmol($input) {
    global $wgJmolExtension;
    return $wgJmolExtension->renderJmol($input);
  }
} else {
  $wgExtensionFunctions[] = "wfJmolDevExtension";

  function wfJmolDevExtension() {
    global $wgParser;
    $wgParser->setHook( "jmolDev", "wfRenderJmolDev" );
  }

  function wfRenderJmolDev($input) {
    global $wgJmolExtension;
    return $wgJmolExtension->renderJmol($input);
  }
}

}
?>


External links