Open main menu

Extension:Whitelist Namespaces

MediaWiki extensions manual
Crystal Clear action run.svg
Whitelist Namespaces
Release status: beta
Implementation User rights
Description Whitelists an entire namespace as readable
Author(s) Bawolff Drzraftalk
Latest version 0.3 (2012-12-12)
MediaWiki tested on 1.11.0rc1, and 1.13.2, 1.26
License GNU General Public License 2.0 or later
Download See the section labeled code below
Parameters
$egNamespaceReadWhitelist
Hooks used
userCan
UserGetRights
Translate the Whitelist Namespaces extension if it is available at translatewiki.net
Check usage and version matrix.

Contents

What can this extension do?Edit

If you have a private wiki, this extension can be used to make a namespace public. This is very similar to Manual:$wgWhitelistRead just it works on entire namespaces instead. I noticed there seemed to be a lot of user authorization extensions, but they seemed to be much more complicated than they needed to be. Please note this is my first extension, its fairly simple, but its quite possible I made a mistake somewhere, so ymmv. Please also read Security issues with authorization extensions before using this extension.

Note: If you have any comments, questions, suggestions etc, please don't hesitate to contact me. (on my talk or by email).

UsageEdit

Download instructionsEdit

Please cut and paste the code found below and place it in $IP/extensions/WhitelistNamespaces.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/WhitelistNamespaces.php");
//Add Namespaces you want to be readable. use namespace number (ex 1 for talk
//pages, 0 for main namespace, etc) or constants (NS_TALK, NS_MAIN, etc)
//example:
//$egNamespaceReadWhitelist = Array(NS_TALK, NS_MAIN, NS_HELP);
//for main, talk and help namespaces.
//Note: It is important this comes after the require_once line
$egNamespaceReadWhitelist = Array();

You must also make the wiki private, so there is something to whitelist (ex: add $wgGroupPermissions['*']['read']=false; to LocalSettings.php)

Configuration parametersEdit

$egNamespaceReadWhitelist - array of namespace numbers to whitelist. Examples:

  • $egNamespaceReadWhitelist = Array(NS_TALK, NS_MAIN, NS_HELP); #this would whitelist the main, talk and help namespaces
  • $egNamespaceReadWhitelist = Array(100, 101); #this would whitelist the first custom namespace, and its associated talk namespace.
  • If you have defined a constant NS_PUBLIC$egNamespaceReadWhitelist = Array(NS_PUBLIC); would whitelist that namespace.

CodeEdit

<?php
if ( ! defined( 'MEDIAWIKI' ) )
  die();
/*

 Purpose:       Have certain namespaces whitelisted so 
                that they can be read, regardless of
                if the user has permission to read.

                For example, a public namespace, on
                a private wiki.

 @author 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/WhitelistNamespaces.php");
   $egNamespaceReadWhitelist = Array();
 
 Note, the order of those lines are important
 
 changing the $egNamespaceReadWhitelist = Array(); to be an array of namespaces
 you want to whitelist. ex: 
   $egNamespaceReadWhitelist = Array(NS_TALK, NS_MAIN, NS_HELP);
 for main help and talk namespaces.
 
 
Note: the UserGetRights hook is useful if the wiki is world readable.
In such a case the $egNamespaceActionWhitelist array defines the
permissions offered to anonymous users in this namespace. Eg:
$egNamespaceActionWhitelist = array('read', 'edit', 'createpage', 'createtalk');
Default right is read which must be safe.
However non-world-readable wikis have not be tested extensively.

Remember that 'createpage' right is given automagically with 'edit'

*/

// default. overide it in LocalSettings.php
$egNamespaceReadWhitelist = Array();
// default. overide it in LocalSettings.php
$egNamespaceActionWhitelist = Array('read');

$wgHooks['userCan'][] = 'efExtensionWhitelistNamespaces';
$wgHooks['UserGetRights'][] = 'efWhitelistedNamespaces';

$wgExtensionCredits['other'][] = array(
  'name' => 'Whitelist Namespaces',
  'description' => 'Allows an entire namespace to be read-whitelisted',
  'url' => 'http://www.mediawiki.org/wiki/Extension:Whitelist_Namespaces',
  'author' => array('Bawolff', 'Drzraf'),
  'version' => '0.3'
);
 
 
function efExtensionWhitelistNamespaces(&$title, &$user, $action, &$result) {
  global $egNamespaceReadWhitelist, $egNamespaceActionWhitelist;
  if (in_array($action, $egNamespaceActionWhitelist, true)) {
    if( in_array($title->getNamespace(), $egNamespaceReadWhitelist, true)) {
      $result = true;
      return false;
    }
  }
  return true;
}

// There was (abandonned) plans to introduce $ns as a
// 3rd parameter during 1.19 cycle, see 3da36a9103
function efWhitelistedNamespaces( $user, &$aRights ) {
  global $egNamespaceReadWhitelist, $egNamespaceActionWhitelist;
  $ns = RequestContext::getMain()->getTitle()->getNamespace();
  if($user->isAnon() && $ns && in_array( $ns, $egNamespaceReadWhitelist, true ) ) {
    $aRights = array_merge($aRights, $egNamespaceActionWhitelist);
  }
  return true;
}

See alsoEdit