Merge "Allow constructing a Message from a MessageSpecifier"
[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 checking whether the API is enabled on this wiki; if not,
6 * it informs the user that s/he should set $wgEnableAPI to true and exits.
7 * Otherwise, it constructs a new ApiMain using the parameter passed to it
8 * as an argument in the URL ('?action=') and with write-enabled set to the
9 * value of $wgEnableWriteAPI as specified in LocalSettings.php.
10 * It then invokes "execute()" on the ApiMain object instance, which
11 * produces output in the format specified in the URL.
12 *
13 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 * http://www.gnu.org/copyleft/gpl.html
29 *
30 * @file
31 */
32
33 use MediaWiki\Logger\LegacyLogger;
34
35 // So extensions (and other code) can check whether they're running in API mode
36 define( 'MW_API', true );
37
38 // Bail on old versions of PHP, or if composer has not been run yet to install
39 // dependencies. Using dirname( __FILE__ ) here because __DIR__ is PHP5.3+.
40 require_once dirname( __FILE__ ) . '/includes/PHPVersionCheck.php';
41 wfEntryPointCheck( 'api.php' );
42
43 require __DIR__ . '/includes/WebStart.php';
44
45 $starttime = microtime( true );
46
47 // URL safety checks
48 if ( !$wgRequest->checkUrlExtension() ) {
49 return;
50 }
51
52 // Verify that the API has not been disabled
53 if ( !$wgEnableAPI ) {
54 header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
55 echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
56 . '<pre><b>$wgEnableAPI=true;</b></pre>';
57 die( 1 );
58 }
59
60 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
61 // In a perfect world this wouldn't be necessary
62 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for API calls set in api.php' );
63
64 // RequestContext will read from $wgTitle, but it will also whine about it.
65 // In a perfect world this wouldn't be necessary either.
66 RequestContext::getMain()->setTitle( $wgTitle );
67
68 try {
69 /* Construct an ApiMain with the arguments passed via the URL. What we get back
70 * is some form of an ApiMain, possibly even one that produces an error message,
71 * but we don't care here, as that is handled by the ctor.
72 */
73 $processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
74
75 // Last chance hook before executing the API
76 Hooks::run( 'ApiBeforeMain', array( &$processor ) );
77 if ( !$processor instanceof ApiMain ) {
78 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
79 }
80 } catch ( Exception $e ) {
81 // Crap. Try to report the exception in API format to be friendly to clients.
82 ApiMain::handleApiBeforeMainException( $e );
83 $processor = false;
84 }
85
86 // Process data & print results
87 if ( $processor ) {
88 $processor->execute();
89 }
90
91 // Log what the user did, for book-keeping purposes.
92 $endtime = microtime( true );
93
94 // Log the request
95 if ( $wgAPIRequestLog ) {
96 $items = array(
97 wfTimestamp( TS_MW ),
98 $endtime - $starttime,
99 $wgRequest->getIP(),
100 $wgRequest->getHeader( 'User-agent' )
101 );
102 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
103 if ( $processor ) {
104 try {
105 $manager = $processor->getModuleManager();
106 $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
107 } catch ( Exception $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' );