]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - resources/src/mediawiki.messagePoster/mediawiki.messagePoster.factory.js
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.git] / resources / src / mediawiki.messagePoster / mediawiki.messagePoster.factory.js
1 ( function ( mw, $ ) {
2         /**
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.
5          *
6          * @class mw.messagePoster.factory
7          * @singleton
8          */
9         function MessagePosterFactory() {
10                 this.contentModelToClass = {};
11         }
12
13         OO.initClass( MessagePosterFactory );
14
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
17         // MessagePoster.
18         /**
19          * Register a MessagePoster subclass for a given content model.
20          *
21          * @param {string} contentModel Content model of pages this MessagePoster can post to
22          * @param {Function} constructor Constructor of a MessagePoster subclass
23          */
24         MessagePosterFactory.prototype.register = function ( contentModel, constructor ) {
25                 if ( this.contentModelToClass[ contentModel ] !== undefined ) {
26                         throw new Error( 'Content model "' + contentModel + '" is already registered' );
27                 }
28
29                 this.contentModelToClass[ contentModel ] = constructor;
30         };
31
32         /**
33          * Unregister a given content model.
34          * This is exposed for testing and should not normally be used.
35          *
36          * @param {string} contentModel Content model to unregister
37          */
38         MessagePosterFactory.prototype.unregister = function ( contentModel ) {
39                 delete this.contentModelToClass[ contentModel ];
40         };
41
42         /**
43          * Create a MessagePoster for given a title.
44          *
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.
48          *
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.
51          *
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:
56          *
57          *   - errorCode Error code string
58          *   - error Error explanation
59          *   - details Further error details
60          */
61         MessagePosterFactory.prototype.create = function ( title, apiUrl ) {
62                 var factory = this,
63                         api = apiUrl ? new mw.ForeignApi( apiUrl ) : new mw.Api();
64
65                 return api.get( {
66                         formatversion: 2,
67                         action: 'query',
68                         prop: 'info',
69                         titles: title.getPrefixedDb()
70                 } ).then( function ( data ) {
71                         var contentModel, moduleName, page = data.query.pages[ 0 ];
72                         if ( !page ) {
73                                 return $.Deferred().reject( 'unexpected-response', 'Unexpected API response' );
74                         }
75                         contentModel = page.contentmodel;
76                         moduleName = 'mediawiki.messagePoster.' + contentModel;
77                         return mw.loader.using( moduleName ).then( function () {
78                                 return factory.createForContentModel(
79                                         contentModel,
80                                         title,
81                                         api
82                                 );
83                         }, function () {
84                                 return $.Deferred().reject( 'failed-to-load-module', 'Failed to load "' + moduleName + '"' );
85                         } );
86                 }, function ( error, details ) {
87                         return $.Deferred().reject( 'content-model-query-failed', error, details );
88                 } );
89         };
90
91         /**
92          * Creates a MessagePoster instance, given a title and content model
93          *
94          * @private
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}
99          */
100         MessagePosterFactory.prototype.createForContentModel = function ( contentModel, title, api ) {
101                 return new this.contentModelToClass[ contentModel ]( title, api );
102         };
103
104         mw.messagePoster = {
105                 factory: new MessagePosterFactory()
106         };
107 }( mediaWiki, jQuery ) );