X-Git-Url: https://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2Fapi%2FApiFormatXml.php;h=183d48c44f0bcbe0d84cef5fd212869fecc13f81;hb=2772d8c1a6250507e699930fc8e030e58d345e23;hp=5ccf1859a08ee9c17758991ec470ccacbcfe979d;hpb=bc4c78889f9544e81bef8f88d09149a3d6d8a90c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/api/ApiFormatXml.php b/includes/api/ApiFormatXml.php index 5ccf1859a0..183d48c44f 100644 --- a/includes/api/ApiFormatXml.php +++ b/includes/api/ApiFormatXml.php @@ -36,10 +36,6 @@ class ApiFormatXml extends ApiFormatBase { private $mIncludeNamespace = false; private $mXslt = null; - public function __construct( $main, $format ) { - parent::__construct( $main, $format ); - } - public function getMimeType() { return 'text/xml'; } @@ -92,7 +88,7 @@ class ApiFormatXml extends ApiFormatBase { * * @par Example: * @verbatim - * name='root', value = array( '_element'=>'page', 'x', 'y', 'z') + * name='root', value = array( '_element'=>'page', 'x', 'y', 'z') * @endverbatim * creates: * @verbatim @@ -105,7 +101,7 @@ class ApiFormatXml extends ApiFormatBase { * * @par Example: * @verbatim - * name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) + * name='root', value = array( '*'=>'text', 'lang'=>'en', 'id'=>10) * @endverbatim * creates: * @verbatim @@ -135,78 +131,78 @@ class ApiFormatXml extends ApiFormatBase { } $elemName = str_replace( ' ', '_', $elemName ); - switch ( gettype( $elemValue ) ) { - case 'array': - if ( isset( $elemValue['*'] ) ) { - $subElemContent = $elemValue['*']; - if ( $doublequote ) { - $subElemContent = Sanitizer::encodeAttribute( $subElemContent ); - } - unset( $elemValue['*'] ); - - // Add xml:space="preserve" to the - // element so XML parsers will leave - // whitespace in the content alone - $elemValue['xml:space'] = 'preserve'; - } else { - $subElemContent = null; + if ( is_array( $elemValue ) ) { + if ( isset( $elemValue['*'] ) ) { + $subElemContent = $elemValue['*']; + if ( $doublequote ) { + $subElemContent = Sanitizer::encodeAttribute( $subElemContent ); } - - if ( isset( $elemValue['_element'] ) ) { - $subElemIndName = $elemValue['_element']; - unset( $elemValue['_element'] ); - } else { - $subElemIndName = null; - } - - $indElements = array(); - $subElements = array(); - foreach ( $elemValue as $subElemId => & $subElemValue ) { - if ( is_string( $subElemValue ) && $doublequote ) { - $subElemValue = Sanitizer::encodeAttribute( $subElemValue ); - } - - if ( gettype( $subElemId ) === 'integer' ) { - $indElements[] = $subElemValue; - unset( $elemValue[$subElemId] ); - } elseif ( is_array( $subElemValue ) ) { - $subElements[$subElemId] = $subElemValue; - unset ( $elemValue[$subElemId] ); - } + unset( $elemValue['*'] ); + + // Add xml:space="preserve" to the + // element so XML parsers will leave + // whitespace in the content alone + $elemValue['xml:space'] = 'preserve'; + } else { + $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 ( is_string( $subElemValue ) && $doublequote ) { + $subElemValue = Sanitizer::encodeAttribute( $subElemValue ); } - if ( is_null( $subElemIndName ) && count( $indElements ) ) { - ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." ); + if ( is_int( $subElemId ) ) { + $indElements[] = $subElemValue; + unset( $elemValue[$subElemId] ); + } elseif ( is_array( $subElemValue ) ) { + $subElements[$subElemId] = $subElemValue; + unset ( $elemValue[$subElemId] ); } + } - if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) { - ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" ); - } + if ( is_null( $subElemIndName ) && count( $indElements ) ) { + ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName()." ); + } - if ( !is_null( $subElemContent ) ) { - $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent ); - } elseif ( !count( $indElements ) && !count( $subElements ) ) { - $retval .= $indstr . Xml::element( $elemName, $elemValue ); - } else { - $retval .= $indstr . Xml::element( $elemName, $elemValue, null ); + if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) { + ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" ); + } - foreach ( $subElements as $subElemId => & $subElemValue ) { - $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent ); - } + if ( !is_null( $subElemContent ) ) { + $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent ); + } elseif ( !count( $indElements ) && !count( $subElements ) ) { + $retval .= $indstr . Xml::element( $elemName, $elemValue ); + } else { + $retval .= $indstr . Xml::element( $elemName, $elemValue, null ); - foreach ( $indElements as &$subElemValue ) { - $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent ); - } + foreach ( $subElements as $subElemId => & $subElemValue ) { + $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent ); + } - $retval .= $indstr . Xml::closeElement( $elemName ); + foreach ( $indElements as &$subElemValue ) { + $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent ); } - break; - case 'object': - // ignore - break; - default: + + $retval .= $indstr . Xml::closeElement( $elemName ); + } + } elseif ( !is_object( $elemValue ) ) { + // to make sure null value doesn't produce unclosed element, + // which is what Xml::element( $elemName, null, null ) returns + if ( $elemValue === null ) { + $retval .= $indstr . Xml::element( $elemName ); + } else { $retval .= $indstr . Xml::element( $elemName, null, $elemValue ); - break; + } } return $retval; } @@ -248,8 +244,4 @@ class ApiFormatXml extends ApiFormatBase { public function getDescription() { return 'Output data in XML format' . parent::getDescription(); } - - public function getVersion() { - return __CLASS__ . ': $Id$'; - } }