jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/404245 )

Change subject: Hygiene: move summary library functions to own lib file
......................................................................


Hygiene: move summary library functions to own lib file

There were a lot of summary functions residing in the general
mobile-util.js. I moved them all to a new library module.
Same for the currently single unit test. I hope the new structure will
make it more conducive to write more.

Change-Id: Ic3ade779bd059231e654c49046118ace20eb8e59
---
M lib/mobile-util.js
A lib/summary.js
M routes/summary.js
M test/lib/mobile-util/mobile-util-test.js
A test/lib/summary/summary.test.js
5 files changed, 151 insertions(+), 130 deletions(-)

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



diff --git a/lib/mobile-util.js b/lib/mobile-util.js
index ad68b28..8537433 100644
--- a/lib/mobile-util.js
+++ b/lib/mobile-util.js
@@ -1,13 +1,9 @@
 'use strict';
 
-const domino = require('domino');
 const underscore = require('underscore');
 const uuid = require('cassandra-uuid').TimeUuid;
 const HTTPError = require('./util').HTTPError;
-const transforms = require('./transforms');
 const mUtil = {};
-
-const NS_MAIN = 0;
 
 mUtil.CONTENT_TYPES = {
     mobileSections: { name: 'mobile-sections', version: '0.13.1' },
@@ -22,8 +18,6 @@
     onthisday: { name: 'onthisday', version: '0.3.3' },
     unpublished: { name: 'unpublished', version: '0.0.0' }
 };
-
-mUtil.SUMMARY_NS_WHITELIST = [ NS_MAIN ];
 
 mUtil.setContentType = function(res, spec, mainType = 'application/json') {
     if (!spec.name || !spec.version) {
@@ -143,122 +137,6 @@
         }
     });
     return request.uri;
-};
-
-/**
- * Builds a dictionary containing the various forms of a page title that a 
client may need.
- * @param {!Object} title a mediawiki-title Title object constructed from a 
page title string
- * @param {!Object} meta page metadata
- * @return {!Object} a set of useful page title strings
- */
-mUtil.buildTitleDictionary = function(title, meta) {
-    return {
-        canonical: title.getPrefixedDBKey(),
-        normalized: meta.normalizedtitle,
-        display: meta.displaytitle,
-    };
-};
-
-/**
- * Get the page summary type.
- * @param {!Object} meta page metadata from MW API
- * return {!String} the summary type (one of 'standard', 'disambiguation', or 
'mainpage')
- */
-mUtil.getSummaryType = function(meta) {
-    const isDisambiguationPage = meta.pageprops
-      && {}.hasOwnProperty.call(meta.pageprops, 'disambiguation');
-    if (meta.mainpage) {
-        return 'mainpage';
-    }
-    if (isDisambiguationPage) {
-        return 'disambiguation';
-    }
-    return 'standard';
-};
-
-/*
- * Build a page summary
- * @param {!String} domain the request domain
- * @param {!Object} title a mediawiki-title object for the page title
- * @param {!Object} page page content and metadata from Parsoid
- * @param {!Object} meta metadata from MW API
- * @return {!Object} a summary 2.0 spec-compliant page summary object
- */
-mUtil.buildSummary = function(domain, title, page, meta) {
-    const isContentModelWikitext = meta.contentmodel === 'wikitext';
-    const isWhiteListedNamespace = 
mUtil.SUMMARY_NS_WHITELIST.includes(meta.ns);
-    const isRedirect = meta.redirect;
-
-    if (!isContentModelWikitext) {
-        return { code: 204 };
-    }
-
-    if (!isWhiteListedNamespace) {
-        return { code: 204 };
-    }
-
-    if (isRedirect) {
-        return { code: 204 };
-    }
-
-    const leadText = domino.createDocument(page.sections[0].text);
-    const intro = transforms.extractLeadIntroduction(leadText);
-    const summary = intro.length ? transforms.summarize(intro) : { extract: 
'', extract_html: '' };
-
-    return Object.assign({
-        code: 200,
-        type : mUtil.getSummaryType(meta),
-        title: meta.normalizedtitle,
-        displaytitle: meta.displaytitle,
-        namespace: { id: meta.ns, text: meta.nsText },
-        titles: mUtil.buildTitleDictionary(title, meta),
-        pageid: meta.id,
-        thumbnail: meta.thumbnail,
-        originalimage: meta.originalimage,
-        lang: meta.lang,
-        dir: meta.dir,
-        revision: page.revision,
-        tid: page.tid,
-        timestamp: page.lastmodified,
-        description: meta.description,
-        coordinates: meta.geo && {
-            lat: meta.geo.latitude,
-            lon: meta.geo.longitude
-        },
-        content_urls: mUtil.buildContentUrls(domain, title, meta),
-        api_urls: mUtil.buildApiUrls(domain, title, meta),
-    }, summary);
-};
-
-mUtil.buildContentUrls = function(domain, title, meta) {
-    const mobileBaseUrl = meta.mobileHost;
-    return {
-        desktop: {
-            page: `https://${domain}/wiki/${title.getPrefixedDBKey()}`,
-            revisions: 
`https://${domain}/wiki/${title.getPrefixedDBKey()}?action=history`,
-            edit: 
`https://${domain}/wiki/${title.getPrefixedDBKey()}?action=edit`,
-            talk: meta.talkNsText && 
`https://${domain}/wiki/${meta.talkNsText}:${title.getKey()}`
-        },
-        mobile: mobileBaseUrl && {
-            page: `${mobileBaseUrl}/wiki/${title.getPrefixedDBKey()}`,
-            revisions: 
`${mobileBaseUrl}/wiki/Special:History/${title.getPrefixedDBKey()}`,
-            edit: 
`${mobileBaseUrl}/wiki/${title.getPrefixedDBKey()}?action=edit`,
-            talk: meta.talkNsText && 
`${mobileBaseUrl}/wiki/${meta.talkNsText}:${title.getKey()}`
-        }
-    };
-};
-
-mUtil.buildApiUrls = function(domain, title, meta) {
-    return {
-        summary: 
`https://${domain}/api/rest_v1/page/summary/${title.getPrefixedDBKey()}`,
-        // read_html: 
`https://${domain}/api/rest_v1/page/read-html/${title.getPrefixedDBKey()}`,
-        // content_html: 
`https://${domain}/api/rest_v1/page/content-html/${title.getPrefixedDBKey()}`,
-        // metadata: 
`https://${domain}/api/rest_v1/page/metadata/${title.getPrefixedDBKey()}`,
-        // references: 
`https://${domain}/api/rest_v1/page/references/${title.getPrefixedDBKey()}`,
-        // media: 
`https://${domain}/api/rest_v1/page/media/${title.getPrefixedDBKey()}`,
-        edit_html: 
`https://${domain}/api/rest_v1/page/html/${title.getPrefixedDBKey()}`,
-        talk_page_html: meta.talkNsText && 
`https://${domain}/api/rest_v1/page/html/${meta.talkNsText}:${title.getKey()}`
-    };
 };
 
 mUtil.throw404 = function(message) {
diff --git a/lib/summary.js b/lib/summary.js
new file mode 100644
index 0000000..59db23b
--- /dev/null
+++ b/lib/summary.js
@@ -0,0 +1,130 @@
+'use strict';
+
+const domino = require('domino');
+const transforms = require('./transforms');
+
+const NS_MAIN = 0;
+const SUMMARY_NS_WHITELIST = [ NS_MAIN ];
+
+/**
+ * Builds a dictionary containing the various forms of a page title that a 
client may need.
+ * @param {!Object} title a mediawiki-title Title object constructed from a 
page title string
+ * @param {!Object} meta page metadata
+ * @return {!Object} a set of useful page title strings
+ */
+function buildTitleDictionary(title, meta) {
+    return {
+        canonical: title.getPrefixedDBKey(),
+        normalized: meta.normalizedtitle,
+        display: meta.displaytitle,
+    };
+}
+
+/**
+ * Get the page summary type.
+ * @param {!Object} meta page metadata from MW API
+ * return {!String} the summary type (one of 'standard', 'disambiguation', or 
'mainpage')
+ */
+function getSummaryType(meta) {
+    const isDisambiguationPage = meta.pageprops
+        && {}.hasOwnProperty.call(meta.pageprops, 'disambiguation');
+    if (meta.mainpage) {
+        return 'mainpage';
+    }
+    if (isDisambiguationPage) {
+        return 'disambiguation';
+    }
+    return 'standard';
+}
+
+function buildContentUrls(domain, title, meta) {
+    const mobileBaseUrl = meta.mobileHost;
+    return {
+        desktop: {
+            page: `https://${domain}/wiki/${title.getPrefixedDBKey()}`,
+            revisions: 
`https://${domain}/wiki/${title.getPrefixedDBKey()}?action=history`,
+            edit: 
`https://${domain}/wiki/${title.getPrefixedDBKey()}?action=edit`,
+            talk: meta.talkNsText && 
`https://${domain}/wiki/${meta.talkNsText}:${title.getKey()}`
+        },
+        mobile: mobileBaseUrl && {
+            page: `${mobileBaseUrl}/wiki/${title.getPrefixedDBKey()}`,
+            revisions: 
`${mobileBaseUrl}/wiki/Special:History/${title.getPrefixedDBKey()}`,
+            edit: 
`${mobileBaseUrl}/wiki/${title.getPrefixedDBKey()}?action=edit`,
+            talk: meta.talkNsText && 
`${mobileBaseUrl}/wiki/${meta.talkNsText}:${title.getKey()}`
+        }
+    };
+}
+
+function buildApiUrls(domain, title, meta) {
+    return {
+        summary: 
`https://${domain}/api/rest_v1/page/summary/${title.getPrefixedDBKey()}`,
+        // read_html: 
`https://${domain}/api/rest_v1/page/read-html/${title.getPrefixedDBKey()}`,
+        // content_html: 
`https://${domain}/api/rest_v1/page/content-html/${title.getPrefixedDBKey()}`,
+        // metadata: 
`https://${domain}/api/rest_v1/page/metadata/${title.getPrefixedDBKey()}`,
+        // references: 
`https://${domain}/api/rest_v1/page/references/${title.getPrefixedDBKey()}`,
+        // media: 
`https://${domain}/api/rest_v1/page/media/${title.getPrefixedDBKey()}`,
+        edit_html: 
`https://${domain}/api/rest_v1/page/html/${title.getPrefixedDBKey()}`,
+        talk_page_html: meta.talkNsText && 
`https://${domain}/api/rest_v1/page/html/${meta.talkNsText}:${title.getKey()}`
+    };
+}
+
+/*
+ * Build a page summary
+ * @param {!String} domain the request domain
+ * @param {!Object} title a mediawiki-title object for the page title
+ * @param {!Object} page page content and metadata from Parsoid
+ * @param {!Object} meta metadata from MW API
+ * @return {!Object} a summary 2.0 spec-compliant page summary object
+ */
+function buildSummary(domain, title, page, meta) {
+    const isContentModelWikitext = meta.contentmodel === 'wikitext';
+    const isWhiteListedNamespace = SUMMARY_NS_WHITELIST.includes(meta.ns);
+    const isRedirect = meta.redirect;
+
+    if (!isContentModelWikitext) {
+        return { code: 204 };
+    }
+
+    if (!isWhiteListedNamespace) {
+        return { code: 204 };
+    }
+
+    if (isRedirect) {
+        return { code: 204 };
+    }
+
+    const leadText = domino.createDocument(page.sections[0].text);
+    const intro = transforms.extractLeadIntroduction(leadText);
+    const summary = intro.length ? transforms.summarize(intro) : { extract: 
'', extract_html: '' };
+
+    return Object.assign({
+        code: 200,
+        type : getSummaryType(meta),
+        title: meta.normalizedtitle,
+        displaytitle: meta.displaytitle,
+        namespace: { id: meta.ns, text: meta.nsText },
+        titles: buildTitleDictionary(title, meta),
+        pageid: meta.id,
+        thumbnail: meta.thumbnail,
+        originalimage: meta.originalimage,
+        lang: meta.lang,
+        dir: meta.dir,
+        revision: page.revision,
+        tid: page.tid,
+        timestamp: page.lastmodified,
+        description: meta.description,
+        coordinates: meta.geo && {
+            lat: meta.geo.latitude,
+            lon: meta.geo.longitude
+        },
+        content_urls: buildContentUrls(domain, title, meta),
+        api_urls: buildApiUrls(domain, title, meta),
+    }, summary);
+}
+
+module.exports = {
+    buildSummary,
+    testing: {
+        getSummaryType
+    }
+};
diff --git a/routes/summary.js b/routes/summary.js
index 8a3b058..21ad57b 100644
--- a/routes/summary.js
+++ b/routes/summary.js
@@ -1,5 +1,6 @@
 'use strict';
 
+const lib = require('../lib/summary');
 const BBPromise = require('bluebird');
 const mwapi = require('../lib/mwapi');
 const mUtil = require('../lib/mobile-util');
@@ -29,7 +30,7 @@
         siteinfo: mwapi.getSiteInfo(app, req)
     }).then((response) => {
         const title = Title.newFromText(req.params.title, response.siteinfo);
-        const summary = mUtil.buildSummary(req.params.domain, title, 
response.page, response.meta);
+        const summary = lib.buildSummary(req.params.domain, title, 
response.page, response.meta);
         res.status(summary.code);
         if (summary.code === 200) {
             delete summary.code;
diff --git a/test/lib/mobile-util/mobile-util-test.js 
b/test/lib/mobile-util/mobile-util-test.js
index 0665cf1..68db7e1 100644
--- a/test/lib/mobile-util/mobile-util-test.js
+++ b/test/lib/mobile-util/mobile-util-test.js
@@ -29,11 +29,4 @@
         const link = document.querySelector('a');
         assert.deepEqual(mUtil.extractDbTitleFromAnchor(link), 'My_db_title');
     });
-
-    it('getSummaryType identifies main and disambig pages, defaults to 
\'standard\'', () => {
-        assert.deepEqual(mUtil.getSummaryType({ "mainpage": true }), 
'mainpage');
-        assert.deepEqual(mUtil.getSummaryType({ "pageprops": { 
"disambiguation": "" } }),
-            'disambiguation');
-        assert.deepEqual(mUtil.getSummaryType({}), 'standard');
-    });
 });
diff --git a/test/lib/summary/summary.test.js b/test/lib/summary/summary.test.js
new file mode 100644
index 0000000..de56ba1
--- /dev/null
+++ b/test/lib/summary/summary.test.js
@@ -0,0 +1,19 @@
+'use strict';
+
+const assert = require('../../utils/assert.js');
+const unit = require('../../../lib/summary').testing;
+
+describe('lib:summary', () => {
+    describe('getSummaryType', () => {
+        it('identifies main page', () => {
+            assert.deepEqual(unit.getSummaryType({ "mainpage": true }), 
'mainpage');
+        });
+        it('identifies disambig page', () => {
+            assert.deepEqual(unit.getSummaryType({ "pageprops": { 
"disambiguation": "" } }),
+                'disambiguation');
+        });
+        it('defaults to "standard"', () => {
+            assert.deepEqual(unit.getSummaryType({}), 'standard');
+        });
+    });
+});

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic3ade779bd059231e654c49046118ace20eb8e59
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: BearND <[email protected]>
Gerrit-Reviewer: Fjalapeno <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Mholloway <[email protected]>
Gerrit-Reviewer: Mhurd <[email protected]>
Gerrit-Reviewer: Ppchelko <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to