Extension:Random

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
Random

Release status:Extension status unmaintained

ImplementationTemplate:Extension#type Tag, Parser function
DescriptionTemplate:Extension#description Randomly picks one or more items.
Author(s)Template:Extension#username darklamatalk
Latest versionTemplate:Extension#version 0.3
MediaWikiTemplate:Extension#mediawiki 1.17 - 1.21
LicenseTemplate:Extension#license GNU General Public License 2.0 or later
Download See the code section on this page
TagsTemplate:Extension#tags
<random>
Hooks usedTemplate:Extension#hook
LanguageGetMagicManual:Hooks/LanguageGetMagic
ParserFirstCallInitManual:Hooks/ParserFirstCallInit

Translate the Random extension if it is available at translatewiki.net

Check usage and version matrix.

The Random extension is designed to return one or more items parsed as wikitext in a randomly shuffled order. A random shuffle prevents an item from being displayed twice by the same call to random, unless two or more items contain exactly the same contents.

Semi-random numbers can be generated without an extension like this, using magic words and ParserFunctions - see w:Template:Random number for an example.

For information about the unrelated native feature Special:Random, see Help:Random page.

Installation[edit]

  • Copy the code into a file and place the file(s) in a directory called Random in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php:
require_once "$IP/extensions/Random/Random.php";
  • YesY Done - Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Examples[edit]

tag parser function what happens?
<random count="2">
<item>one</item>
<item>two</item>
<item>three</item>
</random>
{{#random:2
| one
| two
| three
}}
The result is that "onetwo", "onethree", "twoone", "twothree", "threeone", or "threetwo" is returned at random.
<random>
<item>[[one]]</item>
<item>[[two]]</item>
<item>[[three]]</item>
</random>
{{#random:1
| [[one]]
| [[two]]
| [[three]]
}}
The result is that "one", "two", or "three" is returned at random as a link. You can use any wiki mark up that could normally be used.
<random>
<format>[[%ITEM%]]</format>
<item>one</item>
<item>two</item>
<item>three</item>
</random>
not available The result is one, two, or three is returned at random as a link. The format tag provides an easy way to repeat any desired formatting. %ITEM% refers to the content of a picked item, and must be present at least once in a format tag. Using this method, for example, an image (with external link and caption) could be used as an item: <item>File:example.jpg|link=http://wikipedia.org/wiki/Test|caption</item>.
<random>
<format>[[%ITEM%]]</format>
<item>one</item>
<item>two</item>
<item>three</item>
<format>* %ITEM%</format>
<item>one</item>
<item>two</item>
<item>three</item>
</random>
not available The result is one, two, or three is returned at random either as a link or as a unsorted list. More than one format tag can be used to change the formatting of items following it.
Incorporating into a Template

If desired, the <random></random> tag (including the enclosed list of items) can be placed in a template (e.g. Template:MyRandomWikiItems ) and then just the corresponding template tag (e.g. {{MyRandomWikiItems}} ) could be placed within a wiki page. (It is then highly recommended that a wiki administrator "protect" such a template if it is not meant to be changed by casual wiki users.)

Code[edit]

Random.php
<?php

/**
 * @package MediaWiki
 * @subpackage Extensions
 * @author darklama
 * @license https://www.gnu.org/licenses/gpl.html
 * @licence https://creativecommons.org/licenses/by-sa/3.0
 */

if ( !defined( 'MEDIAWIKI' ) ) {
  echo( "This is a mediaWiki extension and cannot be run standalone.\n" );
  die( -1 );
}

$wgHooks['LanguageGetMagic'][] = 'efRandomLanguageGetMagic';
$wgHooks['ParserFirstCallInit'][] = 'efRandomExtension';
$wgExtensionCredits['other'][] = array(
  'name'        => 'Random' ,
  'version'     => '0.3',
  'description' => 'Includes a random line of wikitext from given items',
  'author'      => '[https://en.wikiversity.org/wiki/User:Darklama darklama]',
  'url'         => 'https://www.mediawiki.org/wiki/Extension:Random',
);

function efRandomLanguageGetMagic( $magicWords, $langCode ) {
  $magicWords['random'] = array( 0, 'random' );
  return true;
}

function efRandomExtension( $parser ) {
  $parser->setHook( 'random', 'renderRandom' );
  $parser->setFunctionHook( 'random', 'randomObj', SFH_OBJECT_ARGS );
  return true;
}

function renderRandom( $input, $argv, $parser ) {
  $count = intval( $argv['count'] );
  $result = '';
  $format = '%ITEM%';
  $items = array();
  $elements = array( 'item', 'format' );

  $text = Parser::extractTagsAndParams( $elements, $input, $matches );

  foreach ( $matches as $marker => $data ) {
    list( $element, $content, $params, $tag ) = $data;
    if ( $element === 'item' ) {
      $content = trim( $content, " \t" );
      $content = trim( $content, "\n" );
      $items[] = str_replace( '%ITEM%', $content, $format );
    } else if ( $element === 'format' ) {
      if ( !empty( $content ) && preg_match( '/%ITEM%/', $content ) != 0 ) {
        $format = str_replace( '«', '<', $content );
        $format = str_replace( '»', '>', $format );
        $format = trim( $format, " \t" );
        $format = trim( $format, "\n" );
        $format = str_replace( '\n', "\n", $format );
      }
    }
  }

  $entries = count( $items );
  if ( $entries == 0) {
    return '';
  }
  if ( $count <= 0 ) {
    $count = 1;
  } else if ( $count > $entries ) {
    $count = $entries;
  }

  $keys = array_rand( $items, $count );

  if ( is_array( $keys ) ) {
    foreach ( $keys as $key ) {
      $result .= $parser->recursiveTagParse( $items[$key] );
    }
  } else {
    $result = $parser->recursiveTagParse( $items[$keys] );
  }

  $parser->disableCache();
  return $result;
}

function randomObj( $parser, $frame, $args ) {
  $count = isset($args[0]) ? intval(trim( $frame->expand( $args[0] ) ) ) : 1;
  $entries = count($args);
  $result = '';

  if ( $entries <= 0 ) {
    return '';
  } else if ( $entries == 1 ) {
    return trim( $frame->expand( $args[0] ) );
  } else if ( $count > ($entries-1) ) {
    $count = $entries-1;
  } else if ( $count <= 0 ) {
    $count = 1;
  }

  unset($args[0]);
  $keys = array_rand( $args, $count );

  if ( is_array( $keys ) ) {
    foreach ( $keys as $key ) {
      $result .= ltrim( $frame->expand( $args[$key] ) );
    }
  } else {
    $result = ltrim( $frame->expand( $args[$keys] ) );
  }

  $parser->disableCache();
  return rtrim( $result );
}

Alternatives[edit]

See also[edit]