summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/MultimediaViewer/resources/mmv/model/mmv.model.Repo.js
blob: d23ac74b7e449da52889157afd1dee5014dd9112 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * This file is part of the MediaWiki extension MultimediaViewer.
 *
 * MultimediaViewer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * MultimediaViewer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MultimediaViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

/* eslint-disable no-use-before-define */

( function ( mw, oo ) {
	/**
	 * Represents information about a single image repository
	 *
	 * @class mw.mmv.model.Repo
	 * @constructor
	 * @param {string} displayName
	 * @param {string} favIcon URL to the repo's favicon
	 * @param {boolean} isLocal
	 */
	function Repo(
		displayName,
		favIcon,
		isLocal
	) {
		/** @property {string} displayName Human-readable name of the repository */
		this.displayName = displayName;

		/** @property {string} favIcon An icon that represents the repository */
		this.favIcon = favIcon;

		/** @property {boolean} isLocal Whether the repository is the local wiki */
		this.isLocal = isLocal;
	}

	/**
	 * Creates a new object from repoInfo we found in an API response.
	 *
	 * @static
	 * @param {Object} repoInfo
	 * @return {mw.mmv.model.Repo}
	 */
	Repo.newFromRepoInfo = function ( repoInfo ) {
		if ( repoInfo.apiurl ) {
			return new ForeignApiRepo(
				repoInfo.displayname,
				repoInfo.favicon,
				false,
				repoInfo.apiurl,
				repoInfo.server,
				repoInfo.articlepath
			);
		} else if ( repoInfo.descBaseUrl ) {
			return new ForeignDbRepo(
				repoInfo.displayname,
				repoInfo.favicon,
				false,
				repoInfo.descBaseUrl
			);
		} else {
			return new Repo( repoInfo.displayname, repoInfo.favicon, repoInfo.local );
		}
	};

	/**
	 * Returns true if the repo is Wikimedia Commons.
	 *
	 * @return {boolean}
	 */
	Repo.prototype.isCommons = function () {
		// there does not seem to be a sane way to do this
		return this.displayName === 'Wikimedia Commons';
	};

	/**
	* Gets the article path for the repository.
	*
	* @param {boolean} absolute if true, the URL will be absolute (if false, it still might be)
	* @return {string} Replace $1 with the page name you want to link to.
	*/
	Repo.prototype.getArticlePath = function ( absolute ) {
		var articlePath = mw.config.get( 'wgArticlePath' );
		if ( absolute ) {
			articlePath = mw.config.get( 'wgServer' ) + articlePath;
		}
		return articlePath;
	};

	/**
	 * Gets the a link to the site where the image was uploaded to.
	 * This is a hack and might break for wikis with exotic config; unfortunately no
	 * better data is provided currently.
	 *
	 * @return {string}
	 */
	Repo.prototype.getSiteLink = function () {
		return this.getArticlePath( true ).replace( '$1', '' );
	};

	/**
	 * Represents information about a foreign API repository
	 *
	 * @class mw.mmv.model.ForeignApiRepo
	 * @extends mw.mmv.model.Repo
	 * @constructor
	 * @inheritdoc
	 * @param {string} displayName
	 * @param {string} favIcon
	 * @param {boolean} isLocal
	 * @param {string} apiUrl URL to the wiki's api.php
	 * @param {string} server Hostname for the wiki
	 * @param {string} articlePath Path to articles on the wiki, relative to the hostname.
	 */
	function ForeignApiRepo(
		displayName,
		favIcon,
		isLocal,
		apiUrl,
		server,
		articlePath
	) {
		Repo.call( this, displayName, favIcon, isLocal );

		/** @property {string} apiUrl URL to the wiki's api.php */
		this.apiUrl = apiUrl;

		/** @property {string} server Hostname for the wiki */
		this.server = server;

		/** @property {string} articlePath Path to articles on the wiki, relative to the hostname */
		this.articlePath = articlePath;

		/** @property {string} absoluteArticlePath Path to articles on the wiki, relative to nothing */
		this.absoluteArticlePath = server + articlePath;
	}

	oo.inheritClass( ForeignApiRepo, Repo );

	/**
	* @override
	* @inheritdoc
	*/
	ForeignApiRepo.prototype.getArticlePath = function () {
		return this.absoluteArticlePath;
	};

	/**
	* @override
	* @inheritdoc
	*/
	ForeignApiRepo.prototype.isCommons = function () {
		return /^(https?:)?\/\/commons.wikimedia.org/.test( this.server );
	};

	/**
	 * Represents information about a foreign, shared DB repository
	 *
	 * @class mw.mmv.model.ForeignDbRepo
	 * @extends mw.mmv.model.Repo
	 * @constructor
	 * @inheritdoc
	 * @param {string} displayName
	 * @param {string} favIcon
	 * @param {boolean} isLocal
	 * @param {string} descBaseUrl Base URL for description pages - should include the "File:" prefix or similar.
	 */
	function ForeignDbRepo(
		displayName,
		favIcon,
		isLocal,
		descBaseUrl
	) {
		Repo.call( this, displayName, favIcon, isLocal );

		/** @property {string} descBaseUrl Base URL for descriptions on the wiki - append a file's title to this to get the description page */
		this.descBaseUrl = descBaseUrl;
	}

	oo.inheritClass( ForeignDbRepo, Repo );

	/**
	* @override
	* @inheritdoc
	*/
	ForeignDbRepo.prototype.getArticlePath = function () {
		return this.descBaseUrl.replace( /[^/:]*:$/, '$1' );
	};

	/**
	* @override
	* @inheritdoc
	*/
	ForeignDbRepo.prototype.isCommons = function () {
		return /^(https?:)?\/\/commons.wikimedia.org/.test( this.descBaseUrl );
	};

	mw.mmv.model.Repo = Repo;
	mw.mmv.model.ForeignApiRepo = ForeignApiRepo;
	mw.mmv.model.ForeignDbRepo = ForeignDbRepo;
}( mediaWiki, OO ) );