https://wiki.hard-light.net/index.php?title=User:Nx/randomselection&feed=atom&action=history
User:Nx/randomselection - Revision history
2024-03-29T08:24:17Z
Revision history for this page on the wiki
MediaWiki 1.31.7
https://wiki.hard-light.net/index.php?title=User:Nx/randomselection&diff=29679&oldid=prev
Nx: Created page with '<pre> <?php /* RandomSelection v2.1.3 -- 7/21/08 This extension randomly displays one of the given options. Usage: <choose><option>A</option><option>B</option></choose> …'
2010-06-16T21:16:36Z
<p>Created page with '<pre> <?php /* RandomSelection v2.1.3 -- 7/21/08 This extension randomly displays one of the given options. Usage: <choose><option>A</option><option>B</option></choose> …'</p>
<p><b>New page</b></p><div><pre><br />
<?php<br />
/*<br />
<br />
RandomSelection v2.1.3 -- 7/21/08<br />
<br />
This extension randomly displays one of the given options.<br />
<br />
Usage: <choose><option>A</option><option>B</option></choose><br />
Optional parameter: <option weight="3"> == 3x weight given<br />
<br />
Author: Ross McClure [http://www.mediawiki.org/wiki/User:Algorithm]<br />
*/<br />
<br />
//Avoid unstubbing $wgParser on setHook() too early on modern (1.12+) MW versions, as per r35980<br />
if ( defined( 'MW_SUPPORTS_PARSERFIRSTCALLINIT' ) ) {<br />
$wgHooks['ParserFirstCallInit'][] = 'wfRandomSelection';<br />
} else {<br />
$wgExtensionFunctions[] = 'wfRandomSelection';<br />
}<br />
<br />
$wgHooks['LanguageGetMagic'][] = 'efRandomSelection_Magic';<br />
<br />
$wgExtensionCredits['parserhook'][] = array(<br />
'name' => 'RandomSelection',<br />
'url' => 'http://www.mediawiki.org/wiki/Extension:RandomSelection',<br />
'version' => '2.1.3',<br />
'author' => 'Ross McClure',<br />
'description' => 'Displays a random option from the given set.'<br />
);<br />
<br />
function efRandomSelection_Magic( &$magicWords, $langCode) {<br />
$magicWords['choose'] = array( 0, 'choose' );<br />
return true;<br />
}<br />
<br />
function wfRandomSelection() {<br />
global $wgParser;<br />
$wgParser->setHook( 'choose', 'renderChosen' );<br />
if ( defined( get_class( $wgParser ) . '::SFH_OBJECT_ARGS' ) ) {<br />
$wgParser->setFunctionHook( 'choose', 'efRandomSelection_RenderPF_obj', SFH_OBJECT_ARGS );<br />
} else {<br />
$wgParser->setFunctionHook( 'choose', 'efRandomSelection_RenderPF');<br />
}<br />
return true;<br />
}<br />
<br />
function efRandomSelection_RenderPF_obj(&$parser, $frame, $args ) {<br />
$options = array();<br />
$r = 0;<br />
<br />
//first one is not an object<br />
<br />
$arg = array_shift( $args );<br />
$parts = explode( '=', $arg, 2);<br />
if ( count($parts) == 2 ) {<br />
$options[] = array( intval(trim($parts[0])) , $parts[1] );<br />
$r += intval(trim($parts[0]));<br />
} elseif ( count($parts) == 1 ) {<br />
$options[] = array( 1 , $parts[0] );<br />
$r += 1;<br />
}<br />
<br />
foreach ($args as $arg) {<br />
$bits = $arg->splitArg();<br />
$nameNode = $bits['name'];<br />
$index = $bits['index'];<br />
$valueNode = $bits['value'];<br />
if ( $index === '' ) {<br />
$name = trim($frame->expand( $nameNode ));<br />
$options[] = array( intval($name) , $valueNode );<br />
$r += intval($name);<br />
} else {<br />
$options[] = array( 1 , $valueNode );<br />
$r += 1;<br />
}<br />
}<br />
<br />
# Choose an option at random<br />
if($r <= 0) return '';<br />
$r = mt_rand(1,$r);<br />
for($i = 0; $i < count($options); $i++) {<br />
$r -= $options[$i][0];<br />
if($r <= 0) {<br />
$output = $options[$i][1];<br />
break;<br />
}<br />
}<br />
return $frame->expand($output);<br />
}<br />
<br />
function efRandomSelection_RenderPF( &$parser /*,...*/ ) {<br />
$args = func_get_args();<br />
array_shift($args);<br />
$parts = null;<br />
$options = array();<br />
$r = 0;<br />
foreach ($args as $arg) {<br />
//$parts = array_map( 'trim', explode( '=', $arg, 2));<br />
$parts = explode( '=', $arg, 2);<br />
if ( count($parts) == 2 ) {<br />
$options[] = array( intval(trim($parts[0])) , $parts[1] );<br />
$r += intval(trim($parts[0]));<br />
} elseif ( count($parts) == 1 ) {<br />
$options[] = array( 1 , $parts[0] );<br />
$r += 1;<br />
}<br />
}<br />
<br />
# Choose an option at random<br />
if($r <= 0) return '';<br />
$r = mt_rand(1,$r);<br />
for($i = 0; $i < count($options); $i++) {<br />
$r -= $options[$i][0];<br />
if($r <= 0) {<br />
$output = $options[$i][1];<br />
break;<br />
}<br />
}<br />
return $output;<br />
}<br />
<br />
function renderChosen( $input, $argv, $parser ) {<br />
# Prevent caching<br />
if (isset($argv['uncached'])) {<br />
$parser->disableCache();<br />
}<br />
<br />
# Parse the options and calculate total weight<br />
$len = preg_match_all("/<option(?:(?:\\s[^>]*?)?\\sweight=[\"']?([^\\s>]+))?"<br />
. "(?:\\s[^>]*)?>([\\s\\S]*?)<\\/option>/", $input, $out);<br />
$r = 0;<br />
for($i = 0; $i < $len; $i++) {<br />
if(strlen($out[1][$i])==0) $out[1][$i] = 1;<br />
else $out[1][$i] = intval($out[1][$i]);<br />
$r += $out[1][$i];<br />
}<br />
<br />
# Choose an option at random<br />
if($r <= 0) return "";<br />
$r = mt_rand(1,$r);<br />
for($i = 0; $i < $len; $i++) {<br />
$r -= $out[1][$i];<br />
if($r <= 0) {<br />
$input = $out[2][$i];<br />
break;<br />
}<br />
}<br />
<br />
# If running new parser, take the easy way out<br />
if( defined( 'Parser::VERSION' ) && version_compare( Parser::VERSION, '1.6.1', '>' ) ) {<br />
return $parser->recursiveTagParse($input);<br />
}<br />
<br />
<br />
# Otherwise, create new parser to handle rendering<br />
$localParser = new Parser();<br />
<br />
# Initialize defaults, then copy info from parent parser<br />
$localParser->clearState();<br />
$localParser->mTagHooks = $parser->mTagHooks;<br />
$localParser->mTemplates = $parser->mTemplates;<br />
$localParser->mTemplatePath = $parser->mTemplatePath;<br />
$localParser->mFunctionHooks = $parser->mFunctionHooks;<br />
$localParser->mFunctionSynonyms = $parser->mFunctionSynonyms;<br />
<br />
# Render the chosen option<br />
$output = $localParser->parse($input, $parser->mTitle,<br />
$parser->mOptions, false, false);<br />
return $output->getText();<br />
<br />
}<br />
</pre></div>
Nx