Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiCreateTempUserTrait.php
blob3d36fa6e531da29009e8e6952daf9e9c6f43977f
1 <?php
3 namespace MediaWiki\Api;
5 use MediaWiki\Request\WebRequest;
6 use MediaWiki\User\UserIdentity;
7 use Wikimedia\ParamValidator\ParamValidator;
9 /**
10 * Methods needed by APIs that create a temporary user.
12 * This should only be added to classes that extend ApiBase.
14 * @ingroup API
15 * @since 1.42
17 trait ApiCreateTempUserTrait {
18 /**
19 * Get any login redirect URL added by TempUserCreatedRedirectHook.
21 * @param array $params
22 * @param UserIdentity $savedTempUser
23 * @return string The redirect URL, or '' if none was added
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 );
35 if ( $returnToAnchor !== '' && !str_starts_with( $returnToAnchor, '#' ) ) {
36 // Add leading '#' if missing (it's required)
37 $returnToAnchor = '#' . $returnToAnchor;
39 $redirectUrl = '';
40 $this->getHookRunner()->onTempUserCreatedRedirect(
41 $this->getRequest()->getSession(),
42 $savedTempUser,
43 $params['returnto'] ?: '',
44 $returnToQuery,
45 $returnToAnchor,
46 $redirectUrl
48 return $redirectUrl;
51 /**
52 * Add params needed for TempUserCreatedRedirectHook.
54 * @return array
56 protected function getCreateTempUserParams(): array {
57 return [
58 'returnto' => [
59 ParamValidator::PARAM_TYPE => 'title',
60 ApiBase::PARAM_HELP_MSG => 'apihelp-edit-param-returnto',
62 'returntoquery' => [
63 ParamValidator::PARAM_TYPE => 'string',
64 ParamValidator::PARAM_DEFAULT => '',
65 ApiBase::PARAM_HELP_MSG => 'apihelp-edit-param-returntoquery',
67 'returntoanchor' => [
68 ParamValidator::PARAM_TYPE => 'string',
69 ParamValidator::PARAM_DEFAULT => '',
70 ApiBase::PARAM_HELP_MSG => 'apihelp-edit-param-returntoanchor',
75 // region Methods required from ApiBase
76 /** @name Methods required from ApiBase
77 * @{
80 /**
81 * @see ApiBase::getHookRunner
82 * @return ApiHookRunner
84 abstract protected function getHookRunner();
86 /**
87 * @see IContextSource::getRequest
88 * @return WebRequest
90 abstract public function getRequest();
92 /** @} */
93 // endregion -- end of methods required from ApiBase
96 /** @deprecated class alias since 1.43 */
97 class_alias( ApiCreateTempUserTrait::class, 'ApiCreateTempUserTrait' );