Extension talk:RandomSelection

From MediaWiki.org
Jump to: navigation, search
The following discussion has been transferred from Meta-Wiki.
Any user names refer to users of that site, who are not necessarily users of MediaWiki.org (even if they share the same username).

Version 1.5rc4[edit]

Just replace the caching section with this:

     # Prevent caching
     global $wgParser;

--ProgMania 00:01, 4 September 2005 (UTC)

Option tag[edit]

This is a most useful extension, but I would suggest that the <option> tag be replaced by a tag of a different name as this tag already exists in HTML and may lead to a conflict on closed wiki installations that have that tag enabled. Great script. 14:16, 8 November 2005 (UTC)

Random articles in category(ies)[edit]

An option to show a link to a random article in a specific category (or groups of categories) would be nice; randomized DPL parameters too. —Eep² 13:51, 20 August 2007 (UTC)


I notice that the extension looks at MW_PARSER_VERSION to determine which MediaWiki version is running. This value appears not to be defined at all any more by MediaWiki as of the "new" parser in MW 1.13 (and likely the later 1.12alpha) versions. Will this have any affect on operation? --Carlb 19:59, 23 February 2008 (UTC)

Well spotted! Fixed in latest version. --Algorithm 21:08, 23 February 2008 (UTC)

One issue I've noticed with the new parser (MW1.13 and later revisions of 1.12) is that template substitution is handled differently than in the old version. This can cause problems if a template containing random choose/option tags is called repeatedly from the same page. On the old MediaWiki, each invocation would yield a different random answer. On the new version, the templates are expanded first, then the same result may be placed in the page multiple times - a problem for templates like {{DidYouKnow}}-style factoids that are used repeatedly to generate one random text item per invocation. Call these as {{DidYouKnow|}} (so that the parser is fooled to think there's a parameter there, which could leave the result subject to change) and they work - an ugly kludge but necessary for pages like http://psyklopedin.org/wiki/Huvudsida and http://desciclopedia.ws/wiki/Síndrome_de_Tourette - at least if all cache support (including APC/memcached) is in use.

Wikia presumably is unaffected so far as their antiquated MediaWiki version still has the old parser, but the current MediaWiki does appear to be acting differently. ¿Que pasa?

Unfortunately, if this is the case then there's not much I can do with my extension to change it. Your hack is probably the most effective workaround for newer versions. --Algorithm 06:56, 21 April 2008 (UTC)


Maybe it's obvious, just I don't actually get how this extension is implemented into my MediaWiki project. I could start guessing which php file to extent with the code, but I'm not (yet) at that level of programming. A hint would be most appreciated. --Carsten3m 10:44, 17 April 2008 (UTC)

Figured it. For everyone else not having a clue how to install this extension: Add the code at the end of your LocalSettings.php --Carsten3m 11:01, 17 April 2008 (UTC)

The usual method of installing MediaWiki extensions is to put the code into a separate file (whatever.php) then include that file require_once('whatever.php'); somewhere in LocalSettings. This is true of MediaWiki extensions in general - not just this one. --04:46, 21 April 2008 (UTC)

Undefined property notice[edit]

HI, I've just upgraded to MW 1.12, and I'm now getting the following on my main page (which is where I'm using the extension):

Notice: Undefined property: Parser::$mTemplates in /home/restofpath/extensions/RandomSelection.php on line 64

Notice: Undefined property: Parser::$mTemplatePath in /home/restofpath/extensions/RandomSelection.php on line 65

I've also updated my parser extensions to the latest, but still get those messages.

Any ideas?

Thanks 06:38, 22 April 2008 (UTC)

If you're running 1.12, neither of those lines will ever be executed, so feel free to remove them from your local copy. --Algorithm 06:29, 24 April 2008 (UTC)

Thanks! That of course did the trick... 06:48, 24 April 2008 (UTC)

Enhancement - Multiple Items[edit]

Hi, I've knocked together an enhancement to allow multiple items to be picked from the options. I've found this useful to create my own simple ad-bar. The syntax is <choose pick="x">

Patch below -- Eclecticdave 21:13, 19 July 2008 (UTC)

diff --git a/RandomSelection.php b/RandomSelection.php
index 04fdc4b..b734473 100644
--- a/RandomSelection.php
+++ b/RandomSelection.php
@@ -29,26 +29,34 @@ function renderChosen( $input, $argv, &$parser ) {
     # Prevent caching
+    $pick = 1;
+    if (isset($argv['pick'])) $pick = intval($argv['pick']);
     # Parse the options and calculate total weight
     $len = preg_match_all("/<option(?:(?:\\s[^>]*?)?\\sweight=[\"']?([^\\s>]+))?"
         . "(?:\\s[^>]*)?>([\\s\\S]*?)<\\/option>/", $input, $out);
-    $r = 0;
+    $tw = 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];
+        $tw += $out[1][$i];
+    $input = "";
+    for($j=0; $j<$pick; $j++) {
     # Choose an option at random
-    if($r <= 0) return "";
-    $r = mt_rand(1,$r);
+        if($tw <= 0) return "";
+        $r = mt_rand(1,$tw);
     for($i=0; $i<$len; $i++) {
         $r -= $out[1][$i];
         if($r <= 0) {
-            $input = $out[2][$i];
+                $input .= $out[2][$i];
+                $tw -= $out[1][$i];
+                $out[1][$i] = 0; # Prevents this item being picked twice
+    }
     # If running new parser, take the easy way out
     if( defined( 'Parser::VERSION' ) && version_compare( Parser::VERSION, '1.6.1', '>' ) ) {

Multiple calls to Temlplated RandomSelection yield same results[edit]

I *think* this is some kind of caching issue, but it seems that if I call a template containing RandomSelection multiple times, each template call returns the same random selection on each call, i.e. {{RandomImage}}{{RandomImage}}{{RandomImage}} where RandomImage is a list of graphics, all three will return the same graphic. This is NOT the case if the same page contains multiple choose statements, however, even if those are identical.

So far, the only fix I've found is to force a no-cache on the templates like this: {{RandomImage|1}}{{RandomImage|2}}{{RandomImage|3}} which works but is a tad annoying. I'd prefer to use looping templates like this: {{4x|{{RandomImage}}}}

Any suggestions? Arodicus 20:53, 8 October 2008 (UTC)


this does not appear to be in mediawiki's svn. would be useful to have it there. NSK Nikolaos S. Karastathis 22:52, 27 October 2008 (UTC)

For some reason I can't get this extension to work.[edit]

I have the code in /extensions/RandomSelection.php and I include it in localsettings via #display random text


but it doesn't appear to be doing anything. Are you supposed to close the <?php tag? Is there something else I'm missing here?

-- 17:23, 7 October 2009 (UTC)

Doesn't work on wikipedia?[edit]

This doesn't seem to work on Wikipedia, and that's causing conflict with my signature (which works on wikia). The code section is

[[User talk:B1KWikis|<!--
--><font color="{{{c2|0000aa}}}"><!--
--><span title="Click here to talk with me"><!--



Instead of showing one option, it completely ignores it and shows <option>Wikis</option><option>Music</option><option>Games</option><option>Art</option><option>Links</option><option>Stuff</option><option>Random</option> Why won't it Work? --B1KWikis

The extension is not installed on Wikipedia (see w:Special:Version). Helder 02:57, 15 June 2011 (UTC)

I'm not surprised this isn't installed on Wikipedia; any code which displays a different result each time it's viewed inevitably has to have the 'nocache' status in the generated output, which means repeatedly having to generate the HTML for the same pages from wikicode. Wikipedia is huge and depends very heavily on squid or varnish caching to store rendered copies of everything - without it, the site would slow to a crawl. Regenerating an entire page on every page view is expensive. --Carlb (talk) 20:17, 27 February 2012 (UTC)

Extension unmaintained?[edit]

There are many questions about this extension that have gone unanswered for several years. Does this extension still have a maintainer? Badon (talk) 18:05, 17 April 2012 (UTC)

Substitute this tag?[edit]

Is it at all possible to substitute the <choose> tag so that it generates a random choice when publishing, however stays the same afterwards? User:Iggyvolz, but you'll probably find me on Wikia 21:54, 14 August 2012 (UTC)

Oops, never mind. Found what I was looking for at Extension:Dice. User:Iggyvolz, but you'll probably find me on Wikia 23:02, 14 August 2012 (UTC)

Version 2.1.7[edit]

A version 2.1.7 for MW 1.20+ authored by ShoutWiki exists, which has not been published on the extension's page. Would be a cool thing to have, though. Cheers --[[kgh]] (talk) 00:37, 12 February 2013 (UTC)

The version in question introduced one functional change: removal of the ampersand in the definition of function renderChosen to make the extension compatible with PHP 5.3. The rest of the changes were cosmetic changes and all of the mentioned changes seem to have been integrated into the upstream version (the version on the extension page). For the record, I'm the author of that changeset for ShoutWiki's version (revision 1371 on our internal SVN repository); the change was committed on 11 January 2010 and frankly, I have no clue why I decided to bump the version from 2.1.5 to 2.1.7, skipping 2.1.6, but that's what happened here. :) Hope this clears things up a bit.
Please don't hesitate to contact me or ShoutWiki's technical team if you have any questions regarding our code and open source, as open source is really important to us at ShoutWiki! --Jack Phoenix (Contact) 14:27, 18 May 2013 (UTC)

Any way to get this to work with the variable extension?[edit]

If I could get this to work with loops and with variables it'd make generating procedural content a lot easier. I am particularily thinking as to make it remember between choosing content what has been chosen before.

<option>{{#vardefine: e|1}} </option>
<option>{{#vardefine: e|2}} </option>
{{#var: e }}

Looking back on this I see that the variables works fine if called inside the choice but not once returned to the main-article. The code shows that the extension creates it's own parser, is there any way to make it use the same as the main-article?

And of course running the correct version of the extension fixed it. Nevermind my stupidity.

-- 09:49, 3 March 2013 (UTC)

License and gerrit repository.[edit]

Can this extension have proper license (such as GPL, MIT, etc..) and proper Gerrit repository? Extension stored on wikipage is a bad idea, and no license is also a bad idea. Revicomplaint? 12:43, 19 July 2014 (UTC)

Done. Leucosticte (talk) 02:49, 20 July 2014 (UTC)