api.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * This file is the entry point for all API queries.
  4. *
  5. * It begins by checking whether the API is enabled on this wiki; if not,
  6. * it informs the user that s/he should set $wgEnableAPI to true and exits.
  7. * Otherwise, it constructs a new ApiMain using the parameter passed to it
  8. * as an argument in the URL ('?action=') and with write-enabled set to the
  9. * value of $wgEnableWriteAPI as specified in LocalSettings.php.
  10. * It then invokes "execute()" on the ApiMain object instance, which
  11. * produces output in the format specified in the URL.
  12. *
  13. * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License along
  26. * with this program; if not, write to the Free Software Foundation, Inc.,
  27. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  28. * http://www.gnu.org/copyleft/gpl.html
  29. *
  30. * @file
  31. */
  32. use MediaWiki\Logger\LegacyLogger;
  33. // So extensions (and other code) can check whether they're running in API mode
  34. define( 'MW_API', true );
  35. require __DIR__ . '/includes/WebStart.php';
  36. $starttime = microtime( true );
  37. // URL safety checks
  38. if ( !$wgRequest->checkUrlExtension() ) {
  39. return;
  40. }
  41. // Pathinfo can be used for stupid things. We don't support it for api.php at
  42. // all, so error out if it's present.
  43. if ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
  44. $correctUrl = wfAppendQuery( wfScript( 'api' ), $wgRequest->getQueryValues() );
  45. $correctUrl = wfExpandUrl( $correctUrl, PROTO_CANONICAL );
  46. header( "Location: $correctUrl", true, 301 );
  47. echo 'This endpoint does not support "path info", i.e. extra text between "api.php"'
  48. . 'and the "?". Remove any such text and try again.';
  49. die( 1 );
  50. }
  51. // Verify that the API has not been disabled
  52. if ( !$wgEnableAPI ) {
  53. header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
  54. echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
  55. . '<pre><b>$wgEnableAPI=true;</b></pre>';
  56. die( 1 );
  57. }
  58. // Set a dummy $wgTitle, because $wgTitle == null breaks various things
  59. // In a perfect world this wouldn't be necessary
  60. $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for API calls set in api.php' );
  61. // RequestContext will read from $wgTitle, but it will also whine about it.
  62. // In a perfect world this wouldn't be necessary either.
  63. RequestContext::getMain()->setTitle( $wgTitle );
  64. try {
  65. /* Construct an ApiMain with the arguments passed via the URL. What we get back
  66. * is some form of an ApiMain, possibly even one that produces an error message,
  67. * but we don't care here, as that is handled by the constructor.
  68. */
  69. $processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
  70. // Last chance hook before executing the API
  71. Hooks::run( 'ApiBeforeMain', [ &$processor ] );
  72. if ( !$processor instanceof ApiMain ) {
  73. throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
  74. }
  75. } catch ( Exception $e ) {
  76. // Crap. Try to report the exception in API format to be friendly to clients.
  77. ApiMain::handleApiBeforeMainException( $e );
  78. $processor = false;
  79. }
  80. // Process data & print results
  81. if ( $processor ) {
  82. $processor->execute();
  83. }
  84. // Log what the user did, for book-keeping purposes.
  85. $endtime = microtime( true );
  86. // Log the request
  87. if ( $wgAPIRequestLog ) {
  88. $items = [
  89. wfTimestamp( TS_MW ),
  90. $endtime - $starttime,
  91. $wgRequest->getIP(),
  92. $wgRequest->getHeader( 'User-agent' )
  93. ];
  94. $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
  95. if ( $processor ) {
  96. try {
  97. $manager = $processor->getModuleManager();
  98. $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
  99. } catch ( Exception $ex ) {
  100. $module = null;
  101. }
  102. if ( !$module || $module->mustBePosted() ) {
  103. $items[] = "action=" . $wgRequest->getVal( 'action' );
  104. } else {
  105. $items[] = wfArrayToCgi( $wgRequest->getValues() );
  106. }
  107. } else {
  108. $items[] = "failed in ApiBeforeMain";
  109. }
  110. LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
  111. wfDebug( "Logged API request to $wgAPIRequestLog\n" );
  112. }
  113. $mediawiki = new MediaWiki();
  114. $mediawiki->doPostOutputShutdown( 'fast' );