2013-02-22 18:50:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
2021-11-25 21:35:46 +00:00
|
|
|
|
|
|
|
namespace MediaWiki\Extension\TemplateData;
|
|
|
|
|
|
|
|
use Html;
|
|
|
|
use Language;
|
2019-06-12 04:38:27 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-11-25 21:35:46 +00:00
|
|
|
use Status;
|
|
|
|
use stdClass;
|
2021-07-12 09:47:55 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
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 TemplateDataBlob {
|
2020-08-26 11:00:01 +00:00
|
|
|
/**
|
|
|
|
* Predefined formats for TemplateData to check against
|
|
|
|
*/
|
|
|
|
private const FORMATS = [
|
|
|
|
'block' => "{{_\n| _ = _\n}}",
|
|
|
|
'inline' => '{{_|_=_}}',
|
|
|
|
];
|
|
|
|
|
2020-09-14 12:17:25 +00:00
|
|
|
private const VALID_ROOT_KEYS = [
|
|
|
|
'description',
|
|
|
|
'params',
|
|
|
|
'paramOrder',
|
|
|
|
'sets',
|
|
|
|
'maps',
|
|
|
|
'format',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const VALID_PARAM_KEYS = [
|
|
|
|
'label',
|
|
|
|
'required',
|
|
|
|
'suggested',
|
|
|
|
'description',
|
|
|
|
'example',
|
|
|
|
'deprecated',
|
|
|
|
'aliases',
|
|
|
|
'autovalue',
|
|
|
|
'default',
|
|
|
|
'inherits',
|
|
|
|
'type',
|
2021-03-10 10:54:52 +00:00
|
|
|
'suggestedvalues',
|
2020-09-14 12:17:25 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
private const VALID_TYPES = [
|
|
|
|
'content',
|
|
|
|
'line',
|
|
|
|
'number',
|
|
|
|
'boolean',
|
|
|
|
'string',
|
|
|
|
'date',
|
|
|
|
'unbalanced-wikitext',
|
|
|
|
'unknown',
|
|
|
|
'url',
|
|
|
|
'wiki-page-name',
|
|
|
|
'wiki-user-name',
|
|
|
|
'wiki-file-name',
|
|
|
|
'wiki-template-name',
|
|
|
|
];
|
|
|
|
|
|
|
|
private const DEPRECATED_TYPES_MAP = [
|
|
|
|
'string/line' => 'line',
|
|
|
|
'string/wiki-page-name' => 'wiki-page-name',
|
|
|
|
'string/wiki-user-name' => 'wiki-user-name',
|
|
|
|
'string/wiki-file-name' => 'wiki-file-name',
|
|
|
|
];
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
/**
|
2021-10-02 08:44:30 +00:00
|
|
|
* @var mixed
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
2020-09-03 10:17:21 +00:00
|
|
|
private $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()
|
|
|
|
*/
|
2020-09-03 10:17:21 +00:00
|
|
|
private $json = null;
|
2016-01-27 04:36:45 +00:00
|
|
|
|
|
|
|
/**
|
2020-09-03 10:06:10 +00:00
|
|
|
* @var Status
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
2020-09-03 10:17:21 +00:00
|
|
|
private $status;
|
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
|
2020-09-03 10:06:10 +00:00
|
|
|
* @return TemplateDataBlob
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
public static function newFromJSON( IDatabase $db, string $json ): TemplateDataBlob {
|
2019-07-08 01:46:33 +00:00
|
|
|
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.
|
2020-08-19 12:11:28 +00:00
|
|
|
$tdb->data = (object)[
|
|
|
|
'description' => null,
|
|
|
|
'params' => (object)[],
|
|
|
|
'format' => null,
|
|
|
|
'sets' => [],
|
|
|
|
'maps' => (object)[],
|
|
|
|
];
|
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
|
2020-09-03 10:06:10 +00:00
|
|
|
* @return TemplateDataBlob
|
2013-07-23 13:33:45 +00:00
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
public static function newFromDatabase( IDatabase $db, string $json ): TemplateDataBlob {
|
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
|
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
protected function parse(): Status {
|
2013-02-22 18:50:54 +00:00
|
|
|
$data = $this->data;
|
|
|
|
|
|
|
|
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 ) {
|
2020-09-14 12:17:25 +00:00
|
|
|
if ( !in_array( $key, self::VALID_ROOT_KEYS ) ) {
|
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 ) ) {
|
2020-08-19 13:29:55 +00:00
|
|
|
if ( !$this->isValidInterfaceText( $data->description ) ) {
|
2013-04-16 14:18:10 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type', 'description', 'string|object' );
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
2020-08-19 13:40:55 +00:00
|
|
|
$data->description = $this->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
|
2020-08-19 12:15:35 +00:00
|
|
|
if ( isset( $data->format ) ) {
|
2020-08-26 11:00:01 +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 ) {
|
2020-09-14 12:17:25 +00:00
|
|
|
if ( !in_array( $key, self::VALID_PARAM_KEYS ) ) {
|
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 ) ) {
|
2020-08-19 13:29:55 +00:00
|
|
|
if ( !$this->isValidInterfaceText( $paramObj->label ) ) {
|
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
|
|
|
}
|
2020-08-19 13:40:55 +00:00
|
|
|
$paramObj->label = $this->normaliseInterfaceText( $paramObj->label );
|
2013-05-08 23:56:57 +00:00
|
|
|
} 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 ) ) {
|
2020-08-19 13:29:55 +00:00
|
|
|
if ( !$this->isValidInterfaceText( $paramObj->description ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.description",
|
|
|
|
'string|object'
|
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
2020-08-19 13:40:55 +00:00
|
|
|
$paramObj->description = $this->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 ) ) {
|
2020-08-19 13:29:55 +00:00
|
|
|
if ( !$this->isValidInterfaceText( $paramObj->example ) ) {
|
2015-03-04 20:32:38 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.example",
|
|
|
|
'string|object'
|
|
|
|
);
|
|
|
|
}
|
2020-08-19 13:40:55 +00:00
|
|
|
$paramObj->example = $this->normaliseInterfaceText( $paramObj->example );
|
2015-03-04 20:32:38 +00:00
|
|
|
} 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 ) ) {
|
2021-12-09 13:39:11 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.aliases", 'array' );
|
|
|
|
}
|
2022-01-10 11:13:05 +00:00
|
|
|
foreach ( $paramObj->aliases as $i => &$alias ) {
|
|
|
|
if ( is_int( $alias ) ) {
|
|
|
|
$alias = (string)$alias;
|
|
|
|
} elseif ( !is_string( $alias ) ) {
|
2021-12-09 13:39:11 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
2022-01-10 11:13:05 +00:00
|
|
|
"params.{$paramName}.aliases[$i]", 'int|string' );
|
2021-12-09 13:39:11 +00:00
|
|
|
}
|
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 ) ) {
|
2020-08-19 13:29:55 +00:00
|
|
|
if ( !$this->isValidInterfaceText( $paramObj->default ) ) {
|
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
|
|
|
}
|
2020-08-19 13:40:55 +00:00
|
|
|
$paramObj->default = $this->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
|
2020-09-14 12:17:25 +00:00
|
|
|
if ( isset( self::DEPRECATED_TYPES_MAP[ $paramObj->type ] ) ) {
|
|
|
|
$paramObj->type = self::DEPRECATED_TYPES_MAP[ $paramObj->type ];
|
2013-10-24 16:39:04 +00:00
|
|
|
}
|
|
|
|
|
2020-09-14 12:17:25 +00:00
|
|
|
if ( !in_array( $paramObj->type, self::VALID_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
|
|
|
|
2021-03-10 10:54:52 +00:00
|
|
|
// Param.suggestedvalues
|
|
|
|
if ( isset( $paramObj->suggestedvalues ) ) {
|
|
|
|
if ( !is_array( $paramObj->suggestedvalues ) ) {
|
2021-12-09 13:39:11 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.suggestedvalues", 'array' );
|
|
|
|
}
|
|
|
|
foreach ( $paramObj->suggestedvalues as $i => $value ) {
|
|
|
|
if ( !is_string( $value ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.suggestedvalues[$i]", 'string' );
|
|
|
|
}
|
2021-03-10 10:54:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$paramObj->suggestedvalues = [];
|
|
|
|
}
|
|
|
|
|
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 ) {
|
2020-09-14 12:17:25 +00:00
|
|
|
if ( !in_array( $key, self::VALID_PARAM_KEYS ) ) {
|
2013-04-30 22:32:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-19 13:29:55 +00:00
|
|
|
if ( !$this->isValidInterfaceText( $setObj->label ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"sets.{$setNr}.label",
|
|
|
|
'string|object'
|
|
|
|
);
|
2013-05-15 22:15:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 13:40:55 +00:00
|
|
|
$setObj->label = $this->normaliseInterfaceText( $setObj->label );
|
2013-05-15 22:15:52 +00:00
|
|
|
|
|
|
|
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 {
|
2020-08-19 12:11:28 +00:00
|
|
|
$data->maps = (object)[];
|
2014-10-18 15:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $data->maps as $consumerId => $map ) {
|
|
|
|
if ( !is_object( $map ) ) {
|
2021-08-28 11:27:15 +00:00
|
|
|
return Status::newFatal( 'templatedata-invalid-type', "maps.$consumerId", 'object' );
|
2014-10-18 15:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2021-08-28 11:27:15 +00:00
|
|
|
"maps.$consumerId.{$key}[$key2][$key3]"
|
2014-10-18 15:28:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
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,
|
2021-08-28 11:27:15 +00:00
|
|
|
"maps.$consumerId.{$key}[$key2]"
|
2014-10-18 15:28:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
|
2020-08-19 13:29:55 +00:00
|
|
|
/**
|
|
|
|
* @param mixed $text
|
|
|
|
* @return bool
|
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
private function isValidInterfaceText( $text ): bool {
|
2020-08-19 13:29:55 +00:00
|
|
|
if ( $text instanceof stdClass ) {
|
|
|
|
$isEmpty = true;
|
|
|
|
// An (array) cast would return private/protected properties as well
|
|
|
|
foreach ( get_object_vars( $text ) as $languageCode => $string ) {
|
|
|
|
// TODO: Do we need to validate if these are known interface language codes?
|
|
|
|
if ( !is_string( $languageCode ) ||
|
|
|
|
ltrim( $languageCode ) === '' ||
|
|
|
|
!is_string( $string )
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$isEmpty = false;
|
|
|
|
}
|
|
|
|
return !$isEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return is_string( $text );
|
|
|
|
}
|
|
|
|
|
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
|
2020-08-19 13:40:55 +00:00
|
|
|
* @return stdClass
|
2013-04-16 14:18:10 +00:00
|
|
|
*/
|
2021-10-02 08:44:30 +00:00
|
|
|
private function normaliseInterfaceText( $text ): stdClass {
|
2013-04-16 14:18:10 +00:00
|
|
|
if ( is_string( $text ) ) {
|
2019-06-12 04:38:27 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
2020-08-19 12:11:28 +00:00
|
|
|
return (object)[ $contLang->getCode() => $text ];
|
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
|
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
protected static function getInterfaceTextInLanguage( stdClass $text, string $langCode ): ?string {
|
2013-10-06 16:31:20 +00:00
|
|
|
if ( isset( $text->$langCode ) ) {
|
|
|
|
return $text->$langCode;
|
|
|
|
}
|
|
|
|
|
2020-12-16 11:07:34 +00:00
|
|
|
list( $userlangs, $sitelangs ) = MediaWikiServices::getInstance()->getLanguageFallback()
|
|
|
|
->getAllIncludingSiteLanguage( $langCode );
|
2013-10-06 16:31:20 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
public function getStatus(): Status {
|
2013-02-22 18:50:54 +00:00
|
|
|
return $this->status;
|
|
|
|
}
|
|
|
|
|
2013-10-06 16:31:20 +00:00
|
|
|
/**
|
2021-10-02 08:44:30 +00:00
|
|
|
* @return mixed
|
2013-10-06 16:31:20 +00:00
|
|
|
*/
|
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
|
2020-09-03 10:06:10 +00:00
|
|
|
* @return stdClass
|
2013-10-06 16:31:20 +00:00
|
|
|
*/
|
2021-10-02 08:44:30 +00:00
|
|
|
public function getDataInLanguage( string $langCode ): stdClass {
|
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
|
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
public function getJSON(): string {
|
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
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
public function getJSONForDatabase(): string {
|
2019-07-08 01:46:33 +00:00
|
|
|
return $this->getJSON();
|
2013-07-23 13:33:45 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 10:11:42 +00:00
|
|
|
/**
|
|
|
|
* @param Language $lang
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
public function getHtml( Language $lang ): string {
|
2013-10-06 16:31:20 +00:00
|
|
|
$data = $this->getDataInLanguage( $lang->getCode() );
|
2021-10-02 09:15:30 +00:00
|
|
|
if ( is_string( $data->format ) && isset( self::FORMATS[$data->format] ) ) {
|
|
|
|
// The following icon names are used here:
|
|
|
|
// * template-format-block
|
|
|
|
// * template-format-inline
|
|
|
|
// @phan-suppress-next-line PhanTypeSuspiciousStringExpression
|
|
|
|
$icon = 'template-format-' . $data->format;
|
2016-08-07 01:03:53 +00:00
|
|
|
$formatMsg = $data->format;
|
|
|
|
} else {
|
2021-10-02 09:15:30 +00:00
|
|
|
$icon = 'settings';
|
|
|
|
$formatMsg = $data->format ? 'custom' : null;
|
2016-08-07 01:03:53 +00:00
|
|
|
}
|
2020-06-06 14:07:12 +00:00
|
|
|
$sorting = count( (array)$data->params ) > 1 ? " sortable" : "";
|
2021-07-12 09:45:22 +00:00
|
|
|
$html = '<header>'
|
|
|
|
. Html::element(
|
2013-08-01 20:57:42 +00:00
|
|
|
'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
|
|
|
)
|
2021-07-12 09:45:22 +00:00
|
|
|
. '</header>'
|
2020-06-06 14:07:12 +00:00
|
|
|
. '<table class="wikitable mw-templatedata-doc-params' . $sorting . '">'
|
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
|
|
|
[],
|
2021-11-25 21:35:46 +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-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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-10 10:54:52 +00:00
|
|
|
$suggestedValuesLine = '';
|
2021-12-09 13:39:11 +00:00
|
|
|
if ( $paramObj->suggestedvalues ) {
|
2021-03-10 10:54:52 +00:00
|
|
|
$suggestedValues = '';
|
|
|
|
foreach ( $paramObj->suggestedvalues as $suggestedValue ) {
|
|
|
|
$suggestedValues .= wfMessage( 'word-separator' )->inLanguage( $lang )->escaped()
|
|
|
|
. Html::element( 'code', [
|
|
|
|
'class' => 'mw-templatedata-doc-param-alias'
|
|
|
|
], $suggestedValue );
|
|
|
|
}
|
|
|
|
$suggestedValuesLine .= Html::element( 'dt', [],
|
|
|
|
wfMessage( 'templatedata-doc-param-suggestedvalues' )->inLanguage( $lang )->text()
|
|
|
|
) . Html::rawElement( 'dd', [], $suggestedValues );
|
|
|
|
}
|
|
|
|
|
2014-04-24 22:21:36 +00:00
|
|
|
if ( $paramObj->deprecated ) {
|
2020-08-20 11:27:43 +00:00
|
|
|
$status = 'deprecated';
|
2014-04-24 22:21:36 +00:00
|
|
|
} elseif ( $paramObj->required ) {
|
2020-08-20 11:27:43 +00:00
|
|
|
$status = 'required';
|
2014-04-24 22:21:36 +00:00
|
|
|
} elseif ( $paramObj->suggested ) {
|
2020-08-20 11:27:43 +00:00
|
|
|
$status = 'suggested';
|
2014-04-24 22:21:36 +00:00
|
|
|
} else {
|
2020-08-20 11:27:43 +00:00
|
|
|
$status = 'optional';
|
2014-04-24 22:21:36 +00:00
|
|
|
}
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
$html .= '<tr>'
|
2013-05-08 23:56:57 +00:00
|
|
|
// Label
|
2020-12-23 07:29:05 +00:00
|
|
|
. Html::element( 'th', [], $paramObj->label ?? $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', [],
|
2021-03-10 10:54:52 +00:00
|
|
|
// Suggested Values
|
|
|
|
$suggestedValuesLine .
|
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',
|
2020-08-20 11:27:43 +00:00
|
|
|
[
|
|
|
|
// CSS class names that can be used here:
|
|
|
|
// mw-templatedata-doc-param-status-deprecated
|
|
|
|
// mw-templatedata-doc-param-status-optional
|
|
|
|
// mw-templatedata-doc-param-status-required
|
|
|
|
// mw-templatedata-doc-param-status-suggested
|
|
|
|
'class' => "mw-templatedata-doc-param-status-$status",
|
|
|
|
'data-sort-value' => [
|
|
|
|
'deprecated' => -1,
|
|
|
|
'suggested' => 1,
|
|
|
|
'required' => 2,
|
|
|
|
][$status] ?? 0,
|
|
|
|
],
|
|
|
|
// Messages that can be used here:
|
|
|
|
// templatedata-doc-param-status-deprecated
|
|
|
|
// templatedata-doc-param-status-optional
|
|
|
|
// templatedata-doc-param-status-required
|
|
|
|
// templatedata-doc-param-status-suggested
|
|
|
|
wfMessage( "templatedata-doc-param-status-$status" )->inLanguage( $lang )->text()
|
2016-02-21 01:59:03 +00:00
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
. '</tr>';
|
|
|
|
}
|
2020-09-09 14:48:20 +00:00
|
|
|
$html .= '</tbody></table>';
|
2013-02-22 18:50:54 +00:00
|
|
|
|
2021-07-12 09:45:22 +00:00
|
|
|
return Html::rawElement( 'section', [ 'class' => 'mw-templatedata-doc-wrap' ], $html );
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2021-07-23 22:51:45 +00:00
|
|
|
public static function getRawParams( string $wikitext ): array {
|
2019-12-09 18:12:11 +00:00
|
|
|
// Ignore wikitext within nowiki tags and comments
|
|
|
|
$wikitext = preg_replace( '/<!--.*?-->/s', '', $wikitext );
|
|
|
|
$wikitext = preg_replace( '/<nowiki\s*>.*?<\/nowiki\s*>/s', '', $wikitext );
|
|
|
|
|
2018-04-20 02:18:03 +00:00
|
|
|
// This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js
|
2021-09-03 11:18:42 +00:00
|
|
|
preg_match_all( '/{{3,}([^\n#={|}]*?)([<|]|}{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
|
2020-09-22 16:12:52 +00:00
|
|
|
$normalizedParam = strtolower( trim( preg_replace( '/[-_ ]+/', ' ', $rawParam ) ) );
|
|
|
|
if ( !$normalizedParam || in_array( $normalizedParam, $normalizedParams ) ) {
|
2018-08-27 03:08:49 +00:00
|
|
|
// This or a similarly-named parameter has already been found.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$normalizedParams[] = $normalizedParam;
|
2020-09-22 16:12:52 +00:00
|
|
|
$params[ trim( $rawParam ) ] = [];
|
2018-04-20 02:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2020-09-03 10:18:43 +00:00
|
|
|
/**
|
2021-10-02 08:44:30 +00:00
|
|
|
* @param mixed $data
|
2020-09-03 10:18:43 +00:00
|
|
|
*/
|
|
|
|
protected function __construct( $data ) {
|
2013-02-22 18:50:54 +00:00
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|