4 * API for MediaWiki 1.8+
6 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
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
37 // So extensions (and other code) can check whether they're running in API mode
38 define( 'MW_API', true );
40 // Initialise common code
41 require ( dirname( __FILE__
) . '/includes/WebStart.php' );
43 wfProfileIn( 'api.php' );
44 $starttime = microtime( true );
48 // See RawPage.php for details; summary is that MSIE can override the
49 // Content-Type if it sees a recognized extension on the URL, such as
50 // might be appended via PATH_INFO after 'api.php'.
52 // Some data formats can end up containing unfiltered user-provided data
53 // which will end up triggering HTML detection and execution, hence
54 // XSS injection and all that entails.
56 if ( $wgRequest->isPathInfoBad() ) {
57 wfHttpError( 403, 'Forbidden',
58 'Invalid file extension found in PATH_INFO or QUERY_STRING.' );
62 // Verify that the API has not been disabled
63 if ( !$wgEnableAPI ) {
64 echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php';
65 echo '<pre><b>$wgEnableAPI=true;</b></pre>';
69 // Selectively allow cross-site AJAX
72 * Helper function to convert wildcard string into a regex
77 function convertWildcard( $search ) {
78 $search = preg_quote( $search, '/' );
79 $search = str_replace(
87 if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
88 $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
89 $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
90 foreach ( $regexes as $regex ) {
91 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
92 foreach ( $exceptions as $exc ) { // Check against exceptions
93 if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
97 header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
98 header( 'Access-Control-Allow-Credentials: true' );
104 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
105 // In a perfect world this wouldn't be necessary
106 $wgTitle = Title
::makeTitle( NS_MAIN
, 'API' );
108 /* Construct an ApiMain with the arguments passed via the URL. What we get back
109 * is some form of an ApiMain, possibly even one that produces an error message,
110 * but we don't care here, as that is handled by the ctor.
112 $processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
114 // Process data & print results
115 $processor->execute();
117 // Execute any deferred updates
120 // Log what the user did, for book-keeping purposes.
121 $endtime = microtime( true );
122 wfProfileOut( 'api.php' );
123 wfLogProfilingData();
126 if ( $wgAPIRequestLog ) {
128 wfTimestamp( TS_MW
),
129 $endtime - $starttime,
131 $_SERVER['HTTP_USER_AGENT']
133 $items[] = $wgRequest->wasPosted() ?
'POST' : 'GET';
134 if ( $processor->getModule()->mustBePosted() ) {
135 $items[] = "action=" . $wgRequest->getVal( 'action' );
137 $items[] = wfArrayToCGI( $wgRequest->getValues() );
139 wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
140 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
143 // Shut down the database
144 wfGetLBFactory()->shutdown();