MediaWiki  REL1_23
ApiFormatWddx.php
Go to the documentation of this file.
00001 <?php
00031 class ApiFormatWddx extends ApiFormatBase {
00032 
00033     public function getMimeType() {
00034         return 'text/xml';
00035     }
00036 
00037     public function execute() {
00038         // Some versions of PHP have a broken wddx_serialize_value, see
00039         // PHP bug 45314. Test encoding an affected character (U+00A0)
00040         // to avoid this.
00041         $expected =
00042             "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
00043         if ( function_exists( 'wddx_serialize_value' )
00044             && !$this->getIsHtml()
00045             && wddx_serialize_value( "\xc2\xa0" ) == $expected
00046         ) {
00047             $this->printText( wddx_serialize_value( $this->getResultData() ) );
00048         } else {
00049             // Don't do newlines and indentation if we weren't asked
00050             // for pretty output
00051             $nl = ( $this->getIsHtml() ? "\n" : '' );
00052             $indstr = ' ';
00053             $this->printText( "<?xml version=\"1.0\"?>$nl" );
00054             $this->printText( "<wddxPacket version=\"1.0\">$nl" );
00055             $this->printText( "$indstr<header/>$nl" );
00056             $this->printText( "$indstr<data>$nl" );
00057             $this->slowWddxPrinter( $this->getResultData(), 4 );
00058             $this->printText( "$indstr</data>$nl" );
00059             $this->printText( "</wddxPacket>$nl" );
00060         }
00061     }
00062 
00068     function slowWddxPrinter( $elemValue, $indent = 0 ) {
00069         $indstr = ( $this->getIsHtml() ? str_repeat( ' ', $indent ) : '' );
00070         $indstr2 = ( $this->getIsHtml() ? str_repeat( ' ', $indent + 2 ) : '' );
00071         $nl = ( $this->getIsHtml() ? "\n" : '' );
00072         if ( is_array( $elemValue ) ) {
00073             // Check whether we've got an associative array (<struct>)
00074             // or a regular array (<array>)
00075             $cnt = count( $elemValue );
00076             if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) {
00077                 // Regular array
00078                 $this->printText( $indstr . Xml::element( 'array', array(
00079                     'length' => $cnt ), null ) . $nl );
00080                 foreach ( $elemValue as $subElemValue ) {
00081                     $this->slowWddxPrinter( $subElemValue, $indent + 2 );
00082                 }
00083                 $this->printText( "$indstr</array>$nl" );
00084             } else {
00085                 // Associative array (<struct>)
00086                 $this->printText( "$indstr<struct>$nl" );
00087                 foreach ( $elemValue as $subElemName => $subElemValue ) {
00088                     $this->printText( $indstr2 . Xml::element( 'var', array(
00089                         'name' => $subElemName
00090                     ), null ) . $nl );
00091                     $this->slowWddxPrinter( $subElemValue, $indent + 4 );
00092                     $this->printText( "$indstr2</var>$nl" );
00093                 }
00094                 $this->printText( "$indstr</struct>$nl" );
00095             }
00096         } elseif ( is_int( $elemValue ) || is_float( $elemValue ) ) {
00097             $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
00098         } elseif ( is_string( $elemValue ) ) {
00099             $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
00100         } elseif ( is_bool( $elemValue ) ) {
00101             $this->printText( $indstr . Xml::element( 'boolean',
00102                 array( 'value' => $elemValue ? 'true' : 'false' ) ) . $nl
00103             );
00104         } else {
00105             ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
00106         }
00107     }
00108 
00109     public function getDescription() {
00110         return 'Output data in WDDX format' . parent::getDescription();
00111     }
00112 }