3 * Factory for MessagePoster objects. This provides a pluggable to way to script the action
4 * of adding a message to someone's talk page.
6 * @class mw.messagePoster.factory
9 function MessagePosterFactory() {
10 this.contentModelToClass = {};
13 OO.initClass( MessagePosterFactory );
15 // Note: This registration scheme is currently not compatible with LQT, since that doesn't
16 // have its own content model, just islqttalkpage. LQT pages will be passed to the wikitext
19 * Register a MessagePoster subclass for a given content model.
21 * @param {string} contentModel Content model of pages this MessagePoster can post to
22 * @param {Function} constructor Constructor of a MessagePoster subclass
24 MessagePosterFactory.prototype.register = function ( contentModel, constructor ) {
25 if ( this.contentModelToClass[ contentModel ] !== undefined ) {
26 throw new Error( 'Content model "' + contentModel + '" is already registered' );
29 this.contentModelToClass[ contentModel ] = constructor;
33 * Unregister a given content model.
34 * This is exposed for testing and should not normally be used.
36 * @param {string} contentModel Content model to unregister
38 MessagePosterFactory.prototype.unregister = function ( contentModel ) {
39 delete this.contentModelToClass[ contentModel ];
43 * Create a MessagePoster for given a title.
45 * A promise for this is returned. It works by determining the content model, then loading
46 * the corresponding module (which registers the MessagePoster class), and finally constructing
47 * an object for the given title.
49 * This does not require the message and should be called as soon as possible, so that the
50 * API and ResourceLoader requests run in the background.
52 * @param {mw.Title} title Title that will be posted to
53 * @param {string} [apiUrl] api.php URL if the title is on another wiki
54 * @return {jQuery.Promise} Promise resolving to a mw.messagePoster.MessagePoster.
55 * For failure, rejected with up to three arguments:
57 * - errorCode Error code string
58 * - error Error explanation
59 * - details Further error details
61 MessagePosterFactory.prototype.create = function ( title, apiUrl ) {
63 api = apiUrl ? new mw.ForeignApi( apiUrl ) : new mw.Api();
69 titles: title.getPrefixedDb()
70 } ).then( function ( data ) {
71 var pageId, page, contentModel, moduleName;
72 if ( !data.query.pageids[ 0 ] ) {
73 return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' );
75 pageId = data.query.pageids[ 0 ];
76 page = data.query.pages[ pageId ];
78 contentModel = page.contentmodel;
79 moduleName = 'mediawiki.messagePoster.' + contentModel;
80 return mw.loader.using( moduleName ).then( function () {
81 return factory.createForContentModel(
87 return $.Deferred().reject( 'failed-to-load-module', 'Failed to load "' + moduleName + '"' );
89 }, function ( error, details ) {
90 return $.Deferred().reject( 'content-model-query-failed', error, details );
95 * Creates a MessagePoster instance, given a title and content model
98 * @param {string} contentModel Content model of title
99 * @param {mw.Title} title Title being posted to
100 * @param {mw.Api} api mw.Api instance that the instance should use
101 * @return {mw.messagePoster.MessagePoster}
103 MessagePosterFactory.prototype.createForContentModel = function ( contentModel, title, api ) {
104 return new this.contentModelToClass[ contentModel ]( title, api );
108 factory: new MessagePosterFactory()
110 }( mediaWiki, jQuery ) );