code readability is everything
[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 // We want a plain message on catastrophic errors that machines can identify
41 function wfDie( $msg = '' ) {
42 header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
43 echo $msg;
44 die( 1 );
45 }
46
47 // Die on unsupported PHP versions
48 if( !function_exists( 'version_compare' ) || version_compare( phpversion(), '5.2.3' ) < 0 ){
49 $version = htmlspecialchars( $wgVersion );
50 wfDie( "MediaWiki $version requires at least PHP version 5.2.3." );
51 }
52
53 // Initialise common code.
54 if ( isset( $_SERVER['MW_COMPILED'] ) ) {
55 require ( 'phase3/includes/WebStart.php' );
56 } else {
57 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
58 }
59
60 wfProfileIn( 'api.php' );
61 $starttime = microtime( true );
62
63 // URL safety checks
64 if ( !$wgRequest->checkUrlExtension() ) {
65 return;
66 }
67
68 // Verify that the API has not been disabled
69 if ( !$wgEnableAPI ) {
70 wfDie( 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
71 . '<pre><b>$wgEnableAPI=true;</b></pre>'
72 );
73 }
74
75 // Selectively allow cross-site AJAX
76
77 /*
78 * Helper function to convert wildcard string into a regex
79 * '*' => '.*?'
80 * '?' => '.'
81 * @ return string
82 */
83 function convertWildcard( $search ) {
84 $search = preg_quote( $search, '/' );
85 $search = str_replace(
86 array( '\*', '\?' ),
87 array( '.*?', '.' ),
88 $search
89 );
90 return "/$search/";
91 }
92
93 if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
94 $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
95 $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
96 foreach ( $regexes as $regex ) {
97 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
98 foreach ( $exceptions as $exc ) { // Check against exceptions
99 if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
100 break 2;
101 }
102 }
103 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
104 header( 'Access-Control-Allow-Credentials: true' );
105 break;
106 }
107 }
108 }
109
110 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
111 // In a perfect world this wouldn't be necessary
112 $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
113
114 /* Construct an ApiMain with the arguments passed via the URL. What we get back
115 * is some form of an ApiMain, possibly even one that produces an error message,
116 * but we don't care here, as that is handled by the ctor.
117 */
118 $processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
119
120 // Process data & print results
121 $processor->execute();
122
123 // Execute any deferred updates
124 wfDoUpdates();
125
126 // Log what the user did, for book-keeping purposes.
127 $endtime = microtime( true );
128 wfProfileOut( 'api.php' );
129 wfLogProfilingData();
130
131 // Log the request
132 if ( $wgAPIRequestLog ) {
133 $items = array(
134 wfTimestamp( TS_MW ),
135 $endtime - $starttime,
136 wfGetIP(),
137 $_SERVER['HTTP_USER_AGENT']
138 );
139 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
140 $module = $processor->getModule();
141 if ( $module->mustBePosted() ) {
142 $items[] = "action=" . $wgRequest->getVal( 'action' );
143 } else {
144 $items[] = wfArrayToCGI( $wgRequest->getValues() );
145 }
146 wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
147 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
148 }
149
150 // Shut down the database. foo()->bar() syntax is not supported in PHP4: we won't ever actually
151 // get here to worry about whether this should be = or =&, but the file has to parse properly.
152 $lb = wfGetLBFactory();
153 $lb->shutdown();
154