2013-03-27 20:08:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implement the 'templatedata' query module in the API.
|
|
|
|
* Format JSON only.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup API
|
|
|
|
* @emits error.code templatedata-corrupt
|
2014-10-29 18:46:33 +00:00
|
|
|
* @todo Support continuation (see I1a6e51cd)
|
2013-03-27 20:08:57 +00:00
|
|
|
*/
|
|
|
|
class ApiTemplateData extends ApiBase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override built-in handling of format parameter.
|
|
|
|
* Only JSON is supported.
|
|
|
|
*
|
|
|
|
* @return ApiFormatBase
|
|
|
|
*/
|
|
|
|
public function getCustomPrinter() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$format = $params['format'];
|
|
|
|
$allowed = array( 'json', 'jsonfm' );
|
|
|
|
if ( in_array( $format, $allowed ) ) {
|
|
|
|
return $this->getMain()->createPrinterByName( $format );
|
|
|
|
}
|
|
|
|
return $this->getMain()->createPrinterByName( $allowed[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ApiPageSet
|
|
|
|
*/
|
|
|
|
private function getPageSet() {
|
|
|
|
if ( !isset( $this->mPageSet ) ) {
|
|
|
|
$this->mPageSet = new ApiPageSet( $this );
|
|
|
|
}
|
|
|
|
return $this->mPageSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
2013-10-06 16:31:20 +00:00
|
|
|
if ( is_null( $params['lang'] ) ) {
|
|
|
|
$langCode = false;
|
|
|
|
} elseif ( !Language::isValidCode( $params['lang'] ) ) {
|
|
|
|
$this->dieUsage( 'Invalid language code for parameter lang', 'invalidlang' );
|
|
|
|
} else {
|
|
|
|
$langCode = $params['lang'];
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:08:57 +00:00
|
|
|
$pageSet = $this->getPageSet();
|
|
|
|
$pageSet->execute();
|
|
|
|
$titles = $pageSet->getGoodTitles(); // page_id => Title object
|
|
|
|
|
|
|
|
if ( !count( $titles ) ) {
|
|
|
|
$result->addValue( null, 'pages', (object) array() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$db = $this->getDB();
|
|
|
|
$res = $db->select( 'page_props',
|
|
|
|
array( 'pp_page', 'pp_value' ), array(
|
|
|
|
'pp_page' => array_keys( $titles ),
|
|
|
|
'pp_propname' => 'templatedata'
|
|
|
|
),
|
|
|
|
__METHOD__,
|
|
|
|
array( 'ORDER BY', 'pp_page' )
|
|
|
|
);
|
|
|
|
|
|
|
|
$resp = array();
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$rawData = $row->pp_value;
|
2013-07-23 13:33:45 +00:00
|
|
|
$tdb = TemplateDataBlob::newFromDatabase( $rawData );
|
2013-04-22 19:56:28 +00:00
|
|
|
$status = $tdb->getStatus();
|
2013-10-06 16:31:20 +00:00
|
|
|
|
2013-04-22 19:56:28 +00:00
|
|
|
if ( !$status->isOK() ) {
|
2013-05-16 15:01:00 +00:00
|
|
|
$this->dieUsage(
|
2013-07-23 14:26:58 +00:00
|
|
|
'Page #' . intval( $row->pp_page ) . ' templatedata contains invalid data: '
|
2013-05-16 15:01:00 +00:00
|
|
|
. $status->getMessage(), 'templatedata-corrupt'
|
|
|
|
);
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
2013-10-06 16:31:20 +00:00
|
|
|
|
|
|
|
if ( $langCode ) {
|
|
|
|
$data = $tdb->getDataInLanguage( $langCode );
|
|
|
|
} else {
|
|
|
|
$data = $tdb->getData();
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:08:57 +00:00
|
|
|
$resp[$row->pp_page] = array(
|
|
|
|
'title' => strval( $titles[$row->pp_page] ),
|
2014-01-17 15:52:00 +00:00
|
|
|
) + (array) $data;
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set top level element
|
|
|
|
$result->addValue( null, 'pages', (object) $resp );
|
|
|
|
|
|
|
|
$values = $pageSet->getNormalizedTitlesAsResult();
|
|
|
|
if ( $values ) {
|
|
|
|
$result->addValue( null, 'normalized', $values );
|
|
|
|
}
|
2014-05-23 17:17:53 +00:00
|
|
|
$redirects = $pageSet->getRedirectTitlesAsResult();
|
|
|
|
if ( $redirects ) {
|
|
|
|
$result->addValue( null, 'redirects', $redirects );
|
|
|
|
}
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams( $flags = 0 ) {
|
|
|
|
return $this->getPageSet()->getFinalParams( $flags ) + array(
|
|
|
|
'format' => array(
|
|
|
|
ApiBase::PARAM_DFLT => 'json',
|
|
|
|
ApiBase::PARAM_TYPE => array( 'json', 'jsonfm' ),
|
2013-10-06 16:31:20 +00:00
|
|
|
),
|
|
|
|
'lang' => null
|
2013-03-27 20:08:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-29 18:46:33 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki core 1.25
|
|
|
|
*/
|
2013-03-27 20:08:57 +00:00
|
|
|
public function getParamDescription() {
|
|
|
|
return $this->getPageSet()->getParamDescription() + array(
|
|
|
|
'format' => 'The format of the output',
|
2013-10-06 16:31:20 +00:00
|
|
|
'lang' => 'Return localized values in this language (by default all available' .
|
|
|
|
' translations are returned)',
|
2013-03-27 20:08:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-29 18:46:33 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki core 1.25
|
|
|
|
*/
|
2013-03-27 20:08:57 +00:00
|
|
|
public function getDescription() {
|
|
|
|
return 'Data stored by the TemplateData extension';
|
|
|
|
}
|
|
|
|
|
2014-10-29 18:46:33 +00:00
|
|
|
/**
|
|
|
|
* @deprecated since MediaWiki core 1.25
|
|
|
|
*/
|
2013-03-27 20:08:57 +00:00
|
|
|
public function getExamples() {
|
|
|
|
return array(
|
|
|
|
'api.php?action=templatedata&titles=Template:Stub|Template:Example',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-10-29 18:46:33 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
|
|
|
return array(
|
|
|
|
'action=templatedata&titles=Template:Stub|Template:Example'
|
|
|
|
=> 'apihelp-templatedata-example-1',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:08:57 +00:00
|
|
|
public function getHelpUrls() {
|
2013-07-15 00:14:41 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Extension:TemplateData';
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
}
|