Extension:WhitelistedNamespaces

From mediawiki.org
MediaWiki extensions manual
WhitelistedNamespaces
Release status: unmaintained
Implementation User rights
Description Provides the capability whitelist an entire namespace or array of namespaces
Author(s) Lisa Ridley (Lhridleytalk)
Latest version 0.85 beta (2010-02-25)
MediaWiki 1.11+
Database changes No
License GNU General Public License 2.0 or later
Download Code below

What can this extension do?[edit]

For wikis that choose to restrict pages that anonymous users can see, this extension provides the capability of whitelisting an entire namespace or array of namespaces.

This extension will work in conjunction with specific pages that have been whitelisted using $wgWhitelistRead

Basic usage[edit]

To utilize this extension, a basic understanding of Group Permissions and Whitelisted Pages is needed. If you are not familiar with these concepts, please take a moment to read the following pages:

Compatibility[edit]

  • MediaWiki version 1.11 and higher

Note: This extension utilizes the 'UserGetRights' hook, which was first implemented in MediaWiki version 1.11.

Installation & Setup / Changes to LocalSettings.php[edit]

To install this extension:

  • Save the code below in a file called "WhitelistedNamespaces.php" and place this file in the extensions folder of your MediaWiki installation.
  • Insert the following into your LocalSettings.php file:
$wgGroupPermissions['*']['read'] = false;
$wgGroupPermissions['*']['edit'] = false;
$wgGroupPermissions['*']['create'] = false;
$wgWhitelistedNamespaces = '''see below'''
require_once("$IP/extensions/WhitelistedNamespaces.php");

Set $wgWhitelistedNamespaces to an array of namespace indices that you want to whitelist. For example, if you want anonymous visitors to be able to see all pages in the main namespace, but not be able to read any other pages, then insert the following into LocalSettings.php:

$wgWhitelistedNamespaces = array(NS_MAIN);

It is not necessary to set $wgGroupPermissions['*']['read'] = false; in the LocalSettings.php file as this extension will set those permissions during the initialization process; however it is good practice to keep your Group Permissions settings in one place for ease of maintenance. If you choose this option, you can remove the $wgGroupPermissions settings from the code below. Of course, if you are restricting certain pages to read only access, then it probably makes sense that you turn off creating and editing pages for anonymous members as well. This extension does not set those group permission settings -- you must set those yourself.

Code[edit]

<?php
/**
 * WhitelistedNamespaces
 * Author:  Lisa Ridley
 * Date:  25 Feb 2010
 * Version 0.85 beta
 * Copyright (C) 2010 Lisa Ridley
 *
 * 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 can find a copy of the GNU General Public License at http://www.gnu.org/copyleft/gpl.html
 * A paper copy can be obtained by writing to:  Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * To install this extension, save this file in the "extensions" folder of your
 *   MediaWiki installation, and add the following to LocalSettings.php
 *
 *   $wgGroupPermissions['*']['read'] = false;
 *   require_once("$IP/extensions/WhitelistedNamespaces.php");
 *   $wgWhitelistedNamespaces = array(NS_MAIN, NS_TALK);
 *
 *
 */
 
$wgExtensionCredits['other'][] = array(
    'name' => 'WhitelistedNamespaces',
    'author' => 'Lisa Ridley',
    'url' => 'http://www.mediawiki.org/wiki/Extension:WhitelistedNamespaces',
    'version' => '0.85 beta',
    'description' => 'Allows for whitelisting of all pages in a particular namespace or set of namespaces',
);
 
$wgExtensionFunctions[] = 'fnWhitelistedNamespaceSetup';

/**
 * extension setup
 */
function fnWhitelistedNamespaceSetup(){
    global $wgHooks, $wgGroupPermissions;
    $wgGroupPermissions['*']['read'] = false;
    $wgHooks['UserGetRights'][] = 'fnWhitelistedNamespaces';
}
/**
 * Adds currently viewed page to $wgWhitelistRead if page is in whitelisted namespace
 * Always returns true so that other extensions using the UserGetRights hook
 * will be executed
 *
 * @params $user User object
 * @params $rights array of user rights
 * @return boolean true
 */
function fnWhitelistedNamespaces(&$user, $rights){
    global $wgWhitelistedNamespaces, $wgTitle, $wgWhitelistRead;
    /** if user is not anonymous, then exit the script; access is allowed **/
    if(!$user->isAnon()){
        return true;
    }
    $namespace = $wgTitle->getNamespace();
    $nstext = $wgTitle->getNsText();
    $title = $wgTitle->getFullText();
    //Check to see if namespace of current title is in whitelisted namespaces
    if(in_array($namespace, $wgWhitelistedNamespaces)) {
        //build title with prefix
        $titletoadd = $title;
        //check to see if title is in whitelist
        if(is_array($wgWhitelistRead)) {
            if(!in_array($titletoadd, $wgWhitelistRead)) {
                //add if not in whitelist
                $wgWhitelistRead[] = $titletoadd;
            }
        } else {
            $wgWhitelistRead = array($titletoadd);
        }
    }
    return true;
}

Bug in code[edit]

If you install this extension on a recent version of MediaWiki and receive the following error message:

Detected bug in an extension! Hook fnWhitelistedNamespaces failed to return a value; should return true to continue hook processing or false to abort. 

Warning: Parameter 1 to fnWhitelistedNamespaces() expected to be a reference, value given in /share/MD0_DATA/Web/VHost_qbox4u/conf/tech/mwk/includes/Hooks.php on line 207

identical solution as below

This can be fixed by changing the following line:

function fnWhitelistedNamespaces(&$user, $rights){

to this

function fnWhitelistedNamespaces($user, $rights){

Alternative[edit]

This can also be done without an extension by adding something like this to LocalSettings.php. It is not completely identical and it could in theory show pages in the main namespace with a title that begins with Some namespace: (assuming that Some namespace: is not a valid namespace on the wiki). But it should be a perfectly fine solution in almost all cases.

$wgWhitelistReadRegexp = [
	'/^Some namespace:/',
];