Mholloway has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/391746 )

Change subject: Summary 2.0: handle disambiguation pages
......................................................................

Summary 2.0: handle disambiguation pages

Bug: T171052
Change-Id: Ic74b5ad42af065d3116ede183223d9a4a65aaf0f
---
M lib/mobile-util.js
M routes/mobile-sections.js
M spec.yaml
3 files changed, 97 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/mobileapps 
refs/changes/46/391746/1

diff --git a/lib/mobile-util.js b/lib/mobile-util.js
index 2c3433b..66e89c3 100644
--- a/lib/mobile-util.js
+++ b/lib/mobile-util.js
@@ -4,7 +4,9 @@
 const underscore = require('underscore');
 const uuid = require('cassandra-uuid').TimeUuid;
 const HTTPError = require('./util').HTTPError;
+const Title = require('mediawiki-title').Title;
 const transforms = require('./transforms');
+const api = require('./api-util');
 const mUtil = {};
 
 const NS_MAIN = 0;
@@ -223,9 +225,11 @@
  * @param {!String} domain the request domain
  * @param {!Object} title a mediawiki-title object for the page title
  * @param {!Object} pageData raw page data for the page
+ * @param {!Object} siteinfo siteinfo for the site
+ * @param {?Array} list of linked pages from a disambiguation page
  * @return {!Object} a summary 2.0 spec-compliant page summary object
  */
-mUtil.buildSummary = function(domain, title, pageData) {
+mUtil.buildSummary = function(domain, title, pageData, siteinfo, links) {
     let summary = {};
     const page = pageData.page;
     const meta = pageData.meta;
@@ -258,7 +262,7 @@
     }
     return Object.assign({
         code: 200,
-        type: 'standard',
+        type : links ? 'disambiguation' : 'standard',
         title: meta.normalizedtitle,
         displaytitle: meta.displaytitle,
         namespace: { id: meta.ns, text: meta.nsText },
@@ -278,6 +282,7 @@
         },
         content_urls: mUtil.buildContentUrls(domain, title, meta),
         api_urls: mUtil.buildApiUrls(domain, title, meta),
+        disambiguation_links: links
     }, summary);
 };
 
@@ -312,6 +317,21 @@
     };
 };
 
+mUtil.getDisambiguationLinks = function(app, domain, title, siteinfo) {
+    return api.mwApiGet(app, domain, {
+        action: 'query',
+        titles: title,
+        prop: 'links',
+        format: 'json',
+        formatversion: '2'
+    }).then((resp) => {
+        const result = resp.body;
+        const page = result && result.query && result.query.pages && 
result.query.pages[0];
+        const links = page && page.links;
+        return links ? links.map(l => Title.newFromText(l.title, 
siteinfo).getPrefixedDBKey()) : [];
+    });
+};
+
 mUtil.throw404 = function(message) {
     throw new HTTPError({
         status: 404,
diff --git a/routes/mobile-sections.js b/routes/mobile-sections.js
index b61484d..036afc6 100644
--- a/routes/mobile-sections.js
+++ b/routes/mobile-sections.js
@@ -404,21 +404,28 @@
 router.get('/summary/:title/:revision?/:tid?', (req, res) => {
     return BBPromise.props({
         title: mwapi.getTitleObj(app, req),
-        pageData: _collectRawPageData(req, false)
+        pageData: _collectRawPageData(req, false),
+        siteinfo: mwapi.getSiteInfo(app, req)
     }).then((response) => {
-        const summary = mUtil.buildSummary(req.params.domain, response.title, 
response.pageData);
-        if (summary) {
-            res.status(summary.code);
-            if (summary.code === 200) {
-                delete summary.code;
-                mUtil.setETag(res, summary.revision);
+        const pageProps = response.pageData.meta && 
response.pageData.meta.pageprops;
+        const disambiguation = pageProps && {}.hasOwnProperty.call(pageProps, 
'disambiguation');
+        return BBPromise.props({
+            title: response.title,
+            pageData: response.pageData,
+            links: disambiguation ? mUtil.getDisambiguationLinks(app, 
req.params.domain,
+                req.params.title, response.siteinfo) : undefined
+        }).then((response) => {
+            const result = mUtil.buildSummary(req.params.domain, 
response.title, response.pageData,
+                response.siteinfo, response.links);
+            res.status(result.code);
+            if (result.code === 200) {
+                delete result.code;
+                mUtil.setETag(res, result.revision);
                 mUtil.setContentType(res, mUtil.CONTENT_TYPES.summary);
-                res.send(JSON.stringify(summary));
+                res.send(result);
             }
             res.end();
-        } else {
-            res.status(404);
-        }
+        });
     });
 });
 
diff --git a/spec.yaml b/spec.yaml
index 484eea4..c16d1ad 100644
--- a/spec.yaml
+++ b/spec.yaml
@@ -723,6 +723,57 @@
                 talk_page_html: /.+/
               extract: /.+/
               extract_html: /.+/
+        - title: Get summary for 2nd Earl of Derby
+          request:
+            params:
+              domain: en.wikipedia.org
+              title: 2nd_Earl_of_Derby
+          response:
+            status: 200
+            headers:
+              etag: /.+/
+              content-type: /^application\/json/
+            body:
+              type: disambiguation
+              title: /.+/
+              displaytitle: /.+/
+              namespace:
+                id: /.+/
+                text: /.*/
+              titles:
+                canonical: /.+/
+                normalized: /.+/
+                display: /.+/
+              pageid: /.+/
+              lang: /.+/
+              dir: /.+/
+              revision: /.+/
+              tid: /.+/
+              timestamp: /.+/
+              description: /.+/
+              content_urls:
+                desktop:
+                  page: /.+/
+                  revisions: /.+/
+                  edit: /.+/
+                  talk: /.+/
+#                mobile:
+#                  page: /.+/
+#                  revisions: /.+/
+#                  edit: /.+/
+#                  talk: /.+/
+              api_urls:
+                summary: /.+/
+#                read_html: /.+/
+#                content_html: /.+/
+#                metadata: /.+/
+#                references: /.+/
+#                media: /.+/
+                edit_html: /.+/
+                talk_page_html: /.+/
+              disambiguation_links: [ /.+/ ]
+              extract: /.+/
+              extract_html: /.+/
 
   # from routes/mobile-sections.js
   /{domain}/v1/page/mobile-sections/{title}:
@@ -873,6 +924,7 @@
         description: Type of page summary
         enum:
           - standard
+          - disambiguation
       title:
         type: string
         description: The page title
@@ -937,6 +989,11 @@
 #          $ref: '#/definitions/content_urls'
       api_urls:
         $ref: '#/definitions/api_urls'
+      disambiguation_links:
+        type: array
+        description: linked pages, if the summarized page is a disambiguation 
page
+        items:
+          type: string
       coordinates:
         type: object
         description: The coordinates of the item

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic74b5ad42af065d3116ede183223d9a4a65aaf0f
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

Reply via email to