MediaWiki REL1_39
api.php
Go to the documentation of this file.
1<?php
38
39// So extensions (and other code) can check whether they're running in API mode
40define( 'MW_API', true );
41define( 'MW_ENTRY_POINT', 'api' );
42
43require __DIR__ . '/includes/WebStart.php';
44
45wfApiMain();
46
47function wfApiMain() {
49
50 $starttime = microtime( true );
51
52 // PATH_INFO can be used for stupid things. We don't support it for api.php at
53 // all, so error out if it's present. (T128209)
54 if ( isset( $_SERVER['PATH_INFO'] ) && $_SERVER['PATH_INFO'] != '' ) {
55 $correctUrl = wfAppendQuery( wfScript( 'api' ), $wgRequest->getQueryValuesOnly() );
56 $correctUrl = wfExpandUrl( $correctUrl, PROTO_CANONICAL );
57 header( "Location: $correctUrl", true, 301 );
58 echo 'This endpoint does not support "path info", i.e. extra text between "api.php"'
59 . 'and the "?". Remove any such text and try again.';
60 die( 1 );
61 }
62
63 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
64 // In a perfect world this wouldn't be necessary
65 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle/dummy title for API calls set in api.php' );
66
67 // RequestContext will read from $wgTitle, but it will also whine about it.
68 // In a perfect world this wouldn't be necessary either.
69 RequestContext::getMain()->setTitle( $wgTitle );
70
71 try {
72 // Construct an ApiMain with the arguments passed via the URL. What we get back
73 // is some form of an ApiMain, possibly even one that produces an error message,
74 // but we don't care here, as that is handled by the constructor.
75 $processor = new ApiMain( RequestContext::getMain(), true );
76
77 // Last chance hook before executing the API
78 Hooks::runner()->onApiBeforeMain( $processor );
79 if ( !$processor instanceof ApiMain ) {
80 throw new MWException( 'ApiBeforeMain hook set $processor to a non-ApiMain class' );
81 }
82 } catch ( Throwable $e ) {
83 // Crap. Try to report the exception in API format to be friendly to clients.
84 ApiMain::handleApiBeforeMainException( $e );
85 $processor = false;
86 }
87
88 // Process data & print results
89 if ( $processor ) {
90 $processor->execute();
91 }
92
93 // Log what the user did, for book-keeping purposes.
94 $endtime = microtime( true );
95
96 // Log the request
97 if ( $wgAPIRequestLog ) {
98 $items = [
99 wfTimestamp( TS_MW ),
100 $endtime - $starttime,
101 $wgRequest->getIP(),
102 $wgRequest->getHeader( 'User-agent' )
103 ];
104 $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
105 if ( $processor ) {
106 try {
107 $manager = $processor->getModuleManager();
108 $module = $manager->getModule( $wgRequest->getRawVal( 'action' ), 'action' );
109 } catch ( Throwable $ex ) {
110 $module = null;
111 }
112 if ( !$module || $module->mustBePosted() ) {
113 $items[] = "action=" . $wgRequest->getRawVal( 'action' );
114 } else {
115 $items[] = wfArrayToCgi( $wgRequest->getValues() );
116 }
117 } else {
118 $items[] = "failed in ApiBeforeMain";
119 }
120 LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
121 wfDebug( "Logged API request to $wgAPIRequestLog" );
122 }
123
124 $mediawiki = new MediaWiki();
125 $mediawiki->doPostOutputShutdown();
126}
const PROTO_CANONICAL
Definition Defines.php:199
const NS_SPECIAL
Definition Defines.php:53
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.
global $wgRequest
Definition Setup.php:377
if(!defined( 'MW_NO_SESSION') &&! $wgCommandLineMode) $wgTitle
Definition Setup.php:497
wfApiMain()
Definition api.php:47
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:52
MediaWiki exception.
PSR-3 logger that mimics the historic implementation of MediaWiki's former wfErrorLog logging impleme...
$wgAPIRequestLog
Config variable stub for the APIRequestLog setting, for use by phpdoc and IDEs.
$mediawiki
Definition img_auth.php:48
A helper class for throttling authentication attempts.