Manual:Using custom namespaces/zh

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

自定义命名空间可以使用  配置指令简单管理. 也可以使用  配置指令为自定义（以及预定义的）命名空间定义别名. Some extensions make it easy for you to create custom namespaces. Examples include and.



创建自定义命名空间
在文件中加入全局变量以创建新的命名空间. 这个数组中，所有命名空间都需要数字索引. 一个简单的创建自定义命名空间的例子，将以下行代码添加到文件会定义一个“Foo”命名空间（3000）及其“Foo_talk”命名空间：


 * 挑选未使用的数字
 * 作为惯例，数字100-199的命名空间是为保留的特定命名空间，尽管有的扩展不遵从这个惯例. 扩展编写者使用更高的数字，高达32767.  当选择索引时，你应该避免任何已在extension default namespaces里的任何数字，这是因为你有可能以后会安装那些插件.  从3000到4999的数字是为系统管理员保留的，以定义他们的自定义命名空间.  （并且，你也要避免任何命名空间的名称与扩展默认命名空间内的冲突. ）


 * 偶数，然后奇数
 * 注意在上面的例子中，命名空间的索引是3000.
 * 偶数命名空间索引代表一个主题命名空间.
 * 奇数索引表示其后附属的讨论命名空间.


 * 同时创建讨论命名空间
 * 您通常会为每个自定义命名空间创建一个讨论“Talk”命名空间. 这个例子中，如果你将页面移动到“Foo”命名空间，将被提示移动其关联的讨论页（如果有），如果你选择这样做，MediaWiki将会把讨论页放在“Foo talk”中.


 * 无空格
 * 注册命名空间时，使用下划线而不是空格. “My Namespace”是无效的，请改用“My_Namespace”.

大写部分不允许使用连字符，但它们仍然可以安全地添加到前缀标题中. 例子：
 * 没有连字符


 * 为您选择的数字命名
 * 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.


 * 尽早进行
 * Manipulation of  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.


 * 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 the "LocalSettings.php" file (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  ) 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  registration system has a   key for an extension to list its namespaces that should always exist. 来自扩展：

它也支持 钩子. Extensions should prefer extension registration over CanonicalNamespaces. Note that adding an extension to LocalSettings.php does not necessarily make relevant namespace constants available as globals for other extensions.



内容命名空间
在构建网站统计页面（参见Special:Statistics），MediaWiki使用值存储在数据库来计算总数. One particular total is the "number of articles" or "number of content pages" figure.

一个页面要被认作一篇文章或真内容，必须符合如下条件：


 * 在主名字空间或一个内容名字空间
 * 非重定向页面
 * 至少一个内部链接

创建自定义命名空间以保存其他内容时，最好在配置中指明这一点. 通过设定一个自定名字空间为内容名字空间.

例如在"LocalSettings.php"文件中加入如下内容：


 * 或

MediaWiki现在在会将“Foo”命名空间中的页面视为文章（如果这些页面也满足其余标准），并将在更新站点统计计数器时包括这些页面.



运行维护脚本

 * When adjusting the value of configuration parameter, it is a good idea to run either the "path/to/maintenance/updateArticleCount.php or "path/to/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 ( for example)
 * A uniform prefix for specific content(s), which is searchable for that namespace only
 * If you're a MW developer, sometimes you need to have a custom namespace for your extension(s)



处理已存在的页面
在存储页面记录时，MediaWiki 使用命名空间的数字索引以及剩余的标题文本. 如果在不存在的命名空间中建立了一个页面，例如“Bar:Some page”，它将被视为主命名空间中名称为“Bar:Some page”的页面.

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

有三种方法修正这样的错误：



移动冲突页面
当受影响的页面数量很少时（例如在网站配置中将“Bar”定义为名字空间前，其涉及5个已创建的页面），那么以下途径会比较合适：


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



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



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

处理对应的讨论页面：

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. （参见邮件列表讨论）.

Example on how to remove 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 " ") as well as the namespace constant (here " "), modify the (visible) namespace title and add the old one as an alias.

将 更改为



避免命名空间冲突
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, , $wgContentNamespaces or 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.



参见
Currently in use by the OmegaWiki project.
 * Namespace manager as originally proposed for MW1.6-wikidata and its successors.
 * - 在命名空间中使用不同皮肤
 * - a modified version of the which changes it to provide a namespace manager as a special page.
 * - 控制对命名空间的访问
 * 扩展命名空间注册