Merge "Fix executeTiming statsd metrics"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignStructuredUpload.js
1 ( function ( mw, OO ) {
2 /**
3 * @class mw.ForeignStructuredUpload
4 * @extends mw.ForeignUpload
5 *
6 * Used to represent an upload in progress on the frontend.
7 *
8 * This subclass will upload to a wiki using a structured metadata
9 * system similar to (or identical to) the one on Wikimedia Commons.
10 *
11 * See <https://commons.wikimedia.org/wiki/Commons:Structured_data> for
12 * a more detailed description of how that system works.
13 *
14 * **TODO: This currently only supports uploads under CC-BY-SA 4.0,
15 * and should really have support for more licenses.**
16 *
17 * @inheritdoc
18 */
19 function ForeignStructuredUpload( target, apiconfig ) {
20 this.date = undefined;
21 this.descriptions = [];
22 this.categories = [];
23
24 mw.ForeignUpload.call( this, target, apiconfig );
25 }
26
27 OO.inheritClass( ForeignStructuredUpload, mw.ForeignUpload );
28
29 /**
30 * Add categories to the upload.
31 *
32 * @param {string[]} categories Array of categories to which this upload will be added.
33 */
34 ForeignStructuredUpload.prototype.addCategories = function ( categories ) {
35 var i, category;
36
37 for ( i = 0; i < categories.length; i++ ) {
38 category = categories[ i ];
39 this.categories.push( category );
40 }
41 };
42
43 /**
44 * Empty the list of categories for the upload.
45 */
46 ForeignStructuredUpload.prototype.clearCategories = function () {
47 this.categories = [];
48 };
49
50 /**
51 * Add a description to the upload.
52 *
53 * @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
54 * @param {string} description The description of the file.
55 */
56 ForeignStructuredUpload.prototype.addDescription = function ( language, description ) {
57 this.descriptions.push( {
58 language: language,
59 text: description
60 } );
61 };
62
63 /**
64 * Empty the list of descriptions for the upload.
65 */
66 ForeignStructuredUpload.prototype.clearDescriptions = function () {
67 this.descriptions = [];
68 };
69
70 /**
71 * Set the date of creation for the upload.
72 *
73 * @param {Date} date
74 */
75 ForeignStructuredUpload.prototype.setDate = function ( date ) {
76 this.date = date;
77 };
78
79 /**
80 * Get the text of the file page, to be created on upload. Brings together
81 * several different pieces of information to create useful text.
82 *
83 * @return {string}
84 */
85 ForeignStructuredUpload.prototype.getText = function () {
86 return (
87 '== {{int:filedesc}} ==\n' +
88 '{{Information' +
89 '\n|description=' +
90 this.getDescriptions() +
91 '\n|date=' +
92 this.getDate() +
93 '\n|source=' +
94 this.getSource() +
95 '\n|author=' +
96 this.getUser() +
97 '\n}}\n\n' +
98 '== {{int:license-header}} ==\n' +
99 this.getLicense() +
100 '\n\n' +
101 this.getCategories()
102 );
103 };
104
105 /**
106 * Gets the wikitext for the creation date of this upload.
107 *
108 * @private
109 * @return {string}
110 */
111 ForeignStructuredUpload.prototype.getDate = function () {
112 if ( !this.date ) {
113 return '';
114 }
115
116 return this.date.toString();
117 };
118
119 /**
120 * Fetches the wikitext for any descriptions that have been added
121 * to the upload.
122 *
123 * @private
124 * @return {string}
125 */
126 ForeignStructuredUpload.prototype.getDescriptions = function () {
127 var i, desc, templateCalls = [];
128
129 for ( i = 0; i < this.descriptions.length; i++ ) {
130 desc = this.descriptions[ i ];
131 templateCalls.push( '{{' + desc.language + '|1=' + desc.text + '}}' );
132 }
133
134 return templateCalls.join( '\n' );
135 };
136
137 /**
138 * Fetches the wikitext for the categories to which the upload will
139 * be added.
140 *
141 * @private
142 * @return {string}
143 */
144 ForeignStructuredUpload.prototype.getCategories = function () {
145 var i, cat, categoryLinks = [];
146
147 if ( this.categories.length === 0 ) {
148 return '{{subst:unc}}';
149 }
150
151 for ( i = 0; i < this.categories.length; i++ ) {
152 cat = this.categories[ i ];
153 categoryLinks.push( '[[Category:' + cat + ']]' );
154 }
155
156 return categoryLinks.join( '\n' );
157 };
158
159 /**
160 * Gets the wikitext for the license of the upload.
161 *
162 * @private
163 * @return {string}
164 */
165 ForeignStructuredUpload.prototype.getLicense = function () {
166 // Make sure this matches the messages for different targets in
167 // mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm
168 return this.target === 'shared' ? '{{self|cc-by-sa-4.0}}' : '';
169 };
170
171 /**
172 * Get the source. This should be some sort of localised text for "Own work".
173 *
174 * @private
175 * @return {string}
176 */
177 ForeignStructuredUpload.prototype.getSource = function () {
178 return '{{own}}';
179 };
180
181 /**
182 * Get the username.
183 *
184 * @private
185 * @return {string}
186 */
187 ForeignStructuredUpload.prototype.getUser = function () {
188 var username, namespace;
189 // Do not localise, we don't know the language of target wiki
190 namespace = 'User';
191 username = mw.config.get( 'wgUserName' );
192 if ( !username ) {
193 // The user is not logged in locally. However, they might be logged in on the foreign wiki.
194 // We should record their username there. (If they're not logged in there either, this will
195 // record the IP address.) It's also possible that the user opened this dialog, got an error
196 // about not being logged in, logged in in another browser tab, then continued uploading.
197 username = '{{subst:REVISIONUSER}}';
198 }
199 return '[[' + namespace + ':' + username + '|' + username + ']]';
200 };
201
202 mw.ForeignStructuredUpload = ForeignStructuredUpload;
203 }( mediaWiki, OO ) );