MediaWiki master
ApiEntryPoint.php
Go to the documentation of this file.
1<?php
14namespace MediaWiki\Api;
15
16use LogicException;
23use Throwable;
24
37
38 public function __construct(
39 RequestContext $context,
41 MediaWikiServices $services
42 ) {
43 parent::__construct(
44 $context,
46 $services
47 );
48 }
49
53 protected function getContext(): RequestContext {
55 $context = parent::getContext();
56
57 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType see $context in the constructor
58 return $context;
59 }
60
69 protected function execute() {
70 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle
71 global $wgTitle;
72
73 $context = $this->getContext();
74 $request = $this->getRequest();
75
76 $services = $this->getServiceContainer();
77
78 // PATH_INFO can be used for stupid things. We don't support it for api.php at
79 // all, so error out if it's present. (T128209)
80 $pathInfo = $this->environment->getServerInfo( 'PATH_INFO', '' );
81 if ( $pathInfo != '' ) {
82 $correctUrl = wfAppendQuery(
83 wfScript( 'api' ),
84 $request->getQueryValuesOnly()
85 );
86 $correctUrl = (string)$services->getUrlUtils()->expand(
87 $correctUrl,
89 );
90 $this->header(
91 "Location: $correctUrl",
92 true,
93 301
94 );
95 $this->print(
96 'This endpoint does not support "path info", i.e. extra text ' .
97 'between "api.php" and the "?". Remove any such text and try again.'
98 );
99 $this->exit( 1 );
100 }
101
102 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
103 // In a perfect world this wouldn't be necessary
106 'Badtitle/dummy title for API calls set in api.php'
107 );
108
109 // RequestContext will read from $wgTitle, but it will also whine about it.
110 // In a perfect world this wouldn't be necessary either.
111 $context->setTitle( $wgTitle );
112
113 try {
114 // Construct an ApiMain with the arguments passed via the URL. What we get back
115 // is some form of an ApiMain, possibly even one that produces an error message,
116 // but we don't care here, as that is handled by the constructor.
117 $processor = new ApiMain(
118 $context,
119 true,
120 false
121 );
122
123 // Last chance hook before executing the API
124 ( new HookRunner( $services->getHookContainer() ) )->onApiBeforeMain( $processor );
125 if ( !$processor instanceof ApiMain ) {
126 throw new LogicException(
127 'ApiBeforeMain hook set $processor to a non-ApiMain class'
128 );
129 }
130 } catch ( Throwable $e ) {
131 // Crap. Try to report the exception in API format to be friendly to clients.
133 $processor = false;
134 }
135
136 // Process data & print results
137 if ( $processor ) {
138 $processor->execute();
139 }
140 }
141}
const PROTO_CANONICAL
Definition Defines.php:223
const NS_SPECIAL
Definition Defines.php:40
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfScript( $script='index')
Get the URL path to a MediaWiki entry point.
if(MW_ENTRY_POINT==='index') if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgTitle
Definition Setup.php:551
Implementation of the API entry point, for web browser navigations, usually via an Action or SpecialP...
execute()
Executes a request to the action API.
getContext()
Overwritten to narrow the return type to RequestContext.
__construct(RequestContext $context, EntryPointEnvironment $environment, MediaWikiServices $services)
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
static handleApiBeforeMainException(Throwable $e)
Handle a throwable from the ApiBeforeMain hook.
Definition ApiMain.php:1121
Group all the pieces relevant to the context of a request into one instance.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Utility class wrapping PHP runtime state.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Base class for entry point handlers.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:69