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