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 {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
32 * @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance if not specified
34 mw
.widgets
.TitleWidget
= function MwWidgetsTitleWidget( config
) {
35 // Config initialization
42 this.limit
= config
.limit
;
43 this.maxLength
= config
.maxLength
;
44 this.namespace = config
.namespace !== undefined ? config
.namespace : null;
45 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
46 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
47 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
48 this.showImages
= !!config
.showImages
;
49 this.showDescriptions
= !!config
.showDescriptions
;
50 this.showMissing
= config
.showMissing
!== false;
51 this.addQueryInput
= config
.addQueryInput
!== false;
52 this.excludeCurrentPage
= !!config
.excludeCurrentPage
;
53 this.validateTitle
= config
.validateTitle
!== undefined ? config
.validateTitle
: true;
54 this.cache
= config
.cache
;
55 this.api
= config
.api
|| new mw
.Api();
56 // Supports: IE10, FF28, Chrome23
57 this.compare
= window
.Intl
&& Intl
.Collator
?
58 new Intl
.Collator( mw
.config
.get( 'wgContentLanguage' ), { sensitivity
: 'base' } ).compare
:
62 this.$element
.addClass( 'mw-widget-titleWidget' );
67 OO
.initClass( mw
.widgets
.TitleWidget
);
69 /* Static properties */
71 mw
.widgets
.TitleWidget
.static.interwikiPrefixesPromiseCache
= {};
76 * Get the current value of the search query
79 * @return {string} Search query
81 mw
.widgets
.TitleWidget
.prototype.getQueryValue
= null;
84 * Get the namespace to prepend to titles in suggestions, if any.
86 * @return {number|null} Namespace number
88 mw
.widgets
.TitleWidget
.prototype.getNamespace = function () {
89 return this.namespace;
93 * Set the namespace to prepend to titles in suggestions, if any.
95 * @param {number|null} namespace Namespace number
97 mw
.widgets
.TitleWidget
.prototype.setNamespace = function ( namespace ) {
98 this.namespace = namespace;
101 mw
.widgets
.TitleWidget
.prototype.getInterwikiPrefixesPromise = function () {
102 var api
= this.getApi(),
103 cache
= this.constructor.static.interwikiPrefixesPromiseCache
,
104 key
= api
.defaults
.ajax
.url
;
105 if ( !Object
.prototype.hasOwnProperty
.call( cache
, key
) ) {
106 cache
[ key
] = api
.get( {
109 siprop
: 'interwikimap',
110 // Cache client-side for a day since this info is mostly static
111 maxage
: 60 * 60 * 24,
112 smaxage
: 60 * 60 * 24,
113 // Workaround T97096 by setting uselang=content
115 } ).then( function ( data
) {
116 return data
.query
.interwikimap
.map( function ( interwiki
) {
117 return interwiki
.prefix
;
125 * Get a promise which resolves with an API repsonse for suggested
126 * links for the current query.
128 * @return {jQuery.Promise} Suggestions promise
130 mw
.widgets
.TitleWidget
.prototype.getSuggestionsPromise = function () {
133 query
= this.getQueryValue(),
135 promiseAbortObject
= { abort: function () {
136 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
139 if ( !mw
.Title
.newFromText( query
) ) {
140 // Don't send invalid titles to the API.
141 // Just pretend it returned nothing so we can show the 'invalid title' section
142 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
145 return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes
) {
146 var interwiki
= query
.substring( 0, query
.indexOf( ':' ) );
148 interwiki
&& interwiki
!== '' &&
149 interwikiPrefixes
.indexOf( interwiki
) !== -1
151 return $.Deferred().resolve( { query
: {
155 } } ).promise( promiseAbortObject
);
157 req
= api
.get( widget
.getApiParams( query
) );
158 promiseAbortObject
.abort
= req
.abort
.bind( req
); // TODO ew
159 return req
.then( function ( ret
) {
160 if ( widget
.showMissing
&& ret
.query
=== undefined ) {
161 ret
= api
.get( { action
: 'query', titles
: query
} );
162 promiseAbortObject
.abort
= ret
.abort
.bind( ret
);
167 } ).promise( promiseAbortObject
);
171 * Get API params for a given query
173 * @param {string} query User query
174 * @return {Object} API params
176 mw
.widgets
.TitleWidget
.prototype.getApiParams = function ( query
) {
179 prop
: [ 'info', 'pageprops' ],
180 generator
: 'prefixsearch',
182 gpsnamespace
: this.namespace !== null ? this.namespace : undefined,
183 gpslimit
: this.limit
,
184 ppprop
: 'disambiguation'
186 if ( this.showRedirectTargets
) {
187 params
.redirects
= true;
189 if ( this.showImages
) {
190 params
.prop
.push( 'pageimages' );
191 params
.pithumbsize
= 80;
192 params
.pilimit
= this.limit
;
194 if ( this.showDescriptions
) {
195 params
.prop
.push( 'description' );
201 * Get the API object for title requests
203 * @return {mw.Api} MediaWiki API
205 mw
.widgets
.TitleWidget
.prototype.getApi = function () {
210 * Get option widgets from the server response
212 * @param {Object} data Query result
213 * @return {OO.ui.OptionWidget[]} Menu items
215 mw
.widgets
.TitleWidget
.prototype.getOptionsFromData = function ( data
) {
216 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
217 currentPageName
= new mw
.Title( mw
.config
.get( 'wgRelevantPageName' ) ).getPrefixedText(),
220 titleObj
= mw
.Title
.newFromText( this.getQueryValue() ),
224 if ( data
.redirects
) {
225 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
226 redirect
= data
.redirects
[ i
];
227 redirectsTo
[ redirect
.to
] = redirectsTo
[ redirect
.to
] || [];
228 redirectsTo
[ redirect
.to
].push( redirect
.from );
232 for ( index
in data
.pages
) {
233 suggestionPage
= data
.pages
[ index
];
235 // When excludeCurrentPage is set, don't list the current page unless the user has type the full title
236 if ( this.excludeCurrentPage
&& suggestionPage
.title
=== currentPageName
&& suggestionPage
.title
!== titleObj
.getPrefixedText() ) {
239 pageData
[ suggestionPage
.title
] = {
240 known
: suggestionPage
.known
!== undefined,
241 missing
: suggestionPage
.missing
!== undefined,
242 redirect
: suggestionPage
.redirect
!== undefined,
243 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
244 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
245 description
: suggestionPage
.description
,
247 index
: suggestionPage
.index
,
248 originalData
: suggestionPage
251 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
252 // and we encounter a cross-namespace redirect.
253 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
254 titles
.push( suggestionPage
.title
);
257 redirects
= hasOwn
.call( redirectsTo
, suggestionPage
.title
) ? redirectsTo
[ suggestionPage
.title
] : [];
258 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
259 pageData
[ redirects
[ i
] ] = {
263 disambiguation
: false,
264 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
),
265 // Sort index, just below its target
266 index
: suggestionPage
.index
+ 0.5,
267 originalData
: suggestionPage
269 titles
.push( redirects
[ i
] );
273 titles
.sort( function ( a
, b
) {
274 return pageData
[ a
].index
- pageData
[ b
].index
;
277 // If not found, run value through mw.Title to avoid treating a match as a
278 // mismatch where normalisation would make them matching (T50476)
281 hasOwn
.call( pageData
, this.getQueryValue() ) &&
283 !pageData
[ this.getQueryValue() ].missing
||
284 pageData
[ this.getQueryValue() ].known
287 pageExists
= pageExistsExact
|| (
289 hasOwn
.call( pageData
, titleObj
.getPrefixedText() ) &&
291 !pageData
[ titleObj
.getPrefixedText() ].missing
||
292 pageData
[ titleObj
.getPrefixedText() ].known
297 this.cache
.set( pageData
);
300 // Offer the exact text as a suggestion if the page exists
301 if ( this.addQueryInput
&& pageExists
&& !pageExistsExact
) {
302 titles
.unshift( this.getQueryValue() );
305 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
306 page
= hasOwn
.call( pageData
, titles
[ i
] ) ? pageData
[ titles
[ i
] ] : {};
307 items
.push( this.createOptionWidget( this.getOptionWidgetData( titles
[ i
], page
) ) );
314 * Create a menu option widget with specified data
316 * @param {Object} data Data for option widget
317 * @return {OO.ui.MenuOptionWidget} Data for option widget
319 mw
.widgets
.TitleWidget
.prototype.createOptionWidget = function ( data
) {
320 return new mw
.widgets
.TitleOptionWidget( data
);
324 * Get menu option widget data from the title and page data
326 * @param {string} title Title object
327 * @param {Object} data Page data
328 * @return {Object} Data for option widget
330 mw
.widgets
.TitleWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
331 var mwTitle
= new mw
.Title( title
),
332 description
= data
.description
;
333 if ( data
.missing
&& !description
) {
334 description
= mw
.msg( 'mw-widgets-titleinput-description-new-page' );
337 data
: this.namespace !== null && this.relative
?
338 mwTitle
.getRelativeText( this.namespace ) :
340 url
: mwTitle
.getUrl(),
341 showImages
: this.showImages
,
342 imageUrl
: this.showImages
? data
.imageUrl
: null,
343 description
: this.showDescriptions
? description
: null,
344 missing
: data
.missing
,
345 redirect
: data
.redirect
,
346 disambiguation
: data
.disambiguation
,
347 query
: this.getQueryValue(),
348 compare
: this.compare
353 * Get title object corresponding to given value, or #getQueryValue if not given.
355 * @param {string} [value] Value to get a title for
356 * @return {mw.Title|null} Title object, or null if value is invalid
358 mw
.widgets
.TitleWidget
.prototype.getMWTitle = function ( value
) {
359 var title
= value
!== undefined ? value
: this.getQueryValue(),
360 // mw.Title doesn't handle null well
361 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
367 * Check if the query is valid
369 * @return {boolean} The query is valid
371 mw
.widgets
.TitleWidget
.prototype.isQueryValid = function () {
372 if ( !this.validateTitle
) {
375 if ( !this.required
&& this.getQueryValue() === '' ) {
378 return !!this.getMWTitle();