Merge "API: Identify missing and no-TemplateData pages in the response"

This commit is contained in:
jenkins-bot 2017-04-24 20:36:23 +00:00 committed by Gerrit Code Review
commit 069e9997be
3 changed files with 73 additions and 40 deletions

View file

@ -52,56 +52,80 @@ class ApiTemplateData extends ApiBase {
$pageSet = $this->getPageSet(); $pageSet = $this->getPageSet();
$pageSet->execute(); $pageSet->execute();
$titles = $pageSet->getGoodTitles(); // page_id => Title object $titles = $pageSet->getGoodTitles(); // page_id => Title object
$missingTitles = $pageSet->getMissingTitles(); // page_id => Title object
if ( !count( $titles ) ) { $legacyMode = !$this->getParameter( 'doNotIgnoreMissingTitles' );
if ( !count( $titles ) && ( $legacyMode || !count( $missingTitles ) ) ) {
$result->addValue( null, 'pages', (object) [] ); $result->addValue( null, 'pages', (object) [] );
return; return;
} }
$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' ]
);
$resp = []; $resp = [];
foreach ( $res as $row ) { if ( $legacyMode ) {
$rawData = $row->pp_value; $this->addDeprecation(
$tdb = TemplateDataBlob::newFromDatabase( $rawData ); 'apiwarn-templatedata-deprecation-legacyMode', 'action=templatedata&!doNotIgnoreMissingTitles'
$status = $tdb->getStatus(); );
} else {
if ( !$status->isOK() ) { foreach ( $missingTitles as $missingTitleId => $missingTitle ) {
$this->dieWithError( $resp[ $missingTitleId ] = [ 'title' => $missingTitle, 'missing' => true ];
[ 'apierror-templatedata-corrupt', intval( $row->pp_page ), $status->getMessage() ]
);
} }
if ( $langCode ) { foreach ( $titles as $titleId => $title ) {
$data = $tdb->getDataInLanguage( $langCode ); $resp[ $titleId ] = [ 'title' => $title, 'notemplatedata' => true ];
} else {
$data = $tdb->getData();
} }
}
// HACK: don't let ApiResult's formatversion=1 compatibility layer mangle our booleans if ( count( $titles ) ) {
// to empty strings / absent properties $db = $this->getDB();
foreach ( $data->params as &$param ) { $res = $db->select( 'page_props',
$param->{ApiResult::META_BC_BOOLS} = [ 'required', 'suggested', 'deprecated' ]; [ 'pp_page', 'pp_value' ], [
'pp_page' => array_keys( $titles ),
'pp_propname' => 'templatedata'
],
__METHOD__,
[ 'ORDER BY', 'pp_page' ]
);
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() ]
);
}
if ( $langCode ) {
$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';
ApiResult::setIndexedTagName( $data->paramOrder, 'p' );
if ( count( $data ) ) {
if ( !$legacyMode ) {
unset( $resp[$row->pp_page]['notemplatedata'] );
} else {
$resp[ $row->pp_page ] = [ 'title' => $titles[ $row->pp_page ] ];
}
$resp[$row->pp_page] += (array) $data;
}
} }
unset( $param );
$data->params->{ApiResult::META_TYPE} = 'kvp';
$data->params->{ApiResult::META_KVP_KEY_NAME} = 'key';
$data->params->{ApiResult::META_INDEXED_TAG_NAME} = 'param';
ApiResult::setIndexedTagName( $data->paramOrder, 'p' );
$resp[$row->pp_page] = [
'title' => strval( $titles[$row->pp_page] ),
] + (array) $data;
} }
ApiResult::setArrayType( $resp, 'kvp', 'id' ); ApiResult::setArrayType( $resp, 'kvp', 'id' );
@ -122,6 +146,7 @@ class ApiTemplateData extends ApiBase {
public function getAllowedParams( $flags = 0 ) { public function getAllowedParams( $flags = 0 ) {
return $this->getPageSet()->getFinalParams( $flags ) + [ return $this->getPageSet()->getFinalParams( $flags ) + [
'doNotIgnoreMissingTitles' => false,
'lang' => null 'lang' => null
]; ];
} }
@ -131,8 +156,10 @@ class ApiTemplateData extends ApiBase {
*/ */
protected function getExamplesMessages() { protected function getExamplesMessages() {
return [ return [
'action=templatedata&titles=Template:Stub|Template:Example' 'action=templatedata&titles=Template:Stub|Template:Example&doNotIgnoreMissingTitles=1'
=> 'apihelp-templatedata-example-1', => 'apihelp-templatedata-example-1',
'action=templatedata&titles=Template:Stub|Template:Example&doNotIgnoreMissingTitles=0'
=> 'apihelp-templatedata-example-2',
]; ];
} }

View file

@ -8,9 +8,12 @@
] ]
}, },
"apihelp-templatedata-description": "Fetch data stored by the TemplateData extension.", "apihelp-templatedata-description": "Fetch data stored by the TemplateData extension.",
"apihelp-templatedata-example-1": "Return data for [[Template:Stub]] and [[Template:Example]]", "apihelp-templatedata-example-1": "Return TemplateData for [[Template:Stub]] and [[Template:Example]], with results if the templates do not exist or do exist but have no TemplateData",
"apihelp-templatedata-example-2": "Return TemplateData for [[Template:Stub]] and [[Template:Example]], with no results if the templates do not exist or do exist but have no TemplateData",
"apihelp-templatedata-param-doNotIgnoreMissingTitles": "Return data about titles even if they are missing or lack TemplateData. By default (for backwards compatibility) titles are only returned if they exist and have TemplateData.",
"apihelp-templatedata-param-lang": "Return localized values in this language. By default all available translations are returned.", "apihelp-templatedata-param-lang": "Return localized values in this language. By default all available translations are returned.",
"apierror-templatedata-corrupt": "Page #$1 templatedata contains invalid data: $2", "apierror-templatedata-corrupt": "Page #$1 templatedata contains invalid data: $2",
"apiwarn-templatedata-deprecation-legacyMode": "The default output will change to include missing titles in the future. Please specify <kbd>doNotIgnoreMissingTitles=1</kbd> explicitly.",
"apiwarn-templatedata-deprecation-format": "The default output format will change to <kbd>jsonfm</kbd> in the future. Please specify <kbd>format=json</kbd> explicitly.", "apiwarn-templatedata-deprecation-format": "The default output format will change to <kbd>jsonfm</kbd> in the future. Please specify <kbd>format=json</kbd> explicitly.",
"templatedata-desc": "Implement data storage for template parameters (using JSON)", "templatedata-desc": "Implement data storage for template parameters (using JSON)",
"templatedata-doc-desc-empty": "No description.", "templatedata-doc-desc-empty": "No description.",

View file

@ -20,9 +20,12 @@
}, },
"apihelp-templatedata-description": "{{doc-apihelp-description|templatedata}}", "apihelp-templatedata-description": "{{doc-apihelp-description|templatedata}}",
"apihelp-templatedata-example-1": "{{doc-apihelp-example|templatedata}}", "apihelp-templatedata-example-1": "{{doc-apihelp-example|templatedata}}",
"apihelp-templatedata-example-2": "{{doc-apihelp-example|templatedata}}",
"apihelp-templatedata-param-doNotIgnoreMissingTitles": "{{doc-apihelp-param|templatedata|doNotIgnoreMissingTitles}}",
"apihelp-templatedata-param-lang": "{{doc-apihelp-param|templatedata|lang}}", "apihelp-templatedata-param-lang": "{{doc-apihelp-param|templatedata|lang}}",
"apierror-templatedata-corrupt": "{{doc-apierror}}\n\nParameters:\n* $1 - Page ID number\n* $2 - Localized message with further information.", "apierror-templatedata-corrupt": "{{doc-apierror}}\n\nParameters:\n* $1 - Page ID number\n* $2 - Localized message with further information.",
"apiwarn-templatedata-deprecation-format": "{{doc-apierror}}", "apiwarn-templatedata-deprecation-format": "{{doc-apierror}}",
"apiwarn-templatedata-deprecation-legacyMode": "{{doc-apierror}}",
"templatedata-desc": "{{desc|name=Template Data|url=https://www.mediawiki.org/wiki/Extension:TemplateData}}", "templatedata-desc": "{{desc|name=Template Data|url=https://www.mediawiki.org/wiki/Extension:TemplateData}}",
"templatedata-doc-desc-empty": "Displayed when a template has no description (should be a valid sentence).\n{{Identical|No description}}", "templatedata-doc-desc-empty": "Displayed when a template has no description (should be a valid sentence).\n{{Identical|No description}}",
"templatedata-doc-format-block": "Use block formatting of the template parameters in wikitext, i.e.\n\n<pre><nowiki>{{Template name\n| p1 = v1\n| p2 = v2\n}}</nowiki></pre>\n\nas opposed to <nowiki>{{Template name|p1=v1|p2=v2}}</nowiki> (\"inline\")", "templatedata-doc-format-block": "Use block formatting of the template parameters in wikitext, i.e.\n\n<pre><nowiki>{{Template name\n| p1 = v1\n| p2 = v2\n}}</nowiki></pre>\n\nas opposed to <nowiki>{{Template name|p1=v1|p2=v2}}</nowiki> (\"inline\")",