From: Bartosz DziewoƄski Date: Tue, 22 Dec 2015 18:01:12 +0000 (+0100) Subject: mw.widgets.CategorySelector: Don't lose namespace-like prefixes from category names X-Git-Tag: 1.31.0-rc.0~8274^2 X-Git-Url: http://git.cyclocoop.org//%22%22.url_de_base%28%29.%22/%22?a=commitdiff_plain;h=1ded5ff89cd72e3065f21ce2204e9abc4c7eb6a6;p=lhc%2Fweb%2Fwiklou.git mw.widgets.CategorySelector: Don't lose namespace-like prefixes from category names Also, handle queries which return non-category results. Bug: T122221 Change-Id: I205d2fe52a10bd3adcf0235108005dd96811ae65 --- diff --git a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js index 1e65da5e50..ec38e2be41 100644 --- a/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js +++ b/resources/src/mediawiki.widgets/mw.widgets.CategorySelector.js @@ -150,22 +150,30 @@ this.pushPending(); $.when.apply( $, promises ).done( function () { - var categories, categoryNames, + var categoryNames, allData = [], dataSets = Array.prototype.slice.apply( arguments ); // Collect values from all results allData = allData.concat.apply( allData, dataSets ); - // Remove duplicates - categories = allData.filter( function ( value, index, self ) { - return self.indexOf( value ) === index; - } ); - - // Get titles - categoryNames = categories.map( function ( name ) { - return mw.Title.newFromText( name, NS_CATEGORY ).getMainText(); - } ); + categoryNames = allData + // Remove duplicates + .filter( function ( value, index, self ) { + return self.indexOf( value ) === index; + } ) + // Get Title objects + .map( function ( name ) { + return mw.Title.newFromText( name ); + } ) + // Keep only titles from 'Category' namespace + .filter( function ( title ) { + return title && title.getNamespaceId() === NS_CATEGORY; + } ) + // Convert back to strings, strip 'Category:' prefix + .map( function ( title ) { + return title.getMainText(); + } ); deferred.resolve( categoryNames ); @@ -180,7 +188,7 @@ CSP.createItemWidget = function ( data ) { return new mw.widgets.CategoryCapsuleItemWidget( { apiUrl: this.api.apiUrl || undefined, - title: mw.Title.newFromText( data, NS_CATEGORY ) + title: mw.Title.makeTitle( NS_CATEGORY, data ) } ); }; @@ -190,7 +198,7 @@ CSP.getItemFromData = function ( data ) { // This is a bit of a hack... We have to canonicalize the data in the same way that // #createItemWidget and CategoryCapsuleItemWidget will do, otherwise we won't find duplicates. - data = mw.Title.newFromText( data, NS_CATEGORY ).getMainText(); + data = mw.Title.makeTitle( NS_CATEGORY, data ).getMainText(); return OO.ui.mixin.GroupElement.prototype.getItemFromData.call( this, data ); };