Skip to content

Commit

Permalink
From ShoutWiki with love
Browse files Browse the repository at this point in the history
Change-Id: I8384acef88050ef221243125d95e0789c0b1b5e8
  • Loading branch information
mary-kate committed Jul 23, 2017
0 parents commit c0eee5e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/composer.lock
/node_modules/
/vendor/
*~
*.kate-swp
.*.swp
6 changes: 6 additions & 0 deletions .gitreview
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[gerrit]
host=gerrit.wikimedia.org
port=29418
project=mediawiki/extensions/RandomSelection.git
track=1
defaultrebase=0
69 changes: 69 additions & 0 deletions RandomSelection.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* RandomSelection -- randomly displays one of the given options.
* Usage: <choose><option>A</option><option>B</option></choose>
* Optional parameter: <option weight="3"> == 3x weight given
*
* @file
* @ingroup Extensions
* @version 2.2
* @date 23 June 2015
* @author Ross McClure <http://www.mediawiki.org/wiki/User:Algorithm>
* @link https://www.mediawiki.org/wiki/Extension:RandomSelection Documentation
*/
class RandomSelection {
/**
* Register the <choose> tag with the Parser.
*
* @param Parser $parser
* @return bool
*/
public static function register( &$parser ) {
$parser->setHook( 'choose', array( __CLASS__, 'render' ) );
return true;
}

/**
* Callback for register() which actually does all the processing.
*
* @param string $input User-supplied input
* @param array $argv [unused]
* @param Parser $parser
*/
public static function render( $input, $argv, $parser ) {
# Prevent caching
$parser->disableCache();

# Parse the options and calculate total weight
$len = preg_match_all(
"/<option(?:(?:\\s[^>]*?)?\\sweight=[\"']?([^\\s>]+))?"
. "(?:\\s[^>]*)?>([\\s\\S]*?)<\\/option>/",
$input,
$out
);
$r = 0;
for ( $i = 0; $i < $len; $i++ ) {
if ( strlen( $out[1][$i] ) == 0 ) {
$out[1][$i] = 1;
} else {
$out[1][$i] = intval( $out[1][$i] );
}
$r += $out[1][$i];
}

# Choose an option at random
if ( $r <= 0 ) {
return '';
}
$r = mt_rand( 1, $r );
for ( $i = 0; $i < $len; $i++ ) {
$r -= $out[1][$i];
if ( $r <= 0 ) {
$input = $out[2][$i];
break;
}
}

return $parser->recursiveTagParse( $input );
}
}
20 changes: 20 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "RandomSelection",
"version": "2.2",
"author": [
"Ross McClure"
],
"license-name": "GPL-2.0+",
"url": "https://www.mediawiki.org/wiki/Extension:RandomSelection",
"description": "Displays a random option from the given set",
"type": "parserhook",
"AutoloadClasses": {
"RandomSelection": "RandomSelection.class.php"
},
"Hooks": {
"ParserFirstCallInit": [
"RandomSelection::register"
]
},
"manifest_version": 1
}

0 comments on commit c0eee5e

Please sign in to comment.