Knowledgebase

How to select the default namespaces to be searched by the search function in MediaWiki

In MediaWiki all the pages of your site are grouped into different namespaces. By default, the search function is configured to search only in the Main namespace. That's the namespace that contains the articles. So if the term or phrase for which the user searches is contained on pages belonging to other namespaces (e.g. discussion pages, user pages, etc.), these pages will not be listed in the search results, unless from the advanced search options the user specifically selects other namespaces.

If you want to, you can enable the search function for other namespaces. For example, you may want discussion pages associated with articles and personal user pages also to be searched by default. You can do this with the help of the option $wgNamespacesToBeSearchedDefault. You have to add it at the end of the LocalSettings.php file of your MediaWiki together with a list of the namespaces. So edit LocalSettings.php and add the following at the end of the file:

$wgNamespacesToBeSearchedDefault = array(
        NS_MAIN =>           true,
        NS_TALK =>           false,
        NS_USER =>           false,
        NS_USER_TALK =>      false,
        NS_PROJECT =>        false,
        NS_PROJECT_TALK =>   false,
        NS_FILE =>           false,
        NS_FILE_TALK =>      false,
        NS_MEDIAWIKI =>      false,
        NS_MEDIAWIKI_TALK => false,
        NS_TEMPLATE =>       false,
        NS_TEMPLATE_TALK =>  false,
        NS_HELP =>           false,
        NS_HELP_TALK =>      false,
        NS_CATEGORY =>       false,
        NS_CATEGORY_TALK =>  false
);

The namespaces are listed with their text IDs (also known as constants). By default, only NS_MAIN is set to true, which means that the search function works only with the Main namespace. For a complete list of the namespaces with their respective constants and what each namespace is used for check out the article with the reference list of MediaWiki namespaces with their ID numbers and constants.

To set the search function to search the pages of a namespace simply change false with true for that namespace.

For example, if you want discussion pages associated with articles, personal user pages and their associated discussion pages and category pages to be searched, make sure you have the following in your LocalSettings.php file:

$wgNamespacesToBeSearchedDefault = array(
        NS_MAIN =>           true,
        NS_TALK =>           true,
        NS_USER =>           true,
        NS_USER_TALK =>      true,
        NS_PROJECT =>        false,
        NS_PROJECT_TALK =>   false,
        NS_FILE =>           false,
        NS_FILE_TALK =>      false,
        NS_MEDIAWIKI =>      false,
        NS_MEDIAWIKI_TALK => false,
        NS_TEMPLATE =>       false,
        NS_TEMPLATE_TALK =>  false,
        NS_HELP =>           false,
        NS_HELP_TALK =>      false,
        NS_CATEGORY =>       true,
        NS_CATEGORY_TALK =>  false
);

These changes apply to all users of the site, both unregistered and registered. In MediaWiki versions 1.15 and older the changes will not affect existing registered users. For such users you have to configure the options with the help of the script userOptions.php. For more information on how to use it check out the article on how to edit the options of users in MediaWiki.

Note that the above changes set which namespaces should be automatically searched by default when the search function is used. On the page with the search results on the frontend there are advanced options that can be viewed by clicking on the Advanced button. It displays a list of the namespaces and the user can specifically select which namespaces they want to search. This can be done regardless of which namespaces are set to be searched by default with the option $wgNamespacesToBeSearchedDefault.

The LocalSettings.php file that you have to modify is in the root MediaWiki folder on your MediaWiki hosting account. If you have installed the application directly in the public_html directory on the account (making the frontend URL yourdomain.com), then the path to the file on the account with respect to that directory will be public_html/LocalSettings.php. An easy way for HostKnox customers to edit files on their accounts is with the File manager of the Pixie control panel. You can also download the file on your local computer (e.g. with an FTP client), edit it with a text editor (e.g. Notepad ++) and upload it back overwriting the old file.

Was this answer helpful?

 Print this Article

Also Read