MediaWiki  1.23.2
api.php
Go to the documentation of this file.
1 <?php
33 // So extensions (and other code) can check whether they're running in API mode
34 define( 'MW_API', true );
35 
36 // Bail if PHP is too low
37 if ( !function_exists( 'version_compare' ) || version_compare( phpversion(), '5.3.2' ) < 0 ) {
38  // We need to use dirname( __FILE__ ) here cause __DIR__ is PHP5.3+
39  require dirname( __FILE__ ) . '/includes/PHPVersionError.php';
40  wfPHPVersionError( 'api.php' );
41 }
42 
43 require __DIR__ . '/includes/WebStart.php';
44 
45 wfProfileIn( 'api.php' );
46 $starttime = microtime( true );
47 
48 // URL safety checks
49 if ( !$wgRequest->checkUrlExtension() ) {
50  return;
51 }
52 
53 // Verify that the API has not been disabled
54 if ( !$wgEnableAPI ) {
55  header( $_SERVER['SERVER_PROTOCOL'] . ' 500 MediaWiki configuration Error', true, 500 );
56  echo 'MediaWiki API is not enabled for this site. Add the following line to your LocalSettings.php'
57  . '<pre><b>$wgEnableAPI=true;</b></pre>';
58  die( 1 );
59 }
60 
61 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
62 // In a perfect world this wouldn't be necessary
64 
65 /* Construct an ApiMain with the arguments passed via the URL. What we get back
66  * is some form of an ApiMain, possibly even one that produces an error message,
67  * but we don't care here, as that is handled by the ctor.
68  */
69 $processor = new ApiMain( RequestContext::getMain(), $wgEnableWriteAPI );
70 
71 // Last chance hook before executing the API
72 try {
73  wfRunHooks( 'ApiBeforeMain', array( &$processor ) );
74  if ( !$processor instanceof ApiMain ) {
75  throw new MWException( 'ApiBeforMain hook set $processor to a non-ApiMain class' );
76  }
77 } catch ( Exception $e ) {
78  // Crap. Try to report the exception in API format to be friendly to clients.
80  $processor = false;
81 }
82 
83 // Process data & print results
84 if ( $processor ) {
85  $processor->execute();
86 }
87 
88 if ( function_exists( 'fastcgi_finish_request' ) ) {
89  fastcgi_finish_request();
90 }
91 
92 // Execute any deferred updates
94 
95 // Log what the user did, for book-keeping purposes.
96 $endtime = microtime( true );
97 wfProfileOut( 'api.php' );
98 
100 
101 // Log the request
102 if ( $wgAPIRequestLog ) {
103  $items = array(
104  wfTimestamp( TS_MW ),
106  $wgRequest->getIP(),
107  $_SERVER['HTTP_USER_AGENT']
108  );
109  $items[] = $wgRequest->wasPosted() ? 'POST' : 'GET';
110  if ( $processor ) {
111  $module = $processor->getModule();
112  if ( $module->mustBePosted() ) {
113  $items[] = "action=" . $wgRequest->getVal( 'action' );
114  } else {
115  $items[] = wfArrayToCgi( $wgRequest->getValues() );
116  }
117  } else {
118  $items[] = "failed in ApiBeforeMain";
119  }
120  wfErrorLog( implode( ',', $items ) . "\n", $wgAPIRequestLog );
121  wfDebug( "Logged API request to $wgAPIRequestLog\n" );
122 }
123 
124 // Shut down the database. foo()->bar() syntax is not supported in PHP4: we won't ever actually
125 // get here to worry about whether this should be = or =&, but the file has to parse properly.
127 $lb->shutdown();
$endtime
$endtime
Definition: api.php:96
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
wfErrorLog
wfErrorLog( $text, $file)
Log to a file without getting "file size exceeded" signals.
Definition: GlobalFunctions.php:1168
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
ApiMain\handleApiBeforeMainException
static handleApiBeforeMainException(Exception $e)
Handle an exception from the ApiBeforeMain hook.
Definition: ApiMain.php:436
wfPHPVersionError
wfPHPVersionError( $type)
Display something vaguely comprehensible in the event of a totally unrecoverable error.
Definition: PHPVersionError.php:40
DeferredUpdates\doUpdates
static doUpdates( $commit='')
Do any deferred updates and clear the list.
Definition: DeferredUpdates.php:82
NS_MAIN
const NS_MAIN
Definition: Defines.php:79
$lb
if( $wgAPIRequestLog) $lb
Definition: api.php:126
MWException
MediaWiki exception.
Definition: MWException.php:26
$starttime
$starttime
Definition: api.php:46
$processor
$processor
Definition: api.php:69
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:933
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
wfGetLBFactory
& wfGetLBFactory()
Get the load balancer factory object.
Definition: GlobalFunctions.php:3669
$e
if( $useReadline) $e
Definition: eval.php:66
$wgTitle
if(! $wgRequest->checkUrlExtension()) if(! $wgEnableAPI) $wgTitle
Definition: api.php:63
wfLogProfilingData
wfLogProfilingData()
Definition: GlobalFunctions.php:1226
wfArrayToCgi
wfArrayToCgi( $array1, $array2=null, $prefix='')
This function takes two arrays as input, and returns a CGI-style string, e.g.
Definition: GlobalFunctions.php:367