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