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.
Creating a custom namespaceEdit
Custom namespaces are created via manipulation of the $wgExtraNamespaces global variable. Manipulation of this variable must be completed before completion of MediaWiki initialization; for instance it cannot be manipulated in a extension post initialization hook. As an example of simple custom namespace creation, one can add an appropriate line to LocalSettings.php
, e.g.
$wgExtraNamespaces[500] = "Foo";
Note the use of the constant 500 in defining the namespace. All namespaces require a numerical index; for custom namespaces, the indices start at 100. In choosing such an index, you may want to avoid any that is listed in extension namespace registration as being used by an extension, since you might want to install that extension later.
- even namespace index denotes a subject namespace.
- odd index denotes a discussion ("talk") namespace.
It is recommended that you define a discussion namespace with each custom namespace, so the declaration above might be expanded to:
$wgExtraNamespaces[500] = "Foo"; $wgExtraNamespaces[501] = "Foo_talk";
With this declaration, if a page is moved to the Foo namespace, numbered 500, you will be prompted to move the associated talk page. If you choose to do so, MediaWiki will place the page in the namespace numbered 501.
If you need to refer to the ID of your namespace later on in the configuration, for example for $wgNamespaceProtection, $wgNamespacesWithSubpages or an extension like Lockdown, it is recommended that you define constants for your namespace IDs (only letters, numbers and underscores allowed):
define("NS_FOO", 500); define("NS_FOO_TALK", 501); $wgExtraNamespaces[NS_FOO] = "Foo"; $wgExtraNamespaces[NS_FOO_TALK] = "Foo_talk"; // underscore required. use it to separate all words in the title of 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
Content namespacesEdit
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:
- Be in the main namespace, or a defined content namespace
- Not be a redirect page
- Contain at least one internal link
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 LocalSettings.php
:
$wgContentNamespaces[] = 500;
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 scriptsEdit
- When adjusting the value of
$wgContentNamespaces
, it is a good idea to run either themaintenance/updateArticleCount.php
ormaintenance/initStats.php
script to update the internal statistics cache (see: Manual:Maintenance scripts).
Why you would want a custom namespaceEdit
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.
Dealing with existing pagesEdit
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 pagesEdit
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:
- Comment out the namespace definition in the configuration file
- Access each affected page, and move it out of the pseudo-namespace, e.g. move "Bar:Some page" to "Bar2:Some page"
- Un-comment the namespace definition
- Move the affected pages back into the new namespace
Use a maintenance scriptEdit
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 queryEdit
To move all pages "Bar:Some page" into namespace 500, make the following database query:
UPDATE page SET page_title = REPLACE(page_title, 'Bar:', ''), page_namespace = 500 WHERE page_title LIKE 'Bar:%' AND page_namespace=0
To handle discussion pages:
UPDATE page SET page_title = REPLACE(page_title, 'Bar:', ''), page_namespace = 501 WHERE page_title LIKE 'Bar:%' AND page_namespace=1
Removing custom namespacesEdit
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 conflictsEdit
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 (e.g. Background)Edit
To set the background color of pages in the specified namespace, add following code to your common.css:
.ns-500 #content, .ns-501 #content { background-color: #f3f3ff; } .ns-500 div.thumb, .ns-501 div.thumb { border-color: #f3f3ff; }
where "500" is your namespace index, #f3f3ff is the namespace background color.
See alsoEdit
- Manual:Configuration settings#Namespaces
- $wgNamespacesToBeSearchedDefault
- Namespace manager as originally proposed for MW1.6-wikidata and its successors. Currently in use by the OmegaWiki project.
- Extension:SpecialNamespaces, a modified version of the Extension:Interwiki which changes it to provide a namespace manager as a special page.
- Extension:NamespacePermissions and Extension:Lockdown to control access to namespaces
- Extension namespace registration
- Maintenance scripts
Language: | English • Deutsch • 日本語 • 中文 |
---|