composer.json: Add a phan script command
[mediawiki.git] / api.php
blob99c8430894bb3e249a3bb6b598ad4b31c7488ef8
1 <?php
2 /**
3 * The web entry point for all %Action API queries, handled by ApiMain
4 * and ApiBase subclasses.
6 * This is used by bots to fetch content and information about the wiki,
7 * its pages, and its users. See <https://www.mediawiki.org/wiki/API> for more
8 * information.
10 * It begins by constructing a new ApiMain using the parameter passed to it
11 * as an argument in the URL ('?action='). It then invokes "execute()" on the
12 * ApiMain object instance, which produces output in the format specified in
13 * the URL.
15 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 * http://www.gnu.org/copyleft/gpl.html
32 * @file
33 * @ingroup entrypoint
34 * @ingroup API
37 use MediaWiki\Logger\LegacyLogger;
39 // So extensions (and other code) can check whether they're running in API mode
40 define( 'MW_API', true );
41 define( 'MW_ENTRY_POINT', 'api' );
43 require __DIR__ . '/includes/WebStart.php';
45 wfApiMain();
47 function wfApiMain() {
48 global $wgRequest, $wgTitle, $wgAPIRequestLog;
50 $starttime = microtime( true );
52 // PATH_INFO can be used for stupid things. We don't support it for api.php at
53 // all, so error out if it's present. (T128209)
54 if ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
55 $correctUrl = wfAppendQuery( wfScript( 'api' ), $wgRequest->getQueryValuesOnly() );
56 $correctUrl = wfExpandUrl( $correctUrl, PROTO_CANONICAL );
57 header( "Location: $correctUrl", true, 301 );
58 echo 'This endpoint does not support "path info", i.e. extra text between "api.php"'
59 . 'and the "?". Remove any such text and try again.';
60 die( 1 );
63 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
64 // In a perfect world this wouldn't be necessary
65 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for API calls set in api.php' );
67 // RequestContext will read from $wgTitle, but it will also whine about it.
68 // In a perfect world this wouldn't be necessary either.
69 RequestContext::getMain()->setTitle( $wgTitle );
71 try {
72 // Construct an ApiMain with the arguments passed via the URL. What we get back
73 // is some form of an ApiMain, possibly even one that produces an error message,
74 // but we don't care here, as that is handled by the constructor.
75 $processor = new ApiMain( RequestContext::getMain(), true );
77 // Last chance hook before executing the API
78 Hooks::runner()->onApiBeforeMain( $processor );
79 if ( !$processor instanceof ApiMain ) {
80 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
82 } catch ( Throwable $e ) {
83 // Crap. Try to report the exception in API format to be friendly to clients.
84 ApiMain::handleApiBeforeMainException( $e );
85 $processor = false;
88 // Process data & print results
89 if ( $processor ) {
90 $processor->execute();
93 // Log what the user did, for book-keeping purposes.
94 $endtime = microtime( true );
96 // Log the request
97 if ( $wgAPIRequestLog ) {
98 $items = [
99 wfTimestamp( TS_MW ),
100 $endtime - $starttime,
101 $wgRequest->getIP(),
102 $wgRequest->getHeader( 'User-agent' )
104 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
105 if ( $processor ) {
106 try {
107 $manager = $processor->getModuleManager();
108 $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
109 } catch ( Throwable $ex ) {
110 $module = null;
112 if ( !$module || $module->mustBePosted() ) {
113 $items[] = "action=" . $wgRequest->getVal( 'action' );
114 } else {
115 $items[] = wfArrayToCgi( $wgRequest->getValues() );
117 } else {
118 $items[] = "failed in ApiBeforeMain";
120 LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
121 wfDebug( "Logged API request to $wgAPIRequestLog" );
124 $mediawiki = new MediaWiki();
125 $mediawiki->doPostOutputShutdown();