Extension:PhpMathPublisher alternative

From MediaWiki.org
Jump to navigation Jump to search

PhpMathPublisher is an alternative which needs only PHP with the GD library and some fonts. No need for LaTeX or CGI, the latter is needed for the Mimetex alternative. (Also the graphical output looks better.)

PhpMathPublisher can be found at [1].


How to install PhpMathPublisher as an extension[edit]

MediaWiki and PhpMathPublisher need to be installed of course. (This has been tested with MediaWiki 1.15.1 and PhpMathPublisher 0.3.) Save the text below, which is an adaption of the Mimetex extension, in a file called phpmathpublisher.php

<?php

$wgExtensionFunctions[] = "PhpMathPublisherExtension";
 
function PhpMathPublisherExtension() {
    global $wgParser;
    # register the extension with the WikiText parser
    # the first parameter is the name of the new tag.
    # In this case it defines the tag <example> ... </example>
    # the second parameter is the callback function for
    # processing the text between the tags
    $wgParser->setHook( "m", "render_PhpMathPublisher" );
}

# load MathPublisher
require("mathpublisher.php");
 
# Renders $text in PhpMathPublisher
function render_PhpMathPublisher($input, $argv, $parser = null) {
 
  if (!$parser) $parser =& $GLOBALS['wgParser'];
  
  # create/load image
  # fontsize = 14 (you can change this freely),
  # you maybe have to change the img-path to
  # "wiki/extensions/phpmathpublisher/img/" or
  # "mediawiki/extensions/phpmathpublisher/img/
  # depending on your installation
  $imgLink = mathfilter("<m>".$input."</m>",14,"extensions/phpmathpublisher/img/"); 

  # return <img> HTML-Code
  return $imgLink;

}


Update the img-path and fonts-path in the mathpublisher.php file (absolute paths/full paths):

//********* PARAMETERS TO MODIFY *********************************
// The four global variables. Uncomment the line if you need it.
//global $dirfonts,$dirimg,$symboles,$fontesmath;

// choose the type of the declaration according to your server settings (some servers don't accept the dirname(__FILE__) command for security reasons).

// NEW in 0.3 version : no more / at the end of $dirfonts and $dirimg

// absolute path to the fonts directory,
// you maybe have to change the path to
// "wiki/extensions/phpmathpublisher/fonts" or
// "mediawiki/extensions/phpmathpublisher/fonts/
// depending on your installation
$dirfonts=$_SERVER["DOCUMENT_ROOT"]."extensions/phpmathpublisher/fonts";

// absolute path to the images directory,
// you maybe have to change the path to
// "wiki/extensions/phpmathpublisher/img" or
// "mediawiki/extensions/phpmathpublisher/img/
// depending on your installation
$dirimg=$_SERVER["DOCUMENT_ROOT"]."extensions/phpmathpublisher/img";


Create the following directories in your wiki directory: (1) extensions/phpmathpublisher , (2) extensions/phpmathpublisher/fonts and (3) extensions/phpmathpublisher/img

Put the phpmathpublisher.php and the mathpublisher.php in directory (1), put the fonts in directory (2) and make sure that directory (3) is writable (chmod).


Add the line:

require("extensions/phpmathpublisher/phpmathpublisher.php");

to the end of your LocalSettings.php file in your MediaWiki root directory.

Now you can output PhpMathPublisher syntax with <m>...</m> tags. To put those tags into your Edit Toolbar, edit Editpage.php in your /includes/ directory. (You should make a backup of this file first just in case.)

Search for the following lines:

array(	'image'	=>'button_math.png',
	'open'	=>	"\\<math\\>",
	'close'	=>	"<\\/math\\>",
	'sample'=>	wfMsg('math_sample'),
	'tip'	=>	wfMsg('math_tip'),
	'key'	=>	'C'

And replace them with:

array(	'image'	=>'button_math.png',
	'open'	=>	"\\<m\\>",
	'close'	=>	"\\</m\\>",
	'sample'=>	wfMsg('math_sample'),
	'tip'	=>	wfMsg('math_tip'),
	'key'	=>	'C'

Since this is an adaption of the Mimetex extension the code is also licensed under the GNU General Public License.

This solution does not appear to work on version 1.13.x of MediaWiki.