Move up devunt's name to Developers
[lhc/web/wiklou.git] / includes / api / ApiFormatWddx.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 22, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * API WDDX output formatter
29 * @deprecated since 1.24
30 * @ingroup API
31 */
32 class ApiFormatWddx extends ApiFormatBase {
33
34 public function getMimeType() {
35 return 'text/xml';
36 }
37
38 public function execute() {
39 $this->markDeprecated();
40
41 $data = $this->getResult()->getResultData( null, array(
42 'BC' => array(),
43 'Types' => array( 'AssocAsObject' => true ),
44 'Strip' => 'all',
45 ) );
46
47 if ( !$this->getIsHtml() && !static::useSlowPrinter() ) {
48 $txt = wddx_serialize_value( $data );
49 $txt = str_replace(
50 '<struct><var name=\'php_class_name\'><string>stdClass</string></var>',
51 '<struct>',
52 $txt
53 );
54 $this->printText( $txt );
55 } else {
56 // Don't do newlines and indentation if we weren't asked
57 // for pretty output
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" );
67 }
68 }
69
70 public static function useSlowPrinter() {
71 if ( !function_exists( 'wddx_serialize_value' ) ) {
72 return true;
73 }
74
75 // Some versions of PHP have a broken wddx_serialize_value, see
76 // PHP bug 45314. Test encoding an affected character (U+00A0)
77 // to avoid this.
78 $expected =
79 "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
80 if ( wddx_serialize_value( "\xc2\xa0" ) !== $expected ) {
81 return true;
82 }
83
84 // Some versions of HHVM don't correctly encode ampersands.
85 $expected =
86 "<wddxPacket version='1.0'><header/><data><string>&amp;</string></data></wddxPacket>";
87 if ( wddx_serialize_value( '&' ) !== $expected ) {
88 return true;
89 }
90
91 // Some versions of HHVM don't correctly encode empty arrays as subvalues.
92 $expected =
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 ) {
95 return true;
96 }
97
98 // Some versions of HHVM don't correctly encode associative arrays with numeric keys.
99 $expected =
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 ) {
102 return true;
103 }
104
105 return false;
106 }
107
108 /**
109 * Recursively go through the object and output its data in WDDX format.
110 * @param mixed $elemValue
111 * @param int $indent
112 */
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" : '' );
117
118 if ( is_array( $elemValue ) ) {
119 $cnt = count( $elemValue );
120 if ( $cnt != 0 && array_keys( $elemValue ) !== range( 0, $cnt - 1 ) ) {
121 $elemValue = (object)$elemValue;
122 }
123 }
124
125 if ( is_array( $elemValue ) ) {
126 // Regular array
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 );
131 }
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
139 ), null ) . $nl );
140 $this->slowWddxPrinter( $subElemValue, $indent + 4 );
141 $this->printText( "$indstr2</var>$nl" );
142 }
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
151 );
152 } elseif ( $elemValue === null ) {
153 $this->printText( $indstr . Xml::element( 'null', array() ) . $nl );
154 } else {
155 ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
156 }
157 }
158
159 public function isDeprecated() {
160 return true;
161 }
162 }