Extension:Calc

From MediaWiki.org
Jump to: navigation, search

The Calc extension allows very simple mathematics via a very simple MediaWiki extension.

[edit] Install

Create a file called 'calc.php' in your extensions/ directory, and add the following code...

<?php
$wgExtensionFunctions[] = "wfCalcExtension";
                                                                        
function wfCalcExtension() {
   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 <calc> ... </calc>
   # the second parameter is the callback function for
   # processing the text between the tags
   $wgParser->setHook( "calc", "doMath" );
}
                                                                         
# The callback function for converting the input text to HTML output
function doMath( $input ) {
   global $wgOut;
   # Parser
   $input = $wgOut->parse($input,false);
   $array = explode(' ', $input);
   if($array[0]=="incr")
     return $array[1]+1;
   if($array[0]=="decr")
     return $array[1]-1;
   if($array[0]=="add")
     return $array[1]+$array[2];
   if($array[0]=="sub")
     return $array[1]-$array[2];
   return ;
}
?>

Then simply add the following line to the end of your LocalSettings.php file:

include("extensions/calc.php");

and you are ready to go! Go to the sandbox and see if it works!

[edit] Usage

The following code describes the usage of this extension (which you are invited to extend (and improve!) via the very simple code design ;-)

You type... The result is...
<calc>incr 999</calc> 1000
<calc>decr 1001</calc> 1000
<calc>add 1001 -1</calc> 1000
<calc>sub 1001 +1</calc> 1000
<calc>incr </calc> 1
<calc>decr </calc> -1
<calc>add 1001 </calc> 1001
<calc>sub 1001 </calc> 1001
<calc>add </calc> 0
<calc>sub </calc> 0
<calc>incr kill</calc> 1
<calc>decr kill</calc> -1
<calc>add kill -1</calc> -1
<calc>sub kill +1</calc> -1
<calc>add 1001 kill</calc> 1001
<calc>sub 1001 kill</calc> 1001
<calc>add kill </calc> 0
<calc>sub kill </calc> 0
<calc>add </calc> 0
<calc>sub </calc> 0
<calc></calc>
<calc>add 45 92</calc> 137
<calc>sub 1190 190</calc> 1000
<calc>add {{CURRENTMONTH}} 99</calc> 111

[edit] See Also

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Toolbox