2013-02-22 18:50:54 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents the information about a template,
|
|
|
|
* coming from the JSON blob in the <templatedata> tags
|
|
|
|
* on wiki pages.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
class TemplateDataBlob {
|
2013-07-23 12:29:43 +00:00
|
|
|
// Size of MySQL 'blob' field; page_props table where the data is stored uses one.
|
|
|
|
const MAX_LENGTH = 65535;
|
2013-04-22 20:02:09 +00:00
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
/**
|
|
|
|
* @var stdClass
|
|
|
|
*/
|
|
|
|
private $data;
|
|
|
|
|
|
|
|
/**
|
2013-09-12 09:41:07 +00:00
|
|
|
* @var Status: Cache of TemplateDataBlob::parse
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
|
|
|
private $status;
|
|
|
|
|
|
|
|
/**
|
2013-07-23 13:33:45 +00:00
|
|
|
* Parse and validate passed JSON and create a TemplateDataBlob object.
|
|
|
|
* Accepts and handles user-provided data.
|
|
|
|
*
|
|
|
|
* @param string $json
|
|
|
|
* @throws MWException
|
2013-09-12 04:02:37 +00:00
|
|
|
* @return TemplateDataBlob
|
2013-02-22 18:50:54 +00:00
|
|
|
*/
|
|
|
|
public static function newFromJSON( $json ) {
|
2013-04-22 19:56:28 +00:00
|
|
|
$tdb = new self( 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() ) {
|
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();
|
|
|
|
$tdb->data->sets = array();
|
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
|
|
|
/**
|
|
|
|
* Parse and validate passed JSON (possibly gzip-compressed) and create a TemplateDataBlob object.
|
|
|
|
*
|
|
|
|
* @param string $json
|
2013-09-12 09:41:07 +00:00
|
|
|
* @return TemplateDataBlob
|
2013-07-23 13:33:45 +00:00
|
|
|
*/
|
|
|
|
public static function newFromDatabase( $json ) {
|
|
|
|
// Handle GZIP compression. \037\213 is the header for GZIP files.
|
|
|
|
if ( substr( $json, 0, 2 ) === "\037\213" ) {
|
|
|
|
$json = gzdecode( $json );
|
|
|
|
}
|
|
|
|
return self::newFromJSON( $json );
|
|
|
|
}
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
/**
|
|
|
|
* Parse the data, normalise it and validate it.
|
|
|
|
*
|
|
|
|
* See spec.templatedata.json for the expected format of the JSON object.
|
|
|
|
* @return Status
|
|
|
|
*/
|
|
|
|
private function parse() {
|
|
|
|
$data = $this->data;
|
|
|
|
|
2013-04-30 22:32:08 +00:00
|
|
|
static $rootKeys = array(
|
|
|
|
'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',
|
2013-04-30 22:32:08 +00:00
|
|
|
);
|
2013-09-04 22:47:35 +00:00
|
|
|
|
2013-04-30 22:32:08 +00:00
|
|
|
static $paramKeys = array(
|
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',
|
|
|
|
'deprecated',
|
|
|
|
'aliases',
|
|
|
|
'default',
|
|
|
|
'inherits',
|
2013-05-08 23:56:57 +00:00
|
|
|
'type',
|
|
|
|
);
|
2013-09-04 22:47:35 +00:00
|
|
|
|
2013-05-08 23:56:57 +00:00
|
|
|
static $types = array(
|
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',
|
|
|
|
'wiki-page-name',
|
|
|
|
'wiki-user-name',
|
2014-04-24 21:55:59 +00:00
|
|
|
'wiki-file-name',
|
2013-10-24 16:39:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
static $typeCompatMap = array(
|
|
|
|
'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',
|
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
|
|
|
}
|
|
|
|
|
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 ) );
|
2013-10-09 22:26:22 +00:00
|
|
|
$paramNames = array();
|
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
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
$paramObj->aliases = array();
|
|
|
|
}
|
|
|
|
|
2013-04-30 21:22:25 +00:00
|
|
|
// Param.default
|
2013-02-22 18:50:54 +00:00
|
|
|
if ( isset( $paramObj->default ) ) {
|
|
|
|
if ( !is_string( $paramObj->default ) ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
return Status::newFatal(
|
|
|
|
'templatedata-invalid-type',
|
|
|
|
"params.{$paramName}.default",
|
|
|
|
'string'
|
|
|
|
);
|
2013-02-22 18:50:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$paramObj->default = '';
|
|
|
|
}
|
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 ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-empty-array', "paramOrder" );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
$seen = array();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} elseif ( count( $paramNames ) ) {
|
|
|
|
$data->paramOrder = $paramNames;
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
$data->sets = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-07-23 13:33:45 +00:00
|
|
|
$length = strlen( $this->getJSONForDatabase() );
|
2013-07-23 12:29:43 +00:00
|
|
|
if ( $length > self::MAX_LENGTH ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-length', $length, self::MAX_LENGTH );
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @return stdClass|string $text
|
|
|
|
*/
|
|
|
|
protected static function normaliseInterfaceText( $text ) {
|
|
|
|
if ( is_string( $text ) ) {
|
|
|
|
global $wgContLang;
|
2013-07-31 23:44:35 +00:00
|
|
|
$ret = new stdClass();
|
|
|
|
$ret->{ $wgContLang->getCode() } = $text;
|
|
|
|
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() {
|
2013-10-06 16:31:20 +00:00
|
|
|
// TODO: Returned by reference. Data is a private member. Use clone instead?
|
2013-04-22 19:56:28 +00:00
|
|
|
return $this->data;
|
|
|
|
}
|
|
|
|
|
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 ) {
|
|
|
|
// Deep clone, also need to clone ->params and all interfacetext objects
|
|
|
|
// within param properties.
|
|
|
|
$data = unserialize( serialize( $this->data ) );
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
return json_encode( $this->data );
|
|
|
|
}
|
|
|
|
|
2013-07-23 13:33:45 +00:00
|
|
|
/**
|
|
|
|
* @return string JSON, gzip-compressed
|
|
|
|
*/
|
|
|
|
public function getJSONForDatabase() {
|
|
|
|
return gzencode( $this->getJSON() );
|
|
|
|
}
|
|
|
|
|
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() );
|
2013-02-22 18:50:54 +00:00
|
|
|
$html =
|
|
|
|
Html::openElement( 'div', array( 'class' => 'mw-templatedata-doc-wrap' ) )
|
2013-08-01 20:57:42 +00:00
|
|
|
. Html::element(
|
|
|
|
'p',
|
2013-09-25 17:09:03 +00:00
|
|
|
array(
|
|
|
|
'class' => array(
|
|
|
|
'mw-templatedata-doc-desc',
|
|
|
|
'mw-templatedata-doc-muted' => $data->description === null,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$data->description !== null ?
|
2013-10-06 16:31:20 +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
|
|
|
)
|
2013-04-30 22:32:31 +00:00
|
|
|
. '<table class="wikitable mw-templatedata-doc-params">'
|
2013-08-01 20:57:42 +00:00
|
|
|
. Html::element(
|
|
|
|
'caption',
|
|
|
|
array(),
|
2013-10-06 16:46:00 +00:00
|
|
|
wfMessage( 'templatedata-doc-params' )->inLanguage( $lang )->text()
|
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',
|
|
|
|
array( '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',
|
|
|
|
array(),
|
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',
|
|
|
|
array(),
|
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',
|
|
|
|
array(),
|
2013-10-06 16:46:00 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-default' )->inLanguage( $lang )->text()
|
2013-08-01 20:57:42 +00:00
|
|
|
)
|
|
|
|
. Html::element(
|
|
|
|
'th',
|
|
|
|
array(),
|
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>';
|
|
|
|
|
2013-02-22 18:50:54 +00:00
|
|
|
foreach ( $data->params as $paramName => $paramObj ) {
|
|
|
|
$description = '';
|
|
|
|
$default = '';
|
2013-05-08 23:56:57 +00:00
|
|
|
|
|
|
|
$aliases = '';
|
|
|
|
if ( count( $paramObj->aliases ) ) {
|
|
|
|
foreach ( $paramObj->aliases as $alias ) {
|
2014-06-19 22:25:13 +00:00
|
|
|
$aliases .= Html::element( 'code', array(
|
2013-05-16 15:01:00 +00:00
|
|
|
'class' => 'mw-templatedata-doc-param-alias'
|
|
|
|
), $alias );
|
2013-05-08 23:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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';
|
|
|
|
} 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
|
|
|
|
. Html::element( 'th', array(),
|
2013-10-06 16:31:20 +00:00
|
|
|
$paramObj->label !== null ?
|
|
|
|
$paramObj->label :
|
2013-09-04 22:47:35 +00:00
|
|
|
ucfirst( $paramName )
|
2013-05-08 23:56:57 +00:00
|
|
|
)
|
|
|
|
// Parameters and aliases
|
|
|
|
. Html::rawElement( 'td', array( 'class' => 'mw-templatedata-doc-param-name' ),
|
2014-06-19 22:25:13 +00:00
|
|
|
Html::element( 'code', array(), $paramName ) . $aliases
|
2013-05-08 23:56:57 +00:00
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
// Description
|
2013-05-08 23:56:57 +00:00
|
|
|
. Html::element( 'td', array(
|
|
|
|
'class' => array(
|
2013-05-16 15:01:00 +00:00
|
|
|
'mw-templatedata-doc-muted' => (
|
2013-10-06 16:31:20 +00:00
|
|
|
$paramObj->description === null && $paramObj->deprecated === false
|
2013-05-16 15:01:00 +00:00
|
|
|
)
|
2013-05-08 23:56:57 +00:00
|
|
|
)
|
|
|
|
),
|
2013-10-06 16:31:20 +00:00
|
|
|
$paramObj->description !== null ?
|
|
|
|
$paramObj->description :
|
2013-10-06 16:46:00 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-desc-empty' )->inLanguage( $lang )->text()
|
2013-02-22 18:50:54 +00:00
|
|
|
)
|
2013-05-08 23:56:57 +00:00
|
|
|
// Type
|
|
|
|
. Html::rawElement( 'td', array(
|
|
|
|
'class' => array(
|
|
|
|
'mw-templatedata-doc-param-type',
|
|
|
|
'mw-templatedata-doc-muted' => $paramObj->type === 'unknown'
|
|
|
|
)
|
|
|
|
),
|
2014-06-19 22:25:13 +00:00
|
|
|
Html::element( 'code', array(), $paramObj->type )
|
2013-05-08 23:56:57 +00:00
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
// Default
|
|
|
|
. Html::element( 'td', array(
|
2013-05-08 23:56:57 +00:00
|
|
|
'class' => array(
|
|
|
|
'mw-templatedata-doc-muted' => $paramObj->default === ''
|
|
|
|
)
|
|
|
|
),
|
2013-10-06 16:46:00 +00:00
|
|
|
$paramObj->default !== '' ?
|
|
|
|
$paramObj->default :
|
|
|
|
wfMessage( 'templatedata-doc-param-default-empty' )->inLanguage( $lang )->text()
|
2013-05-08 23:56:57 +00:00
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
// Status
|
2014-04-29 23:33:49 +00:00
|
|
|
. Html::element( 'td', array(), wfMessage( $status )->inLanguage( $lang )->text() )
|
2013-02-22 18:50:54 +00:00
|
|
|
. '</tr>';
|
|
|
|
}
|
|
|
|
$html .= '</tbody></table>'
|
|
|
|
. Html::closeElement( 'div' );
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2013-04-16 14:18:10 +00:00
|
|
|
private function __construct( $data = null ) {
|
2013-02-22 18:50:54 +00:00
|
|
|
$this->data = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|