From 59fca1594c31838181ff2d1875737e1119b52b0e Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Thu, 10 Dec 2015 10:51:27 -0800 Subject: [PATCH] mw.messagePoster: Support foreign wikis Allow passing in an external api.php URL to post a message on a remote wiki. Note that the remote content model must have its messagePoster implementation registered on the local wiki for this to work. Bug: T111590 Change-Id: Id52b7d3a12ed5e57e29d3f22fb7f4f36b8a908b1 --- resources/Resources.php | 1 + ...iki.messagePoster.WikitextMessagePoster.js | 5 +++-- .../mediawiki.messagePoster.factory.js | 22 +++++++++++++------ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/resources/Resources.php b/resources/Resources.php index 4657a6a8c7..d7320f54e0 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1065,6 +1065,7 @@ return array( 'dependencies' => array( 'oojs', 'mediawiki.api', + 'mediawiki.ForeignApi', ), 'targets' => array( 'desktop', 'mobile' ), ), diff --git a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.WikitextMessagePoster.js b/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.WikitextMessagePoster.js index ad01865620..862454d88b 100644 --- a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.WikitextMessagePoster.js +++ b/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.WikitextMessagePoster.js @@ -8,9 +8,10 @@ * * @constructor * @param {mw.Title} title Wikitext page in a talk namespace, to post to + * @param {mw.Api} api mw.Api object to use */ - function WikitextMessagePoster( title ) { - this.api = new mw.Api(); + function WikitextMessagePoster( title, api ) { + this.api = api; this.title = title; } diff --git a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js b/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js index 4d0231f3f6..b6dc2b4566 100644 --- a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js +++ b/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js @@ -8,7 +8,6 @@ * @singleton */ function MwMessagePosterFactory() { - this.api = new mw.Api(); this.contentModelToClass = {}; } @@ -50,6 +49,7 @@ * API and ResourceLoader requests in the background. * * @param {mw.Title} title Title that will be posted to + * @param {string} [apiUrl] api.php URL if the title is on another wiki * @return {jQuery.Promise} Promise resolving to a mw.messagePoster.MessagePoster. * For failure, rejected with up to three arguments: * @@ -57,11 +57,17 @@ * - error Error explanation * - details Further error details */ - MwMessagePosterFactory.prototype.create = function ( title ) { - var pageId, page, contentModel, moduleName, + MwMessagePosterFactory.prototype.create = function ( title, apiUrl ) { + var pageId, page, contentModel, moduleName, api, factory = this; - return this.api.get( { + if ( apiUrl ) { + api = new mw.ForeignApi( apiUrl ); + } else { + api = mw.Api(); + } + + return api.get( { action: 'query', prop: 'info', indexpageids: true, @@ -76,7 +82,8 @@ return mw.loader.using( moduleName ).then( function () { return factory.createForContentModel( contentModel, - title + title, + api ); }, function () { return $.Deferred().reject( 'failed-to-load-module', 'Failed to load the \'' + moduleName + '\' module' ); @@ -96,11 +103,12 @@ * * @param {string} contentModel Content model of title * @param {mw.Title} title Title being posted to + * @param {mw.Api} api mw.Api instance that the instance should use * @return {mw.messagePoster.MessagePoster} * */ - MwMessagePosterFactory.prototype.createForContentModel = function ( contentModel, title ) { - return new this.contentModelToClass[ contentModel ]( title ); + MwMessagePosterFactory.prototype.createForContentModel = function ( contentModel, title, api ) { + return new this.contentModelToClass[ contentModel ]( title, api ); }; mw.messagePoster = { -- 2.20.1