Use Remex for DeduplicateStyles transform
[mediawiki.git] / api.php
blobf6999655b4df2394450d0f883beaec79db012b8e
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\HookContainer\HookRunner;
38 use MediaWiki\Logger\LegacyLogger;
39 use MediaWiki\MediaWikiServices;
40 use MediaWiki\Title\Title;
42 // So extensions (and other code) can check whether they're running in API mode
43 define( 'MW_API', true );
44 define( 'MW_ENTRY_POINT', 'api' );
46 require __DIR__ . '/includes/WebStart.php';
48 wfApiMain();
50 function wfApiMain() {
51 global $wgRequest, $wgTitle, $wgAPIRequestLog;
53 $starttime = microtime( true );
55 $services = MediaWikiServices::getInstance();
57 // PATH_INFO can be used for stupid things. We don't support it for api.php at
58 // all, so error out if it's present. (T128209)
59 if ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
60 $correctUrl = wfAppendQuery( wfScript( 'api' ), $wgRequest->getQueryValuesOnly() );
61 $correctUrl = (string)$services->getUrlUtils()->expand( $correctUrl, PROTO_CANONICAL );
62 header( "Location: $correctUrl", true, 301 );
63 echo 'This endpoint does not support "path info", i.e. extra text between "api.php"'
64 . 'and the "?". Remove any such text and try again.';
65 die( 1 );
68 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
69 // In a perfect world this wouldn't be necessary
70 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for API calls set in api.php' );
72 // RequestContext will read from $wgTitle, but it will also whine about it.
73 // In a perfect world this wouldn't be necessary either.
74 RequestContext::getMain()->setTitle( $wgTitle );
76 try {
77 // Construct an ApiMain with the arguments passed via the URL. What we get back
78 // is some form of an ApiMain, possibly even one that produces an error message,
79 // but we don't care here, as that is handled by the constructor.
80 $processor = new ApiMain( RequestContext::getMain(), true );
82 // Last chance hook before executing the API
83 ( new HookRunner( $services->getHookContainer() ) )->onApiBeforeMain( $processor );
84 if ( !$processor instanceof ApiMain ) {
85 throw new LogicException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
87 } catch ( Throwable $e ) {
88 // Crap. Try to report the exception in API format to be friendly to clients.
89 ApiMain::handleApiBeforeMainException( $e );
90 $processor = false;
93 // Process data & print results
94 if ( $processor ) {
95 $processor->execute();
98 // Log what the user did, for book-keeping purposes.
99 $endtime = microtime( true );
101 // Log the request
102 if ( $wgAPIRequestLog ) {
103 $items = [
104 wfTimestamp( TS_MW ),
105 $endtime - $starttime,
106 $wgRequest->getIP(),
107 $wgRequest->getHeader( 'User-agent' )
109 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
110 if ( $processor ) {
111 try {
112 $manager = $processor->getModuleManager();
113 $module = $manager->getModule( $wgRequest->getRawVal( 'action' ), 'action' );
114 } catch ( Throwable $ex ) {
115 $module = null;
117 if ( !$module || $module->mustBePosted() ) {
118 $items[] = "action=" . $wgRequest->getRawVal( 'action' );
119 } else {
120 $items[] = wfArrayToCgi( $wgRequest->getValues() );
122 } else {
123 $items[] = "failed in ApiBeforeMain";
125 LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
126 wfDebug( "Logged API request to $wgAPIRequestLog" );
129 $mediawiki = new MediaWiki();
130 $mediawiki->doPostOutputShutdown();