Revert part of Brion's 27627: please don't throw away the child (maxlag) with the...
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
index 301130b..c811444 100644 (file)
@@ -1,12 +1,11 @@
 <?php
 
-
 /*
  * Created on Sep 19, 2006
  *
  * API for MediaWiki 1.8+
  *
- * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
+ * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * 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
 
 if (!defined('MEDIAWIKI')) {
        // Eclipse helper - will be ignored in production
-       require_once ("ApiFormatBase.php");
+       require_once ('ApiFormatBase.php');
 }
 
+/**
+ * @addtogroup API
+ */
 class ApiFormatXml extends ApiFormatBase {
 
+       private $mRootElemName = 'api';
+
        public function __construct($main, $format) {
                parent :: __construct($main, $format);
        }
 
-       public function GetMimeType() {
+       public function getMimeType() {
                return 'text/xml';
        }
-       
-       public function GetNeedsRawData() {
+
+       public function getNeedsRawData() {
                return true;
        }
+       
+       public function setRootElement($rootElemName) {
+               $this->mRootElemName = $rootElemName;
+       }
 
-       public function Execute() {
-               $xmlindent = null;
-               extract($this->ExtractRequestParams());
-
-               if ($xmlindent || $this->GetIsHtml())
-                       $xmlindent = -2;
-               else
-                       $xmlindent = null;
-
-               $this->PrintText('<?xml version="1.0" encoding="utf-8"?>');
-               $this->recXmlPrint('api', $this->GetResult()->GetData(), $xmlindent);
+       public function execute() {
+               $this->printText('<?xml version="1.0" encoding="utf-8"?>');
+               $this->recXmlPrint($this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? -2 : null);
        }
 
        /**
        * This method takes an array and converts it into an xml.
        * There are several noteworthy cases:
        *
-       *  If array contains a key "_element", then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
-       *       Example:        name="root",  value = array( "_element"=>"page", "x", "y", "z") creates <root>  <page>x</page>  <page>y</page>  <page>z</page> </root>
+       *  If array contains a key '_element', then the code assumes that ALL other keys are not important and replaces them with the value['_element'].
+       *       Example:        name='root',  value = array( '_element'=>'page', 'x', 'y', 'z') creates <root>  <page>x</page>  <page>y</page>  <page>z</page> </root>
        *
-       *  If any of the array's element key is "*", then the code treats all other key->value pairs as attributes, and the value['*'] as the element's content.
-       *       Example:        name="root",  value = array( "*"=>"text", "lang"=>"en", "id"=>10)   creates  <root lang="en" id="10">text</root>
+       *  If any of the array's element key is '*', then the code treats all other key->value pairs as attributes, and the value['*'] as the element's content.
+       *       Example:        name='root',  value = array( '*'=>'text', 'lang'=>'en', 'id'=>10)   creates  <root lang='en' id='10'>text</root>
        *
        * If neither key is found, all keys become element names, and values become element content.
        * The method is recursive, so the same rules apply to any sub-arrays.
        */
        function recXmlPrint($elemName, $elemValue, $indent) {
-               $indstr = "";
                if (!is_null($indent)) {
                        $indent += 2;
                        $indstr = "\n" . str_repeat(" ", $indent);
+               } else {
+                       $indstr = '';
                }
 
                switch (gettype($elemValue)) {
                        case 'array' :
-                               if (array_key_exists('*', $elemValue)) {
+
+                               if (isset ($elemValue['*'])) {
                                        $subElemContent = $elemValue['*'];
                                        unset ($elemValue['*']);
-                                       if (gettype($subElemContent) === 'array') {
-                                               $this->PrintText($indstr . wfElement($elemName, $elemValue, null));
-                                               $this->recXmlPrint($elemName, $subElemContent, $indent);
-                                               $this->PrintText($indstr . "</$elemName>");
-                                       } else {
-                                               $this->PrintText($indstr . wfElement($elemName, $elemValue, $subElemContent));
-                                       }
                                } else {
-                                       $this->PrintText($indstr . wfElement($elemName, null, null));
-                                       if (array_key_exists('_element', $elemValue)) {
-                                               $subElemName = $elemValue['_element'];
-                                               foreach ($elemValue as $subElemId => & $subElemValue) {
-                                                       if ($subElemId !== '_element') {
-                                                               $this->recXmlPrint($subElemName, $subElemValue, $indent);
-                                                       }
-                                               }
-                                       } else {
-                                               foreach ($elemValue as $subElemName => & $subElemValue) {
-                                                       $this->recXmlPrint($subElemName, $subElemValue, $indent);
-                                               }
+                                       $subElemContent = null;
+                               }
+
+                               if (isset ($elemValue['_element'])) {
+                                       $subElemIndName = $elemValue['_element'];
+                                       unset ($elemValue['_element']);
+                               } else {
+                                       $subElemIndName = null;
+                               }
+
+                               $indElements = array ();
+                               $subElements = array ();
+                               foreach ($elemValue as $subElemId => & $subElemValue) {
+                                       if (gettype($subElemId) === 'integer') {
+                                               $indElements[] = $subElemValue;
+                                               unset ($elemValue[$subElemId]);
+                                       } elseif (is_array($subElemValue)) {
+                                               $subElements[$subElemId] = $subElemValue;
+                                               unset ($elemValue[$subElemId]);
                                        }
-                                       $this->PrintText($indstr . "</$elemName>");
+                               }
+
+                               if (is_null($subElemIndName) && !empty ($indElements))
+                                       ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().");
+
+                               if (!empty ($subElements) && !empty ($indElements) && !is_null($subElemContent))
+                                       ApiBase :: dieDebug(__METHOD__, "($elemName, ...) has content and subelements");
+
+                               if (!is_null($subElemContent)) {
+                                       $this->printText($indstr . wfElement($elemName, $elemValue, $subElemContent));
+                               } elseif (empty ($indElements) && empty ($subElements)) {
+                                               $this->printText($indstr . wfElement($elemName, $elemValue));
+                               } else {
+                                       $this->printText($indstr . wfElement($elemName, $elemValue, null));
+
+                                       foreach ($subElements as $subElemId => & $subElemValue)
+                                               $this->recXmlPrint($subElemId, $subElemValue, $indent);
+
+                                       foreach ($indElements as $subElemId => & $subElemValue)
+                                               $this->recXmlPrint($subElemIndName, $subElemValue, $indent);
+
+                                       $this->printText($indstr . wfCloseElement($elemName));
                                }
                                break;
                        case 'object' :
                                // ignore
                                break;
                        default :
-                               $this->PrintText($indstr . wfElement($elemName, null, $elemValue));
+                               $this->printText($indstr . wfElement($elemName, null, $elemValue));
                                break;
                }
        }
-       protected function GetDescription() {
-               return 'Output data in XML format';
+       protected function getDescription() {
+               return 'Output data in XML format' . parent :: getDescription();
        }
 
-       protected function GetAllowedParams() {
-               return array (
-                       'xmlindent' => false
-               );
-       }
-
-       protected function GetParamDescription() {
-               return array (
-                       'xmlindent' => 'Enable XML indentation'
-               );
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
        }
 }
-?>
\ No newline at end of file
+