Mholloway has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/386396 )
Change subject: Hygiene: Simplify image URL rewriting and set building
......................................................................
Hygiene: Simplify image URL rewriting and set building
Change-Id: Ia2018f8a83c992c45f9930b00a6e78aa462c8547
---
M lib/mwapi.js
M test/lib/mwapi/image-test.js
2 files changed, 34 insertions(+), 29 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps
refs/changes/96/386396/1
diff --git a/lib/mwapi.js b/lib/mwapi.js
index 7d22e01..02993d4 100644
--- a/lib/mwapi.js
+++ b/lib/mwapi.js
@@ -319,45 +319,39 @@
return api.mwApiGet(app, req.params.domain, query);
};
-mwapi.scaledImageUrl = function(initialUrl, initialWidth, desiredWidth) {
- if (initialWidth > desiredWidth) {
- return initialUrl.replace(mwapi.WIDTH_IN_IMAGE_URL_REGEX,
`/${desiredWidth}px-`);
- } else {
- return initialUrl;
+/**
+ * Scales a single image thumbnail URL to another size, if possible.
+ * @param {!string} initialUrl the initial URL for an image
+ * example:
+ *
//upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cat_poster_1.jpg/640px-Cat_poster_1.jpg
+ * @param {!number} width the desired width
+ * @return {!string} an image URL, updated with the desired size if possible
+ */
+mwapi.scaledImageUrl = function(initialUrl, width) {
+ const match = mwapi.WIDTH_IN_IMAGE_URL_REGEX.exec(initialUrl);
+ if (match) {
+ const originalWidth = match[1];
+ if (originalWidth > width) {
+ return initialUrl.replace(mwapi.WIDTH_IN_IMAGE_URL_REGEX,
`/${width}px-`);
+ }
}
+ return initialUrl;
};
/**
* Builds a set of URLs for different sizes of an image based on the provided
array of widths.
- * @param {!string} initialUrl the initial URL for an image (caller already
checked for undefined)
- * example URL:
+ * @param {!string} initialUrl the initial URL for an image
+ * example:
*
//upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cat_poster_1.jpg/640px-Cat_poster_1.jpg
- * @param {!number[]} widths an array of desired widths for which to
construct URLs
+ * @param {!number[]} widths an array of desired widths for which to construct
URLs
+ * @return {!Object} an object containing the widths as keys, and the
corresponding image URLs as
+ * values
*/
mwapi.buildImageUrlSet = function(initialUrl, widths) {
- const match = mwapi.WIDTH_IN_IMAGE_URL_REGEX.exec(initialUrl);
const result = {};
- widths.sort((a, b) => {
- return a - b;
+ widths.forEach((width) => {
+ result[width] = mwapi.scaledImageUrl(initialUrl, width);
});
- if (match) {
- const initialWidth = match[1];
- if (initialWidth > widths[0]) {
- for (const i in widths) {
- if ({}.hasOwnProperty.call(widths, i)) {
- result[widths[i]] = mwapi.scaledImageUrl(initialUrl,
initialWidth, widths[i]);
- }
- }
- return result;
- }
- }
-
- // can't get a bigger size than smallest request size of 320 or don't know
the original size.
- for (const i in widths) {
- if ({}.hasOwnProperty.call(widths, i)) {
- result[widths[i]] = initialUrl;
- }
- }
return result;
};
diff --git a/test/lib/mwapi/image-test.js b/test/lib/mwapi/image-test.js
index 3b0f60c..774f281 100644
--- a/test/lib/mwapi/image-test.js
+++ b/test/lib/mwapi/image-test.js
@@ -2,12 +2,23 @@
const assert = require('../../utils/assert.js');
const buildUrls = require('../../../lib/mwapi').buildLeadImageUrls;
+const scale = require('../../../lib/mwapi').scaledImageUrl;
const path = '//upload.wikimedia.org/wikipedia/commons/thumb/a/a0';
const httpPath = 'http://upload.wikimedia.org/wikipedia/commons/thumb/a/a0';
const httpsPath = 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0';
describe('lib:mwapi:image', () => {
+ it('image URLs are rewritten if URL contains a size and the desired width
is smaller', () => {
+ const prefix = 'https://upload.wikimedia.org/wikipedia/commons';
+
assert.deepEqual(scale(`${prefix}/thumb/0/0b/Cat_poster_1.jpg/720px-Cat_poster_1.jpg`,
320),
+ `${prefix}/thumb/0/0b/Cat_poster_1.jpg/320px-Cat_poster_1.jpg`);
+
assert.deepEqual(scale(`${prefix}/thumb/0/0b/Cat_poster_1.jpg/120px-Cat_poster_1.jpg`,
320),
+ `${prefix}/thumb/0/0b/Cat_poster_1.jpg/120px-Cat_poster_1.jpg`);
+ assert.deepEqual(scale(`${prefix}/9/96/Vasskertentrance.jpg`, 320),
+ `${prefix}/9/96/Vasskertentrance.jpg`);
+ });
+
it('buildLeadImageUrls("a") should return all "a"s', () => {
assert.deepEqual(buildUrls('a'), {
320: 'a',
--
To view, visit https://gerrit.wikimedia.org/r/386396
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia2018f8a83c992c45f9930b00a6e78aa462c8547
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits