Mobrovac has submitted this change and it was merged.

Change subject: Hygiene: Consolidate lib/mwapi.js constants & functions in 
export object
......................................................................


Hygiene: Consolidate lib/mwapi.js constants & functions in export object

Change-Id: Ie15bd921c63ff32d4fcb1112268793f1b0edb0d5
---
M lib/mwapi.js
M test/lib/mwapi/image-test.js
2 files changed, 67 insertions(+), 82 deletions(-)

Approvals:
  Mobrovac: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/lib/mwapi.js b/lib/mwapi.js
index 1e968f8..25c8cfd 100644
--- a/lib/mwapi.js
+++ b/lib/mwapi.js
@@ -10,21 +10,23 @@
 var HTTPError = sUtil.HTTPError;
 var Title = require('mediawiki-title').Title;
 
-var API_QUERY_MAX_TITLES = 50;
-var DEFAULT_THUMB_WIDTH = 320;
+var mwapi = {};
 
-var CARD_THUMB_LIST_ITEM_SIZE = 320;
-var CARD_THUMB_FEATURE_SIZE = 640;
+mwapi.API_QUERY_MAX_TITLES = 50;
+mwapi.DEFAULT_THUMB_WIDTH = 320;
 
-var LEAD_IMAGE_S = 320;
-var LEAD_IMAGE_M = 640;
-var LEAD_IMAGE_L = 800;
-var LEAD_IMAGE_XL = 1024;
+mwapi.CARD_THUMB_LIST_ITEM_SIZE = 320;
+mwapi.CARD_THUMB_FEATURE_SIZE = 640;
 
-var WIDTH_IN_IMAGE_URL_REGEX = /\/(\d+)px-/;
+mwapi.LEAD_IMAGE_S = 320;
+mwapi.LEAD_IMAGE_M = 640;
+mwapi.LEAD_IMAGE_L = 800;
+mwapi.LEAD_IMAGE_XL = 1024;
+
+mwapi.WIDTH_IN_IMAGE_URL_REGEX = /\/(\d+)px-/;
 
 
-function checkForMobileviewInResponse(logger, response) {
+mwapi.checkForMobileviewInResponse = function(logger, response) {
     if (!(response && response.body && response.body.mobileview)) {
         // we did not get our expected mobileview from the MW API, propagate 
that
 
@@ -49,9 +51,9 @@
             detail: response.body
         });
     }
-}
+};
 
-function checkForQueryPagesInResponse(req, response) {
+mwapi.checkForQueryPagesInResponse = function(req, response) {
     if (!(response && response.body && response.body.query && 
response.body.query.pages)) {
         // we did not get our expected query.pages from the MW API, propagate 
that
         req.logger.log('error/mwapi', 'no query.pages in response');
@@ -62,10 +64,10 @@
             detail: response.body
         });
     }
-}
+};
 
 // copied from restbase/lib/mwUtil.js
-function findSharedRepoDomain(siteInfoRes) {
+mwapi.findSharedRepoDomain = function(siteInfoRes) {
     var sharedRepo = (siteInfoRes.body.query.repos || []).find(function(repo) {
         return repo.name === 'shared';
     });
@@ -75,7 +77,7 @@
             return domainMatch[0];
         }
     }
-}
+};
 
 /**
  * Builds a request for siteinfo data for the MW site for the request domain.
@@ -84,7 +86,7 @@
  * @param {Object} req the request object
  * @return {Promise} a promise resolving as an JSON object containing the 
response
  */
-function getSiteInfo(app, req) {
+mwapi.getSiteInfo = function(app, req) {
     var query = {
         action: 'query',
         format: 'json',
@@ -102,10 +104,10 @@
             },
             namespaces: res.body.query.namespaces,
             namespacealiases: res.body.query.namespacealiases,
-            sharedRepoRootURI: findSharedRepoDomain(res)
+            sharedRepoRootURI: mwapi.findSharedRepoDomain(res)
         };
     });
-}
+};
 
 /**
  * Builds the request to get page metadata from MW API action=mobileview.
@@ -114,21 +116,21 @@
  * @param {Object} req the request object
  * @return {Promise} a promise resolving as an JSON object containing the 
response
  */
-function getMetadata(app, req) {
+mwapi.getMetadata = function(app, req) {
     var query = {
         action: 'mobileview',
         format: 'json',
         formatversion: 2,
         page: req.params.title,
         prop: 
'languagecount|thumb|image|id|revision|description|lastmodified|normalizedtitle|displaytitle|protection|editable',
-        thumbsize: LEAD_IMAGE_XL
+        thumbsize: mwapi.LEAD_IMAGE_XL
     };
     return api.mwApiGet(app, req.params.domain, query)
     .then(function (response) {
-        checkForMobileviewInResponse(req.logger, response);
+        mwapi.checkForMobileviewInResponse(req.logger, response);
         return response;
     });
-}
+};
 
 /**
  * Builds the request to get all sections from MW API action=mobileview.
@@ -137,7 +139,7 @@
  * @param {Object} req the request object
  * @return {Promise} a promise resolving as an JSON object containing the 
response
  */
-function getAllSections(app, req) {
+mwapi.getAllSections = function(app, req) {
     var query = {
         action: 'mobileview',
         format: 'json',
@@ -147,14 +149,14 @@
         sections: 'all',
         sectionprop: 'toclevel|line|anchor',
         noheadings: true,
-        thumbsize: LEAD_IMAGE_XL
+        thumbsize: mwapi.LEAD_IMAGE_XL
     };
     return api.mwApiGet(app, req.params.domain, query)
     .then(function (response) {
-        checkForMobileviewInResponse(req.logger, response);
+        mwapi.checkForMobileviewInResponse(req.logger, response);
         return response;
     });
-}
+};
 
 /**
  * Requests an article extract.
@@ -163,7 +165,7 @@
  * @param {Object} req the request object
  * @return {Promise} a promise resolving as an JSON object containing the 
response
  */
-function requestExtract(app, req) {
+mwapi.requestExtract = function(app, req) {
     var query = {
         action: 'query',
         format: 'json',
@@ -173,11 +175,11 @@
         exsentences: 5, // see T59669 + T117082
         explaintext: true,
         piprop: 'thumbnail',
-        pithumbsize: DEFAULT_THUMB_WIDTH,
+        pithumbsize: mwapi.DEFAULT_THUMB_WIDTH,
         titles: req.params.title
     };
     return api.mwApiGet(app, req.params.domain, query);
-}
+};
 
 /**
  * Requests an article extract and its description.
@@ -186,7 +188,7 @@
  * @param {Object} req the request object
  * @return {Promise} a promise resolving as an JSON object containing the 
response
  */
-function requestExtractAndDescription(app, req) {
+mwapi.requestExtractAndDescription = function(app, req) {
     var query = {
         action: 'query',
         format: 'json',
@@ -196,24 +198,24 @@
         exsentences: 5, // see T59669 + T117082
         explaintext: true,
         piprop: 'thumbnail',
-        pithumbsize: CARD_THUMB_FEATURE_SIZE,
+        pithumbsize: mwapi.CARD_THUMB_FEATURE_SIZE,
         wbptterms: 'description',
         titles: req.params.title
       };
       return api.mwApiGet(app, req.params.domain, query);
-}
+};
 
-function getRevisionFromExtract(extractObj) {
+mwapi.getRevisionFromExtract = function(extractObj) {
     return extractObj.revisions[0].revid;
-}
+};
 
-function buildTitleResponse(pageObj) {
+mwapi.buildTitleResponse = function(pageObj) {
     return {
         title: pageObj.title
     };
-}
+};
 
-function buildSummaryResponse(extractObj, dbtitle) {
+mwapi.buildSummaryResponse = function(extractObj, dbtitle) {
     return {
         title: dbtitle,
         normalizedtitle: extractObj.title,
@@ -221,32 +223,32 @@
         description: extractObj.terms && extractObj.terms.description[0],
         extract: extractObj.extract
     };
-}
+};
 
-function getFeedPageListMetadata(app, req, titlesList, needFeatureImage) {
+mwapi.getFeedPageListMetadata = function(app, req, titlesList, 
needFeatureImage) {
     var query = {
         action: 'query',
         format: 'json',
         formatversion: 2,
         prop: 'pageimages|pageterms',
         piprop: 'thumbnail',
-        pilimit: API_QUERY_MAX_TITLES,
-        pithumbsize: needFeatureImage ? LEAD_IMAGE_XL : 
CARD_THUMB_LIST_ITEM_SIZE,
+        pilimit: mwapi.API_QUERY_MAX_TITLES,
+        pithumbsize: needFeatureImage ? mwapi.LEAD_IMAGE_XL : 
mwapi.CARD_THUMB_LIST_ITEM_SIZE,
         wbptterms: 'description',
         meta: 'siteinfo',
         siprop: 'general',
         titles: titlesList
     };
     return api.mwApiGet(app, req.params.domain, query);
-}
+};
 
-function scaledImageUrl(initialUrl, initialWidth, desiredWidth) {
+mwapi.scaledImageUrl = function(initialUrl, initialWidth, desiredWidth) {
     if (initialWidth > desiredWidth) {
-        return initialUrl.replace(WIDTH_IN_IMAGE_URL_REGEX, "/" + desiredWidth 
+ "px-");
+        return initialUrl.replace(mwapi.WIDTH_IN_IMAGE_URL_REGEX, "/" + 
desiredWidth + "px-");
     } else {
         return initialUrl;
     }
-}
+};
 
 /**
  * Builds a set of URLs for different sizes of an image based on the provided 
array of widths.
@@ -254,8 +256,8 @@
  *   example URL: 
//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
  */
-function buildImageUrlSet(initialUrl, widths) {
-    var match = WIDTH_IN_IMAGE_URL_REGEX.exec(initialUrl), result = {};
+mwapi.buildImageUrlSet = function(initialUrl, widths) {
+    var match = mwapi.WIDTH_IN_IMAGE_URL_REGEX.exec(initialUrl), result = {};
     widths.sort(function(a, b) {
         return a - b;
     });
@@ -263,7 +265,7 @@
         var initialWidth = match[1];
         if (initialWidth > widths[0]) {
             for (let i in widths) {
-                result[widths[i]] = scaledImageUrl(initialUrl, initialWidth, 
widths[i]);
+                result[widths[i]] = mwapi.scaledImageUrl(initialUrl, 
initialWidth, widths[i]);
             }
             return result;
         }
@@ -274,15 +276,15 @@
         result[widths[i]] = initialUrl;
     }
     return result;
-}
+};
 
 /**
  * Builds a set of URLs for lead images with different sizes based on common 
bucket widths: 320, 640, 800, 1024.
  */
-function buildLeadImageUrls(initialUrl) {
-    return buildImageUrlSet(initialUrl, [ LEAD_IMAGE_S, LEAD_IMAGE_M,
-                                          LEAD_IMAGE_L, LEAD_IMAGE_XL ]);
-}
+mwapi.buildLeadImageUrls = function(initialUrl) {
+    return mwapi.buildImageUrlSet(initialUrl, [ mwapi.LEAD_IMAGE_S, 
mwapi.LEAD_IMAGE_M,
+                                                mwapi.LEAD_IMAGE_L, 
mwapi.LEAD_IMAGE_XL ]);
+};
 
 /**
  * Get a Title object for a MW title string
@@ -290,8 +292,8 @@
  * TODO: This is copied nearly verbatim from restbase/lib/mwUtil.js.  Should we
  * put it somewhere we can share it?
  */
-function getTitleObj(app, req, title) {
-    return getSiteInfo(app, req)
+mwapi.getTitleObj = function(app, req, title) {
+    return mwapi.getSiteInfo(app, req)
     .then(function(siteInfo) {
         return Title.newFromText(title, siteInfo);
     })
@@ -304,29 +306,12 @@
             }
         });
     });
-}
+};
 
-function getDbTitle(app, req, title) {
-   return getTitleObj(app, req, title).then(function(response) {
+mwapi.getDbTitle = function(app, req, title) {
+   return mwapi.getTitleObj(app, req, title).then(function(response) {
        return response.getPrefixedDBKey();
    });
-}
-
-module.exports = {
-    API_QUERY_MAX_TITLES: API_QUERY_MAX_TITLES,
-    CARD_THUMB_FEATURE_SIZE: CARD_THUMB_FEATURE_SIZE,
-    getMetadata: getMetadata,
-    getAllSections: getAllSections,
-    buildLeadImageUrls: buildLeadImageUrls,
-    checkForQueryPagesInResponse: checkForQueryPagesInResponse,
-    requestExtract: requestExtract,
-    requestExtractAndDescription: requestExtractAndDescription,
-    getRevisionFromExtract: getRevisionFromExtract,
-    buildTitleResponse: buildTitleResponse,
-    buildSummaryResponse: buildSummaryResponse,
-    getFeedPageListMetadata: getFeedPageListMetadata,
-    getDbTitle: getDbTitle,
-
-    // VisibleForTesting
-    _buildLeadImageUrls: buildLeadImageUrls
 };
+
+module.exports = mwapi;
diff --git a/test/lib/mwapi/image-test.js b/test/lib/mwapi/image-test.js
index 161720e..5028083 100644
--- a/test/lib/mwapi/image-test.js
+++ b/test/lib/mwapi/image-test.js
@@ -7,7 +7,7 @@
     this.timeout(20000);
 
     it('buildLeadImageUrls("a") should return all "a"s', function () {
-        assert.deepEqual(mwapi._buildLeadImageUrls('a'), {
+        assert.deepEqual(mwapi.buildLeadImageUrls('a'), {
                 320: 'a',
                 640: 'a',
                 800: 'a',
@@ -17,7 +17,7 @@
     });
 
     it('buildLeadImageUrls with size 1024px', function () {
-        
assert.deepEqual(mwapi._buildLeadImageUrls('//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/1024px-Sun_in_February.jpg'),
 {
+        
assert.deepEqual(mwapi.buildLeadImageUrls('//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/1024px-Sun_in_February.jpg'),
 {
                 320: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/320px-Sun_in_February.jpg',
                 640: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/640px-Sun_in_February.jpg',
                 800: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/800px-Sun_in_February.jpg',
@@ -27,7 +27,7 @@
     });
 
     it('buildLeadImageUrls with size 555px should return size 320 for 320 and 
then 555 for rest', function () {
-        
assert.deepEqual(mwapi._buildLeadImageUrls('//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/555px-Sun_in_February.jpg'),
 {
+        
assert.deepEqual(mwapi.buildLeadImageUrls('//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/555px-Sun_in_February.jpg'),
 {
                 320: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/320px-Sun_in_February.jpg',
                 640: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/555px-Sun_in_February.jpg',
                 800: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/555px-Sun_in_February.jpg',
@@ -37,7 +37,7 @@
     });
 
     it('buildLeadImageUrls with size 750px should return size 320, 640, and 
then 750 for rest', function () {
-        
assert.deepEqual(mwapi._buildLeadImageUrls('//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/750px-Sun_in_February.jpg'),
 {
+        
assert.deepEqual(mwapi.buildLeadImageUrls('//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/750px-Sun_in_February.jpg'),
 {
                 320: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/320px-Sun_in_February.jpg',
                 640: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/640px-Sun_in_February.jpg',
                 800: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/750px-Sun_in_February.jpg',
@@ -46,7 +46,7 @@
         );
     });
     it('buildLeadImageUrls with size 200px should return size 200 for all 
URLs', function () {
-        
assert.deepEqual(mwapi._buildLeadImageUrls('//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/200px-Sun_in_February.jpg'),
 {
+        
assert.deepEqual(mwapi.buildLeadImageUrls('//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/200px-Sun_in_February.jpg'),
 {
                 320: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/200px-Sun_in_February.jpg',
                 640: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/200px-Sun_in_February.jpg',
                 800: 
'//upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Sun_in_February.jpg/200px-Sun_in_February.jpg',

-- 
To view, visit https://gerrit.wikimedia.org/r/298481
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie15bd921c63ff32d4fcb1112268793f1b0edb0d5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Mholloway <[email protected]>
Gerrit-Reviewer: BearND <[email protected]>
Gerrit-Reviewer: Dbrant <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Jhernandez <[email protected]>
Gerrit-Reviewer: Mhurd <[email protected]>
Gerrit-Reviewer: Mobrovac <[email protected]>
Gerrit-Reviewer: Niedzielski <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to