jenkins-bot has submitted this change and it was merged.
Change subject: T51075: Handle template-generated DISPLAYTITLE and DEFAULTSORT.
......................................................................
T51075: Handle template-generated DISPLAYTITLE and DEFAULTSORT.
Use the `properties` key in the action API expandtemplates request so that
we can properly handle DISPLAYTITLE and DEFAULTSORT inside template-generated
content.
Added test cases to parserTests, but they are a bit of cheat: I had to
implement DISPLAYTITLE and DEFAULTSORT in ext.core.ParserFunctions to make
them work, and the tests don't actually exercise the same code path used
in production.
Added a slightly better mocha test, which expands `{{Lowercase title}}` on
enwiki. This is fragile if enwiki ever changes the {{Lowercase title}}
template.
Change-Id: I459d8b32b6a23f25192042b38a74c8860140e7ec
---
M lib/ext.core.ParserFunctions.js
M lib/mediawiki.ApiRequest.js
M tests/mocha/parse.js
M tests/parserTests.txt
4 files changed, 72 insertions(+), 8 deletions(-)
Approvals:
Arlolra: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/ext.core.ParserFunctions.js b/lib/ext.core.ParserFunctions.js
index 3713c06..9851a12 100644
--- a/lib/ext.core.ParserFunctions.js
+++ b/lib/ext.core.ParserFunctions.js
@@ -855,6 +855,25 @@
ParserFunctions.prototype.pf_talkpagename = function ( token, frame, cb, args
) {
cb( { tokens: [this.env.page.name.replace(/^[^:]:/, 'Talk:' ) || ''] }
);
};
+ParserFunctions.prototype.pf_defaultsort = function( token, frame, cb, args ) {
+ var key = args[0].k;
+ cb( { tokens: [
+ new defines.SelfclosingTagTk('meta', [
+ new defines.KV('property',
'mw:PageProp/categorydefaultsort'),
+ new defines.KV('content', key.trim()),
+ ]),
+ ] } );
+};
+ParserFunctions.prototype.pf_displaytitle = function( token, frame, cb, args )
{
+ var key = args[0].k;
+ cb( { tokens: [
+ new defines.SelfclosingTagTk('meta', [
+ new defines.KV('property', 'mw:PageProp/displaytitle'),
+ new defines.KV('content', key.trim()),
+ ]),
+ ] } );
+};
+
// TODO: #titleparts, SUBJECTPAGENAME, BASEPAGENAME. SUBPAGENAME, DEFAULTSORT
diff --git a/lib/mediawiki.ApiRequest.js b/lib/mediawiki.ApiRequest.js
index 3934058..6deb43c 100644
--- a/lib/mediawiki.ApiRequest.js
+++ b/lib/mediawiki.ApiRequest.js
@@ -366,7 +366,7 @@
var apiargs = {
format: 'json',
action: 'expandtemplates',
- prop: 'wikitext|categories',
+ prop: 'wikitext|categories|properties',
text: text
};
@@ -441,7 +441,7 @@
// Add the categories which were added by parser functions directly
// into the page and not as in-text links.
- if (data.expandtemplates.categories) {
+ if (Array.isArray(data.expandtemplates.categories)) {
for (var i in data.expandtemplates.categories) {
var category = data.expandtemplates.categories[i];
src += '\n[[Category:' + category['*'];
@@ -451,6 +451,14 @@
src += ']]';
}
}
+ // Ditto for page properties (like DISPLAYTITLE and DEFAULTSORT)
+ if (Array.isArray(data.expandtemplates.properties)) {
+ data.expandtemplates.properties.forEach(function(prop) {
+ if (prop.name==='displaytitle' ||
prop.name==='defaultsort') {
+ src += '\n{{'+prop.name.toUpperCase()+':' +
prop['*'] + '}}';
+ }
+ });
+ }
// Add the source to the cache
this.env.pageCache[this.text] = src;
diff --git a/tests/mocha/parse.js b/tests/mocha/parse.js
index 4efb501..eee121c 100644
--- a/tests/mocha/parse.js
+++ b/tests/mocha/parse.js
@@ -191,5 +191,30 @@
});
});
});
+ // T51075: This test actually fetches the template contents from
+ // enwiki, fully exercising the `expandtemplates` API, unlike
+ // the parserTests test for this functionality, which ends up
using
+ // our own (incomplete) parser functions implementation.
+ it('should handle template-generated page properties',
function() {
+ return parse('{{Lowercase
title}}{{{{echo|DEFAULTSORT}}:x}}', {
+ prefix: 'enwiki',
+ page_name: 'EBay'
+ }).then(function(doc) {
+ var els = doc.querySelectorAll('HEAD > TITLE');
+ els.length.should.equal(1);
+ els[0].textContent.should.equal('eBay');
+ doc.title.should.equal('eBay');
+ // now check the <meta> elements
+ els = doc.querySelectorAll('META[property]');
+ var o = {}, prop;
+ for (var i=0; i<els.length; i++) {
+ prop = els[i].getAttribute('property');
+ o.should.not.have.property(prop);
+ o[prop] =
els[i].getAttribute('content');
+ }
+
o['mw:PageProp/displaytitle'].should.equal('eBay');
+
o['mw:PageProp/categorydefaultsort'].should.equal('x');
+ });
+ });
});
});
diff --git a/tests/parserTests.txt b/tests/parserTests.txt
index cbd569f..8a0fef6 100644
--- a/tests/parserTests.txt
+++ b/tests/parserTests.txt
@@ -8808,12 +8808,18 @@
# From plwiki:PLOS_ONE
!! test
Parsoid: Page property magic word with magic word contents
-!! options
-parsoid
!! wikitext
{{DISPLAYTITLE:''{{PAGENAME}}''}}
-!! html
+!! html/parsoid
<meta property="mw:PageProp/displaytitle" content="Main Page" about="#mwt2"
typeof="mw:ExpandedAttrs"
data-mw='{"attribs":[[{"txt":"content"},{"html":"<i
data-parsoid=\"{&quot;dsr&quot;:[15,31,2,2]}\"><span about=\"#mwt1\"
typeof=\"mw:Transclusion\"
data-parsoid=\"{&quot;pi&quot;:[[]],&quot;dsr&quot;:[17,29,null,null]}\"
data-mw=\"{&quot;parts&quot;:[{&quot;template&quot;:{&quot;target&quot;:{&quot;wt&quot;:&quot;PAGENAME&quot;,&quot;function&quot;:&quot;pagename&quot;},&quot;params&quot;:{},&quot;i&quot;:0}}]}\">Main
Page</span></i>"}]]}'/>
+!! end
+
+!! test
+Parsoid: Template-generated DISPLAYTITLE
+!! wikitext
+{{{{echo|DISPLAYTITLE}}:Foo}}
+!! html/parsoid
+<meta property="mw:PageProp/displaytitle" content="Foo" about="#mwt1"
typeof="mw:Transclusion" data-parsoid='{"dsr":[0,29,null,null],"pi":[[]]}'
data-mw='{"parts":[{"template":{"target":{"wt":"{{echo|DISPLAYTITLE}}:Foo"},"params":{},"i":0}}]}'/>
!! end
!! test
@@ -13099,14 +13105,20 @@
!! test
Parsoid: Defaultsort
-!! options
-parsoid
!! wikitext
{{DEFAULTSORT:Foo}}
-!! html
+!! html/parsoid
<meta property="mw:PageProp/categorydefaultsort" content="Foo"/>
!! end
+!! test
+Parsoid: Defaultsort (template-generated)
+!! wikitext
+{{{{echo|DEFAULTSORT}}:Foo}}
+!! html/parsoid
+<meta property="mw:PageProp/categorydefaultsort" content="Foo" about="#mwt1"
typeof="mw:Transclusion" data-parsoid='{"dsr":[0,28,null,null],"pi":[[]]}'
data-mw='{"parts":[{"template":{"target":{"wt":"{{echo|DEFAULTSORT}}:Foo"},"params":{},"i":0}}]}'/>
+!! end
+
###
### Inter-language links
###
--
To view, visit https://gerrit.wikimedia.org/r/189994
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I459d8b32b6a23f25192042b38a74c8860140e7ec
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>
Gerrit-Reviewer: Arlolra <[email protected]>
Gerrit-Reviewer: Marcoil <[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