Merge "resourceloader: Add $conf parameter to the 'ResourceLoaderGetConfigVars' hook"
[lhc/web/wiklou.git] / api.php
1 <?php
2 /**
3 * This file is the entry point for all API queries.
4 *
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.
9 *
10 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
11 *
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.
16 *
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.
21 *
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
26 *
27 * @file
28 */
29
30 use MediaWiki\Logger\LegacyLogger;
31
32 // So extensions (and other code) can check whether they're running in API mode
33 define( 'MW_API', true );
34
35 require __DIR__ . '/includes/WebStart.php';
36
37 $starttime = microtime( true );
38
39 // URL safety checks
40 if ( !$wgRequest->checkUrlExtension() ) {
41 return;
42 }
43
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->getQueryValuesOnly() );
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 );
53 }
54
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' );
58
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 );
62
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.
67 $processor = new ApiMain( RequestContext::getMain(), true );
68
69 // Last chance hook before executing the API
70 Hooks::run( 'ApiBeforeMain', [ &$processor ] );
71 if ( !$processor instanceof ApiMain ) {
72 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
73 }
74 } catch ( Exception $e ) { // @todo Remove this block when HHVM is no longer supported
75 // Crap. Try to report the exception in API format to be friendly to clients.
76 ApiMain::handleApiBeforeMainException( $e );
77 $processor = false;
78 } catch ( Throwable $e ) {
79 // Crap. Try to report the exception in API format to be friendly to clients.
80 ApiMain::handleApiBeforeMainException( $e );
81 $processor = false;
82 }
83
84 // Process data & print results
85 if ( $processor ) {
86 $processor->execute();
87 }
88
89 // Log what the user did, for book-keeping purposes.
90 $endtime = microtime( true );
91
92 // Log the request
93 if ( $wgAPIRequestLog ) {
94 $items = [
95 wfTimestamp( TS_MW ),
96 $endtime - $starttime,
97 $wgRequest->getIP(),
98 $wgRequest->getHeader( 'User-agent' )
99 ];
100 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
101 if ( $processor ) {
102 try {
103 $manager = $processor->getModuleManager();
104 $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
105 } catch ( Exception $ex ) { // @todo Remove this block when HHVM is no longer supported
106 $module = null;
107 } catch ( Throwable $ex ) {
108 $module = null;
109 }
110 if ( !$module || $module->mustBePosted() ) {
111 $items[] = "action=" . $wgRequest->getVal( 'action' );
112 } else {
113 $items[] = wfArrayToCgi( $wgRequest->getValues() );
114 }
115 } else {
116 $items[] = "failed in ApiBeforeMain";
117 }
118 LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
119 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
120 }
121
122 $mediawiki = new MediaWiki();
123 $mediawiki->doPostOutputShutdown( 'fast' );