Manual:Using custom namespaces

From MediaWiki.org

(Redirected from Help:Custom Namespaces)
Jump to: navigation, search

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.

Contents

[edit] Creating a custom namespace

To create a custom namespace, add an appropriate line to LocalSettings.php, e.g.

$wgExtraNamespaces[100] = "Foo";

Note the use of the constant 100 in defining the namespace. All namespaces require a numerical index; for custom namespaces, these start at 100. Another point to remember is that an even namespace index denotes a content namespace, whereas an odd index denotes a discussion namespace. It is usual to define a discussion namespace with each custom namespace, so the declaration above might be expanded to:

$wgExtraNamespaces[100] = "Foo";
$wgExtraNamespaces[101] = "Foo_talk";

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:

define("NS_FOO", 100);
define("NS_FOO_TALK", 101);
 
$wgExtraNamespaces[NS_FOO] = "Foo";
$wgExtraNamespaces[NS_FOO_TALK] = "Foo_talk";
 
$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

[edit] Content namespaces

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[] = 100;

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 the maintenance/updateArticleCount.php script to update the internal statistics cache (see: Manual:Maintenance scripts).

[edit] Dealing with existing pages

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 two main approaches.

[edit] Move conflicting pages

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

[edit] Use a maintenance script

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.

[edit] Use a database query

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

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

[edit] Removing custom namespaces

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.

[edit] See also

Personal tools