| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Rest; |
| 4 | |
| 5 | use DateTime; |
| 6 | use MediaWiki\Debug\MWDebug; |
| 7 | use MediaWiki\HookContainer\HookContainer; |
| 8 | use MediaWiki\HookContainer\HookRunner; |
| 9 | use MediaWiki\Permissions\Authority; |
| 10 | use MediaWiki\Rest\Module\Module; |
| 11 | use MediaWiki\Rest\Validator\BodyValidator; |
| 12 | use MediaWiki\Rest\Validator\NullBodyValidator; |
| 13 | use MediaWiki\Rest\Validator\Validator; |
| 14 | use MediaWiki\Session\Session; |
| 15 | use UtfNormal\Validator as UtfNormalValidator; |
| 16 | use Wikimedia\Assert\Assert; |
| 17 | use Wikimedia\Message\MessageValue; |
| 18 | use Wikimedia\ParamValidator\ParamValidator; |
| 19 | |
| 20 | /** |
| 21 | * Base class for REST route handlers. |
| 22 | * |
| 23 | * @stable to extend. |
| 24 | */ |
| 25 | abstract class Handler { |
| 26 | |
| 27 | /** |
| 28 | * @see Validator::KNOWN_PARAM_SOURCES |
| 29 | */ |
| 30 | public const KNOWN_PARAM_SOURCES = Validator::KNOWN_PARAM_SOURCES; |
| 31 | |
| 32 | /** |
| 33 | * @see Validator::PARAM_SOURCE |
| 34 | */ |
| 35 | public const PARAM_SOURCE = Validator::PARAM_SOURCE; |
| 36 | |
| 37 | /** |
| 38 | * @see Validator::PARAM_DESCRIPTION |
| 39 | */ |
| 40 | public const PARAM_DESCRIPTION = Validator::PARAM_DESCRIPTION; |
| 41 | |
| 42 | /** @var Module */ |
| 43 | private $module; |
| 44 | |
| 45 | /** @var RequestInterface */ |
| 46 | private $request; |
| 47 | |
| 48 | /** @var Authority */ |
| 49 | private $authority; |
| 50 | |
| 51 | /** @var string */ |
| 52 | private $path; |
| 53 | |
| 54 | /** @var array */ |
| 55 | private $config; |
| 56 | |
| 57 | /** @var ResponseFactory */ |
| 58 | private $responseFactory; |
| 59 | |
| 60 | /** @var array|null */ |
| 61 | private $validatedParams; |
| 62 | |
| 63 | /** @var mixed|null */ |
| 64 | private $validatedBody; |
| 65 | |
| 66 | /** @var ConditionalHeaderUtil */ |
| 67 | private $conditionalHeaderUtil; |
| 68 | |
| 69 | /** @var HookContainer */ |
| 70 | private $hookContainer; |
| 71 | |
| 72 | /** @var Session */ |
| 73 | private $session; |
| 74 | |
| 75 | /** @var HookRunner */ |
| 76 | private $hookRunner; |
| 77 | |
| 78 | /** |
| 79 | * Injects information about the handler's context in the Module. |
| 80 | * The framework should call this right after the object was constructed. |
| 81 | * |
| 82 | * First function of the initialization function, must be called before |
| 83 | * initServices(). |
| 84 | * |
| 85 | * @param Module $module |
| 86 | * @param string $path |
| 87 | * @param array $routeConfig information about the route declaration. |
| 88 | * |
| 89 | * @internal |
| 90 | */ |
| 91 | final public function initContext( Module $module, string $path, array $routeConfig ) { |
| 92 | Assert::precondition( |
| 93 | $this->authority === null, |
| 94 | 'initContext() must be called before initServices()' |
| 95 | ); |
| 96 | |
| 97 | $this->module = $module; |
| 98 | $this->path = $path; |
| 99 | $this->config = $routeConfig; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Inject service objects. |
| 104 | * |
| 105 | * Second function of the initialization function, must be called after |
| 106 | * initContext() and before initSession(). |
| 107 | * |
| 108 | * @param Authority $authority |
| 109 | * @param ResponseFactory $responseFactory |
| 110 | * @param HookContainer $hookContainer |
| 111 | * |
| 112 | * @internal |
| 113 | */ |
| 114 | final public function initServices( |
| 115 | Authority $authority, ResponseFactory $responseFactory, HookContainer $hookContainer |
| 116 | ) { |
| 117 | // Warn if a subclass overrides getBodyValidator() |
| 118 | MWDebug::detectDeprecatedOverride( |
| 119 | $this, |
| 120 | __CLASS__, |
| 121 | 'getBodyValidator', |
| 122 | '1.43' |
| 123 | ); |
| 124 | |
| 125 | Assert::precondition( |
| 126 | $this->module !== null, |
| 127 | 'initServices() must not be called before initContext()' |
| 128 | ); |
| 129 | Assert::precondition( |
| 130 | $this->session === null, |
| 131 | 'initServices() must be called before initSession()' |
| 132 | ); |
| 133 | |
| 134 | $this->authority = $authority; |
| 135 | $this->responseFactory = $responseFactory; |
| 136 | $this->hookContainer = $hookContainer; |
| 137 | $this->hookRunner = new HookRunner( $hookContainer ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Inject session information. |
| 142 | * |
| 143 | * Third function of the initialization function, must be called after |
| 144 | * initServices() and before initForExecute(). |
| 145 | * |
| 146 | * @param Session $session |
| 147 | * |
| 148 | * @internal |
| 149 | */ |
| 150 | final public function initSession( Session $session ) { |
| 151 | Assert::precondition( |
| 152 | $this->authority !== null, |
| 153 | 'initSession() must not be called before initContext()' |
| 154 | ); |
| 155 | Assert::precondition( |
| 156 | $this->request === null, |
| 157 | 'initSession() must be called before initForExecute()' |
| 158 | ); |
| 159 | |
| 160 | $this->session = $session; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Initialise for execution based on the given request. |
| 165 | * |
| 166 | * Last function of the initialization function, must be called after |
| 167 | * initSession() and before validate() and checkPreconditions(). |
| 168 | * |
| 169 | * This function will call postInitSetup() to allow subclasses to |
| 170 | * perform their own initialization. |
| 171 | * |
| 172 | * The request object is updated with parsed body data if needed. |
| 173 | * |
| 174 | * @internal |
| 175 | * |
| 176 | * @param RequestInterface $request |
| 177 | * |
| 178 | * @throws HttpException if the handler does not accept the request for |
| 179 | * some reason. |
| 180 | */ |
| 181 | final public function initForExecute( RequestInterface $request ) { |
| 182 | Assert::precondition( |
| 183 | $this->session !== null, |
| 184 | 'initForExecute() must not be called before initSession()' |
| 185 | ); |
| 186 | |
| 187 | if ( $request->getParsedBody() === null ) { |
| 188 | $this->processRequestBody( $request ); |
| 189 | } |
| 190 | |
| 191 | $this->request = $request; |
| 192 | |
| 193 | $this->postInitSetup(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Process the request's request body and set the parsed body data |
| 198 | * if appropriate. |
| 199 | * |
| 200 | * @see parseBodyData() |
| 201 | * |
| 202 | * @throws HttpException if the request body is not acceptable. |
| 203 | */ |
| 204 | private function processRequestBody( RequestInterface $request ) { |
| 205 | // fail if the request method is in NO_BODY_METHODS but has body |
| 206 | $requestMethod = $request->getMethod(); |
| 207 | if ( in_array( $requestMethod, RequestInterface::NO_BODY_METHODS ) ) { |
| 208 | // check if the request has a body |
| 209 | if ( $request->hasBody() ) { |
| 210 | // NOTE: Don't throw, see T359509. |
| 211 | // TODO: Ignore only empty bodies, log a warning or fail if |
| 212 | // there is actual content. |
| 213 | return; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // fail if the request method expects a body but has no body |
| 218 | if ( in_array( $requestMethod, RequestInterface::BODY_METHODS ) ) { |
| 219 | // check if it has no body |
| 220 | if ( !$request->hasBody() ) { |
| 221 | throw new LocalizedHttpException( |
| 222 | new MessageValue( |
| 223 | "rest-request-body-expected", |
| 224 | [ $requestMethod ] |
| 225 | ), |
| 226 | 411 |
| 227 | ); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // call parsedbody |
| 232 | if ( $request->hasBody() ) { |
| 233 | $parsedBody = $this->parseBodyData( $request ); |
| 234 | // Set the parsed body data on the request object |
| 235 | $request->setParsedBody( $parsedBody ); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Returns the path this handler is bound to relative to the module prefix. |
| 241 | * Includes path variables. |
| 242 | * |
| 243 | * @return string |
| 244 | */ |
| 245 | public function getPath(): string { |
| 246 | return $this->path; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Get a list of parameter placeholders present in the route's path |
| 251 | * as returned by getPath(). Note that this is independent of the parameters |
| 252 | * defined by getParamSettings(): required path parameters defined in |
| 253 | * getParamSettings() should be present in the path, but there is no |
| 254 | * mechanism to ensure that they are. |
| 255 | * |
| 256 | * @return string[] |
| 257 | */ |
| 258 | public function getSupportedPathParams(): array { |
| 259 | $path = $this->getPath(); |
| 260 | |
| 261 | preg_match_all( '/\{(.*?)\}/', $path, $matches, PREG_PATTERN_ORDER ); |
| 262 | |
| 263 | return $matches[1] ?? []; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Get the Router. |
| 268 | * |
| 269 | * @return Router |
| 270 | */ |
| 271 | protected function getRouter(): Router { |
| 272 | return $this->module->getRouter(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Get the Module this handler belongs to. |
| 277 | * Will fail hard if called before initContext(). |
| 278 | * |
| 279 | * @return Module |
| 280 | */ |
| 281 | protected function getModule(): Module { |
| 282 | return $this->module; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Get the URL of this handler's endpoint. |
| 287 | * Supports the substitution of path parameters, and additions of query parameters. |
| 288 | * |
| 289 | * @see Router::getRouteUrl() |
| 290 | * |
| 291 | * @param string[] $pathParams Path parameters to be injected into the path |
| 292 | * @param string[] $queryParams Query parameters to be attached to the URL |
| 293 | * |
| 294 | * @return string |
| 295 | */ |
| 296 | protected function getRouteUrl( $pathParams = [], $queryParams = [] ): string { |
| 297 | $path = $this->getPath(); |
| 298 | return $this->getRouter()->getRouteUrl( $path, $pathParams, $queryParams ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * URL-encode titles in a "pretty" way. |
| 303 | * |
| 304 | * Keeps intact ;@$!*(),~: (urlencode does not, but wfUrlencode does). |
| 305 | * Encodes spaces as underscores (wfUrlencode does not). |
| 306 | * Encodes slashes (wfUrlencode does not, but keeping them messes with REST paths). |
| 307 | * Encodes pluses (this is not necessary, and may change). |
| 308 | * |
| 309 | * @see wfUrlencode |
| 310 | * |
| 311 | * @param string $title |
| 312 | * |
| 313 | * @return string |
| 314 | */ |
| 315 | protected function urlEncodeTitle( $title ) { |
| 316 | $title = str_replace( ' ', '_', $title ); |
| 317 | $title = urlencode( $title ); |
| 318 | |
| 319 | // %3B_a_%40_b_%24_c_%21_d_%2A_e_%28_f_%29_g_%2C_h_~_i_%3A |
| 320 | $replace = [ '%3B', '%40', '%24', '%21', '%2A', '%28', '%29', '%2C', '%7E', '%3A' ]; |
| 321 | $with = [ ';', '@', '$', '!', '*', '(', ')', ',', '~', ':' ]; |
| 322 | |
| 323 | return str_replace( $replace, $with, $title ); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Get the current request. The return type declaration causes it to raise |
| 328 | * a fatal error if initForExecute() has not yet been called. |
| 329 | * |
| 330 | * @return RequestInterface |
| 331 | */ |
| 332 | public function getRequest(): RequestInterface { |
| 333 | return $this->request; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Get the current acting authority. The return type declaration causes it to raise |
| 338 | * a fatal error if initServices() has not yet been called. |
| 339 | * |
| 340 | * @since 1.36 |
| 341 | * @return Authority |
| 342 | */ |
| 343 | public function getAuthority(): Authority { |
| 344 | return $this->authority; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Get the configuration array for the current route. The return type |
| 349 | * declaration causes it to raise a fatal error if initContext() has not |
| 350 | * been called. |
| 351 | * |
| 352 | * @return array |
| 353 | */ |
| 354 | public function getConfig(): array { |
| 355 | return $this->config; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Get the ResponseFactory which can be used to generate Response objects. |
| 360 | * This will raise a fatal error if initServices() has not been |
| 361 | * called. |
| 362 | * |
| 363 | * @return ResponseFactory |
| 364 | */ |
| 365 | public function getResponseFactory(): ResponseFactory { |
| 366 | return $this->responseFactory; |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Get the Session. |
| 371 | * This will raise a fatal error if initSession() has not been |
| 372 | * called. |
| 373 | * |
| 374 | * @return Session |
| 375 | */ |
| 376 | public function getSession(): Session { |
| 377 | return $this->session; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Validate the request parameters/attributes and body. If there is a validation |
| 382 | * failure, a response with an error message should be returned or an |
| 383 | * HttpException should be thrown. |
| 384 | * |
| 385 | * @stable to override |
| 386 | * @param Validator $restValidator |
| 387 | * @throws HttpException On validation failure. |
| 388 | */ |
| 389 | public function validate( Validator $restValidator ) { |
| 390 | $this->validatedParams = $restValidator->validateParams( |
| 391 | $this->getParamSettings() |
| 392 | ); |
| 393 | |
| 394 | $bodyType = $this->request->getBodyType(); |
| 395 | $legacyBodyValidator = $bodyType === null ? null |
| 396 | : $this->getBodyValidator( $bodyType ); |
| 397 | |
| 398 | if ( $legacyBodyValidator && !$legacyBodyValidator instanceof NullBodyValidator ) { |
| 399 | $this->validatedBody = $restValidator->validateBody( $this->request, $this ); |
| 400 | } else { |
| 401 | // Allow type coercion if the request body is form data. |
| 402 | // For JSON requests, insist on proper types. |
| 403 | $enforceTypes = !in_array( |
| 404 | $this->request->getBodyType(), |
| 405 | RequestInterface::FORM_DATA_CONTENT_TYPES |
| 406 | ); |
| 407 | |
| 408 | $this->validatedBody = $restValidator->validateBodyParams( |
| 409 | $this->getBodyParamSettings(), |
| 410 | $enforceTypes |
| 411 | ); |
| 412 | |
| 413 | // If there is a body, check if it contains extra fields. |
| 414 | if ( $this->getRequest()->hasBody() ) { |
| 415 | $this->detectExtraneousBodyFields( $restValidator ); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | $this->postValidationSetup(); |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Subclasses may override this to disable or modify checks for extraneous |
| 424 | * body fields. |
| 425 | * |
| 426 | * @since 1.42 |
| 427 | * @stable to override |
| 428 | * @param Validator $restValidator |
| 429 | * @throws HttpException On validation failure. |
| 430 | */ |
| 431 | protected function detectExtraneousBodyFields( Validator $restValidator ) { |
| 432 | $parsedBody = $this->getRequest()->getParsedBody(); |
| 433 | |
| 434 | if ( !$parsedBody ) { |
| 435 | // nothing to do |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | $restValidator->detectExtraneousBodyFields( |
| 440 | $this->getBodyParamSettings(), |
| 441 | $parsedBody |
| 442 | ); |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Check the session (and session provider) |
| 447 | * @throws HttpException on failed check |
| 448 | * @internal |
| 449 | */ |
| 450 | public function checkSession() { |
| 451 | if ( !$this->session->getProvider()->safeAgainstCsrf() ) { |
| 452 | if ( $this->requireSafeAgainstCsrf() ) { |
| 453 | throw new LocalizedHttpException( |
| 454 | new MessageValue( 'rest-requires-safe-against-csrf' ), |
| 455 | 400 |
| 456 | ); |
| 457 | } |
| 458 | } elseif ( !empty( $this->validatedBody['token'] ) ) { |
| 459 | throw new LocalizedHttpException( |
| 460 | new MessageValue( 'rest-extraneous-csrf-token' ), |
| 461 | 400 |
| 462 | ); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Get a ConditionalHeaderUtil object. |
| 468 | * |
| 469 | * On the first call to this method, the object will be initialized with |
| 470 | * validator values by calling getETag(), getLastModified() and |
| 471 | * hasRepresentation(). |
| 472 | * |
| 473 | * @return ConditionalHeaderUtil |
| 474 | */ |
| 475 | protected function getConditionalHeaderUtil() { |
| 476 | if ( $this->conditionalHeaderUtil === null ) { |
| 477 | $this->conditionalHeaderUtil = new ConditionalHeaderUtil; |
| 478 | $this->conditionalHeaderUtil->setValidators( |
| 479 | $this->getETag(), |
| 480 | $this->getLastModified(), |
| 481 | $this->hasRepresentation() |
| 482 | ); |
| 483 | } |
| 484 | return $this->conditionalHeaderUtil; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Check the conditional request headers and generate a response if appropriate. |
| 489 | * This is called by the Router before execute() and may be overridden. |
| 490 | * |
| 491 | * @stable to override |
| 492 | * |
| 493 | * @return ResponseInterface|null |
| 494 | */ |
| 495 | public function checkPreconditions() { |
| 496 | $status = $this->getConditionalHeaderUtil()->checkPreconditions( $this->getRequest() ); |
| 497 | if ( $status ) { |
| 498 | $response = $this->getResponseFactory()->create(); |
| 499 | $response->setStatus( $status ); |
| 500 | return $response; |
| 501 | } |
| 502 | |
| 503 | return null; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Apply verifier headers to the response, per RFC 7231 §7.2. |
| 508 | * This is called after execute() returns. |
| 509 | * |
| 510 | * For GET and HEAD requests, the default behavior is to set the ETag and |
| 511 | * Last-Modified headers based on the values returned by getETag() and |
| 512 | * getLastModified() when they were called before execute() was run. |
| 513 | * |
| 514 | * Other request methods are assumed to be state-changing, so no headers |
| 515 | * will be set by default. |
| 516 | * |
| 517 | * This may be overridden to modify the verifier headers sent in the response. |
| 518 | * However, handlers that modify the resource's state would typically just |
| 519 | * set the ETag and Last-Modified headers in the execute() method. |
| 520 | * |
| 521 | * @stable to override |
| 522 | * |
| 523 | * @param ResponseInterface $response |
| 524 | */ |
| 525 | public function applyConditionalResponseHeaders( ResponseInterface $response ) { |
| 526 | $method = $this->getRequest()->getMethod(); |
| 527 | if ( $method === 'GET' || $method === 'HEAD' ) { |
| 528 | $this->getConditionalHeaderUtil()->applyResponseHeaders( $response ); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Apply cache control to enforce privacy. |
| 534 | * |
| 535 | * @param ResponseInterface $response |
| 536 | */ |
| 537 | public function applyCacheControl( ResponseInterface $response ) { |
| 538 | // NOTE: keep this consistent with the logic in OutputPage::sendCacheControl |
| 539 | |
| 540 | // If the response sets cookies, it must not be cached in proxies. |
| 541 | // If there's an active cookie-based session (logged-in user or anonymous user with |
| 542 | // session-scoped cookies), it is not safe to cache either, as the session manager may set |
| 543 | // cookies in the response, or the response itself may vary on user-specific variables, |
| 544 | // for example on private wikis where the 'read' permission is restricted. (T264631) |
| 545 | if ( $response->getHeaderLine( 'Set-Cookie' ) || $this->getSession()->isPersistent() ) { |
| 546 | $response->setHeader( 'Cache-Control', 'private,must-revalidate,s-maxage=0' ); |
| 547 | } |
| 548 | |
| 549 | if ( !$response->getHeaderLine( 'Cache-Control' ) ) { |
| 550 | $rqMethod = $this->getRequest()->getMethod(); |
| 551 | if ( $rqMethod !== 'GET' && $rqMethod !== 'HEAD' ) { |
| 552 | // Responses to requests other than GET or HEAD should not be cacheable by default. |
| 553 | $response->setHeader( 'Cache-Control', 'private,no-cache,s-maxage=0' ); |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Fetch ParamValidator settings for parameters |
| 560 | * |
| 561 | * Every setting must include self::PARAM_SOURCE to specify which part of |
| 562 | * the request is to contain the parameter. |
| 563 | * |
| 564 | * Can be used for the request body as well, by setting self::PARAM_SOURCE |
| 565 | * to "post". Note that the values of "post" parameters will be accessible |
| 566 | * through getValidatedParams(). "post" parameters are used with |
| 567 | * form data (application/x-www-form-urlencoded or multipart/form-data). |
| 568 | * |
| 569 | * For "query" parameters, a PARAM_REQUIRED setting of "false" means the caller |
| 570 | * does not have to supply the parameter. For "path" parameters, the path matcher will always |
| 571 | * require the caller to supply all path parameters for a route, regardless of the |
| 572 | * PARAM_REQUIRED setting. However, "path" parameters may be specified in getParamSettings() |
| 573 | * as non-required to indicate that the handler services multiple routes, some of which may |
| 574 | * not supply the parameter. |
| 575 | * |
| 576 | * @stable to override |
| 577 | * |
| 578 | * @return array[] Associative array mapping parameter names to |
| 579 | * ParamValidator settings arrays |
| 580 | */ |
| 581 | public function getParamSettings() { |
| 582 | return []; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Fetch ParamValidator settings for body fields. Parameters defined |
| 587 | * by this method are used to validate the request body. The parameter |
| 588 | * values will become available through getValidatedBody(). |
| 589 | * |
| 590 | * Subclasses may override this method to specify what fields they support |
| 591 | * in the request body. All parameter settings returned by this method must |
| 592 | * have self::PARAM_SOURCE set to 'body'. |
| 593 | * |
| 594 | * @return array[] |
| 595 | */ |
| 596 | public function getBodyParamSettings(): array { |
| 597 | return []; |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Returns an OpenAPI Operation Object specification structure as an associative array. |
| 602 | * |
| 603 | * @see https://swagger.io/specification/#operation-object |
| 604 | * |
| 605 | * By default, this will contain information about the supported parameters, as well as |
| 606 | * the response for status 200. |
| 607 | * |
| 608 | * Subclasses may override this to provide additional information. |
| 609 | * |
| 610 | * @since 1.42 |
| 611 | * @stable to override |
| 612 | * |
| 613 | * @param string $method The HTTP method to produce a spec for ("get", "post", etc). |
| 614 | * Useful for handlers that behave differently depending on the |
| 615 | * request method. |
| 616 | * |
| 617 | * @return array |
| 618 | */ |
| 619 | public function getOpenApiSpec( string $method ): array { |
| 620 | $parameters = []; |
| 621 | |
| 622 | $supportedPathParams = array_flip( $this->getSupportedPathParams() ); |
| 623 | |
| 624 | foreach ( $this->getParamSettings() as $name => $paramSetting ) { |
| 625 | $source = $paramSetting[ Validator::PARAM_SOURCE ] ?? ''; |
| 626 | |
| 627 | if ( $source !== 'query' && $source !== 'path' ) { |
| 628 | continue; |
| 629 | } |
| 630 | |
| 631 | if ( $source === 'path' && !isset( $supportedPathParams[$name] ) ) { |
| 632 | // Skip optional path param not used in the current path |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | if ( array_key_exists( Validator::PARAM_DESCRIPTION, $paramSetting ) && |
| 637 | $paramSetting[ Validator::PARAM_DESCRIPTION ] instanceof MessageValue |
| 638 | ) { |
| 639 | // TODO: consider if we want to request a specific preferred language |
| 640 | $translation = $this->responseFactory->getFormattedMessage( |
| 641 | $paramSetting[ Validator::PARAM_DESCRIPTION ] |
| 642 | ); |
| 643 | $paramSetting[ Validator::PARAM_DESCRIPTION ] = $translation; |
| 644 | } |
| 645 | |
| 646 | $param = Validator::getParameterSpec( |
| 647 | $name, |
| 648 | $paramSetting |
| 649 | ); |
| 650 | |
| 651 | $parameters[] = $param; |
| 652 | } |
| 653 | |
| 654 | $spec = [ |
| 655 | 'parameters' => $parameters, |
| 656 | 'responses' => $this->generateResponseSpec( $method ), |
| 657 | ]; |
| 658 | |
| 659 | if ( !in_array( $method, RequestInterface::NO_BODY_METHODS ) ) { |
| 660 | $requestBody = $this->getRequestSpec( $method ); |
| 661 | if ( $requestBody ) { |
| 662 | $spec['requestBody'] = $requestBody; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // TODO: Allow additional information about parameters and responses to |
| 667 | // be provided in the route definition. |
| 668 | $oas = $this->getConfig()['OAS'] ?? []; |
| 669 | $spec += $oas; |
| 670 | |
| 671 | return $spec; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * Returns an OpenAPI Request Body Object specification structure as an associative array. |
| 676 | * |
| 677 | * @see https://swagger.io/specification/#request-body-object |
| 678 | * |
| 679 | * This is based on the getBodyParamSettings() and getSupportedRequestTypes(). |
| 680 | * |
| 681 | * Subclasses may override this to provide additional information about the |
| 682 | * structure of responses, or to add support for additional mediaTypes. |
| 683 | * |
| 684 | * @stable to override getBodySchema() to generate a schema for each |
| 685 | * supported media type as returned by getSupportedBodyTypes(). |
| 686 | * |
| 687 | * @param string $method |
| 688 | * |
| 689 | * @return ?array |
| 690 | */ |
| 691 | protected function getRequestSpec( string $method ): ?array { |
| 692 | $mediaTypes = []; |
| 693 | |
| 694 | foreach ( $this->getSupportedRequestTypes() as $type ) { |
| 695 | $schema = $this->getRequestBodySchema( $type ); |
| 696 | |
| 697 | if ( $schema ) { |
| 698 | $mediaTypes[$type] = [ 'schema' => $schema ]; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if ( !$mediaTypes ) { |
| 703 | return null; |
| 704 | } |
| 705 | |
| 706 | return [ |
| 707 | // TODO: some DELETE handlers may require a body that contains a token |
| 708 | // FIXME: check if there are required body params! |
| 709 | 'required' => in_array( $method, RequestInterface::BODY_METHODS ), |
| 710 | 'content' => $mediaTypes |
| 711 | ]; |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Returns a content schema per the OpenAPI spec. |
| 716 | * @see https://swagger.io/specification/#schema-object |
| 717 | * |
| 718 | * Per default, this provides schemas for JSON requests and form data, based |
| 719 | * on the parameter declarations returned by getParamSettings(). |
| 720 | * |
| 721 | * Subclasses may override this to provide additional information about the |
| 722 | * structure of responses, or to add support for additional mediaTypes. |
| 723 | * |
| 724 | * @stable to override |
| 725 | * @return array |
| 726 | */ |
| 727 | protected function getRequestBodySchema( string $mediaType ): array { |
| 728 | if ( $mediaType === RequestInterface::FORM_URLENCODED_CONTENT_TYPE ) { |
| 729 | $allowedSources = [ 'body', 'post' ]; |
| 730 | } elseif ( $mediaType === RequestInterface::MULTIPART_FORM_DATA_CONTENT_TYPE ) { |
| 731 | $allowedSources = [ 'body', 'post' ]; |
| 732 | } else { |
| 733 | $allowedSources = [ 'body' ]; |
| 734 | } |
| 735 | |
| 736 | $paramSettings = $this->getBodyParamSettings(); |
| 737 | |
| 738 | $properties = []; |
| 739 | $required = []; |
| 740 | |
| 741 | foreach ( $paramSettings as $name => $settings ) { |
| 742 | $source = $settings[ Validator::PARAM_SOURCE ] ?? ''; |
| 743 | $isRequired = $settings[ ParamValidator::PARAM_REQUIRED ] ?? false; |
| 744 | |
| 745 | if ( !in_array( $source, $allowedSources ) ) { |
| 746 | // TODO: post parameters also work as body parameters... |
| 747 | continue; |
| 748 | } |
| 749 | |
| 750 | $properties[$name] = Validator::getParameterSchema( $settings ); |
| 751 | |
| 752 | if ( $isRequired ) { |
| 753 | $required[] = $name; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if ( !$properties ) { |
| 758 | return []; |
| 759 | } |
| 760 | |
| 761 | $schema = [ |
| 762 | 'type' => 'object', |
| 763 | 'properties' => $properties, |
| 764 | ]; |
| 765 | |
| 766 | if ( $required ) { |
| 767 | $schema['required'] = $required; |
| 768 | } |
| 769 | |
| 770 | return $schema; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Returns an OpenAPI Schema Object specification structure as an associative array. |
| 775 | * |
| 776 | * @see https://swagger.io/specification/#schema-object |
| 777 | * |
| 778 | * Returns null by default. Subclasses that return a JSON response should |
| 779 | * implement this method to return a schema of the response body. |
| 780 | * |
| 781 | * @param string $method The HTTP method to produce a spec for ("get", "post", etc). |
| 782 | * |
| 783 | * @stable to override |
| 784 | * @return ?array |
| 785 | */ |
| 786 | protected function getResponseBodySchema( string $method ): ?array { |
| 787 | $file = $this->getResponseBodySchemaFileName( $method ); |
| 788 | return $file ? Module::loadJsonFile( $file ) : null; |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Returns the path and name of a JSON file containing an OpenAPI Schema Object |
| 793 | * specification structure. |
| 794 | * |
| 795 | * @see https://swagger.io/specification/#schema-object |
| 796 | * |
| 797 | * Returns null by default. Subclasses with a suitable JSON file should implement this method. |
| 798 | * |
| 799 | * @param string $method The HTTP method to produce a spec for ("get", "post", etc). |
| 800 | * |
| 801 | * @stable to override |
| 802 | * @since 1.43 |
| 803 | * @return ?string |
| 804 | */ |
| 805 | protected function getResponseBodySchemaFileName( string $method ): ?string { |
| 806 | return null; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Returns an OpenAPI Responses Object specification structure as an associative array. |
| 811 | * |
| 812 | * @see https://swagger.io/specification/#responses-object |
| 813 | * |
| 814 | * By default, this will contain basic information response for status 200, 400, and 500. |
| 815 | * The getResponseBodySchema() method is used to determine the structure of the response for status 200. |
| 816 | * |
| 817 | * Subclasses may override this to provide additional information about the structure of responses. |
| 818 | * |
| 819 | * @param string $method The HTTP method to produce a spec for ("get", "post", etc). |
| 820 | * |
| 821 | * @stable to override |
| 822 | * @return array |
| 823 | */ |
| 824 | protected function generateResponseSpec( string $method ): array { |
| 825 | $ok = [ 'description' => 'OK' ]; |
| 826 | |
| 827 | $bodySchema = $this->getResponseBodySchema( $method ); |
| 828 | |
| 829 | if ( $bodySchema ) { |
| 830 | $ok['content']['application/json']['schema'] = $bodySchema; |
| 831 | } |
| 832 | |
| 833 | // XXX: we should add info about redirects, and maybe a default for errors? |
| 834 | return [ |
| 835 | '200' => $ok, |
| 836 | '400' => [ '$ref' => '#/components/responses/GenericErrorResponse' ], |
| 837 | '500' => [ '$ref' => '#/components/responses/GenericErrorResponse' ], |
| 838 | ]; |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * Fetch the BodyValidator |
| 843 | * |
| 844 | * @deprecated since 1.43, return body properties from getBodyParamSettings(). |
| 845 | * Subclasses that need full control over body data parsing should override |
| 846 | * parseBodyData() or implement validation in the execute() method based on |
| 847 | * the unparsed body data returned by getRequest()->getBody(). |
| 848 | * |
| 849 | * @param string $contentType Content type of the request. |
| 850 | * @return BodyValidator A {@see NullBodyValidator} in this default implementation |
| 851 | * @throws HttpException It's possible to fail early here when e.g. $contentType is unsupported, |
| 852 | * or later when {@see BodyValidator::validateBody} is called |
| 853 | */ |
| 854 | public function getBodyValidator( $contentType ) { |
| 855 | // NOTE: When removing this method, also remove the BodyValidator interface and |
| 856 | // all classes implementing it! |
| 857 | return new NullBodyValidator(); |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Fetch the validated parameters. This must be called after validate() is |
| 862 | * called. During execute() is fine. |
| 863 | * |
| 864 | * @return array Array mapping parameter names to validated values |
| 865 | * @throws \RuntimeException If validate() has not been called |
| 866 | */ |
| 867 | public function getValidatedParams() { |
| 868 | if ( $this->validatedParams === null ) { |
| 869 | throw new \RuntimeException( 'getValidatedParams() called before validate()' ); |
| 870 | } |
| 871 | return $this->validatedParams; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Fetch the validated body |
| 876 | * @return mixed|null Value returned by the body validator, or null if validate() was |
| 877 | * not called yet, validation failed, there was no body, or the body was form data. |
| 878 | */ |
| 879 | public function getValidatedBody() { |
| 880 | return $this->validatedBody; |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * Returns the parsed body of the request. |
| 885 | * Should only be called if $request->hasBody() returns true. |
| 886 | * |
| 887 | * The default implementation handles application/x-www-form-urlencoded |
| 888 | * and multipart/form-data by calling $request->getPostParams(), |
| 889 | * if the list returned by getSupportedRequestTypes() includes these types. |
| 890 | * |
| 891 | * The default implementation handles application/json by parsing |
| 892 | * the body content as JSON. Only object structures (maps) are supported, |
| 893 | * other types will trigger an HttpException with status 400. |
| 894 | * |
| 895 | * Other content types will trigger a HttpException with status 415 per |
| 896 | * default. |
| 897 | * |
| 898 | * Subclasses may override this method to support parsing additional |
| 899 | * content types or to disallow content types by throwing an HttpException |
| 900 | * with status 415. Subclasses may also return null to indicate that they |
| 901 | * support reading the content, but intend to handle it as an unparsed |
| 902 | * stream in their implementation of the execute() method. |
| 903 | * |
| 904 | * Subclasses that override this method to support additional request types |
| 905 | * should also override getSupportedRequestTypes() to allow that support |
| 906 | * to be documented in the OpenAPI spec. |
| 907 | * |
| 908 | * @since 1.42 |
| 909 | * |
| 910 | * @throws HttpException If the content type is not supported or the content |
| 911 | * is malformed. |
| 912 | * |
| 913 | * @return array|null The body content represented as an associative array, |
| 914 | * or null if the request body is accepted unparsed. |
| 915 | */ |
| 916 | public function parseBodyData( RequestInterface $request ): ?array { |
| 917 | // Parse the body based on its content type |
| 918 | $contentType = $request->getBodyType(); |
| 919 | |
| 920 | // HACK: If the Handler uses a custom BodyValidator, the |
| 921 | // getBodyValidator() is also responsible for checking whether |
| 922 | // the content type is valid, and for parsing the body. |
| 923 | // See T359149. |
| 924 | // TODO: remove once no subclasses override getBodyValidator() anymore |
| 925 | $bodyValidator = $this->getBodyValidator( $contentType ?? 'unknown/unknown' ); |
| 926 | if ( !$bodyValidator instanceof NullBodyValidator ) { |
| 927 | // TODO: Trigger a deprecation warning. |
| 928 | return null; |
| 929 | } |
| 930 | |
| 931 | $supportedTypes = $this->getSupportedRequestTypes(); |
| 932 | if ( $contentType !== null && !in_array( $contentType, $supportedTypes ) ) { |
| 933 | throw new LocalizedHttpException( |
| 934 | new MessageValue( 'rest-unsupported-content-type', [ $contentType ] ), |
| 935 | 415 |
| 936 | ); |
| 937 | } |
| 938 | |
| 939 | // if it's supported and ends with "+json", we can probably parse it like a normal application/json request |
| 940 | $contentType = str_ends_with( $contentType ?? '', '+json' ) |
| 941 | ? RequestInterface::JSON_CONTENT_TYPE |
| 942 | : $contentType; |
| 943 | |
| 944 | switch ( $contentType ) { |
| 945 | case RequestInterface::FORM_URLENCODED_CONTENT_TYPE: |
| 946 | case RequestInterface::MULTIPART_FORM_DATA_CONTENT_TYPE: |
| 947 | $params = $request->getPostParams(); |
| 948 | foreach ( $params as $key => $value ) { |
| 949 | $params[ $key ] = UtfNormalValidator::cleanUp( $value ); |
| 950 | // TODO: Warn if normalization was applied |
| 951 | } |
| 952 | return $params; |
| 953 | case RequestInterface::JSON_CONTENT_TYPE: |
| 954 | $jsonStream = $request->getBody(); |
| 955 | $jsonString = (string)$jsonStream; |
| 956 | $normalizedJsonString = UtfNormalValidator::cleanUp( $jsonString ); |
| 957 | $parsedBody = json_decode( $normalizedJsonString, true ); |
| 958 | if ( !is_array( $parsedBody ) ) { |
| 959 | throw new LocalizedHttpException( |
| 960 | new MessageValue( |
| 961 | 'rest-json-body-parse-error', |
| 962 | [ 'not a valid JSON object' ] |
| 963 | ), |
| 964 | 400 |
| 965 | ); |
| 966 | } |
| 967 | // TODO: Warn if normalization was applied |
| 968 | return $parsedBody; |
| 969 | case null: |
| 970 | // Specifying no Content-Type is fine if the body is empty |
| 971 | if ( $request->getBody()->getSize() === 0 ) { |
| 972 | return null; |
| 973 | } |
| 974 | // no break, else fall through to the error below. |
| 975 | default: |
| 976 | throw new LocalizedHttpException( |
| 977 | new MessageValue( 'rest-unsupported-content-type', [ $contentType ?? '(null)' ] ), |
| 978 | 415 |
| 979 | ); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | /** |
| 984 | * Returns the content types that should be accepted by parseBodyData(). |
| 985 | * |
| 986 | * Subclasses that support request types other than application/json |
| 987 | * should override this method. |
| 988 | * |
| 989 | * If "application/x-www-form-urlencoded" or "multipart/form-data" are |
| 990 | * returned, parseBodyData() will use $request->getPostParams() to determine |
| 991 | * the body data. |
| 992 | * |
| 993 | * @note The return value of this method is ignored for requests |
| 994 | * using a method listed in Validator::NO_BODY_METHODS, |
| 995 | * in particular for the GET method. |
| 996 | * |
| 997 | * @note for backwards compatibility, the default implementation of this |
| 998 | * method will examine the parameter definitions returned by getParamSettings() |
| 999 | * to see if any of the parameters are declared as "post" parameters. If this |
| 1000 | * is the case, support for "application/x-www-form-urlencoded" and |
| 1001 | * "multipart/form-data" is added. This may change in future releases. |
| 1002 | * It is preferred to use "body" parameters and override this method explicitly |
| 1003 | * when support for form data is desired. |
| 1004 | * |
| 1005 | * @stable to override |
| 1006 | * |
| 1007 | * @return string[] A list of content-types |
| 1008 | */ |
| 1009 | public function getSupportedRequestTypes(): array { |
| 1010 | $types = [ |
| 1011 | RequestInterface::JSON_CONTENT_TYPE |
| 1012 | ]; |
| 1013 | |
| 1014 | // TODO: remove this once "post" parameters are no longer supported! T362850 |
| 1015 | foreach ( $this->getParamSettings() as $settings ) { |
| 1016 | if ( ( $settings[self::PARAM_SOURCE] ?? null ) === 'post' ) { |
| 1017 | $types[] = RequestInterface::FORM_URLENCODED_CONTENT_TYPE; |
| 1018 | $types[] = RequestInterface::MULTIPART_FORM_DATA_CONTENT_TYPE; |
| 1019 | break; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | return $types; |
| 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * Get a HookContainer, for running extension hooks or for hook metadata. |
| 1028 | * |
| 1029 | * @since 1.35 |
| 1030 | * @return HookContainer |
| 1031 | */ |
| 1032 | protected function getHookContainer() { |
| 1033 | return $this->hookContainer; |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * Get a HookRunner for running core hooks. |
| 1038 | * |
| 1039 | * @internal This is for use by core only. Hook interfaces may be removed |
| 1040 | * without notice. |
| 1041 | * @since 1.35 |
| 1042 | * @return HookRunner |
| 1043 | */ |
| 1044 | protected function getHookRunner() { |
| 1045 | return $this->hookRunner; |
| 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * The subclass should override this to provide the maximum last modified |
| 1050 | * timestamp of the requested resource. This is called before execute() in |
| 1051 | * order to decide whether to send a 304. If the request is going to |
| 1052 | * change the state of the resource, the time returned must represent |
| 1053 | * the last modification date before the change. In other words, it must |
| 1054 | * provide the timestamp of the entity that the change is going to be |
| 1055 | * applied to. |
| 1056 | * |
| 1057 | * For GET and HEAD requests, this value will automatically be included |
| 1058 | * in the response in the Last-Modified header. |
| 1059 | * |
| 1060 | * Handlers that modify the resource and want to return a Last-Modified |
| 1061 | * header representing the new state in the response should set the header |
| 1062 | * in the execute() method. |
| 1063 | * |
| 1064 | * See RFC 7231 §7.2 and RFC 7232 §2.3 for semantics. |
| 1065 | * |
| 1066 | * @stable to override |
| 1067 | * |
| 1068 | * @return string|int|float|DateTime|null |
| 1069 | */ |
| 1070 | protected function getLastModified() { |
| 1071 | return null; |
| 1072 | } |
| 1073 | |
| 1074 | /** |
| 1075 | * The subclass should override this to provide an ETag for the current |
| 1076 | * state of the requested resource. This is called before execute() in |
| 1077 | * order to decide whether to send a 304. If the request is going to |
| 1078 | * change the state of the resource, the ETag returned must represent |
| 1079 | * the state before the change. In other words, it must identify |
| 1080 | * the entity that the change is going to be applied to. |
| 1081 | * |
| 1082 | * For GET and HEAD requests, this ETag will also be included in the |
| 1083 | * response. |
| 1084 | * |
| 1085 | * Handlers that modify the resource and want to return an ETag |
| 1086 | * header representing the new state in the response should set the header |
| 1087 | * in the execute() method. However, note that responses to PUT requests |
| 1088 | * must not return an ETag unless the new content of the resource is exactly |
| 1089 | * the data that was sent by the client in the request body. |
| 1090 | * |
| 1091 | * This must be a complete ETag, including double quotes. |
| 1092 | * See RFC 7231 §7.2 and RFC 7232 §2.3 for semantics. |
| 1093 | * |
| 1094 | * @stable to override |
| 1095 | * |
| 1096 | * @return string|null |
| 1097 | */ |
| 1098 | protected function getETag() { |
| 1099 | return null; |
| 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * The subclass should override this to indicate whether the resource |
| 1104 | * exists. This is used for wildcard validators, for example "If-Match: *" |
| 1105 | * fails if the resource does not exist. |
| 1106 | * |
| 1107 | * In a state-changing request, the return value of this method should |
| 1108 | * reflect the state before the requested change is applied. |
| 1109 | * |
| 1110 | * @stable to override |
| 1111 | * |
| 1112 | * @return bool|null |
| 1113 | */ |
| 1114 | protected function hasRepresentation() { |
| 1115 | return null; |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * Indicates whether this route requires read rights. |
| 1120 | * |
| 1121 | * The handler should override this if it does not need to read from the |
| 1122 | * wiki. This is uncommon, but may be useful for login and other account |
| 1123 | * management APIs. |
| 1124 | * |
| 1125 | * @stable to override |
| 1126 | * |
| 1127 | * @return bool |
| 1128 | */ |
| 1129 | public function needsReadAccess() { |
| 1130 | return true; |
| 1131 | } |
| 1132 | |
| 1133 | /** |
| 1134 | * Indicates whether this route requires write access to the wiki. |
| 1135 | * |
| 1136 | * Handlers may override this method to return false if and only if the operation they |
| 1137 | * implement is "safe" per RFC 7231 section 4.2.1. A handler's operation is "safe" if |
| 1138 | * it is essentially read-only, i.e. the client does not request nor expect any state |
| 1139 | * change that would be observable in the responses to future requests. |
| 1140 | * |
| 1141 | * Implementations of this method must always return the same value, regardless of the |
| 1142 | * parameters passed to the constructor or system state. |
| 1143 | * |
| 1144 | * Handlers for GET, HEAD, OPTIONS, and TRACE requests should each implement a "safe" |
| 1145 | * operation. Handlers of PUT and DELETE requests should each implement a non-"safe" |
| 1146 | * operation. Note that handlers of POST requests can implement a "safe" operation, |
| 1147 | * particularly in the case where large input parameters are required. |
| 1148 | * |
| 1149 | * The information provided by this method is used to perform basic authorization checks |
| 1150 | * and to determine whether cross-origin requests are safe. |
| 1151 | * |
| 1152 | * @stable to override |
| 1153 | * |
| 1154 | * @return bool |
| 1155 | */ |
| 1156 | public function needsWriteAccess() { |
| 1157 | return true; |
| 1158 | } |
| 1159 | |
| 1160 | /** |
| 1161 | * Indicates whether this route can be accessed only by session providers safe vs csrf |
| 1162 | * |
| 1163 | * The handler should override this if the route must only be accessed by session |
| 1164 | * providers that are safe against csrf. |
| 1165 | * |
| 1166 | * A return value of false does not necessarily mean the route is vulnerable to csrf attacks. |
| 1167 | * It means the route can be accessed by session providers that are not automatically safe |
| 1168 | * against csrf attacks, so the possibility of csrf attacks must be considered. |
| 1169 | * |
| 1170 | * @stable to override |
| 1171 | * |
| 1172 | * @return bool |
| 1173 | */ |
| 1174 | public function requireSafeAgainstCsrf() { |
| 1175 | return false; |
| 1176 | } |
| 1177 | |
| 1178 | /** |
| 1179 | * The handler can override this to do any necessary setup after the init functions |
| 1180 | * are called to inject dependencies. |
| 1181 | * |
| 1182 | * @stable to override |
| 1183 | * @throws HttpException if the handler does not accept the request for |
| 1184 | * some reason. |
| 1185 | */ |
| 1186 | protected function postInitSetup() { |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * The handler can override this to do any necessary setup after validate() |
| 1191 | * has been called. This gives the handler an opportunity to do initialization |
| 1192 | * based on parameters before pre-execution calls like getLastModified() or getETag(). |
| 1193 | * |
| 1194 | * @stable to override |
| 1195 | * @since 1.36 |
| 1196 | */ |
| 1197 | protected function postValidationSetup() { |
| 1198 | } |
| 1199 | |
| 1200 | /** |
| 1201 | * Execute the handler. This is called after parameter validation. The |
| 1202 | * return value can either be a Response or any type accepted by |
| 1203 | * ResponseFactory::createFromReturnValue(). |
| 1204 | * |
| 1205 | * To automatically construct an error response, execute() should throw a |
| 1206 | * \MediaWiki\Rest\HttpException. Such exceptions will not be logged like |
| 1207 | * a normal exception. |
| 1208 | * |
| 1209 | * If execute() throws any other kind of exception, the exception will be |
| 1210 | * logged and a generic 500 error page will be shown. |
| 1211 | * |
| 1212 | * @stable to override |
| 1213 | * |
| 1214 | * @return mixed |
| 1215 | */ |
| 1216 | abstract public function execute(); |
| 1217 | } |