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
10 * Mixin for title widgets
16 * @param {Object} [config] Configuration options
17 * @cfg {number} [limit=10] Number of results to show
18 * @cfg {number} [namespace] Namespace to prepend to queries
19 * @cfg {number} [maxLength=255] Maximum query length
20 * @cfg {boolean} [relative=true] If a namespace is set, display titles relative to it
21 * @cfg {boolean} [suggestions=true] Display search suggestions
22 * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
23 * @cfg {boolean} [showImages] Show page images
24 * @cfg {boolean} [showDescriptions] Show page descriptions
25 * @cfg {boolean} [showMissing=true] Show missing pages
26 * @cfg {boolean} [excludeCurrentPage] Exclude the current page from suggestions
27 * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title (if set to true,
28 * the widget will marks itself red for invalid inputs, including an empty query).
29 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
30 * @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance if not specified
32 mw
.widgets
.TitleWidget
= function MwWidgetsTitleWidget( config
) {
33 // Config initialization
40 this.limit
= config
.limit
;
41 this.maxLength
= config
.maxLength
;
42 this.namespace = config
.namespace !== undefined ? config
.namespace : null;
43 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
44 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
45 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
46 this.showImages
= !!config
.showImages
;
47 this.showDescriptions
= !!config
.showDescriptions
;
48 this.showMissing
= config
.showMissing
!== false;
49 this.excludeCurrentPage
= !!config
.excludeCurrentPage
;
50 this.validateTitle
= config
.validateTitle
!== undefined ? config
.validateTitle
: true;
51 this.cache
= config
.cache
;
52 this.api
= config
.api
|| new mw
.Api();
53 // Supports: IE10, FF28, Chrome23
54 this.compare
= window
.Intl
&& Intl
.Collator
?
55 new Intl
.Collator( mw
.config
.get( 'wgContentLanguage' ), { sensitivity
: 'base' } ).compare
:
59 this.$element
.addClass( 'mw-widget-titleWidget' );
64 OO
.initClass( mw
.widgets
.TitleWidget
);
66 /* Static properties */
68 mw
.widgets
.TitleWidget
.static.interwikiPrefixesPromiseCache
= {};
73 * Get the current value of the search query
76 * @return {string} Search query
78 mw
.widgets
.TitleWidget
.prototype.getQueryValue
= null;
81 * Get the namespace to prepend to titles in suggestions, if any.
83 * @return {number|null} Namespace number
85 mw
.widgets
.TitleWidget
.prototype.getNamespace = function () {
86 return this.namespace;
90 * Set the namespace to prepend to titles in suggestions, if any.
92 * @param {number|null} namespace Namespace number
94 mw
.widgets
.TitleWidget
.prototype.setNamespace = function ( namespace ) {
95 this.namespace = namespace;
98 mw
.widgets
.TitleWidget
.prototype.getInterwikiPrefixesPromise = function () {
99 var api
= this.getApi(),
100 cache
= this.constructor.static.interwikiPrefixesPromiseCache
,
101 key
= api
.defaults
.ajax
.url
;
102 if ( !cache
.hasOwnProperty( key
) ) {
103 cache
[ key
] = api
.get( {
106 siprop
: 'interwikimap',
107 // Cache client-side for a day since this info is mostly static
108 maxage
: 60 * 60 * 24,
109 smaxage
: 60 * 60 * 24,
110 // Workaround T97096 by setting uselang=content
112 } ).then( function ( data
) {
113 return $.map( data
.query
.interwikimap
, function ( interwiki
) {
114 return interwiki
.prefix
;
122 * Get a promise which resolves with an API repsonse for suggested
123 * links for the current query.
125 * @return {jQuery.Promise} Suggestions promise
127 mw
.widgets
.TitleWidget
.prototype.getSuggestionsPromise = function () {
130 query
= this.getQueryValue(),
132 promiseAbortObject
= { abort: function () {
133 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
136 if ( !mw
.Title
.newFromText( query
) ) {
137 // Don't send invalid titles to the API.
138 // Just pretend it returned nothing so we can show the 'invalid title' section
139 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
142 return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes
) {
143 var interwiki
= query
.substring( 0, query
.indexOf( ':' ) );
145 interwiki
&& interwiki
!== '' &&
146 interwikiPrefixes
.indexOf( interwiki
) !== -1
148 return $.Deferred().resolve( { query
: {
152 } } ).promise( promiseAbortObject
);
154 req
= api
.get( widget
.getApiParams( query
) );
155 promiseAbortObject
.abort
= req
.abort
.bind( req
); // TODO ew
156 return req
.then( function ( ret
) {
157 if ( widget
.showMissing
&& ret
.query
=== undefined ) {
158 ret
= api
.get( { action
: 'query', titles
: query
} );
159 promiseAbortObject
.abort
= ret
.abort
.bind( ret
);
164 } ).promise( promiseAbortObject
);
168 * Get API params for a given query
170 * @param {string} query User query
171 * @return {Object} API params
173 mw
.widgets
.TitleWidget
.prototype.getApiParams = function ( query
) {
176 prop
: [ 'info', 'pageprops' ],
177 generator
: 'prefixsearch',
179 gpsnamespace
: this.namespace !== null ? this.namespace : undefined,
180 gpslimit
: this.limit
,
181 ppprop
: 'disambiguation'
183 if ( this.showRedirectTargets
) {
184 params
.redirects
= true;
186 if ( this.showImages
) {
187 params
.prop
.push( 'pageimages' );
188 params
.pithumbsize
= 80;
189 params
.pilimit
= this.limit
;
191 if ( this.showDescriptions
) {
192 params
.prop
.push( 'pageterms' );
193 params
.wbptterms
= 'description';
199 * Get the API object for title requests
201 * @return {mw.Api} MediaWiki API
203 mw
.widgets
.TitleWidget
.prototype.getApi = function () {
208 * Get option widgets from the server response
210 * @param {Object} data Query result
211 * @return {OO.ui.OptionWidget[]} Menu items
213 mw
.widgets
.TitleWidget
.prototype.getOptionsFromData = function ( data
) {
214 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
215 currentPageName
= new mw
.Title( mw
.config
.get( 'wgRelevantPageName' ) ).getPrefixedText(),
218 titleObj
= mw
.Title
.newFromText( this.getQueryValue() ),
222 if ( data
.redirects
) {
223 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
224 redirect
= data
.redirects
[ i
];
225 redirectsTo
[ redirect
.to
] = redirectsTo
[ redirect
.to
] || [];
226 redirectsTo
[ redirect
.to
].push( redirect
.from );
230 for ( index
in data
.pages
) {
231 suggestionPage
= data
.pages
[ index
];
233 // When excludeCurrentPage is set, don't list the current page unless the user has type the full title
234 if ( this.excludeCurrentPage
&& suggestionPage
.title
=== currentPageName
&& suggestionPage
.title
!== titleObj
.getPrefixedText() ) {
237 pageData
[ suggestionPage
.title
] = {
238 known
: suggestionPage
.known
!== undefined,
239 missing
: suggestionPage
.missing
!== undefined,
240 redirect
: suggestionPage
.redirect
!== undefined,
241 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
242 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
243 description
: OO
.getProp( suggestionPage
, 'terms', 'description' ),
245 index
: suggestionPage
.index
,
246 originalData
: suggestionPage
249 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
250 // and we encounter a cross-namespace redirect.
251 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
252 titles
.push( suggestionPage
.title
);
255 redirects
= redirectsTo
[ suggestionPage
.title
] || [];
256 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
257 pageData
[ redirects
[ i
] ] = {
261 disambiguation
: false,
262 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
),
263 // Sort index, just below its target
264 index
: suggestionPage
.index
+ 0.5,
265 originalData
: suggestionPage
267 titles
.push( redirects
[ i
] );
271 titles
.sort( function ( a
, b
) {
272 return pageData
[ a
].index
- pageData
[ b
].index
;
275 // If not found, run value through mw.Title to avoid treating a match as a
276 // mismatch where normalisation would make them matching (T50476)
279 Object
.prototype.hasOwnProperty
.call( pageData
, this.getQueryValue() ) &&
281 !pageData
[ this.getQueryValue() ].missing
||
282 pageData
[ this.getQueryValue() ].known
285 pageExists
= pageExistsExact
|| (
287 Object
.prototype.hasOwnProperty
.call( pageData
, titleObj
.getPrefixedText() ) &&
289 !pageData
[ titleObj
.getPrefixedText() ].missing
||
290 pageData
[ titleObj
.getPrefixedText() ].known
295 this.cache
.set( pageData
);
298 // Offer the exact text as a suggestion if the page exists
299 if ( pageExists
&& !pageExistsExact
) {
300 titles
.unshift( this.getQueryValue() );
303 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
304 page
= pageData
[ titles
[ i
] ] || {};
305 items
.push( this.createOptionWidget( this.getOptionWidgetData( titles
[ i
], page
) ) );
312 * Create a menu option widget with specified data
314 * @param {Object} data Data for option widget
315 * @return {OO.ui.MenuOptionWidget} Data for option widget
317 mw
.widgets
.TitleWidget
.prototype.createOptionWidget = function ( data
) {
318 return new mw
.widgets
.TitleOptionWidget( data
);
322 * Get menu option widget data from the title and page data
324 * @param {string} title Title object
325 * @param {Object} data Page data
326 * @return {Object} Data for option widget
328 mw
.widgets
.TitleWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
329 var mwTitle
= new mw
.Title( title
),
330 description
= data
.description
;
331 if ( data
.missing
&& !description
) {
332 description
= mw
.msg( 'mw-widgets-titleinput-description-new-page' );
335 data
: this.namespace !== null && this.relative
?
336 mwTitle
.getRelativeText( this.namespace ) :
338 url
: mwTitle
.getUrl(),
339 showImages
: this.showImages
,
340 imageUrl
: this.showImages
? data
.imageUrl
: null,
341 description
: this.showDescriptions
? description
: null,
342 missing
: data
.missing
,
343 redirect
: data
.redirect
,
344 disambiguation
: data
.disambiguation
,
345 query
: this.getQueryValue(),
346 compare
: this.compare
351 * Get title object corresponding to given value, or #getQueryValue if not given.
353 * @param {string} [value] Value to get a title for
354 * @return {mw.Title|null} Title object, or null if value is invalid
356 mw
.widgets
.TitleWidget
.prototype.getTitle = function ( value
) {
357 var title
= value
!== undefined ? value
: this.getQueryValue(),
358 // mw.Title doesn't handle null well
359 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
365 * Check if the query is valid
367 * @return {boolean} The query is valid
369 mw
.widgets
.TitleWidget
.prototype.isQueryValid = function () {
370 return this.validateTitle
? !!this.getTitle() : true;
373 }( jQuery
, mediaWiki
) );