Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/56204
Change subject: API: Transform into a standalone module instead of a query
submodule.
......................................................................
API: Transform into a standalone module instead of a query submodule.
Change-Id: Ia858fa43a50ade947e6353d22dc89a519c3da61a
---
M TemplateData.php
D api/ApiQueryTemplateData.php
A api/ApiTemplateData.php
3 files changed, 127 insertions(+), 87 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/TemplateData
refs/changes/04/56204/1
diff --git a/TemplateData.php b/TemplateData.php
index 8ccbd63..44e53c7 100644
--- a/TemplateData.php
+++ b/TemplateData.php
@@ -28,16 +28,16 @@
$wgExtensionMessagesFiles['TemplateData'] = $dir . '/TemplateData.i18n.php';
$wgAutoloadClasses['TemplateDataHooks'] = $dir . '/TemplateData.hooks.php';
$wgAutoloadClasses['TemplateDataBlob'] = $dir . '/TemplateDataBlob.php';
-$wgAutoloadClasses['ApiQueryTemplateData'] = $dir .
'/api/ApiQueryTemplateData.php';
+$wgAutoloadClasses['ApiTemplateData'] = $dir . '/api/ApiTemplateData.php';
// Register hooks
$wgHooks['ParserFirstCallInit'][] = 'TemplateDataHooks::onParserFirstCallInit';
$wgHooks['PageContentSave'][] = 'TemplateDataHooks::onPageContentSave';
-// Register API actions
-$wgAPIPropModules['templatedata'] = 'ApiQueryTemplateData';
+// Register APIs
+$wgAPIModules['templatedata'] = 'ApiTemplateData';
-// Register page_props
+// Register page properties
$wgPageProps['templatedata'] = 'Content of <templatedata> tag';
// Register modules
diff --git a/api/ApiQueryTemplateData.php b/api/ApiQueryTemplateData.php
deleted file mode 100644
index 8a0053a..0000000
--- a/api/ApiQueryTemplateData.php
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-/**
- * Implement the 'templatedata' query module in the API.
- * Format JSON only.
- *
- * @ingroup API
- * @emits error.code templatedata-corrupt
- */
-class ApiQueryTemplateData extends ApiQueryBase {
-
- public function __construct( $query, $module ) {
- parent::__construct( $query, $module, 'td' );
- }
-
- /**
- * TODO: This currently outputs it in an ugly '*' property
- * and it fails in formats like XML (works in JSON/YAML).
- */
- public function execute() {
- $params = $this->extractRequestParams();
- $titles = $this->getPageSet()->getGoodTitles(); // page_id =>
Title object
-
- if ( !count( $titles ) ) {
- return;
- }
-
- $this->addTables( 'page_props' );
- $this->addFields( array( 'pp_page', 'pp_value' ) );
- $this->addWhere( array(
- 'pp_page' => array_keys( $titles ),
- 'pp_propname' => 'templatedata'
- ) );
- $this->addOption( 'ORDER BY', 'pp_page' );
-
- if ( $params['continue'] !== null ) {
- $fromid = intval( $params['continue'] );
- $this->addWhere( "pp_page >= $fromid" );
- }
-
- $res = $this->select( __METHOD__ );
- foreach ( $res as $row ) {
- $rawData = $row->pp_value;
- $data = json_decode( $rawData );
-
- if ( !$data ) {
- $this->dieUsage( 'Database data is corrupted.',
'templatedata-corrupt' );
- }
- $value = array();
- ApiResult::setContent( $value, $data->params, 'params'
);
-
- $fit = $this->addPageSubItems( $row->pp_page, $value );
-
- if ( !$fit ) {
- $this->setContinueEnumParameter( 'continue',
$row->pp_page );
- break;
- }
- }
- }
-
- public function getAllowedParams() {
- return array(
- 'continue' => null,
- );
- }
-
- public function getParamDescription() {
- return array(
- 'continue' => 'When more results are available, use
this to continue',
- );
- }
-
- public function getDescription() {
- return 'Data stored by the TemplateData extension
(https://www.mediawiki.org/Extension:TemplateData)';
- }
-
- // getPossibleErrors() is provided by ApiQueryBase
-
- protected function getExamples() {
- return array(
-
'api.php?action=query&prop=templatedata&titles=Template:Stub|Template:Example',
- );
- }
-}
diff --git a/api/ApiTemplateData.php b/api/ApiTemplateData.php
new file mode 100644
index 0000000..94305c9
--- /dev/null
+++ b/api/ApiTemplateData.php
@@ -0,0 +1,123 @@
+<?php
+/**
+ * Implement the 'templatedata' query module in the API.
+ * Format JSON only.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * @ingroup API
+ * @emits error.code templatedata-corrupt
+ */
+class ApiTemplateData extends ApiBase {
+
+ public function getCustomPrinter() {
+ return $this->getMain()->createPrinterByName( 'json' );
+ }
+
+ /**
+ * @return ApiPageSet
+ */
+ private function getPageSet() {
+ if ( !isset( $this->mPageSet ) ) {
+ $this->mPageSet = new ApiPageSet( $this );
+ }
+ return $this->mPageSet;
+ }
+
+ public function execute() {
+ $params = $this->extractRequestParams();
+ $result = $this->getResult();
+
+ $pageSet = $this->getPageSet();
+ $pageSet->execute();
+ $titles = $pageSet->getGoodTitles(); // page_id => Title object
+
+ if ( !count( $titles ) ) {
+ $result->addValue( null, 'pages', (object) array() );
+ return;
+ }
+
+ $db = $this->getDB();
+ $res = $db->select( 'page_props',
+ array( 'pp_page', 'pp_value' ), array(
+ 'pp_page' => array_keys( $titles ),
+ 'pp_propname' => 'templatedata'
+ ),
+ __METHOD__,
+ array( 'ORDER BY', 'pp_page' )
+ );
+
+ $resp = array();
+
+ foreach ( $res as $row ) {
+ $rawData = $row->pp_value;
+ $data = json_decode( $rawData );
+
+ if ( !$data ) {
+ $this->dieUsage( 'Corrupt data found in
templatedata storage for page #' . intval( $row->pp_page ),
'templatedata-corrupt' );
+ }
+ $resp[$row->pp_page] = array(
+ 'title' => strval( $titles[$row->pp_page] ),
+ 'params' => $data->params,
+ );
+ }
+
+ // Set top level element
+ $result->addValue( null, 'pages', (object) $resp );
+
+ $values = $pageSet->getNormalizedTitlesAsResult( $result );
+ if ( $values ) {
+ $result->addValue( null, 'normalized', $values );
+ }
+ }
+
+ public function getAllowedParams( $flags = 0 ) {
+ $result = array();
+ if ( $flags ) {
+ $result += $this->getPageSet()->getFinalParams( $flags
);
+ }
+ return $result;
+ }
+
+ public function getParamDescription() {
+ return $this->getPageSet()->getParamDescription();
+ }
+
+ public function getDescription() {
+ return 'Data stored by the TemplateData extension';
+ }
+
+ public function getPossibleErrors() {
+ return array_merge(
+ parent::getPossibleErrors(),
+ $this->getPageSet()->getPossibleErrors()
+ );
+ }
+
+ public function getExamples() {
+ return array(
+
'api.php?action=templatedata&titles=Template:Stub|Template:Example',
+ );
+ }
+
+ public function getHelpUrls() {
+ return 'https://www.mediawiki.org/Extension:TemplateData';
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/56204
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia858fa43a50ade947e6353d22dc89a519c3da61a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/TemplateData
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits