Jump to content

Manual:Using custom namespaces: Difference between revisions

From mediawiki.org
Content deleted Content added
== See also ==: reorganised
Tag: 2017 source edit
m Reverted edits by 110.54.154.29 (talk) to last version by Wladek92
Tag: Rollback
 
(30 intermediate revisions by 10 users not shown)
Line 28: Line 28:


; <translate><!--T:13--> Pick an unused number</translate>
; <translate><!--T:13--> Pick an unused number</translate>
: <translate><!--T:14--> As a convention, the namespaces numbered 100-199 are reserved for site-specific namespaces, although there are [[<tvar name=1>Extension default namespaces#ID_100–199</tvar>|some extensions]] that don't follow this convention.</translate> <translate><!--T:15--> Extension writers use higher numbers, up to 32767.</translate> <translate><!--T:16--> When choosing your index, you should avoid any number already in [[extension default namespaces]], since you might want to install that extension later.</translate> <translate><!--T:17--> [[<tvar name=1>Extension default namespaces#ID 3000+</tvar>|Numbers from 3000 to 4999]] are reserved for system administrators to define their custom namespaces.</translate> <translate><!--T:128--> (Also, you’ll want to avoid any namespace '''name''' that’s already in [[<tvar name=1>Extension default namespaces</tvar>|Extension default namespaces]].)</translate>
: <translate><!--T:14--> As a convention, the namespaces numbered 100-199 are reserved for site-specific namespaces, although there are [[<tvar name=1>Special:MyLanguage/Extension default namespaces#ID_100–199</tvar>|some extensions]] that don't follow this convention.</translate> <translate><!--T:15--> Extension writers use higher numbers, up to 32767.</translate> <translate><!--T:16--> When choosing your index, you should avoid any number already in <tvar name=1>{{ll|extension default namespaces}}</tvar>, since you might want to install that extension later.</translate> <translate><!--T:17--> [[<tvar name=1>Special:MyLanguage/Extension default namespaces#ID 3000+</tvar>|Numbers from 3000 to 4999]] are reserved for system administrators to define their custom namespaces.</translate> <translate><!--T:128--> (Also, you’ll want to avoid any namespace '''name''' that’s already in [[<tvar name=1>Special:MyLanguage/Extension default namespaces</tvar>|Extension default namespaces]].)</translate>


; <translate><!--T:18--> Even then odd </translate>
; <translate><!--T:18--> Even then odd </translate>
Line 36: Line 36:


; <translate><!--T:22--> Create the talk namespace too</translate>
; <translate><!--T:22--> Create the talk namespace too</translate>
:<translate><!--T:23--> You typically create a discussion "Talk" namespace along with each custom namespace.</translate> <translate><!--T:24--> With this example, if you [[<tvar name=man>Special:MyLanguage/Help:Moving a page</tvar>|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".</translate>
:<translate><!--T:23--> You typically create a discussion "Talk" namespace along with each custom namespace.</translate> <translate><!--T:24--> With this example, if you [[<tvar name=man>Special:MyLanguage/Help:Moving a page</tvar>|move a page]] into the "Foo" namespace, you 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".</translate>


; <translate><!--T:25--> No spaces</translate>
; <translate><!--T:25--> No spaces</translate>
Line 61: Line 61:


; <translate><!--T:37--> Do it early</translate>
; <translate><!--T:37--> Do it early</translate>
: <translate><!--T:38--> Manipulation of <code>$wgExtraNamespaces</code> 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 <tvar name=ExtensionFunctions>{{wg|ExtensionFunctions}}</tvar>.</translate>
: <translate><!--T:38--> Manipulation of <tvar name=1><code>$wgExtraNamespaces</code></tvar> 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 <tvar name=ExtensionFunctions>{{wg|ExtensionFunctions}}</tvar>.</translate>


;<translate><!--T:39--> Watch out for collisions with URL protocols</translate>
;<translate><!--T:39--> Watch out for collisions with URL protocols</translate>
: <translate><!--T:40--> MediaWiki's linking code knows about a number of URL protocols, defined in the <tvar name=UrlProtocols>{{$wg|UrlProtocols}}</tvar> variable.</translate> <translate><!--T:41--> If your namespace name is identical to one of these protocols, you're going to have trouble creating <nowiki>[[wikilinks]]</nowiki> to pages in your custom namespace.</translate> <translate><!--T:42--> This most commonly arises when someone tries to create a "News" namespace, because <code>news:</code> is a URL protocol for NNTP newsgroups.</translate>
: <translate><!--T:40--> MediaWiki's linking code knows about a number of URL protocols, defined in the <tvar name=UrlProtocols>{{$wg|UrlProtocols}}</tvar> variable.</translate> <translate><!--T:41--> If your namespace name is identical to one of these protocols, you're going to have trouble creating <nowiki>[[wikilinks]]</nowiki> to pages in your custom namespace.</translate> <translate><!--T:42--> This most commonly arises when someone tries to create a "News" namespace, because <tvar name=1><code>news:</code></tvar> is a URL protocol for NNTP newsgroups.</translate>
: <translate><!--T:43--> To avoid this issue, you can deregister the relevant URL protocol by adding the following code to the "<tvar name=1>LocalSettings.php</tvar>" file (replacing <code>news</code> by the lowercased name of the protocol you wish to remove):</translate>
: <translate><!--T:43--> To avoid this issue, you can deregister the relevant URL protocol by adding the following code to the "<tvar name=1>LocalSettings.php</tvar>" file (replacing <tvar name=2><code>news</code></tvar> by the lowercased name of the protocol you wish to remove):</translate>


<syntaxhighlight lang="php">
<syntaxhighlight lang="php">
Line 74: Line 74:
=== In extensions === <!--T:44-->
=== In extensions === <!--T:44-->
</translate>
</translate>
<translate><!--T:45--> Extensions often add their own namespaces, such as the <tvar name=Flow>{{ll|Extension:Flow|Flow}}</tvar> extension's "Topic" namespace.</translate>
<translate><!--T:46--> An extension can unconditionally add to <tvar name=ExtraNamespaces><code>$wgExtraNamespaces</code></tvar> as described above, or if its namespace registration is conditional (for example <tvar name=EventLogging>{{ll|Extension:EventLogging|EventLogging}}</tvar> only defines its "Schema" namespace on the wiki where it stores schemas), then it can add a handler function for the <tvar name=CanonicalNamespaces>{{ll|Manual:Hooks/CanonicalNamespaces|CanonicalNamespaces}}</tvar> hook that decides what to do.</translate>

<translate><!--T:47--> The timing of registering extensions is subtle.</translate>
<translate><!--T:48--> Functions that extensions register with <tvar name=ExtensionFunctions>{{wg|ExtensionFunctions}}</tvar> are executed too late to register additional namespaces (<tvar name=bug>{{task|T47031}}</tvar>).</translate>
<translate><!--T:49--> So extensions should bind the <code>CanonicalNamespaces</code> hook at file scope (in <code>''MyExtension''.php</code>) and check there whether the wiki should activate the extra namespace or not.</translate>
<translate><!--T:50--> Extensions can configure namespace permissions and content handlers unconditionally at file scope since they do not require the namespace to actually be created.</translate>

{{MW version|version=1.25|comment=and after|gerrit change=166705}}
{{MW version|version=1.25|comment=and after|gerrit change=166705}}
<translate><!--T:51--> The <code>[[<tvar name=1>Special:MyLanguage/extension.json</tvar>|extension.json]]</code> registration system has a <code>namespaces</code> key for an extension to list its namespaces that should always exist.</translate>
<translate><!--T:45--> Extensions often add their own namespaces, such as the <tvar name=Flow>{{ll|Extension:Flow|Flow}}</tvar> extension's "Topic" namespace.</translate>
<translate><!--T:51--> The <tvar name=3>{{ll|extension.json}}</tvar> registration system has a <tvar name=2><code>namespaces</code></tvar> key for an extension to list its namespaces.</translate>
<translate><!--T:111--> From the <tvar name=Gadgets>{{ll|Extension:Gadgets|Gadgets}}</tvar> extension:</translate>
<translate><!--T:111--> From the <tvar name=Gadgets>{{ll|Extension:Gadgets|Gadgets}}</tvar> extension:</translate>


Line 102: Line 95:
</syntaxhighlight>
</syntaxhighlight>


<translate><!--T:143--> You can also set other namespace-related settings here, such as whether it should be a content namespace or not; see <tvar name=1>{{ll|Manual:Extension.json/Schema#namespaces}}</tvar> for the available properties.</translate>
<translate><!--T:52--> It also supports the <code>CanonicalNamespaces</code> hook.</translate>

<translate><!--T:131--> Extensions should prefer extension registration over CanonicalNamespaces.</translate>
<translate><!--T:144--> If namespace registration is conditional (for example <tvar name=1>{{ll|Extension:EventLogging|EventLogging}}</tvar> only defines its "Schema" namespace on the wiki where it stores schemas), the extension should add <tvar name=2><code>"conditional": true</code></tvar> to the namespace definition in <tvar name=3><code>extension.json</code></tvar>, and also register a handler for the <tvar name=4>{{ll|Manual:Hooks/CanonicalNamespaces|CanonicalNamespaces}}</tvar> hook there which decides whether to register the namespace or not.</translate>
<translate><!--T:132--> Note that adding an extension to LocalSettings.php does not necessarily make relevant namespace constants available as globals for other extensions.</translate>
<translate><!--T:145--> The hook handler should only change the <tvar name=1><code>$namespaces</code></tvar> with which it is called; all other settings of the namespace should still be registered in the <tvar name=2><code>extension.json</code></tvar>.</translate>
<translate><!--T:146--> If those settings should also be dynamic, do not change <tvar name=1>{{wg|ContentNamespaces}}</tvar>, <tvar name=2>{{wg|NamespaceContentModels}}</tvar> etc. in the <tvar name=3>CanonicalNamespaces</tvar> hook handler (it will have no effect – <tvar name=4>[[phab:T288819|T288819]]</tvar>); instead, you will have to set them earlier, such as in a {{<tvar name=5>ll|Manual:Extension.json/Schema#callback</tvar>|callback}} ('''not''' in <tvar name=6>{{wg|ExtensionFunctions}}</tvar>).</translate>

<translate><!--T:132--> Note that adding an extension to <tvar name=1>LocalSettings.php</tvar> does not necessarily make relevant namespace constants available as globals for other extensions.</translate>


{{Anchor|content-ns}}
{{Anchor|content-ns}}
<translate>
<translate>

== Content namespaces == <!--T:53-->
== Content namespaces == <!--T:53-->
</translate>
</translate>
<translate><!--T:54--> When building the site statistics page (see [[Special:Statistics]]), MediaWiki uses values stored in the database to calculate certain totals.</translate>
<translate><!--T:54--> When building the site statistics page (see <tvar name=1>[[Special:Statistics]]</tvar>), MediaWiki uses values stored in the database to calculate certain totals.</translate>
<translate><!--T:55--> One particular total is the "number of articles" or "number of content pages" figure.</translate>
<translate><!--T:55--> One particular total is the "number of articles" or "number of content pages" figure.</translate>


Line 169: Line 167:
<translate>
<translate>
<!--T:73-->
<!--T:73-->
* If you're a MW developer, sometimes you need to have a custom [[Extension default namespaces|namespace for your extension(s)]]
* If you're a MW developer, sometimes you need to have a custom [[<tvar name=1>Special:MyLanguage/Extension default namespaces</tvar>|namespace for your extension(s)]]


== Dealing with existing pages == <!--T:74-->
== Dealing with existing pages == <!--T:74-->
Line 202: Line 200:
=== Use a maintenance script === <!--T:85-->
=== Use a maintenance script === <!--T:85-->
</translate>
</translate>
<translate><!--T:86--> Within the '''maintenance''' directory, there is a maintenance script which performs the above operation more effectively for a large number of pages; <tvar name=NamespaceDupes>'''{{ll|Manual:NamespaceDupes.php|NamespaceDupes.php}}'''</tvar></translate>
<translate><!--T:86--> Within the '''maintenance''' directory, there is a maintenance script which performs the above operation more effectively for a large number of pages: <tvar name=NamespaceDupes>'''{{ll|Manual:NamespaceDupes.php|NamespaceDupes.php}}'''</tvar></translate>
<translate><!--T:87--> It is simple to use, but as with all MediaWiki maintenance scripts, consult the available usage information first (use <code>--help</code> as an option).</translate>
<translate><!--T:87--> It is simple to use, but as with all MediaWiki maintenance scripts, consult the available usage information first (use <tvar name=1><code>--help</code></tvar> as an option).</translate>


<translate>
<translate>
Line 257: Line 255:
</translate>
</translate>
<translate><!--T:117--> Suppose that you need to rename custom namespace "Foo" to "New" without performing a mass move of pages.</translate>
<translate><!--T:117--> Suppose that you need to rename custom namespace "Foo" to "New" without performing a mass move of pages.</translate>
<translate><!--T:118--> The easiest way to achieve this is to preserve the namespace ID (here "<code>3000</code>") as well as the namespace constant (here "<code>NS_FOO</code>"), modify the (visible) namespace title and add the old one as an alias. </translate>
<translate><!--T:118--> The easiest way to achieve this is to preserve the namespace ID (here "<tvar name=1><code>3000</code></tvar>") as well as the namespace constant (here "<tvar name=2><code>NS_FOO</code></tvar>"), modify the (visible) namespace title and add the old one as an alias. </translate>


{{tmpl|0=<translate><!--T:119--> change <tvar name=1>$1</tvar> to <tvar name=2>$2</tvar></translate>
{{tmpl|0=<translate><!--T:119--> change <tvar name=1>$1</tvar> to <tvar name=2>$2</tvar></translate>
Line 275: Line 273:


<!--T:97-->
<!--T:97-->
In order for you to avoid namespace conflicts e.g. your namespace has the same number as a namespace defined by an [[<tvar name=cat>Special:MyLanguage/Category:Extensions</tvar>|extension]], the [[Extension namespace registration|extension namespace]] list shows you which numbers to avoid to prevent conflicts.
In order for you to avoid namespace conflicts e.g. your namespace has the same number as a namespace defined by an [[<tvar name=cat>Special:MyLanguage/Category:Extensions</tvar>|extension]], the [[<tvar name=1>Special:MyLanguage/Extension namespace registration</tvar>|extension namespace]] list shows you which numbers to avoid to prevent conflicts.


<!--T:98-->
<!--T:98-->
Line 293: Line 291:
<translate>
<translate>
<!--T:101-->
<!--T:101-->
where <code>3000</code> is the namespace's index and <code>#f3f3ff</code> is <span style="background-color: #f3f3ff">the color you want as its background color</span>.
where <tvar name=1><code>3000</code></tvar> is the namespace's index and <tvar name=2><code>#f3f3ff</code></tvar> is <span style="background-color: #f3f3ff">the color you want as its background color</span>.
</translate>
</translate>


You might also want to change the name of the tab from its default (the namespace's name).
<translate><!--T:135--> You might also want to change the name of the tab from its default (the namespace's name).</translate>
This is located in your system messages at MediaWiki:nstab-''namespace''.
<translate><!--T:142--> This is located in your system messages at {{<tvar name=1>tmpl|0=MediaWiki:nstab-$1</tvar>|''namespace''}}.</translate>


<translate>
<translate>
== See also == <!--T:102-->
== See also == <!--T:102-->
</translate>
</translate>
* [[Manual:Namespace]]
* {{ll|Manual:Namespace}}
* [[Namespace manager]] - <translate><!--T:104--> as originally proposed for MW1.6-wikidata and its successors.</translate> <translate><!--T:105--> Currently in use by the OmegaWiki project.</translate>
* {{ll|Namespace manager}} - <translate><!--T:104--> as originally proposed for MW1.6-wikidata and its successors.</translate> <translate><!--T:105--> Currently in use by the OmegaWiki project.</translate>


<translate>
=== Site administration ===
=== Site administration === <!--T:136-->
* {{ll|Manual:Configuration settings|Configuration settings}}, notably:
</translate>
* {{ll|Manual:Configuration settings}}
** {{ll|Manual:Configuration settings#Namespaces|2=<translate><!--T:103--> Manual:Configuration settings#Namespaces</translate>}}
** {{ll|Manual:Configuration settings#Namespaces|2=<translate><!--T:103--> Manual:Configuration settings#Namespaces</translate>}}
** {{wg|NamespacesToBeSearchedDefault}}
** {{wg|NamespacesToBeSearchedDefault}}
* {{ll|Manual:Maintenance scripts|2=<translate><!--T:110--> Maintenance scripts</translate>}}
* {{ll|Manual:Maintenance scripts|2=<translate><!--T:110--> Maintenance scripts</translate>}}


<translate>
=== Extensions ===
=== Extensions === <!--T:137-->
* [[:Category:Namespace extensions|Category:Namespace extensions]]
</translate>
* {{ll|Category:Namespace extensions}}
* {{ll|Extension:NamespaceManager}} - <translate><!--T:133--> for management of namespaces</translate>
* {{ll|Extension:NamespaceManager}} - <translate><!--T:133--> for management of namespaces</translate>
* {{ll|Extension:BlueSpiceNamespaceManager}} - lets you create custom namespaces
* {{ll|Extension:BlueSpiceNamespaceManager}} - <translate><!--T:138--> lets you create custom namespaces</translate>
* {{ll|Extension:SpecialNamespaces}} - <translate><!--T:107--> a modified version of the <tvar name=Interwiki>{{ll|Extension:Interwiki}}</tvar> which changes it to provide a namespace manager as a special page.</translate>
* {{ll|Extension:SpecialNamespaces}} - <translate><!--T:107--> a modified version of the <tvar name=Interwiki>{{ll|Extension:Interwiki}}</tvar> which changes it to provide a namespace manager as a special page.</translate>
* {{ll|Extension:Lockdown}} - <translate><!--T:108--> to control access to namespaces</translate>
* {{ll|Extension:Lockdown}} - <translate><!--T:108--> to control access to namespaces</translate>


<translate>
=== For extension developers ===
=== For extension developers === <!--T:139-->
* [[Extension namespace registration|<translate><!--T:109--> List of custom namespaces registered by extensions</translate>]]. If you define a constant for a custom namespace, list it here, too.
</translate>
* {{ll|1=Extension namespace registration}} - <translate><!--T:140--> List of custom namespaces registered by extensions.</translate> <translate><!--T:141--> If you define a constant for a custom namespace, list it here, too.</translate>


[[Category:MediaWiki configuration{{#translation:}}|{{PAGENAME}}]]
[[Category:MediaWiki configuration{{#translation:}}|{{PAGENAME}}]]

Latest revision as of 17:04, 16 October 2024

In addition to the built-in namespaces, it is possible to add custom namespaces to a MediaWiki installation, to further separate content and allow more logical organization.

Custom namespaces are simple to manage using the $wgExtraNamespaces configuration directive. It is also possible to define alias names for custom (and also predefined) namespaces, using the $wgNamespaceAliases configuration directive. Some extensions make it easy for you to create custom namespaces. Examples include NamespaceManager and BlueSpiceNamespaceManager .

It is 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.

Creating a custom namespace

[edit]

You register additional namespaces by adding them to the $wgExtraNamespaces global variable to your "LocalSettings.php" file. All namespaces require a unique numerical index in this array. As an example of simple custom namespace creation, adding the following lines to the "LocalSettings.php" file defines a "Foo" namespace 3000 and its associated "Foo_talk" namespace. Note that having a talk namespace associated with your custom namespace is currently a hard requirement.

// Define constants for my additional namespaces.
define("NS_FOO", 3000); // This MUST be even.
define("NS_FOO_TALK", 3001); // This MUST be the following odd integer.

// Add namespaces.
$wgExtraNamespaces[NS_FOO] = "Foo";
$wgExtraNamespaces[NS_FOO_TALK] = "Foo_talk"; // Note underscores in the namespace name.
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. Numbers from 3000 to 4999 are reserved for system administrators to define their custom namespaces. (Also, you’ll want to avoid any namespace name that’s already in Extension default namespaces.)
Even then odd
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, you 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.
No hyphens

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

$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] = [ '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
Do it early
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 the "LocalSettings.php" file (replacing news by the lowercased name of the protocol you wish to remove):
$wgUrlProtocols = array_diff( $wgUrlProtocols, array( 'news:' ) );

In extensions

[edit]
MediaWiki version:
1.25
Gerrit change 166705

Extensions often add their own namespaces, such as the Flow extension's "Topic" namespace. The extension.json registration system has a namespaces key for an extension to list its namespaces. From the Gadgets extension:

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

You can also set other namespace-related settings here, such as whether it should be a content namespace or not; see Manual:Extension.json/Schema for the available properties.

If namespace registration is conditional (for example EventLogging only defines its "Schema" namespace on the wiki where it stores schemas), the extension should add "conditional": true to the namespace definition in extension.json, and also register a handler for the CanonicalNamespaces hook there which decides whether to register the namespace or not. The hook handler should only change the $namespaces with which it is called; all other settings of the namespace should still be registered in the extension.json. If those settings should also be dynamic, do not change $wgContentNamespaces , $wgNamespaceContentModels etc. in the CanonicalNamespaces hook handler (it will have no effect – T288819); instead, you will have to set them earlier, such as in a callback (not in $wgExtensionFunctions ).

Note that adding an extension to LocalSettings.php does not necessarily make relevant namespace constants available as globals for other extensions.

Content namespaces

[edit]

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.

For a page to be considered an article, or proper content, it must:

When creating custom namespaces to hold additional content, it is a good idea to indicate this in the configuration. This is done via the $wgContentNamespaces configuration directive.

To extend the example above, one might add the following to the "LocalSettings.php" file:

$wgContentNamespaces[] = 3000;
or
$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.

Running maintenance scripts

[edit]
  • When adjusting the value of configuration parameter $wgContentNamespaces, 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 Manual:Maintenance scripts ).

Why you would want a custom namespace

[edit]

There are several reasons you might want this:

  • 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 namespaces require additional privilege(s) for editing.
  • 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)

Dealing with existing pages

[edit]

When storing page records, MediaWiki uses a namespace's numerical index, along with the remaining title text. Thus, when a page is created in a namespace that doesn't exist, e.g. "Bar:Some page", it is treated as being in the main namespace.

This can cause problems if adding a custom namespace definition for "Bar" at a later date, as MediaWiki will look for a page indexed via the proper namespace, but won't be able to find it, thus making the content inaccessible.

To correct this problem, there are three main approaches.

Move conflicting pages

[edit]

If the number of pages affected is small (e.g. "Bar" held five pages created before the namespace was defined in the site configuration), then the following approach might be suitable:

  1. Comment out the namespace definition in the configuration file
  2. Access each affected page, and move it out of the pseudo-namespace, e.g. move "Bar:Some page" to "Bar2:Some page"
  3. Un-comment the namespace definition
  4. Move the affected pages back into the new namespace

Use a maintenance script

[edit]

Within the maintenance directory, there is a maintenance script which performs the above operation more effectively for a large number of pages: NamespaceDupes.php It is simple to use, but as with all MediaWiki maintenance scripts, consult the available usage information first (use --help as an option).

Use a database query

[edit]

To move all pages "Bar:Some page" into namespace 3000, make the following database query:

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

To handle discussion pages:

UPDATE page SET
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.

Removing custom namespaces

[edit]

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

Example on how to remove Flow and the Topic namespace:

  • Uninstall Flow
  • Temporarily add $wgExtraNamespaces[2600] = 'Topic'; to the config
  • Use deleteBatch.php to delete all pages in the Topic namespace
  • Remove the $wgExtraNamespaces config

Renaming custom namespaces

[edit]

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.

change

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

to

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

Avoid namespace conflicts

[edit]

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.

Styling namespaces

[edit]

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.

You might also want to change the name of the tab from its default (the namespace's name). This is located in your system messages at MediaWiki:nstab-namespace.

See also

[edit]

Site administration

[edit]

Extensions

[edit]

For extension developers

[edit]