]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiFormatWddx.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiFormatWddx.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
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 if ( !defined( 'MEDIAWIKI' ) ) {
28         // Eclipse helper - will be ignored in production
29         require_once( 'ApiFormatBase.php' );
30 }
31
32 /**
33  * API WDDX output formatter
34  * @ingroup API
35  */
36 class ApiFormatWddx extends ApiFormatBase {
37
38         public function __construct( $main, $format ) {
39                 parent::__construct( $main, $format );
40         }
41
42         public function getMimeType() {
43                 return 'text/xml';
44         }
45
46         public function execute() {
47                 // Some versions of PHP have a broken wddx_serialize_value, see
48                 // PHP bug 45314. Test encoding an affected character (U+00A0)
49                 // to avoid this.
50                 $expected = "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
51                 if ( function_exists( 'wddx_serialize_value' )
52                                 && !$this->getIsHtml()
53                                 && wddx_serialize_value( "\xc2\xa0" ) == $expected ) {
54                         $this->printText( wddx_serialize_value( $this->getResultData() ) );
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 = ' ';
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( $this->getResultData(), 4 );
65                         $this->printText( "$indstr</data>$nl" );
66                         $this->printText( "</wddxPacket>$nl" );
67                 }
68         }
69
70         /**
71          * Recursively go through the object and output its data in WDDX format.
72          */
73         function slowWddxPrinter( $elemValue, $indent = 0 ) {
74                 $indstr = ( $this->getIsHtml() ? '' : str_repeat( ' ', $indent ) );
75                 $indstr2 = ( $this->getIsHtml() ? '' : str_repeat( ' ', $indent + 2 ) );
76                 $nl = ( $this->getIsHtml() ? '' : "\n" );
77                 switch ( gettype( $elemValue ) ) {
78                         case 'array':
79                                 // Check whether we've got an associative array (<struct>)
80                                 // or a regular array (<array>)
81                                 $cnt = count( $elemValue );
82                                 if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) {
83                                         // Regular array
84                                         $this->printText( $indstr . Xml::element( 'array', array(
85                                                 'length' => $cnt ), null ) . $nl );
86                                         foreach ( $elemValue as $subElemValue ) {
87                                                 $this->slowWddxPrinter( $subElemValue, $indent + 2 );
88                                         }
89                                         $this->printText( "$indstr</array>$nl" );
90                                 } else {
91                                         // Associative array (<struct>)
92                                         $this->printText( "$indstr<struct>$nl" );
93                                         foreach ( $elemValue as $subElemName => $subElemValue ) {
94                                                 $this->printText( $indstr2 . Xml::element( 'var', array(
95                                                         'name' => $subElemName
96                                                 ), null ) . $nl );
97                                                 $this->slowWddxPrinter( $subElemValue, $indent + 4 );
98                                                 $this->printText( "$indstr2</var>$nl" );
99                                         }
100                                         $this->printText( "$indstr</struct>$nl" );
101                                 }
102                                 break;
103                         case 'integer':
104                         case 'double':
105                                 $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
106                                 break;
107                         case 'string':
108                                 $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
109                                 break;
110                         default:
111                                 ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
112                 }
113         }
114
115         public function getDescription() {
116                 return 'Output data in WDDX format' . parent::getDescription();
117         }
118
119         public function getVersion() {
120                 return __CLASS__ . ': $Id$';
121         }
122 }