Merge "Use batch inserts for watchlist"
[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 * @ingroup API
30 */
31 class ApiFormatWddx extends ApiFormatBase {
32
33 public function getMimeType() {
34 return 'text/xml';
35 }
36
37 public function execute() {
38 $this->markDeprecated();
39
40 // Some versions of PHP have a broken wddx_serialize_value, see
41 // PHP bug 45314. Test encoding an affected character (U+00A0)
42 // to avoid this.
43 $expected =
44 "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
45 if ( function_exists( 'wddx_serialize_value' )
46 && !$this->getIsHtml()
47 && wddx_serialize_value( "\xc2\xa0" ) == $expected
48 ) {
49 $this->printText( wddx_serialize_value( $this->getResultData() ) );
50 } else {
51 // Don't do newlines and indentation if we weren't asked
52 // for pretty output
53 $nl = ( $this->getIsHtml() ? "\n" : '' );
54 $indstr = ' ';
55 $this->printText( "<?xml version=\"1.0\"?>$nl" );
56 $this->printText( "<wddxPacket version=\"1.0\">$nl" );
57 $this->printText( "$indstr<header/>$nl" );
58 $this->printText( "$indstr<data>$nl" );
59 $this->slowWddxPrinter( $this->getResultData(), 4 );
60 $this->printText( "$indstr</data>$nl" );
61 $this->printText( "</wddxPacket>$nl" );
62 }
63 }
64
65 /**
66 * Recursively go through the object and output its data in WDDX format.
67 * @param mixed $elemValue
68 * @param int $indent
69 */
70 function slowWddxPrinter( $elemValue, $indent = 0 ) {
71 $indstr = ( $this->getIsHtml() ? str_repeat( ' ', $indent ) : '' );
72 $indstr2 = ( $this->getIsHtml() ? str_repeat( ' ', $indent + 2 ) : '' );
73 $nl = ( $this->getIsHtml() ? "\n" : '' );
74 if ( is_array( $elemValue ) ) {
75 // Check whether we've got an associative array (<struct>)
76 // or a regular array (<array>)
77 $cnt = count( $elemValue );
78 if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) {
79 // Regular array
80 $this->printText( $indstr . Xml::element( 'array', array(
81 'length' => $cnt ), null ) . $nl );
82 foreach ( $elemValue as $subElemValue ) {
83 $this->slowWddxPrinter( $subElemValue, $indent + 2 );
84 }
85 $this->printText( "$indstr</array>$nl" );
86 } else {
87 // Associative array (<struct>)
88 $this->printText( "$indstr<struct>$nl" );
89 foreach ( $elemValue as $subElemName => $subElemValue ) {
90 $this->printText( $indstr2 . Xml::element( 'var', array(
91 'name' => $subElemName
92 ), null ) . $nl );
93 $this->slowWddxPrinter( $subElemValue, $indent + 4 );
94 $this->printText( "$indstr2</var>$nl" );
95 }
96 $this->printText( "$indstr</struct>$nl" );
97 }
98 } elseif ( is_int( $elemValue ) || is_float( $elemValue ) ) {
99 $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
100 } elseif ( is_string( $elemValue ) ) {
101 $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
102 } elseif ( is_bool( $elemValue ) ) {
103 $this->printText( $indstr . Xml::element( 'boolean',
104 array( 'value' => $elemValue ? 'true' : 'false' ) ) . $nl
105 );
106 } else {
107 ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
108 }
109 }
110
111 public function getDescription() {
112 return 'DEPRECATED! Output data in WDDX format' . parent::getDescription();
113 }
114 }