From mediawiki.org
Jump to navigation Jump to search
This page is a translated version of the page Manual:Using custom namespaces and the translation is 60% complete.
Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Tiếng Việt • ‎español • ‎français • ‎galego • ‎polski • ‎português • ‎português do Brasil • ‎русский • ‎中文 • ‎日本語 • ‎한국어


自定义名字空间可以使用 $wgExtraNamespaces 配置指令简单的管理。 也可以使用 $wgNamespaceAliases 配置指令自定义别名(或者预定义)名字空间。 Some extensions make it easy for you to create custom namespaces. Examples include NamespaceManager and BlueSpiceNamespaceManager .

It's recommended to ensure there are no pending jobs in the job queue before manipulating namespaces, to avoid such jobs from failing if they target pages from namespaces you are about to delete or rename. Use runJobs.php to run all pending jobs and clear the queue before manipulating namespace configuration.


通过加入$wgExtraNamespaces 全域变量至您的LocalSettings.php 文件来创建新的名字空间。 所有的名字空间都需要一个数字作为索引。 作为简单的自定义名字空间创建例子,将以下行代码添加到LocalSettings.php 会定义一个“Foo”名字空间(3000)及其“Foo_talk”名字空间:

// 为我添加的名字空间定义常量。
define("NS_FOO", 3000); // 这必须是偶数。
define("NS_FOO_TALK", 3001); // 这必须是下一个奇整数。

// 添加名字空间。
$wgExtraNamespaces[NS_FOO] = "Foo";
$wgExtraNamespaces[NS_FOO_TALK] = "Foo_talk"; // 请注意命名空间名称中的下划线
作为一个惯例,数字100-199的名字空间是保留为站点特定名字空间,尽管有的扩展不遵从这个惯例。 扩展编写者使用更高的数字,高达32767。 当选择索引时,你应该避免任何已在extension default namespaces里的任何数字,这是因为你有可能以后会安装那些插件。 从3000到4999的数字是保留为系统管理员自定义名字空间的。 (并且,你也要避免任何名字空间的名字扩展默认名字空间内的冲突。)
Create the talk namespace too
You typically create a discussion "Talk" namespace along with each custom namespace. With this example, if you move a page into the "Foo" namespace, will be prompted to move its associated talk page, if any, and if you choose to do so, MediaWiki will place the talk page in "Foo talk".
注册名字空间时,使用下划线而不是空格。 “我的 名字空间”在此无效;请改用“我的_名字空间”。

The uppercase part does not permit hyphens but they can still be safely added to the prefix title. 例子:

$wgExtraNamespaces[NS_FOOFOO] = "Foo-Foo";
Name the numbers you pick
The example defines constants for the namespace IDs, so that you can refer to these namespaces later on in the configuration, for example in $wgNamespaceProtection , $wgNamespacesWithSubpages , or $wgExtraGenderNamespaces .

You could go on to configure additional settings for your new namespace.

$wgNamespaceProtection[NS_FOO] = array( 'editfoo' ); // permission "editfoo" required to edit the foo namespace
$wgNamespacesWithSubpages[NS_FOO] = true;            // subpages enabled for the foo namespace
$wgGroupPermissions['sysop']['editfoo'] = true;      // permission "editfoo" granted to users in the "sysop" group
Manipulation of $wgExtraNamespaces must be completed during MediaWiki initialization, i.e. in case an extension etc. should work with the newly created custom namespace, make sure that you define and name them prior to invoking the respective extension. For instance it cannot be manipulated in a post-initialization hook like $wgExtensionFunctions .
Watch out for collisions with URL protocols
MediaWiki's linking code knows about a number of URL protocols, defined in the $wgUrlProtocols variable. If your namespace name is identical to one of these protocols, you're going to have trouble creating [[wikilinks]] to pages in your custom namespace. This most commonly arises when someone tries to create a "News" namespace, because news: is a URL protocol for NNTP newsgroups.
To avoid this issue, you can deregister the relevant URL protocol by adding the following code to LocalSettings.php (replacing news by the lowercased name of the protocol you wish to remove):
$wgUrlProtocols = array_diff( $wgUrlProtocols, array( 'news:' ) );


Extensions often add their own namespaces, such as the Flow extension's "Topic" namespace. An extension can unconditionally add to $wgExtraNamespaces as described above, or if its namespace registration is conditional (for example EventLogging only defines its "Schema" namespace on the wiki where it stores schemas), then it can add a handler function for the CanonicalNamespaces hook that decides what to do.

The timing of registering extensions is subtle. Functions that extensions register with $wgExtensionFunctions are executed too late to register additional namespaces (任务T47031). So extensions should bind the CanonicalNamespaces hook at file scope (in MyExtension.php) and check there whether the wiki should activate the extra namespace or not. Extensions can configure namespace permissions and content handlers unconditionally at file scope since they do not require the namespace to actually be created.


Gerrit change 166705

The new extension.json registration system has a namespaces key for an extension to list its namespaces that should always exist. 来自Gadgets 扩展:

"namespaces": [
			"id": 2300,
			"constant": "NS_GADGET",
			"name": "Gadget",
			"protection": "gadgets-edit"
			"id": 2301,
			"constant": "NS_GADGET_TALK",
			"name": "Gadget_talk"



在构建网站统计页面(参见Special:Statistics),MediaWiki使用值存储在数据库来计算总数。 One particular total is the "number of articles" or "number of content pages" figure.


When creating custom namespaces to hold additional content, it is a good idea to indicate this in the configuration. 通过$wgContentNamespaces 设定一个自定名字空间为内容名字空间。


$wgContentNamespaces[] = 3000;
$wgContentNamespaces[] = NS_FOO;

MediaWiki will now consider pages in the "Foo" namespace to be articles, if they meet the remaining criteria, and will include them when updating the site statistics counters.


  • When adjusting the value of $wgContentNamespaces, it is a good idea to run either the maintenance/updateArticleCount.php or maintenance/initSiteStats.php script to update the internal statistics cache (see 手册:维护脚本 ).



  • A custom namespace can be used to hold content that should not be shown on the search results page, for example pages that are used only for transclusion.
  • 特定名字空间需要额外编辑特权。
  • You want certain namespaces not to be subjected to certain limitations or default settings ($wgNoFollowNsExceptions for example)
  • A uniform prefix for specific content(s), which is searchable for that namespace only


在数据库中,页面是通过名字空间编号与页面名称进行索引的。 如果在不存在的名字空间中建立了一个页面,例如“Bar:Some page”,它将被视为主名字空间中名称为“Bar:Some page”的页面。

如果之后建立了“Bar”名字空间则会出现问题:查找“Bar:Some page”页面时,会因为在“Bar”名字空间中不存在“Some page”页面而失败。




  1. 将出现错误的名字空间从配制文件中注释掉
  2. 依次将每个错误页面从先前的名字空间中移出,如“Bar:Some page”移动为“Bar2:Some page”。
  3. 将配制文件恢复原样
  4. 最后将移动的页面移回正确的名字空间


“maintenance”目录中有一个叫“NamespaceDupes.php ”的脚本,用它可以对大量页面更有效地执行上述操作, 其使用方法很简单,请用“--help”选项调用脚本来显示其使用说明。



page_title = REPLACE(page_title, 'Bar:', ''),
page_namespace = 3000
WHERE page_title LIKE 'Bar:%' AND page_namespace=0


page_title = REPLACE(page_title, 'Bar_talk:', ''),
page_namespace = 3001
WHERE page_title LIKE 'Bar_talk:%' AND page_namespace=1

After such fiddling, run the refreshLinks.php script and the updateSearchIndex.php script to update internal links and search results in your wiki. Note that external search engines like Google will take some time to update their index.


The problem addressed above also occurs when a custom namespace definition is removed; MediaWiki is no longer aware of the numerical index for the namespace, and attempts to search the main namespace for the desired pages, leading to inaccessible content. This is a rare occurrence, since most sites will not need namespaces removed, but it is a problem. (参见邮件列表讨论)。

Example on how to remove Flow and the Topic namespace:

  • 卸载Flow
  • 临时添加$wgExtraNamespaces[2600] = 'Topic';至配置
  • 使用deleteBatch.php删除所有Topic名字空间中的页面
  • 移除$wgExtraNamespaces配置


Suppose that you need to rename custom namespace "Foo" to "New" without performing a mass move of pages. The easiest way to achieve this is to preserve the namespace ID (here "3000") as well as the namespace constant (here "NS_FOO"), modify the (visible) namespace title and add the old one as an alias.

define("NS_FOO", 3000);
$wgExtraNamespaces[NS_FOO] = "Foo";
define("NS_FOO", 3000);
$wgExtraNamespaces[NS_FOO] = "New";
$wgNamespaceAliases['Foo'] = NS_FOO;


In order for you to avoid namespace conflicts e.g. your namespace has the same number as a namespace defined by an extension, the extension namespace list shows you which numbers to avoid to prevent conflicts.

Defining $wgNamespacesToBeSearchedDefault , $wgNamespacesWithSubpages , $wgContentNamespaces or $wgNamespaceAliases for an ID not associated to any existing namespace in $wgExtraNamespaces doesn't break the wiki; MediaWiki gracefully ignores such configurations.


For example, to set the background color of pages in a particular namespace (and its associated talk namespace) you can add the following code to your common.css:

.ns-3000 #content, .ns-3001 #content { background-color: #f3f3ff; }
.ns-3000 div.thumb, .ns-3001 div.thumb { border-color: #f3f3ff; }

where 3000 is the namespace's index and #f3f3ff is the color you want as its background color.


Currently in use by the OmegaWiki project.