]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - api.php
MediaWiki 1.17.0
[autoinstalls/mediawiki.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 // Initialise common code
41 require ( dirname( __FILE__ ) . '/includes/WebStart.php' );
42
43 wfProfileIn( 'api.php' );
44 $starttime = microtime( true );
45
46 // URL safety checks
47 if ( !$wgRequest->checkUrlExtension() ) {
48         return;
49 }
50
51 // Verify that the API has not been disabled
52 if ( !$wgEnableAPI ) {
53         echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php';
54         echo '<pre><b>$wgEnableAPI=true;</b></pre>';
55         die( 1 );
56 }
57
58 // Selectively allow cross-site AJAX
59
60 /*
61  * Helper function to convert wildcard string into a regex
62  * '*' => '.*?'
63  * '?' => '.'
64  * @ return string
65  */
66 function convertWildcard( $search ) {
67         $search = preg_quote( $search, '/' );
68         $search = str_replace(
69                 array( '\*', '\?' ),
70                 array( '.*?', '.' ),
71                 $search
72         );
73         return "/$search/";
74 }
75
76 if ( $wgCrossSiteAJAXdomains && isset( $_SERVER['HTTP_ORIGIN'] ) ) {
77         $exceptions = array_map( 'convertWildcard', $wgCrossSiteAJAXdomainExceptions );
78         $regexes = array_map( 'convertWildcard', $wgCrossSiteAJAXdomains );
79         foreach ( $regexes as $regex ) {
80                 if ( preg_match( $regex, $_SERVER['HTTP_ORIGIN'] ) ) {
81                         foreach ( $exceptions as $exc ) { // Check against exceptions
82                                 if ( preg_match( $exc, $_SERVER['HTTP_ORIGIN'] ) ) {
83                                         break 2;
84                                 }
85                         }
86                         header( "Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}" );
87                         header( 'Access-Control-Allow-Credentials: true' );
88                         break;
89                 }
90         }
91 }
92
93 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
94 // In a perfect world this wouldn't be necessary
95 $wgTitle = Title::makeTitle( NS_MAIN, 'API' );
96
97 /* Construct an ApiMain with the arguments passed via the URL. What we get back
98  * is some form of an ApiMain, possibly even one that produces an error message,
99  * but we don't care here, as that is handled by the ctor.
100  */
101 $processor = new ApiMain( $wgRequest, $wgEnableWriteAPI );
102
103 // Process data & print results
104 $processor->execute();
105
106 // Execute any deferred updates
107 wfDoUpdates();
108
109 // Log what the user did, for book-keeping purposes.
110 $endtime = microtime( true );
111 wfProfileOut( 'api.php' );
112 wfLogProfilingData();
113
114 // Log the request
115 if ( $wgAPIRequestLog ) {
116         $items = array(
117                         wfTimestamp( TS_MW ),
118                         $endtime - $starttime,
119                         wfGetIP(),
120                         $_SERVER['HTTP_USER_AGENT']
121         );
122         $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
123         if ( $processor->getModule()->mustBePosted() ) {
124                 $items[] = "action=" . $wgRequest->getVal( 'action' );
125         } else {
126                 $items[] = wfArrayToCGI( $wgRequest->getValues() );
127         }
128         wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
129         wfDebug( "Logged API request to $wgAPIRequestLog\n" );
130 }
131
132 // Shut down the database
133 wfGetLBFactory()->shutdown();
134