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
|
2013-03-11 03:16:28 +00:00
|
|
|
* produces output in the format specified 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
|
|
|
|
2015-03-23 00:53:24 +00:00
|
|
|
use MediaWiki\Logger\LegacyLogger;
|
|
|
|
|
|
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 );
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require __DIR__ . '/includes/WebStart.php';
|
2006-10-01 04:38:31 +00:00
|
|
|
|
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 );
|
2013-05-09 16:58:21 +00:00
|
|
|
echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
|
|
|
|
|
. '<pre><b>$wgEnableAPI=true;</b></pre>';
|
2013-02-13 18:38:32 +00:00
|
|
|
die( 1 );
|
2006-10-01 04:38:31 +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
|
2015-04-03 08:33:38 +00:00
|
|
|
$wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for API calls set in api.php' );
|
2009-05-02 14:47:26 +00:00
|
|
|
|
2015-03-14 01:11:13 +00:00
|
|
|
// RequestContext will read from $wgTitle, but it will also whine about it.
|
|
|
|
|
// In a perfect world this wouldn't be necessary either.
|
|
|
|
|
RequestContext::getMain()->setTitle( $wgTitle );
|
|
|
|
|
|
2014-03-06 14:37:26 +00:00
|
|
|
try {
|
2014-10-22 21:26:40 +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,
|
2017-03-01 04:50:31 +00:00
|
|
|
* but we don't care here, as that is handled by the constructor.
|
2014-10-22 21:26:40 +00:00
|
|
|
*/
|
|
|
|
|
$processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
|
|
|
|
|
|
|
|
|
|
// Last chance hook before executing the API
|
2016-02-17 09:09:32 +00:00
|
|
|
Hooks::run( 'ApiBeforeMain', [ &$processor ] );
|
2014-03-06 14:37:26 +00:00
|
|
|
if ( !$processor instanceof ApiMain ) {
|
2014-12-12 08:41:27 +00:00
|
|
|
throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
|
2014-03-06 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
|
// Crap. Try to report the exception in API format to be friendly to clients.
|
|
|
|
|
ApiMain::handleApiBeforeMainException( $e );
|
|
|
|
|
$processor = false;
|
|
|
|
|
}
|
2013-12-12 15:01:33 +00:00
|
|
|
|
2007-07-30 08:09:15 +00:00
|
|
|
// Process data & print results
|
2014-03-06 14:37:26 +00:00
|
|
|
if ( $processor ) {
|
|
|
|
|
$processor->execute();
|
|
|
|
|
}
|
2006-10-01 04:38:31 +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 );
|
2013-11-21 23:05:00 +00:00
|
|
|
|
2009-08-27 17:07:23 +00:00
|
|
|
// Log the request
|
|
|
|
|
if ( $wgAPIRequestLog ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$items = [
|
2013-02-13 18:38:32 +00:00
|
|
|
wfTimestamp( TS_MW ),
|
|
|
|
|
$endtime - $starttime,
|
|
|
|
|
$wgRequest->getIP(),
|
2014-07-20 13:47:21 +00:00
|
|
|
$wgRequest->getHeader( 'User-agent' )
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2009-08-27 22:09:28 +00:00
|
|
|
$items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
|
2014-03-06 14:37:26 +00:00
|
|
|
if ( $processor ) {
|
2014-07-22 13:18:15 +00:00
|
|
|
try {
|
|
|
|
|
$manager = $processor->getModuleManager();
|
|
|
|
|
$module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
|
|
|
|
|
} catch ( Exception $ex ) {
|
|
|
|
|
$module = null;
|
|
|
|
|
}
|
|
|
|
|
if ( !$module || $module->mustBePosted() ) {
|
2014-03-06 14:37:26 +00:00
|
|
|
$items[] = "action=" . $wgRequest->getVal( 'action' );
|
|
|
|
|
} else {
|
|
|
|
|
$items[] = wfArrayToCgi( $wgRequest->getValues() );
|
|
|
|
|
}
|
2009-08-27 22:09:28 +00:00
|
|
|
} else {
|
2014-03-06 14:37:26 +00:00
|
|
|
$items[] = "failed in ApiBeforeMain";
|
2009-08-27 22:09:28 +00:00
|
|
|
}
|
2015-03-23 00:53:24 +00:00
|
|
|
LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
|
2009-08-27 17:07:23 +00:00
|
|
|
wfDebug( "Logged API request to $wgAPIRequestLog\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-20 23:32:20 +00:00
|
|
|
$mediawiki = new MediaWiki();
|
|
|
|
|
$mediawiki->doPostOutputShutdown( 'fast' );
|