MediaWiki  REL1_24
api.php
Go to the documentation of this file.
00001 <?php
00033 // So extensions (and other code) can check whether they're running in API mode
00034 define( 'MW_API', true );
00035 
00036 // Bail if PHP is too low
00037 if ( !function_exists( 'version_compare' ) || version_compare( PHP_VERSION, '5.3.2' ) < 0 ) {
00038     // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+
00039     require dirname( __FILE__ ) . '/includes/PHPVersionError.php';
00040     wfPHPVersionError( 'api.php' );
00041 }
00042 
00043 require __DIR__ . '/includes/WebStart.php';
00044 
00045 wfProfileIn( 'api.php' );
00046 $starttime = microtime( true );
00047 
00048 // URL safety checks
00049 if ( !$wgRequest->checkUrlExtension() ) {
00050     return;
00051 }
00052 
00053 // Verify that the API has not been disabled
00054 if ( !$wgEnableAPI ) {
00055     header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
00056     echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
00057         . '<pre><b>$wgEnableAPI=true;</b></pre>';
00058     die( 1 );
00059 }
00060 
00061 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
00062 // In a perfect world this wouldn't be necessary
00063 $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
00064 
00065 /* Construct an ApiMain with the arguments passed via the URL. What we get back
00066  * is some form of an ApiMain, possibly even one that produces an error message,
00067  * but we don't care here, as that is handled by the ctor.
00068  */
00069 $processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
00070 
00071 // Last chance hook before executing the API
00072 try {
00073     wfRunHooks( 'ApiBeforeMain', array( &$processor ) );
00074     if ( !$processor instanceof ApiMain ) {
00075         throw new MWException( 'ApiBeforMain hook set $processor to a non-ApiMain class' );
00076     }
00077 } catch ( Exception $e ) {
00078     // Crap. Try to report the exception in API format to be friendly to clients.
00079     ApiMain::handleApiBeforeMainException( $e );
00080     $processor = false;
00081 }
00082 
00083 // Process data & print results
00084 if ( $processor ) {
00085     $processor->execute();
00086 }
00087 
00088 if ( function_exists( 'fastcgi_finish_request' ) ) {
00089     fastcgi_finish_request();
00090 }
00091 
00092 // Execute any deferred updates
00093 DeferredUpdates::doUpdates();
00094 
00095 // Log what the user did, for book-keeping purposes.
00096 $endtime = microtime( true );
00097 wfProfileOut( 'api.php' );
00098 
00099 wfLogProfilingData();
00100 
00101 // Log the request
00102 if ( $wgAPIRequestLog ) {
00103     $items = array(
00104         wfTimestamp( TS_MW ),
00105         $endtime - $starttime,
00106         $wgRequest->getIP(),
00107         $wgRequest->getHeader( 'User-agent' )
00108     );
00109     $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
00110     if ( $processor ) {
00111         try {
00112             $manager = $processor->getModuleManager();
00113             $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
00114         } catch ( Exception $ex ) {
00115             $module = null;
00116         }
00117         if ( !$module || $module->mustBePosted() ) {
00118             $items[] = "action=" . $wgRequest->getVal( 'action' );
00119         } else {
00120             $items[] = wfArrayToCgi( $wgRequest->getValues() );
00121         }
00122     } else {
00123         $items[] = "failed in ApiBeforeMain";
00124     }
00125     wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
00126     wfDebug( "Logged API request to $wgAPIRequestLog\n" );
00127 }
00128 
00129 // Shut down the database.  foo()->bar() syntax is not supported in PHP4: we won't ever actually
00130 // get here to worry about whether this should be = or =&, but the file has to parse properly.
00131 $lb = wfGetLBFactory();
00132 $lb->shutdown();