MediaWiki master
ApiEntryPoint.php
Go to the documentation of this file.
1<?php
28namespace MediaWiki\Api;
29
30use LogicException;
37use Throwable;
38
51
52 public function __construct(
53 RequestContext $context,
55 MediaWikiServices $services
56 ) {
57 parent::__construct(
58 $context,
60 $services
61 );
62 }
63
68 protected function getContext(): RequestContext {
70 $context = parent::getContext();
71
72 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType see $context in the constructor
73 return $context;
74 }
75
84 protected function execute() {
85 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgTitle
86 global $wgTitle;
87
88 $context = $this->getContext();
89 $request = $this->getRequest();
90
91 $services = $this->getServiceContainer();
92
93 // PATH_INFO can be used for stupid things. We don't support it for api.php at
94 // all, so error out if it's present. (T128209)
95 $pathInfo = $this->environment->getServerInfo( 'PATH_INFO', '' );
96 if ( $pathInfo != '' ) {
97 $correctUrl = wfAppendQuery(
98 wfScript( 'api' ),
99 $request->getQueryValuesOnly()
100 );
101 $correctUrl = (string)$services->getUrlUtils()->expand(
102 $correctUrl,
104 );
105 $this->header(
106 "Location: $correctUrl",
107 true,
108 301
109 );
110 $this->print(
111 'This endpoint does not support "path info", i.e. extra text ' .
112 'between "api.php" and the "?". Remove any such text and try again.'
113 );
114 $this->exit( 1 );
115 }
116
117 // Set a dummy $wgTitle, because $wgTitle == null breaks various things
118 // In a perfect world this wouldn't be necessary
119 $wgTitle = Title::makeTitle(
121 'Badtitle/dummy title for API calls set in api.php'
122 );
123
124 // RequestContext will read from $wgTitle, but it will also whine about it.
125 // In a perfect world this wouldn't be necessary either.
126 $context->setTitle( $wgTitle );
127
128 try {
129 // Construct an ApiMain with the arguments passed via the URL. What we get back
130 // is some form of an ApiMain, possibly even one that produces an error message,
131 // but we don't care here, as that is handled by the constructor.
132 $processor = new ApiMain(
133 $context,
134 true,
135 false
136 );
137
138 // Last chance hook before executing the API
139 ( new HookRunner( $services->getHookContainer() ) )->onApiBeforeMain( $processor );
140 if ( !$processor instanceof ApiMain ) {
141 throw new LogicException(
142 'ApiBeforeMain hook set $processor to a non-ApiMain class'
143 );
144 }
145 } catch ( Throwable $e ) {
146 // Crap. Try to report the exception in API format to be friendly to clients.
148 $processor = false;
149 }
150
151 // Process data & print results
152 if ( $processor ) {
153 $processor->execute();
154 }
155 }
156}
const PROTO_CANONICAL
Definition Defines.php:216
const NS_SPECIAL
Definition Defines.php:54
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(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgTitle
Definition Setup.php:572
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:78
static handleApiBeforeMainException(Throwable $e)
Handle a throwable from the ApiBeforeMain hook.
Definition ApiMain.php:1065
Group all the pieces relevant to the context of a request into one instance.
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:78