Extension:RandomSelection
| This extension stores its source code on a wiki page. Please be aware that this code may be unreviewed or maliciously altered. They may contain security holes, outdated interfaces that are no longer compatible etc. Note: No localisation updates are provided for this extension by translatewiki.net. |
|
RandomSelection Release status: stable |
|||
|---|---|---|---|
| Implementation | Tag | ||
| Description | Displays a random option from the given set. | ||
| Author(s) | Ross McClure (Algorithmtalk) | ||
| Last version | 2.1.6 (2012-09-02) | ||
| MediaWiki | 1.12+ | ||
| Database changes | no | ||
| License | No license specified | ||
| Download | see below Changelog |
||
| Example | Example | ||
|
|||
|
|||
| Check usage and version matrix | |||
The RandomSelection extension allows for randomly-generated content inside your wiki; e.g. rotating images, random greetings, etc.
Contents |
Usage [edit]
To choose between one or more options, simply place each option inside an <option> tag, like so:
<choose> <option>This is the first choice.</option> <option>This is the second choice.</option> <option>And so on...</option> </choose>
From each set of <choose> tags, precisely one <option> tag will be selected at random, and its contents shown. Anything can be placed inside of option tags: links, images, even templates. Note, however, that template arguments will not be expanded due to bugzilla:2257.
Weight [edit]
If you'd like some options to be shown more often than others, you can add weight to the option tags, like so:
<choose> <option weight="2">This option will be shown twice as often as a normal option.</option> <option>By default, options have a weight of 1.</option> <option weight="1.5">This option also has a weight of 1. Fractional portions are ignored.</option> </choose>
You can also have what is placed inside the option tags be sent as an argument to a template, e.g.:
Choice templates [edit]
<choose> <option>Template:Featured article/Elephant</option> <option>Template:Featured article/Giraffe</option> <option>Template:Featured article/Rhinoceros</option> <choicetemplate>PageWithEditButton</choicetemplate> </choose>
You could then create a Template:PageWithEditButton with these contents:
[{{canonicalurl:{{{1}}}|action=edit}} Edit] {{{{{1}}}}}
This is useful if you want to set up randomly rotating featured articles and have an edit button to allow the user to edit the featured article blurbs.
Installation [edit]
- Save the source code into a file called RandomSelection.php
- Create a folder called RandomSelection in the extensions directory of your installation (.../extensions/) and move the file RandomSelection.php into it
- Add following line in LocalSettings.php:
require_once( "$IP/extensions/RandomSelection/RandomSelection.php" );
- Installation may now be verified at Special:Version
Source Code [edit]
The code in this page will only work in MediaWiki 1.12 and above. (Alternate version for 1.5 and above).
- RandomSelection.php
<?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 RandomSelection.php * @ingroup Extensions * @version 2.1.6 * @date 17 January 2010 * @author Ross McClure <https://www.mediawiki.org/wiki/User:Algorithm> * @link https://www.mediawiki.org/wiki/Extension:RandomSelection Documentation */ if( !defined( 'MEDIAWIKI' ) ) { die( "This is not a valid entry point to MediaWiki.\n" ); } $wgHooks['ParserFirstCallInit'][] = 'wfRandomSelection'; $wgExtensionCredits['parserhook'][] = array( 'name' => 'RandomSelection', 'version' => '2.1.6', 'author' => 'Ross McClure', 'description' => 'Displays a random option from the given set', 'url' => 'https://www.mediawiki.org/wiki/Extension:RandomSelection', ); function wfRandomSelection( &$parser ) { $parser->setHook( 'choose', 'renderChosen' ); return true; } function renderChosen( $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 ); # Find any references to a surrounding template preg_match_all( "/<choicetemplate(?:(?:\\s[^>]*?)?\\sweight=[\"']?([^\\s>]+))?" . "(?:\\s[^>]*)?>([\\s\\S]*?)<\\/choicetemplate>/", $input, $outTemplate ); $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; } } # Surround by template if applicable if ( isset ( $outTemplate[2][0] ) ) { $input = '{{' . $outTemplate[2][0] . '|' . $input . '}}'; } # Parse tags and return return $parser->recursiveTagParse( $input ); }
Changelog [edit]
- 2012-09-02 -- Added
<choicetemplate>functionality - 2007-12-10 -- Updated for compatibility with MW 1.12's new parser model
- 2008-02-23 -- Minor bugfix
- 2008-05-08 -- Bugfix
- 2008-07-01 -- Use ParserFirstCallInit hook if it's available
- 2009-09-30 -- Doxygen comments added + minor tweaks
- 2010-01-17 -- dropped backwards compatibility with old MediaWikis as per r55838 and made the extension work under PHP 5.3+
Tested [edit]
- MW 1.19.x -
OK --Leucosticte (talk) 21:58, 2 September 2012 (UTC) - MW 1.16.x -
OK --[[kgh]] (talk) 22:10, 7 June 2012 (UTC) - MW 1.18.x -
OK --[[kgh]] (talk) 18:37, 7 June 2012 (UTC)