Extension:Counter
From MediaWiki.org
|
Release status: beta |
|
|---|---|
| Implementation | Parser function |
| Description | |
| Author(s) | Rinick |
| Last Version | 0.1 |
| 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}}'''
|
total:
|
| simple counter with numbers |
# {{#+: 1 person}}
# {{#+: 2 person}}s
# {{#+: 3 person}}s
total: '''{{#+: ? person}}s''' <br />
number of person: '''{{#+: ? | person}}'''
|
total: 6 persons |
| 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 |
| 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 |
| 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 |
| 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 |
[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 # Define a setup function $wgExtensionFunctions[] = 'wfParserFunctionCounter_Setup'; # Add a hook to initialise the magic word $wgHooks['LanguageGetMagic'][] = 'wfParserFunctionCounter_Magic'; $counterDict = array(); function wfParserFunctionCounter_Setup() { global $wgParser; # Set a function hook associating the "example" magic word with our function $wgParser->setFunctionHook( '+', 'wfParserFunctionCounter_Render' ); } function wfParserFunctionCounter_Magic( &$magicWords, $langCode ) { # Add the magic word # The first array element is case sensitivity, in this case it is not case sensitive # All remaining elements are synonyms for our parser function $magicWords['+'] = array( 0, '+' ); return true; } 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 = 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; } $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); function counterStrToNumber($str) { global $counterStrToNumberTable; if (array_key_exists($str, $counterStrToNumberTable)) { return $counterStrToNumberTable[$str]; } return $str + 0; }