API:
authorVictor Vasiliev <vasilievvv@users.mediawiki.org>
Sat, 17 Nov 2007 16:45:59 +0000 (16:45 +0000)
committerVictor Vasiliev <vasilievvv@users.mediawiki.org>
Sat, 17 Nov 2007 16:45:59 +0000 (16:45 +0000)
* Add format=raw
* Added raw output support to ApiExpandTemplates and ApiRender

RELEASE-NOTES
includes/AutoLoader.php
includes/api/ApiBase.php
includes/api/ApiExpandTemplates.php
includes/api/ApiFormatBase.php
includes/api/ApiMain.php
includes/api/ApiRender.php

index 6dfca8a..1e96146 100644 (file)
@@ -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 ===
 
index 828be04..aa6d909 100644 (file)
@@ -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',
index 37bd4e7..585b22e 100644 (file)
@@ -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 {
        }
 }
 
+
index df99ba1..d043bcb 100644 (file)
@@ -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 {
        }
 }
 
+
index 5bfbd9e..84205b7 100644 (file)
@@ -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$';
+       }
+}
index 406d23a..e5fc508 100644 (file)
@@ -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 {
 }
 
 
+
index 9302da3..1a10138 100644 (file)
@@ -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 {
        }
 }
 
+