Use CSS columns instead of tables in Special:SpecialPages
[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 if PHP is too low
39 if ( !function_exists( 'version_compare' ) || version_compare( PHP_VERSION, '5.3.3' ) < 0 ) {
40 // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+
41 require dirname( __FILE__ ) . '/includes/PHPVersionError.php';
42 wfPHPVersionError( 'api.php' );
43 }
44
45 require __DIR__ . '/includes/WebStart.php';
46
47 $starttime = microtime( true );
48
49 // URL safety checks
50 if ( !$wgRequest->checkUrlExtension() ) {
51 return;
52 }
53
54 // Verify that the API has not been disabled
55 if ( !$wgEnableAPI ) {
56 header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
57 echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
58 . '<pre><b>$wgEnableAPI=true;</b></pre>';
59 die( 1 );
60 }
61
62 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
63 // In a perfect world this wouldn't be necessary
64 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for API calls set in api.php' );
65
66 // RequestContext will read from $wgTitle, but it will also whine about it.
67 // In a perfect world this wouldn't be necessary either.
68 RequestContext::getMain()->setTitle( $wgTitle );
69
70 try {
71 /* Construct an ApiMain with the arguments passed via the URL. What we get back
72 * is some form of an ApiMain, possibly even one that produces an error message,
73 * but we don't care here, as that is handled by the ctor.
74 */
75 $processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
76
77 // Last chance hook before executing the API
78 wfRunHooks( 'ApiBeforeMain', array( &$processor ) );
79 if ( !$processor instanceof ApiMain ) {
80 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
81 }
82 } catch ( Exception $e ) {
83 // Crap. Try to report the exception in API format to be friendly to clients.
84 ApiMain::handleApiBeforeMainException( $e );
85 $processor = false;
86 }
87
88 // Process data & print results
89 if ( $processor ) {
90 $processor->execute();
91 }
92
93 if ( function_exists( 'fastcgi_finish_request' ) ) {
94 fastcgi_finish_request();
95 }
96
97 // Execute any deferred updates
98 DeferredUpdates::doUpdates();
99
100 // Log what the user did, for book-keeping purposes.
101 $endtime = microtime( true );
102
103 wfLogProfilingData();
104
105 // Log the request
106 if ( $wgAPIRequestLog ) {
107 $items = array(
108 wfTimestamp( TS_MW ),
109 $endtime - $starttime,
110 $wgRequest->getIP(),
111 $wgRequest->getHeader( 'User-agent' )
112 );
113 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
114 if ( $processor ) {
115 try {
116 $manager = $processor->getModuleManager();
117 $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
118 } catch ( Exception $ex ) {
119 $module = null;
120 }
121 if ( !$module || $module->mustBePosted() ) {
122 $items[] = "action=" . $wgRequest->getVal( 'action' );
123 } else {
124 $items[] = wfArrayToCgi( $wgRequest->getValues() );
125 }
126 } else {
127 $items[] = "failed in ApiBeforeMain";
128 }
129 LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
130 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
131 }
132
133 // Shut down the database. foo()->bar() syntax is not supported in PHP4: we won't ever actually
134 // get here to worry about whether this should be = or =&, but the file has to parse properly.
135 $lb = wfGetLBFactory();
136 $lb->shutdown();