5 * Created on Oct 22, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API WDDX output formatter
29 * @deprecated since 1.24
32 class ApiFormatWddx
extends ApiFormatBase
{
34 public function getMimeType() {
38 public function execute() {
39 $this->markDeprecated();
41 $data = $this->getResult()->getResultData( null, array(
43 'Types' => array( 'AssocAsObject' => true ),
47 if ( !$this->getIsHtml() && !static::useSlowPrinter() ) {
48 $txt = wddx_serialize_value( $data );
50 '<struct><var name=\'php_class_name\'><string>stdClass</string></var>',
54 $this->printText( $txt );
56 // Don't do newlines and indentation if we weren't asked
58 $nl = ( $this->getIsHtml() ?
"\n" : '' );
59 $indstr = ( $this->getIsHtml() ?
' ' : '' );
60 $this->printText( "<?xml version=\"1.0\"?>$nl" );
61 $this->printText( "<wddxPacket version=\"1.0\">$nl" );
62 $this->printText( "$indstr<header />$nl" );
63 $this->printText( "$indstr<data>$nl" );
64 $this->slowWddxPrinter( $data, 4 );
65 $this->printText( "$indstr</data>$nl" );
66 $this->printText( "</wddxPacket>$nl" );
70 public static function useSlowPrinter() {
71 if ( !function_exists( 'wddx_serialize_value' ) ) {
75 // Some versions of PHP have a broken wddx_serialize_value, see
76 // PHP bug 45314. Test encoding an affected character (U+00A0)
79 "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
80 if ( wddx_serialize_value( "\xc2\xa0" ) !== $expected ) {
84 // Some versions of HHVM don't correctly encode ampersands.
86 "<wddxPacket version='1.0'><header/><data><string>&</string></data></wddxPacket>";
87 if ( wddx_serialize_value( '&' ) !== $expected ) {
91 // Some versions of HHVM don't correctly encode empty arrays as subvalues.
93 "<wddxPacket version='1.0'><header/><data><array length='1'><array length='0'></array></array></data></wddxPacket>";
94 if ( wddx_serialize_value( array( array() ) ) !== $expected ) {
98 // Some versions of HHVM don't correctly encode associative arrays with numeric keys.
100 "<wddxPacket version='1.0'><header/><data><struct><var name='2'><number>1</number></var></struct></data></wddxPacket>";
101 if ( wddx_serialize_value( array( 2 => 1 ) ) !== $expected ) {
109 * Recursively go through the object and output its data in WDDX format.
110 * @param mixed $elemValue
113 function slowWddxPrinter( $elemValue, $indent = 0 ) {
114 $indstr = ( $this->getIsHtml() ?
str_repeat( ' ', $indent ) : '' );
115 $indstr2 = ( $this->getIsHtml() ?
str_repeat( ' ', $indent +
2 ) : '' );
116 $nl = ( $this->getIsHtml() ?
"\n" : '' );
118 if ( is_array( $elemValue ) ) {
119 $cnt = count( $elemValue );
120 if ( $cnt != 0 && array_keys( $elemValue ) !== range( 0, $cnt - 1 ) ) {
121 $elemValue = (object)$elemValue;
125 if ( is_array( $elemValue ) ) {
127 $this->printText( $indstr . Xml
::element( 'array', array(
128 'length' => count( $elemValue ) ), null ) . $nl );
129 foreach ( $elemValue as $subElemValue ) {
130 $this->slowWddxPrinter( $subElemValue, $indent +
2 );
132 $this->printText( "$indstr</array>$nl" );
133 } elseif ( is_object( $elemValue ) ) {
134 // Associative array (<struct>)
135 $this->printText( "$indstr<struct>$nl" );
136 foreach ( $elemValue as $subElemName => $subElemValue ) {
137 $this->printText( $indstr2 . Xml
::element( 'var', array(
138 'name' => $subElemName
140 $this->slowWddxPrinter( $subElemValue, $indent +
4 );
141 $this->printText( "$indstr2</var>$nl" );
143 $this->printText( "$indstr</struct>$nl" );
144 } elseif ( is_int( $elemValue ) ||
is_float( $elemValue ) ) {
145 $this->printText( $indstr . Xml
::element( 'number', null, $elemValue ) . $nl );
146 } elseif ( is_string( $elemValue ) ) {
147 $this->printText( $indstr . Xml
::element( 'string', null, $elemValue, false ) . $nl );
148 } elseif ( is_bool( $elemValue ) ) {
149 $this->printText( $indstr . Xml
::element( 'boolean',
150 array( 'value' => $elemValue ?
'true' : 'false' ) ) . $nl
152 } elseif ( $elemValue === null ) {
153 $this->printText( $indstr . Xml
::element( 'null', array() ) . $nl );
155 ApiBase
::dieDebug( __METHOD__
, 'Unknown type ' . gettype( $elemValue ) );
159 public function isDeprecated() {