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