SpecialMute.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /*
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup SpecialPage
  20. */
  21. use MediaWiki\Preferences\MultiUsernameFilter;
  22. /**
  23. * A special page that allows users to modify their notification
  24. * preferences
  25. *
  26. * @ingroup SpecialPage
  27. */
  28. class SpecialMute extends FormSpecialPage {
  29. const PAGE_NAME = 'Mute';
  30. /** @var User */
  31. private $target;
  32. /** @var int */
  33. private $targetCentralId;
  34. /** @var bool */
  35. private $enableUserEmailBlacklist;
  36. /** @var bool */
  37. private $enableUserEmail;
  38. /** @var CentralIdLookup */
  39. private $centralIdLookup;
  40. public function __construct() {
  41. // TODO: inject all these dependencies once T222388 is resolved
  42. $config = RequestContext::getMain()->getConfig();
  43. $this->enableUserEmailBlacklist = $config->get( 'EnableUserEmailBlacklist' );
  44. $this->enableUserEmail = $config->get( 'EnableUserEmail' );
  45. $this->centralIdLookup = CentralIdLookup::factory();
  46. parent::__construct( self::PAGE_NAME, '', false );
  47. }
  48. /**
  49. * Entry point for special pages
  50. *
  51. * @param string $par
  52. */
  53. public function execute( $par ) {
  54. $this->requireLogin( 'specialmute-login-required' );
  55. $this->loadTarget( $par );
  56. parent::execute( $par );
  57. $out = $this->getOutput();
  58. $out->addModules( 'mediawiki.misc-authed-ooui' );
  59. }
  60. /**
  61. * @inheritDoc
  62. */
  63. public function requiresUnblock() {
  64. return false;
  65. }
  66. /**
  67. * @inheritDoc
  68. */
  69. protected function getDisplayFormat() {
  70. return 'ooui';
  71. }
  72. /**
  73. * @inheritDoc
  74. */
  75. public function onSuccess() {
  76. $out = $this->getOutput();
  77. $out->addWikiMsg( 'specialmute-success' );
  78. }
  79. /**
  80. * @param array $data
  81. * @param HTMLForm|null $form
  82. * @return bool
  83. */
  84. public function onSubmit( array $data, HTMLForm $form = null ) {
  85. $hookData = [];
  86. foreach ( $data as $userOption => $value ) {
  87. $hookData[$userOption]['before'] = $this->isTargetBlacklisted( $userOption );
  88. if ( $value ) {
  89. $this->muteTarget( $userOption );
  90. } else {
  91. $this->unmuteTarget( $userOption );
  92. }
  93. $hookData[$userOption]['after'] = (bool)$value;
  94. }
  95. // NOTE: this hook is temporary
  96. Hooks::run( 'SpecialMuteSubmit', [ $hookData ] );
  97. return true;
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function getDescription() {
  103. return $this->msg( 'specialmute' )->text();
  104. }
  105. /**
  106. * Un-mute target
  107. *
  108. * @param string $userOption up_property key that holds the blacklist
  109. */
  110. private function unmuteTarget( $userOption ) {
  111. $blacklist = $this->getBlacklist( $userOption );
  112. $key = array_search( $this->targetCentralId, $blacklist );
  113. if ( $key !== false ) {
  114. unset( $blacklist[$key] );
  115. $blacklist = implode( "\n", $blacklist );
  116. $user = $this->getUser();
  117. $user->setOption( $userOption, $blacklist );
  118. $user->saveSettings();
  119. }
  120. }
  121. /**
  122. * Mute target
  123. * @param string $userOption up_property key that holds the blacklist
  124. */
  125. private function muteTarget( $userOption ) {
  126. // avoid duplicates just in case
  127. if ( !$this->isTargetBlacklisted( $userOption ) ) {
  128. $blacklist = $this->getBlacklist( $userOption );
  129. $blacklist[] = $this->targetCentralId;
  130. $blacklist = implode( "\n", $blacklist );
  131. $user = $this->getUser();
  132. $user->setOption( $userOption, $blacklist );
  133. $user->saveSettings();
  134. }
  135. }
  136. /**
  137. * @inheritDoc
  138. */
  139. protected function getForm() {
  140. $form = parent::getForm();
  141. $form->setId( 'mw-specialmute-form' );
  142. $form->setHeaderText( $this->msg( 'specialmute-header', $this->target )->parse() );
  143. $form->setSubmitTextMsg( 'specialmute-submit' );
  144. $form->setSubmitID( 'save' );
  145. return $form;
  146. }
  147. /**
  148. * @inheritDoc
  149. */
  150. protected function getFormFields() {
  151. $fields = [];
  152. if (
  153. $this->enableUserEmailBlacklist &&
  154. $this->enableUserEmail &&
  155. $this->getUser()->getEmailAuthenticationTimestamp()
  156. ) {
  157. $fields['email-blacklist'] = [
  158. 'type' => 'check',
  159. 'label-message' => 'specialmute-label-mute-email',
  160. 'default' => $this->isTargetBlacklisted( 'email-blacklist' ),
  161. ];
  162. }
  163. Hooks::run( 'SpecialMuteModifyFormFields', [ $this, &$fields ] );
  164. if ( count( $fields ) == 0 ) {
  165. throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
  166. }
  167. return $fields;
  168. }
  169. /**
  170. * @param string $username
  171. */
  172. private function loadTarget( $username ) {
  173. $target = User::newFromName( $username );
  174. if ( !$target || !$target->getId() ) {
  175. throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
  176. } else {
  177. $this->target = $target;
  178. $this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target );
  179. }
  180. }
  181. /**
  182. * @param string $userOption
  183. * @return bool
  184. */
  185. public function isTargetBlacklisted( $userOption ) {
  186. $blacklist = $this->getBlacklist( $userOption );
  187. return in_array( $this->targetCentralId, $blacklist, true );
  188. }
  189. /**
  190. * @param string $userOption
  191. * @return array
  192. */
  193. private function getBlacklist( $userOption ) {
  194. $blacklist = $this->getUser()->getOption( $userOption );
  195. if ( !$blacklist ) {
  196. return [];
  197. }
  198. return MultiUsernameFilter::splitIds( $blacklist );
  199. }
  200. }