/**
* This file is the entry point for all API queries.
*
- * It begins by checking whether the API is enabled on this wiki; if not,
- * it informs the user that s/he should set $wgEnableAPI to true and exits.
- * Otherwise, it constructs a new ApiMain using the parameter passed to it
- * as an argument in the URL ('?action=') and with write-enabled set to the
- * value of $wgEnableWriteAPI as specified in LocalSettings.php.
- * It then invokes "execute()" on the ApiMain object instance, which
- * produces output in the format specified in the URL.
+ * It begins by constructing a new ApiMain using the parameter passed to it
+ * as an argument in the URL ('?action='). It then invokes "execute()" on the
+ * ApiMain object instance, which produces output in the format specified in
+ * the URL.
*
* Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
*
// So extensions (and other code) can check whether they're running in API mode
define( 'MW_API', true );
-// Bail if PHP is too low
-if ( !function_exists( 'version_compare' ) || version_compare( PHP_VERSION, '5.3.3' ) < 0 ) {
- // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+
- require dirname( __FILE__ ) . '/includes/PHPVersionError.php';
- wfPHPVersionError( 'api.php' );
-}
-
require __DIR__ . '/includes/WebStart.php';
$starttime = microtime( true );
return;
}
-// Verify that the API has not been disabled
-if ( !$wgEnableAPI ) {
- header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
- echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
- . '<pre><b>$wgEnableAPI=true;</b></pre>';
+// PATH_INFO can be used for stupid things. We don't support it for api.php at
+// all, so error out if it's present.
+if ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
+ $correctUrl = wfAppendQuery( wfScript( 'api' ), $wgRequest->getQueryValues() );
+ $correctUrl = wfExpandUrl( $correctUrl, PROTO_CANONICAL );
+ header( "Location: $correctUrl", true, 301 );
+ echo 'This endpoint does not support "path info", i.e. extra text between "api.php"'
+ . 'and the "?". Remove any such text and try again.';
die( 1 );
}
try {
/* Construct an ApiMain with the arguments passed via the URL. What we get back
* is some form of an ApiMain, possibly even one that produces an error message,
- * but we don't care here, as that is handled by the ctor.
+ * but we don't care here, as that is handled by the constructor.
*/
- $processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
+ $processor = new ApiMain( RequestContext::getMain(), true );
// Last chance hook before executing the API
- Hooks::run( 'ApiBeforeMain', array( &$processor ) );
+ Hooks::run( 'ApiBeforeMain', [ &$processor ] );
if ( !$processor instanceof ApiMain ) {
throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
}
-} catch ( Exception $e ) {
+} catch ( Exception $e ) { // @todo Remove this block when HHVM is no longer supported
+ // Crap. Try to report the exception in API format to be friendly to clients.
+ ApiMain::handleApiBeforeMainException( $e );
+ $processor = false;
+} catch ( Throwable $e ) {
// Crap. Try to report the exception in API format to be friendly to clients.
ApiMain::handleApiBeforeMainException( $e );
$processor = false;
$processor->execute();
}
-if ( function_exists( 'fastcgi_finish_request' ) ) {
- fastcgi_finish_request();
-}
-
-// Execute any deferred updates
-DeferredUpdates::doUpdates();
-
// Log what the user did, for book-keeping purposes.
$endtime = microtime( true );
-wfLogProfilingData();
-
// Log the request
if ( $wgAPIRequestLog ) {
- $items = array(
+ $items = [
wfTimestamp( TS_MW ),
$endtime - $starttime,
$wgRequest->getIP(),
$wgRequest->getHeader( 'User-agent' )
- );
+ ];
$items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
if ( $processor ) {
try {
$manager = $processor->getModuleManager();
$module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
- } catch ( Exception $ex ) {
+ } catch ( Exception $ex ) { // @todo Remove this block when HHVM is no longer supported
+ $module = null;
+ } catch ( Throwable $ex ) {
$module = null;
}
if ( !$module || $module->mustBePosted() ) {
wfDebug( "Logged API request to $wgAPIRequestLog\n" );
}
-// Shut down the database. foo()->bar() syntax is not supported in PHP4: we won't ever actually
-// get here to worry about whether this should be = or =&, but the file has to parse properly.
-$lb = wfGetLBFactory();
-$lb->shutdown();
+$mediawiki = new MediaWiki();
+$mediawiki->doPostOutputShutdown( 'fast' );