Manual:Using custom namespaces/zh

若要生成命名空间，在MediaWiki安装中增加命名空间是可以的，进一步分离内容和允许更多的逻辑组织.

对管理使用 的配置指令时，自定义命名空间十分普遍. 定义命名空间的别名（或者预设的）来自定义命名空间是可以的，使用 指令即可.

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


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


 * Even then odd
 * 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".


 * No spaces
 * 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):

In extensions
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. It also supports the  hook.

内容名字空间
When building the site statistics page (see Special:Statistics), MediaWiki uses values stored in the database to calculate certain totals. 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”设定一个自定名字空间为内容名字空间.

例如在 “LocalSettings.php“ 中加入如下内容：


 * or

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.

Running maintenance scripts

 * 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”的脚本可以处理这个问题， 其使用方法很简单，请用“--help”选项调用脚本来显示其使用说明.

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

处理对应的讨论页面：

注意，Wiki的搜索引擎需要在一段时间之后，才能对你做的修改做出反应.

移除自定义名字空间
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. (See mailing list discussion).

Avoid namespace conflicts
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