MediaWiki master
SpecialUserLogout.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Specials;
8
16use OOUI\HtmlSnippet;
17use OOUI\MessageWidget;
18
29 private $oldUserName;
30
31 public function __construct(
32 private readonly TempUserConfig $tempUserConfig,
33 ) {
34 parent::__construct( 'Userlogout' );
35 }
36
38 public function doesWrites() {
39 return true;
40 }
41
43 public function isListed() {
44 return $this->getAuthManager()->canAuthenticateNow();
45 }
46
48 protected function getGroupName() {
49 return 'login';
50 }
51
53 protected function getFormFields() {
54 return [];
55 }
56
58 protected function getDisplayFormat() {
59 return 'ooui';
60 }
61
63 public function execute( $par ) {
64 $user = $this->getUser();
65 if ( $user->isAnon() ) {
66 $this->setHeaders();
67 $this->showSuccess();
68 return;
69 }
70 $this->oldUserName = $user->getName();
71
72 parent::execute( $par );
73 }
74
75 public function alterForm( HTMLForm $form ) {
76 $form->setTokenSalt( 'logoutToken' );
77 $form->setSubmitTextMsg( 'userlogout-submit' );
78 if ( $this->getUser()->isTemp() ) {
79 $form->addHeaderHtml(
80 // Keep in sync with mediawiki.page.ready/ready.js
81 Html::rawElement( 'p', [], $this->msg( 'userlogout-temp' )->parse() ) .
82 Html::rawElement( 'p', [], $this->msg( 'userlogout-temp-moreinfo' )->parse() ) .
83 ( new MessageWidget( [
84 'type' => 'notice',
85 'label' => new HtmlSnippet(
87 'strong',
88 [],
89 $this->msg( 'userlogout-temp-messagebox-title' )->text()
90 ) .
91 Html::element( 'br' ) .
92 $this->msg( 'userlogout-temp-messagebox-body' )->parse()
93 ),
94 ] ) )->toString()
95 );
96 } else {
97 $form->addHeaderHtml(
98 Html::element( 'p', [], $this->msg( 'userlogout-continue' )->text() )
99 );
100 }
101
102 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
103 }
104
112 public function onSubmit( array $data ) {
113 // Make sure it's possible to log out
114 $session = $this->getRequest()->getSession();
115 if ( !$session->canSetUser() ) {
116 throw new ErrorPageError(
117 'cannotlogoutnow-title',
118 'cannotlogoutnow-text',
119 [
120 $session->getProvider()->describe( $this->getLanguage() )
121 ]
122 );
123 }
124
125 $user = $this->getUser();
126
127 $user->logout();
128 return new Status();
129 }
130
131 public function onSuccess() {
132 $this->showSuccess();
133
134 $out = $this->getOutput();
135 // Hook.
136 $injected_html = '';
137 $this->getHookRunner()->onUserLogoutComplete( $this->getUser(), $injected_html, $this->oldUserName );
138 $out->addHTML( $injected_html );
139 }
140
141 private function showSuccess() {
142 $loginURL = SpecialPage::getTitleFor( 'Userlogin' )->getFullURL(
143 $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
144
145 $out = $this->getOutput();
146
147 $messageKey = 'logouttext';
148 if (
149 ( $this->oldUserName !== null && $this->tempUserConfig->isTempName( $this->oldUserName ) ) ||
150 $this->getRequest()->getCheck( 'wasTempUser' )
151 ) {
152 // Generates the message key logouttext-for-temporary-account which is used to customise the success
153 // message for a temporary account.
154 $messageKey .= '-for-temporary-account';
155 }
156 $out->addWikiMsg( $messageKey, $loginURL );
157
158 $out->returnToMain();
159 }
160
165 public function requiresUnblock() {
166 return false;
167 }
168
170 public function getDescription() {
171 // Set the page title as "templogout" if the user is (or just was) logged in to a temporary account
172 if (
173 $this->getUser()->isTemp() ||
174 ( $this->oldUserName !== null && $this->tempUserConfig->isTempName( $this->oldUserName ) ) ||
175 $this->getRequest()->getCheck( 'wasTempUser' )
176 ) {
177 return $this->msg( 'templogout' );
178 }
179 return parent::getDescription();
180 }
181}
182
187class_alias( SpecialUserLogout::class, 'SpecialUserLogout' );
An error page which can definitely be safely rendered using the OutputPage.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:207
setTokenSalt( $salt)
Set the salt for the edit token.
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
addHiddenFields(array $fields)
Add an array of hidden fields to the output Array values are discarded for security reasons (per WebR...
addHeaderHtml( $html, $section=null)
Add HTML to the header, inside the form.
Definition HTMLForm.php:937
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
Special page which uses an HTMLForm to handle processing.
string null $par
The subpage of the special page.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getUser()
Shortcut to get the User executing this instance.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
Implements Special:Userlogout.
__construct(private readonly TempUserConfig $tempUserConfig,)
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
requiresUnblock()
Let blocked users to log out and come back with their sockpuppets.
execute( $par)
Basic SpecialPage workflow: get a form, send it to the user; get some data back,.
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
doesWrites()
Indicates whether POST requests to this special page require write access to the wiki....
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
getFormFields()
Get an HTMLForm descriptor array.array
onSubmit(array $data)
Process the form.
isListed()
Whether this special page is listed in Special:SpecialPages.to override 1.3 (r3583) bool
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Interface for temporary user creation config and name matching.
element(SerializerNode $parent, SerializerNode $node, $contents)