2006-10-01 04:38:31 +00:00
|
|
|
<?php
|
2009-03-21 16:48:09 +00:00
|
|
|
/**
|
2012-05-23 11:41:30 +00:00
|
|
|
* 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 sepecified in the URL.
|
2009-03-21 16:48:09 +00:00
|
|
|
*
|
2012-05-23 11:41:30 +00:00
|
|
|
* Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
|
2009-03-21 16:48:09 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2009-03-21 16:48:09 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2010-12-10 22:39:17 +00:00
|
|
|
// So extensions (and other code) can check whether they're running in API mode
|
|
|
|
|
define( 'MW_API', true );
|
|
|
|
|
|
2011-07-06 21:01:12 +00:00
|
|
|
// Bail if PHP is too low
|
2012-04-16 14:23:41 +00:00
|
|
|
if ( !function_exists( 'version_compare' ) || version_compare( phpversion(), '5.3.2' ) < 0 ) {
|
2011-07-06 21:01:12 +00:00
|
|
|
require( dirname( __FILE__ ) . '/includes/PHPVersionError.php' );
|
|
|
|
|
wfPHPVersionError( 'api.php' );
|
Improvements to handling of 'catastrophic' errors, like unsupported PHP versions, no MySQL functions, no LocalSettings, etc.
* Fix parsing of the three major entry points (index.php, api.php, load.php) back to PHP 4.4.9. We don't care what happens if you actually try to run these files on old versions, but the entry files need to parse correctly.
* consign /includes/templates/PHP4.php and /includes/templates/NoLocalSettings.php to the fiery pit of hell where they belong.
* Prevent loading of any other files for PHP < 5. WebStart.php was rendered unparseable in PHP 4 by the introduction of try/catch blocks in r85327.
* Die outright with a pretty error message on PHP < 5.2.3 as well as PHP 4. All versions of PHP below that throw parse errors of various sorts.
* Reimplement wfDie() to provide an entry-point-dependent die-with-readable-error-message function (for instance, we want a pretty human-readable page in index.php, something wrapped in CSS/JS /*...*/ comment block in load.php, etc).
* Standardise the appearance of the catastrophic errors thrown at the top of the stack with the ones lower down (exception-within-exception, etc). There isn't really a way to do this without duplication, AFAICT.
2011-04-12 20:38:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialise common code.
|
2011-05-30 13:49:09 +00:00
|
|
|
if ( isset( $_SERVER['MW_COMPILED'] ) ) {
|
2012-03-23 16:45:45 +00:00
|
|
|
require ( 'core/includes/WebStart.php' );
|
2011-05-30 13:49:09 +00:00
|
|
|
} else {
|
|
|
|
|
require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
|
|
|
|
|
}
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
wfProfileIn( 'api.php' );
|
2009-08-27 17:07:23 +00:00
|
|
|
$starttime = microtime( true );
|
2006-10-01 04:38:31 +00:00
|
|
|
|
2008-01-23 23:45:46 +00:00
|
|
|
// URL safety checks
|
2011-06-03 05:32:51 +00:00
|
|
|
if ( !$wgRequest->checkUrlExtension() ) {
|
2008-01-23 23:45:46 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-01 04:38:31 +00:00
|
|
|
// Verify that the API has not been disabled
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( !$wgEnableAPI ) {
|
2011-07-06 21:01:12 +00:00
|
|
|
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>' );
|
|
|
|
|
die(1);
|
2006-10-01 04:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-31 21:56:34 +00:00
|
|
|
// Selectively allow cross-site AJAX
|
2009-08-21 00:22:08 +00:00
|
|
|
|
2011-09-13 12:37:05 +00:00
|
|
|
/**
|
2009-08-21 00:22:08 +00:00
|
|
|
* Helper function to convert wildcard string into a regex
|
|
|
|
|
* '*' => '.*?'
|
|
|
|
|
* '?' => '.'
|
2011-09-13 12:37:05 +00:00
|
|
|
*
|
|
|
|
|
* @param $search string
|
|
|
|
|
* @return string
|
2009-08-21 00:22:08 +00:00
|
|
|
*/
|
|
|
|
|
function convertWildcard( $search ) {
|
|
|
|
|
$search = preg_quote( $search, '/' );
|
|
|
|
|
$search = str_replace(
|
|
|
|
|
array( '\*', '\?' ),
|
|
|
|
|
array( '.*?', '.' ),
|
|
|
|
|
$search
|
|
|
|
|
);
|
|
|
|
|
return "/$search/";
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
|
2009-08-21 00:22:08 +00:00
|
|
|
$exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
|
|
|
|
|
$regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
|
|
|
|
|
foreach ( $regexes as $regex ) {
|
|
|
|
|
if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
|
|
|
|
|
foreach ( $exceptions as $exc ) { // Check against exceptions
|
|
|
|
|
if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
|
|
|
|
|
break 2;
|
|
|
|
|
}
|
2009-07-31 21:56:34 +00:00
|
|
|
}
|
2009-08-21 00:22:08 +00:00
|
|
|
header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
|
|
|
|
|
header( 'Access-Control-Allow-Credentials: true' );
|
|
|
|
|
break;
|
2009-07-31 21:56:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-02 14:47:26 +00:00
|
|
|
// Set a dummy $wgTitle, because $wgTitle == null breaks various things
|
|
|
|
|
// In a perfect world this wouldn't be necessary
|
2010-02-15 20:50:21 +00:00
|
|
|
$wgTitle = Title::makeTitle( NS_MAIN, 'API' );
|
2009-05-02 14:47:26 +00:00
|
|
|
|
2007-05-22 04:39:49 +00:00
|
|
|
/* 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.
|
|
|
|
|
*/
|
2010-01-11 15:55:52 +00:00
|
|
|
$processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
|
2007-05-22 04:39:49 +00:00
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
// Process data & print results
|
2006-10-01 04:38:31 +00:00
|
|
|
$processor->execute();
|
|
|
|
|
|
2008-04-02 18:04:54 +00:00
|
|
|
// Execute any deferred updates
|
2011-10-14 21:18:38 +00:00
|
|
|
DeferredUpdates::doUpdates();
|
2008-04-02 18:04:54 +00:00
|
|
|
|
2007-05-22 04:39:49 +00:00
|
|
|
// Log what the user did, for book-keeping purposes.
|
2009-08-27 17:07:23 +00:00
|
|
|
$endtime = microtime( true );
|
2010-01-11 15:55:52 +00:00
|
|
|
wfProfileOut( 'api.php' );
|
2006-10-01 04:38:31 +00:00
|
|
|
wfLogProfilingData();
|
2007-06-29 01:19:14 +00:00
|
|
|
|
2009-08-27 17:07:23 +00:00
|
|
|
// Log the request
|
|
|
|
|
if ( $wgAPIRequestLog ) {
|
2009-08-27 22:09:28 +00:00
|
|
|
$items = array(
|
2009-08-27 17:07:23 +00:00
|
|
|
wfTimestamp( TS_MW ),
|
|
|
|
|
$endtime - $starttime,
|
2011-08-18 20:03:30 +00:00
|
|
|
$wgRequest->getIP(),
|
2009-08-27 22:09:28 +00:00
|
|
|
$_SERVER['HTTP_USER_AGENT']
|
|
|
|
|
);
|
|
|
|
|
$items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
|
Improvements to handling of 'catastrophic' errors, like unsupported PHP versions, no MySQL functions, no LocalSettings, etc.
* Fix parsing of the three major entry points (index.php, api.php, load.php) back to PHP 4.4.9. We don't care what happens if you actually try to run these files on old versions, but the entry files need to parse correctly.
* consign /includes/templates/PHP4.php and /includes/templates/NoLocalSettings.php to the fiery pit of hell where they belong.
* Prevent loading of any other files for PHP < 5. WebStart.php was rendered unparseable in PHP 4 by the introduction of try/catch blocks in r85327.
* Die outright with a pretty error message on PHP < 5.2.3 as well as PHP 4. All versions of PHP below that throw parse errors of various sorts.
* Reimplement wfDie() to provide an entry-point-dependent die-with-readable-error-message function (for instance, we want a pretty human-readable page in index.php, something wrapped in CSS/JS /*...*/ comment block in load.php, etc).
* Standardise the appearance of the catastrophic errors thrown at the top of the stack with the ones lower down (exception-within-exception, etc). There isn't really a way to do this without duplication, AFAICT.
2011-04-12 20:38:16 +00:00
|
|
|
$module = $processor->getModule();
|
|
|
|
|
if ( $module->mustBePosted() ) {
|
2009-08-27 22:09:28 +00:00
|
|
|
$items[] = "action=" . $wgRequest->getVal( 'action' );
|
|
|
|
|
} else {
|
|
|
|
|
$items[] = wfArrayToCGI( $wgRequest->getValues() );
|
|
|
|
|
}
|
|
|
|
|
wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
|
2009-08-27 17:07:23 +00:00
|
|
|
wfDebug( "Logged API request to $wgAPIRequestLog\n" );
|
|
|
|
|
}
|
|
|
|
|
|
Improvements to handling of 'catastrophic' errors, like unsupported PHP versions, no MySQL functions, no LocalSettings, etc.
* Fix parsing of the three major entry points (index.php, api.php, load.php) back to PHP 4.4.9. We don't care what happens if you actually try to run these files on old versions, but the entry files need to parse correctly.
* consign /includes/templates/PHP4.php and /includes/templates/NoLocalSettings.php to the fiery pit of hell where they belong.
* Prevent loading of any other files for PHP < 5. WebStart.php was rendered unparseable in PHP 4 by the introduction of try/catch blocks in r85327.
* Die outright with a pretty error message on PHP < 5.2.3 as well as PHP 4. All versions of PHP below that throw parse errors of various sorts.
* Reimplement wfDie() to provide an entry-point-dependent die-with-readable-error-message function (for instance, we want a pretty human-readable page in index.php, something wrapped in CSS/JS /*...*/ comment block in load.php, etc).
* Standardise the appearance of the catastrophic errors thrown at the top of the stack with the ones lower down (exception-within-exception, etc). There isn't really a way to do this without duplication, AFAICT.
2011-04-12 20:38:16 +00:00
|
|
|
// 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();
|
2008-05-17 04:26:26 +00:00
|
|
|
|