一、目 的:为Mediawiki添加用户权限控制
二、实现途径:通过自定义名字空间(custom namespaces)实现
三、说 明:1. 本文仅对mediawiki-1.16.1进行测试过,其他版本是否适合,有待进一步测试
2. 如果要实现该权限控制,必须要求所有链接均要加上其对应所在的名字空间名称
eg. [[Yfzx:研发中心|研发中心首页]]
----------------------------------------------------------------------------
1. 首先禁用匿名用户权限:
修改文件includes/DefaultSettings.php
$wgGroupPermissions['*']['createaccount'] =
false;
$wgGroupPermissions['*']['read'] =
false;
$wgGroupPermissions['*']['edit'] =
false;
$wgGroupPermissions['*']['createpage'] = false;
$wgGroupPermissions['*']['createtalk'] =
false;
$wgGroupPermissions['*']['writeapi'] =
false;
2. 添加自定义名字空间:
修改文件LocalSettings.php,在其最后添加如下代码:
$wgExtraNamespaces = array(700 => "Yfzx", 701 => "Yfzx_Talk", 702 => "Gkzx", 703 => "Gkzx_Talk");
require_once('extensions/NamespacePermissions.php');
说明:
700 => "Yfzx", 701 => "Yfzx_Talk"
700,701表示名字空间ID,mediawiki要求自定义名字空间ID>=100,其中偶数表示名字空间,紧随其后的奇数表示该名字空间的讨论页
Yfzx Yfzx_talk 表示名字空间名称和其对应讨论页
3. 创建步骤2中引用的PHP文件NamespacePermissions.php
在extensions目录下创建文件NamespacePermissions.php,内容如下:
<?php
/* NamespacePermissions - MediaWiki extension
*
* provides separate permissions for each action (read,edit,create,move)
* on articles in custom namespaces for fine access management
*
* Author: Petr Andreev
*
* Sample usage:3
*
* $wgExtraNamespaces = array(100 => "Foo", 101 => "Foo_Talk");
* // optional (example): allow registered users to view and edit articles in Foo
* $wgGroupPermissions[ 'user' ][ 'ns100_read' ] = true;
* $wgGroupPermissions[ 'user' ][ 'ns100_edit' ] = true;
* // end of optional
* require('extensions/NamespacePermissions.php');
*
* Permissions provided:
* # ns{$num}_read
* # ns{$num}_edit
* # ns{$num}_create
* # ns{$num}_move
* where {$num} - namespace number (e.g. ns100_read, ns101_create)
*
* Groups provided:
* # ns{$title}RW - full access to the namespace {$title}
* # ns{$title}RO - read-only access to the namespace {$title}
* e.g. nsFoo_talkRW, nsFooRO
*/
// permissions for autocreated groups should be set now,
// before the User object for current user is instantiated
namespacePermissionsCreateGroups();
// other stuff should better be done via standard mechanism of running extensions
$wgExtensionFunctions[] = "wfNamespacePermissions";
// create groups for each custom namespace
function namespacePermissionsCreateGroups() {
global $wgExtraNamespaces, $wgGroupPermissions;
foreach ( $wgExtraNamespaces as $num => $title ) {
$wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_edit" ] = true;
$wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_read" ] = true;
$wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_create" ] = true;
$wgGroupPermissions[ "ns{$title}RW" ][ "ns{$num}_move" ] = true;
$wgGroupPermissions[ "ns{$title}RO" ][ "ns{$num}_read" ] = true;
}
}
function wfNamespacePermissions() {
global $wgHooks;
// use the userCan hook to check permissions
$wgHooks[ 'userCan' ][] = 'namespacePermissionsCheckNamespace';
}
function namespacePermissionsCheckNamespace( $title, $user, $action, $result ) {
if ( ( $ns = $title->getNamespace() ) >= 100 ) {
if ( ! $user->isAllowed("ns{$ns}_{$action}") ) {
$result = false;
return false;
}
}
return true;
}
/**
* Add extension information to Special:Version
*/
$wgExtensionCredits['other'][] = array(
'name' => 'NamespacePermissions',
'version' => '',
'author' => 'Petr Andreev',
'description' => 'flexible access management for custom namespaces',
'url' => 'http://www.mediawiki.org/wiki/Extension:NamespacePermissions'
);
?>
4. 如何设置权限:
1) 首先以管理员身份登录wiki
2) 首页中点击‘特殊页面’或‘所有特殊页面’
3) 找到并点击链接‘用户权限管理’
4) 输入用户名并点击按钮
5) 根据实际情况,为用户选择合适的名字空间权限,每个名字空间和其讨论页均对应权限RW(可读写)和RO(只读)