Manual:Using custom namespaces/zh

除了固定的名字空间，可以为MediaWiki安装添加自定义名字空间，进一步分离内容和允许更多的逻辑组织.

自定义名字空间可以使用 配置指令简单的管理. 也可以使用 配置指令自定义别名（或者预定义）名字空间.

建立自定名字空间
通过加入全域变量至您的文件来创建新的名字空间. 所有的名字空间都需要一个数字代号. 例如， 创建一个名字空间，把 定义到“Foo”，代号为3000，以及其“Foo talk”名字空间：


 * Pick an unused number
 * As a convention, the namespaces numbered 100-199 are reserved for site-specific namespaces, although there are some extensions that don't follow this convention. Extension writers use higher numbers, up to 32767. When choosing your index, you should avoid any number already in extension default namespaces, since you might want to install that extension later. Thus numbers from 3000 and higher are a good pick for system administrators to define their custom namespaces.


 * 偶数，然后奇数
 * Note the namespace array index is 3000 in the above example.
 * an even namespace index denotes a subject namespace.
 * the odd index immediately following that number denotes its associated discussion ("talk") namespace


 * 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".


 * 无空格
 * Use underscores instead of spaces when registering namespace names. "My Namespace" is invalid here; use "My_Namespace" instead.

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


 * 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, , or.

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


 * Do it early
 * Manipulation of  must be completed during MediaWiki initialization; for instance it cannot be manipulated in a post-initialization hook like.


 * Watch out for collisions with URL protocols
 * MediaWiki's linking code knows about a number of URL protocols, defined in the 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   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  by the lowercased name of the protocol you wish to remove):

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

The timing of registering extensions is subtle. Functions that extensions register with are executed too late to register additional namespaces. So extensions should bind the 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.

The new extension.json registration system has a  key for an extension to list its namespaces that should always exist. From the extension:

It also supports the  hook.

内容名字空间
在构建网站统计页面（参见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. 通过设定一个自定名字空间为内容名字空间.

例如在 中加入如下内容：


 * 或

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, it is a good idea to run either the   or   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.
 * Certain namespace require additional privilege(s), i.e. for reading or editing
 * You want certain namespace not to be subjected to certain limitation or default settings ( for example)
 * A uniform prefix for specific content(s), which is searchable for that namespace only
 * If you're an MW developer, sometimes you need to have a custom namespace for your extension(s)

处理已存在的页面
在数据库中，页面是通过名字空间编号与页面名称进行索引的. 如果在不存在的名字空间中建立了一个页面，例如“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”的脚本可以处理这个问题， 其使用方法很简单，请用“ ”选项调用脚本来显示其使用说明.

使用数据库查询
通过下面的数据库查询操作，可以将所有在主名字空间的“Bar:某页面”页面移动到编号为“3000”的名字空间：

处理对应的讨论页面：

The above example is wrong. It should look for "Bar_Talk:" or "Bar_talk:" or how MediaWiki saves that inside the page_title field... After such fiddling, run the script and the  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. （参见邮件列表讨论）.

避免名字空间冲突
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:

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

参见

 * Namespace manager as originally proposed for MW1.6-wikidata and its successors. Currently in use by the OmegaWiki project.
 * - to use a different skin in a namespace
 * - a modified version of the which changes it to provide a namespace manager as a special page.
 * - to control access to namespaces
 * Extension namespace registration
 * - to control access to namespaces
 * Extension namespace registration