Extension:Hebrew Numbers

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
Hebrew Numbers

Release status: experimental

Implementation Parser function
Description Converts numbers to hebrew numerals
Author(s) Ike Hecht (tosfosTalk)
Last version 0.1 (1-13-2010)
MediaWiki 1.15.0 (probably earlier too)
License GPL
Download See below

Check usage (experimental)

Contents

[edit] What can this extension do?

Hebrew Numbers is a MediaWiki extension that converts numbers to hebrew numerals. It contains a parser function "hebnum" which can take a number up to 9,999 and output its hebrew representation. You can also optionally represent a talmudic folio and side.

[edit] Usage

{{ #hebnum: number }}

You may optionally end the number with an a or b to denote a side of a talmudic folio.

[edit] Examples

  • {{#hebnum:6}} → ו׳
  • {{#hebnum:3731}} → ג׳תשל״א
  • {{#hebnum:90b}} → צ׃


[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/HebrewNumbers/HebrewNumbers.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

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

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


[edit] Code

<?php
/**
 * Hebrew Numbers is a MediaWiki extension that converts numbers to hebrew
 * numerals. See http://en.wikipedia.org/wiki/Hebrew_numerals
 *
 * It contains a parser function "hebnum" which can either take a number
 * or a talmudic folio and side (up to 9,999), with the side represented by "a" or "b" 
 *
 * Examples:
 * {{#hebnum:6}} outputs כ״ג
 * {{#hebnum:3731}} outputs ג׳תשל״א
 * {{#hebnum:90b}} outputs צ׃
 * 
 * @author Ike Hecht
 */
 
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is a MediaWiki extension; it is not a valid entry point.' );
}
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Hebrew Numbers',
        'version' => '0.1',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Hebrew_Numbers',
        'author' => 'Ike Hecht',
        'description' => 'Turn numbers into Hebrew numerical representation',
);
 
$wgHooks['LanguageGetMagic'][]       = 'efHebrewNumbersLanguageGetMagic';
 
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
        $wgHooks['ParserFirstCallInit'][] = 'efHebrewNumbersSetup';
} else {
        $wgExtensionFunctions[] = 'efHebrewNumbersSetup';
}
 
define( 'HEBREW_NUMBERS_NAME', 'hebnum' );
 
function efHebrewNumbersLanguageGetMagic( &$magicWords, $langCode ) {
        $magicWords[HEBREW_NUMBERS_NAME] = array( 0, HEBREW_NUMBERS_NAME );
        return true;
}
 
function efHebrewNumbersSetup( $parser ) {
        $parser->setFunctionHook( HEBREW_NUMBERS_NAME, 'efHebrewNumbersCalc' );
        return true;
}
 
function efHebrewNumbersCalc( &$parser, $text ) {
        $hebnum = new HebrewNumber( $text );
        return $hebnum->convertNumberToHebrew();
}
 
 
// Converts a given number to its Hebrew numeral representation
class HebrewNumber
{
        var $text;
        var $number;
        var $daf;
 
        function __construct( $in ) {
                $this->text = $in;
        }
 
        function convertNumberToHebrew() {
                mb_language( 'uni' );
                mb_internal_encoding( 'UTF-8' );
 
                // split off daf
                $lastChar = strtolower( $this->text { strlen( $this->text ) - 1 } );
                if ( $lastChar == 'a' or $lastChar == 'b' ) {
                        $this->daf = $lastChar;
                }
 
                $this->number = intval( $this->text );
                $output = "";
 
                // Do thousands
                $thousands = $this->calcOnes( intval( $this->number / 1000 ) % 10 );
                if ( $thousands != null )
                        $output .= $thousands . unichr( hexdec( "5F3" ) );
 
                // Do hundreds
                $hundreds = $this->calcHundreds( intval( $this->number / 100 ) % 10 );
                $output .= $hundreds;
 
                // fix exceptions
                if ( $this->number % 100 == 15 ) {
                        $ones = $this->calcOnes( 9 );
                        $output .= $ones;
                        $ones = $this->calcOnes( 6 );
                        $output .= $ones;
                }
                elseif ( $this->number % 100 == 16 ) {
                        $ones = $this->calcOnes( 9 );
                        $output .= $ones;
                        $ones = $this->calcOnes( 7 );
                        $output .= $ones;
                }
                else {
                        // Do tens
                        $tens = $this->calcTens( intval( $this->number / 10 ) % 10 );
                        $output .= $tens;
 
                        // Do ones
                        $ones = $this->calcOnes( intval( $this->number ) % 10 );
                        $output .= $ones;
                }
                // Add dot or apostrophe
                if ( $this->daf == 'a' ) {
                                $output .= '.'; // This is really the wrong symbol.
                } elseif ( $this->daf == 'b' ) {
                                $output .= unichr( hexdec( "5C3" ) );
                } elseif ( mb_strlen( $output ) > 1 ) {
                                $output = mb_substr( $output, 0, - 1 ) . unichr( hexdec( "5F4" ) ) . mb_substr( $output, - 1, mb_strlen( $output ) + 1 );
                } elseif ( mb_strlen( $output ) == 1 ) {
                                $output .= unichr( hexdec( "5F3" ) );
                }
 
                return "<span dir=\"rtl\" lang=\"he\">$output</span>";
        }
 
        function calcHundreds( $digit ) {
                if ( $digit != null ) {
                        $output = "";
                        while ( $digit >= 4 ) {
                                $output .= unichr( hexdec( "5EA" ) );
                                $digit -= 4;
                        }
                        // add number to numerical value for unicode character before "kuf"
                        if ( $digit > 0 )
                                $output .= unichr( $digit + 1510 );
 
                        return $output;
                }
        }
 
        function calcTens( $digit ) {
                if ( $digit != null )
                {
                        // store the unicode value in decimal of hebrew representation for tens
                        $tensUnicodes = array( "5D9", "5DB", "5DC", "5DE", "5E0", "5E1", "5E2", "5E4", "5E6" );
                        return unichr( hexdec( $tensUnicodes[$digit - 1] ) );
                }
        }
 
        function calcOnes( $digit ) {
                if ( $digit != null )
                        // add number to numerical value for unicode character before "aleph"
                        return unichr( $digit + 1487 );
        }
}
 
// returns a one character string containing the unicode character specified by unicode
// from http://www.php.net/manual/en/function.chr.php#88611
function unichr( $unicode ) {
        return mb_convert_encoding( '&#' . $unicode . ';', 'UTF-8', 'HTML-ENTITIES' );
}
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox