MediaWiki master
SpecialUserLogout.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
30
41 private $oldUserName;
42
43 private TempUserConfig $tempUserConfig;
44
45 public function __construct( TempUserConfig $tempUserConfig ) {
46 parent::__construct( 'Userlogout' );
47 $this->tempUserConfig = $tempUserConfig;
48 }
49
50 public function doesWrites() {
51 return true;
52 }
53
54 public function isListed() {
55 return $this->getAuthManager()->canAuthenticateNow();
56 }
57
58 protected function getGroupName() {
59 return 'login';
60 }
61
62 protected function getFormFields() {
63 return [];
64 }
65
66 protected function getDisplayFormat() {
67 return 'ooui';
68 }
69
70 public function execute( $par ) {
71 $user = $this->getUser();
72 if ( $user->isAnon() ) {
73 $this->setHeaders();
74 $this->showSuccess();
75 return;
76 }
77 $this->oldUserName = $user->getName();
78
79 parent::execute( $par );
80 }
81
82 public function alterForm( HTMLForm $form ) {
83 $form->setTokenSalt( 'logoutToken' );
84 $form->addHeaderHtml( $this->msg(
85 $this->getUser()->isTemp() ? 'userlogout-temp' : 'userlogout-continue'
86 ) );
87
88 $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
89 }
90
98 public function onSubmit( array $data ) {
99 // Make sure it's possible to log out
100 $session = SessionManager::getGlobalSession();
101 if ( !$session->canSetUser() ) {
102 throw new ErrorPageError(
103 'cannotlogoutnow-title',
104 'cannotlogoutnow-text',
105 [
106 $session->getProvider()->describe( $this->getLanguage() )
107 ]
108 );
109 }
110
111 $user = $this->getUser();
112
113 $user->logout();
114 return new Status();
115 }
116
117 public function onSuccess() {
118 $this->showSuccess();
119
120 $out = $this->getOutput();
121 // Hook.
122 $injected_html = '';
123 $this->getHookRunner()->onUserLogoutComplete( $this->getUser(), $injected_html, $this->oldUserName );
124 $out->addHTML( $injected_html );
125 }
126
127 private function showSuccess() {
128 $loginURL = SpecialPage::getTitleFor( 'Userlogin' )->getFullURL(
129 $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
130
131 $out = $this->getOutput();
132
133 $messageKey = 'logouttext';
134 if (
135 ( isset( $this->oldUserName ) && $this->tempUserConfig->isTempName( $this->oldUserName ) ) ||
136 $this->getRequest()->getCheck( 'wasTempUser' )
137 ) {
138 // Generates the message key logouttext-for-temporary-account which is used to customise the success
139 // message for a temporary account.
140 $messageKey .= '-for-temporary-account';
141 }
142 $out->addWikiMsg( $messageKey, $loginURL );
143
144 $out->returnToMain();
145 }
146
151 public function requiresUnblock() {
152 return false;
153 }
154
155 public function getDescription() {
156 // Set the page title as "templogout" if the user is (or just was) logged in to a temporary account
157 if (
158 $this->getUser()->isTemp() ||
159 ( isset( $this->oldUserName ) && $this->tempUserConfig->isTempName( $this->oldUserName ) ) ||
160 $this->getRequest()->getCheck( 'wasTempUser' )
161 ) {
162 return $this->msg( 'templogout' );
163 }
164 return parent::getDescription();
165 }
166}
167
172class_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:208
setTokenSalt( $salt)
Set the salt for the edit token.
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:976
This serves as the entry point to the MediaWiki session handling system.
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.
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.
onSubmit(array $data)
Process the form.
isListed()
Whether this special page is listed in Special:SpecialPages.
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:54
Interface for temporary user creation config and name matching.