Debian Codegraph @

extensions/AbuseFilter/includes/View/HideAbuseLog.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\AbuseFilter\View;
4
5 use LogEventsList;
6 use LogPage;
7 use ManualLogEntry;
8 use MediaWiki\Context\IContextSource;
9 use MediaWiki\Deferred\DeferredUpdates;
10 use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
11 use MediaWiki\Extension\AbuseFilter\Pager\AbuseLogPager;
12 use MediaWiki\Html\Html;
13 use MediaWiki\HTMLForm\HTMLForm;
14 use MediaWiki\Linker\LinkRenderer;
15 use MediaWiki\MediaWikiServices;
16 use Wikimedia\Rdbms\LBFactory;
17
18 class HideAbuseLog extends AbuseFilterView {
19
20 /** @var LBFactory */
21 private $lbFactory;
22
23 /** @var int[] */
24 private $hideIDs;
25
26 /**
27 * @param LBFactory $lbFactory
28 * @param AbuseFilterPermissionManager $afPermManager
29 * @param IContextSource $context
30 * @param LinkRenderer $linkRenderer
31 * @param string $basePageName
32 */
33 public function __construct(
34 LBFactory $lbFactory,
35 AbuseFilterPermissionManager $afPermManager,
36 IContextSource $context,
37 LinkRenderer $linkRenderer,
38 string $basePageName
39 ) {
40 parent::__construct( $afPermManager, $context, $linkRenderer, $basePageName, [] );
41 $this->lbFactory = $lbFactory;
42 $this->hideIDs = array_keys( $this->getRequest()->getArray( 'hideids', [] ) );
43 }
44
45 /**
46 * Shows the page
47 */
48 public function show(): void {
49 $output = $this->getOutput();
50 $output->enableOOUI();
51
52 if ( !$this->afPermManager->canHideAbuseLog( $this->getAuthority() ) ) {
53 $output->addWikiMsg( 'abusefilter-log-hide-forbidden' );
54 return;
55 }
56
57 if ( !$this->hideIDs ) {
58 $output->addWikiMsg( 'abusefilter-log-hide-no-selected' );
59 return;
60 }
61
62 // TODO DI
63 $pager = new AbuseLogPager(
64 $this->getContext(),
65 MediaWikiServices::getInstance()->getLinkRenderer(),
66 [ 'afl_id' => $this->hideIDs ],
67 MediaWikiServices::getInstance()->getLinkBatchFactory(),
68 MediaWikiServices::getInstance()->getPermissionManager(),
69 $this->afPermManager,
70 $this->basePageName,
71 array_fill_keys( $this->hideIDs, $this->getRequest()->getVal( 'wpshoworhide' ) )
72 );
73 $pager->doQuery();
74 if ( $pager->getResult()->numRows() === 0 ) {
75 $output->addWikiMsg( 'abusefilter-log-hide-no-selected' );
76 return;
77 }
78
79 $output->addModuleStyles( 'mediawiki.interface.helpers.styles' );
80 $output->wrapWikiMsg(
81 "<strong>$1</strong>",
82 [
83 'abusefilter-log-hide-selected',
84 $this->getLanguage()->formatNum( count( $this->hideIDs ) )
85 ]
86 );
87 $output->addHTML( Html::rawElement( 'ul', [ 'class' => 'plainlinks' ], $pager->getBody() ) );
88
89 $hideReasonsOther = $this->msg( 'revdelete-reasonotherlist' )->text();
90 $hideReasons = $this->msg( 'revdelete-reason-dropdown-suppress' )->inContentLanguage()->text();
91 $hideReasons = Html::listDropdownOptions( $hideReasons, [ 'other' => $hideReasonsOther ] );
92
93 $formInfo = [
94 'showorhide' => [
95 'type' => 'radio',
96 'label-message' => 'abusefilter-log-hide-set-visibility',
97 'options-messages' => [
98 'abusefilter-log-hide-show' => 'show',
99 'abusefilter-log-hide-hide' => 'hide'
100 ],
101 'default' => 'hide',
102 'flatlist' => true
103 ],
104 'dropdownreason' => [
105 'type' => 'select',
106 'options' => $hideReasons,
107 'label-message' => 'abusefilter-log-hide-reason'
108 ],
109 'reason' => [
110 'type' => 'text',
111 'label-message' => 'abusefilter-log-hide-reason-other',
112 ],
113 ];
114
115 $actionURL = $this->getTitle( 'hide' )->getLocalURL( [ 'hideids' => array_fill_keys( $this->hideIDs, 1 ) ] );
116 HTMLForm::factory( 'ooui', $formInfo, $this->getContext() )
117 ->setAction( $actionURL )
118 ->setWrapperLegend( $this->msg( 'abusefilter-log-hide-legend' )->text() )
119 ->setSubmitCallback( [ $this, 'saveHideForm' ] )
120 ->showAlways();
121
122 // Show suppress log for this entry. Hack: since every suppression is performed on a
123 // totally different page (i.e. Special:AbuseLog/xxx), we use showLogExtract without
124 // specifying a title and then adding it in conds.
125 // This isn't shown if the request was posted because we update visibility in a DeferredUpdate, so it would
126 // display outdated info that might confuse the user.
127 // TODO Can we improve this somehow?
128 if ( !$this->getRequest()->wasPosted() ) {
129 $suppressLogPage = new LogPage( 'suppress' );
130 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
131 $searchTitles = [];
132 foreach ( $this->hideIDs as $id ) {
133 $searchTitles[] = $this->getTitle( (string)$id )->getDBkey();
134 }
135 $conds = [ 'log_namespace' => NS_SPECIAL, 'log_title' => $searchTitles ];
136 LogEventsList::showLogExtract( $output, 'suppress', '', '', [ 'conds' => $conds ] );
137 }
138 }
139
140 /**
141 * Process the hide form after submission. This performs the actual visibility update. Used as callback by HTMLForm
142 *
143 * @param array $fields
144 * @return bool|array True on success, array of error message keys otherwise
145 */
146 public function saveHideForm( array $fields ) {
147 // Determine which rows actually have to be changed
148 $dbw = $this->lbFactory->getPrimaryDatabase();
149 $newValue = $fields['showorhide'] === 'hide' ? 1 : 0;
150 $actualIDs = $dbw->newSelectQueryBuilder()
151 ->select( 'afl_id' )
152 ->from( 'abuse_filter_log' )
153 ->where( [
154 'afl_id' => $this->hideIDs,
155 $dbw->expr( 'afl_deleted', '!=', $newValue ),
156 ] )
157 ->caller( __METHOD__ )
158 ->fetchFieldValues();
159 if ( !count( $actualIDs ) ) {
160 return [ 'abusefilter-log-hide-no-change' ];
161 }
162
163 $dbw->newUpdateQueryBuilder()
164 ->update( 'abuse_filter_log' )
165 ->set( [ 'afl_deleted' => $newValue ] )
166 ->where( [ 'afl_id' => $actualIDs ] )
167 ->caller( __METHOD__ )
168 ->execute();
169
170 // Log in a DeferredUpdates to avoid potential flood
171 DeferredUpdates::addCallableUpdate( function () use ( $fields, $actualIDs ) {
172 $reason = $fields['dropdownreason'];
173 if ( $reason === 'other' ) {
174 $reason = $fields['reason'];
175 } elseif ( $fields['reason'] !== '' ) {
176 $reason .=
177 $this->msg( 'colon-separator' )->inContentLanguage()->text() . $fields['reason'];
178 }
179
180 $action = $fields['showorhide'] === 'hide' ? 'hide-afl' : 'unhide-afl';
181 foreach ( $actualIDs as $logid ) {
182 $logEntry = new ManualLogEntry( 'suppress', $action );
183 $logEntry->setPerformer( $this->getUser() );
184 $logEntry->setTarget( $this->getTitle( $logid ) );
185 $logEntry->setComment( $reason );
186 $logEntry->insert();
187 }
188 } );
189
190 $count = count( $actualIDs );
191 $this->getOutput()->prependHTML(
192 Html::successBox(
193 $this->msg( 'abusefilter-log-hide-done' )->params(
194 $this->getLanguage()->formatNum( $count ),
195 // Messages used: abusefilter-log-hide-done-hide, abusefilter-log-hide-done-show
196 $this->msg( 'abusefilter-log-hide-done-' . $fields['showorhide'] )->numParams( $count )->text()
197 )->escaped()
198 )
199 );
200
201 return true;
202 }
203
204 }