Arlolra has uploaded a new change for review.

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

Change subject: [WIP] Add redlinks to a doc
......................................................................

[WIP] Add redlinks to a doc

 * Makes use of the suggestion here:
   https://phabricator.wikimedia.org/T39902#2209625

 * Since this is still a WIP, provides an internal qs param (_redlinks)
   to add them to wt2html requests.

   This probably belongs in html2html, as described here:
   https://phabricator.wikimedia.org/T39902#2240375

   However, it's not clear that there's a consensus on doing that yet.
   See the discussion here, https://phabricator.wikimedia.org/T114413

Bug: T39902
Change-Id: I263e6b1f1aea8b0217eb8d558537eca5cab5652c
---
M lib/api/apiUtils.js
M lib/api/routes.js
M lib/config/ParsoidConfig.js
M lib/mw/ApiRequest.js
4 files changed, 98 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/27/303827/1

diff --git a/lib/api/apiUtils.js b/lib/api/apiUtils.js
index 9b4c259..d844560 100644
--- a/lib/api/apiUtils.js
+++ b/lib/api/apiUtils.js
@@ -11,7 +11,10 @@
 var DU = require('../utils/DOMUtils.js').DOMUtils;
 var PegTokenizer = require('../wt2html/tokenizer.js').PegTokenizer;
 var Promise = require('../utils/promise.js');
-var PHPParseRequest = require('../mw/ApiRequest.js').PHPParseRequest;
+var ApiRequest = require('../mw/ApiRequest.js');
+
+var PHPParseRequest = ApiRequest.PHPParseRequest;
+var LinkRequest = ApiRequest.LinkRequest;
 
 
 /**
@@ -458,3 +461,30 @@
        });
        env.setCaches(expansions);
 };
+
+/**
+ * Add red links to a document.
+ *
+ * @method
+ * @param {Environment} env
+ * @param {Document} doc
+ * @param {String} [oldid]
+ */
+apiUtils.addRedLinks = function(env, doc, oldid) {
+       var title = env.normalizeAndResolvePageTitle();
+       return LinkRequest.promise(env, title, oldid)
+       .then(function(links) {
+               var missing = new Map();
+               Object.keys(links).forEach(function(pageid) {
+                       var l = links[pageid];
+                       missing.set(l.title, l.hasOwnProperty('missing'));
+               });
+               doc.body.querySelectorAll('a[rel~="mw:WikiLink"]')
+               .forEach(function(a) {
+                       var t = a.getAttribute('title');
+                       if (missing.get(t)) {
+                               a.setAttribute('class', 'new');
+                       }
+               });
+       });
+};
diff --git a/lib/api/routes.js b/lib/api/routes.js
index a4640e6..beddcfa 100644
--- a/lib/api/routes.js
+++ b/lib/api/routes.js
@@ -518,6 +518,13 @@
                                console.assert(false, 'Should be unreachable');
                        }
 
+                       // FIXME: This has no business being in this route.
+                       if (req.query.hasOwnProperty('_redlinks')) {
+                               p2 = p2.tap(function(doc) {
+                                       return apiUtils.addRedLinks(env, doc, 
res.locals.oldid);
+                               });
+                       }
+
                        return p2
                        // .timeout(REQ_TIMEOUT)
                        .then(function(doc) {
diff --git a/lib/config/ParsoidConfig.js b/lib/config/ParsoidConfig.js
index 39121c9..88eab4f 100644
--- a/lib/config/ParsoidConfig.js
+++ b/lib/config/ParsoidConfig.js
@@ -38,6 +38,8 @@
                        imgInfo: 40 * 1000,
                        // action=query&meta=siteinfo
                        configInfo: 40 * 1000,
+                       // action=query&generator=links
+                       link: 40 * 1000,
                        // Connection timeout setting for the http agent
                        connect: 5 * 1000,
                },
diff --git a/lib/mw/ApiRequest.js b/lib/mw/ApiRequest.js
index 3eb9c53..c0b7fcc 100644
--- a/lib/mw/ApiRequest.js
+++ b/lib/mw/ApiRequest.js
@@ -1096,6 +1096,63 @@
        }
 };
 
+/**
+ * Fetch links for a given title, or oldid.
+ *
+ * @param {MWParserEnvironment} env
+ * @param {String} title
+ * @param {String} oldid
+ */
+function LinkRequest(env, title, oldid) {
+       ApiRequest.call(this, env, title);
+       this.reqType = 'Link Request';
+
+       var apiargs = {
+               format: 'json',
+               action: 'query',
+               generator: 'links',
+       };
+
+       if (oldid) {
+               this.oldid = oldid;
+               apiargs.revids = oldid;
+       } else {
+               apiargs.titles = title;
+       }
+
+       this.requestOptions = {
+               method: 'GET',
+               qs: apiargs,
+               followRedirect: true,
+               uri: env.conf.wiki.apiURI,
+               timeout: env.conf.parsoid.timeouts.mwApi.link,
+       };
+
+       this.request(this.requestOptions, this._requestCB.bind(this));
+}
+
+// Inherit from ApiRequest
+util.inherits(LinkRequest, ApiRequest);
+
+// Function which returns a promise for the result of a link request.
+LinkRequest.promise = promiseFor(LinkRequest);
+
+LinkRequest.prototype._handleJSON = function(error, data) {
+       logAPIWarnings(this, data);
+
+       if (!error && !(data && data.query && data.query.pages)) {
+               error = this._errorObj(data, this.text, 'Missing data.pages.');
+       }
+
+       if (error) {
+               this.env.log('error', error);
+               this._processListeners(error, null);
+       } else {
+               this._processListeners(error, data.query.pages);
+       }
+};
+
+
 if (typeof module === "object") {
        module.exports.ConfigRequest = ConfigRequest;
        module.exports.TemplateRequest = TemplateRequest;
@@ -1104,6 +1161,7 @@
        module.exports.BatchRequest = BatchRequest;
        module.exports.ImageInfoRequest = ImageInfoRequest;
        module.exports.TemplateDataRequest = TemplateDataRequest;
+       module.exports.LinkRequest = LinkRequest;
        module.exports.DoesNotExistError = DoesNotExistError;
        module.exports.ParserError = ParserError;
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I263e6b1f1aea8b0217eb8d558537eca5cab5652c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>

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

Reply via email to