Extension:RomanNumbers

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
RomanNumbers

Release status: beta

Implementation Parser extension, Parser function
Description Convert a roman number to an arabic one or vice versa.
Author(s) Massimiliano Salvemini
Version 1.1
MediaWiki ≥ 1.8.0
Download see below

Add a parser function that converts one arabic number to a roman number or vice versa. If the argument is a string, it try to parse the string and convert it in an arabic number, while, if the argument is an arabic number, the parser converts it in a roman number.

The allowed range is from 1 to 9999 on the conversion arabic to roman, while when converting a roman number to an arabic one the conversion range is limited only by PHP variable's size.

[edit] Examples

{{#roman:123}} returns CXXIII
{{#roman:MDXCII}} returns 1,592
{{#roman:xMyDzXCII}} returns always 1,592 (unrecognized chars are skipped)
{{#roman:123MII}} returns 1,002


Here there is a live example of a page using this parser function.

[edit] Installation

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/RomanNumbers/RomanNumbers.php");

You must create a directory called RomanNumbers in the extensions dir.

[edit] RomanNumbers.php

You can create a file called RomanNumbers.php in the RomanNumbers directory. The file must contain the following code:

<?
if (!defined('MEDIAWIKI'))
        die('<pre>This file is a MediaWiki extension, it is not a valid entry point</pre>');
 
 
class RomanNumbers {
        function toArabic($roman)
        {
                $conv = array(
                        array("letter" => 'I', "number" => 1),
                        array("letter" => 'V', "number" => 5),
                        array("letter" => 'X', "number" => 10),
                        array("letter" => 'L', "number" => 50),
                        array("letter" => 'C', "number" => 100),
                        array("letter" => 'D', "number" => 500),
                        array("letter" => 'M', "number" => 1000),
                        array("letter" => 0,   "number" => 0)
                );
                $arabic = 0;
                $state  = 0;
                $sidx   = 0;
                $len    = strlen($roman);
 
                while ($len >= 0) {
                        $i = 0;
                        $sidx = $len;
 
                        while ($conv[$i]['number'] > 0) {
                                if (strtoupper($roman[$sidx]) == $conv[$i]['letter']) {
                                        if ($state > $conv[$i]['number']) {
                                                $arabic -= $conv[$i]['number'];
                                        } else {
                                                $arabic += $conv[$i]['number'];
                                                $state   = $conv[$i]['number'];
                                        }
                                }
                                $i++;
                        }
 
                        $len--;
                }
 
                return($arabic);
        }
        function toRoman($num) {
                if ($num < 0 || $num > 9999) return -1;
 
                $romanOnes = array(1=> "I",2=>"II",3=>"III",4=>"IV", 5=>"V", 6=>"VI", 7=>"VII", 8=>"VIII", 9=>"IX"   );
                $romanTens = array(1=> "X", 2=>"XX", 3=>"XXX", 4=>"XL", 5=>"L", 6=>"LX", 7=>"LXX",8=>"LXXX", 9=>"XC");
                $romanHund = array(1=> "C", 2=>"CC", 3=>"CCC", 4=>"CD", 5=>"D", 6=>"DC", 7=>"DCC",8=>"DCCC", 9=>"CM");
                $romanThou = array(1=> "M", 2=>"MM", 3=>"MMM", 4=>"MMMM", 5=>"MMMMM", 6=>"MMMMMM",7=>"MMMMMMM", 8=>"MMMMMMMM", 9=>"MMMMMMMMM");
 
                $ones = $num % 10;
                $tens = ($num - $ones) % 100;
                $hund = ($num - $tens - $ones) % 1000;
                $thou = ($num - $hund - $tens - $ones) % 10000;
 
                $tens = $tens / 10;
                $hund = $hund / 100;
                $thou = $thou / 1000;
 
                if ($thou) $romanNum .= $romanThou[$thou];
                if ($hund) $romanNum .= $romanHund[$hund];
                if ($tens) $romanNum .= $romanTens[$tens];
                if ($ones) $romanNum .= $romanOnes[$ones];
 
        return $romanNum;
 
        }
}
 
 
$wgExtensionFunctions[] = 'wfRomanParse_Setup';
$wgHooks['LanguageGetMagic'][] = 'wfRomanParse_Magic';
 
function wfRomanParse_Setup() {
        global $wgParser;
        $wgParser->setFunctionHook( 'roman', 'wfRomanParse_Render' );
}
 
function wfRomanParse_Magic( &$magicWords, $langCode ) {
        $magicWords['roman'] = array( 0, 'roman' );
        return true;
}
 
function wfRomanParse_Render(&$parser, $param1 = '') {
        return convertRomanNumber($param1);
}
 
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'RomanNumbers',
        'author' =>'Massimiliano Salvemini', 
        'version' => 1.0,
        'url' => 'http://www.mediawiki.org/wiki/Extension:RomanNumbers', 
        'description' => 'Converts an arabic number to a roman number and vice versa'
        );
 
$wgAjaxExportList[] = "convertRomanNumber";
 
function convertRomanNumber($num) {
if (is_numeric($num)) {
                if (($num > 9999)||($num<1)) return "error";
        return RomanNumbers::toRoman($num);
}
else
        return number_format(RomanNumbers::toArabic($num));
}
Personal tools