Add missing validation for empty parameter names

Note this will make the templatedata API temporarily fail for
templates that are affected by this, until their documentation is
fixed. I think we have two ways to proceed here:

1. Thanks to Ie572809 we can change the API module to not do the
validation again. This allows us to make the validation more strict,
but the API module will continue to deliver the same data as before
until the parser cache is invalidated.

2. We accept it and merge this patch as it is. The problem is
extremely rare and easy to fix.

Bug: T333826
Change-Id: I16c7cc2328c47dde196e2dc07edb2eace33a624f
This commit is contained in:
thiemowmde 2023-04-11 18:20:31 +02:00 committed by Thiemo Kreuz (WMDE)
parent 0298a2d116
commit b632162245
4 changed files with 10 additions and 0 deletions

View file

@ -47,6 +47,7 @@
"templatedata-exists-on-related-page": "Please note: there is already a template data block on the related page \"[[$1]]\".",
"templatedata-helplink": "Information about template data",
"templatedata-helplink-target": "//www.mediawiki.org/wiki/Special:MyLanguage/Help:TemplateData",
"templatedata-invalid-unnamed-parameter": "Parameter names cannot be empty. To document unnamed parameters use their internal numbers \"1\", \"2\", and so on.",
"templatedata-invalid-duplicate-value": "Property \"$1\" (\"$3\") is a duplicate of \"$2\".",
"templatedata-invalid-empty-array": "Property \"$1\" must have at least one value in its array.",
"templatedata-invalid-format": "Property \"$1\" is expected to be \"inline\", \"block\", or a valid format string.",

View file

@ -60,6 +60,7 @@
"templatedata-exists-on-related-page": "A messaged that is displayed when there already is a templatedata string in a related page. $1: The page where templatedata already exists.",
"templatedata-helplink": "The label of the link to the TemplateData documentation, appearing above the editor field.",
"templatedata-helplink-target": "{{ignore}} The target of the link to the TemplateData documentation",
"templatedata-invalid-unnamed-parameter": "Error message when a parameter name is empty.",
"templatedata-invalid-duplicate-value": "Displayed when an array that must only contain unique values contains a duplicate.\n* $1 - name of property containing the duplicate\n* $2 - name of property with first occurrence of value\n* $3 - the value being duplicated",
"templatedata-invalid-empty-array": "Error message when a property that must be non-empty is empty. Parameters:\n* $1 - property name (\"paramOrder\" or \"sets.{$setNr}.params\")",
"templatedata-invalid-format": "{{Doc-important|Don't translate \"inline\" and \"block\".}}\n\nError message when format property gets an unexpected value.",

View file

@ -123,6 +123,10 @@ class TemplateDataValidator {
*/
private function validateParameters( stdClass $params ): ?Status {
foreach ( $params as $paramName => $param ) {
if ( trim( $paramName ) === '' ) {
return Status::newFatal( 'templatedata-invalid-unnamed-parameter' );
}
if ( !( $param instanceof stdClass ) ) {
return Status::newFatal( 'templatedata-invalid-type', "params.{$paramName}",
'object' );

View file

@ -87,6 +87,10 @@ class TemplateDataBlobTest extends MediaWikiIntegrationTestCase {
'input' => '{ "params": [] }',
'status' => '(templatedata-invalid-type: params, object)',
],
[
'input' => '{ "params": { "": {} } }',
'status' => '(templatedata-invalid-unnamed-parameter)',
],
[
'input' => '{ "params": { "a": [] } }',
'status' => '(templatedata-invalid-type: params.a, object)',