MediaWiki  REL1_19
api.php
Go to the documentation of this file.
00001 <?php
00002 
00037 // So extensions (and other code) can check whether they're running in API mode
00038 define( 'MW_API', true );
00039 
00040 // Bail if PHP is too low
00041 if ( !function_exists( 'version_compare' ) || version_compare( phpversion(), '5.2.3' ) < 0 ) {
00042         require( dirname( __FILE__ ) . '/includes/PHPVersionError.php' );
00043         wfPHPVersionError( 'api.php' );
00044 }
00045 
00046 // Initialise common code.
00047 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
00048         require ( 'phase3/includes/WebStart.php' );
00049 } else {
00050         require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
00051 }
00052 
00053 wfProfileIn( 'api.php' );
00054 $starttime = microtime( true );
00055 
00056 // URL safety checks
00057 if ( !$wgRequest->checkUrlExtension() ) {
00058         return;
00059 }
00060 
00061 // Verify that the API has not been disabled
00062 if ( !$wgEnableAPI ) {
00063         header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
00064         echo( 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
00065                 . '<pre><b>$wgEnableAPI=true;</b></pre>' );
00066         die(1);
00067 }
00068 
00069 // Selectively allow cross-site AJAX
00070 
00079 function convertWildcard( $search ) {
00080         $search = preg_quote( $search, '/' );
00081         $search = str_replace(
00082                 array( '\*', '\?' ),
00083                 array( '.*?', '.' ),
00084                 $search
00085         );
00086         return "/$search/";
00087 }
00088 
00089 if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
00090         $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
00091         $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
00092         foreach ( $regexes as $regex ) {
00093                 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
00094                         foreach ( $exceptions as $exc ) { // Check against exceptions
00095                                 if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
00096                                         break 2;
00097                                 }
00098                         }
00099                         header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
00100                         header( 'Access-Control-Allow-Credentials: true' );
00101                         break;
00102                 }
00103         }
00104 }
00105 
00106 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
00107 // In a perfect world this wouldn't be necessary
00108 $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
00109 
00110 /* Construct an ApiMain with the arguments passed via the URL. What we get back
00111  * is some form of an ApiMain, possibly even one that produces an error message,
00112  * but we don't care here, as that is handled by the ctor.
00113  */
00114 $processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
00115 
00116 // Process data & print results
00117 $processor->execute();
00118 
00119 // Execute any deferred updates
00120 DeferredUpdates::doUpdates();
00121 
00122 // Log what the user did, for book-keeping purposes.
00123 $endtime = microtime( true );
00124 wfProfileOut( 'api.php' );
00125 wfLogProfilingData();
00126 
00127 // Log the request
00128 if ( $wgAPIRequestLog ) {
00129         $items = array(
00130                         wfTimestamp( TS_MW ),
00131                         $endtime - $starttime,
00132                         $wgRequest->getIP(),
00133                         $_SERVER['HTTP_USER_AGENT']
00134         );
00135         $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
00136         $module = $processor->getModule();
00137         if ( $module->mustBePosted() ) {
00138                 $items[] = "action=" . $wgRequest->getVal( 'action' );
00139         } else {
00140                 $items[] = wfArrayToCGI( $wgRequest->getValues() );
00141         }
00142         wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
00143         wfDebug( "Logged API request to $wgAPIRequestLog\n" );
00144 }
00145 
00146 // Shut down the database.  foo()->bar() syntax is not supported in PHP4: we won't ever actually
00147 // get here to worry about whether this should be = or =&, but the file has to parse properly.
00148 $lb = wfGetLBFactory();
00149 $lb->shutdown();
00150