Add jakub-onderka/php-console-highlighter 0.3.2 explicitly
[mediawiki.git] / api.php
blobdb9de75156e9502a8548fdf9a7258ba42b33d8ad
1 <?php
2 /**
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
8 * the URL.
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
27 * @file
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 );
39 // URL safety checks
40 if ( !$wgRequest->checkUrlExtension() ) {
41 return;
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.';
52 die( 1 );
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 );
63 try {
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 );
78 $processor = false;
79 } catch ( Throwable $e ) {
80 // Crap. Try to report the exception in API format to be friendly to clients.
81 ApiMain::handleApiBeforeMainException( $e );
82 $processor = false;
85 // Process data & print results
86 if ( $processor ) {
87 $processor->execute();
90 // Log what the user did, for book-keeping purposes.
91 $endtime = microtime( true );
93 // Log the request
94 if ( $wgAPIRequestLog ) {
95 $items = [
96 wfTimestamp( TS_MW ),
97 $endtime - $starttime,
98 $wgRequest->getIP(),
99 $wgRequest->getHeader( 'User-agent' )
101 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
102 if ( $processor ) {
103 try {
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
107 $module = null;
108 } catch ( Throwable $ex ) {
109 $module = null;
111 if ( !$module || $module->mustBePosted() ) {
112 $items[] = "action=" . $wgRequest->getVal( 'action' );
113 } else {
114 $items[] = wfArrayToCgi( $wgRequest->getValues() );
116 } else {
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' );