three new hooks in SpecialUndelete.php from Wikia codebase so that extensions, such...
[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 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
55
56 wfProfileIn( 'api.php' );
57 $starttime = microtime( true );
58
59 // URL safety checks
60 //
61 // See RawPage.php for details; summary is that MSIE can override the
62 // Content-Type if it sees a recognized extension on the URL, such as
63 // might be appended via PATH_INFO after 'api.php'.
64 //
65 // Some data formats can end up containing unfiltered user-provided data
66 // which will end up triggering HTML detection and execution, hence
67 // XSS injection and all that entails.
68 //
69 if ( $wgRequest->isPathInfoBad() ) {
70 wfHttpError( 403, 'Forbidden',
71 'Invalid file extension found in PATH_INFO or QUERY_STRING.' );
72 return;
73 }
74
75 // Verify that the API has not been disabled
76 if ( !$wgEnableAPI ) {
77 wfDie( 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
78 . '<pre><b>$wgEnableAPI=true;</b></pre>'
79 );
80 }
81
82 // Selectively allow cross-site AJAX
83
84 /*
85 * Helper function to convert wildcard string into a regex
86 * '*' => '.*?'
87 * '?' => '.'
88 * @ return string
89 */
90 function convertWildcard( $search ) {
91 $search = preg_quote( $search, '/' );
92 $search = str_replace(
93 array( '\*', '\?' ),
94 array( '.*?', '.' ),
95 $search
96 );
97 return "/$search/";
98 }
99
100 if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
101 $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
102 $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
103 foreach ( $regexes as $regex ) {
104 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
105 foreach ( $exceptions as $exc ) { // Check against exceptions
106 if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
107 break 2;
108 }
109 }
110 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
111 header( 'Access-Control-Allow-Credentials: true' );
112 break;
113 }
114 }
115 }
116
117 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
118 // In a perfect world this wouldn't be necessary
119 $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
120
121 /* Construct an ApiMain with the arguments passed via the URL. What we get back
122 * is some form of an ApiMain, possibly even one that produces an error message,
123 * but we don't care here, as that is handled by the ctor.
124 */
125 $processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
126
127 // Process data & print results
128 $processor->execute();
129
130 // Execute any deferred updates
131 wfDoUpdates();
132
133 // Log what the user did, for book-keeping purposes.
134 $endtime = microtime( true );
135 wfProfileOut( 'api.php' );
136 wfLogProfilingData();
137
138 // Log the request
139 if ( $wgAPIRequestLog ) {
140 $items = array(
141 wfTimestamp( TS_MW ),
142 $endtime - $starttime,
143 wfGetIP(),
144 $_SERVER['HTTP_USER_AGENT']
145 );
146 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
147 $module = $processor->getModule();
148 if ( $module->mustBePosted() ) {
149 $items[] = "action=" . $wgRequest->getVal( 'action' );
150 } else {
151 $items[] = wfArrayToCGI( $wgRequest->getValues() );
152 }
153 wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
154 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
155 }
156
157 // Shut down the database. foo()->bar() syntax is not supported in PHP4: we won't ever actually
158 // get here to worry about whether this should be = or =&, but the file has to parse properly.
159 $lb = wfGetLBFactory();
160 $lb->shutdown();
161