手册:使用自定义名字空间

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 53% complete.

Other languages:
Deutsch • ‎English • ‎Tiếng Việt • ‎español • ‎français • ‎galego • ‎polski • ‎português do Brasil • ‎русский • ‎中文 • ‎日本語 • ‎한국어

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

自定义名字空间可以使用$wgExtraNamespace 配置指令简单的管理。 也可以使用$wgNamespaceAliases 配置指令自定义别名(或者预定义)名字空间。

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"; // Note underscores in the namespace name.
挑选未使用的序号
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.
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. “我的 名字空间”在此无效;请改用“我的_名字空间”。
没有连字符

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 (bug 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.

MediaWiki版本: 1.25
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"
		},
]

它也支持CanonicalNamespaces钩子。

内容名字空间

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

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

$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
  • If you're a 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”页面而失败。

有三种方法消除这样的错误:

移动冲突页面

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

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

使用维护脚本

“maintenance”目录中有一个叫“NamespaceDupes.php ”的脚本可以处理这个问题, 其使用方法很简单,请用“--help”选项调用脚本来显示其使用说明。

使用数据库查询

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

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

处理对应的讨论页面:

UPDATE page SET
page_title = REPLACE(page_title, 'Bar:', ''),
page_namespace = 3001
WHERE page_title LIKE 'Bar:%' AND page_namespace=1

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 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:

  • 卸载low
  • 临时添加$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.

参见