Extension:RandomPageSettings

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
Random Page Settings

Release status: beta

Description Extension to allow the wiki administrator to control the behaviour of Special:Random
Author(s) Säsongsmat (rotseeTalk)
License BSD
Download No link
Hooks used
SpecialRandomGetRandomTitle

Check usage (experimental)

As pointed out on the discussion page, this extension in it's current state contains some vulnerabilities, and should be rewritten. Feel free to help out!

Contents

[edit] What can this extension do?

Enables the wiki administrator to control the behaviour of Special:Random.

[edit] Usage

In LocalSettings.php, add a variable $wgRandomPageSettings with the settings you want to do. Possible settingas are:

namespaces, to chose what namespaces to get random pages from, e.g. $wgRandomPageSettings['namespaces'] = array( NS_MAIN, NS_PROJECT);

sameLanguage, to get only pages in the same language as the current page, e.g. $wgRandomPageSettings['sameLanguage'] = true;

noSubpages, to discard any subpages (actually discards any page with a '/' in the title), e.g. $wgRandomPageSettings['noSubpages'] = true;

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/RandomPageSettings/RandomPageSettings.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/RandomPageSettings/RandomPageSettings.php");

You are now ready to add settings with $wgRandomPageSettings in LocalSettings.php, as described above.

[edit] Code

<?php
/**
 * Extension to allow the wiki administrator to control the behaviour of Special:Random
 
 * @author Leo Wallentin, http://xn--ssongsmat-v2a.nu
 * @ingroup Extensions
 * @url http://www.mediawiki.org/wiki/Extension:RandomPageSettings
 * @licence BSD
 */
 
/*
Usage:
 
In LocalSettings.php, add a variable $wgRandomPageSettings with the settings you want to do. Possible settingas are:
 
namespaces, to chose what namespaces to get random pages from,
e.g. $wgRandomPageSettings['namespaces'] = array( NS_MAIN, NS_PROJECT);
 
sameLanguage, to get only pages in the same language as the current page,
e.g. $wgRandomPageSettings['sameLanguage'] = true;
 
noSubpages, to discard any subpages (actually discards any page with a '/' in the title),
e.g. $wgRandomPageSettings['noSubpages'] = true;
 
Changelog:
 
* 1.0 - first version
 
TODO:
 
 - add user settings
 
*/
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
$wgHooks['SpecialRandomGetRandomTitle'][] = 'fnSpecialRandomGetRandomTitle';
 
function fnSpecialRandomGetRandomTitle (&$rand, &$isRedir, &$namespaces, &$extra, &$title) {
 
        /* fetch global settings */
        global $wgRandomPageSettings;
 
        /* return if no options are set*/
        if ( !isset($wgRandomPageSettings) || !is_array($wgRandomPageSettings))
                return true;
 
        /* only return pages in the same language? 
           from an extension by WindPower at teamfortress.org */
        if ( isset($wgRandomPageSettings['sameLanguage']) && $wgRandomPageSettings['sameLanguage'] ) {
 
                if(preg_match('%/wiki/[^:\\s]+/([-\\w]+)$%i', $_SERVER['HTTP_REFERER'], $regs)) {
 
                        $extra[] = "`page_title` LIKE '%/".rawurlencode($regs[1])."'";
 
                } else {
 
                        $extra[] = "`page_title` NOT LIKE '%/%'";
 
                }
 
        }
 
        /* do not return subpages */
        if ( isset($wgRandomPageSettings['noSubpages']) && $wgRandomPageSettings['noSubpages'] ) {
 
                $extra[] = "`page_title` NOT LIKE '%/%'";
 
        }
 
        /* override namespaces */
        if ( isset($wgRandomPageSettings['namespaces']) && $wgRandomPageSettings['namespaces'] ) {
 
                if ( !is_array($wgRandomPageSettings['namespaces']) )
                        $wgRandomPageSettings['namespaces'] = array($wgRandomPageSettings['namespaces']);
 
                $namespaces = $wgRandomPageSettings['namespaces'];
 
        }
 
        return true;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox