Page MenuHomePhabricator

mediawiki.searchSuggest: Support user search namespace preferences
Closed, ResolvedPublic

Description

Author: rainman

Description:
SimpleSearch searches only in the main namespace (namespaces=0) and ignores user/site-configured default search namespaces, as well as checked namespaces in advanced search.

This works with mwsuggest.js and is implemented in function os_getNamespaces()

Raised on en.wp VPT:
http://en.wikipedia.org/wiki/WP:VPT#Search_bar


See Also:
T67753: Provide way to override the behavior of default search box

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 21 2014, 11:09 PM
bzimport set Reference to bz24214.
bzimport added a subscriber: Unknown Object (MLST).

Also reported on [[zh:WP:UX]], and the reporter says prefixing the namespace names works for a specific namespace.

Thehelpfulonewiki wrote:

Reassign to default per bug 37789

Is this still happening? It has since been extracted into a stand-alone extension (Extension:Vector).

Moving to mediawiki-core/interface and setting to unconfirmed as it has been changed quite a bit since 2010.

chuckak wrote:

Confirmed still broken in 1.22.0rc2

chuckak wrote:

Here's a fix since the data is right there in mw.user.options.

Add this function to mediawiki.searchSuggest.js:

		// Get current namespaces
		function getNamespaces() {
			var state = null,
				namespaces = '',
				i = 0,
				limit = 1000;
			do {
				state = mw.user.options.get('searchNs'+i);
				switch (state) {
					case true:
						namespaces += i+'|';
					case null:
						break;
				}
				i++;
			} while (state != null);
			if (namespaces.length > 1) {
				namespaces = namespaces.substring(0, namespaces.length-1);
			}
			return namespaces;
		}

Then, on line 149, change "namespace: 0," to "namespace: getNamespaces(),".

Code I am referencing: https://git.wikimedia.org/blob/mediawiki%2Fcore.git/8bb12babcd4c791c50aa53f14f68224162aaef80/resources%2Fmediawiki%2Fmediawiki.searchSuggest.js

chuckak wrote:

One adjustment on the return so it always at least sends 0:

		// Get current namespaces
		function getNamespaces() {
			var state = null,
				namespaces = '',
				i = 0,
				limit = 1000;
			do {
				state = mw.user.options.get('searchNs'+i);
				switch (state) {
					case true:
						namespaces += i+'|';
					case null:
						break;
				}
				i++;
			} while (state != null);
			if (namespaces.length > 1) {
				return namespaces.substring(0, namespaces.length-1);
			}
			return 0;
		}

chuckak wrote:

I didn't realize how custom namespaces work so the above code wont work. Something like this should though:

// Get current namespaces

		function getNamespaces() {
			var namespaces = '',
				options = mw.user.options.get();
			for(var opt in options) {
				if(opt.substring(0,8) === 'searchNs') {
					if(options[opt] === true) {
						namespaces += opt.substr(8)+'|';
					}
				}
			}
			if (namespaces.length > 1) {
				return namespaces.substring(0, namespaces.length-1);
			}
			else {
				return 0;
			}
		}

Oh yay, thanks for going through the code!
You are very welcome to use Developer access

https://www.mediawiki.org/wiki/Developer_access

to submit this as a Git branch directly into Gerrit:

https://www.mediawiki.org/wiki/Git/Tutorial

Putting your branch in Git makes it easier to review it quickly.
Thanks again!

Somebody please add this to core. I was about to open a request for that.

Change 164893 had a related patch set uploaded by saper:
searchSuggest.js: Pass user ns preferences to API

https://gerrit.wikimedia.org/r/164893

Change 164893 had a related patch set uploaded (by Krinkle):
mediawiki.searchSuggest: Use user preferences for search namespaces

https://gerrit.wikimedia.org/r/164893

Patch-For-Review

I've tested the patch can confirm it's sending the right namespace IDs to the API, however I don't see any change in behaviour. And actually, I'm not sure what the right behaviour would be.

What is it supposed to do? Is an unprefixed query like "Foo" supposed to include "Template:Foo" if the template namespace is in the list? That's not happening in the API itself. It doesn't seem to be using the list of namespaces for anything.

Krinkle set Security to None.
Krinkle removed a subscriber: Unknown Object (MLST).

Yes, typing "Foo" should suggest (if exist)

  • "Foobar Article"
  • "Category:Foos"
  • "Template:Basename/Bar Foos"

Per T26214#1043978, the parameter passed here is (or was at least) ignored by the API. If changing that fixed it, I'd have merged the patch already.

It seems the prefix index engine itself is limited to searching one namespace at a time. So when first typing, it is a prefix for the main namespace. Now, it does support custom prefixes, so when you type "User:" or "Special:", it does naturally complete for the specified namespace prefix as expected.

Once the prefix engine supports and exposes a capability to have a query "Foo" produce results from one of multiple namespaces (T32323), then we can make use of it in searchSuggest.js

Per T26214#1043978, the parameter passed here is (or was at least) ignored by the API. If changing that fixed it, I'd have merged the patch already.

I think MediaWiki may be getting weirdly conflated with Wikimedia. There are other non-Wikimedia wikis with functional search and we're kind of screwing them over by dragging our feet here.

On Wikimedia wikis, $wgContentNamespaces may be weirdly indexed. If so, we should fix that.

However, in the case I saw over the weekend helping a user in #mediawiki, his wiki prefix search worked fine across namespaces (as it should...) and he had appropriately defined the namespaces as searchable in LocalSettings.php. However, I recommended to him to set namespace: '', instead of namespace: 0, in resources/src/mediawiki/mediawiki.searchSuggest.js as that seemed to be what fixed the issue he was having.

Here's what I saw.

With &namespace= (after hacking mediawiki.searchSuggest.js manually):

$ curl "http://pravnickyslovnik.cz/api.php?action=opensearch&format=json&formatversion=2&search=agres&limit=10&suggest=true&namespace=" && echo;
["agres",["Trestné činy:agrese"],[""],["http://pravnickyslovnik.cz/Trestn%C3%A9_%C4%8Diny:agrese"]]

Previous behavior, where &namespace=0 was always specified:

$ curl "http://pravnickyslovnik.cz/api.php?action=opensearch&format=json&formatversion=2&search=agres&limit=10&suggest=true&namespace=0" && echo;
["agres",[],[],[]]

With no namespace URL parameter at all:

$ curl "http://pravnickyslovnik.cz/api.php?action=opensearch&format=json&formatversion=2&search=agres&limit=10&suggest=true" && echo;
["agres",[],[],[]]

https://phabricator.wikimedia.org/rMW8f52f912f37f397c09748ddfe83d7214dcf73dd9

So if we replace the 0 with '' will that work for getting it to search all namespaces?

Change 332137 had a related patch set uploaded (by Paladox):
Add support for searching namespaces other then NS_MAIN

https://gerrit.wikimedia.org/r/332137

Change 332137 merged by jenkins-bot:
[mediawiki/core@master] mediawiki.searchSuggest: Add support for search namespace preferences

https://gerrit.wikimedia.org/r/332137

Verified on Beta Cluster.

  1. On any page type "Krinkle" in the search bar, no results.
  2. https://en.wikipedia.beta.wmflabs.org/w/index.php?title=Special:Search&search=&profile=advanced Enable Main and User namespace, check "Remember settings" and submit form.
  3. On any page type "Krinkle", the results now include User:Krinkle/common.js.

Change 164893 abandoned by saper:
mediawiki.searchSuggest: Use search namespace preferences

Reason:
https://gerrit.wikimedia.org/r/332137 fixed this

https://gerrit.wikimedia.org/r/164893

Change 164893 restored by saper:
mediawiki.searchSuggest: Use search namespace preferences

https://gerrit.wikimedia.org/r/164893

Change 164893 abandoned by saper:
mediawiki.searchSuggest: Use search namespace preferences

https://gerrit.wikimedia.org/r/164893