In addition to the built-innamespaces, 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 includeNamespaceManager andBlueSpiceNamespaceManager.
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 ahard 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.
The uppercase part does not permit hyphens but they can still be safely added to the prefix title.Example:
$wgExtraNamespaces[NS_FOOFOO]="Foo-Foo";
$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
$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
.news:
is a URL protocol for NNTP newsgroups.news
by the lowercased name of the protocol you wish to remove):$wgUrlProtocols=array_diff($wgUrlProtocols,array('news:'));
MediaWiki version: | ≥ 1.25 Gerrit change 166705 |
Extensions often add their own namespaces, such as theFlow extension's "Topic" namespace.Theextension.json registration system has anamespaces
key for an extension to list its namespaces.From theGadgets 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; seeManual:Extension.json/Schema for the available properties.
If namespace registration is conditional (for exampleEventLogging only defines its "Schema" namespace on the wiki where it stores schemas), the extension should add"conditional": true
to the namespace definition inextension.json
, and also register a handler for theCanonicalNamespaces 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 theextension.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 acallback (not in$wgExtensionFunctions
).
Note that adding an extension to LocalSettings.php does not necessarily make relevant namespace constants available as globals for other extensions.
When building the site statistics page (seeSpecial: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:
$wgArticleCountMethod
.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;
$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 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 (seeManual:Maintenance scripts).
There are several reasons you might want this:
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.
You can use theNamespaceDupes maintenance script to correct this problem. It will find pages in the main namespace with a prefix matching any of the defined namespaces, and update their records.
If you are not able to run maintenance scripts, then the following approach might be suitable:
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 namespacesremoved, but it is a problem.
MediaWiki version: | ≥ 1.43 Gerrit change 1052196 |
You can use theCleanupTitles maintenance script to correct this problem. It will find pages any namespace that is no longer defined, and update their records. The pages will be moved to the main namespace with an added prefix likeNS123:, unless specified otherwise by parameters to the script.
As above, you can also temporarily restore the namespace configuration and move the pages from the user interface (or delete them).
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;
Note that some extensions store namespace names in plain text instead of their internal IDs, and should therefore be dealt with extra care.Examples include:
In order for you to avoid namespace conflicts e.g. your namespace has the same number as a namespace defined by anextension, theextension 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 yourcommon.css:
.ns-3000#content,.ns-3001#content{background-color:#f3f3ff;}.ns-3000div.thumb,.ns-3001div.thumb{border-color:#f3f3ff;}
where3000
is the namespace's index and#f3f3ff
isthe 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.