jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/343244 )
Change subject: Add follow_redirects query param to the wt2html endpoint in dev
API
......................................................................
Add follow_redirects query param to the wt2html endpoint in dev API
* We need this to make visual diff testing more useful. Looks like
almost 1 in 3 titles added to the test database is a redirect
which makes all those titles useless for visual diff testing.
Change-Id: I7d8727476a9a05bfc87a6f32ece2f61d357df5c7
---
M lib/api/apiUtils.js
M lib/api/routes.js
2 files changed, 64 insertions(+), 25 deletions(-)
Approvals:
jenkins-bot: Verified
Arlolra: Looks good to me, approved
diff --git a/lib/api/apiUtils.js b/lib/api/apiUtils.js
index 8c77f20..97502c6 100644
--- a/lib/api/apiUtils.js
+++ b/lib/api/apiUtils.js
@@ -425,38 +425,67 @@
});
};
+apiUtils._redirect = function(req, res, target, processRedirect) {
+ var locals = res.locals;
+ var path = processRedirect([
+ '',
+ locals.env.conf.parsoid.mwApiMap.get(locals.iwp).domain,
+ 'v3',
+ 'page',
+ locals.opts.format,
+ encodeURIComponent(target),
+ ].join('/'));
+
+ // Don't cache redirect requests
+ apiUtils.setHeader(res, 'Cache-Control', 'private,no-cache,s-maxage=0');
+ apiUtils.relativeRedirect(res, path);
+};
+
/**
* @method
* @param {Request} req
* @param {Response} res
*/
apiUtils.redirectToOldid = function(req, res) {
- var opts = res.locals.opts;
var env = res.locals.env;
- var metrics = env.conf.parsoid.metrics;
- var prefix = res.locals.iwp;
- var format = opts.format;
- var target = env.normalizeAndResolvePageTitle();
- var revid = env.page.meta.revision.revid;
- var path = [
- '',
- env.conf.parsoid.mwApiMap.get(prefix).domain,
- 'v3',
- 'page',
- format,
- encodeURIComponent(target),
- revid,
- ].join('/');
- if (Object.keys(req.query).length > 0) {
- path += '?' + qs.stringify(req.query);
- }
- env.log('info', 'redirecting to revision', revid, 'for', format);
- if (metrics) {
- metrics.increment('redirectToOldid.' + format.toLowerCase());
- }
- // Don't cache requests with no oldid
- apiUtils.setHeader(res, 'Cache-Control', 'private,no-cache,s-maxage=0');
- apiUtils.relativeRedirect(res, path);
+ return this._redirect(
+ req,
+ res,
+ env.normalizeAndResolvePageTitle(),
+ function(redirPath) {
+ var revid = env.page.meta.revision.revid;
+ redirPath += '/' + revid;
+ if (Object.keys(req.query).length > 0) {
+ redirPath += '?' + qs.stringify(req.query);
+ }
+ var format = res.locals.opts.format;
+ env.log('info', 'redirecting to revision', revid,
'for', format);
+ var metrics = env.conf.parsoid.metrics;
+ if (metrics) {
+ metrics.increment('redirectToOldid.' +
format.toLowerCase());
+ }
+ return redirPath;
+ }
+ );
+
+};
+
+/**
+ * @method
+ * @param {String} title
+ * @param {Request} req
+ * @param {Response} res
+ */
+apiUtils._redirectToPage = function(title, req, res) {
+ return this._redirect(
+ req,
+ res,
+ title,
+ function(path) {
+ res.locals.env.log('info', 'redirecting to ', path);
+ return path;
+ }
+ );
};
/**
diff --git a/lib/api/routes.js b/lib/api/routes.js
index 0e5c6f0..ef9cd6a 100644
--- a/lib/api/routes.js
+++ b/lib/api/routes.js
@@ -472,6 +472,15 @@
return apiUtils.redirectToOldid(req, res);
}
+ // Follow redirects if asked
+ if (env.conf.parsoid.devAPI &&
req.query.follow_redirects) {
+ var s = wikitext || env.page.src;
+ var redirMatch =
s.match(/^\s*#redirect\s\[\[([^\[\]]+)\]\]\s*/i);
+ if (redirMatch) {
+ return
apiUtils._redirectToPage(redirMatch[1], req, res);
+ }
+ }
+
env.log('info', 'started parsing');
var p2;
@@ -499,6 +508,7 @@
p2 = env.getContentHandler().toHTML(env);
} else if (oldid) {
+
// Indicate the MediaWiki revision in a header
as well for
// ease of extraction in clients.
apiUtils.setHeader(res, 'content-revision-id',
oldid);
--
To view, visit https://gerrit.wikimedia.org/r/343244
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I7d8727476a9a05bfc87a6f32ece2f61d357df5c7
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: C. Scott Ananian <[email protected]>
Gerrit-Reviewer: GWicke <[email protected]>
Gerrit-Reviewer: Subramanya Sastry <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits