MediaWiki  1.29.1
api.php
Go to the documentation of this file.
1 <?php
34 
35 // So extensions (and other code) can check whether they're running in API mode
36 define( 'MW_API', true );
37 
38 require __DIR__ . '/includes/WebStart.php';
39 
40 $starttime = microtime( true );
41 
42 // URL safety checks
43 if ( !$wgRequest->checkUrlExtension() ) {
44  return;
45 }
46 
47 // Verify that the API has not been disabled
48 if ( !$wgEnableAPI ) {
49  header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
50  echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
51  . '<pre><b>$wgEnableAPI=true;</b></pre>';
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.
61 RequestContext::getMain()->setTitle( $wgTitle );
62 
63 try {
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(), $wgEnableWriteAPI );
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 ) {
76  // Crap. Try to report the exception in API format to be friendly to clients.
78  $processor = false;
79 }
80 
81 // Process data & print results
82 if ( $processor ) {
83  $processor->execute();
84 }
85 
86 // Log what the user did, for book-keeping purposes.
87 $endtime = microtime( true );
88 
89 // Log the request
90 if ( $wgAPIRequestLog ) {
91  $items = [
92  wfTimestamp( TS_MW ),
94  $wgRequest->getIP(),
95  $wgRequest->getHeader( 'User-agent' )
96  ];
97  $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
98  if ( $processor ) {
99  try {
100  $manager = $processor->getModuleManager();
101  $module = $manager->getModule( $wgRequest->getVal( 'action' ), 'action' );
102  } catch ( Exception $ex ) {
103  $module = null;
104  }
105  if ( !$module || $module->mustBePosted() ) {
106  $items[] = "action=" . $wgRequest->getVal( 'action' );
107  } else {
108  $items[] = wfArrayToCgi( $wgRequest->getValues() );
109  }
110  } else {
111  $items[] = "failed in ApiBeforeMain";
112  }
113  LegacyLogger::emit( implode( ',', $items ) . "\n", $wgAPIRequestLog );
114  wfDebug( "Logged API request to $wgAPIRequestLog\n" );
115 }
116 
118 $mediawiki->doPostOutputShutdown( 'fast' );
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:45
$mediawiki
if( $wgAPIRequestLog) $mediawiki
Definition: api.php:117
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1994
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ApiMain\handleApiBeforeMainException
static handleApiBeforeMainException(Exception $e)
Handle an exception from the ApiBeforeMain hook.
Definition: ApiMain.php:653
$wgTitle
if(! $wgRequest->checkUrlExtension()) if(! $wgEnableAPI) $wgTitle
Definition: api.php:57
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$endtime
if(! $processor instanceof ApiMain) catch(Exception $e) if( $processor) $endtime
Definition: api.php:87
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:51
MWException
MediaWiki exception.
Definition: MWException.php:26
$starttime
$starttime
Definition: api.php:40
MediaWiki
This document describes the state of Postgres support in MediaWiki
Definition: postgres.txt:4
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:514
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:999
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:468
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:639
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
LegacyLogger
MediaWiki Logger LegacyLogger
Definition: logger.txt:54
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:408