SpecialUserLogout.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Implements Special:Userlogout
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. */
  23. /**
  24. * Implements Special:Userlogout
  25. *
  26. * @ingroup SpecialPage
  27. */
  28. class SpecialUserLogout extends FormSpecialPage {
  29. function __construct() {
  30. parent::__construct( 'Userlogout' );
  31. }
  32. public function doesWrites() {
  33. return true;
  34. }
  35. public function isListed() {
  36. return false;
  37. }
  38. protected function getGroupName() {
  39. return 'login';
  40. }
  41. protected function getFormFields() {
  42. return [];
  43. }
  44. protected function getDisplayFormat() {
  45. return 'ooui';
  46. }
  47. public function execute( $par ) {
  48. if ( $this->getUser()->isAnon() ) {
  49. $this->setHeaders();
  50. $this->showSuccess();
  51. return;
  52. }
  53. parent::execute( $par );
  54. }
  55. public function alterForm( HTMLForm $form ) {
  56. $form->setTokenSalt( 'logoutToken' );
  57. $form->addHeaderText( $this->msg( 'userlogout-continue' ) );
  58. $form->addHiddenFields( $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
  59. }
  60. /**
  61. * Process the form. At this point we know that the user passes all the criteria in
  62. * userCanExecute(), and if the data array contains 'Username', etc, then Username
  63. * resets are allowed.
  64. * @param array $data
  65. * @throws MWException
  66. * @throws ThrottledError|PermissionsError
  67. * @return Status
  68. */
  69. public function onSubmit( array $data ) {
  70. // Make sure it's possible to log out
  71. $session = MediaWiki\Session\SessionManager::getGlobalSession();
  72. if ( !$session->canSetUser() ) {
  73. throw new ErrorPageError(
  74. 'cannotlogoutnow-title',
  75. 'cannotlogoutnow-text',
  76. [
  77. $session->getProvider()->describe( RequestContext::getMain()->getLanguage() )
  78. ]
  79. );
  80. }
  81. $user = $this->getUser();
  82. $user->logout();
  83. return new Status();
  84. }
  85. public function onSuccess() {
  86. $this->showSuccess();
  87. $user = $this->getUser();
  88. $oldName = $user->getName();
  89. $out = $this->getOutput();
  90. // Hook.
  91. $injected_html = '';
  92. Hooks::run( 'UserLogoutComplete', [ &$user, &$injected_html, $oldName ] );
  93. $out->addHTML( $injected_html );
  94. }
  95. private function showSuccess() {
  96. $loginURL = SpecialPage::getTitleFor( 'Userlogin' )->getFullURL(
  97. $this->getRequest()->getValues( 'returnto', 'returntoquery' ) );
  98. $out = $this->getOutput();
  99. $out->addWikiMsg( 'logouttext', $loginURL );
  100. $out->returnToMain();
  101. }
  102. /**
  103. * Let blocked users to log out and come back with their sockpuppets
  104. */
  105. public function requiresUnblock() {
  106. return false;
  107. }
  108. }