[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/ -> api.php (source)

   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  
  33  // So extensions (and other code) can check whether they're running in API mode
  34  define( 'MW_API', true );
  35  
  36  // Bail if PHP is too low
  37  if ( !function_exists( 'version_compare' ) || version_compare( PHP_VERSION, '5.3.2' ) < 0 ) {
  38      // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+
  39      require dirname( __FILE__ ) . '/includes/PHPVersionError.php';
  40      wfPHPVersionError( 'api.php' );
  41  }
  42  
  43  require  __DIR__ . '/includes/WebStart.php';
  44  
  45  wfProfileIn( 'api.php' );
  46  $starttime = microtime( true );
  47  
  48  // URL safety checks
  49  if ( !$wgRequest->checkUrlExtension() ) {
  50      return;
  51  }
  52  
  53  // Verify that the API has not been disabled
  54  if ( !$wgEnableAPI ) {
  55      header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
  56      echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
  57          . '<pre><b>$wgEnableAPI=true;</b></pre>';
  58      die( 1 );
  59  }
  60  
  61  // Set a dummy $wgTitle, because $wgTitle == null breaks various things
  62  // In a perfect world this wouldn't be necessary
  63  $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
  64  
  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 ctor.
  68   */
  69  $processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
  70  
  71  // Last chance hook before executing the API
  72  try {
  73      wfRunHooks( 'ApiBeforeMain', array( &$processor ) );
  74      if ( !$processor instanceof ApiMain ) {
  75          throw new MWException( 'ApiBeforMain hook set $processor to a non-ApiMain class' );
  76      }
  77  } catch ( Exception $e ) {
  78      // Crap. Try to report the exception in API format to be friendly to clients.
  79      ApiMain::handleApiBeforeMainException( $e );
  80      $processor = false;
  81  }
  82  
  83  // Process data & print results
  84  if ( $processor ) {
  85      $processor->execute();
  86  }
  87  
  88  if ( function_exists( 'fastcgi_finish_request' ) ) {
  89      fastcgi_finish_request();
  90  }
  91  
  92  // Execute any deferred updates
  93  DeferredUpdates::doUpdates();
  94  
  95  // Log what the user did, for book-keeping purposes.
  96  $endtime = microtime( true );
  97  wfProfileOut( 'api.php' );
  98  
  99  wfLogProfilingData();
 100  
 101  // Log the request
 102  if ( $wgAPIRequestLog ) {
 103      $items = array(
 104          wfTimestamp( TS_MW ),
 105          $endtime - $starttime,
 106          $wgRequest->getIP(),
 107          $wgRequest->getHeader( 'User-agent' )
 108      );
 109      $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
 110      if ( $processor ) {
 111          try {
 112              $manager = $processor->getModuleManager();
 113              $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
 114          } catch ( Exception $ex ) {
 115              $module = null;
 116          }
 117          if ( !$module || $module->mustBePosted() ) {
 118              $items[] = "action=" . $wgRequest->getVal( 'action' );
 119          } else {
 120              $items[] = wfArrayToCgi( $wgRequest->getValues() );
 121          }
 122      } else {
 123          $items[] = "failed in ApiBeforeMain";
 124      }
 125      wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
 126      wfDebug( "Logged API request to $wgAPIRequestLog\n" );
 127  }
 128  
 129  // Shut down the database.  foo()->bar() syntax is not supported in PHP4: we won't ever actually
 130  // get here to worry about whether this should be = or =&, but the file has to parse properly.
 131  $lb = wfGetLBFactory();
 132  $lb->shutdown();


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1