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 private TempUserConfig $tempUserConfig;
32
33 public function __construct( TempUserConfig $tempUserConfig ) {
34 parent::__construct( 'Userlogout' );
35 $this->tempUserConfig = $tempUserConfig;
36 }
37
39 public function doesWrites() {
40 return true;
41 }
42
44 public function isListed() {
45 return $this->getAuthManager()->canAuthenticateNow();
46 }
47
49 protected function getGroupName() {
50 return 'login';
51 }
52
54 protected function getFormFields() {
55 return [];
56 }
57
59 protected function getDisplayFormat() {
60 return 'ooui';
61 }
62
64 public function execute( $par ) {
65 $user = $this->getUser();
66 if ( $user->isAnon() ) {
67 $this->setHeaders();
68 $this->showSuccess();
69 return;
70 }
71 $this->oldUserName = $user->getName();
72
73 parent::execute( $par );
74 }
75
76 public function alterForm( HTMLForm $form ) {
77 $form->setTokenSalt( 'logoutToken' );
78 $form->setSubmitTextMsg( 'userlogout-submit' );
79 if ( $this->getUser()->isTemp() ) {
80 $form->addHeaderHtml(
81 // Keep in sync with mediawiki.page.ready/ready.js
82 Html::rawElement( 'p', [], $this->msg( 'userlogout-temp' )->parse() ) .
83 Html::rawElement( 'p', [], $this->msg( 'userlogout-temp-moreinfo' )->parse() ) .
84 ( new MessageWidget( [
85 'type' => 'notice',
86 'label' => new HtmlSnippet(
88 'strong',
89 [],
90 $this->msg( 'userlogout-temp-messagebox-title' )->text()
91 ) .
92 Html::element( 'br' ) .
93 $this->msg( 'userlogout-temp-messagebox-body' )->parse()
94 ),
95 ] ) )->toString()
96 );
97 } else {
98 $form->addHeaderHtml(
99 Html::element( 'p', [], $this->msg( 'userlogout-continue' )->text() )
100 );
101 }
102
103 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
104 }
105
113 public function onSubmit( array $data ) {
114 // Make sure it's possible to log out
115 $session = $this->getRequest()->getSession();
116 if ( !$session->canSetUser() ) {
117 throw new ErrorPageError(
118 'cannotlogoutnow-title',
119 'cannotlogoutnow-text',
120 [
121 $session->getProvider()->describe( $this->getLanguage() )
122 ]
123 );
124 }
125
126 $user = $this->getUser();
127
128 $user->logout();
129 return new Status();
130 }
131
132 public function onSuccess() {
133 $this->showSuccess();
134
135 $out = $this->getOutput();
136 // Hook.
137 $injected_html = '';
138 $this->getHookRunner()->onUserLogoutComplete( $this->getUser(), $injected_html, $this->oldUserName );
139 $out->addHTML( $injected_html );
140 }
141
142 private function showSuccess() {
143 $loginURL = SpecialPage::getTitleFor( 'Userlogin' )->getFullURL(
144 $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
145
146 $out = $this->getOutput();
147
148 $messageKey = 'logouttext';
149 if (
150 ( $this->oldUserName !== null && $this->tempUserConfig->isTempName( $this->oldUserName ) ) ||
151 $this->getRequest()->getCheck( 'wasTempUser' )
152 ) {
153 // Generates the message key logouttext-for-temporary-account which is used to customise the success
154 // message for a temporary account.
155 $messageKey .= '-for-temporary-account';
156 }
157 $out->addWikiMsg( $messageKey, $loginURL );
158
159 $out->returnToMain();
160 }
161
166 public function requiresUnblock() {
167 return false;
168 }
169
171 public function getDescription() {
172 // Set the page title as "templogout" if the user is (or just was) logged in to a temporary account
173 if (
174 $this->getUser()->isTemp() ||
175 ( $this->oldUserName !== null && $this->tempUserConfig->isTempName( $this->oldUserName ) ) ||
176 $this->getRequest()->getCheck( 'wasTempUser' )
177 ) {
178 return $this->msg( 'templogout' );
179 }
180 return parent::getDescription();
181 }
182}
183
188class_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:195
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:924
This class is a collection of static functions that serve two purposes:
Definition Html.php:43
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(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)