BearND has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/316734

Change subject: Hygiene: move news templates to separate file
......................................................................

Hygiene: move news templates to separate file

so we can also use it for the tests.

This is a follow-up of a comment in
Iac6c156a242b1cb545ed671338bf525a07e105b4.

Change-Id: I1c3c85f44e914be8e48a179a40609c8ef9ea5289
---
A etc/feed/news-sites.js
M lib/feed/news.js
M test/features/news/news.js
3 files changed, 39 insertions(+), 25 deletions(-)


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

diff --git a/etc/feed/news-sites.js b/etc/feed/news-sites.js
new file mode 100644
index 0000000..0cc07f9
--- /dev/null
+++ b/etc/feed/news-sites.js
@@ -0,0 +1,32 @@
+/**
+ * A hash map of (Wikipedia site) languages codes to objects with
+ * information to control how to get news items for this particular
+ * Wikipedia site.
+ * The object consists of a title and a selector.
+ * 1) The title is the page title used to scrape the news items from.
+ * 2) The selector is used to find a parent HTML element for <li> elements.
+ *    In most cases simply the first 'ul' suffices but there are exceptions,
+ *    often because the first <ul> contains template instructions instead
+ *    of the actual news items, or instead of a <ul> Parsoid uses a <div>.
+ */
+const NEWS_TEMPLATES = {
+    en: {title: 'Template:In_the_news', selector: 'ul[id^=mw]'},
+
+    da: {title: 'Skabelon:Forside_aktuelle_begivenheder', selector: 'div'},
+    de: {title: 'Wikipedia:Hauptseite/Aktuelles', selector: 'ul'},
+    el: {title: 'Πύλη:Τρέχοντα_γεγονότα/Επικεφαλίδες', selector: 'ul'},
+    es: {title: 'Portal:Actualidad', selector: 'ul'},
+    fi: {title: 'Malline:Uutisissa', selector: 'ul'},
+    fr: {title: 'Modèle:Accueil_actualité', selector: 'ul[id^=mw]'},
+    he: {title: 'תבנית:חדשות_ואקטואליה', selector: 'ul'},
+    ko: {title: '틀:새로_들어온_소식', selector: 'ul'},
+    no: {title: 'Mal:Aktuelt', selector: 'ul'},
+    pl: {title: 'Szablon:Aktualności', selector: 'ul:last-of-type'},
+    pt: {title: 'Portal:Eventos_atuais', selector: 'ul'},
+    ru: {title: 'Шаблон:Актуальные_события', selector: 'ul'},
+    sv: {title: 'Portal:Huvudsida/Aktuella händelser', selector: 'ul'},
+    vi: {title: 'Bản_mẫu:Tin_tức', selector: 'ul'},
+    zh: {title: 'Portal:新聞動態', selector: 'ul'}
+};
+
+module.exports = NEWS_TEMPLATES;
diff --git a/lib/feed/news.js b/lib/feed/news.js
index 55e9c5d..95ac537 100644
--- a/lib/feed/news.js
+++ b/lib/feed/news.js
@@ -7,26 +7,7 @@
 var mwapi = require('../mwapi');
 var parsoid = require('../parsoid-access');
 var HTTPError = require('../util').HTTPError;
-
-var newsTemplates = {
-    en: {title: 'Template:In_the_news', selector: 'ul[id^=mw]'},
-
-    da: {title: 'Skabelon:Forside_aktuelle_begivenheder', selector: 'div'},
-    de: {title: 'Wikipedia:Hauptseite/Aktuelles', selector: 'ul'},
-    el: {title: 'Πύλη:Τρέχοντα_γεγονότα/Επικεφαλίδες', selector: 'ul'},
-    es: {title: 'Portal:Actualidad', selector: 'ul'},
-    fi: {title: 'Malline:Uutisissa', selector: 'ul'},
-    fr: {title: 'Modèle:Accueil_actualité', selector: 'ul[id^=mw]'},
-    he: {title: 'תבנית:חדשות_ואקטואליה', selector: 'ul'},
-    ko: {title: '틀:새로_들어온_소식', selector: 'ul'},
-    no: {title: 'Mal:Aktuelt', selector: 'ul'},
-    pl: {title: 'Szablon:Aktualności', selector: 'ul:last-of-type'},
-    pt: {title: 'Portal:Eventos_atuais', selector: 'ul'},
-    ru: {title: 'Шаблон:Актуальные_события', selector: 'ul'},
-    sv: {title: 'Portal:Huvudsida/Aktuella händelser', selector: 'ul'},
-    vi: {title: 'Bản_mẫu:Tin_tức', selector: 'ul'},
-    zh: {title: 'Portal:新聞動態', selector: 'ul'}
-};
+var NEWS_TEMPLATES = require('../../etc/feed/news-sites');
 
 function removeFragment(href) {
     if (href.indexOf('#') > -1) {
@@ -49,7 +30,7 @@
 function promise(app, req) {
     var lang = req.params.domain.split('.')[0];
     var aggregated = !!req.query.aggregated;
-    if (!newsTemplates[lang]) {
+    if (!NEWS_TEMPLATES[lang]) {
         if (aggregated) {
             return BBPromise.resolve({ payload: undefined, meta: undefined });
         }
@@ -65,14 +46,14 @@
         payload: [],
         meta: {}
     };
-    req.params.title = newsTemplates[lang].title;
+    req.params.title = NEWS_TEMPLATES[lang].title;
     return parsoid.getParsoidHtml(app, req)
     .then(function (response) {
         result.meta.etag = parsoid.getRevisionFromEtag(response.headers);
 
         var linkTitles = [];
         var doc = domino.createDocument(response.body);
-        var newsList = doc.querySelector(newsTemplates[lang].selector);
+        var newsList = doc.querySelector(NEWS_TEMPLATES[lang].selector);
         var stories = newsList.getElementsByTagName('li');
 
         for (var j = 0, m = stories.length; j < m; j++) {
diff --git a/test/features/news/news.js b/test/features/news/news.js
index f2478fc..1f13982 100644
--- a/test/features/news/news.js
+++ b/test/features/news/news.js
@@ -7,6 +7,7 @@
 var server = require('../../utils/server');
 var headers = require('../../utils/headers');
 var constants = require('./constants');
+var NEWS_TEMPLATES = require('../../../etc/feed/news-sites');
 
 var hrefs = [
     '/Sport_of_athletics',
@@ -40,7 +41,7 @@
     this.timeout(20000);
 
     before(function () { return server.start(); });
-    [ 'da', 'de', 'el', 'en', 'es', 'fi', 'fr', 'he', 'ko', 'no', 'pl', 'pt', 
'ru', 'sv', 'vi', 'zh' ].forEach(function(lang) {
+    for (let lang in NEWS_TEMPLATES) {
         it(lang + ': should respond to GET request with expected headers, 
incl. CORS and CSP headers', function () {
             return headers.checkHeaders(server.config.uri + lang + 
'.wikipedia.org/v1/page/news',
                 'application/json');
@@ -60,7 +61,7 @@
                     });
                 });
         });
-    });
+    }
 
     it('unsupported language with aggregated=true should return 200', 
function() {
         return preq.get({

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1c3c85f44e914be8e48a179a40609c8ef9ea5289
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/mobileapps
Gerrit-Branch: master
Gerrit-Owner: BearND <bsitzm...@wikimedia.org>

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

Reply via email to