2013-03-27 20:08:57 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implement the 'templatedata' query module in the API.
|
|
|
|
* Format JSON only.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2021-11-25 21:35:46 +00:00
|
|
|
namespace MediaWiki\Extension\TemplateData\Api;
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
use ApiContinuationManager;
|
|
|
|
use ApiFormatBase;
|
|
|
|
use ApiPageSet;
|
|
|
|
use ApiResult;
|
|
|
|
use ExtensionRegistry;
|
2022-03-06 16:07:22 +00:00
|
|
|
use MediaWiki\Extension\EventLogging\EventLogging;
|
2021-11-25 21:35:46 +00:00
|
|
|
use MediaWiki\Extension\TemplateData\TemplateDataBlob;
|
2020-12-15 11:06:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-11-25 21:35:46 +00:00
|
|
|
use TextContent;
|
2022-04-03 23:29:48 +00:00
|
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
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
|
|
|
|
*/
|
2021-10-02 08:44:30 +00:00
|
|
|
private function getPageSet(): ApiPageSet {
|
2023-08-23 07:24:23 +00:00
|
|
|
$this->mPageSet ??= new ApiPageSet( $this );
|
2013-03-27 20:08:57 +00:00
|
|
|
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' ] );
|
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();
|
2023-08-23 07:28:00 +00:00
|
|
|
$titles = $pageSet->getGoodPages();
|
|
|
|
$missingTitles = $pageSet->getMissingPages();
|
2013-03-27 20:08:57 +00:00
|
|
|
|
2023-08-23 07:24:23 +00:00
|
|
|
$includeMissingTitles = $this->getParameter( 'doNotIgnoreMissingTitles' ) ?:
|
|
|
|
$this->getParameter( 'includeMissingTitles' );
|
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();
|
2022-08-12 21:54:22 +00:00
|
|
|
$res = $db->newSelectQueryBuilder()
|
|
|
|
->from( 'page_props' )
|
|
|
|
->fields( [ 'pp_page', 'pp_value' ] )
|
|
|
|
->where( [
|
2017-04-21 01:20:48 +00:00
|
|
|
'pp_page' => array_keys( $titles ),
|
|
|
|
'pp_propname' => 'templatedata'
|
2022-08-12 21:54:22 +00:00
|
|
|
] )
|
|
|
|
->orderBy( 'pp_page' )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
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();
|
2021-12-23 09:45:11 +00:00
|
|
|
$resp[$pageId]['params'] = $this->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 ) {
|
2023-01-30 16:47:15 +00:00
|
|
|
EventLogging::submit(
|
|
|
|
'eventlogging_TemplateDataApi',
|
2020-12-14 16:48:51 +00:00
|
|
|
[
|
2023-01-30 16:47:15 +00:00
|
|
|
'$schema' => '/analytics/legacy/templatedataapi/1.0.0',
|
|
|
|
'event' => [
|
|
|
|
'template_name' => $wikiPageFactory->newFromTitle( $pageInfo['title'] )
|
|
|
|
->getTitle()->getDBkey(),
|
|
|
|
'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
|
|
|
}
|
|
|
|
|
2021-12-23 09:45:11 +00:00
|
|
|
/**
|
|
|
|
* Get parameter descriptions from raw wikitext (used for templates that have no templatedata).
|
|
|
|
* @param string $wikitext The text to extract parameters from.
|
|
|
|
* @return array[] Parameter info in the same format as the templatedata 'params' key.
|
|
|
|
*/
|
|
|
|
private function getRawParams( string $wikitext ): array {
|
2022-01-19 13:51:52 +00:00
|
|
|
// Ignore non-wikitext content in comments and wikitext-escaping tags
|
2021-12-23 09:45:11 +00:00
|
|
|
$wikitext = preg_replace( '/<!--.*?-->/s', '', $wikitext );
|
|
|
|
$wikitext = preg_replace( '/<nowiki\s*>.*?<\/nowiki\s*>/s', '', $wikitext );
|
2022-01-19 13:51:52 +00:00
|
|
|
$wikitext = preg_replace( '/<pre\s*>.*?<\/pre\s*>/s', '', $wikitext );
|
2021-12-23 09:45:11 +00:00
|
|
|
|
|
|
|
// This regex matches the one in ext.TemplateDataGenerator.sourceHandler.js
|
|
|
|
if ( !preg_match_all( '/{{{+([^\n#={|}]*?)([<|]|}}})/m', $wikitext, $rawParams ) ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$params = [];
|
|
|
|
$normalizedParams = [];
|
|
|
|
foreach ( $rawParams[1] as $rawParam ) {
|
|
|
|
// This normalization process is repeated in JS in ext.TemplateDataGenerator.sourceHandler.js
|
|
|
|
$normalizedParam = strtolower( trim( preg_replace( '/[-_ ]+/', ' ', $rawParam ) ) );
|
|
|
|
if ( !$normalizedParam || in_array( $normalizedParam, $normalizedParams ) ) {
|
|
|
|
// This or a similarly-named parameter has already been found.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$normalizedParams[] = $normalizedParam;
|
|
|
|
$params[ trim( $rawParam ) ] = [];
|
|
|
|
}
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
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' => [
|
2022-04-03 23:29:48 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'boolean',
|
2018-04-20 02:18:03 +00:00
|
|
|
],
|
|
|
|
'doNotIgnoreMissingTitles' => [
|
2022-04-03 23:29:48 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'boolean',
|
|
|
|
ParamValidator::PARAM_DEPRECATED => true,
|
2018-04-20 02:18:03 +00:00
|
|
|
],
|
|
|
|
'lang' => [
|
2022-04-03 23:29:48 +00:00
|
|
|
ParamValidator::PARAM_TYPE => 'string',
|
2018-04-20 02:18:03 +00:00
|
|
|
],
|
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 [
|
2023-01-15 22:22:43 +00:00
|
|
|
'action=templatedata&titles=Template:Foobar&includeMissingTitles=1'
|
2014-10-29 18:46:33 +00:00
|
|
|
=> 'apihelp-templatedata-example-1',
|
2023-01-15 22:22:43 +00:00
|
|
|
'action=templatedata&titles=Template:Phabricator'
|
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
|
|
|
}
|
|
|
|
}
|