Extension:Counter

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

Release status: beta

Implementation Parser function
Description auto count objects in a page
Author(s) Rinick
Last version 0.2
License MPL
Download No link

Check usage (experimental)

Contents

[edit] What can this extension do?

If you change some item to {{#+: item}}
You can get the number of item in this article with {{#+: ? item}}

[edit] Usage

Description You type You get
Basic usage
simple counter
* Alice: {{#+: agree}}
* Bob: {{#+: agree}}
* Carol: {{#+: disagree}}
* Dave: {{#+: agree}}
* Eve: {{#+: disagree}}
* Isaac: {{#+: agree}}
total:
* '''{{#+: ? agree}} '''
* '''{{#+: ? disagree}}'''
  • Alice: agree
  • Bob: agree
  • Carol: disagree
  • Dave: agree
  • Eve: disagree
  • Isaac: agree

total:

  • 4 agree
  • 2 disagree
simple counter with numbers
# {{#+: 1 person}}
# {{#+: 2 person}}s
# {{#+: 3 person}}s
total: '''{{#+: ? person}}s''' <br />
number of person: '''{{#+: ? | person}}'''
  1. 1 person
  2. 2 persons
  3. 3 persons

total: 6 persons
number of person: 6

a, an, one, two...twenty
{{#+: an apple}} + {{#+: a apple}}  + {{#+: one apple}} + {{#+: two apple}}s <br />
= '''{{#+: ? apple}}s'''

an apple + a apple + one apple + two apples
= 5 apples

You can count more than one item in a single tag
We have
{{#+: an apple|fruit,red}}, 
{{#+: an orange|fruit,yellow}}
and {{#+: 2 banana|fruit,yellow}}s <br />
We have '''{{#+: ? fruit}}s''', '''{{#+: ? | yellow}}''' of them are yellow

We have an apple, an orange and 2 bananas
We have 4 fruits, 3 of them are yellow

Handle special plural
you can add the results
{{#+: 1 man}} and {{#+: 1 man}} and {{#+: 2 men}} <br />
total: '''{{#+: ? men|man,men}}'''

1 man and 1 man and 2 men
total: 4 men

you can also specify that 1 man is 1 men
{{#+: 1 man|men}} plus {{#+: 5 men}}, then {{#+: 2 men|men|-2}} leave <br />
remain: '''{{#+: ? men}}'''

1 man plus 5 men, then 2 men leave
remain: 4 men

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/ExtensionName/ExtensionName.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:

#add configuration parameters here
#setup user rights here
require_once("$IP/extensions/Counter/Counter.php");

[edit] Configuration parameters

[edit] User rights

[edit] Code

<?php
 
$wgExtensionCredits['counter'][] = array(
        'name' => 'Counter',
        'version' => '0.2',
        'author' => 'Rinick',
        'description' => 'auto count objects in a page',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Counter',
);
 
$wgExtensionFunctions[] = 'wfParserFunctionCounter_Setup';
$wgHooks['LanguageGetMagic'][]       = 'wfParserFunctionCounter_Magic';
 
 
function wfParserFunctionCounter_Setup() {
        global $wgParser, $counterFunctions, $wgHooks ;
 
        $counterFunctions = new CounterParserFunctions;
 
        if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {
                $wgHooks['ParserFirstCallInit'][] = array( &$counterFunctions, 'registerParser' );
        } else {
                if ( class_exists( 'StubObject' ) && !StubObject::isRealObject( $wgParser ) ) {
                        $wgParser->_unstub();
                }
                $counterFunctions->registerParser( $wgParser );
        }
}
 
function wfParserFunctionCounter_Magic( &$magicWords, $langCode ) {
    $magicWords['+'] = array( 0, '+' );
    return true;
}
 
class CounterParserFunctions
{
        var $counterStrToNumberTable = array(
                'a' => 1, 'an' => 1, 'another' => 1, 'one' => 1,
                'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5,
                'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9,
                'ten' => 10, 'eleven' => 11, 'twelve' => 12, 'thirteen' => 13,
                'fourteen' => 14, 'fifteen' => 15, 'sixteen' => 16, 'seventeen' => 17,
                'eighteen' => 18, 'nineteen' => 19, 'twenty' => 20);
 
        var $counterDict = array();
 
        function registerParser( &$parser ) {
                $parser->setFunctionHook( '+', array(&$this, 'wfParserFunctionCounter_Render') );
                return true;
        }
        function counterStrToNumber($str)
        {
                if (array_key_exists($str, $this->counterStrToNumberTable)) //Yo I fixed a Bug, Chakaaa!!
                {
                        return $counterStrToNumberTable[$str];
                }
                return $str + 0;
        }
 
 
        function wfParserFunctionCounter_Render( &$parser, $param1 = '', $param2 = '', $param3 = '' )
        {
                global $counterDict;
 
                $str = trim($param1);
                $keys = trim($param2);
                $num = $param3 + 0;
                if ($num == 0)
                {
                        $idx = strpos($str, ' ');
                        if ($idx)
                        {
                                $num = substr($str, 0, $idx);
                                $num = $this->counterStrToNumber($num);
                                if ($num != 0)
                                {
                                        $name = substr($str, $idx + 1);
                                }
                                else if (substr($str, 0, 1) == '?')
                                {
                                        $num = '?';
                                        $name = trim(substr($str,1));
                                }
                                else
                                {
                                        $num = 1;
                                        $name = $str;
                                }
                        }
                        else
                        {
                                $num = 1;
                                $name = $str;
                        }
                }
                if ($keys)
                {
                        $keys = split(',', $keys);
                }
                else
                {
                        $keys = array($name);
                }
                if ($num == '?')
                {
                        $num = 0;
                        foreach ($keys as $key)
                        {
                                $num += $counterDict[trim($key)];
                        }
                        $str = $num . ' ' . $name;
                }
                else
                {
                        foreach ($keys as $key)
                        {
                                $counterDict[trim($key)] += $num;
                        }
                }
                return $str;
        }
}
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox