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
9 var interwikiPrefixesPromise
= new mw
.Api().get( {
12 siprop
: 'interwikimap'
13 } ).then( function ( data
) {
14 return $.map( data
.query
.interwikimap
, function ( interwiki
) {
15 return interwiki
.prefix
;
20 * Mixin for title widgets
26 * @param {Object} [config] Configuration options
27 * @cfg {number} [limit=10] Number of results to show
28 * @cfg {number} [namespace] Namespace to prepend to queries
29 * @cfg {number} [maxLength=255] Maximum query length
30 * @cfg {boolean} [relative=true] If a namespace is set, display titles relative to it
31 * @cfg {boolean} [suggestions=true] Display search suggestions
32 * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
33 * @cfg {boolean} [showRedlink] Show red link to exact match if it doesn't exist
34 * @cfg {boolean} [showImages] Show page images
35 * @cfg {boolean} [showDescriptions] Show page descriptions
36 * @cfg {boolean} [excludeCurrentPage] Exclude the current page from suggestions
37 * @cfg {boolean} [validateTitle=true] Whether the input must be a valid title (if set to true,
38 * the widget will marks itself red for invalid inputs, including an empty query).
39 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
41 mw
.widgets
.TitleWidget
= function MwWidgetsTitleWidget( config
) {
42 // Config initialization
49 this.limit
= config
.limit
;
50 this.maxLength
= config
.maxLength
;
51 this.namespace = config
.namespace !== undefined ? config
.namespace : null;
52 this.relative
= config
.relative
!== undefined ? config
.relative
: true;
53 this.suggestions
= config
.suggestions
!== undefined ? config
.suggestions
: true;
54 this.showRedirectTargets
= config
.showRedirectTargets
!== false;
55 this.showRedlink
= !!config
.showRedlink
;
56 this.showImages
= !!config
.showImages
;
57 this.showDescriptions
= !!config
.showDescriptions
;
58 this.excludeCurrentPage
= !!config
.excludeCurrentPage
;
59 this.validateTitle
= config
.validateTitle
!== undefined ? config
.validateTitle
: true;
60 this.cache
= config
.cache
;
63 this.$element
.addClass( 'mw-widget-titleWidget' );
68 OO
.initClass( mw
.widgets
.TitleWidget
);
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;
99 * Get a promise which resolves with an API repsonse for suggested
100 * links for the current query.
102 * @return {jQuery.Promise} Suggestions promise
104 mw
.widgets
.TitleWidget
.prototype.getSuggestionsPromise = function () {
106 query
= this.getQueryValue(),
108 promiseAbortObject
= { abort: function () {
109 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
112 if ( mw
.Title
.newFromText( query
) ) {
113 return interwikiPrefixesPromise
.then( function ( interwikiPrefixes
) {
115 interwiki
= query
.substring( 0, query
.indexOf( ':' ) );
117 interwiki
&& interwiki
!== '' &&
118 interwikiPrefixes
.indexOf( interwiki
) !== -1
120 return $.Deferred().resolve( { query
: {
124 } } ).promise( promiseAbortObject
);
128 prop
: [ 'info', 'pageprops' ],
129 generator
: 'prefixsearch',
131 gpsnamespace
: widget
.namespace !== null ? widget
.namespace : undefined,
132 gpslimit
: widget
.limit
,
133 ppprop
: 'disambiguation'
135 if ( widget
.showRedirectTargets
) {
136 params
.redirects
= true;
138 if ( widget
.showImages
) {
139 params
.prop
.push( 'pageimages' );
140 params
.pithumbsize
= 80;
141 params
.pilimit
= widget
.limit
;
143 if ( widget
.showDescriptions
) {
144 params
.prop
.push( 'pageterms' );
145 params
.wbptterms
= 'description';
147 req
= new mw
.Api().get( params
);
148 promiseAbortObject
.abort
= req
.abort
.bind( req
); // TODO ew
149 return req
.then( function ( ret
) {
150 if ( ret
.query
=== undefined ) {
151 ret
= new mw
.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 option widgets from the server response
168 * @param {Object} data Query result
169 * @return {OO.ui.OptionWidget[]} Menu items
171 mw
.widgets
.TitleWidget
.prototype.getOptionsFromData = function ( data
) {
172 var i
, len
, index
, pageExists
, pageExistsExact
, suggestionPage
, page
, redirect
, redirects
,
173 currentPageName
= new mw
.Title( mw
.config
.get( 'wgRelevantPageName' ) ).getPrefixedText(),
176 titleObj
= mw
.Title
.newFromText( this.getQueryValue() ),
180 if ( data
.redirects
) {
181 for ( i
= 0, len
= data
.redirects
.length
; i
< len
; i
++ ) {
182 redirect
= data
.redirects
[ i
];
183 redirectsTo
[ redirect
.to
] = redirectsTo
[ redirect
.to
] || [];
184 redirectsTo
[ redirect
.to
].push( redirect
.from );
188 for ( index
in data
.pages
) {
189 suggestionPage
= data
.pages
[ index
];
190 // When excludeCurrentPage is set, don't list the current page unless the user has type the full title
191 if ( this.excludeCurrentPage
&& suggestionPage
.title
=== currentPageName
&& suggestionPage
.title
!== titleObj
.getPrefixedText() ) {
194 pageData
[ suggestionPage
.title
] = {
195 known
: suggestionPage
.known
!== undefined,
196 missing
: suggestionPage
.missing
!== undefined,
197 redirect
: suggestionPage
.redirect
!== undefined,
198 disambiguation
: OO
.getProp( suggestionPage
, 'pageprops', 'disambiguation' ) !== undefined,
199 imageUrl
: OO
.getProp( suggestionPage
, 'thumbnail', 'source' ),
200 description
: OO
.getProp( suggestionPage
, 'terms', 'description' ),
202 index
: suggestionPage
.index
205 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
206 // and we encounter a cross-namespace redirect.
207 if ( this.namespace === null || this.namespace === suggestionPage
.ns
) {
208 titles
.push( suggestionPage
.title
);
211 redirects
= redirectsTo
[ suggestionPage
.title
] || [];
212 for ( i
= 0, len
= redirects
.length
; i
< len
; i
++ ) {
213 pageData
[ redirects
[ i
] ] = {
217 disambiguation
: false,
218 description
: mw
.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage
.title
),
219 // Sort index, just below its target
220 index
: suggestionPage
.index
+ 0.5
222 titles
.push( redirects
[ i
] );
226 titles
.sort( function ( a
, b
) {
227 return pageData
[ a
].index
- pageData
[ b
].index
;
230 // If not found, run value through mw.Title to avoid treating a match as a
231 // mismatch where normalisation would make them matching (bug 48476)
234 Object
.prototype.hasOwnProperty
.call( pageData
, this.getQueryValue() ) &&
236 !pageData
[ this.getQueryValue() ].missing
||
237 pageData
[ this.getQueryValue() ].known
240 pageExists
= pageExistsExact
|| (
242 Object
.prototype.hasOwnProperty
.call( pageData
, titleObj
.getPrefixedText() ) &&
244 !pageData
[ titleObj
.getPrefixedText() ].missing
||
245 pageData
[ titleObj
.getPrefixedText() ].known
250 pageData
[ this.getQueryValue() ] = {
251 missing
: true, known
: false, redirect
: false, disambiguation
: false,
252 description
: mw
.msg( 'mw-widgets-titleinput-description-new-page' )
257 this.cache
.set( pageData
);
260 // Offer the exact text as a suggestion if the page exists
261 if ( pageExists
&& !pageExistsExact
) {
262 titles
.unshift( this.getQueryValue() );
264 // Offer the exact text as a new page if the title is valid
265 if ( this.showRedlink
&& !pageExists
&& titleObj
) {
266 titles
.push( this.getQueryValue() );
268 for ( i
= 0, len
= titles
.length
; i
< len
; i
++ ) {
269 page
= pageData
[ titles
[ i
] ] || {};
270 items
.push( new mw
.widgets
.TitleOptionWidget( this.getOptionWidgetData( titles
[ i
], page
) ) );
277 * Get menu option widget data from the title and page data
279 * @param {string} title Title object
280 * @param {Object} data Page data
281 * @return {Object} Data for option widget
283 mw
.widgets
.TitleWidget
.prototype.getOptionWidgetData = function ( title
, data
) {
284 var mwTitle
= new mw
.Title( title
);
286 data
: this.namespace !== null && this.relative
287 ? mwTitle
.getRelativeText( this.namespace )
289 url
: mwTitle
.getUrl(),
290 imageUrl
: this.showImages
? data
.imageUrl
: null,
291 description
: this.showDescriptions
? data
.description
: null,
292 missing
: data
.missing
,
293 redirect
: data
.redirect
,
294 disambiguation
: data
.disambiguation
,
295 query
: this.getQueryValue()
300 * Get title object corresponding to given value, or #getQueryValue if not given.
302 * @param {string} [value] Value to get a title for
303 * @return {mw.Title|null} Title object, or null if value is invalid
305 mw
.widgets
.TitleWidget
.prototype.getTitle = function ( value
) {
306 var title
= value
!== undefined ? value
: this.getQueryValue(),
307 // mw.Title doesn't handle null well
308 titleObj
= mw
.Title
.newFromText( title
, this.namespace !== null ? this.namespace : undefined );
314 * Check if the query is valid
316 * @return {boolean} The query is valid
318 mw
.widgets
.TitleWidget
.prototype.isQueryValid = function () {
319 return this.validateTitle
? !!this.getTitle() : true;
322 }( jQuery
, mediaWiki
) );