3 * This file is the entry point for all API queries.
5 * It begins by constructing a new ApiMain using the parameter passed to it
6 * as an argument in the URL ('?action='). It then invokes "execute()" on the
7 * ApiMain object instance, which produces output in the format specified in
10 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
30 use MediaWiki\Logger\LegacyLogger
;
32 // So extensions (and other code) can check whether they're running in API mode
33 define( 'MW_API', true );
35 require __DIR__
. '/includes/WebStart.php';
37 $starttime = microtime( true );
40 if ( !$wgRequest->checkUrlExtension() ) {
44 // PATH_INFO can be used for stupid things. We don't support it for api.php at
45 // all, so error out if it's present.
46 if ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
47 $correctUrl = wfAppendQuery( wfScript( 'api' ), $wgRequest->getQueryValues() );
48 $correctUrl = wfExpandUrl( $correctUrl, PROTO_CANONICAL
);
49 header( "Location: $correctUrl", true, 301 );
50 echo 'This endpoint does not support "path info", i.e. extra text between "api.php"'
51 . 'and the "?". Remove any such text and try again.';
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 constructor.
68 $processor = new ApiMain( RequestContext
::getMain(), true );
70 // Last chance hook before executing the API
71 Hooks
::run( 'ApiBeforeMain', [ &$processor ] );
72 if ( !$processor instanceof ApiMain
) {
73 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
75 } catch ( Exception
$e ) { // @todo Remove this block when HHVM is no longer supported
76 // Crap. Try to report the exception in API format to be friendly to clients.
77 ApiMain
::handleApiBeforeMainException( $e );
79 } catch ( Throwable
$e ) {
80 // Crap. Try to report the exception in API format to be friendly to clients.
81 ApiMain
::handleApiBeforeMainException( $e );
85 // Process data & print results
87 $processor->execute();
90 // Log what the user did, for book-keeping purposes.
91 $endtime = microtime( true );
94 if ( $wgAPIRequestLog ) {
97 $endtime - $starttime,
99 $wgRequest->getHeader( 'User-agent' )
101 $items[] = $wgRequest->wasPosted() ?
'POST' : 'GET';
104 $manager = $processor->getModuleManager();
105 $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
106 } catch ( Exception
$ex ) { // @todo Remove this block when HHVM is no longer supported
108 } catch ( Throwable
$ex ) {
111 if ( !$module ||
$module->mustBePosted() ) {
112 $items[] = "action=" . $wgRequest->getVal( 'action' );
114 $items[] = wfArrayToCgi( $wgRequest->getValues() );
117 $items[] = "failed in ApiBeforeMain";
119 LegacyLogger
::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
120 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
123 $mediawiki = new MediaWiki();
124 $mediawiki->doPostOutputShutdown( 'fast' );