2013-03-27 20:08:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implement the 'templatedata' query module in the API.
|
|
|
|
* Format JSON only.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2018-10-11 17:56:27 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2018-04-20 02:18:03 +00:00
|
|
|
|
2013-03-27 20:08:57 +00:00
|
|
|
/**
|
|
|
|
* @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 {
|
2018-04-05 10:28:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ApiPageSet|null
|
|
|
|
*/
|
|
|
|
private $mPageSet = null;
|
|
|
|
|
2013-03-27 20:08:57 +00:00
|
|
|
/**
|
2016-01-13 18:00:05 +00:00
|
|
|
* For backwards compatibility, this module needs to output format=json when
|
|
|
|
* no format is specified.
|
|
|
|
* @return ApiFormatBase|null
|
2013-03-27 20:08:57 +00:00
|
|
|
*/
|
|
|
|
public function getCustomPrinter() {
|
2016-01-13 18:00:05 +00:00
|
|
|
if ( $this->getMain()->getVal( 'format' ) === null ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->addDeprecation(
|
|
|
|
'apiwarn-templatedata-deprecation-format', 'action=templatedata&!format'
|
2016-01-13 18:00:05 +00:00
|
|
|
);
|
|
|
|
return $this->getMain()->createPrinterByName( 'json' );
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
2016-01-13 18:00:05 +00:00
|
|
|
return null;
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ApiPageSet
|
|
|
|
*/
|
|
|
|
private function getPageSet() {
|
2018-04-05 10:28:42 +00:00
|
|
|
if ( $this->mPageSet === null ) {
|
2013-03-27 20:08:57 +00:00
|
|
|
$this->mPageSet = new ApiPageSet( $this );
|
|
|
|
}
|
|
|
|
return $this->mPageSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
2018-04-13 01:34:36 +00:00
|
|
|
$continuationManager = new ApiContinuationManager( $this, [], [] );
|
|
|
|
$this->setContinuationManager( $continuationManager );
|
|
|
|
|
2013-10-06 16:31:20 +00:00
|
|
|
if ( is_null( $params['lang'] ) ) {
|
|
|
|
$langCode = false;
|
|
|
|
} elseif ( !Language::isValidCode( $params['lang'] ) ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( [ 'apierror-invalidlang', 'lang' ] );
|
2013-10-06 16:31:20 +00:00
|
|
|
} else {
|
|
|
|
$langCode = $params['lang'];
|
|
|
|
}
|
|
|
|
|
2013-03-27 20:08:57 +00:00
|
|
|
$pageSet = $this->getPageSet();
|
|
|
|
$pageSet->execute();
|
|
|
|
$titles = $pageSet->getGoodTitles(); // page_id => Title object
|
2017-04-21 01:20:48 +00:00
|
|
|
$missingTitles = $pageSet->getMissingTitles(); // page_id => Title object
|
2013-03-27 20:08:57 +00:00
|
|
|
|
2018-04-20 02:18:03 +00:00
|
|
|
$includeMissingTitles = $this->getParameter( 'includeMissingTitles' );
|
|
|
|
$doNotIgnoreMissingTitles = $this->getParameter( 'doNotIgnoreMissingTitles' );
|
|
|
|
if ( $doNotIgnoreMissingTitles ) {
|
|
|
|
$includeMissingTitles = $doNotIgnoreMissingTitles;
|
|
|
|
}
|
2017-04-21 01:20:48 +00:00
|
|
|
|
2018-10-14 02:51:38 +00:00
|
|
|
if ( !$titles && ( !$includeMissingTitles || !$missingTitles ) ) {
|
2017-06-20 07:22:37 +00:00
|
|
|
$result->addValue( null, 'pages', (object)[] );
|
2018-04-13 01:34:36 +00:00
|
|
|
$this->setContinuationManager( null );
|
|
|
|
$continuationManager->setContinuationIntoResult( $this->getResult() );
|
2013-03-27 20:08:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-10 00:00:14 +00:00
|
|
|
$resp = [];
|
2013-03-27 20:08:57 +00:00
|
|
|
|
2018-04-20 02:18:03 +00:00
|
|
|
if ( $includeMissingTitles ) {
|
2017-04-21 01:20:48 +00:00
|
|
|
foreach ( $missingTitles as $missingTitleId => $missingTitle ) {
|
|
|
|
$resp[ $missingTitleId ] = [ 'title' => $missingTitle, 'missing' => true ];
|
2013-10-06 16:31:20 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 01:20:48 +00:00
|
|
|
foreach ( $titles as $titleId => $title ) {
|
|
|
|
$resp[ $titleId ] = [ 'title' => $title, 'notemplatedata' => true ];
|
2015-04-17 23:47:07 +00:00
|
|
|
}
|
2017-04-21 01:20:48 +00:00
|
|
|
}
|
2016-09-20 19:21:21 +00:00
|
|
|
|
2018-10-14 02:51:38 +00:00
|
|
|
if ( $titles ) {
|
2017-04-21 01:20:48 +00:00
|
|
|
$db = $this->getDB();
|
|
|
|
$res = $db->select( 'page_props',
|
|
|
|
[ 'pp_page', 'pp_value' ], [
|
|
|
|
'pp_page' => array_keys( $titles ),
|
|
|
|
'pp_propname' => 'templatedata'
|
|
|
|
],
|
|
|
|
__METHOD__,
|
|
|
|
[ 'ORDER BY', 'pp_page' ]
|
|
|
|
);
|
2015-04-17 23:47:07 +00:00
|
|
|
|
2017-04-21 01:20:48 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$rawData = $row->pp_value;
|
|
|
|
$tdb = TemplateDataBlob::newFromDatabase( $rawData );
|
|
|
|
$status = $tdb->getStatus();
|
|
|
|
|
|
|
|
if ( !$status->isOK() ) {
|
|
|
|
$this->dieWithError(
|
|
|
|
[ 'apierror-templatedata-corrupt', intval( $row->pp_page ), $status->getMessage() ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-14 02:52:55 +00:00
|
|
|
if ( $langCode !== false ) {
|
2017-04-21 01:20:48 +00:00
|
|
|
$data = $tdb->getDataInLanguage( $langCode );
|
|
|
|
} else {
|
|
|
|
$data = $tdb->getData();
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: don't let ApiResult's formatversion=1 compatibility layer mangle our booleans
|
|
|
|
// to empty strings / absent properties
|
|
|
|
foreach ( $data->params as &$param ) {
|
|
|
|
$param->{ApiResult::META_BC_BOOLS} = [ 'required', 'suggested', 'deprecated' ];
|
|
|
|
}
|
|
|
|
unset( $param );
|
|
|
|
|
|
|
|
$data->params->{ApiResult::META_TYPE} = 'kvp';
|
|
|
|
$data->params->{ApiResult::META_KVP_KEY_NAME} = 'key';
|
|
|
|
$data->params->{ApiResult::META_INDEXED_TAG_NAME} = 'param';
|
2019-01-20 15:41:02 +00:00
|
|
|
if ( isset( $data->paramOrder ) ) {
|
|
|
|
ApiResult::setIndexedTagName( $data->paramOrder, 'p' );
|
|
|
|
}
|
2017-04-21 01:20:48 +00:00
|
|
|
|
2018-10-14 02:54:24 +00:00
|
|
|
if ( $includeMissingTitles ) {
|
|
|
|
unset( $resp[$row->pp_page]['notemplatedata'] );
|
|
|
|
} else {
|
|
|
|
$resp[ $row->pp_page ] = [ 'title' => $titles[ $row->pp_page ] ];
|
2017-04-21 01:20:48 +00:00
|
|
|
}
|
2018-10-14 02:54:24 +00:00
|
|
|
$resp[$row->pp_page] += (array)$data;
|
2017-04-21 01:20:48 +00:00
|
|
|
}
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 02:18:03 +00:00
|
|
|
// Now go through all the titles again, and attempt to extract parameter names from the
|
|
|
|
// wikitext for templates with no templatedata.
|
|
|
|
if ( $includeMissingTitles ) {
|
|
|
|
foreach ( $resp as $pageId => $pageInfo ) {
|
|
|
|
if ( !isset( $pageInfo['notemplatedata'] ) ) {
|
|
|
|
// Ignore pages that already have templatedata or that don't exist.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$content = WikiPage::factory( $pageInfo['title'] )
|
|
|
|
->getContent( RevisionRecord::FOR_PUBLIC )
|
|
|
|
->getNativeData();
|
|
|
|
$resp[ $pageId ][ 'params' ] = TemplateDataBlob::getRawParams( $content );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-13 18:00:05 +00:00
|
|
|
ApiResult::setArrayType( $resp, 'kvp', 'id' );
|
|
|
|
ApiResult::setIndexedTagName( $resp, 'page' );
|
|
|
|
|
2013-03-27 20:08:57 +00:00
|
|
|
// Set top level element
|
2017-06-20 07:22:37 +00:00
|
|
|
$result->addValue( null, 'pages', (object)$resp );
|
2013-03-27 20:08:57 +00:00
|
|
|
|
|
|
|
$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 );
|
|
|
|
}
|
2018-04-13 01:34:36 +00:00
|
|
|
|
|
|
|
$this->setContinuationManager( null );
|
|
|
|
$continuationManager->setContinuationIntoResult( $this->getResult() );
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams( $flags = 0 ) {
|
2018-04-13 01:34:36 +00:00
|
|
|
$result = [
|
2018-04-20 02:18:03 +00:00
|
|
|
'includeMissingTitles' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
|
|
|
],
|
|
|
|
'doNotIgnoreMissingTitles' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
|
|
|
ApiBase::PARAM_DEPRECATED => true,
|
|
|
|
],
|
|
|
|
'lang' => [
|
|
|
|
ApiBase::PARAM_TYPE => 'string',
|
|
|
|
],
|
2016-05-10 00:00:14 +00:00
|
|
|
];
|
2018-04-13 01:34:36 +00:00
|
|
|
if ( $flags ) {
|
|
|
|
$result += $this->getPageSet()->getFinalParams( $flags );
|
|
|
|
}
|
|
|
|
return $result;
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
2014-10-29 18:46:33 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
2017-09-24 20:19:03 +00:00
|
|
|
* @return array
|
2014-10-29 18:46:33 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
2016-05-10 00:00:14 +00:00
|
|
|
return [
|
2018-04-20 02:18:03 +00:00
|
|
|
'action=templatedata&titles=Template:Stub|Template:Example&includeMissingTitles=1'
|
2014-10-29 18:46:33 +00:00
|
|
|
=> 'apihelp-templatedata-example-1',
|
2018-04-20 02:18:03 +00:00
|
|
|
'action=templatedata&titles=Template:Stub|Template:Example'
|
2017-04-21 01:20:48 +00:00
|
|
|
=> 'apihelp-templatedata-example-2',
|
2016-05-10 00:00:14 +00:00
|
|
|
];
|
2014-10-29 18:46:33 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|