MediaWiki  REL1_23
ApiFormatXml.php
Go to the documentation of this file.
00001 <?php
00031 class ApiFormatXml extends ApiFormatBase {
00032 
00033     private $mRootElemName = 'api';
00034     public static $namespace = 'http://www.mediawiki.org/xml/api/';
00035     private $mIncludeNamespace = false;
00036     private $mXslt = null;
00037 
00038     public function getMimeType() {
00039         return 'text/xml';
00040     }
00041 
00042     public function getNeedsRawData() {
00043         return true;
00044     }
00045 
00046     public function setRootElement( $rootElemName ) {
00047         $this->mRootElemName = $rootElemName;
00048     }
00049 
00050     public function execute() {
00051         $params = $this->extractRequestParams();
00052         $this->mIncludeNamespace = $params['includexmlnamespace'];
00053         $this->mXslt = $params['xslt'];
00054 
00055         $this->printText( '<?xml version="1.0"?>' );
00056         if ( !is_null( $this->mXslt ) ) {
00057             $this->addXslt();
00058         }
00059         if ( $this->mIncludeNamespace ) {
00060             // If the result data already contains an 'xmlns' namespace added
00061             // for custom XML output types, it will override the one for the
00062             // generic API results.
00063             // This allows API output of other XML types like Atom, RSS, RSD.
00064             $data = $this->getResultData() + array( 'xmlns' => self::$namespace );
00065         } else {
00066             $data = $this->getResultData();
00067         }
00068 
00069         $this->printText(
00070             self::recXmlPrint( $this->mRootElemName,
00071                 $data,
00072                 $this->getIsHtml() ? -2 : null
00073             )
00074         );
00075     }
00076 
00120     public static function recXmlPrint( $elemName, $elemValue, $indent ) {
00121         $retval = '';
00122         if ( !is_null( $indent ) ) {
00123             $indent += 2;
00124             $indstr = "\n" . str_repeat( ' ', $indent );
00125         } else {
00126             $indstr = '';
00127         }
00128         $elemName = str_replace( ' ', '_', $elemName );
00129 
00130         if ( is_array( $elemValue ) ) {
00131             if ( isset( $elemValue['*'] ) ) {
00132                 $subElemContent = $elemValue['*'];
00133                 unset( $elemValue['*'] );
00134 
00135                 // Add xml:space="preserve" to the
00136                 // element so XML parsers will leave
00137                 // whitespace in the content alone
00138                 $elemValue['xml:space'] = 'preserve';
00139             } else {
00140                 $subElemContent = null;
00141             }
00142 
00143             if ( isset( $elemValue['_element'] ) ) {
00144                 $subElemIndName = $elemValue['_element'];
00145                 unset( $elemValue['_element'] );
00146             } else {
00147                 $subElemIndName = null;
00148             }
00149 
00150             $indElements = array();
00151             $subElements = array();
00152             foreach ( $elemValue as $subElemId => & $subElemValue ) {
00153                 if ( is_int( $subElemId ) ) {
00154                     $indElements[] = $subElemValue;
00155                     unset( $elemValue[$subElemId] );
00156                 } elseif ( is_array( $subElemValue ) ) {
00157                     $subElements[$subElemId] = $subElemValue;
00158                     unset( $elemValue[$subElemId] );
00159                 } elseif ( is_bool( $subElemValue ) ) {
00160                     // treat true as empty string, skip false in xml format
00161                     if ( $subElemValue === true ) {
00162                         $subElemValue = '';
00163                     } else {
00164                         unset( $elemValue[$subElemId] );
00165                     }
00166                 }
00167             }
00168 
00169             if ( is_null( $subElemIndName ) && count( $indElements ) ) {
00170                 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has integer keys " .
00171                     "without _element value. Use ApiResult::setIndexedTagName()." );
00172             }
00173 
00174             if ( count( $subElements ) && count( $indElements ) && !is_null( $subElemContent ) ) {
00175                 ApiBase::dieDebug( __METHOD__, "($elemName, ...) has content and subelements" );
00176             }
00177 
00178             if ( !is_null( $subElemContent ) ) {
00179                 $retval .= $indstr . Xml::element( $elemName, $elemValue, $subElemContent );
00180             } elseif ( !count( $indElements ) && !count( $subElements ) ) {
00181                 $retval .= $indstr . Xml::element( $elemName, $elemValue );
00182             } else {
00183                 $retval .= $indstr . Xml::element( $elemName, $elemValue, null );
00184 
00185                 foreach ( $subElements as $subElemId => & $subElemValue ) {
00186                     $retval .= self::recXmlPrint( $subElemId, $subElemValue, $indent );
00187                 }
00188 
00189                 foreach ( $indElements as &$subElemValue ) {
00190                     $retval .= self::recXmlPrint( $subElemIndName, $subElemValue, $indent );
00191                 }
00192 
00193                 $retval .= $indstr . Xml::closeElement( $elemName );
00194             }
00195         } elseif ( !is_object( $elemValue ) ) {
00196             // to make sure null value doesn't produce unclosed element,
00197             // which is what Xml::element( $elemName, null, null ) returns
00198             if ( $elemValue === null ) {
00199                 $retval .= $indstr . Xml::element( $elemName );
00200             } else {
00201                 $retval .= $indstr . Xml::element( $elemName, null, $elemValue );
00202             }
00203         }
00204 
00205         return $retval;
00206     }
00207 
00208     function addXslt() {
00209         $nt = Title::newFromText( $this->mXslt );
00210         if ( is_null( $nt ) || !$nt->exists() ) {
00211             $this->setWarning( 'Invalid or non-existent stylesheet specified' );
00212 
00213             return;
00214         }
00215         if ( $nt->getNamespace() != NS_MEDIAWIKI ) {
00216             $this->setWarning( 'Stylesheet should be in the MediaWiki namespace.' );
00217 
00218             return;
00219         }
00220         if ( substr( $nt->getText(), -4 ) !== '.xsl' ) {
00221             $this->setWarning( 'Stylesheet should have .xsl extension.' );
00222 
00223             return;
00224         }
00225         $this->printText( '<?xml-stylesheet href="' .
00226             htmlspecialchars( $nt->getLocalURL( 'action=raw' ) ) . '" type="text/xsl" ?>' );
00227     }
00228 
00229     public function getAllowedParams() {
00230         return array(
00231             'xslt' => null,
00232             'includexmlnamespace' => false,
00233         );
00234     }
00235 
00236     public function getParamDescription() {
00237         return array(
00238             'xslt' => 'If specified, adds <xslt> as stylesheet. This should be a wiki page '
00239                 . 'in the MediaWiki namespace whose page name ends with ".xsl"',
00240             'includexmlnamespace' => 'If specified, adds an XML namespace'
00241         );
00242     }
00243 
00244     public function getDescription() {
00245         return 'Output data in XML format' . parent::getDescription();
00246     }
00247 }