Merge "Refactor a bunch of CategoryInputWidget code, tear out dead/broken redirect handling that should be rewritten"

This commit is contained in:
jenkins-bot 2014-09-17 02:13:22 +00:00 committed by Gerrit Code Review
commit d59053575e

View file

@ -36,7 +36,6 @@ ve.ui.MWCategoryInputWidget = function VeUiMWCategoryInputWidget( categoryWidget
// Initialization // Initialization
this.$element.addClass( 've-ui-mwCategoryInputWidget' ); this.$element.addClass( 've-ui-mwCategoryInputWidget' );
this.lookupMenu.$element.addClass( 've-ui-mwCategoryInputWidget-menu' ); this.lookupMenu.$element.addClass( 've-ui-mwCategoryInputWidget-menu' );
this.categoryRedirects = [];
}; };
/* Inheritance */ /* Inheritance */
@ -59,8 +58,7 @@ ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () {
list: 'allcategories', list: 'allcategories',
acmin: 1, acmin: 1,
acprefix: this.value, acprefix: this.value,
acprop: 'hidden', acprop: 'hidden'
redirects: ''
} ); } );
}; };
@ -71,31 +69,14 @@ ve.ui.MWCategoryInputWidget.prototype.getLookupRequest = function () {
* @param {Mixed} data Response from server * @param {Mixed} data Response from server
*/ */
ve.ui.MWCategoryInputWidget.prototype.getLookupCacheItemFromData = function ( data ) { ve.ui.MWCategoryInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
var categoryInputWidget = this, result = {}; var result = [];
if ( data.query && data.query.allcategories ) { data.query = data.query || {};
$.each( data.query.allcategories, function () {
var title = mw.Title.newFromText( this['*'] ); $.each( data.query.allcategories || [], function ( index, category ) {
if ( title ) { result.push( category['*'] );
result[title.getMainText()] = this.hasOwnProperty( 'hidden' ); this.categoryWidget.categoryHiddenStatus[category['*']] = category.hasOwnProperty( 'hidden' );
categoryInputWidget.categoryWidget.categoryHiddenStatus[this['*']] = result[title.getMainText()]; }.bind( this ) );
} else {
mw.log.warning( '"' + this['*'] + '" is an invalid title!' );
}
} );
}
if ( data.query && data.query.redirects ) {
$.each( data.query.redirects, function ( index, redirectInfo ) {
var foundIdentical = false;
$.each( categoryInputWidget.categoryRedirects, function ( index, existingRedirectInfo ) {
if ( existingRedirectInfo.from === redirectInfo.from && existingRedirectInfo.to === redirectInfo.to ) {
foundIdentical = true;
}
} );
if ( !foundIdentical ) {
categoryInputWidget.categoryRedirects.push( redirectInfo );
}
} );
}
return result; return result;
}; };
@ -106,171 +87,86 @@ ve.ui.MWCategoryInputWidget.prototype.getLookupCacheItemFromData = function ( da
* @returns {OO.ui.MenuItemWidget[]} Menu items * @returns {OO.ui.MenuItemWidget[]} Menu items
*/ */
ve.ui.MWCategoryInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) { ve.ui.MWCategoryInputWidget.prototype.getLookupMenuItemsFromData = function ( data ) {
var i, len, item, var exactMatch = false,
exactMatch = false, itemWidgets = [],
newCategoryItems = [], existingCategoryItems = [], matchingCategoryItems = [],
existingCategoryItems = [], hiddenCategoryItems = [], newCategoryItems = [],
matchingCategoryItems = [],
hiddenCategoryItems = [],
items = [],
menu$ = this.lookupMenu.$,
category = this.getCategoryItemFromValue( this.value ), category = this.getCategoryItemFromValue( this.value ),
existingCategories = this.categoryWidget.getCategories(), existingCategories = this.categoryWidget.getCategories(),
matchingCategories = [], linkCacheUpdate = {},
hiddenCategories = [], canonicalQueryValue = mw.Title.newFromText( this.value );
redirectStorage = {},
itemTitle,
searchForQueryWithinRedirectInfo = function ( element ) {
return element.lastIndexOf( new mw.Title( 'Category:' + category.value ).getPrefixedText(), 0 ) === 0;
};
$.each( this.categoryRedirects, function () { if ( canonicalQueryValue ) {
if ( redirectStorage.hasOwnProperty( this.to ) && redirectStorage[this.to].indexOf( this.from ) === -1 ) { canonicalQueryValue = canonicalQueryValue.getMainText();
redirectStorage[this.to].push( this.from ); } // Invalid titles just end up with canonicalQueryValue being null.
} else {
redirectStorage[this.to] = [this.from];
}
} );
$.each( data, function ( title, hiddenStatus ) { $.each( data, function ( index, suggestedCategory ) {
if ( hiddenStatus ) { if ( canonicalQueryValue === suggestedCategory ) {
hiddenCategories.push( title ); exactMatch = true;
} else {
matchingCategories.push( title );
} }
} ); linkCacheUpdate['Category:' + suggestedCategory] = { missing: false };
if ( ve.indexOf( suggestedCategory, existingCategories ) === -1 && suggestedCategory.lastIndexOf( canonicalQueryValue, 0 ) === 0 ) {
if ( this.categoryWidget.categoryHiddenStatus[suggestedCategory] ) {
hiddenCategoryItems.push( suggestedCategory );
} else {
matchingCategoryItems.push( suggestedCategory );
}
}
}.bind( this ) );
// Existing categories // Existing categories
// This is deliberately not checking the last existingCategories entry so we don't show it under $.each( existingCategories, function ( index, existingCategory ) {
// "Move this category here" etc. That is done below. if ( existingCategory === canonicalQueryValue ) {
for ( i = 0, len = existingCategories.length - 1; i < len; i++ ) { exactMatch = true;
item = existingCategories[i];
// Verify that item starts with category.value
if ( item.lastIndexOf( category.value, 0 ) === 0 ) {
if ( item === category.value ) {
exactMatch = true;
}
existingCategoryItems.push( item );
} }
} if ( index < existingCategories.length - 1 && existingCategory.lastIndexOf( canonicalQueryValue, 0 ) === 0 ) {
// Now check the last one. Don't add to existingCategoryItems but do make it a match // Verify that item starts with category.value
if ( existingCategories[existingCategories.length - 1] === category.value ) { existingCategoryItems.push( existingCategory );
exactMatch = true;
}
// Matching categories
for ( i = 0, len = matchingCategories.length; i < len; i++ ) {
item = matchingCategories[i];
itemTitle = new mw.Title( 'Category:' + item ).getPrefixedText();
if (
ve.indexOf( item, existingCategories ) === -1 &&
item.lastIndexOf( category.value, 0 ) === 0 || (
redirectStorage[itemTitle] !== undefined &&
$.grep( redirectStorage[itemTitle], searchForQueryWithinRedirectInfo ).length
)
) {
if ( ( item === category.value ) || (
redirectStorage[itemTitle] !== undefined &&
redirectStorage[itemTitle].indexOf( new mw.Title( 'Category:' + category.value ).getPrefixedText() ) !== -1
) ) {
exactMatch = true;
}
matchingCategoryItems.push( item );
} }
} } );
// Hidden categories
for ( i = 0, len = hiddenCategories.length; i < len; i++ ) {
item = hiddenCategories[i];
itemTitle = new mw.Title( 'Category:' + item ).getPrefixedText();
if (
ve.indexOf( item, existingCategories ) === -1 &&
item.lastIndexOf( category.value, 0 ) === 0 || (
redirectStorage[itemTitle] !== undefined &&
$.grep( redirectStorage[itemTitle], searchForQueryWithinRedirectInfo ).length
)
) {
if ( ( item === category.value ) || (
redirectStorage[itemTitle] !== undefined &&
redirectStorage[itemTitle].indexOf( new mw.Title( 'Category:' + category.value ).getPrefixedText() ) !== -1
) ) {
exactMatch = true;
}
hiddenCategoryItems.push( item );
}
}
// New category // New category
if ( !exactMatch ) { if ( !exactMatch ) {
newCategoryItems.push( category.value ); newCategoryItems.push( canonicalQueryValue );
linkCacheUpdate['Category:' + category.value] = { missing: true };
} }
// Add sections for non-empty groups ve.init.platform.linkCache.set( linkCacheUpdate );
if ( newCategoryItems.length ) {
items.push( new OO.ui.MenuSectionItemWidget(
'newCategory', { $: menu$, label: ve.msg( 'visualeditor-dialog-meta-categories-input-newcategorylabel' ) }
) );
for ( i = 0, len = newCategoryItems.length; i < len; i++ ) {
item = newCategoryItems[i];
items.push( new OO.ui.MenuItemWidget( item, { $: menu$, label: item } ) );
}
}
if ( existingCategoryItems.length ) {
items.push( new OO.ui.MenuSectionItemWidget(
'inArticle', { $: menu$, label: ve.msg( 'visualeditor-dialog-meta-categories-input-movecategorylabel' ) }
) );
for ( i = 0, len = existingCategoryItems.length; i < len; i++ ) {
item = existingCategoryItems[i];
items.push( new OO.ui.MenuItemWidget( item, { $: menu$, label: item } ) );
}
}
if ( matchingCategoryItems.length ) {
items.push( new OO.ui.MenuSectionItemWidget(
'matchingCategories', { $: menu$, label: ve.msg( 'visualeditor-dialog-meta-categories-input-matchingcategorieslabel' ) }
) );
for ( i = 0, len = matchingCategoryItems.length; i < len; i++ ) {
item = matchingCategoryItems[i];
items.push( this.getMenuItemWidgetFromCategoryName( item, menu$ ) );
}
}
if ( hiddenCategoryItems.length ) {
items.push( new OO.ui.MenuSectionItemWidget(
'hiddenCategories', { $: menu$, label: ve.msg( 'visualeditor-dialog-meta-categories-input-hiddencategorieslabel' ) }
) );
for ( i = 0, len = hiddenCategoryItems.length; i < len; i++ ) {
item = hiddenCategoryItems[i];
items.push( this.getMenuItemWidgetFromCategoryName( item, menu$ ) );
}
}
return items; // Add sections for non-empty groups. Each section consists of an id, a label and items
}; $.each( [
{
id: 'newCategory',
label: ve.msg( 'visualeditor-dialog-meta-categories-input-newcategorylabel' ),
items: newCategoryItems
},
{
id: 'inArticle',
label: ve.msg( 'visualeditor-dialog-meta-categories-input-movecategorylabel' ),
items: existingCategoryItems
},
{
id: 'matchingCategories',
label: ve.msg( 'visualeditor-dialog-meta-categories-input-matchingcategorieslabel' ),
items: matchingCategoryItems
},
{
id: 'hiddenCategories',
label: ve.msg( 'visualeditor-dialog-meta-categories-input-hiddencategorieslabel' ),
items: hiddenCategoryItems
}
], function ( index, sectionData ) {
if ( sectionData.items.length ) {
itemWidgets.push( new OO.ui.MenuSectionItemWidget(
sectionData.id, { $: this.lookupMenu.$, label: sectionData.label }
) );
$.each( sectionData.items, function ( index, categoryItem ) {
itemWidgets.push( new OO.ui.MenuItemWidget( categoryItem, { $: this.lookupMenu.$, label: categoryItem } ) );
}.bind( this ) );
}
}.bind( this ) );
/** return itemWidgets;
* Get a OO.ui.MenuSectionItemWidget object for a given category name.
* Deals with redirects.
*
* @method
* @param {string} item Category name
* @param {jQuery} menu$ Lookup menu jQuery
* @returns {OO.ui.MenuSectionItemWidget} Menu item
*/
ve.ui.MWCategoryInputWidget.prototype.getMenuItemWidgetFromCategoryName = function ( item, menu$ ) {
var itemTitle = new mw.Title( 'Category:' + item ).getPrefixedText(),
redirectInfo = $.grep( this.categoryRedirects, function ( redirectInfo ) {
return redirectInfo.to === itemTitle;
} );
if ( redirectInfo.length ) {
return new OO.ui.MenuItemWidget( item, {
$: menu$,
autoFitLabel: false,
label: this.$( '<span>' )
.text( new mw.Title( redirectInfo[0].from ).getMainText() )
.append( '<br>↳ ' )
.append( $( '<span>' ).text( new mw.Title( item ).getMainText() ) )
} );
} else {
return new OO.ui.MenuItemWidget( item, { $: menu$, label: item } );
}
}; };
/** /**