Page MenuHomePhabricator

“Create a new item” link on search results page can produce incorrectly-escaped label
Closed, ResolvedPublic

Description

As a Wikidata editor, when I search for a label and find no matching item, I want to create a new item for this label, in order to use that new item e. g. in a statement elsewhere.

Problem:
On Wikidata, the search page includes a link to Special:NewItem if there are no search results, with the label for the new item pre-filled with the search term. (This is done by customizing an interface message at MediaWiki:search-nonefound.) However, sometimes the label is escaped incorrectly; namely, an apostrophe turns into “'”.

This issue was first reported on the contact the development team page (permanent link).

Example:
Example search link

BDD
GIVEN a search via Special:Search
WHEN 0 results are returned
AND I follow the link to create a new Item
THEN the label field on Special:NewItem is filled with the exact search term I was searching for

Acceptance criteria:

  • The “create a new item” link on the search page always produces the correct label (the one that the user entered).

Event Timeline

While this issue arises from the on-wiki-overridden interface message (and therefore arguably isn’t the responsibility of the Wikidata development team), I think it requires a fix at the software level. Specifically, the Special:Search page wikitext-escapes the search term before sending it to the interface message:

SpecialSearch.php
// If we have no results and have not already displayed an error message
if ( $num === 0 && !$hasSearchErrors ) {
	$out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", [
		$hasOtherResults ? 'search-nonefound-thiswiki' : 'search-nonefound',
		wfEscapeWikiText( $term )
	] );
}

I’m not aware of any way for the interface message to undo this escaping, so that it can put the original search term in the URL. My suggestion is to the unescaped search term as a second parameter to the message: it would be unused by default, but the Wikidata version of the message could use $2 to build the link (while still displaying the original, escaped $1).

Change 643075 had a related patch set uploaded (by Lucas Werkmeister; owner: Lucas Werkmeister):
[mediawiki/core@master] Add unescaped term as argument to search-nonefound messages

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

LucasWerkmeister subscribed.

I tested the above patch locally with the following version of the search-nonefound message (adapted from the Wikidata version):

There were no results matching the query.

{{#switch:{{NAMESPACENUMBER:$1}}
|122=You may [[Special:MyLanguage/Project:Property proposal|propose a new property]].
|146=You may [{{fullurl:Special:NewLexeme|lemma={{urlencode:{{#switch:{{ucfirst:$2}}
 |L:{{PAGENAME:$2}}
 |Lexeme:{{PAGENAME:$1}}={{PAGENAME:$2}}
 |#default={{lcfirst:{{PAGENAME:$2}}}}
}}|PATH}}}} create a new lexeme] for "{{#switch:{{ucfirst:$1}}
 |L:{{PAGENAME:$1}}
 |Lexeme:{{PAGENAME:$1}}={{PAGENAME:$1}}
 |#default={{lcfirst:{{PAGENAME:$1}}}}
}}".</translate>
|#default=You may [{{fullurl:Special:NewItem|label={{urlencode:$2|PATH}}}} create a new item] for "$1".
}}

Seems to work like a charm.

Change 643075 merged by jenkins-bot:
[mediawiki/core@master] Add unescaped term as argument to search-nonefound messages

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

Once wmf.21 has rolled out (sometime next week or the the week after), an interface admin can update MediaWiki:search-nonefound, and then we can move this to Verification. (Maybe I’ll remember to leave an {{edit request}}.)

wmf.21 is out, so I think we can apply it. But I'm afraid it will now be possible to break this with stuff like {{ or }}.

I would wait at least until Friday in case the train rolls back. But you should be able to test the behavior with {{ etc. on Test Wikidata.

Edit: sorry, I misread the user names and thought you’re an interface admin on Test Wikidata.

I edited the Test Wikidata message and it seems to work as expected now, at least for Items. (For Lexemes, the lemma is still escaped, but that seems to be due to the {{PAGENAME:}} magic word, which also wikitext-escapes its argument, and would require a different fix.)

For Lexemes, the lemma is still escaped, but that seems to be due to the {{PAGENAME:}} magic word, which also wikitext-escapes its argument, and would require a different fix.

I will take a look, it should be possible to fix it locally.