jenkins-bot has submitted this change and it was merged.

Change subject: Mark reference sections with isReferenceSection flag
......................................................................


Mark reference sections with isReferenceSection flag

When a section is a reference section add a flag for API consumers.
This is helpful for the lazy loading use case in MobileFrontend.

a follow up patch will expose reference sections in a separate endpoint

Bug: T146398
Change-Id: I2ed205781c65576f0b874491497268c4b5291442
---
A lib/transformations/markReferenceSections.js
M lib/transforms.js
M routes/mobile-sections.js
M test/features/mobile-sections/pagecontent.js
4 files changed, 78 insertions(+), 2 deletions(-)

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



diff --git a/lib/transformations/markReferenceSections.js 
b/lib/transformations/markReferenceSections.js
new file mode 100644
index 0000000..55e6549
--- /dev/null
+++ b/lib/transformations/markReferenceSections.js
@@ -0,0 +1,43 @@
+"use strict";
+
+/**
+ * Scan an array of sections and add an isReferenceSection property to any 
sections that
+ * contain references.
+ *
+ * @param {Array} sections to scan for references
+ * @param {Boolean} [removeText] whether to remove references from the section 
text after marking
+ */
+function markReferenceSections( sections, removeText ) {
+    var topHeadingLevel = sections[0].toclevel;
+    var lastTopLevelSection,
+      isReferenceSection = false;
+
+    function mark( from, to ) {
+        if ( isReferenceSection && from !== undefined ) {
+            // Mark all the sections between the last heading and this one as 
reference sections
+            sections.slice( from, to ).forEach( function ( section ) {
+                section.isReferenceSection = true;
+                if ( removeText ) {
+                    delete section.text;
+                }
+            } );
+        }
+    }
+
+    sections.forEach( function ( section, i ) {
+        var text = section.text;
+        if ( section.toclevel === topHeadingLevel ) {
+            mark( lastTopLevelSection, i );
+            // reset the top level section and begin the hunt for references 
again.
+            lastTopLevelSection = i;
+            isReferenceSection = false;
+        }
+        if ( text.indexOf( 'class="mw-references' ) > -1 || text.indexOf( 
'class="refbegin' ) > -1 ) {
+            isReferenceSection = true;
+        }
+    } );
+    // the last section may have been a reference section
+    mark( lastTopLevelSection, sections.length );
+}
+
+module.exports = markReferenceSections;
diff --git a/lib/transforms.js b/lib/transforms.js
index c20bfa9..aca4950 100644
--- a/lib/transforms.js
+++ b/lib/transforms.js
@@ -11,6 +11,7 @@
 var hideRedLinks = require('./transformations/hideRedLinks');
 var hideIPA = require('./transformations/hideIPA');
 var extractHatnotes = require('./transformations/extractHatnotes');
+var markReferenceSections = require('./transformations/markReferenceSections');
 
 var transforms = {};
 
@@ -259,5 +260,6 @@
 transforms._rmElementsWithSelectors = _rmElementsWithSelectors;
 
 transforms.extractHatnotes = extractHatnotes;
+transforms.markReferenceSections = markReferenceSections;
 
 module.exports = transforms;
diff --git a/routes/mobile-sections.js b/routes/mobile-sections.js
index 9498ee1..5cb300f 100644
--- a/routes/mobile-sections.js
+++ b/routes/mobile-sections.js
@@ -116,8 +116,14 @@
 }
 
 function buildRemaining(input) {
+    // don't repeat the first section in remaining
+    var sections = input.page.sections.slice(1);
+    // mark references sections with a flag (if no sections its a stub or main 
page)
+    if ( sections.length ) {
+        transforms.markReferenceSections( sections, false );
+    }
     return {
-        sections: input.page.sections.slice(1) // don't repeat the first 
section
+        sections: sections
     };
 }
 
diff --git a/test/features/mobile-sections/pagecontent.js 
b/test/features/mobile-sections/pagecontent.js
index 4e167dc..374e983 100644
--- a/test/features/mobile-sections/pagecontent.js
+++ b/test/features/mobile-sections/pagecontent.js
@@ -164,7 +164,32 @@
                 assert.contains(res.body.lead.sections[0].text, '<a 
href="/wiki/Sort_(C++)" title="Sort (C++)">');
             });
     });
-
+    it('Any sections that contain references should have a reference flag', 
function() {
+        return preq.get({uri: server.config.uri + 
'en.wikipedia.org/v1/page/mobile-sections/Barack_Obama'})
+            .then(function (res) {
+                assert.equal(res.status, 200);
+                res.body.remaining.sections.forEach(function(section) {
+                    if ( [ 'Notes and references', 'Notes', 'References', 
'Further reading' ].indexOf( section.line ) > -1 ) {
+                        assert.ok(section.isReferenceSection === true, 
section.line + ' should have a reference flag');
+                    } else {
+                        assert.ok(section.isReferenceSection === undefined, 
section.line + ' should have no reference flag');
+                    }
+                });
+            });
+    });
+    it('The last section can be marked as a reference section', function() {
+        return preq.get({uri: server.config.uri + 
'en.wikipedia.org/v1/page/mobile-sections/Vallejo_(ferry)'})
+            .then(function (res) {
+                assert.equal(res.status, 200);
+                res.body.remaining.sections.forEach(function(section) {
+                    if ( [ 'References' ].indexOf( section.line ) > -1 ) {
+                        assert.ok(section.isReferenceSection === true, 
section.line + ' should have a reference flag');
+                    } else {
+                        assert.ok(section.isReferenceSection === undefined, 
section.line + ' should have no reference flag');
+                    }
+                });
+            });
+    });
     it('Page with math formulas should load without error', function() {
         return preq.get({uri: server.config.uri + 
'de.wikipedia.org/v1/page/mobile-sections/Verallgemeinerter_Laplace-Operator'})
             .then(function (res) {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I2ed205781c65576f0b874491497268c4b5291442
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: BearND <bsitzm...@wikimedia.org>
Gerrit-Reviewer: Dbrant <dbr...@wikimedia.org>
Gerrit-Reviewer: Fjalapeno <cfl...@wikimedia.org>
Gerrit-Reviewer: GWicke <gwi...@wikimedia.org>
Gerrit-Reviewer: Jdlrobson <jrob...@wikimedia.org>
Gerrit-Reviewer: Jhernandez <jhernan...@wikimedia.org>
Gerrit-Reviewer: Mholloway <mhollo...@wikimedia.org>
Gerrit-Reviewer: Mhurd <mh...@wikimedia.org>
Gerrit-Reviewer: Mobrovac <mobro...@wikimedia.org>
Gerrit-Reviewer: Niedzielski <sniedziel...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to