3 * This file is the entry point for all API queries.
5 * It begins by checking whether the API is enabled on this wiki; if not,
6 * it informs the user that s/he should set $wgEnableAPI to true and exits.
7 * Otherwise, it constructs a new ApiMain using the parameter passed to it
8 * as an argument in the URL ('?action=') and with write-enabled set to the
9 * value of $wgEnableWriteAPI as specified in LocalSettings.php.
10 * It then invokes "execute()" on the ApiMain object instance, which
11 * produces output in the format specified in the URL.
13 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
33 use MediaWiki\Logger\LegacyLogger
;
35 // So extensions (and other code) can check whether they're running in API mode
36 define( 'MW_API', true );
38 require __DIR__
. '/includes/WebStart.php';
40 $starttime = microtime( true );
43 if ( !$wgRequest->checkUrlExtension() ) {
47 // Verify that the API has not been disabled
48 if ( !$wgEnableAPI ) {
49 header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
50 echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
51 . '<pre><b>$wgEnableAPI=true;</b></pre>';
55 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
56 // In a perfect world this wouldn't be necessary
57 $wgTitle = Title
::makeTitle( NS_SPECIAL
, 'Badtitle/dummy title for API calls set in api.php' );
59 // RequestContext will read from $wgTitle, but it will also whine about it.
60 // In a perfect world this wouldn't be necessary either.
61 RequestContext
::getMain()->setTitle( $wgTitle );
64 /* Construct an ApiMain with the arguments passed via the URL. What we get back
65 * is some form of an ApiMain, possibly even one that produces an error message,
66 * but we don't care here, as that is handled by the ctor.
68 $processor = new ApiMain( RequestContext
::getMain(), $wgEnableWriteAPI );
70 // Last chance hook before executing the API
71 Hooks
::run( 'ApiBeforeMain', array( &$processor ) );
72 if ( !$processor instanceof ApiMain
) {
73 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
75 } catch ( Exception
$e ) {
76 // Crap. Try to report the exception in API format to be friendly to clients.
77 ApiMain
::handleApiBeforeMainException( $e );
81 // Process data & print results
83 $processor->execute();
86 // Log what the user did, for book-keeping purposes.
87 $endtime = microtime( true );
90 if ( $wgAPIRequestLog ) {
93 $endtime - $starttime,
95 $wgRequest->getHeader( 'User-agent' )
97 $items[] = $wgRequest->wasPosted() ?
'POST' : 'GET';
100 $manager = $processor->getModuleManager();
101 $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
102 } catch ( Exception
$ex ) {
105 if ( !$module ||
$module->mustBePosted() ) {
106 $items[] = "action=" . $wgRequest->getVal( 'action' );
108 $items[] = wfArrayToCgi( $wgRequest->getValues() );
111 $items[] = "failed in ApiBeforeMain";
113 LegacyLogger
::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
114 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
117 $mediawiki = new MediaWiki();
118 $mediawiki->doPostOutputShutdown( 'fast' );