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.api
= new mw
.Api();
12 this.contentModelToClass
= {};
15 OO
.initClass( MwMessagePosterFactory
);
17 // Note: This registration scheme is currently not compatible with LQT, since that doesn't
18 // have its own content model, just islqttalkpage. LQT pages will be passed to the wikitext
21 * Registers a MessagePoster subclass for a given content model.
23 * @param {string} contentModel Content model of pages this MessagePoster can post to
24 * @param {Function} messagePosterConstructor Constructor for MessagePoster
26 MwMessagePosterFactory
.prototype.register = function ( contentModel
, messagePosterConstructor
) {
27 if ( this.contentModelToClass
[contentModel
] !== undefined ) {
28 throw new Error( 'The content model \'' + contentModel
+ '\' is already registered.' );
31 this.contentModelToClass
[contentModel
] = messagePosterConstructor
;
35 * Unregisters a given content model
36 * This is exposed for testing and should not normally be needed.
38 * @param {string} contentModel Content model to unregister
40 MwMessagePosterFactory
.prototype.unregister = function ( contentModel
) {
41 delete this.contentModelToClass
[contentModel
];
45 * Creates a MessagePoster, given a title. A promise for this is returned.
46 * This works by determining the content model, then loading the corresponding
47 * module (which will register the MessagePoster class), and finally constructing it.
49 * This does not require the message and should be called as soon as possible, so it does the
50 * API and ResourceLoader requests in the background.
52 * @param {mw.Title} title Title that will be posted to
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
) {
61 var pageId
, page
, contentModel
, moduleName
,
64 return this.api
.get( {
68 titles
: title
.getPrefixedDb()
69 } ).then( function ( result
) {
70 if ( result
.query
.pageids
.length
> 0 ) {
71 pageId
= result
.query
.pageids
[0];
72 page
= result
.query
.pages
[pageId
];
74 contentModel
= page
.contentmodel
;
75 moduleName
= 'mediawiki.messagePoster.' + contentModel
;
76 return mw
.loader
.using( moduleName
).then( function () {
77 return factory
.createForContentModel(
82 return $.Deferred().reject( 'failed-to-load-module', 'Failed to load the \'' + moduleName
+ '\' module' );
85 return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' );
87 }, function ( errorCode
, details
) {
88 return $.Deferred().reject( 'content-model-query-failed', errorCode
, details
);
93 * Creates a MessagePoster instance, given a title and content model
97 * @param {string} contentModel Content model of title
98 * @param {mw.Title} title Title being posted to
99 * @return {mw.messagePoster.MessagePoster}
102 MwMessagePosterFactory
.prototype.createForContentModel = function ( contentModel
, title
) {
103 return new this.contentModelToClass
[contentModel
]( title
);
107 factory
: new MwMessagePosterFactory()
109 }( mediaWiki
, jQuery
) );