4 * Factory for MessagePoster objects. This provides a pluggable to way to script the action
5 * of adding a message to someone's talk page.
7 * @class mw.messagePoster.factory
10 function MessagePosterFactory() {
11 this.contentModelToClass
= {};
14 OO
.initClass( MessagePosterFactory
);
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 * Register a MessagePoster subclass for a given content model.
22 * @param {string} contentModel Content model of pages this MessagePoster can post to
23 * @param {Function} constructor Constructor of a MessagePoster subclass
25 MessagePosterFactory
.prototype.register = function ( contentModel
, constructor ) {
26 if ( this.contentModelToClass
[ contentModel
] !== undefined ) {
27 throw new Error( 'Content model "' + contentModel
+ '" is already registered' );
30 this.contentModelToClass
[ contentModel
] = constructor;
34 * Unregister a given content model.
35 * This is exposed for testing and should not normally be used.
37 * @param {string} contentModel Content model to unregister
39 MessagePosterFactory
.prototype.unregister = function ( contentModel
) {
40 delete this.contentModelToClass
[ contentModel
];
44 * Create a MessagePoster for given a title.
46 * A promise for this is returned. It works by determining the content model, then loading
47 * the corresponding module (which registers the MessagePoster class), and finally constructing
48 * an object for the given title.
50 * This does not require the message and should be called as soon as possible, so that the
51 * API and ResourceLoader requests run in the background.
53 * @param {mw.Title} title Title that will be posted to
54 * @param {string} [apiUrl] api.php URL if the title is on another wiki
55 * @return {jQuery.Promise} Promise resolving to a mw.messagePoster.MessagePoster.
56 * For failure, rejected with up to three arguments:
58 * - errorCode Error code string
59 * - error Error explanation
60 * - details Further error details
62 MessagePosterFactory
.prototype.create = function ( title
, apiUrl
) {
64 api
= apiUrl
? new mw
.ForeignApi( apiUrl
) : new mw
.Api();
70 titles
: title
.getPrefixedDb()
71 } ).then( function ( data
) {
72 var pageId
, page
, contentModel
, moduleName
;
73 if ( !data
.query
.pageids
[ 0 ] ) {
74 return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' );
76 pageId
= data
.query
.pageids
[ 0 ];
77 page
= data
.query
.pages
[ pageId
];
79 contentModel
= page
.contentmodel
;
80 moduleName
= 'mediawiki.messagePoster.' + contentModel
;
81 return mw
.loader
.using( moduleName
).then( function () {
82 return factory
.createForContentModel(
88 return $.Deferred().reject( 'failed-to-load-module', 'Failed to load "' + moduleName
+ '"' );
90 }, function ( error
, details
) {
91 return $.Deferred().reject( 'content-model-query-failed', error
, details
);
96 * Creates a MessagePoster instance, given a title and content model
99 * @param {string} contentModel Content model of title
100 * @param {mw.Title} title Title being posted to
101 * @param {mw.Api} api mw.Api instance that the instance should use
102 * @return {mw.messagePoster.MessagePoster}
105 MessagePosterFactory
.prototype.createForContentModel = function ( contentModel
, title
, api
) {
106 return new this.contentModelToClass
[ contentModel
]( title
, api
);
110 factory
: new MessagePosterFactory()
112 }( mediaWiki
, jQuery
) );