Merge "Let BagOStuff::merge() callbacks override the TTL"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignUpload.js
1 ( function ( mw, OO, $ ) {
2 /**
3 * @class mw.ForeignUpload
4 * @extends mw.Upload
5 *
6 * Used to represent an upload in progress on the frontend.
7 *
8 * Subclassed to upload to a foreign API, with no other goodies. Use
9 * this for a generic foreign image repository on your wiki farm.
10 *
11 * Note you can provide the {@link #target target} or not - if the first argument is
12 * an object, we assume you want the default, and treat it as apiconfig
13 * instead.
14 *
15 * @constructor
16 * @param {string} [target] Used to set up the target
17 * wiki. If not remote, this class behaves identically to mw.Upload (unless further subclassed)
18 * Use the same names as set in $wgForeignFileRepos for this. Also,
19 * make sure there is an entry in the $wgForeignUploadTargets array for this name.
20 * @param {Object} [apiconfig] Passed to the constructor of mw.ForeignApi or mw.Api, as needed.
21 */
22 function ForeignUpload( target, apiconfig ) {
23 var api,
24 validTargets = mw.config.get( 'wgForeignUploadTargets' ),
25 upload = this;
26
27 if ( typeof target === 'object' ) {
28 // target probably wasn't passed in, it must
29 // be apiconfig
30 apiconfig = target;
31 target = undefined;
32 }
33
34 // * Use the given `target` first;
35 // * If not given, fall back to default (first) ForeignUploadTarget;
36 // * If none is configured, fall back to local uploads.
37 this.target = target || validTargets[ 0 ] || 'local';
38
39 // Now we have several different options.
40 // If the local wiki is the target, then we can skip a bunch of steps
41 // and just return an mw.Api object, because we don't need any special
42 // configuration for that.
43 // However, if the target is a remote wiki, we must check the API
44 // to confirm that the target is one that this site is configured to
45 // support.
46 if ( this.target === 'local' ) {
47 // If local uploads were requested, but they are disabled, fail.
48 if ( !mw.config.get( 'wgEnableUploads' ) ) {
49 this.apiPromise = $.Deferred().reject( 'uploaddisabledtext' );
50 } else {
51 // We'll ignore the CORS and centralauth stuff if the target is
52 // the local wiki.
53 this.apiPromise = $.Deferred().resolve( new mw.Api( apiconfig ) );
54 }
55 } else {
56 api = new mw.Api();
57 this.apiPromise = api.get( {
58 action: 'query',
59 meta: 'filerepoinfo',
60 friprop: [ 'name', 'scriptDirUrl', 'canUpload' ]
61 } ).then( function ( data ) {
62 var i, repo,
63 repos = data.query.repos;
64
65 // First pass - try to find the passed-in target and check
66 // that it's configured for uploads.
67 for ( i in repos ) {
68 repo = repos[ i ];
69
70 // Skip repos that are not our target, or if they
71 // are the target, cannot be uploaded to.
72 if ( repo.name === upload.target && repo.canUpload === '' ) {
73 return new mw.ForeignApi(
74 repo.scriptDirUrl + '/api.php',
75 apiconfig
76 );
77 }
78 }
79
80 return $.Deferred().reject( 'upload-foreign-cant-upload' );
81 } );
82 }
83
84 // Build the upload object without an API - this class overrides the
85 // actual API call methods to wait for the apiPromise to resolve
86 // before continuing.
87 mw.Upload.call( this, null );
88 }
89
90 OO.inheritClass( ForeignUpload, mw.Upload );
91
92 /**
93 * @property {string} target
94 * Used to specify the target repository of the upload.
95 *
96 * If you set this to something that isn't 'local', you must be sure to
97 * add that target to $wgForeignUploadTargets in LocalSettings, and the
98 * repository must be set up to use CORS and CentralAuth.
99 *
100 * Most wikis use "shared" to refer to Wikimedia Commons, we assume that
101 * in this class and in the messages linked to it.
102 *
103 * Defaults to the first available foreign upload target,
104 * or to local uploads if no foreign target is configured.
105 */
106
107 /**
108 * @inheritdoc
109 */
110 ForeignUpload.prototype.getApi = function () {
111 return this.apiPromise;
112 };
113
114 /**
115 * Override from mw.Upload to make sure the API info is found and allowed
116 */
117 ForeignUpload.prototype.upload = function () {
118 var upload = this;
119 return this.apiPromise.then( function ( api ) {
120 upload.api = api;
121 return mw.Upload.prototype.upload.call( upload );
122 } );
123 };
124
125 /**
126 * Override from mw.Upload to make sure the API info is found and allowed
127 */
128 ForeignUpload.prototype.uploadToStash = function () {
129 var upload = this;
130 return this.apiPromise.then( function ( api ) {
131 upload.api = api;
132 return mw.Upload.prototype.uploadToStash.call( upload );
133 } );
134 };
135
136 mw.ForeignUpload = ForeignUpload;
137 }( mediaWiki, OO, jQuery ) );