Add links to Special:EditWatchlist in Preferences' Watchlist tab
[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 // So extensions (and other code) can check whether they're running in API mode
34 define( 'MW_API', true );
35
36 // Bail if PHP is too low
37 if ( !function_exists( 'version_compare' ) || version_compare( PHP_VERSION, '5.3.3' ) < 0 ) {
38 // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+
39 require dirname( __FILE__ ) . '/includes/PHPVersionError.php';
40 wfPHPVersionError( 'api.php' );
41 }
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_MAIN, 'API' );
63
64 try {
65 /* Construct an ApiMain with the arguments passed via the URL. What we get back
66 * is some form of an ApiMain, possibly even one that produces an error message,
67 * but we don't care here, as that is handled by the ctor.
68 */
69 $processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
70
71 // Last chance hook before executing the API
72 wfRunHooks( 'ApiBeforeMain', array( &$processor ) );
73 if ( !$processor instanceof ApiMain ) {
74 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
75 }
76 } catch ( Exception $e ) {
77 // Crap. Try to report the exception in API format to be friendly to clients.
78 ApiMain::handleApiBeforeMainException( $e );
79 $processor = false;
80 }
81
82 // Process data & print results
83 if ( $processor ) {
84 $processor->execute();
85 }
86
87 if ( function_exists( 'fastcgi_finish_request' ) ) {
88 fastcgi_finish_request();
89 }
90
91 // Execute any deferred updates
92 DeferredUpdates::doUpdates();
93
94 // Log what the user did, for book-keeping purposes.
95 $endtime = microtime( true );
96
97 wfLogProfilingData();
98
99 // Log the request
100 if ( $wgAPIRequestLog ) {
101 $items = array(
102 wfTimestamp( TS_MW ),
103 $endtime - $starttime,
104 $wgRequest->getIP(),
105 $wgRequest->getHeader( 'User-agent' )
106 );
107 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
108 if ( $processor ) {
109 try {
110 $manager = $processor->getModuleManager();
111 $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
112 } catch ( Exception $ex ) {
113 $module = null;
114 }
115 if ( !$module || $module->mustBePosted() ) {
116 $items[] = "action=" . $wgRequest->getVal( 'action' );
117 } else {
118 $items[] = wfArrayToCgi( $wgRequest->getValues() );
119 }
120 } else {
121 $items[] = "failed in ApiBeforeMain";
122 }
123 MWLoggerLegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
124 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
125 }
126
127 // Shut down the database. foo()->bar() syntax is not supported in PHP4: we won't ever actually
128 // get here to worry about whether this should be = or =&, but the file has to parse properly.
129 $lb = wfGetLBFactory();
130 $lb->shutdown();