MediaWiki REL1_32
api.php
Go to the documentation of this file.
1<?php
31
32// So extensions (and other code) can check whether they're running in API mode
33define( 'MW_API', true );
34
35require __DIR__ . '/includes/WebStart.php';
36
37$starttime = microtime( true );
38
39// URL safety checks
40if ( !$wgRequest->checkUrlExtension() ) {
41 return;
42}
43
44// PATH_INFO can be used for stupid things. We don't support it for api.php at
45// all, so error out if it's present.
46if ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
47 $correctUrl = wfAppendQuery( wfScript( 'api' ), $wgRequest->getQueryValues() );
48 $correctUrl = wfExpandUrl( $correctUrl, PROTO_CANONICAL );
49 header( "Location: $correctUrl", true, 301 );
50 echo 'This endpoint does not support "path info", i.e. extra text between "api.php"'
51 . 'and the "?". Remove any such text and try again.';
52 die( 1 );
53}
54
55// Set a dummy $wgTitle, because $wgTitle == null breaks various things
56// In a perfect world this wouldn't be necessary
57$wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for API calls set in api.php' );
58
59// RequestContext will read from $wgTitle, but it will also whine about it.
60// In a perfect world this wouldn't be necessary either.
61RequestContext::getMain()->setTitle( $wgTitle );
62
63try {
64 /* Construct an ApiMain with the arguments passed via the URL. What we get back
65 * is some form of an ApiMain, possibly even one that produces an error message,
66 * but we don't care here, as that is handled by the constructor.
67 */
68 $processor = new ApiMain( RequestContext::getMain(), true );
69
70 // Last chance hook before executing the API
71 Hooks::run( 'ApiBeforeMain', [ &$processor ] );
72 if ( !$processor instanceof ApiMain ) {
73 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
74 }
75} catch ( Exception $e ) { // @todo Remove this block when HHVM is no longer supported
76 // Crap. Try to report the exception in API format to be friendly to clients.
78 $processor = false;
79} catch ( Throwable $e ) {
80 // Crap. Try to report the exception in API format to be friendly to clients.
82 $processor = false;
83}
84
85// Process data & print results
86if ( $processor ) {
87 $processor->execute();
88}
89
90// Log what the user did, for book-keeping purposes.
91$endtime = microtime( true );
92
93// Log the request
94if ( $wgAPIRequestLog ) {
95 $items = [
96 wfTimestamp( TS_MW ),
98 $wgRequest->getIP(),
99 $wgRequest->getHeader( 'User-agent' )
100 ];
101 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
102 if ( $processor ) {
103 try {
104 $manager = $processor->getModuleManager();
105 $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
106 } catch ( Exception $ex ) { // @todo Remove this block when HHVM is no longer supported
107 $module = null;
108 } catch ( Throwable $ex ) {
109 $module = null;
110 }
111 if ( !$module || $module->mustBePosted() ) {
112 $items[] = "action=" . $wgRequest->getVal( 'action' );
113 } else {
114 $items[] = wfArrayToCgi( $wgRequest->getValues() );
115 }
116 } else {
117 $items[] = "failed in ApiBeforeMain";
118 }
119 LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
120 wfDebug( "Logged API request to $wgAPIRequestLog\n" );
121}
122
124$mediawiki->doPostOutputShutdown( 'fast' );
$wgAPIRequestLog
Log file or URL (TCP or UDP) to log API requests to, or false to disable API request logging.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes one or two arrays as input, and returns a CGI-style string, e....
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(! $wgDBerrorLogTZ) $wgRequest
Definition Setup.php:747
if( $wgAPIRequestLog) $mediawiki
Definition api.php:123
if(! $processor instanceof ApiMain) catch(Exception $e) catch(Throwable $e) if( $processor) $endtime
Definition api.php:91
if(! $wgRequest->checkUrlExtension()) if(isset( $_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !='') $wgTitle
Definition api.php:57
$starttime
Definition api.php:37
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:41
static handleApiBeforeMainException( $e)
Handle an exception from the ApiBeforeMain hook.
Definition ApiMain.php:637
MediaWiki exception.
PSR-3 logger that mimics the historic implementation of MediaWiki's former wfErrorLog logging impleme...
returning false will NOT prevent logging $e
Definition hooks.txt:2226
const PROTO_CANONICAL
Definition Defines.php:223
const NS_SPECIAL
Definition Defines.php:53
A helper class for throttling authentication attempts.