Page MenuHomePhabricator

Investigation: Check how search requests for gendered category labels could be altered
Closed, ResolvedPublic

Description

People might start using female gendered categories when they search in categories. So we might be able to consider that and manipulate the search request on submit.

  • Figure out a way to change ( gendered ) category names in search requests.

Event Timeline

WMDE-Fisch triaged this task as Low priority.Jul 15 2019, 12:24 PM
WMDE-Fisch created this task.
WMDE-Fisch added a comment.EditedJul 16 2019, 1:23 PM

So the general search in core is triggered by using searchText/searchTitle on an instance of SearchEngine. The type of instance is set in the corresponding SearchEngineFactory and depends on configuration. ( by default it's some type of SearchDatabase with CirrusSearch installed it's CirrusSearch ).

Most search queries are triggered either in SpecialSearch or via ApiQuerySearch.

On searches triggered by SpecialSearch we could alter search terms by using the SpecialSearchResultsPrepend hook. On ApiQuerySearch there seems to be no such hook, so we would either need to add one or do manipulations at some other place

For the autosuggestion in the search form MediaWiki mainly uses ApiOpenSearch in the background. This eventually calls completionSearchWithVariants and completionSearchBackend on the according instance of SearchEngine.

In the default implementation of the search there's the PrefixSearchBackend hook that could be used to manipulate the search request here. For CirrusSearch it looks like this hook is only called in cases where the default namespace is part of the search[1].

[1] https://gerrit.wikimedia.org/g/mediawiki/extensions/CirrusSearch/+/fe9f27a967ce0005e0a4610231f5ead4af9e1849/includes/CirrusSearch.php#331

So manipulating search terms in the backend would be possible. On the front-end side we could obviously use JS to do fancy stuff on category searches, but that might then interfere with different UIs and gadgets.

All in all I guess that having these lookups for every search request only should be done if it's not to costly ( e.g. having a local cache or table for 1on1 matching ) .

POC snippet

public static function onSpecialSearchResultsPrepend( SpecialSearch $specialSearch, OutputPage $out, $term ) {
	global $onSpecialSearchResultsPrepend;
	if ( !$onSpecialSearchResultsPrepend ) {
		$onSpecialSearchResultsPrepend = true;

		$newTerm = preg_replace_callback( '/(incategory:)(\s*)(\S*)/s', 'self::replaceCategoriesInSearch', $term );

		$specialSearch->showResults( $newTerm );
		return false;
	}
}

private static function replaceCategoriesInSearch( $matches ) {
	if ( $replacment = self::getOriginalFromGendered( $matches[3] ) ) {
		return $matches[1] . $matches[2] . $replacment;
	}
	return $matches[0];
}
awight added a subscriber: awight.

Dastardly! And it works :-)

Lea_WMDE closed this task as Resolved.Jul 25 2019, 1:46 PM
Lea_WMDE claimed this task.
Lea_WMDE moved this task from Demo to Done on the WMDE-QWERTY-Spike-2019-07-09 board.