-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I8384acef88050ef221243125d95e0789c0b1b5e8
- Loading branch information
0 parents
commit c0eee5e
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/composer.lock | ||
/node_modules/ | ||
/vendor/ | ||
*~ | ||
*.kate-swp | ||
.*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |