Handbuch:Verwendung von benutzerdefinierten Namensräumen

This page is a translated version of the page Manual:Using custom namespaces and the translation is 29% complete.
Outdated translations are marked like this.

Zusätzlich zu den eingebauten Namespaces ist es möglich, benutzerdefinierte Namensräume zu einer MediaWiki-Installation hinzuzufügen, um Inhalte weiter zu trennen und eine logischere Organisation zu ermöglichen.

Benutzerdefinierte Namensräume sind mit der Konfigurationsrichtlinie $wgExtraNamespaces einfach zu verwalten. Es ist auch möglich, Aliasnamen für benutzerdefinierte (und auch vordefinierte) Namensräume zu definieren, indem man die Konfigurationsdirektive $wgNamespaceAliases verwendet. Some extensions make it easy for you to create custom namespaces. Beispiele enthalten NamespaceManager und BlueSpiceNamespaceManager .

Es wird empfohlen, vor der Bearbeitung von Namensräumen sicherzustellen, dass keine Aufträge in der job queue anstehen, um zu vermeiden, dass solche Aufträge fehlschlagen, wenn sie auf Seiten von Namensräumen abzielen, die Sie gerade löschen oder umbenennen wollen. Verwenden Sie runJobs.php , um alle anstehenden Aufträge auszuführen und die Warteschlange zu leeren, bevor Sie die Namespace-Konfiguration ändern.

Einen benutzerdefinierten Namensraum erstellen

Sie registrieren zusätzliche Namespaces, indem Sie sie der globalen Variable $wgExtraNamespaces in Ihrer Datei "LocalSettings.php" hinzufügen. Alle Namensräume benötigen einen eindeutigen numerischen Index in diesem 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:

// Define constants for my additional namespaces.
define("NS_FOO", 3000); // Diese Zahl MUSS gerade sein.
define("NS_FOO_TALK", 3001); // Dies MUSS die darauffolgende Ganzzahl sein.

// Füge Namensräume hinzu.
$wgExtraNamespaces[NS_FOO] = "Foo";
$wgExtraNamespaces[NS_FOO_TALK] = "Foo_talk"; // Note underscores in the namespace name.
Wähle eine unbenutzte Nummer
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.
Erstelle den Diskussions-Namensraum ebenfalls.
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".
Keine Leerzeichen
Use underscores instead of spaces when registering namespace names. "My Namespace" is invalid here; use "My_Namespace" instead.
Keine Bindestriche

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

$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
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 .
Achten Sie auf Kollisionen mit URL-Protokollen
Der Verlinkungscode von MediaWiki kennt eine Reihe von URL-Protokollen, die in der Variable $wgUrlProtocols definiert sind. 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 Erweiterungen

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 (task 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 Version:
Gerrit change 166705

The extension.json registration system has a namespaces key for an extension to list its namespaces that should always exist. From the Gadgets extension:

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

It also supports the CanonicalNamespaces hook. Extensions should prefer extension registration over CanonicalNamespaces. 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 (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;
$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

  • 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 Handbuch:Wartungsskripte ).

Warum möchtest du einen benutzerdefinierten Namensraum

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

Handeln mit bestehenden Seiten

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.

Konfliktseiten verschieben

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
  1. Access each affected page, and move it out of the pseudo-namespace, e.g. move "Bar:Some page" to "Bar2:Some page"
  1. Un-comment the namespace definition
  1. Move the affected pages back into the new namespace

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

Use a database query

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

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

To handle discussion pages:

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.

Entfernen benutzerdefinierter Namensräume

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

Beispiel, wie man Flow und den Thema-Namensraum entfernt:

  • Deinstalliere Flow
  • Füge temporär $wgExtraNamespaces[2600] = 'Topic'; in der Konfiguration hinzu
  • Benutze deleteBatch.php um alle Seiten im Thema-Namensraum zu löschen
  • Entferne die $wgExtraNamespaces-Einstellung

Umbenennen benutzerdefinierter Namensräume

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.


Namensraumkonflikte vermeiden

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.

Namensräume gestalten

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.

Siehe auch

Aktuell vom OmegaWiki-Projekt genutzt.