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->execute();
$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) [] );
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 = [];
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 ( $legacyMode ) {
$this->addDeprecation(
'apiwarn-templatedata-deprecation-legacyMode', 'action=templatedata&!doNotIgnoreMissingTitles'
);
} else {
foreach ( $missingTitles as $missingTitleId => $missingTitle ) {
$resp[ $missingTitleId ] = [ 'title' => $missingTitle, 'missing' => true ];
}
if ( $langCode ) {
$data = $tdb->getDataInLanguage( $langCode );
} else {
$data = $tdb->getData();
foreach ( $titles as $titleId => $title ) {
$resp[ $titleId ] = [ 'title' => $title, 'notemplatedata' => true ];
}
}
// 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' ];
if ( count( $titles ) ) {
$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' ]
);
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' );
@ -122,6 +146,7 @@ class ApiTemplateData extends ApiBase {
public function getAllowedParams( $flags = 0 ) {
return $this->getPageSet()->getFinalParams( $flags ) + [
'doNotIgnoreMissingTitles' => false,
'lang' => null
];
}
@ -131,8 +156,10 @@ class ApiTemplateData extends ApiBase {
*/
protected function getExamplesMessages() {
return [
'action=templatedata&titles=Template:Stub|Template:Example'
'action=templatedata&titles=Template:Stub|Template:Example&doNotIgnoreMissingTitles=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-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.",
"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.",
"templatedata-desc": "Implement data storage for template parameters (using JSON)",
"templatedata-doc-desc-empty": "No description.",

View file

@ -20,9 +20,12 @@
},
"apihelp-templatedata-description": "{{doc-apihelp-description|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}}",
"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-legacyMode": "{{doc-apierror}}",
"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-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\")",