2 * MediaWiki Widgets - TitleWidget class.
4 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
8 var hasOwn
= Object
.prototype.hasOwnProperty
;
11 * Mixin for title widgets
17 * @param {Object} [config] Configuration options
18 * @cfg {number} [limit=10] Number of results to show
19 * @cfg {number} [namespace] Namespace to prepend to queries
20 * @cfg {number} [maxLength=255] Maximum query length
21 * @cfg {boolean} [relative=true] If a namespace is set, display titles relative to it
22 * @cfg {boolean} [suggestions=true] Display search suggestions
23 * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
24 * @cfg {boolean} [showImages] Show page images
25 * @cfg {boolean} [showDescriptions] Show page descriptions
26 * @cfg {boolean} [showMissing=true] Show missing pages
27 * @cfg {boolean} [addQueryInput=true] Add exact user's input query to results
28 * @cfg {boolean} [excludeCurrentPage] Exclude the current page from suggestions
29 * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title
30 * @cfg {boolean} [required=false] Whether the input must not be empty
31 * @cfg {boolean} [highlightSearchQuery=true] Highlight the partial query the user used for this title
32 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
33 * @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance if not specified
35 mw
.widgets
.TitleWidget
= function MwWidgetsTitleWidget( config
) {
36 // Config initialization
43 this.limit
= config
.limit
;
44 this.maxLength
= config
.maxLength
;
45 this.namespace = config
.namespace !== undefined ? config
.namespace : null;
46 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
47 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
48 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
49 this.showImages
= !!config
.showImages
;
50 this.showDescriptions
= !!config
.showDescriptions
;
51 this.showMissing
= config
.showMissing
!== false;
52 this.addQueryInput
= config
.addQueryInput
!== false;
53 this.excludeCurrentPage
= !!config
.excludeCurrentPage
;
54 this.validateTitle
= config
.validateTitle
!== undefined ? config
.validateTitle
: true;
55 this.highlightSearchQuery
= config
.highlightSearchQuery
=== undefined ? true : !!config
.highlightSearchQuery
;
56 this.cache
= config
.cache
;
57 this.api
= config
.api
|| new mw
.Api();
58 // Supports: IE10, FF28, Chrome23
59 this.compare
= window
.Intl
&& Intl
.Collator
?
60 new Intl
.Collator( mw
.config
.get( 'wgContentLanguage' ), { sensitivity
: 'base' } ).compare
:
64 this.$element
.addClass( 'mw-widget-titleWidget' );
69 OO
.initClass( mw
.widgets
.TitleWidget
);
71 /* Static properties */
73 mw
.widgets
.TitleWidget
.static.interwikiPrefixesPromiseCache
= {};
78 * Get the current value of the search query
81 * @return {string} Search query
83 mw
.widgets
.TitleWidget
.prototype.getQueryValue
= null;
86 * Get the namespace to prepend to titles in suggestions, if any.
88 * @return {number|null} Namespace number
90 mw
.widgets
.TitleWidget
.prototype.getNamespace = function () {
91 return this.namespace;
95 * Set the namespace to prepend to titles in suggestions, if any.
97 * @param {number|null} namespace Namespace number
99 mw
.widgets
.TitleWidget
.prototype.setNamespace = function ( namespace ) {
100 this.namespace = namespace;
103 mw
.widgets
.TitleWidget
.prototype.getInterwikiPrefixesPromise = function () {
104 var api
= this.getApi(),
105 cache
= this.constructor.static.interwikiPrefixesPromiseCache
,
106 key
= api
.defaults
.ajax
.url
;
107 if ( !Object
.prototype.hasOwnProperty
.call( cache
, key
) ) {
108 cache
[ key
] = api
.get( {
111 siprop
: 'interwikimap',
112 // Cache client-side for a day since this info is mostly static
113 maxage
: 60 * 60 * 24,
114 smaxage
: 60 * 60 * 24,
115 // Workaround T97096 by setting uselang=content
117 } ).then( function ( data
) {
118 return data
.query
.interwikimap
.map( function ( interwiki
) {
119 return interwiki
.prefix
;
127 * Get a promise which resolves with an API repsonse for suggested
128 * links for the current query.
130 * @return {jQuery.Promise} Suggestions promise
132 mw
.widgets
.TitleWidget
.prototype.getSuggestionsPromise = function () {
135 query
= this.getQueryValue(),
137 promiseAbortObject
= { abort: function () {
138 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
141 if ( !mw
.Title
.newFromText( query
) ) {
142 // Don't send invalid titles to the API.
143 // Just pretend it returned nothing so we can show the 'invalid title' section
144 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
147 return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes
) {
148 var interwiki
= query
.substring( 0, query
.indexOf( ':' ) );
150 interwiki
&& interwiki
!== '' &&
151 interwikiPrefixes
.indexOf( interwiki
) !== -1
153 return $.Deferred().resolve( { query
: {
157 } } ).promise( promiseAbortObject
);
159 req
= api
.get( widget
.getApiParams( query
) );
160 promiseAbortObject
.abort
= req
.abort
.bind( req
); // TODO ew
161 return req
.then( function ( ret
) {
162 if ( widget
.showMissing
&& ret
.query
=== undefined ) {
163 ret
= api
.get( { action
: 'query', titles
: query
} );
164 promiseAbortObject
.abort
= ret
.abort
.bind( ret
);
169 } ).promise( promiseAbortObject
);
173 * Get API params for a given query
175 * @param {string} query User query
176 * @return {Object} API params
178 mw
.widgets
.TitleWidget
.prototype.getApiParams = function ( query
) {
181 prop
: [ 'info', 'pageprops' ],
182 generator
: 'prefixsearch',
184 gpsnamespace
: this.namespace !== null ? this.namespace : undefined,
185 gpslimit
: this.limit
,
186 ppprop
: 'disambiguation'
188 if ( this.showRedirectTargets
) {
189 params
.redirects
= true;
191 if ( this.showImages
) {
192 params
.prop
.push( 'pageimages' );
193 params
.pithumbsize
= 80;
194 params
.pilimit
= this.limit
;
196 if ( this.showDescriptions
) {
197 params
.prop
.push( 'description' );
203 * Get the API object for title requests
205 * @return {mw.Api} MediaWiki API
207 mw
.widgets
.TitleWidget
.prototype.getApi = function () {
212 * Get option widgets from the server response
214 * @param {Object} data Query result
215 * @return {OO.ui.OptionWidget[]} Menu items
217 mw
.widgets
.TitleWidget
.prototype.getOptionsFromData = function ( data
) {
218 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
219 currentPageName
= new mw
.Title( mw
.config
.get( 'wgRelevantPageName' ) ).getPrefixedText(),
222 titleObj
= mw
.Title
.newFromText( this.getQueryValue() ),
226 if ( data
.redirects
) {
227 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
228 redirect
= data
.redirects
[ i
];
229 redirectsTo
[ redirect
.to
] = redirectsTo
[ redirect
.to
] || [];
230 redirectsTo
[ redirect
.to
].push( redirect
.from );
234 for ( index
in data
.pages
) {
235 suggestionPage
= data
.pages
[ index
];
237 // When excludeCurrentPage is set, don't list the current page unless the user has type the full title
238 if ( this.excludeCurrentPage
&& suggestionPage
.title
=== currentPageName
&& suggestionPage
.title
!== titleObj
.getPrefixedText() ) {
241 pageData
[ suggestionPage
.title
] = {
242 known
: suggestionPage
.known
!== undefined,
243 missing
: suggestionPage
.missing
!== undefined,
244 redirect
: suggestionPage
.redirect
!== undefined,
245 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
246 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
247 description
: suggestionPage
.description
,
249 index
: suggestionPage
.index
,
250 originalData
: suggestionPage
253 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
254 // and we encounter a cross-namespace redirect.
255 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
256 titles
.push( suggestionPage
.title
);
259 redirects
= hasOwn
.call( redirectsTo
, suggestionPage
.title
) ? redirectsTo
[ suggestionPage
.title
] : [];
260 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
261 pageData
[ redirects
[ i
] ] = {
265 disambiguation
: false,
266 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
),
267 // Sort index, just below its target
268 index
: suggestionPage
.index
+ 0.5,
269 originalData
: suggestionPage
271 titles
.push( redirects
[ i
] );
275 titles
.sort( function ( a
, b
) {
276 return pageData
[ a
].index
- pageData
[ b
].index
;
279 // If not found, run value through mw.Title to avoid treating a match as a
280 // mismatch where normalisation would make them matching (T50476)
283 hasOwn
.call( pageData
, this.getQueryValue() ) &&
285 !pageData
[ this.getQueryValue() ].missing
||
286 pageData
[ this.getQueryValue() ].known
289 pageExists
= pageExistsExact
|| (
291 hasOwn
.call( pageData
, titleObj
.getPrefixedText() ) &&
293 !pageData
[ titleObj
.getPrefixedText() ].missing
||
294 pageData
[ titleObj
.getPrefixedText() ].known
299 this.cache
.set( pageData
);
302 // Offer the exact text as a suggestion if the page exists
303 if ( this.addQueryInput
&& pageExists
&& !pageExistsExact
) {
304 titles
.unshift( this.getQueryValue() );
307 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
308 page
= hasOwn
.call( pageData
, titles
[ i
] ) ? pageData
[ titles
[ i
] ] : {};
309 items
.push( this.createOptionWidget( this.getOptionWidgetData( titles
[ i
], page
) ) );
316 * Create a menu option widget with specified data
318 * @param {Object} data Data for option widget
319 * @return {OO.ui.MenuOptionWidget} Data for option widget
321 mw
.widgets
.TitleWidget
.prototype.createOptionWidget = function ( data
) {
322 return new mw
.widgets
.TitleOptionWidget( data
);
326 * Get menu option widget data from the title and page data
328 * @param {string} title Title object
329 * @param {Object} data Page data
330 * @return {Object} Data for option widget
332 mw
.widgets
.TitleWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
333 var mwTitle
= new mw
.Title( title
),
334 description
= data
.description
;
335 if ( data
.missing
&& !description
) {
336 description
= mw
.msg( 'mw-widgets-titleinput-description-new-page' );
339 data
: this.namespace !== null && this.relative
?
340 mwTitle
.getRelativeText( this.namespace ) :
342 url
: mwTitle
.getUrl(),
343 showImages
: this.showImages
,
344 imageUrl
: this.showImages
? data
.imageUrl
: null,
345 description
: this.showDescriptions
? description
: null,
346 missing
: data
.missing
,
347 redirect
: data
.redirect
,
348 disambiguation
: data
.disambiguation
,
349 query
: this.highlightSearchQuery
? this.getQueryValue() : null,
350 compare
: this.compare
355 * Get title object corresponding to given value, or #getQueryValue if not given.
357 * @param {string} [value] Value to get a title for
358 * @return {mw.Title|null} Title object, or null if value is invalid
360 mw
.widgets
.TitleWidget
.prototype.getMWTitle = function ( value
) {
361 var title
= value
!== undefined ? value
: this.getQueryValue(),
362 // mw.Title doesn't handle null well
363 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
369 * Check if the query is valid
371 * @return {boolean} The query is valid
373 mw
.widgets
.TitleWidget
.prototype.isQueryValid = function () {
374 if ( !this.validateTitle
) {
377 if ( !this.required
&& this.getQueryValue() === '' ) {
380 return !!this.getMWTitle();