Paste P1923

WikibaseSearchApi
ActivePublic

Authored by Bene on Aug 25 2015, 11:31 AM.
1/**
2 * API for search
3 * @extends Api
4 * @class SearchApi
5 */
6( function ( M, $ ) {
7
8var WikibaseSearchApi,
9Page = M.require( 'Page' ),
10SearchApi = M.require( 'modules/search/SearchApi' );
11
12/**
13 * @class WikibaseSearchApi
14 * @extends SearchApi
15 */
16WikibaseSearchApi = SearchApi.extend( {
17
18/**
19 * Get the data used to do the search query api call.
20 * @method
21 * @param {String} query to search for
22 * @return {Object}
23 */
24getApiData: function ( query ) {
25return {
26action: 'wbsearchentities',
27language: mw.config.get( 'wgUserLanguage' ), //FIXME
28search: query,
29format: 'json',
30type: 'item' //FIXME
31};
32},
33
34/**
35 * Process the data returned by the api call.
36 * @param {String} query to search for
37 * @param {Object} data from api
38 * @return {Array}
39 * @private
40 */
41_processData: function( query, data ) {
42var self = this,
43results = [];
44
45if ( data.search ) {
46$.each( data.search, function ( i, entity ) {
47// data used for creating {Page} objects
48results.push( {
49id: entity.pageid, //FIXME
50title: entity.title, //FIXME
51displayTitle: self._highlightSearchTerm( entity.label, query ),
52url: entity.url,
53thumbnail: entity.thumbnail //FIXME
54} );
55} );
56}
57
58return results;
59}
60} );
61
62M.define( 'modules/search/WikibaseSearchApi', WikibaseSearchApi );
63
64}( mw.mobileFrontend, jQuery ) );
Bene edited the content of this paste. (Show Details)Aug 25 2015, 11:31 AM
Bene changed the title of this paste from untitled to WikibaseSearchApi.
Bene updated the paste's language from autodetect to js.

@Bene

displayTitle could / should be set to the matched term instead of always the label?
Otherwise sometimes results could be confusing.

Would be nice to actually be able to show more than 1 thing here, but meh...

Bene edited the content of this paste. (Show Details)Aug 25 2015, 1:08 PM

We could add the matched term in brackets behind the label, but that seems to be even hackier :S

My only reason for saying the above is otherwise you could search for "Foo" but instead be shown "ABCDE" in the list (as a random example)....

Jonas added a subscriber: Jonas.Aug 26 2015, 10:51 AM

Looks good in general prepare a patch?

Bene added a comment.Aug 26 2015, 12:56 PM

I will prepare a patch as soon as T110069 is resolved