Debian Codegraph @

resources/src/mediawiki.ForeignUpload.js

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