Page MenuHomePhabricator

Search for anonymous users default to Main, but no checkbox?
Closed, DeclinedPublic


Author: jr534

It appears confusing to naive users that on a search as an anonymous user,
the search results page doesn't show the Main checkbox as checked (although
anonymous searches default as such), so the user may wonder what exactly is
the search space. Furthermore, why DO the default namespaces to search for
anonymous users only include Main?

The first topic:

Looking at the code, the checkboxes are outputted in
SpecialSearch::powerSearchBox and are checked for each user namespace found
in the user options (for anonymous users, there will be none).

Now if you look at SpecialSearch::showResults, the search is conducted
before the call to SpecialSearch::powerSearchBox. When that search is
conducted, a copy of the user namespaces (up to now, empty) is passed and
in SearchEngine::queryNamespaces, it is finally realized that if no user
namespaces exist, then default to the Main namespace. But since this is a
copy of the namespaces array, the change is not reflected when control
passes back to SpecialSearch:showResults and never displays (bug?).

The second topic:

When users are created, they inherit the $wgNamespacesToBeSearchedDefault
as their settings, why not allow anonymous searches to include the same?

A humble suggestion to address the two topics is to rewrite
SpecialSearch::userNamespaces as follows:

function userNamespaces( &$user ) {

global $wgNamespacesToBeSearchedDefault;
if( !$user->mId ) {
  return array_keys( $wgNamespacesToBeSearchedDefault );
else {
  ... //put the entire code currently in userNamespaces here


Version: unspecified
Severity: minor



Event Timeline

bzimport raised the priority of this task from to Low.Nov 21 2014, 8:46 PM
bzimport added a project: MediaWiki-Search.
bzimport set Reference to bz3188.
bzimport added a subscriber: Unknown Object (MLST).

jr534 wrote:

This would also allow the wiki admin to control what are the initial
search settings for all newly-created and anonymous users if they
choose to override $wgNamespacesToBeSearchedDefault in
LocalSettings.php (a db script is needed to update for existing users)

wikipedia wrote:

I agree that this needs fixing.

wikipedia wrote:

The code above simply checks all namespaces as it just returns all namespaces
that have a key in '$wgNamespacesToBeSearchedDefault'.

This works as intended:
function userNamespaces( &$user ) {

		global $wgNamespacesToBeSearchedDefault;
		$arr = array();
		if( !$user->mId ) {
			foreach( $wgNamespacesToBeSearchedDefault as $nsId => $value ) {
				if ($value) {
					$arr[] = $nsId;
			foreach( SearchEngine::searchableNamespaces() as $ns => $name ) {
				if( $user->getOption( 'searchNs' . $ns ) ) {
					$arr[] = $ns;
		return $arr;


wikipedia wrote:

Patched search function for anonymous users

Just adding the above code as a file. Apply by replacing the respective
function in 'SpecialSearch.php'.


Doesn't appear to be such a problem in current MediaWiki.