Drop wf(Suppress|Restore)Warnings, deprecated in 1.26
[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->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 );
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 */
68 $processor = new ApiMain( RequestContext::getMain(), true );
69
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' );
74 }
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;
83 }
84
85 // Process data & print results
86 if ( $processor ) {
87 $processor->execute();
88 }
89
90 // Log what the user did, for book-keeping purposes.
91 $endtime = microtime( true );
92
93 // Log the request
94 if ( $wgAPIRequestLog ) {
95 $items = [
96 wfTimestamp( TS_MW ),
97 $endtime - $starttime,
98 $wgRequest->getIP(),
99 $wgRequest->getHeader( 'User-agent' )
100 ];
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;
110 }
111 if ( !$module || $module->mustBePosted() ) {
112 $items[] = "action=" . $wgRequest->getVal( 'action' );
113 } else {
114 $items[] = wfArrayToCgi( $wgRequest->getValues() );
115 }
116 } else {
117 $items[] = "failed in ApiBeforeMain";
118 }
119 LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
120 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
121 }
122
123 $mediawiki = new MediaWiki();
124 $mediawiki->doPostOutputShutdown( 'fast' );