Extension:NamespaceReadRestrict

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
NamespaceReadRestrict
Release status: beta
Implementation Hook
Description Restricts reading to particular namespaces
Author(s) Leucostictetalk
Latest version 1.0.0 (2014-11-24)
MediaWiki 1.19+
PHP 5
Database changes No
License GNU General Public License 2.0
Download
Hooks used
TitleReadWhitelist
Translate the NamespaceReadRestrict extension if it is available at translatewiki.net
Check usage and version matrix.

The NamespaceReadRestrict extension can allow reading of pages in particular namespaces, but not others.

Usage[edit]

Installation[edit]

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

Configuration[edit]

In accordance with the instructions at Manual:Preventing access, abolish the ability of non-logged-in users to read, edit or create accounts on your wiki and initialize the $wgWhitelistRead array (it must be initialized, even if it's empty, in order for this extension to work) by adding to your LocalSettings.php file the following:

$wgGroupPermissions['*']['read'] = false;
$wgGroupPermissions['*']['edit'] = false;
$wgGroupPermissions['*']['createaccount'] = false;
$wgWhitelistRead = array(
	'MediaWiki:Common.css',
	'-',
	'Main Page'
);

If most of your content will be private[edit]

Also add to LocalSettings.php, after the require_once line, this configuration setting specifying what namespaces are okay for non-logged-in users to read. E.g. in this example, non-logged-in users will only be allowed to read pages in namespace 0, the main namespace. See Manual:Namespace#Built-in_namespaces and Manual:Using custom namespaces.

define("NS_PUBLIC", 500);
define("NS_PUBLIC_TALK", 501);
$wgExtraNamespaces[NS_PUBLIC] = "Public";
$wgExtraNamespaces[NS_PUBLIC_TALK] = "Public_talk";   # underscore required
$wgAllowedReadNamespaces = array (
        NS_PUBLIC,
        NS_PUBLIC_TALK
);

If most of your content will be public[edit]

If most of your wiki content will be open to the public, then you may want to establish a Private: namespace for the rest. Conversely, if you want most of your wiki to be private, then you might want to establish a Public: namespace for the rest. Don't forget about $wgNonincludableNamespaces, if you've left open the option of non-logged-in users to edit. E.g.:

define("NS_PRIVATE", 500);
define("NS_PRIVATE_TALK", 501);
$wgExtraNamespaces[NS_PRIVATE] = "Private";
$wgExtraNamespaces[NS_PRIVATE_TALK] = "Private_talk";   # underscore required
$wgPrivateNamespaces = array (
        NS_PRIVATE,
        NS_PRIVATE_TALK
);
$wgAllowedReadNamespaces = array_diff ( array_merge ( range ( -2, 15 ),
        $wgExtraNamespaces ), $wgPrivateNamespaces);
$wgNonincludableNamespaces = array_merge ( $wgNonincludableNamespaces, $wgPrivateNamespaces );

Parser function[edit]

There is a parser function, {{#isloggedin}}. If you also have Extension:ParserFunctions installed, then you can use this to, for example, give logged-in users and non-logged-in users different main pages. To do that, you would create pages "Template:Private main page" and "Template:Public main page" and then replace the content of "Main Page" with:

{{#if: {{#isloggedin:}}|{{Private main page}}|{{Public main page}}}}

Alternatives[edit]

An alternative solution that utilises MW in-built since v1.21.0 functionality might be to affect a general reading restriction on the wiki by

$wgGroupPermissions['*']['read'] = false;

then excluding specific namespaces from that restriction by the configuration setting $wgWhitelistReadRegexp.