From: Victor Vasiliev Date: Sat, 17 Nov 2007 16:45:59 +0000 (+0000) Subject: API: X-Git-Tag: 1.31.0-rc.0~50837 X-Git-Url: http://git.cyclocoop.org/%22.%24match%5B1%5D.%22?a=commitdiff_plain;h=60fc9cb245c23ce3554bd04afd42061ce7988929;p=lhc%2Fweb%2Fwiklou.git API: * Add format=raw * Added raw output support to ApiExpandTemplates and ApiRender --- diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 6dfca8a4c6..1e96146fbf 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -187,6 +187,7 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API * (bug 11562) Added a user_registration parameter/field to the list=allusers query. * (bug 11588) Preserve document structure for empty dataset in backlinks query. * Outputting list of all user preferences rather than having to request them by name +* Add raw formatting support. Now several actions like expandtemplates support raw output with format=raw === Languages updated in 1.12 === diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 828be04a9a..aa6d9091d5 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -301,6 +301,7 @@ function __autoload($className) { 'Services_JSON' => 'includes/api/ApiFormatJson_json.php', 'ApiFormatJson' => 'includes/api/ApiFormatJson.php', 'ApiFormatPhp' => 'includes/api/ApiFormatPhp.php', + 'ApiFormatRaw' => 'includes/api/ApiFormatBase.php', 'ApiFormatWddx' => 'includes/api/ApiFormatWddx.php', 'ApiFormatXml' => 'includes/api/ApiFormatXml.php', 'Spyc' => 'includes/api/ApiFormatYaml_spyc.php', diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index 37bd4e767a..585b22ee68 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -509,6 +509,29 @@ abstract class ApiBase { wfDebugDieBacktrace("Internal error in $method: $message"); } + private $mRawFormat = false; + + /** + * Returns if module supports raw mode + */ + public function supportRaw() { + return false; + } + + /** + * Enables raw mode + */ + public function setRaw() { + $this->mRawFormat = true; + } + + /** + * Checks if raw mode is enabled + */ + public function isRaw() { + return $this->mRawFormat; + } + /** * Profiling: total module execution time */ @@ -617,3 +640,4 @@ abstract class ApiBase { } } + diff --git a/includes/api/ApiExpandTemplates.php b/includes/api/ApiExpandTemplates.php index df99ba1099..d043bcbcd2 100644 --- a/includes/api/ApiExpandTemplates.php +++ b/includes/api/ApiExpandTemplates.php @@ -55,11 +55,18 @@ class ApiExpandTemplates extends ApiBase { // Return result $result = $this->getResult(); + if( $this->isRaw() ) { + ApiFormatRaw :: setRawData( $result, $retval ); + } $retval_array = array(); $result->setContent( $retval_array, $retval ); $result->addValue( null, $this->getModuleName(), $retval_array ); } + public function supportRaw() { + return true; + } + protected function getAllowedParams() { return array ( 'title' => array( @@ -91,3 +98,4 @@ class ApiExpandTemplates extends ApiBase { } } + diff --git a/includes/api/ApiFormatBase.php b/includes/api/ApiFormatBase.php index 5bfbd9e12a..84205b7169 100644 --- a/includes/api/ApiFormatBase.php +++ b/includes/api/ApiFormatBase.php @@ -276,3 +276,49 @@ class ApiFormatFeedWrapper extends ApiFormatBase { return __CLASS__ . ': $Id$'; } } + +/** + * This printer is used to wrap raw printer + * @addtogroup API + */ +class ApiFormatRaw extends ApiFormatBase { + + public function __construct($main, $format) { + parent :: __construct($main, $format); + } + + public static function setRawData( $result, $raw_data ) { + $data = & $result->getData(); + $data['_raw'] = $raw_data; + } + + public function getMimeType() { + return 'text/plain'; + } + + public function execute() { + $data = $this->getResultData(); + if( !isset( $data['_raw'] ) && !isset( $data['error'] ) ) { + ApiBase :: dieDebug( 'ApiFormatRaw', 'No raw data is set for this module' ); + } + elseif( isset( $data['error'] ) ) { + header( '500 Internal error' ); + echo "{$data['error']['code']}\n"; + echo "{$data['error']['info']}\n"; + return; + } + $this->printText( $data['_raw'] ); + } + + public function getNeedsRawData() { + return true; + } + + protected function getDescription() { + return 'Output data in raw format. NOTE: not all actions support it' . parent :: getDescription(); + } + + public function getVersion() { + return __CLASS__ . ': $Id$'; + } +} diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 406d23a018..e5fc508147 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -75,6 +75,7 @@ class ApiMain extends ApiBase { 'xmlfm' => 'ApiFormatXml', 'yaml' => 'ApiFormatYaml', 'yamlfm' => 'ApiFormatYaml', + 'raw' => 'ApiFormatRaw', 'rawfm' => 'ApiFormatJson' ); @@ -292,6 +293,15 @@ class ApiMain extends ApiBase { if (!$this->mInternalMode) { + //Check usage of raw printer + if( $params['format'] == 'raw' ) { + if( !$module->supportRaw() ) { + ApiBase :: dieUsage( 'This module doesn\'t support format=raw', 'rawnotsupported' ); + return; + } + $module->setRaw(); + } + // See if custom printer is used $this->mPrinter = $module->getCustomPrinter(); if (is_null($this->mPrinter)) { @@ -543,3 +553,4 @@ class UsageException extends Exception { } + diff --git a/includes/api/ApiRender.php b/includes/api/ApiRender.php index 9302da392a..1a101386f4 100644 --- a/includes/api/ApiRender.php +++ b/includes/api/ApiRender.php @@ -56,11 +56,18 @@ class ApiRender extends ApiBase { // Return result $result = $this->getResult(); + if( $this->isRaw() ) { + ApiFormatRaw :: setRawData( $result, $retval ); + } $retval_array = array(); $result->setContent( $retval_array, $retval ); $result->addValue( null, $this->getModuleName(), $retval_array ); } + public function supportRaw() { + return true; + } + protected function getAllowedParams() { return array ( 'title' => array( @@ -92,3 +99,4 @@ class ApiRender extends ApiBase { } } +