Mooeypoo has uploaded a new change for review. https://gerrit.wikimedia.org/r/102377
Change subject: Use image sources from API's fileRepo ...................................................................... Use image sources from API's fileRepo Instead of hard-coding the image resources to search from in the media insert dialog, this commit queries the local API to get the list of file repos through meta=filerepoinfo API request. The API request is only done once per session. Bug: 50673 Change-Id: Ia5ad9a8c00cca6cbbbc890359dc529e29e1a6be7 --- M modules/ve-mw/init/ve.init.mw.Platform.js M modules/ve-mw/ui/dialogs/ve.ui.MWMediaInsertDialog.js M modules/ve-mw/ui/widgets/ve.ui.MWMediaSearchWidget.js 3 files changed, 105 insertions(+), 14 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor refs/changes/77/102377/1 diff --git a/modules/ve-mw/init/ve.init.mw.Platform.js b/modules/ve-mw/init/ve.init.mw.Platform.js index b78271e..a841988 100644 --- a/modules/ve-mw/init/ve.init.mw.Platform.js +++ b/modules/ve-mw/init/ve.init.mw.Platform.js @@ -25,8 +25,8 @@ this.parsedMessages = {}; this.mediaSources = [ //TODO: Bug 50673 - { 'url': mw.util.wikiScript( 'api' ) }, - { 'url': '//commons.wikimedia.org/w/api.php' } + { 'url': mw.util.wikiScript( 'api' ), 'local': true }, + { 'apiurl': '//commons.wikimedia.org/w/api.php' } ]; }; diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaInsertDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaInsertDialog.js index 4cd9855..35a0a54 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaInsertDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaInsertDialog.js @@ -5,6 +5,8 @@ * @license The MIT License (MIT); see LICENSE.txt */ +/*global mw */ + /** * Dialog for inserting MediaWiki media objects. * @@ -24,6 +26,7 @@ // Properties this.item = null; + this.sources = {}; }; /* Inheritance */ @@ -59,14 +62,19 @@ // Parent method ve.ui.MWDialog.prototype.initialize.call( this ); - // Properties - this.search = new ve.ui.MWMediaSearchWidget( { '$': this.$ } ); + // Widget + this.search = new ve.ui.MWMediaSearchWidget( { + '$': this.$ + } ); + + // Initialization + this.search.$element.addClass( 've-ui-mwMediaInsertDialog-select' ); // Events this.search.connect( this, { 'select': 'onSearchSelect' } ); - // Initialization - this.search.$element.addClass( 've-ui-mwMediaInsertDialog-select' ); + this.$spinner = this.$( '<div>' ).addClass( 've-specialchar-spinner' ); + this.$body.append( this.$spinner ); this.$body.append( this.search.$element ); }; @@ -77,10 +85,81 @@ // Parent method ve.ui.MWDialog.prototype.setup.call( this, data ); - // Initialization - this.search.getQuery().$input.focus().select(); - this.search.getResults().selectItem(); - this.search.getResults().highlightItem(); + this.$spinner.show(); + this.search.$element.hide(); + // Get the repos from the API first + // The ajax request will only be done once per session + this.getFileRepos().done( ve.bind( function () { + // Done, hide the spinner + this.$spinner.hide(); + + // Show the search and query the media sources + this.search.$element.show(); + this.search.queryMediaSources(); + + // Initialization + this.search.getQuery().$input.focus().select(); + this.search.getResults().selectItem(); + this.search.getResults().highlightItem(); + }, this ) ); +}; + +/** + * Get the list of file repos to use for the media search + * @return {jQuery.Promise} + */ +ve.ui.MWMediaInsertDialog.prototype.getFileRepos = function () { + var xhr, deferred = $.Deferred(); + + // Only take this.sources if they aren't already loaded + if ( $.isEmptyObject( this.sources ) ) { + // Take sources from api.php?action=query&meta=filerepoinfo&format=jsonfm + xhr = $.ajax( { + 'url': mw.util.wikiScript( 'api' ), + 'data': { + 'action': 'query', + 'meta': 'filerepoinfo', + 'format': 'json' + }, + 'dataType': 'json', + 'type': 'POST', + // Wait up to 100 seconds before giving up + 'timeout': 100000, + 'cache': 'false' + } ) + .done( ve.bind( this.onResponseSuccess, this, deferred ) ) + .fail( ve.bind( this.onResponseFailure, this, deferred ) ); + } else { + deferred.resolve(); + } + + return deferred.promise(); + +}; + +/** + * Handle a successful response from the API to retrieve external repos. + * + * @param {jQuery.Deferred} deferred The Deferred object created by generateContents + * @param {Object} response Response data + */ +ve.ui.MWMediaInsertDialog.prototype.onResponseSuccess = function ( deferred, response ) { + this.sources = response.query.repos; + this.search.setSources( this.sources ); + deferred.resolve(); +}; + +/** + * Handle a successful response from the API to retrieve external repos. + * + * @param {jQuery.Deferred} deferred The Deferred object created by generateContents + * @param {Object} response Response data + */ +ve.ui.MWMediaInsertDialog.prototype.onResponseFailure = function ( deferred ) { + // Fallback on the default repos + this.sources = ve.copy( ve.init.platform.getMediaSources() ); + this.search.setSources( this.sources ); + deferred.resolve(); }; /** diff --git a/modules/ve-mw/ui/widgets/ve.ui.MWMediaSearchWidget.js b/modules/ve-mw/ui/widgets/ve.ui.MWMediaSearchWidget.js index 18f65cf..323ca17 100755 --- a/modules/ve-mw/ui/widgets/ve.ui.MWMediaSearchWidget.js +++ b/modules/ve-mw/ui/widgets/ve.ui.MWMediaSearchWidget.js @@ -28,7 +28,7 @@ OO.ui.SearchWidget.call( this, config ); // Properties - this.sources = ve.copy( ve.init.platform.getMediaSources() ); + this.sources = {}; this.size = config.size || 150; this.queryTimeout = null; this.titles = {}; @@ -39,7 +39,6 @@ // Initialization this.$element.addClass( 've-ui-mwMediaSearchWidget' ); - this.queryMediaSources(); }; /* Inheritance */ @@ -47,6 +46,14 @@ OO.inheritClass( ve.ui.MWMediaSearchWidget, OO.ui.SearchWidget ); /* Methods */ + +/** + * Get the fileRepo sources for the media search + * @param {Object} sources The sources object + */ +ve.ui.MWMediaSearchWidget.prototype.setSources = function ( sources ) { + this.sources = sources; +}; /** * Handle select widget select events. @@ -89,7 +96,7 @@ * @method */ ve.ui.MWMediaSearchWidget.prototype.queryMediaSources = function () { - var i, len, source, + var i, len, source, url, value = this.query.getValue(); if ( value === '' ) { @@ -104,9 +111,14 @@ if ( !source.gsroffset ) { source.gsroffset = 0; } + if ( source.local ) { + url = mw.util.wikiScript( 'api' ); + } else { + url = source.apiurl; + } this.query.pushPending(); source.request = $.ajax( { - 'url': source.url, + 'url': url, 'data': { 'format': 'json', 'action': 'query', -- To view, visit https://gerrit.wikimedia.org/r/102377 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ia5ad9a8c00cca6cbbbc890359dc529e29e1a6be7 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/VisualEditor Gerrit-Branch: master Gerrit-Owner: Mooeypoo <mor...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits