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} [excludeDynamicNamespaces] Exclude pages whose namespace is negative
30 * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title
31 * @cfg {boolean} [required=false] Whether the input must not be empty
32 * @cfg {boolean} [highlightSearchQuery=true] Highlight the partial query the user used for this title
33 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
34 * @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance if not specified
36 mw
.widgets
.TitleWidget
= function MwWidgetsTitleWidget( config
) {
37 // Config initialization
44 this.limit
= config
.limit
;
45 this.maxLength
= config
.maxLength
;
46 this.namespace = config
.namespace !== undefined ? config
.namespace : null;
47 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
48 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
49 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
50 this.showImages
= !!config
.showImages
;
51 this.showDescriptions
= !!config
.showDescriptions
;
52 this.showMissing
= config
.showMissing
!== false;
53 this.addQueryInput
= config
.addQueryInput
!== false;
54 this.excludeCurrentPage
= !!config
.excludeCurrentPage
;
55 this.excludeDynamicNamespaces
= !!config
.excludeDynamicNamespaces
;
56 this.validateTitle
= config
.validateTitle
!== undefined ? config
.validateTitle
: true;
57 this.highlightSearchQuery
= config
.highlightSearchQuery
=== undefined ? true : !!config
.highlightSearchQuery
;
58 this.cache
= config
.cache
;
59 this.api
= config
.api
|| new mw
.Api();
60 // Supports: IE10, FF28, Chrome23
61 this.compare
= window
.Intl
&& Intl
.Collator
?
62 new Intl
.Collator( mw
.config
.get( 'wgContentLanguage' ), { sensitivity
: 'base' } ).compare
:
66 this.$element
.addClass( 'mw-widget-titleWidget' );
71 OO
.initClass( mw
.widgets
.TitleWidget
);
73 /* Static properties */
75 mw
.widgets
.TitleWidget
.static.interwikiPrefixesPromiseCache
= {};
80 * Get the current value of the search query
83 * @return {string} Search query
85 mw
.widgets
.TitleWidget
.prototype.getQueryValue
= null;
88 * Get the namespace to prepend to titles in suggestions, if any.
90 * @return {number|null} Namespace number
92 mw
.widgets
.TitleWidget
.prototype.getNamespace = function () {
93 return this.namespace;
97 * Set the namespace to prepend to titles in suggestions, if any.
99 * @param {number|null} namespace Namespace number
101 mw
.widgets
.TitleWidget
.prototype.setNamespace = function ( namespace ) {
102 this.namespace = namespace;
105 mw
.widgets
.TitleWidget
.prototype.getInterwikiPrefixesPromise = function () {
106 var api
= this.getApi(),
107 cache
= this.constructor.static.interwikiPrefixesPromiseCache
,
108 key
= api
.defaults
.ajax
.url
;
109 if ( !Object
.prototype.hasOwnProperty
.call( cache
, key
) ) {
110 cache
[ key
] = api
.get( {
113 siprop
: 'interwikimap',
114 // Cache client-side for a day since this info is mostly static
115 maxage
: 60 * 60 * 24,
116 smaxage
: 60 * 60 * 24,
117 // Workaround T97096 by setting uselang=content
119 } ).then( function ( data
) {
120 return data
.query
.interwikimap
.map( function ( interwiki
) {
121 return interwiki
.prefix
;
129 * Get a promise which resolves with an API repsonse for suggested
130 * links for the current query.
132 * @return {jQuery.Promise} Suggestions promise
134 mw
.widgets
.TitleWidget
.prototype.getSuggestionsPromise = function () {
137 query
= this.getQueryValue(),
139 promiseAbortObject
= { abort: function () {
140 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
143 if ( !mw
.Title
.newFromText( query
) ) {
144 // Don't send invalid titles to the API.
145 // Just pretend it returned nothing so we can show the 'invalid title' section
146 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
149 return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes
) {
150 var interwiki
= query
.substring( 0, query
.indexOf( ':' ) );
152 interwiki
&& interwiki
!== '' &&
153 interwikiPrefixes
.indexOf( interwiki
) !== -1
155 return $.Deferred().resolve( { query
: {
159 } } ).promise( promiseAbortObject
);
161 req
= api
.get( widget
.getApiParams( query
) );
162 promiseAbortObject
.abort
= req
.abort
.bind( req
); // TODO ew
163 return req
.then( function ( ret
) {
164 if ( widget
.showMissing
&& ret
.query
=== undefined ) {
165 ret
= api
.get( { action
: 'query', titles
: query
} );
166 promiseAbortObject
.abort
= ret
.abort
.bind( ret
);
171 } ).promise( promiseAbortObject
);
175 * Get API params for a given query
177 * @param {string} query User query
178 * @return {Object} API params
180 mw
.widgets
.TitleWidget
.prototype.getApiParams = function ( query
) {
183 prop
: [ 'info', 'pageprops' ],
184 generator
: 'prefixsearch',
186 gpsnamespace
: this.namespace !== null ? this.namespace : undefined,
187 gpslimit
: this.limit
,
188 ppprop
: 'disambiguation'
190 if ( this.showRedirectTargets
) {
191 params
.redirects
= true;
193 if ( this.showImages
) {
194 params
.prop
.push( 'pageimages' );
195 params
.pithumbsize
= 80;
196 params
.pilimit
= this.limit
;
198 if ( this.showDescriptions
) {
199 params
.prop
.push( 'description' );
205 * Get the API object for title requests
207 * @return {mw.Api} MediaWiki API
209 mw
.widgets
.TitleWidget
.prototype.getApi = function () {
214 * Get option widgets from the server response
216 * @param {Object} data Query result
217 * @return {OO.ui.OptionWidget[]} Menu items
219 mw
.widgets
.TitleWidget
.prototype.getOptionsFromData = function ( data
) {
220 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
221 currentPageName
= new mw
.Title( mw
.config
.get( 'wgRelevantPageName' ) ).getPrefixedText(),
224 titleObj
= mw
.Title
.newFromText( this.getQueryValue() ),
228 if ( data
.redirects
) {
229 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
230 redirect
= data
.redirects
[ i
];
231 redirectsTo
[ redirect
.to
] = redirectsTo
[ redirect
.to
] || [];
232 redirectsTo
[ redirect
.to
].push( redirect
.from );
236 for ( index
in data
.pages
) {
237 suggestionPage
= data
.pages
[ index
];
239 // When excludeCurrentPage is set, don't list the current page unless the user has type the full title
240 if ( this.excludeCurrentPage
&& suggestionPage
.title
=== currentPageName
&& suggestionPage
.title
!== titleObj
.getPrefixedText() ) {
244 // When excludeDynamicNamespaces is set, ignore all pages with negative namespace
245 if ( this.excludeDynamicNamespaces
&& suggestionPage
.ns
< 0 ) {
248 pageData
[ suggestionPage
.title
] = {
249 known
: suggestionPage
.known
!== undefined,
250 missing
: suggestionPage
.missing
!== undefined,
251 redirect
: suggestionPage
.redirect
!== undefined,
252 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
253 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
254 description
: suggestionPage
.description
,
256 index
: suggestionPage
.index
,
257 originalData
: suggestionPage
260 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
261 // and we encounter a cross-namespace redirect.
262 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
263 titles
.push( suggestionPage
.title
);
266 redirects
= hasOwn
.call( redirectsTo
, suggestionPage
.title
) ? redirectsTo
[ suggestionPage
.title
] : [];
267 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
268 pageData
[ redirects
[ i
] ] = {
272 disambiguation
: false,
273 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
),
274 // Sort index, just below its target
275 index
: suggestionPage
.index
+ 0.5,
276 originalData
: suggestionPage
278 titles
.push( redirects
[ i
] );
282 titles
.sort( function ( a
, b
) {
283 return pageData
[ a
].index
- pageData
[ b
].index
;
286 // If not found, run value through mw.Title to avoid treating a match as a
287 // mismatch where normalisation would make them matching (T50476)
290 hasOwn
.call( pageData
, this.getQueryValue() ) &&
292 !pageData
[ this.getQueryValue() ].missing
||
293 pageData
[ this.getQueryValue() ].known
296 pageExists
= pageExistsExact
|| (
298 hasOwn
.call( pageData
, titleObj
.getPrefixedText() ) &&
300 !pageData
[ titleObj
.getPrefixedText() ].missing
||
301 pageData
[ titleObj
.getPrefixedText() ].known
306 this.cache
.set( pageData
);
309 // Offer the exact text as a suggestion if the page exists
310 if ( this.addQueryInput
&& pageExists
&& !pageExistsExact
) {
311 titles
.unshift( this.getQueryValue() );
314 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
315 page
= hasOwn
.call( pageData
, titles
[ i
] ) ? pageData
[ titles
[ i
] ] : {};
316 items
.push( this.createOptionWidget( this.getOptionWidgetData( titles
[ i
], page
) ) );
323 * Create a menu option widget with specified data
325 * @param {Object} data Data for option widget
326 * @return {OO.ui.MenuOptionWidget} Data for option widget
328 mw
.widgets
.TitleWidget
.prototype.createOptionWidget = function ( data
) {
329 return new mw
.widgets
.TitleOptionWidget( data
);
333 * Get menu option widget data from the title and page data
335 * @param {string} title Title object
336 * @param {Object} data Page data
337 * @return {Object} Data for option widget
339 mw
.widgets
.TitleWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
340 var mwTitle
= new mw
.Title( title
),
341 description
= data
.description
;
342 if ( data
.missing
&& !description
) {
343 description
= mw
.msg( 'mw-widgets-titleinput-description-new-page' );
346 data
: this.namespace !== null && this.relative
?
347 mwTitle
.getRelativeText( this.namespace ) :
349 url
: mwTitle
.getUrl(),
350 showImages
: this.showImages
,
351 imageUrl
: this.showImages
? data
.imageUrl
: null,
352 description
: this.showDescriptions
? description
: null,
353 missing
: data
.missing
,
354 redirect
: data
.redirect
,
355 disambiguation
: data
.disambiguation
,
356 query
: this.highlightSearchQuery
? this.getQueryValue() : null,
357 compare
: this.compare
362 * Get title object corresponding to given value, or #getQueryValue if not given.
364 * @param {string} [value] Value to get a title for
365 * @return {mw.Title|null} Title object, or null if value is invalid
367 mw
.widgets
.TitleWidget
.prototype.getMWTitle = function ( value
) {
368 var title
= value
!== undefined ? value
: this.getQueryValue(),
369 // mw.Title doesn't handle null well
370 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
376 * Check if the query is valid
378 * @return {boolean} The query is valid
380 mw
.widgets
.TitleWidget
.prototype.isQueryValid = function () {
381 if ( !this.validateTitle
) {
384 if ( !this.required
&& this.getQueryValue() === '' ) {
387 return !!this.getMWTitle();