Prevent leading hash from starting param names when guessing

Parameter names can be constructed with parser functions, so
we should not include these as raw parameter names. There
might be more characters to add here.

Bug: T203605
Change-Id: If57a9ed7edf1e881cd121d9a1bcb2e7455c04ec9
This commit is contained in:
Sam Wilson 2018-09-12 10:04:45 +08:00
parent e01b0bcea7
commit 79e5347d90
3 changed files with 17 additions and 10 deletions

View file

@ -922,7 +922,7 @@ class TemplateDataBlob {
*/ */
public static function getRawParams( $wikitext ) { public static function getRawParams( $wikitext ) {
// This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js // This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js
preg_match_all( '/{{3,}(.*?)[<|}]/m', $wikitext, $rawParams ); preg_match_all( '/{{3,}([^#]*?)[<|}]/m', $wikitext, $rawParams );
$params = []; $params = [];
$normalizedParams = []; $normalizedParams = [];
if ( isset( $rawParams[1] ) ) { if ( isset( $rawParams[1] ) ) {

View file

@ -167,7 +167,7 @@ mw.TemplateData.SourceHandler.prototype.extractParametersFromTemplateCode = func
paramNames = [], paramNames = [],
normalizedParamNames = [], normalizedParamNames = [],
// This regex matches the one in TemplateDataBlob.php // This regex matches the one in TemplateDataBlob.php
paramExtractor = /{{3,}(.*?)[<|}]/mg; paramExtractor = /{{3,}([^#]*?)[<|}]/mg;
while ( ( matches = paramExtractor.exec( templateCode ) ) !== null ) { while ( ( matches = paramExtractor.exec( templateCode ) ) !== null ) {
// This normalization process is repeated in PHP in TemplateDataBlob.php // This normalization process is repeated in PHP in TemplateDataBlob.php

View file

@ -1214,27 +1214,34 @@ class TemplateDataBlobTest extends MediaWikiTestCase {
public function provideGetRawParams() { public function provideGetRawParams() {
return [ return [
[ 'No params' => [
'Lorem ipsum {{tpl}}.', 'Lorem ipsum {{tpl}}.',
[] []
], ],
[ 'Two plain params' => [
'Lorem {{{name}}} ipsum', 'Lorem {{{name}}} ipsum {{{surname}}}',
[ 'name' => [] ] [ 'name' => [], 'surname' => [] ]
], ],
[ 'Param with multiple casing and default value' => [
'Lorem {{{name|{{{Name|Default name}}}}}} ipsum', 'Lorem {{{name|{{{Name|Default name}}}}}} ipsum',
[ 'name' => [] ] [ 'name' => [] ]
], ],
[ 'Param name contains comment' => [
'Lorem {{{name<!-- comment -->}}} ipsum', 'Lorem {{{name<!-- comment -->}}} ipsum',
[ 'name' => [] ] [ 'name' => [] ]
], ],
[ 'Letter-case and underscore-space normalization' => [
// Check for letter-case and underscore-space normalization.
'Lorem {{{First name|{{{first_name}}}}}} ipsum {{{first-Name}}}', 'Lorem {{{First name|{{{first_name}}}}}} ipsum {{{first-Name}}}',
[ 'First name' => [] ] [ 'First name' => [] ]
], ],
'Dynamic param name' => [
'{{{{{#if:{{{nominee|}}}|nominee|candidate}}|}}}',
[ 'nominee' => [] ]
],
'More complicated dynamic param name' => [
'{{{party{{#if:{{{party_election||}}}|_election||}}|}}}',
[ 'party_election' => [] ]
]
]; ];
} }
} }