Extension:Counter

MediaWiki extensions manual
OOjs UI icon advanced.svg
Counter
Release status: unmaintained
Implementation Parser function
Description Allows to auto count objects in a page
Author(s) (Rinicktalk)
Latest version 0.2 (2010-01-05)
MediaWiki 1.15+
Database changes No
License Mozilla Public License 2.0
Download See the code section
Check usage and version matrix.

The Counter extension allows to auto count objects in a page.

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

UsageEdit

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

InstallationEdit

  • Copy the code into a file and place the file(s) in a directory called Counter in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/Counter/Counter.php";
    
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

CodeEdit

Counter.php
<?php
/**
 * Counter extension for MediaWiki
 * Allows auto counting of objects in a page
 *
 * @file
 * @ingroup Extensions
 * @author Rinick
 * @date 2010/01/05
 * @version 0.2
 * @link http://www.mediawiki.org/wiki/Extension:Counter Documentation
 * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License 2.0
 */
 
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}

$wgExtensionCredits['counter'][] = array(
        'name' => 'Counter',
	'version' => '0.2',
        'author' => 'Rinick',
        'description' => 'Allows auto counting of objects in a page',
        'url' => 'https://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;
	}
}