Gergő Tisza has uploaded a new change for review. https://gerrit.wikimedia.org/r/107588
Change subject: WIP Create model & providers for file usage information ...................................................................... WIP Create model & providers for file usage information Incomplete and untested - shared to get early feedback on the code organization. Bug: 60087 Change-Id: I0e72c1e4590714e92a365f9741633221af5b0553 --- M resources/ext.multimediaViewer/ext.multimediaViewer.dataModel.js A resources/ext.multimediaViewer/ext.multimediaViewer.dataProvider.js 2 files changed, 210 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MultimediaViewer refs/changes/88/107588/1 diff --git a/resources/ext.multimediaViewer/ext.multimediaViewer.dataModel.js b/resources/ext.multimediaViewer/ext.multimediaViewer.dataModel.js index 5753e95..3f8add6 100644 --- a/resources/ext.multimediaViewer/ext.multimediaViewer.dataModel.js +++ b/resources/ext.multimediaViewer/ext.multimediaViewer.dataModel.js @@ -295,9 +295,71 @@ oo.inheritClass( ForeignDbRepo, Repo ); + /** + * @class mw.mmv.model.FileUsage + * Represents information about the wiki pages using a given file + * @constructor + * @param {mw.Title} file + * @param {String} scope scope of pages - one of 'local', 'global' + * @param {{wiki: String|null, page: mw.Title}[]} pages + * A list of pages which use this file. Each page is an object with a 'page' field + * containing the wiki page (a Title object) and a 'wiki' field which is a domain name, + * or null for local files. + * @param {Number} [totalCount] total number of pages where the file is used (the list passed + * in pages parameter might be cut off at some limit) + * @param {boolean} [totalCountIsLowerBound = false] for files which are used on a huge amount + * of pages, getting an exact count might be impossible, in which case this parameter + * is expected to be true. + * + */ + function FileUsage( + file, + scope, + pages, + totalCount, + totalCountIsLowerBound + ) { + /** + * @type {mw.Title} + */ + this.file = file; + + /** + * Scope of pages - one of 'local', 'global'. 'local' means only pages from the local wiki, + * 'global' means all usages. + * @type {String} + */ + this.scope = scope; + + /** + * A list of pages which use this file. Each page is an object with a 'page' field + * containing the wiki page (a Title object) and a 'wiki' field which is an interwiki + * prefix, or null for local files. + * @type {{wiki: String|null, page: mw.Title}[]} + */ + this.pages = pages; + + /** + * The total number of pages where the file is used. + * The list stored in this.pages might be cut off at some limit. + * @type {Number} + */ + this.totalCount = totalCount || pages.length; + + /** + * For files which are used on a huge amount of pages, getting an exact count might be + * impossible. In such a case totalCount is a lower bound. For example if totalCount is 100 + * and totalCountIsLowerBound is true, a message about usage count should be something + * like "this file is used on more than 100 pages". + * @type {boolean} + */ + this.totalCountIsLowerBound = !!totalCountIsLowerBound; + } + mw.mmv.model = {}; mw.mmv.model.Image = Image; mw.mmv.model.Repo = Repo; mw.mmv.model.ForeignApiRepo = ForeignApiRepo; mw.mmv.model.ForeignDbRepo = ForeignDbRepo; + mw.mmv.model.FileUsage = FileUsage; }( mediaWiki, OO ) ); diff --git a/resources/ext.multimediaViewer/ext.multimediaViewer.dataProvider.js b/resources/ext.multimediaViewer/ext.multimediaViewer.dataProvider.js new file mode 100644 index 0000000..3f0aa01 --- /dev/null +++ b/resources/ext.multimediaViewer/ext.multimediaViewer.dataProvider.js @@ -0,0 +1,148 @@ +/* + * This file is part of the MediaWiki extension MultimediaViewer. + * + * MultimediaViewer is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * MultimediaViewer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>. + */ + +( function ( mw, oo ) { + + /** + * @class mw.mmv.DataProvider + * @param {mediaWiki.Api} api + * @constructor + */ + function ApiDataProvider( api ) { + /** + * @type {mediaWiki.Api} + */ + this.api = api; + } + + /** + * @param {Object} data + */ + ApiDataProvider.prototype.getErrorMessage = function( data ) { + var errorCode, errorMessage; + errorCode = data.error && data.error.code; + errorMessage = data.error && data.error.info || 'unknown error'; + if ( errorCode ) { + errorMessage = errorCode + ': ' + errorMessage; + } + return errorMessage; + }; + + function ImageUsageDataProvider( api ) { + ApiDataProvider.apply( this, arguments ); + } + oo.inheritClass( ImageUsageDataProvider, ApiDataProvider ); + + /** + * @param {mw.Title} file + * @param {Object} options + * @return {jQuery.Promise} + */ + ImageUsageDataProvider.prototype.get = function( file, options ) { + options = $.extend( { + namespace: [0, 100], // article, portal + apiLimit: 100, + dataLimit: 10 + }, options ); + + return this.api.get( { + action: 'query', + list: 'imageusage', + iutitle: file.getPrefixedDb(), + namespace: options.namespace, + limit: options.limit, + format: 'json' + }).then( function( data ) { + var pages; + if ( data && data.query && data.query.imageusage ) { + pages = $.map( data.query.imageusage, function( item ) { + return { + wiki: null, + page: new mw.Title( item.title, item.ns ) + }; + } ); + return new mw.mmv.model.FileUsage( + file, + 'local', + pages.slice(0, options.dataLimit ), + pages.length, + data['query-continue'] && data['query-continue'].imageusage + ); + } else { + return $.Deferred().reject( this.getErrorMessage( data ) ); + } + } ); + }; + + + function GlobalUsageDataProvider( api ) { + ApiDataProvider.apply( this, arguments ); + } + oo.inheritClass( GlobalUsageDataProvider, ApiDataProvider ); + + /** + * @param {mw.Title} file + * @param {Object} options + * @return {jQuery.Promise} + */ + GlobalUsageDataProvider.prototype.get = function( file, options ) { + options = $.extend( { + namespace: [0, 100], // article, portal + apiLimit: 100, + dataLimit: 10 + }, options ); + + return this.api.get( { + action: 'query', + prop: 'globalusage', + titles: file.getPrefixedDb(), + namespace: options.namespace, + guprops: ['url', 'namespace'], + gulimit: options.limit, + format: 'json' + }).then( function( data ) { + var pages; + if ( data && data.query && data.query.pages ) { + // pages is an associative array indexed by pageid, turn it to proper array + pages = $.map( data.query.pages, function ( v, k ) { return v; } ); + if ( !pages.length ) { + return $.Deferred().reject( 'File not found' ); + } + pages = $.map( pages[0], function( item ) { + return { + wiki: item.wiki, + page: new mw.Title( item.title, item.ns ) + }; + } ); + return new mw.mmv.model.FileUsage( + file, + 'global', + pages.slice(0, options.dataLimit ), + pages.length, + data['query-continue'] && data['query-continue'].globalusage + ); + } else { + return $.Deferred().reject( this.getErrorMessage( data ) ); + } + } ); + }; + + mw.mmv.provider = {}; + mw.mmv.provider.ApiDataProvider = ApiDataProvider; + mw.mmv.provider.ImageUsageDataProvider = ImageUsageDataProvider; + mw.mmv.provider.GlobalUsageDataProvider = GlobalUsageDataProvider; +}( mediaWiki, OO ) ); -- To view, visit https://gerrit.wikimedia.org/r/107588 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I0e72c1e4590714e92a365f9741633221af5b0553 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MultimediaViewer Gerrit-Branch: master Gerrit-Owner: Gergő Tisza <gti...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits