MediaWiki  REL1_21
ApiRsd.php
Go to the documentation of this file.
00001 <?php
00002 
00032 class ApiRsd extends ApiBase {
00033 
00034         public function execute() {
00035                 $result = $this->getResult();
00036 
00037                 $result->addValue( null, 'version', '1.0' );
00038                 $result->addValue( null, 'xmlns', 'http://archipelago.phrasewise.com/rsd' );
00039 
00040                 $service = array( 'apis' => $this->formatRsdApiList() );
00041                 ApiResult::setContent( $service, 'MediaWiki', 'engineName' );
00042                 ApiResult::setContent( $service, 'https://www.mediawiki.org/', 'engineLink' );
00043                 ApiResult::setContent( $service, Title::newMainPage()->getCanonicalUrl(), 'homePageLink' );
00044 
00045                 $result->setIndexedTagName( $service['apis'], 'api' );
00046 
00047                 $result->addValue( null, 'service', $service );
00048         }
00049 
00050         public function getCustomPrinter() {
00051                 return new ApiFormatXmlRsd( $this->getMain(), 'xml' );
00052         }
00053 
00054         public function getAllowedParams() {
00055                 return array();
00056         }
00057 
00058         public function getParamDescription() {
00059                 return array();
00060         }
00061 
00062         public function getDescription() {
00063                 return 'Export an RSD (Really Simple Discovery) schema';
00064         }
00065 
00066         public function getExamples() {
00067                 return array(
00068                         'api.php?action=rsd'
00069                 );
00070         }
00071 
00089         protected function getRsdApiList() {
00090                 $apis = array(
00091                         'MediaWiki' => array(
00092                                 // The API link is required for all RSD API entries.
00093                                 'apiLink' => wfExpandUrl( wfScript( 'api' ), PROTO_CURRENT ),
00094 
00095                                 // Docs link is optional, but recommended.
00096                                 'docs' => 'https://www.mediawiki.org/wiki/API',
00097 
00098                                 // Some APIs may need a blog ID, but it may be left blank.
00099                                 'blogID' => '',
00100 
00101                                 // Additional settings are optional.
00102                                 'settings' => array(
00103                                         // Change this to true in the future as an aid to
00104                                         // machine discovery of OAuth for API access.
00105                                         'OAuth' => false,
00106                                 )
00107                         ),
00108                 );
00109                 wfRunHooks( 'ApiRsdServiceApis', array( &$apis ) );
00110                 return $apis;
00111         }
00112 
00119         protected function formatRsdApiList() {
00120                 $apis = $this->getRsdApiList();
00121 
00122                 $outputData = array();
00123                 foreach ( $apis as $name => $info ) {
00124                         $data = array(
00125                                 'name' => $name,
00126                                 'preferred' => wfBoolToStr( $name == 'MediaWiki' ),
00127                                 'apiLink' => $info['apiLink'],
00128                                 'blogID' => isset( $info['blogID'] ) ? $info['blogID'] : '',
00129                         );
00130                         $settings = array();
00131                         if ( isset( $info['docs'] ) ) {
00132                                 ApiResult::setContent( $settings, $info['docs'], 'docs' );
00133                         }
00134                         if ( isset( $info['settings'] ) ) {
00135                                 foreach ( $info['settings'] as $setting => $val ) {
00136                                         if ( is_bool( $val ) ) {
00137                                                 $xmlVal = wfBoolToStr( $val );
00138                                         } else {
00139                                                 $xmlVal = $val;
00140                                         }
00141                                         $setting = array( 'name' => $setting );
00142                                         ApiResult::setContent( $setting, $xmlVal );
00143                                         $settings[] = $setting;
00144                                 }
00145                         }
00146                         if ( count( $settings ) ) {
00147                                 $this->getResult()->setIndexedTagName( $settings, 'setting' );
00148                                 $data['settings'] = $settings;
00149                         }
00150                         $outputData[] = $data;
00151                 }
00152                 return $outputData;
00153         }
00154 }
00155 
00156 class ApiFormatXmlRsd extends ApiFormatXml {
00157         public function __construct( $main, $format ) {
00158                 parent::__construct( $main, $format );
00159                 $this->setRootElement( 'rsd' );
00160         }
00161 
00162         public function getMimeType() {
00163                 return 'application/rsd+xml';
00164         }
00165 }