From MediaWiki.org
Jump to: navigation, search

MediaWiki extensions manual
Crystal Clear action run.png
CalcII Extension

Release status: beta

Implementation Tag
Description Insert a JavaScript-Calculator to a page.
Author(s) Peter Lehmacher, translated by Patrick
Latest version 0.1.0 (2007-05-22)
MediaWiki from 1.6.6
License No license specified
Download Download and Installation

Translate the CalcII extension if it is available at translatewiki.net

Check usage and version matrix; code metrics


By putting the tag <CalcII>predefined expression</calcII> somewhere in the wikitext of a page, this extension produces a calculator at that position of the page with the predefined expression. It has an input field and a results field, and a button to start the calculation.

Find a working example at the following website (german language): http://www.technikundkultur.de/wiki1/index.php5?title=Rechner

The input field allows any sequence of mathematical expressions and assignment statements (in JavaScript syntax), separated by semi-colons. If a predefined expression is inserted, it will be shown first but it can be changed or deleted. The interpretation of the Javascript expressions is done by a separate Javascript which has to be stored in the extension folder together with the PHP-script.

While the mathematical Javascript functions and constants [1] can be used directly, the extension also allows the following additional functions, constants, and abbreviations:

  • Functions:
absolute value: ABS(x)
square root: SQRT(x)
xy: POW(x,y)
ex: EXP(x)
natural logarithm: LN(x)
logarithm base 10: LG(x)
change radian to degree: DEG(x)
change degree to radian: RAD(x)
x!: FAC(x)
trigonometric functions SIN(x) COS(x) TAN(x) ASIN(x) ACOS(x) ATAN(x)
unit conversion, where a and b are units (see below): CONV(x,a,b)
  • Constants: pi e lge ln10
  • Units: _ft (in m) _in (in m) _ps (in W) _btu (in J) _mmWS (in Pa) _psi (in Pa) _gal (US gallon in l) _bar (in Pa) _mbar (in Pa) _cal (in J) _km (in m) _m (in m) _mm (in m) _j (in J) _w (in W) _kw _kj _qbm (in l) _pa (in Pa) _sec (in Sec) _h (in Sec) _d (in Sec) _min (in Sec)

Example (the left and center column are the boxes on the wiki page):

Input: <calcII>12*13;a=CONV(25,_km/_h,_m/_sec);b=a*10*_h;Radius=5;Area=POW(Radius*2,2)*pi/4</calcII>

(predefined expression must be inserted without any line break, the expression is optional)


just an expression
conversion of velocity in km/h to m/sec, and assigning the value to a variable
calculation of distance in 10h
assigning a value to a variable
calculation of the area of a circle

Please note:

  • type the formula in the left field
  • separate formulas by ";"
  • define variables (i.e. a=SQRT(4))
  • use decimal point, no comma
  • accuracy: 8 decimal places
  • the functions in the extension-file are defined as JavaScript functions and may be changed for individual requests

Download and installation[edit]

Step 1: Copy-paste this code into a file calcII.php and place it in extensions directory.


# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/calcII.php");
# Syntaxe: <CalcII/>

$wgExtensionFunctions[] = "wfCalcII";

function wfCalcII() {
    global $wgParser;

    $wgParser->setHook( "CalcII", "renderCalcII" );

# The callback function for converting the input text to HTML output
function renderCalcII( $input ) {
    global $wgOut;
    # Parser
    $input = $wgOut->parse($input,false);    
    $output = '<!-- CalcII -->';
    #please check the path to the javascript file and adjust it, if necessary
    $output .= '<script type="text/javascript" src="/mediawiki/extensions/calcII.js">';
    $output .= '</script>';
    $output .= '<center>';
    $output .= '<form name="felder";">';
    $output .= '<table bgcolor="#FFFFFF"><tr><td>';
    $output .= '<input type="hidden" name="ie" value="UTF-8"/>';
    $output .= '<input type="hidden" name="oe" value="UTF-8"/>';
    $output .= '<textarea name="formel" cols="60" rows="20" wrap="off"/>';
    $output .= $input;
    $output .= '</textarea></td><td><textarea name="ergebnis" cols="20" rows="20" value=""/></textarea></td></tr>';
    $output .= '<tr><td><input type="button" value="calculate" onclick="rechnen()"/></td>';
    $output .= '<td><input type="button" value="delete" onclick="delete_all()"/></td>';
    $output .= '</tr></table>';
    $output .= '</form>';
    $output .= '</center>';
    $output .= '<script type="text/javascript">';
    $output .= 'rechnen();';
    $output .= '</script>';
    $output .= '<!-- CalcII -->';
    return $output;

Step 2: Copy-paste this code into a file calcII.js and place it in extensions directory.

Attention: Extension folder must be set readable by everyone - adjust path to javascript file in php script (above)

function rechnen(){
var m1eingabe_w=window.document.felder.formel.value;
var m1genau_w=100000000000000;
var m1formel_a = new Array();
var m1ergebnis_w=new Array();
var m1ergebnis_text;
var m1anzahl;
var _min=60;
var _h=3600;
var _sec=1;
var _d=24*3600;
var _m=1;
var _l=0.001;
var _j=1;
var _w=1;
var _pa=1;
var _mm=0.001;
var _km=1000;
var _cbm=1;
var _kj=1000;
var _kw=1000;
var _bar=100000;
var pi=Math.PI;
var _ft=0.3048;
var _in=0.00254;
var _ps=745.7;
var _btu=1055;
var _mmws=9.813752;
var _psi=6895;
var _gal=0.003785;
var _bar=100000;
var _mbar=100;
var _cal=4.1855;
var _kcal=4185.5;
var ln10=Math.LN10;
var lge=Math.LOG10E;
var j;
for (j=0;j<m1anzahl;j++)
if (j<m1anzahl-1)
function delete_all()
var m1ergebnis_text="";
var m1eingabe_w="";
// Funktion zur Interpolation
function IPOL(t,x_1,x_2,x_3,x_4,y_1,y_2,y_3,y_4)
var x_1;
var x_2;
var x_3;
var x_4;
var y_1;
var y_2;
var y_3;
var y_4;
var t;
var y_cal;
t0=new Array (x_1,x_2,x_3,x_4);
dvw=new Array (y_1,y_2,y_3,y_4);
y_cal=    dvw[0]*((t-t0[1])*(t-t0[2])*(t-t0[3]))/((t0[0]-t0[1])*(t0[0]-t0[2])*(t0[0]-t0[3]))+
return y_cal
function ABS(x)
var x;
return Math.abs(x);
function CONV(x,a,b)
var x;
var a;
var b;
return x*a/b;
function ACOS(x)
var x;
return Math.acos(x);
function COS(x)
var x;
return Math.cos(x);
function SIN(x)
var x;
return Math.sin(x);
function ASIN(x)
var x;
return Math.asin(x);
}function TAN(x)
var x;
return Math.tan(x);
function ATAN(x)
var x;
return Math.atan(x);
function EXP(x)
var x;
return Math.exp(x);
function POW(x,y)
var x;
return Math.pow(x,y);
function SQRT(x)
var x;
var y;
return Math.sqrt(x);
function RAD(x)
var x;
return Math.PI/180*x;
function DEG(x)
var x;
return 180/Math.PI*x;

function LN(x)
var x;
if (x<=0) 
alert("Calculation LN(x) with x<=0 not allowed!");
return Math.log(x);

function LG(x)
var x;
if (x<=0)
alert("Calculation LG(x) with x<=0 not allowed!");
return Math.LOG10E*Math.log(x);

function FAC(x)
var x;
var y=1;
var i;
if (Math.ceil(x)!=x)
alert("Calculation FAK(x) only with integer!");
for (i=1;i<=x;i++){
return y;

Step 3: Add the following line to LocalSettings.php:

 include ("extensions/calcII.php");

Step 4: To insert the calculator to a page, put <CalcII></CalcII> or <CalcII>Expression 1;Expression 2;...;Expression n</CalcII> in the wikitext.


You are free to use this extension for any reason and mutilate it to your heart's liking. If you feel your work might benefit others, post your changes here.

Release 0.1.0: The script has been devided in a php part and a javascript part. It is now possible to define an expression directly in the edit mode of the wikipage. The expression can be deleted or changed in the input field at view mode but will always be in the input field when the page opens.

At the moment it is not possible to set line breaks between the predefined expressions. If you do, the calculation won't work. If there is any syntax mistake, the calculation doesn't work too. So check your input and test the calculation properly.

See also[edit]