api.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * API for MediaWiki 1.8+
  4. *
  5. * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  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. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. */
  24. /**
  25. * This file is the entry point for all API queries. It begins by checking
  26. * whether the API is enabled on this wiki; if not, it informs the user that
  27. * s/he should set $wgEnableAPI to true and exits. Otherwise, it constructs
  28. * a new ApiMain using the parameter passed to it as an argument in the URL
  29. * ('?action=') and with write-enabled set to the value of $wgEnableWriteAPI
  30. * as specified in LocalSettings.php. It then invokes "execute()" on the
  31. * ApiMain object instance, which produces output in the format sepecified
  32. * in the URL.
  33. */
  34. // Initialise common code
  35. require (dirname(__FILE__) . '/includes/WebStart.php');
  36. wfProfileIn('api.php');
  37. // URL safety checks
  38. //
  39. // See RawPage.php for details; summary is that MSIE can override the
  40. // Content-Type if it sees a recognized extension on the URL, such as
  41. // might be appended via PATH_INFO after 'api.php'.
  42. //
  43. // Some data formats can end up containing unfiltered user-provided data
  44. // which will end up triggering HTML detection and execution, hence
  45. // XSS injection and all that entails.
  46. //
  47. // Ensure that all access is through the canonical entry point...
  48. //
  49. if( isset( $_SERVER['SCRIPT_URL'] ) ) {
  50. $url = $_SERVER['SCRIPT_URL'];
  51. } else {
  52. $url = $_SERVER['PHP_SELF'];
  53. }
  54. if( strcmp( "$wgScriptPath/api$wgScriptExtension", $url ) ) {
  55. wfHttpError( 403, 'Forbidden',
  56. 'API must be accessed through the primary script entry point.' );
  57. return;
  58. }
  59. // Verify that the API has not been disabled
  60. if (!$wgEnableAPI) {
  61. echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php';
  62. echo '<pre><b>$wgEnableAPI=true;</b></pre>';
  63. die(1);
  64. }
  65. // So extensions can check whether they're running in API mode
  66. define('MW_API', true);
  67. // Set a dummy $wgTitle, because $wgTitle == null breaks various things
  68. // In a perfect world this wouldn't be necessary
  69. $wgTitle = Title::newFromText('API');
  70. /* Construct an ApiMain with the arguments passed via the URL. What we get back
  71. * is some form of an ApiMain, possibly even one that produces an error message,
  72. * but we don't care here, as that is handled by the ctor.
  73. */
  74. $processor = new ApiMain($wgRequest, $wgEnableWriteAPI);
  75. // Process data & print results
  76. $processor->execute();
  77. // Execute any deferred updates
  78. wfDoUpdates();
  79. // Log what the user did, for book-keeping purposes.
  80. wfProfileOut('api.php');
  81. wfLogProfilingData();
  82. // Shut down the database
  83. wfGetLBFactory()->shutdown();