summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki/mediawiki.ForeignStructuredUpload.js
blob: dd28ddd4a59f9ee305d30664396cded24dfd9adb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
( function ( mw, OO ) {
	/**
	 * @class mw.ForeignStructuredUpload
	 * @extends mw.ForeignUpload
	 *
	 * Used to represent an upload in progress on the frontend.
	 *
	 * This subclass will upload to a wiki using a structured metadata
	 * system similar to (or identical to) the one on Wikimedia Commons.
	 *
	 * See <https://commons.wikimedia.org/wiki/Commons:Structured_data> for
	 * a more detailed description of how that system works.
	 *
	 * **TODO: This currently only supports uploads under CC-BY-SA 4.0,
	 * and should really have support for more licenses.**
	 *
	 * @inheritdoc
	 */
	function ForeignStructuredUpload( target, apiconfig ) {
		this.date = undefined;
		this.descriptions = [];
		this.categories = [];

		mw.ForeignUpload.call( this, target, apiconfig );
	}

	OO.inheritClass( ForeignStructuredUpload, mw.ForeignUpload );

	/**
	 * Add categories to the upload.
	 *
	 * @param {string[]} categories Array of categories to which this upload will be added.
	 */
	ForeignStructuredUpload.prototype.addCategories = function ( categories ) {
		var i, category;

		for ( i = 0; i < categories.length; i++ ) {
			category = categories[ i ];
			this.categories.push( category );
		}
	};

	/**
	 * Add a description to the upload.
	 *
	 * @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
	 * @param {string} description The description of the file.
	 */
	ForeignStructuredUpload.prototype.addDescription = function ( language, description ) {
		this.descriptions.push( {
			language: language,
			text: description
		} );
	};

	/**
	 * Set the date of creation for the upload.
	 *
	 * @param {Date} date
	 */
	ForeignStructuredUpload.prototype.setDate = function ( date ) {
		this.date = date;
	};

	/**
	 * Get the text of the file page, to be created on upload. Brings together
	 * several different pieces of information to create useful text.
	 *
	 * @return {string}
	 */
	ForeignStructuredUpload.prototype.getText = function () {
		return (
			'{{' +
			this.getTemplateName() +
			'\n|description=' +
			this.getDescriptions() +
			'\n|date=' +
			this.getDate() +
			'\n|source=' +
			this.getSource() +
			'\n|author=' +
			this.getUser() +
			'\n}}\n\n' +
			this.getLicense() +
			'\n\n' +
			this.getCategories()
		);
	};

	/**
	 * Gets the wikitext for the creation date of this upload.
	 *
	 * @private
	 * @return {string}
	 */
	ForeignStructuredUpload.prototype.getDate = function () {
		if ( !this.date ) {
			return '';
		}

		return this.date.toString();
	};

	/**
	 * Gets the name of the template to use for creating the file metadata.
	 * Override in subclasses for other templates.
	 *
	 * @private
	 * @return {string}
	 */
	ForeignStructuredUpload.prototype.getTemplateName = function () {
		return 'Information';
	};

	/**
	 * Fetches the wikitext for any descriptions that have been added
	 * to the upload.
	 *
	 * @private
	 * @return {string}
	 */
	ForeignStructuredUpload.prototype.getDescriptions = function () {
		var i, desc, templateCalls = [];

		for ( i = 0; i < this.descriptions.length; i++ ) {
			desc = this.descriptions[ i ];
			templateCalls.push( '{{' + desc.language + '|' + desc.text + '}}' );
		}

		return templateCalls.join( '\n' );
	};

	/**
	 * Fetches the wikitext for the categories to which the upload will
	 * be added.
	 *
	 * @private
	 * @return {string}
	 */
	ForeignStructuredUpload.prototype.getCategories = function () {
		var i, cat, categoryLinks = [];

		for ( i = 0; i < this.categories.length; i++ ) {
			cat = this.categories[ i ];
			categoryLinks.push( '[[Category:' + cat + ']]' );
		}

		return categoryLinks.join( '\n' );
	};

	/**
	 * Gets the wikitext for the license of the upload.
	 *
	 * @private
	 * @return {string}
	 */
	ForeignStructuredUpload.prototype.getLicense = function () {
		// Make sure this matches the messages for different targets in
		// mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm
		return this.target === 'shared' ? '{{self|cc-by-sa-4.0}}' : '';
	};

	/**
	 * Get the source. This should be some sort of localised text for "Own work".
	 *
	 * @private
	 * @return {string}
	 */
	ForeignStructuredUpload.prototype.getSource = function () {
		return '{{own}}';
	};

	/**
	 * Get the username.
	 *
	 * @private
	 * @return {string}
	 */
	ForeignStructuredUpload.prototype.getUser = function () {
		return mw.config.get( 'wgUserName' );
	};

	mw.ForeignStructuredUpload = ForeignStructuredUpload;
}( mediaWiki, OO ) );