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