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 contentModel
, moduleName
, page
= data
.query
.pages
[ 0 ];
73 return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' );
75 contentModel
= page
.contentmodel
;
76 moduleName
= 'mediawiki.messagePoster.' + contentModel
;
77 return mw
.loader
.using( moduleName
).then( function () {
78 return factory
.createForContentModel(
84 return $.Deferred().reject( 'failed-to-load-module', 'Failed to load "' + moduleName
+ '"' );
86 }, function ( error
, details
) {
87 return $.Deferred().reject( 'content-model-query-failed', error
, details
);
92 * Creates a MessagePoster instance, given a title and content model
95 * @param {string} contentModel Content model of title
96 * @param {mw.Title} title Title being posted to
97 * @param {mw.Api} api mw.Api instance that the instance should use
98 * @return {mw.messagePoster.MessagePoster}
100 MessagePosterFactory
.prototype.createForContentModel = function ( contentModel
, title
, api
) {
101 return new this.contentModelToClass
[ contentModel
]( title
, api
);
105 factory
: new MessagePosterFactory()
107 }( mediaWiki
, jQuery
) );