2013-02-22 18:50:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2019-06-12 04:38:27 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2013-02-22 18:50:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the information about a template,
|
|
|
|
* coming from the JSON blob in the <templatedata> tags
|
|
|
|
* on wiki pages.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
class TemplateDataBlob {
|
|
|
|
/**
|
|
|
|
* @var stdClass
|
|
|
|
*/
|
2019-07-08 01:46:33 +00:00
|
|
|
protected $data;
|
2013-02-22 18:50:54 +00:00
|
|
|
|
|
|
|
/**
|
2016-01-27 04:36:45 +00:00
|
|
|
* @var string|null In-object cache for getJSON()
|
|
|
|
*/
|
2019-07-08 01:46:33 +00:00
|
|
|
protected $json = null;
|
2016-01-27 04:36:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Status Cache of TemplateDataBlob::parse
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
2019-07-08 01:46:33 +00:00
|
|
|
protected $status;
|
2013-02-22 18:50:54 +00:00
|
|
|
|
2018-07-12 22:30:53 +00:00
|
|
|
/**
|
2019-01-02 23:21:18 +00:00
|
|
|
* @var string[] Predefined formats for TemplateData to check against
|
2018-07-12 22:30:53 +00:00
|
|
|
*/
|
2019-07-08 01:46:33 +00:00
|
|
|
protected static $formats = [
|
2018-07-12 22:30:53 +00:00
|
|
|
'block' => "{{_\n| _ = _\n}}",
|
|
|
|
'inline' => '{{_|_=_}}',
|
|
|
|
];
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
/**
|
2019-07-08 01:46:33 +00:00
|
|
|
* Parse and validate passed JSON and create a blob handling
|
|
|
|
* instance.
|
2013-07-23 13:33:45 +00:00
|
|
|
* Accepts and handles user-provided data.
|
|
|
|
*
|
2019-07-08 01:46:33 +00:00
|
|
|
* @param IDatabase $db
|
2013-07-23 13:33:45 +00:00
|
|
|
* @param string $json
|
2015-01-10 03:05:53 +00:00
|
|
|
* @throws Exception
|
2019-07-08 01:46:33 +00:00
|
|
|
* @return TemplateDataBlob|TemplateDataCompressedBlob
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
2019-07-08 01:46:33 +00:00
|
|
|
public static function newFromJSON( $db, $json ) {
|
|
|
|
if ( $db->getType() === 'mysql' ) {
|
|
|
|
$tdb = new TemplateDataCompressedBlob( json_decode( $json ) );
|
|
|
|
} else {
|
|
|
|
$tdb = new TemplateDataBlob( json_decode( $json ) );
|
|
|
|
}
|
2013-07-23 13:33:45 +00:00
|
|
|
|
2013-04-22 19:56:28 +00:00
|
|
|
$status = $tdb->parse();
|
2013-02-22 18:50:54 +00:00
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
2016-01-27 04:36:45 +00:00
|
|
|
// Reset in-object caches
|
|
|
|
$tdb->json = null;
|
|
|
|
$tdb->jsonDB = null;
|
|
|
|
|
2013-10-24 16:34:04 +00:00
|
|
|
// If data is invalid, replace with the minimal valid blob.
|
|
|
|
// This is to make sure that, if something forgets to check the status first,
|
|
|
|
// we don't end up with invalid data in the database.
|
2013-04-22 19:56:28 +00:00
|
|
|
$tdb->data = new stdClass();
|
2013-10-24 16:34:04 +00:00
|
|
|
$tdb->data->description = null;
|
|
|
|
$tdb->data->params = new stdClass();
|
2016-03-01 17:10:23 +00:00
|
|
|
$tdb->data->format = null;
|
2016-05-10 00:00:14 +00:00
|
|
|
$tdb->data->sets = [];
|
2014-10-18 15:28:26 +00:00
|
|
|
$tdb->data->maps = new stdClass();
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
2013-04-22 19:56:28 +00:00
|
|
|
$tdb->status = $status;
|
|
|
|
return $tdb;
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:33:45 +00:00
|
|
|
/**
|
2019-07-08 01:46:33 +00:00
|
|
|
* Parse and validate passed JSON (possibly gzip-compressed) and create a blob handling
|
|
|
|
* instance.
|
2013-07-23 13:33:45 +00:00
|
|
|
*
|
2019-07-08 01:46:33 +00:00
|
|
|
* @param IDatabase $db
|
2013-07-23 13:33:45 +00:00
|
|
|
* @param string $json
|
2019-07-08 01:46:33 +00:00
|
|
|
* @return TemplateDataBlob or TemplateDataCompressedBlob
|
2013-07-23 13:33:45 +00:00
|
|
|
*/
|
2019-07-08 01:46:33 +00:00
|
|
|
public static function newFromDatabase( $db, $json ) {
|
2013-07-23 13:33:45 +00:00
|
|
|
// Handle GZIP compression. \037\213 is the header for GZIP files.
|
|
|
|
if ( substr( $json, 0, 2 ) === "\037\213" ) {
|
|
|
|
$json = gzdecode( $json );
|
|
|
|
}
|
2019-07-08 01:46:33 +00:00
|
|
|
return self::newFromJSON( $db, $json );
|
2013-07-23 13:33:45 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
/**
|
|
|
|
* Parse the data, normalise it and validate it.
|
|
|
|
*
|
2016-01-12 19:44:17 +00:00
|
|
|
* See Specification.md for the expected format of the JSON object.
|
2013-02-22 18:50:54 +00:00
|
|
|
* @return Status
|
|
|
|
*/
|
2019-07-08 01:46:33 +00:00
|
|
|
protected function parse() {
|
2013-02-22 18:50:54 +00:00
|
|
|
$data = $this->data;
|
|
|
|
|
2016-05-10 00:00:14 +00:00
|
|
|
static $rootKeys = [
|
2013-04-30 22:32:08 +00:00
|
|
|
'description',
|
2013-05-15 22:15:52 +00:00
|
|
|
'params',
|
2013-10-09 22:26:22 +00:00
|
|
|
'paramOrder',
|
2013-05-15 22:15:52 +00:00
|
|
|
'sets',
|
2014-10-18 15:28:26 +00:00
|
|
|
'maps',
|
2015-10-02 10:34:57 +00:00
|
|
|
'format',
|
2016-05-10 00:00:14 +00:00
|
|
|
];
|
2013-09-04 22:47:35 +00:00
|
|
|
|
2016-05-10 00:00:14 +00:00
|
|
|
static $paramKeys = [
|
2013-05-08 23:56:57 +00:00
|
|
|
'label',
|
2013-04-30 22:32:08 +00:00
|
|
|
'required',
|
2014-04-24 22:21:36 +00:00
|
|
|
'suggested',
|
2013-04-30 22:32:08 +00:00
|
|
|
'description',
|
2015-03-04 20:32:38 +00:00
|
|
|
'example',
|
2013-04-30 22:32:08 +00:00
|
|
|
'deprecated',
|
|
|
|
'aliases',
|
2014-09-02 15:29:33 +00:00
|
|
|
'autovalue',
|
2013-04-30 22:32:08 +00:00
|
|
|
'default',
|
|
|
|
'inherits',
|
2013-05-08 23:56:57 +00:00
|
|
|
'type',
|
2016-05-10 00:00:14 +00:00
|
|
|
];
|
2013-09-04 22:47:35 +00:00
|
|
|
|
2016-05-10 00:00:14 +00:00
|
|
|
static $types = [
|
2013-10-24 16:41:51 +00:00
|
|
|
'content',
|
2013-10-24 16:39:04 +00:00
|
|
|
'line',
|
2013-05-08 23:56:57 +00:00
|
|
|
'number',
|
2014-05-09 17:44:25 +00:00
|
|
|
'boolean',
|
2013-10-24 16:39:04 +00:00
|
|
|
'string',
|
2014-05-09 16:07:37 +00:00
|
|
|
'date',
|
2013-10-24 16:41:51 +00:00
|
|
|
'unbalanced-wikitext',
|
2013-10-24 16:39:04 +00:00
|
|
|
'unknown',
|
2015-09-17 19:39:18 +00:00
|
|
|
'url',
|
2013-10-24 16:39:04 +00:00
|
|
|
'wiki-page-name',
|
|
|
|
'wiki-user-name',
|
2014-04-24 21:55:59 +00:00
|
|
|
'wiki-file-name',
|
2015-09-17 19:28:07 +00:00
|
|
|
'wiki-template-name',
|
2016-05-10 00:00:14 +00:00
|
|
|
];
|
2013-10-24 16:39:04 +00:00
|
|
|
|
2016-05-10 00:00:14 +00:00
|
|
|
static $typeCompatMap = [
|
2013-10-24 16:39:04 +00:00
|
|
|
'string/line' => 'line',
|
|
|
|
'string/wiki-page-name' => 'wiki-page-name',
|
|
|
|
'string/wiki-user-name' => 'wiki-user-name',
|
2014-04-24 21:55:59 +00:00
|
|
|
'string/wiki-file-name' => 'wiki-file-name',
|
2016-05-10 00:00:14 +00:00
|
|
|
];
|
2013-04-30 22:32:08 +00:00
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
if ( $data === null ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-parse' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_object( $data ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'templatedata', 'object' );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $data as $key => $value ) {
|
2013-04-30 22:32:08 +00:00
|
|
|
if ( !in_array( $key, $rootKeys ) ) {
|
2013-02-22 18:50:54 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-unknown', $key );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:22:25 +00:00
|
|
|
// Root.description
|
2013-02-22 18:50:54 +00:00
|
|
|
if ( isset( $data->description ) ) {
|
2013-04-16 14:18:10 +00:00
|
|
|
if ( !is_object( $data->description ) && !is_string( $data->description ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'description', 'string|object' );
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
2013-04-16 14:18:10 +00:00
|
|
|
$data->description = self::normaliseInterfaceText( $data->description );
|
2013-02-22 18:50:54 +00:00
|
|
|
} else {
|
2013-05-08 23:56:57 +00:00
|
|
|
$data->description = null;
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 10:34:57 +00:00
|
|
|
// Root.format
|
2016-08-07 01:03:53 +00:00
|
|
|
if ( isset( $data->format ) && $data->format !== null ) {
|
2019-03-11 21:03:39 +00:00
|
|
|
$f = self::$formats[$data->format] ?? $data->format;
|
2016-08-07 01:03:53 +00:00
|
|
|
if (
|
|
|
|
!is_string( $f ) ||
|
2017-09-20 16:58:11 +00:00
|
|
|
!preg_match( '/^\n?\{\{ *_+\n? *\|\n? *_+ *= *_+\n? *\}\}\n?$/', $f )
|
2016-08-07 01:03:53 +00:00
|
|
|
) {
|
2015-10-02 10:34:57 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-format',
|
|
|
|
'format'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-01 17:10:23 +00:00
|
|
|
$data->format = null;
|
2015-10-02 10:34:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 21:22:25 +00:00
|
|
|
// Root.params
|
|
|
|
if ( !isset( $data->params ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing', 'params', 'object' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_object( $data->params ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'params', 'object' );
|
|
|
|
}
|
|
|
|
|
2013-04-30 22:32:08 +00:00
|
|
|
// Deep clone
|
|
|
|
// We need this to determine whether a property was originally set
|
|
|
|
// to decide whether 'inherits' will add it or not.
|
|
|
|
$unnormalizedParams = unserialize( serialize( $data->params ) );
|
2016-05-10 00:00:14 +00:00
|
|
|
$paramNames = [];
|
2013-04-30 22:32:08 +00:00
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
foreach ( $data->params as $paramName => $paramObj ) {
|
|
|
|
if ( !is_object( $paramObj ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}",
|
|
|
|
'object'
|
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $paramObj as $key => $value ) {
|
2013-04-30 22:32:08 +00:00
|
|
|
if ( !in_array( $key, $paramKeys ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-unknown',
|
|
|
|
"params.{$paramName}.{$key}"
|
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 23:56:57 +00:00
|
|
|
// Param.label
|
|
|
|
if ( isset( $paramObj->label ) ) {
|
|
|
|
if ( !is_object( $paramObj->label ) && !is_string( $paramObj->label ) ) {
|
2013-10-06 16:31:20 +00:00
|
|
|
// TODO: Also validate that the keys are valid lang codes and the values strings.
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.label",
|
|
|
|
'string|object'
|
|
|
|
);
|
2013-05-08 23:56:57 +00:00
|
|
|
}
|
|
|
|
$paramObj->label = self::normaliseInterfaceText( $paramObj->label );
|
|
|
|
} else {
|
|
|
|
$paramObj->label = null;
|
|
|
|
}
|
2013-04-30 21:22:25 +00:00
|
|
|
|
|
|
|
// Param.required
|
2013-02-22 18:50:54 +00:00
|
|
|
if ( isset( $paramObj->required ) ) {
|
|
|
|
if ( !is_bool( $paramObj->required ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.required",
|
|
|
|
'boolean'
|
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$paramObj->required = false;
|
|
|
|
}
|
|
|
|
|
2014-04-24 22:21:36 +00:00
|
|
|
// Param.suggested
|
|
|
|
if ( isset( $paramObj->suggested ) ) {
|
|
|
|
if ( !is_bool( $paramObj->suggested ) ) {
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.suggested",
|
|
|
|
'boolean'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$paramObj->suggested = false;
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:22:25 +00:00
|
|
|
// Param.description
|
2013-02-22 18:50:54 +00:00
|
|
|
if ( isset( $paramObj->description ) ) {
|
|
|
|
if ( !is_object( $paramObj->description ) && !is_string( $paramObj->description ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
// TODO: Also validate that the keys are valid lang codes and the values strings.
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.description",
|
|
|
|
'string|object'
|
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
2013-04-16 14:18:10 +00:00
|
|
|
$paramObj->description = self::normaliseInterfaceText( $paramObj->description );
|
2013-02-22 18:50:54 +00:00
|
|
|
} else {
|
2013-05-08 23:56:57 +00:00
|
|
|
$paramObj->description = null;
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-04 20:32:38 +00:00
|
|
|
// Param.example
|
|
|
|
if ( isset( $paramObj->example ) ) {
|
|
|
|
if ( !is_object( $paramObj->example ) && !is_string( $paramObj->example ) ) {
|
|
|
|
// TODO: Also validate that the keys are valid lang codes and the values strings.
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.example",
|
|
|
|
'string|object'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$paramObj->example = self::normaliseInterfaceText( $paramObj->example );
|
|
|
|
} else {
|
|
|
|
$paramObj->example = null;
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:22:25 +00:00
|
|
|
// Param.deprecated
|
2013-02-22 18:50:54 +00:00
|
|
|
if ( isset( $paramObj->deprecated ) ) {
|
2014-04-25 22:49:38 +00:00
|
|
|
if ( !is_bool( $paramObj->deprecated ) && !is_string( $paramObj->deprecated ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.deprecated",
|
|
|
|
'boolean|string'
|
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$paramObj->deprecated = false;
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:22:25 +00:00
|
|
|
// Param.aliases
|
2013-02-22 18:50:54 +00:00
|
|
|
if ( isset( $paramObj->aliases ) ) {
|
|
|
|
if ( !is_array( $paramObj->aliases ) ) {
|
|
|
|
// TODO: Validate the array values.
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.aliases",
|
|
|
|
'array'
|
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 00:00:14 +00:00
|
|
|
$paramObj->aliases = [];
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 15:29:33 +00:00
|
|
|
// Param.autovalue
|
|
|
|
if ( isset( $paramObj->autovalue ) ) {
|
|
|
|
if ( !is_string( $paramObj->autovalue ) ) {
|
|
|
|
// TODO: Validate the autovalue values.
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.autovalue",
|
|
|
|
'string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$paramObj->autovalue = null;
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:22:25 +00:00
|
|
|
// Param.default
|
2013-02-22 18:50:54 +00:00
|
|
|
if ( isset( $paramObj->default ) ) {
|
2015-03-09 21:45:04 +00:00
|
|
|
if ( !is_object( $paramObj->default ) && !is_string( $paramObj->default ) ) {
|
|
|
|
// TODO: Also validate that the keys are valid lang codes and the values strings.
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.default",
|
2015-03-09 21:45:04 +00:00
|
|
|
'string|object'
|
2013-05-16 15:01:00 +00:00
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
2015-03-09 21:45:04 +00:00
|
|
|
$paramObj->default = self::normaliseInterfaceText( $paramObj->default );
|
2013-02-22 18:50:54 +00:00
|
|
|
} else {
|
2015-03-09 21:45:04 +00:00
|
|
|
$paramObj->default = null;
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
2013-04-30 21:22:25 +00:00
|
|
|
|
2013-05-08 23:56:57 +00:00
|
|
|
// Param.type
|
|
|
|
if ( isset( $paramObj->type ) ) {
|
|
|
|
if ( !is_string( $paramObj->type ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.type",
|
|
|
|
'string'
|
|
|
|
);
|
2013-05-08 23:56:57 +00:00
|
|
|
}
|
2013-10-24 16:39:04 +00:00
|
|
|
|
|
|
|
// Map deprecated types to newer versions
|
|
|
|
if ( isset( $typeCompatMap[ $paramObj->type ] ) ) {
|
|
|
|
$paramObj->type = $typeCompatMap[ $paramObj->type ];
|
|
|
|
}
|
|
|
|
|
2013-05-08 23:56:57 +00:00
|
|
|
if ( !in_array( $paramObj->type, $types ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-value',
|
|
|
|
'params.' . $paramName . '.type'
|
|
|
|
);
|
2013-05-08 23:56:57 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$paramObj->type = 'unknown';
|
|
|
|
}
|
2013-10-09 22:26:22 +00:00
|
|
|
|
|
|
|
$paramNames[] = $paramName;
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
2013-04-30 22:32:08 +00:00
|
|
|
// Param.inherits
|
|
|
|
// Done afterwards to avoid code duplication
|
|
|
|
foreach ( $data->params as $paramName => $paramObj ) {
|
|
|
|
if ( isset( $paramObj->inherits ) ) {
|
|
|
|
if ( !isset( $data->params->{ $paramObj->inherits } ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-missing',
|
|
|
|
"params.{$paramObj->inherits}"
|
|
|
|
);
|
2013-04-30 22:32:08 +00:00
|
|
|
}
|
|
|
|
$parentParamObj = $data->params->{ $paramObj->inherits };
|
|
|
|
foreach ( $parentParamObj as $key => $value ) {
|
|
|
|
if ( !in_array( $key, $paramKeys ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-unknown', $key );
|
|
|
|
}
|
|
|
|
if ( !isset( $unnormalizedParams->$paramName->$key ) ) {
|
2013-09-04 22:47:35 +00:00
|
|
|
$paramObj->$key = is_object( $parentParamObj->$key ) ?
|
|
|
|
clone $parentParamObj->$key :
|
|
|
|
$parentParamObj->$key;
|
2013-04-30 22:32:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unset( $paramObj->inherits );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-09 22:26:22 +00:00
|
|
|
// Root.paramOrder
|
|
|
|
if ( isset( $data->paramOrder ) ) {
|
|
|
|
if ( !is_array( $data->paramOrder ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'paramOrder', 'array' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( count( $data->paramOrder ) < count( $paramNames ) ) {
|
|
|
|
$i = count( $data->paramOrder );
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing', "paramOrder[$i]" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate each of the values corresponds to a parameter and that there are no
|
|
|
|
// duplicates
|
2016-05-10 00:00:14 +00:00
|
|
|
$seen = [];
|
2013-10-09 22:26:22 +00:00
|
|
|
foreach ( $data->paramOrder as $i => $param ) {
|
|
|
|
if ( !isset( $data->params->$param ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-value', "paramOrder[$i]" );
|
|
|
|
}
|
|
|
|
if ( isset( $seen[$param] ) ) {
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-duplicate-value',
|
|
|
|
"paramOrder[$i]",
|
|
|
|
"paramOrder[{$seen[$param]}]",
|
|
|
|
$param
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$seen[$param] = $i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-15 22:15:52 +00:00
|
|
|
// Root.sets
|
|
|
|
if ( isset( $data->sets ) ) {
|
|
|
|
if ( !is_array( $data->sets ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'sets', 'array' );
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-10 00:00:14 +00:00
|
|
|
$data->sets = [];
|
2013-05-15 22:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $data->sets as $setNr => $setObj ) {
|
|
|
|
if ( !is_object( $setObj ) ) {
|
2013-10-09 22:26:22 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-value', "sets.{$setNr}" );
|
2013-05-15 22:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !isset( $setObj->label ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-missing',
|
|
|
|
"sets.{$setNr}.label",
|
|
|
|
'string|object'
|
|
|
|
);
|
2013-05-15 22:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_object( $setObj->label ) && !is_string( $setObj->label ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
// TODO: Also validate that the keys are valid lang codes and the values strings.
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"sets.{$setNr}.label",
|
|
|
|
'string|object'
|
|
|
|
);
|
2013-05-15 22:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$setObj->label = self::normaliseInterfaceText( $setObj->label );
|
|
|
|
|
|
|
|
if ( !isset( $setObj->params ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing', "sets.{$setNr}.params", 'array' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !is_array( $setObj->params ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', "sets.{$setNr}.params", 'array' );
|
|
|
|
}
|
|
|
|
|
2013-10-15 19:40:09 +00:00
|
|
|
if ( !count( $setObj->params ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-empty-array', "sets.{$setNr}.params" );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $setObj->params as $i => $param ) {
|
2013-05-15 22:15:52 +00:00
|
|
|
if ( !isset( $data->params->$param ) ) {
|
2013-10-15 19:40:09 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-value', "sets.{$setNr}.params[$i]" );
|
2013-05-15 22:15:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-30 21:22:25 +00:00
|
|
|
|
2014-10-18 15:28:26 +00:00
|
|
|
// Root.maps
|
|
|
|
if ( isset( $data->maps ) ) {
|
|
|
|
if ( !is_object( $data->maps ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'maps', 'object' );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data->maps = new stdClass();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $data->maps as $consumerId => $map ) {
|
|
|
|
if ( !is_object( $map ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'maps', 'object' );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $map as $key => $value ) {
|
|
|
|
// Key is not validated as this is used by a third-party application
|
|
|
|
// Value must be 2d array of parameter names, 1d array of parameter names, or valid
|
|
|
|
// parameter name
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
foreach ( $value as $key2 => $value2 ) {
|
|
|
|
if ( is_array( $value2 ) ) {
|
|
|
|
foreach ( $value2 as $key3 => $value3 ) {
|
|
|
|
if ( !is_string( $value3 ) ) {
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"maps.{$consumerId}.{$key}[$key2][$key3]",
|
|
|
|
'string'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ( !isset( $data->params->$value3 ) ) {
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-param',
|
|
|
|
$value3,
|
|
|
|
"maps.{$consumerId}.{$key}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 07:22:37 +00:00
|
|
|
} elseif ( is_string( $value2 ) ) {
|
2014-10-18 15:28:26 +00:00
|
|
|
if ( !isset( $data->params->$value2 ) ) {
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-param',
|
|
|
|
$value2,
|
|
|
|
"maps.{$consumerId}.{$key}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"maps.{$consumerId}.{$key}[$key2]",
|
|
|
|
'string|array'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} elseif ( is_string( $value ) ) {
|
|
|
|
if ( !isset( $data->params->$value ) ) {
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-param',
|
|
|
|
$value,
|
|
|
|
"maps.{$consumerId}.{$key}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"maps.{$consumerId}.{$key}",
|
|
|
|
'string|array'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-02-22 18:50:54 +00:00
|
|
|
return Status::newGood();
|
|
|
|
}
|
|
|
|
|
2013-04-16 14:18:10 +00:00
|
|
|
/**
|
|
|
|
* Normalise a InterfaceText field in the TemplateData blob.
|
2017-09-24 20:19:03 +00:00
|
|
|
* @param stdClass|string $text
|
|
|
|
* @return stdClass|string
|
2013-04-16 14:18:10 +00:00
|
|
|
*/
|
|
|
|
protected static function normaliseInterfaceText( $text ) {
|
|
|
|
if ( is_string( $text ) ) {
|
2019-06-12 04:38:27 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
2013-07-31 23:44:35 +00:00
|
|
|
$ret = new stdClass();
|
2019-06-12 04:38:27 +00:00
|
|
|
$ret->{ $contLang->getCode() } = $text;
|
2013-07-31 23:44:35 +00:00
|
|
|
return $ret;
|
2013-04-16 14:18:10 +00:00
|
|
|
}
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2013-10-06 16:31:20 +00:00
|
|
|
/**
|
|
|
|
* Get a single localized string from an InterfaceText object.
|
|
|
|
*
|
|
|
|
* Uses the preferred language passed to this function, or one of its fallbacks,
|
|
|
|
* or the site content language, or its fallbacks.
|
|
|
|
*
|
|
|
|
* @param stdClass $text An InterfaceText object
|
|
|
|
* @param string $langCode Preferred language
|
|
|
|
* @return null|string Text value from the InterfaceText object or null if no suitable
|
|
|
|
* match was found
|
|
|
|
*/
|
|
|
|
protected static function getInterfaceTextInLanguage( stdClass $text, $langCode ) {
|
|
|
|
if ( isset( $text->$langCode ) ) {
|
|
|
|
return $text->$langCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
list( $userlangs, $sitelangs ) = Language::getFallbacksIncludingSiteLanguage( $langCode );
|
|
|
|
|
|
|
|
foreach ( $userlangs as $lang ) {
|
|
|
|
if ( isset( $text->$lang ) ) {
|
|
|
|
return $text->$lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $sitelangs as $lang ) {
|
|
|
|
if ( isset( $text->$lang ) ) {
|
|
|
|
return $text->$lang;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If none of the languages are found fallback to null. Alternatively we could fallback to
|
|
|
|
// reset( $text ) which will return whatever key there is, but we should't give the user a
|
|
|
|
// "random" language with no context (e.g. could be RTL/Hebrew for an LTR/Japanese user).
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Status
|
|
|
|
*/
|
2013-02-22 18:50:54 +00:00
|
|
|
public function getStatus() {
|
|
|
|
return $this->status;
|
|
|
|
}
|
|
|
|
|
2013-10-06 16:31:20 +00:00
|
|
|
/**
|
|
|
|
* @return object
|
|
|
|
*/
|
2013-04-22 19:56:28 +00:00
|
|
|
public function getData() {
|
2016-01-27 04:36:45 +00:00
|
|
|
// Return deep clone so callers can't modify data. Needed for getDataInLanguage().
|
|
|
|
// Modification must clear 'json' and 'jsonDB' in-object cache.
|
|
|
|
return unserialize( serialize( $this->data ) );
|
2013-04-22 19:56:28 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 16:31:20 +00:00
|
|
|
/**
|
|
|
|
* Get data with all InterfaceText objects resolved to a single string to the
|
|
|
|
* appropriate language.
|
|
|
|
*
|
|
|
|
* @param string $langCode Preferred language
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function getDataInLanguage( $langCode ) {
|
2016-01-27 04:36:45 +00:00
|
|
|
$data = $this->getData();
|
2013-10-06 16:31:20 +00:00
|
|
|
|
|
|
|
// Root.description
|
|
|
|
if ( $data->description !== null ) {
|
|
|
|
$data->description = self::getInterfaceTextInLanguage( $data->description, $langCode );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $data->params as $paramObj ) {
|
|
|
|
// Param.label
|
|
|
|
if ( $paramObj->label !== null ) {
|
|
|
|
$paramObj->label = self::getInterfaceTextInLanguage( $paramObj->label, $langCode );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Param.description
|
|
|
|
if ( $paramObj->description !== null ) {
|
|
|
|
$paramObj->description = self::getInterfaceTextInLanguage( $paramObj->description, $langCode );
|
|
|
|
}
|
2015-03-09 21:45:04 +00:00
|
|
|
|
|
|
|
// Param.default
|
|
|
|
if ( $paramObj->default !== null ) {
|
|
|
|
$paramObj->default = self::getInterfaceTextInLanguage( $paramObj->default, $langCode );
|
|
|
|
}
|
2015-04-28 15:48:11 +00:00
|
|
|
|
|
|
|
// Param.example
|
|
|
|
if ( $paramObj->example !== null ) {
|
|
|
|
$paramObj->example = self::getInterfaceTextInLanguage( $paramObj->example, $langCode );
|
|
|
|
}
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $data->sets as $setObj ) {
|
|
|
|
$label = self::getInterfaceTextInLanguage( $setObj->label, $langCode );
|
|
|
|
if ( $label === null ) {
|
|
|
|
// Contrary to other InterfaceTexts, set label is not optional. If we're here it
|
|
|
|
// means the template data from the wiki doesn't contain either the user language,
|
|
|
|
// site language or any of its fallbacks. Wikis should fix data that is in this
|
|
|
|
// condition (TODO: Disallow during saving?). For now, fallback to whatever we can
|
|
|
|
// get that does exist in the text object.
|
2014-07-18 00:23:13 +00:00
|
|
|
$arr = (array)$setObj->label;
|
|
|
|
$label = reset( $arr );
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$setObj->label = $label;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2013-04-22 19:56:28 +00:00
|
|
|
/**
|
|
|
|
* @return string JSON
|
|
|
|
*/
|
2013-02-22 18:50:54 +00:00
|
|
|
public function getJSON() {
|
2016-01-27 04:36:45 +00:00
|
|
|
if ( $this->json === null ) {
|
|
|
|
// Cache for repeat calls
|
|
|
|
$this->json = json_encode( $this->data );
|
|
|
|
}
|
|
|
|
return $this->json;
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 13:33:45 +00:00
|
|
|
/**
|
2019-07-08 01:46:33 +00:00
|
|
|
* @return string JSON
|
2013-07-23 13:33:45 +00:00
|
|
|
*/
|
|
|
|
public function getJSONForDatabase() {
|
2019-07-08 01:46:33 +00:00
|
|
|
return $this->getJSON();
|
2013-07-23 13:33:45 +00:00
|
|
|
}
|
|
|
|
|
2013-08-01 20:57:42 +00:00
|
|
|
public function getHtml( Language $lang ) {
|
2013-10-06 16:31:20 +00:00
|
|
|
$data = $this->getDataInLanguage( $lang->getCode() );
|
2018-09-12 19:49:10 +00:00
|
|
|
$icon = 'settings';
|
2016-08-07 01:03:53 +00:00
|
|
|
if ( $data->format === null ) {
|
|
|
|
$formatMsg = null;
|
2018-07-12 22:30:53 +00:00
|
|
|
} elseif ( isset( self::$formats[$data->format] ) ) {
|
2016-08-07 01:03:53 +00:00
|
|
|
$formatMsg = $data->format;
|
2018-07-12 22:30:53 +00:00
|
|
|
$icon = 'template-format-' . $formatMsg;
|
2016-08-07 01:03:53 +00:00
|
|
|
} else {
|
|
|
|
$formatMsg = 'custom';
|
|
|
|
}
|
2013-02-22 18:50:54 +00:00
|
|
|
$html =
|
2016-05-10 00:00:14 +00:00
|
|
|
Html::openElement( 'div', [ 'class' => 'mw-templatedata-doc-wrap' ] )
|
2013-08-01 20:57:42 +00:00
|
|
|
. Html::element(
|
|
|
|
'p',
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
|
|
|
'class' => [
|
2013-09-25 17:09:03 +00:00
|
|
|
'mw-templatedata-doc-desc',
|
|
|
|
'mw-templatedata-doc-muted' => $data->description === null,
|
2016-05-10 00:00:14 +00:00
|
|
|
]
|
|
|
|
],
|
2019-05-19 11:35:02 +00:00
|
|
|
$data->description ??
|
2013-10-06 16:46:00 +00:00
|
|
|
wfMessage( 'templatedata-doc-desc-empty' )->inLanguage( $lang )->text()
|
2013-08-01 20:57:42 +00:00
|
|
|
)
|
2014-11-15 17:17:35 +00:00
|
|
|
. '<table class="wikitable mw-templatedata-doc-params sortable">'
|
2015-11-13 20:35:07 +00:00
|
|
|
. Html::rawElement(
|
2013-08-01 20:57:42 +00:00
|
|
|
'caption',
|
2016-05-10 00:00:14 +00:00
|
|
|
[],
|
2015-11-13 20:35:07 +00:00
|
|
|
Html::element(
|
|
|
|
'p',
|
2016-05-10 00:00:14 +00:00
|
|
|
[],
|
2015-11-13 20:35:07 +00:00
|
|
|
wfMessage( 'templatedata-doc-params' )->inLanguage( $lang )->text()
|
|
|
|
)
|
2016-08-07 01:03:53 +00:00
|
|
|
. ( $formatMsg !== null ?
|
2016-03-01 17:10:23 +00:00
|
|
|
Html::rawElement(
|
|
|
|
'p',
|
2016-05-10 00:00:14 +00:00
|
|
|
[],
|
2018-07-12 22:30:53 +00:00
|
|
|
new OOUI\IconWidget( [ 'icon' => $icon ] )
|
2016-03-01 17:10:23 +00:00
|
|
|
. Html::element(
|
|
|
|
'span',
|
2016-05-10 00:00:14 +00:00
|
|
|
[ 'class' => 'mw-templatedata-format' ],
|
2016-08-07 01:03:53 +00:00
|
|
|
// Messages that can be used here:
|
|
|
|
// * templatedata-doc-format-block
|
|
|
|
// * templatedata-doc-format-custom
|
|
|
|
// * templatedata-doc-format-inline
|
|
|
|
wfMessage( 'templatedata-doc-format-' . $formatMsg )->inLanguage( $lang )->text()
|
2016-03-01 17:10:23 +00:00
|
|
|
)
|
|
|
|
) :
|
|
|
|
'' )
|
2013-08-01 20:57:42 +00:00
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
. '<thead><tr>'
|
2013-08-01 20:57:42 +00:00
|
|
|
. Html::element(
|
|
|
|
'th',
|
2016-05-10 00:00:14 +00:00
|
|
|
[ 'colspan' => 2 ],
|
2013-10-06 16:46:00 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-name' )->inLanguage( $lang )->text()
|
2013-08-01 20:57:42 +00:00
|
|
|
)
|
|
|
|
. Html::element(
|
|
|
|
'th',
|
2016-05-10 00:00:14 +00:00
|
|
|
[],
|
2013-10-06 16:46:00 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-desc' )->inLanguage( $lang )->text()
|
2013-08-01 20:57:42 +00:00
|
|
|
)
|
|
|
|
. Html::element(
|
|
|
|
'th',
|
2016-05-10 00:00:14 +00:00
|
|
|
[],
|
2013-10-06 16:46:00 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-type' )->inLanguage( $lang )->text()
|
2013-08-01 20:57:42 +00:00
|
|
|
)
|
|
|
|
. Html::element(
|
|
|
|
'th',
|
2016-05-10 00:00:14 +00:00
|
|
|
[],
|
2013-10-06 16:46:00 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-status' )->inLanguage( $lang )->text()
|
2013-08-01 20:57:42 +00:00
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
. '</tr></thead>'
|
2013-05-16 15:01:00 +00:00
|
|
|
. '<tbody>';
|
|
|
|
|
2015-02-19 01:11:39 +00:00
|
|
|
if ( count( (array)$data->params ) === 0 ) {
|
|
|
|
// Display no parameters message
|
|
|
|
$html .= '<tr>'
|
|
|
|
. Html::element( 'td',
|
2016-05-10 00:00:14 +00:00
|
|
|
[
|
2015-02-19 01:11:39 +00:00
|
|
|
'class' => 'mw-templatedata-doc-muted',
|
|
|
|
'colspan' => 7
|
2016-05-10 00:00:14 +00:00
|
|
|
],
|
2015-02-19 01:11:39 +00:00
|
|
|
wfMessage( 'templatedata-doc-no-params-set' )->inLanguage( $lang )->text()
|
|
|
|
)
|
|
|
|
. '</tr>';
|
|
|
|
}
|
2019-01-02 00:43:01 +00:00
|
|
|
|
|
|
|
$paramNames = $data->paramOrder ?? array_keys( (array)$data->params );
|
|
|
|
foreach ( $paramNames as $paramName ) {
|
2015-02-19 00:53:59 +00:00
|
|
|
$paramObj = $data->params->$paramName;
|
2013-02-22 18:50:54 +00:00
|
|
|
$description = '';
|
|
|
|
$default = '';
|
2013-05-08 23:56:57 +00:00
|
|
|
|
|
|
|
$aliases = '';
|
|
|
|
if ( count( $paramObj->aliases ) ) {
|
|
|
|
foreach ( $paramObj->aliases as $alias ) {
|
2018-11-15 22:59:58 +00:00
|
|
|
$aliases .= wfMessage( 'word-separator' )->inLanguage( $lang )->escaped()
|
|
|
|
. Html::element( 'code', [
|
2013-05-16 15:01:00 +00:00
|
|
|
'class' => 'mw-templatedata-doc-param-alias'
|
2016-05-10 00:00:14 +00:00
|
|
|
], $alias );
|
2013-05-08 23:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-21 01:59:03 +00:00
|
|
|
$statusClass = '';
|
2014-04-24 22:21:36 +00:00
|
|
|
if ( $paramObj->deprecated ) {
|
|
|
|
$status = 'templatedata-doc-param-status-deprecated';
|
|
|
|
} elseif ( $paramObj->required ) {
|
|
|
|
$status = 'templatedata-doc-param-status-required';
|
2016-02-21 01:59:03 +00:00
|
|
|
$statusClass = 'mw-templatedata-doc-param-status-required';
|
2014-04-24 22:21:36 +00:00
|
|
|
} elseif ( $paramObj->suggested ) {
|
|
|
|
$status = 'templatedata-doc-param-status-suggested';
|
|
|
|
} else {
|
|
|
|
$status = 'templatedata-doc-param-status-optional';
|
|
|
|
}
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
$html .= '<tr>'
|
2013-05-08 23:56:57 +00:00
|
|
|
// Label
|
2019-05-19 11:35:02 +00:00
|
|
|
. Html::element( 'th', [], $paramObj->label ?? $lang->ucfirst( $paramName ) )
|
2013-05-08 23:56:57 +00:00
|
|
|
// Parameters and aliases
|
2016-05-10 00:00:14 +00:00
|
|
|
. Html::rawElement( 'td', [ 'class' => 'mw-templatedata-doc-param-name' ],
|
|
|
|
Html::element( 'code', [], $paramName ) . $aliases
|
2013-05-08 23:56:57 +00:00
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
// Description
|
2016-05-10 00:00:14 +00:00
|
|
|
. Html::rawElement( 'td', [
|
|
|
|
'class' => [
|
2015-01-12 21:11:21 +00:00
|
|
|
'mw-templatedata-doc-muted' => ( $paramObj->description === null )
|
2016-05-10 00:00:14 +00:00
|
|
|
]
|
|
|
|
],
|
2015-11-16 02:23:38 +00:00
|
|
|
Html::element( 'p', [],
|
2019-05-19 11:35:02 +00:00
|
|
|
$paramObj->description ??
|
|
|
|
wfMessage( 'templatedata-doc-param-desc-empty' )->inLanguage( $lang )->text()
|
2013-02-22 18:50:54 +00:00
|
|
|
)
|
2016-05-10 00:00:14 +00:00
|
|
|
. Html::rawElement( 'dl', [],
|
2015-04-28 15:57:19 +00:00
|
|
|
// Default
|
2018-09-02 00:36:06 +00:00
|
|
|
( $paramObj->default !== null ? ( Html::element( 'dt', [],
|
|
|
|
wfMessage( 'templatedata-doc-param-default' )->inLanguage( $lang )->text()
|
2013-05-08 23:56:57 +00:00
|
|
|
)
|
2018-09-02 00:36:06 +00:00
|
|
|
. Html::element( 'dd', [],
|
|
|
|
$paramObj->default
|
|
|
|
) ) : '' )
|
2015-04-28 15:57:19 +00:00
|
|
|
// Example
|
2018-09-02 00:36:06 +00:00
|
|
|
. ( $paramObj->example !== null ? ( Html::element( 'dt', [],
|
2015-04-28 15:57:19 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-example' )->inLanguage( $lang )->text()
|
2015-04-28 15:48:11 +00:00
|
|
|
)
|
2018-09-02 00:36:06 +00:00
|
|
|
. Html::element( 'dd', [],
|
|
|
|
$paramObj->example
|
|
|
|
) ) : '' )
|
2015-04-28 15:57:19 +00:00
|
|
|
// Auto value
|
2018-09-02 00:36:06 +00:00
|
|
|
. ( $paramObj->autovalue !== null ? ( Html::element( 'dt', [],
|
2015-04-28 15:57:19 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-autovalue' )->inLanguage( $lang )->text()
|
|
|
|
)
|
2018-09-02 00:36:06 +00:00
|
|
|
. Html::rawElement( 'dd', [],
|
|
|
|
Html::element( 'code', [], $paramObj->autovalue )
|
|
|
|
) ) : '' )
|
2015-04-28 15:57:19 +00:00
|
|
|
)
|
2015-04-28 15:48:11 +00:00
|
|
|
)
|
2015-04-28 15:57:19 +00:00
|
|
|
// Type
|
2016-05-10 00:00:14 +00:00
|
|
|
. Html::element( 'td', [
|
|
|
|
'class' => [
|
2015-04-28 15:57:19 +00:00
|
|
|
'mw-templatedata-doc-param-type',
|
|
|
|
'mw-templatedata-doc-muted' => $paramObj->type === 'unknown'
|
2016-05-10 00:00:14 +00:00
|
|
|
]
|
|
|
|
],
|
2016-01-13 19:29:05 +00:00
|
|
|
|
|
|
|
// Known messages, for grepping:
|
|
|
|
// templatedata-doc-param-type-boolean, templatedata-doc-param-type-content,
|
|
|
|
// templatedata-doc-param-type-date, templatedata-doc-param-type-line,
|
|
|
|
// templatedata-doc-param-type-number, templatedata-doc-param-type-string,
|
|
|
|
// templatedata-doc-param-type-unbalanced-wikitext, templatedata-doc-param-type-unknown,
|
|
|
|
// templatedata-doc-param-type-url, templatedata-doc-param-type-wiki-file-name,
|
|
|
|
// templatedata-doc-param-type-wiki-page-name, templatedata-doc-param-type-wiki-template-name,
|
|
|
|
// templatedata-doc-param-type-wiki-user-name
|
|
|
|
wfMessage( 'templatedata-doc-param-type-' . $paramObj->type )->inLanguage( $lang )->text()
|
2014-09-02 15:29:33 +00:00
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
// Status
|
2016-02-21 01:59:03 +00:00
|
|
|
. Html::element(
|
|
|
|
'td',
|
|
|
|
[ 'class' => $statusClass ],
|
|
|
|
wfMessage( $status )->inLanguage( $lang )->text()
|
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
. '</tr>';
|
|
|
|
}
|
|
|
|
$html .= '</tbody></table>'
|
|
|
|
. Html::closeElement( 'div' );
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2018-04-20 02:18:03 +00:00
|
|
|
/**
|
|
|
|
* Get parameter descriptions from raw wikitext (used for templates that have no templatedata).
|
|
|
|
* @param string $wikitext The text to extract parameters from.
|
|
|
|
* @return string[] Parameter info in the same format as the templatedata 'params' key.
|
|
|
|
*/
|
|
|
|
public static function getRawParams( $wikitext ) {
|
|
|
|
// This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js
|
2018-10-23 04:59:11 +00:00
|
|
|
preg_match_all( '/{{3,}([^#]*?)([<|]|}{3,})/m', $wikitext, $rawParams );
|
2018-04-20 02:18:03 +00:00
|
|
|
$params = [];
|
2018-08-27 03:08:49 +00:00
|
|
|
$normalizedParams = [];
|
2018-04-20 02:18:03 +00:00
|
|
|
if ( isset( $rawParams[1] ) ) {
|
|
|
|
foreach ( $rawParams[1] as $rawParam ) {
|
2018-08-27 03:08:49 +00:00
|
|
|
// This normalization process is repeated in JS in ext.TemplateDataGenerator.sourceHandler.js
|
|
|
|
$normalizedParam = preg_replace( '/[-_ ]+/', ' ', strtolower( $rawParam ) );
|
|
|
|
if ( in_array( $normalizedParam, $normalizedParams ) ) {
|
|
|
|
// This or a similarly-named parameter has already been found.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$normalizedParams[] = $normalizedParam;
|
2018-04-20 02:18:03 +00:00
|
|
|
$params[ $rawParam ] = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2013-04-16 14:18:10 +00:00
|
|
|
private function __construct( $data = null ) {
|
2013-02-22 18:50:54 +00:00
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|