X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/mediawiki.git/blobdiff_plain/87219ebd28426c6d21cb545233ee52f5f7af7dfd..refs/tags/mediawiki-1.17.0:/includes/api/ApiFormatXml.php diff --git a/includes/api/ApiFormatXml.php b/includes/api/ApiFormatXml.php index a3758a49..45ab73ef 100644 --- a/includes/api/ApiFormatXml.php +++ b/includes/api/ApiFormatXml.php @@ -1,11 +1,10 @@ @gmail.com + * Created on Sep 19, 2006 + * + * Copyright © 2006 Yuri Astrakhan @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 @@ -19,16 +18,19 @@ * * 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., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * 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' ); + require_once( 'ApiFormatBase.php' ); } /** + * API XML output formatter * @ingroup API */ class ApiFormatXml extends ApiFormatBase { @@ -38,7 +40,7 @@ class ApiFormatXml extends ApiFormatBase { private $mXslt = null; public function __construct( $main, $format ) { - parent :: __construct( $main, $format ); + parent::__construct( $main, $format ); } public function getMimeType() { @@ -59,45 +61,50 @@ class ApiFormatXml extends ApiFormatBase { $this->mXslt = $params['xslt']; $this->printText( '' ); - if ( !is_null( $this->mXslt ) ) + if ( !is_null( $this->mXslt ) ) { $this->addXslt(); - $this->printText( self::recXmlPrint( $this->mRootElemName, + } + $this->printText( + self::recXmlPrint( $this->mRootElemName, $this->getResultData(), $this->getIsHtml() ? - 2 : null, - $this->mDoubleQuote ) ); + $this->mDoubleQuote + ) + ); } /** - * This method takes an array and converts it to 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 x y z - * - * 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 text - * - * 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. - */ + * This method takes an array and converts it to 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 x y z + * + * 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 text + * + * 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. + */ public static function recXmlPrint( $elemName, $elemValue, $indent, $doublequote = false ) { $retval = ''; if ( !is_null( $indent ) ) { $indent += 2; - $indstr = "\n" . str_repeat( " ", $indent ); + $indstr = "\n" . str_repeat( ' ', $indent ); } else { $indstr = ''; } $elemName = str_replace( ' ', '_', $elemName ); switch ( gettype( $elemValue ) ) { - case 'array' : - if ( isset ( $elemValue['*'] ) ) { + case 'array': + if ( isset( $elemValue['*'] ) ) { $subElemContent = $elemValue['*']; - if ( $doublequote ) + if ( $doublequote ) { $subElemContent = Sanitizer::encodeAttribute( $subElemContent ); - unset ( $elemValue['*'] ); - + } + unset( $elemValue['*'] ); + // Add xml:space="preserve" to the // element so XML parsers will leave // whitespace in the content alone @@ -106,59 +113,65 @@ class ApiFormatXml extends ApiFormatBase { $subElemContent = null; } - if ( isset ( $elemValue['_element'] ) ) { + if ( isset( $elemValue['_element'] ) ) { $subElemIndName = $elemValue['_element']; - unset ( $elemValue['_element'] ); + unset( $elemValue['_element'] ); } else { $subElemIndName = null; } - $indElements = array (); - $subElements = array (); + $indElements = array(); + $subElements = array(); foreach ( $elemValue as $subElemId => & $subElemValue ) { - if ( is_string( $subElemValue ) && $doublequote ) + if ( is_string( $subElemValue ) && $doublequote ) { $subElemValue = Sanitizer::encodeAttribute( $subElemValue ); - + } + if ( gettype( $subElemId ) === 'integer' ) { $indElements[] = $subElemValue; - unset ( $elemValue[$subElemId] ); + unset( $elemValue[$subElemId] ); } elseif ( is_array( $subElemValue ) ) { $subElements[$subElemId] = $subElemValue; unset ( $elemValue[$subElemId] ); } } - if ( is_null( $subElemIndName ) && count( $indElements ) ) - ApiBase :: dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." ); + if ( is_null( $subElemIndName ) && count( $indElements ) ) { + ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." ); + } - if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) - ApiBase :: dieDebug( __METHOD__, "($elemName, ...) has content and subelements" ); + if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) { + ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" ); + } if ( !is_null( $subElemContent ) ) { $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent ); } elseif ( !count( $indElements ) && !count( $subElements ) ) { - $retval .= $indstr . Xml::element( $elemName, $elemValue ); + $retval .= $indstr . Xml::element( $elemName, $elemValue ); } else { $retval .= $indstr . Xml::element( $elemName, $elemValue, null ); - foreach ( $subElements as $subElemId => & $subElemValue ) + foreach ( $subElements as $subElemId => & $subElemValue ) { $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent ); + } - foreach ( $indElements as $subElemId => & $subElemValue ) + foreach ( $indElements as &$subElemValue ) { $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent ); + } $retval .= $indstr . Xml::closeElement( $elemName ); } break; - case 'object' : + case 'object': // ignore break; - default : + default: $retval .= $indstr . Xml::element( $elemName, null, $elemValue ); break; } return $retval; } + function addXslt() { $nt = Title::newFromText( $this->mXslt ); if ( is_null( $nt ) || !$nt->exists() ) { @@ -175,26 +188,26 @@ class ApiFormatXml extends ApiFormatBase { } $this->printText( 'escapeLocalURL( 'action=raw' ) . '" type="text/xsl" ?>' ); } - + public function getAllowedParams() { - return array ( + return array( 'xmldoublequote' => false, 'xslt' => null, ); } public function getParamDescription() { - return array ( - 'xmldoublequote' => 'If specified, double quotes all attributes and content.', + return array( + 'xmldoublequote' => 'If specified, double quotes all attributes and content', 'xslt' => 'If specified, adds as stylesheet', ); } public function getDescription() { - return 'Output data in XML format' . parent :: getDescription(); + return 'Output data in XML format' . parent::getDescription(); } public function getVersion() { - return __CLASS__ . ': $Id: ApiFormatXml.php 62402 2010-02-13 00:09:05Z reedy $'; + return __CLASS__ . ': $Id: ApiFormatXml.php 73753 2010-09-25 16:56:03Z reedy $'; } }