MediaWiki  REL1_22
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 = "<wddxPacket version='1.0'><header/><data><string>\xc2\xa0</string></data></wddxPacket>";
00042         if ( function_exists( 'wddx_serialize_value' )
00043                 && !$this->getIsHtml()
00044                 && wddx_serialize_value( "\xc2\xa0" ) == $expected ) {
00045             $this->printText( wddx_serialize_value( $this->getResultData() ) );
00046         } else {
00047             // Don't do newlines and indentation if we weren't asked
00048             // for pretty output
00049             $nl = ( $this->getIsHtml() ? "\n" : '' );
00050             $indstr = ' ';
00051             $this->printText( "<?xml version=\"1.0\"?>$nl" );
00052             $this->printText( "<wddxPacket version=\"1.0\">$nl" );
00053             $this->printText( "$indstr<header/>$nl" );
00054             $this->printText( "$indstr<data>$nl" );
00055             $this->slowWddxPrinter( $this->getResultData(), 4 );
00056             $this->printText( "$indstr</data>$nl" );
00057             $this->printText( "</wddxPacket>$nl" );
00058         }
00059     }
00060 
00066     function slowWddxPrinter( $elemValue, $indent = 0 ) {
00067         $indstr = ( $this->getIsHtml() ? str_repeat( ' ', $indent ) : '' );
00068         $indstr2 = ( $this->getIsHtml() ? str_repeat( ' ', $indent + 2 ) : '' );
00069         $nl = ( $this->getIsHtml() ? "\n" : '' );
00070         if ( is_array( $elemValue ) ) {
00071             // Check whether we've got an associative array (<struct>)
00072             // or a regular array (<array>)
00073             $cnt = count( $elemValue );
00074             if ( $cnt == 0 || array_keys( $elemValue ) === range( 0, $cnt - 1 ) ) {
00075                 // Regular array
00076                 $this->printText( $indstr . Xml::element( 'array', array(
00077                     'length' => $cnt ), null ) . $nl );
00078                 foreach ( $elemValue as $subElemValue ) {
00079                     $this->slowWddxPrinter( $subElemValue, $indent + 2 );
00080                 }
00081                 $this->printText( "$indstr</array>$nl" );
00082             } else {
00083                 // Associative array (<struct>)
00084                 $this->printText( "$indstr<struct>$nl" );
00085                 foreach ( $elemValue as $subElemName => $subElemValue ) {
00086                     $this->printText( $indstr2 . Xml::element( 'var', array(
00087                         'name' => $subElemName
00088                     ), null ) . $nl );
00089                     $this->slowWddxPrinter( $subElemValue, $indent + 4 );
00090                     $this->printText( "$indstr2</var>$nl" );
00091                 }
00092                 $this->printText( "$indstr</struct>$nl" );
00093             }
00094         } elseif ( is_int( $elemValue ) || is_float( $elemValue ) ) {
00095             $this->printText( $indstr . Xml::element( 'number', null, $elemValue ) . $nl );
00096         } elseif ( is_string( $elemValue ) ) {
00097             $this->printText( $indstr . Xml::element( 'string', null, $elemValue ) . $nl );
00098         } elseif ( is_bool( $elemValue ) ) {
00099             $this->printText( $indstr . Xml::element( 'boolean',
00100                 array( 'value' => $elemValue ? 'true' : 'false' ) ) . $nl
00101             );
00102         } else {
00103             ApiBase::dieDebug( __METHOD__, 'Unknown type ' . gettype( $elemValue ) );
00104         }
00105     }
00106 
00107     public function getDescription() {
00108         return 'Output data in WDDX format' . parent::getDescription();
00109     }
00110 }