Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.94% |
31 / 33 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ApiCreateTempUserTrait | |
96.88% |
31 / 32 |
|
50.00% |
1 / 2 |
6 | |
0.00% |
0 / 1 |
| getTempUserRedirectUrl | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
5.01 | |||
| getCreateTempUserParams | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| getHookRunner | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getRequest | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Api; |
| 4 | |
| 5 | use MediaWiki\Request\WebRequest; |
| 6 | use MediaWiki\User\UserIdentity; |
| 7 | use Wikimedia\ParamValidator\ParamValidator; |
| 8 | |
| 9 | /** |
| 10 | * Methods needed by APIs that create a temporary user. |
| 11 | * |
| 12 | * This should only be added to classes that extend ApiBase. |
| 13 | * |
| 14 | * @ingroup API |
| 15 | * @since 1.42 |
| 16 | */ |
| 17 | trait ApiCreateTempUserTrait { |
| 18 | /** |
| 19 | * Get any login redirect URL added by TempUserCreatedRedirectHook. |
| 20 | * |
| 21 | * @param array $params |
| 22 | * @param UserIdentity $savedTempUser |
| 23 | * @return string The redirect URL, or '' if none was added |
| 24 | */ |
| 25 | protected function getTempUserRedirectUrl( |
| 26 | array $params, |
| 27 | UserIdentity $savedTempUser |
| 28 | ): string { |
| 29 | $returnToQuery = $params['returntoquery']; |
| 30 | $returnToAnchor = $params['returntoanchor']; |
| 31 | if ( str_starts_with( $returnToQuery, '?' ) ) { |
| 32 | // Remove leading '?' if provided (both ways work, but this is more common elsewhere) |
| 33 | $returnToQuery = substr( $returnToQuery, 1 ); |
| 34 | } |
| 35 | if ( $returnToAnchor !== '' && !str_starts_with( $returnToAnchor, '#' ) ) { |
| 36 | // Add leading '#' if missing (it's required) |
| 37 | $returnToAnchor = '#' . $returnToAnchor; |
| 38 | } |
| 39 | $redirectUrl = ''; |
| 40 | $this->getHookRunner()->onTempUserCreatedRedirect( |
| 41 | $this->getRequest()->getSession(), |
| 42 | $savedTempUser, |
| 43 | $params['returnto'] ?: '', |
| 44 | $returnToQuery, |
| 45 | $returnToAnchor, |
| 46 | $redirectUrl |
| 47 | ); |
| 48 | return $redirectUrl; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Add params needed for TempUserCreatedRedirectHook. |
| 53 | */ |
| 54 | protected function getCreateTempUserParams(): array { |
| 55 | return [ |
| 56 | 'returnto' => [ |
| 57 | ParamValidator::PARAM_TYPE => 'title', |
| 58 | ApiBase::PARAM_HELP_MSG => 'apihelp-edit-param-returnto', |
| 59 | ], |
| 60 | 'returntoquery' => [ |
| 61 | ParamValidator::PARAM_TYPE => 'string', |
| 62 | ParamValidator::PARAM_DEFAULT => '', |
| 63 | ApiBase::PARAM_HELP_MSG => 'apihelp-edit-param-returntoquery', |
| 64 | ], |
| 65 | 'returntoanchor' => [ |
| 66 | ParamValidator::PARAM_TYPE => 'string', |
| 67 | ParamValidator::PARAM_DEFAULT => '', |
| 68 | ApiBase::PARAM_HELP_MSG => 'apihelp-edit-param-returntoanchor', |
| 69 | ], |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | // region Methods required from ApiBase |
| 74 | /** @name Methods required from ApiBase |
| 75 | * @{ |
| 76 | */ |
| 77 | |
| 78 | /** |
| 79 | * @see ApiBase::getHookRunner |
| 80 | * @return ApiHookRunner |
| 81 | */ |
| 82 | abstract protected function getHookRunner(); |
| 83 | |
| 84 | /** |
| 85 | * @see IContextSource::getRequest |
| 86 | * @return WebRequest |
| 87 | */ |
| 88 | abstract public function getRequest(); |
| 89 | |
| 90 | /** @} */ |
| 91 | // endregion -- end of methods required from ApiBase |
| 92 | } |
| 93 | |
| 94 | /** @deprecated class alias since 1.43 */ |
| 95 | class_alias( ApiCreateTempUserTrait::class, 'ApiCreateTempUserTrait' ); |