Open main menu

Extension:Whitelist Regex

MediaWiki extensions manual
OOjs UI icon advanced.svg
Whitelist Regex
Release status: beta
Implementation User rights
Description Allows pages to be read-whitelisted based on a regular expression
Author(s) Sylvain Lerouxtalk
Latest version 0.1 (2010-02-01)
MediaWiki tested with 1.15.1
PHP tested with 5.1
License GPLv2 or later
Download See the section labeled code below
$egRegexReadWhitelist
Translate the Whitelist Regex extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?Edit

If you have a private wiki, you may wish to made some groups of pages publicly readable. This extension extends the default MediaWiki Manual:$wgWhitelistRead option by allowing one to identify the whitelisted pages by their namespace and a regular expression. Thus allowing to made public either a whole namespace and/or some groups of pages, based on their title.

This work is based on Extension:Whitelist_Namespaces by User:Bawolff

Usage sampleEdit

// Required, since it is pointless to bother with whitelist if
// the wiki is world readable (I think).
$wgGroupPermissions['*']['read']=false;

require_once("extensions/WhitelistRegex.php");
$egRegexReadWhitelist = Array();
$egRegexReadWhitelist[NS_MAIN][] = "!^(Accueil|Main Page|Welcome)$!";
$egRegexReadWhitelist[NS_USER][] = "!^Sylvain$!";
$egRegexReadWhitelist[NS_USER][] = "!^Sylvain/public/.*$!";

$wgExtraNamespaces = array(202 => "Public", 203 => "Public_talk");
$egRegexReadWhitelist[202][] = "!.*!";

Please note:

  • That the use of start (^) and end ($) delimiters in the regular expression is strongly encouraged. Otherwise a partial match will be made. As an example, You are not Welcome will match !Welcome! but not !^Welcome$!.
  • In all examples of this page, I use a bang (!) as delimiter for the regular expression instead of the most usual slash (/) since the later will clash with some page titles – notably in sub-pages (and I find the use of a backslash to shield it both less readable and more error prone).

Download instructionsEdit

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

InstallationEdit

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

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

Configuration parametersEdit

The global variable $egRegexReadWhitelist holds an array with an entry for each namespace (identified by their numeric ID) handled by this extension. In its turn, each entry holds an array of PCRE regular expressions. Any page matching one of the regular expressions in the corresponding namespace entry will be read-whitelisted. Here are some examples:

$egRegexReadWhitelist[NS_MAIN][] = "!^(Accueil|Main Page)$!"
Grant anyone read access to the pages Accueil and Main Page in the main (NS_MAIN) namespace. Please note that a space is used in Main Page, since the pattern is matched against the title text – not the DB key (i.e.: by using _ instead of space).
$egRegexReadWhitelist[NS_USER][] = "!^Sylvain$!";
Grant anyone read access to the Sylvain page in the User namespace.
$egRegexReadWhitelist[NS_USER][] = "!^Sylvain/public/.*$!"
Grant anyone read access to any sub-page of Sylvain/public/ in the User namespace.
$egRegexReadWhitelist[202][] = "!.*!";
Grant anyone read access to any page in the namespace whose id is 202 (but not to the corresponding talk pages).

CodeEdit

<?php
if ( ! defined( 'MEDIAWIKI' ) )
    die();
/*
 Whitelist Regex 0.1 (beta) - Feb. 1st, 2010.
 http://www.mediawiki.org/wiki/Extension:Whitelist_Regex
 @author Sylvain Leroux <sylvain@chicoree.fr>
 
 Purpose:       Allows pages to be read-whitelisted based on a 
                regular expression (PCRE).
 
 Based on http://www.mediawiki.org/wiki/Extension:Whilelist_namespaces
 by n:en:User:Bawolff <http://en.wikinews.org/wiki/User:Bawolff>
 
 ----
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or 
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License along
 with this program; if not, write to the Free Software Foundation, Inc.,
 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 http://www.gnu.org/copyleft/gpl.html
 
 
 To install, add following to LocalSettings.php
   require_once("extensions/WhitelistRegex.php");
   $egRegexReadWhitelist = Array();
 
 Then, add to $egRegexReadWhitelist regular
 expressions whose matching page in the corresponding namespace will be 
 white-listed. Huh? Here is a example to made it clear:
   // Grant to anyone read access to Accueil and Main Page in the NS_MAIN
   // namespace (please note the use of a 'space' in "Main Page":
   // Pages are identified by their title text, not DB key) 
   $egRegexReadWhitelist[NS_MAIN][] = "!^(Accueil|Main Page)$!";
   // Grant to anyone access to User:Sylvain
   $egRegexReadWhitelist[NS_USER][] = "!^Sylvain$!";
   // Grant to anyone access to any page in the User namespace
   // whose title starts with "Sylvain/public/".
   $egRegexReadWhitelist[NS_USER][] = "!^Sylvain/public/.*$!";
   // Define a new custom namespace, and grant to anyone read access to 
   // the corresponding articles, but not to talk pages.
   $wgExtraNamespaces = array(202 => "Public", 203 => "Public_talk");
   $egRegexReadWhitelist[202][] = "!.*!";
 
 Note this will useless if the wiki is world readable. You should
 make the wiki not readable (ex $wgGroupPermissions['*']['read']=false;)
 before you can whitelist specific groups of pages as readable.
*/
 
 
$egRegexReadWhitelist = Array(); #default. override in LocalSettings.php

$wgHooks['userCan'][] = 'efExtensionWhitelistRegex';
$wgExtensionCredits['other'][] = array(
        'name' => 'Whitelist Regex',
        'description' => 'Allows pages to be read-whitelisted based on regular expressions',
        'url' => 'http://www.mediawiki.org/wiki/Extension:Whitelist_Regex',
        'author' => '[http://www.mediawiki.org/wiki/User:Sylvain_Leroux Sylvain Leroux]',
        'version' => '0.1'
);
 
 
function efExtensionWhitelistRegex(&$title, &$user, $action, &$result) {
    global $egRegexReadWhitelist;
    if ('read' === $action) {
        $pageTitle = $title->getText();
        $pageNs = $title->getNamespace();
        
        if (array_key_exists($pageNs, $egRegexReadWhitelist)) {
            foreach ($egRegexReadWhitelist[$pageNs] as $pattern) {
                if (preg_match($pattern, $pageTitle)) {
                    $result = true;
                    return false;
                }
            }
        }
    }
    return true;

}

See alsoEdit