build: Enable jscs rules 'requireSpacesInside*Brackets' and make pass
[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( targetHost, apiconfig ) {
20 this.date = undefined;
21 this.descriptions = [];
22 this.categories = [];
23
24 mw.ForeignUpload.call( this, targetHost, apiconfig );
25 }
26
27 OO.inheritClass( ForeignStructuredUpload, mw.ForeignUpload );
28
29 /**
30 * Add categories to the upload.
31 * @param {string[]} categories Array of categories to which this upload will be added.
32 */
33 ForeignStructuredUpload.prototype.addCategories = function ( categories ) {
34 var i, category;
35
36 for ( i = 0; i < categories.length; i++ ) {
37 category = categories[ i ];
38 this.categories.push( category );
39 }
40 };
41
42 /**
43 * Add a description to the upload.
44 * @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
45 * @param {string} description The description of the file.
46 */
47 ForeignStructuredUpload.prototype.addDescription = function ( language, description ) {
48 this.descriptions.push( {
49 language: language,
50 text: description
51 } );
52 };
53
54 /**
55 * Set the date of creation for the upload.
56 * @param {Date} date
57 */
58 ForeignStructuredUpload.prototype.setDate = function ( date ) {
59 this.date = date;
60 };
61
62 /**
63 * Get the text of the file page, to be created on upload. Brings together
64 * several different pieces of information to create useful text.
65 * @return {string}
66 */
67 ForeignStructuredUpload.prototype.getText = function () {
68 return (
69 '{{' +
70 this.getTemplateName() +
71 '\n|description=' +
72 this.getDescriptions() +
73 '\n|date=' +
74 this.getDate() +
75 '\n|source=' +
76 this.getUser() +
77 '\n|author=' +
78 this.getUser() +
79 '\n}}\n\n' +
80 this.getLicense() +
81 '\n\n' +
82 this.getCategories()
83 );
84 };
85
86 /**
87 * Gets the wikitext for the creation date of this upload.
88 * @private
89 * @return {string}
90 */
91 ForeignStructuredUpload.prototype.getDate = function () {
92 if ( !this.date ) {
93 return '';
94 }
95
96 return this.date.toString();
97 };
98
99 /**
100 * Gets the name of the template to use for creating the file metadata.
101 * Override in subclasses for other templates.
102 * @private
103 * @return {string}
104 */
105 ForeignStructuredUpload.prototype.getTemplateName = function () {
106 return 'Information';
107 };
108
109 /**
110 * Fetches the wikitext for any descriptions that have been added
111 * to the upload.
112 * @private
113 * @return {string}
114 */
115 ForeignStructuredUpload.prototype.getDescriptions = function () {
116 var i, desc, templateCalls = [];
117
118 for ( i = 0; i < this.descriptions.length; i++ ) {
119 desc = this.descriptions[ i ];
120 templateCalls.push( '{{' + desc.language + '|' + desc.text + '}}' );
121 }
122
123 return templateCalls.join( '\n' );
124 };
125
126 /**
127 * Fetches the wikitext for the categories to which the upload will
128 * be added.
129 * @private
130 * @return {string}
131 */
132 ForeignStructuredUpload.prototype.getCategories = function () {
133 var i, cat, categoryLinks = [];
134
135 for ( i = 0; i < this.categories.length; i++ ) {
136 cat = this.categories[ i ];
137 categoryLinks.push( '[[Category:' + cat + ']]' );
138 }
139
140 return categoryLinks.join( '\n' );
141 };
142
143 /**
144 * Gets the wikitext for the license of the upload. Abstract for now.
145 * @private
146 * @return {string}
147 */
148 ForeignStructuredUpload.prototype.getLicense = function () {
149 return '';
150 };
151
152 /**
153 * Get the username.
154 * @private
155 * @return {string}
156 */
157 ForeignStructuredUpload.prototype.getUser = function () {
158 return mw.config.get( 'wgUserName' );
159 };
160
161 mw.ForeignStructuredUpload = ForeignStructuredUpload;
162 }( mediaWiki, OO ) );