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, return a title relative to it
21 * @cfg {boolean} [suggestions=true] Display search suggestions
22 * @cfg {boolean} [showRedirectTargets=true] Show the targets of redirects
23 * @cfg {boolean} [showRedlink] Show red link to exact match if it doesn't exist
24 * @cfg {boolean} [showImages] Show page images
25 * @cfg {boolean} [showDescriptions] Show page descriptions
26 * @cfg {Object} [cache] Result cache which implements a 'set' method, taking keyed values as an argument
28 mw.widgets.TitleWidget = function MwWidgetsTitleWidget( config ) {
31 // Config initialization
38 this.limit = config.limit;
39 this.maxLength = config.maxLength;
40 this.namespace = config.namespace !== undefined ? config.namespace : null;
41 this.relative = config.relative !== undefined ? config.relative : true;
42 this.suggestions = config.suggestions !== undefined ? config.suggestions : true;
43 this.showRedirectTargets = config.showRedirectTargets !== false;
44 this.showRedlink = !!config.showRedlink;
45 this.showImages = !!config.showImages;
46 this.showDescriptions = !!config.showDescriptions;
47 this.cache = config.cache;
50 this.$element.addClass( 'mw-widget-titleWidget' );
51 this.interwikiPrefixes = [];
52 this.interwikiPrefixesPromise = new mw.Api().get( {
55 siprop: 'interwikimap'
56 } ).done( function ( data ) {
57 $.each( data.query.interwikimap, function ( index, interwiki ) {
58 widget.interwikiPrefixes.push( interwiki.prefix );
65 OO.initClass( mw.widgets.TitleWidget );
70 * Get the current value of the search query
73 * @return {string} Search query
75 mw.widgets.TitleWidget.prototype.getQueryValue = null;
78 * Get the namespace to prepend to titles in suggestions, if any.
80 * @return {number|null} Namespace number
82 mw.widgets.TitleWidget.prototype.getNamespace = function () {
83 return this.namespace;
87 * Set the namespace to prepend to titles in suggestions, if any.
89 * @param {number|null} namespace Namespace number
91 mw.widgets.TitleWidget.prototype.setNamespace = function ( namespace ) {
92 this.namespace = namespace;
96 * Get a promise which resolves with an API repsonse for suggested
97 * links for the current query.
99 mw.widgets.TitleWidget.prototype.getSuggestionsPromise = function () {
101 query = this.getQueryValue(),
103 promiseAbortObject = { abort: function () {
104 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
107 if ( mw.Title.newFromText( query ) ) {
108 return this.interwikiPrefixesPromise.then( function () {
110 interwiki = query.substring( 0, query.indexOf( ':' ) );
112 interwiki && interwiki !== '' &&
113 widget.interwikiPrefixes.indexOf( interwiki ) !== -1
115 return $.Deferred().resolve( { query: {
119 } } ).promise( promiseAbortObject );
123 generator: 'prefixsearch',
125 gpsnamespace: widget.namespace !== null ? widget.namespace : undefined,
126 gpslimit: widget.limit,
127 ppprop: 'disambiguation'
129 props = [ 'info', 'pageprops' ];
130 if ( widget.showRedirectTargets ) {
131 params.redirects = '1';
133 if ( widget.showImages ) {
134 props.push( 'pageimages' );
135 params.pithumbsize = 80;
136 params.pilimit = widget.limit;
138 if ( widget.showDescriptions ) {
139 props.push( 'pageterms' );
140 params.wbptterms = 'description';
142 params.prop = props.join( '|' );
143 req = new mw.Api().get( params );
144 promiseAbortObject.abort = req.abort.bind( req ); // todo: ew
147 } ).promise( promiseAbortObject );
149 // Don't send invalid titles to the API.
150 // Just pretend it returned nothing so we can show the 'invalid title' section
151 return $.Deferred().resolve( {} ).promise( promiseAbortObject );
156 * Get option widgets from the server response
158 * @param {Object} data Query result
159 * @returns {OO.ui.OptionWidget[]} Menu items
161 mw.widgets.TitleWidget.prototype.getOptionsFromData = function ( data ) {
162 var i, len, index, pageExists, pageExistsExact, suggestionPage, page, redirect, redirects,
165 titleObj = mw.Title.newFromText( this.getQueryValue() ),
169 if ( data.redirects ) {
170 for ( i = 0, len = data.redirects.length; i < len; i++ ) {
171 redirect = data.redirects[ i ];
172 redirectsTo[ redirect.to ] = redirectsTo[ redirect.to ] || [];
173 redirectsTo[ redirect.to ].push( redirect.from );
177 for ( index in data.pages ) {
178 suggestionPage = data.pages[ index ];
179 pageData[ suggestionPage.title ] = {
180 missing: suggestionPage.missing !== undefined,
181 redirect: suggestionPage.redirect !== undefined,
182 disambiguation: OO.getProp( suggestionPage, 'pageprops', 'disambiguation' ) !== undefined,
183 imageUrl: OO.getProp( suggestionPage, 'thumbnail', 'source' ),
184 description: OO.getProp( suggestionPage, 'terms', 'description' )
187 // Throw away pages from wrong namespaces. This can happen when 'showRedirectTargets' is true
188 // and we encounter a cross-namespace redirect.
189 if ( this.namespace === null || this.namespace === suggestionPage.ns ) {
190 titles.push( suggestionPage.title );
193 redirects = redirectsTo[ suggestionPage.title ] || [];
194 for ( i = 0, len = redirects.length; i < len; i++ ) {
195 pageData[ redirects[ i ] ] = {
198 disambiguation: false,
199 description: mw.msg( 'mw-widgets-titleinput-description-redirect', suggestionPage.title )
201 titles.push( redirects[ i ] );
205 // If not found, run value through mw.Title to avoid treating a match as a
206 // mismatch where normalisation would make them matching (bug 48476)
208 pageExistsExact = titles.indexOf( this.getQueryValue() ) !== -1;
209 pageExists = pageExistsExact || (
210 titleObj && titles.indexOf( titleObj.getPrefixedText() ) !== -1
214 pageData[ this.getQueryValue() ] = {
215 missing: true, redirect: false, disambiguation: false,
216 description: mw.msg( 'mw-widgets-titleinput-description-new-page' )
221 this.cache.set( pageData );
224 // Offer the exact text as a suggestion if the page exists
225 if ( pageExists && !pageExistsExact ) {
226 titles.unshift( this.getQueryValue() );
228 // Offer the exact text as a new page if the title is valid
229 if ( this.showRedlink && !pageExists && titleObj ) {
230 titles.push( this.getQueryValue() );
232 for ( i = 0, len = titles.length; i < len; i++ ) {
233 page = pageData[ titles[ i ] ] || {};
234 items.push( new mw.widgets.TitleOptionWidget( this.getOptionWidgetData( titles[ i ], page ) ) );
241 * Get menu option widget data from the title and page data
243 * @param {string} title Title object
244 * @param {Object} data Page data
245 * @return {Object} Data for option widget
247 mw.widgets.TitleWidget.prototype.getOptionWidgetData = function ( title, data ) {
248 var mwTitle = new mw.Title( title );
250 data: this.namespace !== null && this.relative
251 ? mwTitle.getRelativeText( this.namespace )
253 url: mwTitle.getUrl(),
254 imageUrl: this.showImages ? data.imageUrl : null,
255 description: this.showDescriptions ? data.description : null,
256 missing: data.missing,
257 redirect: data.redirect,
258 disambiguation: data.disambiguation,
259 query: this.getQueryValue()
264 * Get title object corresponding to given value, or #getQueryValue if not given.
266 * @param {string} [value] Value to get a title for
267 * @returns {mw.Title|null} Title object, or null if value is invalid
269 mw.widgets.TitleWidget.prototype.getTitle = function ( value ) {
270 var title = value !== undefined ? value : this.getQueryValue(),
271 // mw.Title doesn't handle null well
272 titleObj = mw.Title.newFromText( title, this.namespace !== null ? this.namespace : undefined );
278 * Check if the query is valid
280 * @return {boolean} The query is valid
282 mw.widgets.TitleWidget.prototype.isQueryValid = function () {
283 return !!this.getTitle();
286 }( jQuery, mediaWiki ) );