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} [excludeCurrentPage] Exclude the current page from suggestions
26 * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title (if set to true,
27 * the widget will marks itself red for invalid inputs, including an empty query).
28 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
29 * @cfg {mw.Api} [api] API object to use, creates a default mw.Api instance if not specified
31 mw
.widgets
.TitleWidget
= function MwWidgetsTitleWidget( config
) {
32 // Config initialization
39 this.limit
= config
.limit
;
40 this.maxLength
= config
.maxLength
;
41 this.namespace = config
.namespace !== undefined ? config
.namespace : null;
42 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
43 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
44 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
45 this.showImages
= !!config
.showImages
;
46 this.showDescriptions
= !!config
.showDescriptions
;
47 this.excludeCurrentPage
= !!config
.excludeCurrentPage
;
48 this.validateTitle
= config
.validateTitle
!== undefined ? config
.validateTitle
: true;
49 this.cache
= config
.cache
;
50 this.api
= config
.api
|| new mw
.Api();
51 // Supports: IE10, FF28, Chrome23
52 this.compare
= window
.Intl
&& Intl
.Collator
?
53 new Intl
.Collator( mw
.config
.get( 'wgContentLanguage' ), { sensitivity
: 'base' } ).compare
:
57 this.$element
.addClass( 'mw-widget-titleWidget' );
62 OO
.initClass( mw
.widgets
.TitleWidget
);
64 /* Static properties */
66 mw
.widgets
.TitleWidget
.static.interwikiPrefixesPromiseCache
= {};
71 * Get the current value of the search query
74 * @return {string} Search query
76 mw
.widgets
.TitleWidget
.prototype.getQueryValue
= null;
79 * Get the namespace to prepend to titles in suggestions, if any.
81 * @return {number|null} Namespace number
83 mw
.widgets
.TitleWidget
.prototype.getNamespace = function () {
84 return this.namespace;
88 * Set the namespace to prepend to titles in suggestions, if any.
90 * @param {number|null} namespace Namespace number
92 mw
.widgets
.TitleWidget
.prototype.setNamespace = function ( namespace ) {
93 this.namespace = namespace;
96 mw
.widgets
.TitleWidget
.prototype.getInterwikiPrefixesPromise = function () {
97 var api
= this.getApi(),
98 cache
= this.constructor.static.interwikiPrefixesPromiseCache
,
99 key
= api
.defaults
.ajax
.url
;
100 if ( !cache
.hasOwnProperty( key
) ) {
101 cache
[ key
] = api
.get( {
104 siprop
: 'interwikimap',
105 // Cache client-side for a day since this info is mostly static
106 maxage
: 60 * 60 * 24,
107 smaxage
: 60 * 60 * 24,
108 // Workaround T97096 by setting uselang=content
110 } ).then( function ( data
) {
111 return $.map( data
.query
.interwikimap
, function ( interwiki
) {
112 return interwiki
.prefix
;
120 * Get a promise which resolves with an API repsonse for suggested
121 * links for the current query.
123 * @return {jQuery.Promise} Suggestions promise
125 mw
.widgets
.TitleWidget
.prototype.getSuggestionsPromise = function () {
128 query
= this.getQueryValue(),
130 promiseAbortObject
= { abort: function () {
131 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
134 if ( mw
.Title
.newFromText( query
) ) {
135 return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes
) {
136 var interwiki
= query
.substring( 0, query
.indexOf( ':' ) );
138 interwiki
&& interwiki
!== '' &&
139 interwikiPrefixes
.indexOf( interwiki
) !== -1
141 return $.Deferred().resolve( { query
: {
145 } } ).promise( promiseAbortObject
);
147 req
= api
.get( widget
.getApiParams( query
) );
148 promiseAbortObject
.abort
= req
.abort
.bind( req
); // TODO ew
149 return req
.then( function ( ret
) {
150 if ( ret
.query
=== undefined ) {
151 ret
= api
.get( { action
: 'query', titles
: query
} );
152 promiseAbortObject
.abort
= ret
.abort
.bind( ret
);
157 } ).promise( promiseAbortObject
);
159 // Don't send invalid titles to the API.
160 // Just pretend it returned nothing so we can show the 'invalid title' section
161 return $.Deferred().resolve( {} ).promise( promiseAbortObject
);
166 * Get API params for a given query
168 * @param {string} query User query
169 * @return {Object} API params
171 mw
.widgets
.TitleWidget
.prototype.getApiParams = function ( query
) {
174 prop
: [ 'info', 'pageprops' ],
175 generator
: 'prefixsearch',
177 gpsnamespace
: this.namespace !== null ? this.namespace : undefined,
178 gpslimit
: this.limit
,
179 ppprop
: 'disambiguation'
181 if ( this.showRedirectTargets
) {
182 params
.redirects
= true;
184 if ( this.showImages
) {
185 params
.prop
.push( 'pageimages' );
186 params
.pithumbsize
= 80;
187 params
.pilimit
= this.limit
;
189 if ( this.showDescriptions
) {
190 params
.prop
.push( 'pageterms' );
191 params
.wbptterms
= 'description';
197 * Get the API object for title requests
199 * @return {mw.Api} MediaWiki API
201 mw
.widgets
.TitleWidget
.prototype.getApi = function () {
206 * Get option widgets from the server response
208 * @param {Object} data Query result
209 * @return {OO.ui.OptionWidget[]} Menu items
211 mw
.widgets
.TitleWidget
.prototype.getOptionsFromData = function ( data
) {
212 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
213 currentPageName
= new mw
.Title( mw
.config
.get( 'wgRelevantPageName' ) ).getPrefixedText(),
216 titleObj
= mw
.Title
.newFromText( this.getQueryValue() ),
220 if ( data
.redirects
) {
221 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
222 redirect
= data
.redirects
[ i
];
223 redirectsTo
[ redirect
.to
] = redirectsTo
[ redirect
.to
] || [];
224 redirectsTo
[ redirect
.to
].push( redirect
.from );
228 for ( index
in data
.pages
) {
229 suggestionPage
= data
.pages
[ index
];
230 // When excludeCurrentPage is set, don't list the current page unless the user has type the full title
231 if ( this.excludeCurrentPage
&& suggestionPage
.title
=== currentPageName
&& suggestionPage
.title
!== titleObj
.getPrefixedText() ) {
234 pageData
[ suggestionPage
.title
] = {
235 known
: suggestionPage
.known
!== undefined,
236 missing
: suggestionPage
.missing
!== undefined,
237 redirect
: suggestionPage
.redirect
!== undefined,
238 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
239 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
240 description
: OO
.getProp( suggestionPage
, 'terms', 'description' ),
242 index
: suggestionPage
.index
,
243 originalData
: suggestionPage
246 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
247 // and we encounter a cross-namespace redirect.
248 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
249 titles
.push( suggestionPage
.title
);
252 redirects
= redirectsTo
[ suggestionPage
.title
] || [];
253 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
254 pageData
[ redirects
[ i
] ] = {
258 disambiguation
: false,
259 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
),
260 // Sort index, just below its target
261 index
: suggestionPage
.index
+ 0.5,
262 originalData
: suggestionPage
264 titles
.push( redirects
[ i
] );
268 titles
.sort( function ( a
, b
) {
269 return pageData
[ a
].index
- pageData
[ b
].index
;
272 // If not found, run value through mw.Title to avoid treating a match as a
273 // mismatch where normalisation would make them matching (T50476)
276 Object
.prototype.hasOwnProperty
.call( pageData
, this.getQueryValue() ) &&
278 !pageData
[ this.getQueryValue() ].missing
||
279 pageData
[ this.getQueryValue() ].known
282 pageExists
= pageExistsExact
|| (
284 Object
.prototype.hasOwnProperty
.call( pageData
, titleObj
.getPrefixedText() ) &&
286 !pageData
[ titleObj
.getPrefixedText() ].missing
||
287 pageData
[ titleObj
.getPrefixedText() ].known
292 this.cache
.set( pageData
);
295 // Offer the exact text as a suggestion if the page exists
296 if ( pageExists
&& !pageExistsExact
) {
297 titles
.unshift( this.getQueryValue() );
300 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
301 page
= pageData
[ titles
[ i
] ] || {};
302 items
.push( this.createOptionWidget( this.getOptionWidgetData( titles
[ i
], page
) ) );
309 * Create a menu option widget with specified data
311 * @param {Object} data Data for option widget
312 * @return {OO.ui.MenuOptionWidget} Data for option widget
314 mw
.widgets
.TitleWidget
.prototype.createOptionWidget = function ( data
) {
315 return new mw
.widgets
.TitleOptionWidget( data
);
319 * Get menu option widget data from the title and page data
321 * @param {string} title Title object
322 * @param {Object} data Page data
323 * @return {Object} Data for option widget
325 mw
.widgets
.TitleWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
326 var mwTitle
= new mw
.Title( title
),
327 description
= data
.description
;
328 if ( data
.missing
&& !description
) {
329 description
= mw
.msg( 'mw-widgets-titleinput-description-new-page' );
332 data
: this.namespace !== null && this.relative
?
333 mwTitle
.getRelativeText( this.namespace ) :
335 url
: mwTitle
.getUrl(),
336 showImages
: this.showImages
,
337 imageUrl
: this.showImages
? data
.imageUrl
: null,
338 description
: this.showDescriptions
? description
: null,
339 missing
: data
.missing
,
340 redirect
: data
.redirect
,
341 disambiguation
: data
.disambiguation
,
342 query
: this.getQueryValue(),
343 compare
: this.compare
348 * Get title object corresponding to given value, or #getQueryValue if not given.
350 * @param {string} [value] Value to get a title for
351 * @return {mw.Title|null} Title object, or null if value is invalid
353 mw
.widgets
.TitleWidget
.prototype.getTitle = function ( value
) {
354 var title
= value
!== undefined ? value
: this.getQueryValue(),
355 // mw.Title doesn't handle null well
356 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
362 * Check if the query is valid
364 * @return {boolean} The query is valid
366 mw
.widgets
.TitleWidget
.prototype.isQueryValid = function () {
367 return this.validateTitle
? !!this.getTitle() : true;
370 }( jQuery
, mediaWiki
) );