Merge "Revert "Adding sanity check to Title::isRedirect().""
[lhc/web/wiklou.git] / includes / api / ApiFormatXml.php
index eaf2c23..ef2c54f 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-
 /**
- * Created on Sep 19, 2006
  *
- * API for MediaWiki 1.8+
+ *
+ * Created on Sep 19, 2006
  *
  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * 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
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( 'ApiFormatBase.php' );
-}
-
 /**
  * API XML output formatter
  * @ingroup API
@@ -35,7 +31,9 @@ if ( !defined( 'MEDIAWIKI' ) ) {
 class ApiFormatXml extends ApiFormatBase {
 
        private $mRootElemName = 'api';
+       public static $namespace = 'http://www.mediawiki.org/xml/api/';
        private $mDoubleQuote = false;
+       private $mIncludeNamespace = false;
        private $mXslt = null;
 
        public function __construct( $main, $format ) {
@@ -57,15 +55,26 @@ class ApiFormatXml extends ApiFormatBase {
        public function execute() {
                $params = $this->extractRequestParams();
                $this->mDoubleQuote = $params['xmldoublequote'];
+               $this->mIncludeNamespace = $params['includexmlnamespace'];
                $this->mXslt = $params['xslt'];
 
                $this->printText( '<?xml version="1.0"?>' );
                if ( !is_null( $this->mXslt ) ) {
                        $this->addXslt();
                }
+               if ( $this->mIncludeNamespace ) {
+                       // If the result data already contains an 'xmlns' namespace added
+                       // for custom XML output types, it will override the one for the
+                       // generic API results.
+                       // This allows API output of other XML types like Atom, RSS, RSD.
+                       $data = $this->getResultData() + array( 'xmlns' => self::$namespace );
+               } else {
+                       $data = $this->getResultData();
+               }
+
                $this->printText(
                        self::recXmlPrint( $this->mRootElemName,
-                               $this->getResultData(),
+                               $data,
                                $this->getIsHtml() ? - 2 : null,
                                $this->mDoubleQuote
                        )
@@ -84,6 +93,13 @@ class ApiFormatXml extends ApiFormatBase {
         *
         * 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.
+        *
+        * @param $elemName
+        * @param $elemValue
+        * @param $indent
+        * @param $doublequote bool
+        *
+        * @return string
         */
        public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) {
                $retval = '';
@@ -154,7 +170,7 @@ class ApiFormatXml extends ApiFormatBase {
                                                $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
                                        }
 
-                                       foreach ( $indElements as $subElemId => & $subElemValue ) {
+                                       foreach ( $indElements as &$subElemValue ) {
                                                $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
                                        }
 
@@ -185,20 +201,23 @@ class ApiFormatXml extends ApiFormatBase {
                        $this->setWarning( 'Stylesheet should have .xsl extension.' );
                        return;
                }
-               $this->printText( '<?xml-stylesheet href="' . $nt->escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' );
+               $this->printText( '<?xml-stylesheet href="' . htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
        }
 
        public function getAllowedParams() {
                return array(
                        'xmldoublequote' => false,
                        'xslt' => null,
+                       'includexmlnamespace' => false,
                );
        }
 
        public function getParamDescription() {
                return array(
                        'xmldoublequote' => 'If specified, double quotes all attributes and content',
-                       'xslt' => 'If specified, adds <xslt> as stylesheet',
+                       'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
+                               . 'in the MediaWiki namespace whose page name ends with ".xsl"',
+                       'includexmlnamespace' => 'If specified, adds an XML namespace'
                );
        }