ApiRsd.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * API for MediaWiki 1.17+
  4. *
  5. * Copyright © 2010 Bryan Tong Minh and Brion Vibber
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. */
  24. /**
  25. * API module for sending out RSD information
  26. * @ingroup API
  27. */
  28. class ApiRsd extends ApiBase {
  29. public function execute() {
  30. $result = $this->getResult();
  31. $result->addValue( null, 'version', '1.0' );
  32. $result->addValue( null, 'xmlns', 'http://archipelago.phrasewise.com/rsd' );
  33. $service = [
  34. 'apis' => $this->formatRsdApiList(),
  35. 'engineName' => 'MediaWiki',
  36. 'engineLink' => 'https://www.mediawiki.org/',
  37. 'homePageLink' => Title::newMainPage()->getCanonicalURL(),
  38. ];
  39. ApiResult::setSubelementsList( $service, [ 'engineName', 'engineLink', 'homePageLink' ] );
  40. ApiResult::setIndexedTagName( $service['apis'], 'api' );
  41. $result->addValue( null, 'service', $service );
  42. }
  43. public function getCustomPrinter() {
  44. return new ApiFormatXmlRsd( $this->getMain(), 'xml' );
  45. }
  46. protected function getExamplesMessages() {
  47. return [
  48. 'action=rsd'
  49. => 'apihelp-rsd-example-simple',
  50. ];
  51. }
  52. public function isReadMode() {
  53. return false;
  54. }
  55. /**
  56. * Builds an internal list of APIs to expose information about.
  57. * Normally this only lists the MediaWiki API, with its base URL,
  58. * link to documentation, and a marker as to available authentication
  59. * (to aid in OAuth client apps switching to support in the future).
  60. *
  61. * Extensions can expose other APIs, such as WordPress or Twitter-
  62. * compatible APIs, by hooking 'ApiRsdServiceApis' and adding more
  63. * elements to the array.
  64. *
  65. * See https://cyber.harvard.edu/blogs/gems/tech/rsd.html for
  66. * the base RSD spec, and check WordPress and StatusNet sites for
  67. * in-production examples listing several blogging and micrblogging
  68. * APIs.
  69. *
  70. * @return array
  71. */
  72. protected function getRsdApiList() {
  73. $apis = [
  74. 'MediaWiki' => [
  75. // The API link is required for all RSD API entries.
  76. 'apiLink' => wfExpandUrl( wfScript( 'api' ), PROTO_CURRENT ),
  77. // Docs link is optional, but recommended.
  78. 'docs' => 'https://www.mediawiki.org/wiki/Special:MyLanguage/API',
  79. // Some APIs may need a blog ID, but it may be left blank.
  80. 'blogID' => '',
  81. // Additional settings are optional.
  82. 'settings' => [
  83. // Change this to true in the future as an aid to
  84. // machine discovery of OAuth for API access.
  85. 'OAuth' => false,
  86. ]
  87. ],
  88. ];
  89. Hooks::run( 'ApiRsdServiceApis', [ &$apis ] );
  90. return $apis;
  91. }
  92. /**
  93. * Formats the internal list of exposed APIs into an array suitable
  94. * to pass to the API's XML formatter.
  95. *
  96. * @return array
  97. */
  98. protected function formatRsdApiList() {
  99. $apis = $this->getRsdApiList();
  100. $outputData = [];
  101. foreach ( $apis as $name => $info ) {
  102. $data = [
  103. 'name' => $name,
  104. 'preferred' => wfBoolToStr( $name == 'MediaWiki' ),
  105. 'apiLink' => $info['apiLink'],
  106. 'blogID' => $info['blogID'] ?? '',
  107. ];
  108. $settings = [];
  109. if ( isset( $info['docs'] ) ) {
  110. $settings['docs'] = $info['docs'];
  111. ApiResult::setSubelementsList( $settings, 'docs' );
  112. }
  113. if ( isset( $info['settings'] ) ) {
  114. foreach ( $info['settings'] as $setting => $val ) {
  115. if ( is_bool( $val ) ) {
  116. $xmlVal = wfBoolToStr( $val );
  117. } else {
  118. $xmlVal = $val;
  119. }
  120. $setting = [ 'name' => $setting ];
  121. ApiResult::setContentValue( $setting, 'value', $xmlVal );
  122. $settings[] = $setting;
  123. }
  124. }
  125. if ( count( $settings ) ) {
  126. ApiResult::setIndexedTagName( $settings, 'setting' );
  127. $data['settings'] = $settings;
  128. }
  129. $outputData[] = $data;
  130. }
  131. return $outputData;
  132. }
  133. }