Partial revert r97035, followup r96930: make recentchanges tests pass again
[lhc/web/wiklou.git] / api.php
1 <?php
2
3 /**
4 * API for MediaWiki 1.8+
5 *
6 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 /**
27 * This file is the entry point for all API queries. It begins by checking
28 * whether the API is enabled on this wiki; if not, it informs the user that
29 * s/he should set $wgEnableAPI to true and exits. Otherwise, it constructs
30 * a new ApiMain using the parameter passed to it as an argument in the URL
31 * ('?action=') and with write-enabled set to the value of $wgEnableWriteAPI
32 * as specified in LocalSettings.php. It then invokes "execute()" on the
33 * ApiMain object instance, which produces output in the format sepecified
34 * in the URL.
35 */
36
37 // So extensions (and other code) can check whether they're running in API mode
38 define( 'MW_API', true );
39
40 // Bail if PHP is too low
41 if ( !function_exists( 'version_compare' ) || version_compare( phpversion(), '5.2.3' ) < 0 ) {
42 require( dirname( __FILE__ ) . '/includes/PHPVersionError.php' );
43 wfPHPVersionError( 'api.php' );
44 }
45
46 // Initialise common code.
47 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
48 require ( 'phase3/includes/WebStart.php' );
49 } else {
50 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
51 }
52
53 wfProfileIn( 'api.php' );
54 $starttime = microtime( true );
55
56 // URL safety checks
57 if ( !$wgRequest->checkUrlExtension() ) {
58 return;
59 }
60
61 // Verify that the API has not been disabled
62 if ( !$wgEnableAPI ) {
63 header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
64 echo( 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
65 . '<pre><b>$wgEnableAPI=true;</b></pre>' );
66 die(1);
67 }
68
69 // Selectively allow cross-site AJAX
70
71 /**
72 * Helper function to convert wildcard string into a regex
73 * '*' => '.*?'
74 * '?' => '.'
75 *
76 * @param $search string
77 * @return string
78 */
79 function convertWildcard( $search ) {
80 $search = preg_quote( $search, '/' );
81 $search = str_replace(
82 array( '\*', '\?' ),
83 array( '.*?', '.' ),
84 $search
85 );
86 return "/$search/";
87 }
88
89 if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
90 $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
91 $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
92 foreach ( $regexes as $regex ) {
93 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
94 foreach ( $exceptions as $exc ) { // Check against exceptions
95 if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
96 break 2;
97 }
98 }
99 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
100 header( 'Access-Control-Allow-Credentials: true' );
101 break;
102 }
103 }
104 }
105
106 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
107 // In a perfect world this wouldn't be necessary
108 //
109 // @todo @fixme Ummmm, doesn't this line make the PHP4 check at the top completely
110 // useless? Suggest moving more stuff out of api.php like we did with index.php
111 $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
112
113 /* Construct an ApiMain with the arguments passed via the URL. What we get back
114 * is some form of an ApiMain, possibly even one that produces an error message,
115 * but we don't care here, as that is handled by the ctor.
116 */
117 $processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
118
119 // Process data & print results
120 $processor->execute();
121
122 // Execute any deferred updates
123 wfDoUpdates();
124
125 // Log what the user did, for book-keeping purposes.
126 $endtime = microtime( true );
127 wfProfileOut( 'api.php' );
128 wfLogProfilingData();
129
130 // Log the request
131 if ( $wgAPIRequestLog ) {
132 $items = array(
133 wfTimestamp( TS_MW ),
134 $endtime - $starttime,
135 $wgRequest->getIP(),
136 $_SERVER['HTTP_USER_AGENT']
137 );
138 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
139 $module = $processor->getModule();
140 if ( $module->mustBePosted() ) {
141 $items[] = "action=" . $wgRequest->getVal( 'action' );
142 } else {
143 $items[] = wfArrayToCGI( $wgRequest->getValues() );
144 }
145 wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
146 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
147 }
148
149 // Shut down the database. foo()->bar() syntax is not supported in PHP4: we won't ever actually
150 // get here to worry about whether this should be = or =&, but the file has to parse properly.
151 $lb = wfGetLBFactory();
152 $lb->shutdown();
153