From: Timo Tijhof Date: Fri, 4 May 2018 02:12:18 +0000 (+0100) Subject: mediawiki.messagePoster: Move WikitextMessagePoster.js to its own directory X-Git-Tag: 1.34.0-rc.0~5480^2 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=151a8e730d3272855b3d2e2659192b63440574de;p=lhc%2Fweb%2Fwiklou.git mediawiki.messagePoster: Move WikitextMessagePoster.js to its own directory It was currently in the directory for 'mediawiki.messagePoster', but this file actually belongs to a different module. Bug: T193826 Change-Id: Ia51d2a373173c1bc7fe78196dbef89089c51ac86 --- diff --git a/resources/Resources.php b/resources/Resources.php index e8a5e24b92..66f37db531 100644 --- a/resources/Resources.php +++ b/resources/Resources.php @@ -1135,8 +1135,8 @@ return [ ], 'mediawiki.messagePoster' => [ 'scripts' => [ - 'resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js', - 'resources/src/mediawiki.messagePoster/mediawiki.messagePoster.MessagePoster.js', + 'resources/src/mediawiki.messagePoster/factory.js', + 'resources/src/mediawiki.messagePoster/MessagePoster.js', ], 'dependencies' => [ 'oojs', @@ -1147,7 +1147,7 @@ return [ ], 'mediawiki.messagePoster.wikitext' => [ 'scripts' => [ - 'resources/src/mediawiki.messagePoster/mediawiki.messagePoster.WikitextMessagePoster.js', + 'resources/src/mediawiki.messagePoster.wikitext/WikitextMessagePoster.js', ], 'dependencies' => [ 'mediawiki.api.edit', diff --git a/resources/src/mediawiki.messagePoster.wikitext/WikitextMessagePoster.js b/resources/src/mediawiki.messagePoster.wikitext/WikitextMessagePoster.js new file mode 100644 index 0000000000..a2dbcd4919 --- /dev/null +++ b/resources/src/mediawiki.messagePoster.wikitext/WikitextMessagePoster.js @@ -0,0 +1,53 @@ +( function ( mw, $ ) { + /** + * This is an implementation of MessagePoster for wikitext talk pages. + * + * @class mw.messagePoster.WikitextMessagePoster + * @extends mw.messagePoster.MessagePoster + * + * @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, api ) { + this.api = api; + this.title = title; + } + + OO.inheritClass( + WikitextMessagePoster, + mw.messagePoster.MessagePoster + ); + + /** + * @inheritdoc + */ + WikitextMessagePoster.prototype.post = function ( subject, body ) { + mw.messagePoster.WikitextMessagePoster.parent.prototype.post.call( this, subject, body ); + + // Add signature if needed + if ( body.indexOf( '~~~' ) === -1 ) { + body += '\n\n~~~~'; + } + + return this.api.newSection( + this.title, + subject, + body, + { redirect: true } + ).then( function ( resp, jqXHR ) { + if ( resp.edit.result === 'Success' ) { + return $.Deferred().resolve( resp, jqXHR ); + } else { + // mw.Api checks for response error. Are there actually cases where the + // request fails, but it's not caught there? + return $.Deferred().reject( 'api-unexpected' ); + } + }, function ( code, details ) { + return $.Deferred().reject( 'api-fail', code, details ); + } ).promise(); + }; + + mw.messagePoster.factory.register( 'wikitext', WikitextMessagePoster ); + mw.messagePoster.WikitextMessagePoster = WikitextMessagePoster; +}( mediaWiki, jQuery ) ); diff --git a/resources/src/mediawiki.messagePoster/MessagePoster.js b/resources/src/mediawiki.messagePoster/MessagePoster.js new file mode 100644 index 0000000000..64642b26bf --- /dev/null +++ b/resources/src/mediawiki.messagePoster/MessagePoster.js @@ -0,0 +1,40 @@ +( function ( mw ) { + /** + * This is the abstract base class for MessagePoster implementations. + * + * @abstract + * @class + * + * @constructor + * @param {mw.Title} title Title to post to + */ + mw.messagePoster.MessagePoster = function MwMessagePoster() {}; + + OO.initClass( mw.messagePoster.MessagePoster ); + + /** + * Post a message (with subject and body) to a talk page. + * + * @abstract + * @param {string} subject Subject/topic title. The amount of wikitext supported is + * implementation-specific. It is recommended to only use basic wikilink syntax for + * maximum compatibility. + * @param {string} body Body, as wikitext. Signature code will automatically be added + * by MessagePosters that require one, unless the message already contains the string + * ~~~. + * @return {jQuery.Promise} Promise completing when the post succeeds or fails. + * For failure, will be rejected with three arguments: + * + * - primaryError - Primary error code. For a mw.Api failure, + * this should be 'api-fail'. + * - secondaryError - Secondary error code. For a mw.Api failure, + * this, should be mw.Api's code, e.g. 'http', 'ok-but-empty', or the error passed through + * from the server. + * - details - Further details about the error + * + * @localdoc + * The base class currently does nothing, but could be used for shared analytics or + * something. + */ + mw.messagePoster.MessagePoster.prototype.post = function () {}; +}( mediaWiki ) ); diff --git a/resources/src/mediawiki.messagePoster/factory.js b/resources/src/mediawiki.messagePoster/factory.js new file mode 100644 index 0000000000..e20b422f14 --- /dev/null +++ b/resources/src/mediawiki.messagePoster/factory.js @@ -0,0 +1,107 @@ +( function ( mw, $ ) { + /** + * Factory for MessagePoster objects. This provides a pluggable to way to script the action + * of adding a message to someone's talk page. + * + * @class mw.messagePoster.factory + * @singleton + */ + function MessagePosterFactory() { + this.contentModelToClass = {}; + } + + OO.initClass( MessagePosterFactory ); + + // Note: This registration scheme is currently not compatible with LQT, since that doesn't + // have its own content model, just islqttalkpage. LQT pages will be passed to the wikitext + // MessagePoster. + /** + * Register a MessagePoster subclass for a given content model. + * + * @param {string} contentModel Content model of pages this MessagePoster can post to + * @param {Function} constructor Constructor of a MessagePoster subclass + */ + MessagePosterFactory.prototype.register = function ( contentModel, constructor ) { + if ( this.contentModelToClass[ contentModel ] !== undefined ) { + throw new Error( 'Content model "' + contentModel + '" is already registered' ); + } + + this.contentModelToClass[ contentModel ] = constructor; + }; + + /** + * Unregister a given content model. + * This is exposed for testing and should not normally be used. + * + * @param {string} contentModel Content model to unregister + */ + MessagePosterFactory.prototype.unregister = function ( contentModel ) { + delete this.contentModelToClass[ contentModel ]; + }; + + /** + * Create a MessagePoster for given a title. + * + * A promise for this is returned. It works by determining the content model, then loading + * the corresponding module (which registers the MessagePoster class), and finally constructing + * an object for the given title. + * + * This does not require the message and should be called as soon as possible, so that the + * API and ResourceLoader requests run 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: + * + * - errorCode Error code string + * - error Error explanation + * - details Further error details + */ + MessagePosterFactory.prototype.create = function ( title, apiUrl ) { + var factory = this, + api = apiUrl ? new mw.ForeignApi( apiUrl ) : new mw.Api(); + + return api.get( { + formatversion: 2, + action: 'query', + prop: 'info', + titles: title.getPrefixedDb() + } ).then( function ( data ) { + var contentModel, moduleName, page = data.query.pages[ 0 ]; + if ( !page ) { + return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' ); + } + contentModel = page.contentmodel; + moduleName = 'mediawiki.messagePoster.' + contentModel; + return mw.loader.using( moduleName ).then( function () { + return factory.createForContentModel( + contentModel, + title, + api + ); + }, function () { + return $.Deferred().reject( 'failed-to-load-module', 'Failed to load "' + moduleName + '"' ); + } ); + }, function ( error, details ) { + return $.Deferred().reject( 'content-model-query-failed', error, details ); + } ); + }; + + /** + * Creates a MessagePoster instance, given a title and content model + * + * @private + * @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} + */ + MessagePosterFactory.prototype.createForContentModel = function ( contentModel, title, api ) { + return new this.contentModelToClass[ contentModel ]( title, api ); + }; + + mw.messagePoster = { + factory: new MessagePosterFactory() + }; +}( mediaWiki, jQuery ) ); diff --git a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.MessagePoster.js b/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.MessagePoster.js deleted file mode 100644 index 64642b26bf..0000000000 --- a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.MessagePoster.js +++ /dev/null @@ -1,40 +0,0 @@ -( function ( mw ) { - /** - * This is the abstract base class for MessagePoster implementations. - * - * @abstract - * @class - * - * @constructor - * @param {mw.Title} title Title to post to - */ - mw.messagePoster.MessagePoster = function MwMessagePoster() {}; - - OO.initClass( mw.messagePoster.MessagePoster ); - - /** - * Post a message (with subject and body) to a talk page. - * - * @abstract - * @param {string} subject Subject/topic title. The amount of wikitext supported is - * implementation-specific. It is recommended to only use basic wikilink syntax for - * maximum compatibility. - * @param {string} body Body, as wikitext. Signature code will automatically be added - * by MessagePosters that require one, unless the message already contains the string - * ~~~. - * @return {jQuery.Promise} Promise completing when the post succeeds or fails. - * For failure, will be rejected with three arguments: - * - * - primaryError - Primary error code. For a mw.Api failure, - * this should be 'api-fail'. - * - secondaryError - Secondary error code. For a mw.Api failure, - * this, should be mw.Api's code, e.g. 'http', 'ok-but-empty', or the error passed through - * from the server. - * - details - Further details about the error - * - * @localdoc - * The base class currently does nothing, but could be used for shared analytics or - * something. - */ - mw.messagePoster.MessagePoster.prototype.post = function () {}; -}( mediaWiki ) ); diff --git a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.WikitextMessagePoster.js b/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.WikitextMessagePoster.js deleted file mode 100644 index a2dbcd4919..0000000000 --- a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.WikitextMessagePoster.js +++ /dev/null @@ -1,53 +0,0 @@ -( function ( mw, $ ) { - /** - * This is an implementation of MessagePoster for wikitext talk pages. - * - * @class mw.messagePoster.WikitextMessagePoster - * @extends mw.messagePoster.MessagePoster - * - * @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, api ) { - this.api = api; - this.title = title; - } - - OO.inheritClass( - WikitextMessagePoster, - mw.messagePoster.MessagePoster - ); - - /** - * @inheritdoc - */ - WikitextMessagePoster.prototype.post = function ( subject, body ) { - mw.messagePoster.WikitextMessagePoster.parent.prototype.post.call( this, subject, body ); - - // Add signature if needed - if ( body.indexOf( '~~~' ) === -1 ) { - body += '\n\n~~~~'; - } - - return this.api.newSection( - this.title, - subject, - body, - { redirect: true } - ).then( function ( resp, jqXHR ) { - if ( resp.edit.result === 'Success' ) { - return $.Deferred().resolve( resp, jqXHR ); - } else { - // mw.Api checks for response error. Are there actually cases where the - // request fails, but it's not caught there? - return $.Deferred().reject( 'api-unexpected' ); - } - }, function ( code, details ) { - return $.Deferred().reject( 'api-fail', code, details ); - } ).promise(); - }; - - mw.messagePoster.factory.register( 'wikitext', WikitextMessagePoster ); - mw.messagePoster.WikitextMessagePoster = WikitextMessagePoster; -}( mediaWiki, jQuery ) ); diff --git a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js b/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js deleted file mode 100644 index e20b422f14..0000000000 --- a/resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js +++ /dev/null @@ -1,107 +0,0 @@ -( function ( mw, $ ) { - /** - * Factory for MessagePoster objects. This provides a pluggable to way to script the action - * of adding a message to someone's talk page. - * - * @class mw.messagePoster.factory - * @singleton - */ - function MessagePosterFactory() { - this.contentModelToClass = {}; - } - - OO.initClass( MessagePosterFactory ); - - // Note: This registration scheme is currently not compatible with LQT, since that doesn't - // have its own content model, just islqttalkpage. LQT pages will be passed to the wikitext - // MessagePoster. - /** - * Register a MessagePoster subclass for a given content model. - * - * @param {string} contentModel Content model of pages this MessagePoster can post to - * @param {Function} constructor Constructor of a MessagePoster subclass - */ - MessagePosterFactory.prototype.register = function ( contentModel, constructor ) { - if ( this.contentModelToClass[ contentModel ] !== undefined ) { - throw new Error( 'Content model "' + contentModel + '" is already registered' ); - } - - this.contentModelToClass[ contentModel ] = constructor; - }; - - /** - * Unregister a given content model. - * This is exposed for testing and should not normally be used. - * - * @param {string} contentModel Content model to unregister - */ - MessagePosterFactory.prototype.unregister = function ( contentModel ) { - delete this.contentModelToClass[ contentModel ]; - }; - - /** - * Create a MessagePoster for given a title. - * - * A promise for this is returned. It works by determining the content model, then loading - * the corresponding module (which registers the MessagePoster class), and finally constructing - * an object for the given title. - * - * This does not require the message and should be called as soon as possible, so that the - * API and ResourceLoader requests run 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: - * - * - errorCode Error code string - * - error Error explanation - * - details Further error details - */ - MessagePosterFactory.prototype.create = function ( title, apiUrl ) { - var factory = this, - api = apiUrl ? new mw.ForeignApi( apiUrl ) : new mw.Api(); - - return api.get( { - formatversion: 2, - action: 'query', - prop: 'info', - titles: title.getPrefixedDb() - } ).then( function ( data ) { - var contentModel, moduleName, page = data.query.pages[ 0 ]; - if ( !page ) { - return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' ); - } - contentModel = page.contentmodel; - moduleName = 'mediawiki.messagePoster.' + contentModel; - return mw.loader.using( moduleName ).then( function () { - return factory.createForContentModel( - contentModel, - title, - api - ); - }, function () { - return $.Deferred().reject( 'failed-to-load-module', 'Failed to load "' + moduleName + '"' ); - } ); - }, function ( error, details ) { - return $.Deferred().reject( 'content-model-query-failed', error, details ); - } ); - }; - - /** - * Creates a MessagePoster instance, given a title and content model - * - * @private - * @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} - */ - MessagePosterFactory.prototype.createForContentModel = function ( contentModel, title, api ) { - return new this.contentModelToClass[ contentModel ]( title, api ); - }; - - mw.messagePoster = { - factory: new MessagePosterFactory() - }; -}( mediaWiki, jQuery ) );