4 * This is a factory for MessagePoster objects, which allows a pluggable to way to script leaving a
7 * @class mw.messagePoster.factory
10 function MwMessagePosterFactory() {
11 this.contentModelToClass = {};
14 OO.initClass( MwMessagePosterFactory );
16 // Note: This registration scheme is currently not compatible with LQT, since that doesn't
17 // have its own content model, just islqttalkpage. LQT pages will be passed to the wikitext
20 * Registers a MessagePoster subclass for a given content model.
22 * @param {string} contentModel Content model of pages this MessagePoster can post to
23 * @param {Function} messagePosterConstructor Constructor for MessagePoster
25 MwMessagePosterFactory.prototype.register = function ( contentModel, messagePosterConstructor ) {
26 if ( this.contentModelToClass[ contentModel ] !== undefined ) {
27 throw new Error( 'The content model \'' + contentModel + '\' is already registered.' );
30 this.contentModelToClass[ contentModel ] = messagePosterConstructor;
34 * Unregisters a given content model
35 * This is exposed for testing and should not normally be needed.
37 * @param {string} contentModel Content model to unregister
39 MwMessagePosterFactory.prototype.unregister = function ( contentModel ) {
40 delete this.contentModelToClass[ contentModel ];
44 * Creates a MessagePoster, given a title. A promise for this is returned.
45 * This works by determining the content model, then loading the corresponding
46 * module (which will register the MessagePoster class), and finally constructing it.
48 * This does not require the message and should be called as soon as possible, so it does the
49 * API and ResourceLoader requests in the background.
51 * @param {mw.Title} title Title that will be posted to
52 * @param {string} [apiUrl] api.php URL if the title is on another wiki
53 * @return {jQuery.Promise} Promise resolving to a mw.messagePoster.MessagePoster.
54 * For failure, rejected with up to three arguments:
56 * - errorCode Error code string
57 * - error Error explanation
58 * - details Further error details
60 MwMessagePosterFactory.prototype.create = function ( title, apiUrl ) {
61 var pageId, page, contentModel, moduleName, api,
65 api = new mw.ForeignApi( apiUrl );
74 titles: title.getPrefixedDb()
75 } ).then( function ( result ) {
76 if ( result.query.pageids && result.query.pageids.length > 0 ) {
77 pageId = result.query.pageids[ 0 ];
78 page = result.query.pages[ pageId ];
80 contentModel = page.contentmodel;
81 moduleName = 'mediawiki.messagePoster.' + contentModel;
82 return mw.loader.using( moduleName ).then( function () {
83 return factory.createForContentModel(
89 return $.Deferred().reject( 'failed-to-load-module', 'Failed to load the \'' + moduleName + '\' module' );
92 return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' );
94 }, function ( errorCode, details ) {
95 return $.Deferred().reject( 'content-model-query-failed', errorCode, details );
100 * Creates a MessagePoster instance, given a title and content model
104 * @param {string} contentModel Content model of title
105 * @param {mw.Title} title Title being posted to
106 * @param {mw.Api} api mw.Api instance that the instance should use
107 * @return {mw.messagePoster.MessagePoster}
110 MwMessagePosterFactory.prototype.createForContentModel = function ( contentModel, title, api ) {
111 return new this.contentModelToClass[ contentModel ]( title, api );
115 factory: new MwMessagePosterFactory()
117 }( mediaWiki, jQuery ) );