2013-03-27 20:08:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implement the 'templatedata' query module in the API.
|
|
|
|
* Format JSON only.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2020-12-15 11:06:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
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;
|
|
|
|
}
|
|
|
|
|
2020-09-09 15:05:54 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2013-03-27 20:08:57 +00:00
|
|
|
public function execute() {
|
2020-12-15 11:06:18 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2013-03-27 20:08:57 +00:00
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
$result = $this->getResult();
|
|
|
|
|
2018-04-13 01:34:36 +00:00
|
|
|
$continuationManager = new ApiContinuationManager( $this, [], [] );
|
|
|
|
$this->setContinuationManager( $continuationManager );
|
|
|
|
|
2020-01-14 12:46:50 +00:00
|
|
|
if ( $params['lang'] === null ) {
|
2013-10-06 16:31:20 +00:00
|
|
|
$langCode = false;
|
2020-12-15 11:06:18 +00:00
|
|
|
} elseif ( !$services->getLanguageNameUtils()->isValidCode( $params['lang'] ) ) {
|
2016-11-03 19:16:57 +00:00
|
|
|
$this->dieWithError( [ 'apierror-invalidlang', 'lang' ] );
|
2020-01-30 07:29:24 +00:00
|
|
|
throw new LogicException();
|
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)[] );
|
2021-06-10 12:54:14 +00:00
|
|
|
$this->setContinuationManager();
|
2018-04-13 01:34:36 +00:00
|
|
|
$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__,
|
2021-07-15 00:01:30 +00:00
|
|
|
[ 'ORDER BY' => 'pp_page' ]
|
2017-04-21 01:20:48 +00:00
|
|
|
);
|
2015-04-17 23:47:07 +00:00
|
|
|
|
2017-04-21 01:20:48 +00:00
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$rawData = $row->pp_value;
|
2019-07-08 01:46:33 +00:00
|
|
|
$tdb = TemplateDataBlob::newFromDatabase( $db, $rawData );
|
2017-04-21 01:20:48 +00:00
|
|
|
$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
|
2021-06-10 12:54:14 +00:00
|
|
|
foreach ( $data->params as $param ) {
|
2017-04-21 01:20:48 +00:00
|
|
|
$param->{ApiResult::META_BC_BOOLS} = [ 'required', 'suggested', 'deprecated' ];
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2020-12-14 16:48:51 +00:00
|
|
|
$wikiPageFactory = $services->getWikiPageFactory();
|
|
|
|
|
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;
|
|
|
|
}
|
2020-12-14 16:48:51 +00:00
|
|
|
|
2021-06-10 12:54:14 +00:00
|
|
|
$content = $wikiPageFactory->newFromTitle( $pageInfo['title'] )->getContent();
|
2020-12-15 11:34:07 +00:00
|
|
|
$text = $content instanceof TextContent
|
|
|
|
? $content->getText()
|
|
|
|
: $content->getTextForSearchIndex();
|
|
|
|
$resp[ $pageId ][ 'params' ] = TemplateDataBlob::getRawParams( $text );
|
2018-04-20 02:18:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 16:48:51 +00:00
|
|
|
// TODO tracking will only be implemented temporarily to answer questions on
|
|
|
|
// template usage for the Technical Wishes topic area see T258917
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) ) {
|
2021-06-10 12:54:14 +00:00
|
|
|
foreach ( $resp as $pageInfo ) {
|
|
|
|
EventLogging::logEvent(
|
2020-12-14 16:48:51 +00:00
|
|
|
'TemplateDataApi',
|
2021-02-24 15:52:55 +00:00
|
|
|
-1,
|
2020-12-14 16:48:51 +00:00
|
|
|
[
|
|
|
|
'template_name' => $wikiPageFactory->newFromTitle( $pageInfo['title'] )
|
|
|
|
->getTitle()->getDBkey(),
|
2021-06-10 12:54:14 +00:00
|
|
|
'has_template_data' => !isset( $pageInfo['notemplatedata'] ),
|
2020-12-14 16:48:51 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 12:46:11 +00:00
|
|
|
$pageSet->populateGeneratorData( $resp );
|
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
|
|
|
|
2021-06-10 12:54:14 +00:00
|
|
|
$this->setContinuationManager();
|
2018-04-13 01:34:36 +00:00
|
|
|
$continuationManager->setContinuationIntoResult( $this->getResult() );
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 15:05:54 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
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
|
|
|
/**
|
2020-09-09 15:05:54 +00:00
|
|
|
* @inheritDoc
|
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
|
|
|
}
|
|
|
|
|
2020-09-09 15:05:54 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2013-03-27 20:08:57 +00:00
|
|
|
public function getHelpUrls() {
|
2019-08-27 06:44:03 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:TemplateData';
|
2013-03-27 20:08:57 +00:00
|
|
|
}
|
|
|
|
}
|