Extension talk:RandomSelection/Archive

Version 1.5rc4
Just replace the caching section with this:

# Prevent caching global $wgParser; $wgParser->disableCache;

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

Option tag
This is a most useful extension, but I would suggest that the 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. 86.132.17.233 14:16, 8 November 2005 (UTC)

Random articles in category(ies)
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)

MW_PARSER_VERSION?
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 -style factoids that are used repeatedly to generate one random text item per invocation. Call these as (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)

Installation
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
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 118.92.29.247 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... 118.92.29.247 06:48, 24 April 2008 (UTC)

Enhancement - Multiple Items
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

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 $parser->disableCache; +   $pick = 1; +   if (isset($argv['pick'])) $pick = intval($argv['pick']); +     # Parse the options and calculate total weight $len = preg_match_all("/]*?)?\\sweight=[\"']?([^\\s>]+))?"        . "(?:\\s[^>]*)?>([\\s\\S]*?)/", $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             break;         }     } +    }     # If running new parser, take the easy way out if( defined( 'Parser::VERSION' ) && version_compare( Parser::VERSION, '1.6.1', '>' ) ) {