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