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() ) {
|
|
|
|
// Don't save invalid data, clear it.
|
2013-04-22 19:56:28 +00:00
|
|
|
$tdb->data = 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
|
|
|
/**
|
|
|
|
* 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',
|
|
|
|
'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',
|
|
|
|
'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(
|
|
|
|
'unknown',
|
|
|
|
'string',
|
|
|
|
'number',
|
|
|
|
'string/wiki-page-name',
|
|
|
|
'string/wiki-user-name',
|
2013-07-03 20:22:48 +00:00
|
|
|
'string/line',
|
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-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;
|
|
|
|
}
|
|
|
|
|
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 ) ) {
|
2013-04-22 19:56:28 +00:00
|
|
|
if ( $paramObj->deprecated !== false && !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
|
|
|
}
|
|
|
|
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-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-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 ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-type', "sets.{$setNr}", 'object' );
|
|
|
|
}
|
|
|
|
|
|
|
|
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' );
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $setObj->params as $param ) {
|
|
|
|
if ( !isset( $data->params->$param ) ) {
|
|
|
|
return Status::newFatal( 'templatedata-invalid-missing', "params.{$param}" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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.
|
|
|
|
$label = reset( $setObj->label );
|
|
|
|
}
|
|
|
|
|
|
|
|
$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-09-25 17:09:03 +00:00
|
|
|
wfMessage( 'templatedata-doc-desc-empty' )->inLanguage( $lang )
|
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(),
|
|
|
|
wfMessage( 'templatedata-doc-params' )->inLanguage( $lang )
|
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
. '<thead><tr>'
|
2013-08-01 20:57:42 +00:00
|
|
|
. Html::element(
|
|
|
|
'th',
|
|
|
|
array( 'colspan' => 2 ),
|
|
|
|
wfMessage( 'templatedata-doc-param-name' )->inLanguage( $lang )
|
|
|
|
)
|
|
|
|
. Html::element(
|
|
|
|
'th',
|
|
|
|
array(),
|
|
|
|
wfMessage( 'templatedata-doc-param-desc' )->inLanguage( $lang )
|
|
|
|
)
|
|
|
|
. Html::element(
|
|
|
|
'th',
|
|
|
|
array(),
|
|
|
|
wfMessage( 'templatedata-doc-param-type' )->inLanguage( $lang )
|
|
|
|
)
|
|
|
|
. Html::element(
|
|
|
|
'th',
|
|
|
|
array(),
|
|
|
|
wfMessage( 'templatedata-doc-param-default' )->inLanguage( $lang )
|
|
|
|
)
|
|
|
|
. Html::element(
|
|
|
|
'th',
|
|
|
|
array(),
|
|
|
|
wfMessage( 'templatedata-doc-param-status' )->inLanguage( $lang )
|
|
|
|
)
|
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 ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
$aliases .= Html::element( 'tt', array(
|
|
|
|
'class' => 'mw-templatedata-doc-param-alias'
|
|
|
|
), $alias );
|
2013-05-08 23:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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' ),
|
|
|
|
Html::element( 'tt', array(), $paramName ) . $aliases
|
|
|
|
)
|
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-09-25 17:09:03 +00:00
|
|
|
wfMessage( 'templatedata-doc-param-desc-empty' )->inLanguage( $lang )
|
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'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
Html::element( 'tt', array(), $paramObj->type )
|
|
|
|
)
|
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 === ''
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$paramObj->default !== '' ? $paramObj->default : 'empty'
|
|
|
|
)
|
2013-02-22 18:50:54 +00:00
|
|
|
// Status
|
|
|
|
. Html::element( 'td', array(),
|
|
|
|
$paramObj->deprecated ? 'deprecated' : (
|
|
|
|
$paramObj->required ? 'required' : 'optional'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
. '</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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|