MediaWiki Namespaces Tutorial - Learn How to Manage Namespaces in MediaWiki
What are namespaces and how to manage them in MediaWiki
Namespaces in MediaWiki are used for grouping pages. The pages are grouped based on their purpose and function on the site. Namespaces make it easier to manage the pages. Each page belongs to a namespace. For example, all the regular content pages, also called articles, are part of the Main namespace, while all personal user pages are stored in the User namespace. In this tutorial we'll review in details what the default namespaces are, how to create custom ones, and we'll discuss various aspects related to namespaces.
We'll go over:
- what the default namespaces are, their ID numbers and constants
- how the URLs of namespaces are formed
- how to restrict the editing of namespaces
- how to create custom namespaces
- links to articles related to namespaces
Default Namespaces
MediaWiki comes preconfigured with 18 namespaces. Two of these namespaces cannot be modified by users, meaning that their pages cannot be edited and new ones can't be added. The pages in the other 16 namespaces can be edited and new ones can be added; these namespaces go in pairs. There are, so to say, 8 general namespaces and for each of them there's a corresponding namespace for discussion pages associated with the pages in that namespace.
Every namespace has an ID number and a constant with which it's identified in the code. The constants are in uppercase and they all start with NS_ followed by the name of the namespace. These numbers and constants are defined in the file Defines.php. The file is in the includes folder, and that folder itself is in the root MediaWiki directory on your MediaWiki hosting account. The ID numbers are used when you want to make some changes to the options of the namespaces.
Here we'll list and describe each of the default namespaces. We'll also include the ID number of each namespace and in brackets after it the constant with which it's identified.
-2 (NS_MEDIA) - the Media namespace. This is one of the two virtual namespaces. It cannot be modified by users. The purpose of that namespace is to make it possible to provide direct links to uploaded media files.
-1 (NS_SPECIAL) - the namespace Special is the other virtual namespace that contains pages that cannot be edited by users. The pages with all the tools and statistics belong to that namespaces. These pages can be accessed and used by clicking on the Special pages button in the Tools menu.
0 (NS_MAIN) - the Main namespace. As the name suggests this is the namespace that's intended for the bulk of the pages. It's for storing all the content pages with the articles contributed by users. It's also the only namespace that doesn't have a prefix in the URL. This means that unlike the pages from the other namespaces, the pages in the Main namespace don't have a prefix in front of their names in the URL address.
1 (NS_TALK) - the Talk namespace for the discussion pages of the articles. Each page in the Main namespace can have a discussion page on which users can post comments about the article. The Talk namespace contains these discussion pages.
2 (NS_USER) - the User namespace is for personal user pages. Each user can create their own page that bears the username of that user. On that page the user can provide personal information or any other information that they see fit. For detailed information on users check out the tutorial on how to manage users, user groups and permissions in MediaWiki.
3 (NS_USER_TALK) - each personal user page can have an associated discussion page on which other users can leave comments to the particular user. The User talk namespace contains these discussion pages. When a user opens the site and the talk page of that user has been edited by somebody else (i.e. a comment has been made), a message is displayed at the top of the site on all pages. The message is removed after the user visits their talk page. It's also possible for users to receive notifications via email for changes on their user talk pages; for this to happen a corresponding option has to be enabled for the whole site. For more information read the article on how to enable and disable user talk page notifications via email in MediaWiki.
4 (NS_PROJECT) - the purpose of the Project namespace is to store pages about the site itself. It doesn't have any special options or functions that differentiate it from the Main namespace. It's there just for convenience. By default, it doesn't contain any pages but in the footer of the site there are links to pages that you can create and that are intended for the Project namespace. These include an About us page, Disclaimers and Privacy policy.
Note that label used on the frontend for the Project namespace may actually be different than Project. This is initially set during the installation of the application, and by default the namespace is set to be labeled with the name that you provide for your site. If you haven't changed or edited the corresponding option, then the Project namespace should in fact have the name of your site. So if you have, for example, labeled your site Example Wiki, then the name of the namespace will be Example Wiki (that's also the prefix that will be used in the URLs of pages in the namespace). Keep in mind however that Project can still be used as a prefix when searching for or opening the URL of a page in that namespace. They will be redirected to the actual name (if it's different than Project). If we use our example name, the page Privacy policy in that namespace can be opened with both yourdomain.com/index.php/Example_Wiki:Privacy_policy and yourdomain.com/index.php/Project:Privacy_policy.
Once the application is installed the name of the site and the name of the Project namespace (also known as meta name) can be changed by modifying the respective options in the file LocalSettings.php. For more information check out the article on how to change the name of the site and the Project namespace in MediaWiki.
5 (NS_PROJECT_TALK) - as the other talk namespaces the Project talk namespace is for storing discussion pages associated in this case with the Project namespace. The exact name of the namespace depends on what the name of the Project namespace is. For instance, if the name is Example Wiki, then the corresponding name for the talk namespace will be Example Wiki talk.
6 (NS_FILE) - the File namespace is for pages of uploaded files. When a user uploads a file like an image, for instance, this automatically creates a page for that image. That page lists the file, in the case of images a resized preview copy of the image is displayed, and some additional information is also shown (e.g. the time the file was uploaded, the user who uploaded it, the pages that link to it, etc.). It's also possible to add some more content and information to the page by editing it in the same way as any other page.
7 (NS_FILE_TALK) - the File talk namespace is similar to other talk namespaces; it simply contains the talk pages associated with the particular namespace. In this case it stores the discussion pages for commenting the pages in the File namespace.
8 (NS_MEDIAWIKI) - the MediaWiki namespace is for pages that are used for modifying the site. Basically, they are used mostly for changing the appearance of the site. Here are stored the pages for making CSS (Cascading Style Sheets) changes to all or some of the skins. Such pages are for example MediaWiki:Common.css (for the whole site), MediaWiki:Vector.css (for changing the Vector skin), etc. Here are also stored the pages for editing the interface text and messages of the site; for more information on this check out our article on how to change the interface text in MediaWiki. In our knowledge base you'll also find some articles on making CSS changes to the skins.
9 (NS_MEDIAWIKI_TALK) - the MediaWiki talk namespace is no different than the other talk namespaces. It's for discussion pages associated with the pages from the MediaWiki namespace.
10 (NS_TEMPLATE) - the Template namespace is for storing template pages. With a template page you can create a template, or edit an existing one. Creating and editing a template page is done in the same way as creating/editing a regular page. The content of the page is the template. A template can be added to other pages (also referred to as transclusion). This can be used for adding some standard message on many pages without the need to type the whole message every time.
11 (NS_TEMPLATE_TALK) - the Template talk namespace for the pages with discussions about the template pages.
12 (NS_HELP) - the Help namespace is primarily created for convenience. In terms of its functions and options it's not any different than the Main namespace. You can use it to add to it pages with helpful information, instructions on anything related to the site or whatever you want. By default, it doesn't contain any preconfigured pages.
13 (NS_HELP_TALK) - the Help talk is for discussion pages for commenting the pages of the Help namespace.
14 (NS_CATEGORY) - the Category namespace contains all the category pages. Categories are used for grouping pages that have something similar in terms of their content or the subject they deal with. The pages in a category can be from different namespaces. Each category page lists the pages that are in that category. When a page is added to a category, that category is also automatically listed at the bottom of the page. For more information on categories check out the tutorial on how to create and manage categories in MediaWiki.
15 (NS_CATEGORY_TALK) - the Category talk namespace stores the talk pages for commenting category pages.
URL Addresses of Namespaces
The URL address of a page is formed by adding to the base URL of the site the prefix for the namespace to which the page belongs, followed by a colon and the name of the page. So to be more precise the full name of the page is actually formed by the prefix of the namespace plus the name of the page.
The name of a namespace is used for its prefix. In the above section of this tutorial the name of each namespace is shown in bold and italicized. If the name consists of more than one word, in the prefix put an underscore between the words. For example, the prefix of the User talk namespace is User_talk. The words in a page title are also separated with underscores in the URL address, but even if you type the URL with spaces between the words, MediaWiki will automatically put underscores. So, for example, if a page in the File namespaces is named Example file.jpg, then the full page name with the namespace prefix will be File:Example file.jpg. If the main URL of your site is yourdomain.com, then the URL to that page will be yourdomain.com/index.php/File:Example_file.jpg.
Note that the only namespace that doesn't add a prefix to its pages is the Main namespace. So the URLs of all articles don't have a namespace prefix, just the name of the page is added to the base URL.
Restrict the Editing of Namespaces
In MediaWiki you can prevent the users of particular user groups from editing specific pages, or you can also do it for all pages on the site. It's also possible to restrict users from editing all the pages in particular namespaces. For example, you may want to prevent all unregistered users from editing the pages in the User talk namespace.
Such restrictions can be achieved by modifying the file LocalSettings.php of your MediaWiki. There's an option $wgNamespaceProtection that's specifically intended for restricting the editing of namespaces. With that option you can specify the users that can edit the pages in the selected namespace. On a new line at the end of LocalSettings.php you have to add a line that has the following form:
$wgNamespaceProtection[NS_NAMESPACE] = array( 'permission' );
You have to replace NS_NAMESPACE with the actual constant of the namespace and permission with a user permission. You can find the IDs/constants of all namespaces in the first section of this tutorial. All users who have the specified user right will be able to edit the pages in the particular namespace. The rest of the users will not be allowed to do it.
You can either use an existing permission that is assigned only to the user groups that you want to allow to edit the pages in the namespace or you can create a new permission, assign it to the desired groups and then use that permission. For example, with the following added to LocalSettings.php:
$wgNamespaceProtection[NS_USER_TALK] = array( 'editinterface' );
only administrators/sysops will be able to edit the pages of the User talk namespace. The editinterface right is a core permission that's assigned by default only to admins. You can create a new permission and then use it to restrict the editing of namespaces. For example, with the following:
$wgGroupPermissions['user']['editusertalk'] = true; $wgNamespaceProtection[NS_USER_TALK] = array( 'editusertalk' );
you'll create a new permission labeled editusertalk, you'll assign it to the user group (that's the group of all registered users) and you'll allow only users that have that permission to edit pages from the User talk namespace. This means that only visitors/unregistered users will not be able to add comments to the talk pages of users.
For more information on users and user groups check out the tutorial on how to manage users, user groups and permissions in MediaWiki.
The LocalSettings.php file that you have to edit to achieve this is located in the root MediaWiki folder on your MediaWiki hosting account. HostKnox clients can edit it with the File manager of the HostKnox control panel. Another way is to download it on your local computer, edit it with a text editor (e.g. Notepad ++) and upload it back.
For information on how to restrict the editing of all pages on the site or just of individual pages read the article on how to restrict the editing of pages in MediaWiki.
Create Custom Namespaces
In addition to the default namespaces it's possible to create custom ones. If for any reason you need a new namespace(s), you can create namespaces with the desired names. You may, for instance, want to have a namespace for storing pages with content on a specific subject and/or a namespace that can be edited only by a selected user group(s).
As we explained, normally namespaces go in pairs: one for the content pages and one talk namespace for the discussion pages for commenting the content pages. So if you decide to create a namespace, it's recommended that you also create a talk namespace for it.
You can create a namespace by adding a few lines at the end of the LocalSettings.php file of your MediaWiki. One line is used for defining the ID of the namespace, meaning it's ID number and the text ID/constant. Then another line is used for adding the namespace; this is done with the option $wgExtraNamespaces. Using that option and the constant for the namespace that you defined with the previous line, you can specify the name of the namespace to be used on the frontend.
For example, with the following added to your LocalSettings.php file:
define("NS_EXAMPLE", 3000); define("NS_EXAMPLE_TALK", 3001); $wgExtraNamespaces[NS_EXAMPLE] = "Example"; $wgExtraNamespaces[NS_EXAMPLE_TALK] = "Example_talk";
you'll create the namespace Example and its associated discussion namespace Example talk. With the define directive you specify the ID number and the constant of each namespace. A few important points to keep in mind:
- The constant is all in uppercase and should start with NS_ followed by the name; words are separate with underscores. In our example the constants are NS_EXAMPLE and NS_EXAMPLE_TALK.
- The ID number of a content namespace should be an even number and the number of a discussion namespace should be an odd number. In our example they are respectively 3000 and 3001.
- You have to be careful what numbers you choose for the IDs of the new namespaces. They shouldn't be the same as the ones of existing namespaces or ones that may be created by extensions. The numbers of the default namespaces are from -2 to 15 but there are many extensions that, when installed, automatically create new namespaces needed for their functions with some specified ID numbers. Most extensions use the numbers from 100 to 3000, so a save way to go about it is to use numbers from 3000 and up.
With the option $wgExtraNamespaces use the constant of the namespace that's specified with the define option and put the corresponding name to be used on the frontend. In our example, the namespace NS_EXAMPLE has the name Example and the NS_EXAMPLE_TALK namespace has the name Example_talk; that name is used as the prefix in the URLs of pages belonging to the namespace. Make sure that if the name consists of more than one word you separate them with underscores (e.g. Example_talk); don't leave spaces between the words.
Links to Articles Related to Namespaces
When it comes to namespaces you may also find useful the following articles in our knowledge base:
- how to add namespace aliases in MediaWiki
- how to select the default namespaces to be searched by the search function in MediaWiki
- how to limit the sitemap to specific namespaces in MediaWiki
- how to turn namespaces into content namespaces in MediaWiki
- how to check the ID number of a namespace in MediaWiki