Convert onTransactionIdle() callers to DeferredUpdate subclasses
[lhc/web/wiklou.git] / includes / revisiondelete / RevDelList.php
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 RevisionDelete
20 */
21
22 /**
23 * Abstract base class for a list of deletable items. The list class
24 * needs to be able to make a query from a set of identifiers to pull
25 * relevant rows, to return RevDelItem subclasses wrapping them, and
26 * to wrap bulk update operations.
27 */
28 abstract class RevDelList extends RevisionListBase {
29 function __construct( IContextSource $context, Title $title, array $ids ) {
30 parent::__construct( $context, $title );
31 $this->ids = $ids;
32 }
33
34 /**
35 * Get the DB field name associated with the ID list.
36 * This used to populate the log_search table for finding log entries.
37 * Override this function.
38 * @return string|null
39 */
40 public static function getRelationType() {
41 return null;
42 }
43
44 /**
45 * Get the user right required for this list type
46 * Override this function.
47 * @since 1.22
48 * @return string|null
49 */
50 public static function getRestriction() {
51 return null;
52 }
53
54 /**
55 * Get the revision deletion constant for this list type
56 * Override this function.
57 * @since 1.22
58 * @return int|null
59 */
60 public static function getRevdelConstant() {
61 return null;
62 }
63
64 /**
65 * Suggest a target for the revision deletion
66 * Optionally override this function.
67 * @since 1.22
68 * @param Title|null $target User-supplied target
69 * @param array $ids
70 * @return Title|null
71 */
72 public static function suggestTarget( $target, array $ids ) {
73 return $target;
74 }
75
76 /**
77 * Indicate whether any item in this list is suppressed
78 * @since 1.25
79 * @return bool
80 */
81 public function areAnySuppressed() {
82 $bit = $this->getSuppressBit();
83
84 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
85 for ( $this->reset(); $this->current(); $this->next() ) {
86 // @codingStandardsIgnoreEnd
87 $item = $this->current();
88 if ( $item->getBits() & $bit ) {
89 return true;
90 }
91 }
92 return false;
93 }
94
95 /**
96 * Set the visibility for the revisions in this list. Logging and
97 * transactions are done here.
98 *
99 * @param array $params Associative array of parameters. Members are:
100 * value: ExtractBitParams() bitfield array
101 * comment: The log comment.
102 * perItemStatus: Set if you want per-item status reports
103 * @return Status
104 * @since 1.23 Added 'perItemStatus' param
105 */
106 public function setVisibility( array $params ) {
107 $bitPars = $params['value'];
108 $comment = $params['comment'];
109 $perItemStatus = isset( $params['perItemStatus'] ) ? $params['perItemStatus'] : false;
110
111 // CAS-style checks are done on the _deleted fields so the select
112 // does not need to use FOR UPDATE nor be in the atomic section
113 $dbw = wfGetDB( DB_MASTER );
114 $this->res = $this->doQuery( $dbw );
115
116 $dbw->startAtomic( __METHOD__ );
117
118 $status = Status::newGood();
119 $missing = array_flip( $this->ids );
120 $this->clearFileOps();
121 $idsForLog = [];
122 $authorIds = $authorIPs = [];
123
124 if ( $perItemStatus ) {
125 $status->itemStatuses = [];
126 }
127
128 // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
129 // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
130 // form does not let the same field get hidden and unhidden in different items at once.
131 $virtualOldBits = 0;
132 $virtualNewBits = 0;
133 $logType = 'delete';
134
135 // Will be filled with id => [old, new bits] information and
136 // passed to doPostCommitUpdates().
137 $visibilityChangeMap = [];
138
139 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
140 for ( $this->reset(); $this->current(); $this->next() ) {
141 // @codingStandardsIgnoreEnd
142 /** @var $item RevDelItem */
143 $item = $this->current();
144 unset( $missing[$item->getId()] );
145
146 if ( $perItemStatus ) {
147 $itemStatus = Status::newGood();
148 $status->itemStatuses[$item->getId()] = $itemStatus;
149 } else {
150 $itemStatus = $status;
151 }
152
153 $oldBits = $item->getBits();
154 // Build the actual new rev_deleted bitfield
155 $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
156
157 if ( $oldBits == $newBits ) {
158 $itemStatus->warning(
159 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
160 $status->failCount++;
161 continue;
162 } elseif ( $oldBits == 0 && $newBits != 0 ) {
163 $opType = 'hide';
164 } elseif ( $oldBits != 0 && $newBits == 0 ) {
165 $opType = 'show';
166 } else {
167 $opType = 'modify';
168 }
169
170 if ( $item->isHideCurrentOp( $newBits ) ) {
171 // Cannot hide current version text
172 $itemStatus->error(
173 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
174 $status->failCount++;
175 continue;
176 } elseif ( !$item->canView() ) {
177 // Cannot access this revision
178 $msg = ( $opType == 'show' ) ?
179 'revdelete-show-no-access' : 'revdelete-modify-no-access';
180 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
181 $status->failCount++;
182 continue;
183 // Cannot just "hide from Sysops" without hiding any fields
184 } elseif ( $newBits == Revision::DELETED_RESTRICTED ) {
185 $itemStatus->warning(
186 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
187 $status->failCount++;
188 continue;
189 }
190
191 // Update the revision
192 $ok = $item->setBits( $newBits );
193
194 if ( $ok ) {
195 $idsForLog[] = $item->getId();
196 // If any item field was suppressed or unsupressed
197 if ( ( $oldBits | $newBits ) & $this->getSuppressBit() ) {
198 $logType = 'suppress';
199 }
200 // Track which fields where (un)hidden for each item
201 $addedBits = ( $oldBits ^ $newBits ) & $newBits;
202 $removedBits = ( $oldBits ^ $newBits ) & $oldBits;
203 $virtualNewBits |= $addedBits;
204 $virtualOldBits |= $removedBits;
205
206 $status->successCount++;
207 if ( $item->getAuthorId() > 0 ) {
208 $authorIds[] = $item->getAuthorId();
209 } elseif ( IP::isIPAddress( $item->getAuthorName() ) ) {
210 $authorIPs[] = $item->getAuthorName();
211 }
212
213 // Save the old and new bits in $visibilityChangeMap for
214 // later use.
215 $visibilityChangeMap[$item->getId()] = [
216 'oldBits' => $oldBits,
217 'newBits' => $newBits,
218 ];
219 } else {
220 $itemStatus->error(
221 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
222 $status->failCount++;
223 }
224 }
225
226 // Handle missing revisions
227 foreach ( $missing as $id => $unused ) {
228 if ( $perItemStatus ) {
229 $status->itemStatuses[$id] = Status::newFatal( 'revdelete-modify-missing', $id );
230 } else {
231 $status->error( 'revdelete-modify-missing', $id );
232 }
233 $status->failCount++;
234 }
235
236 if ( $status->successCount == 0 ) {
237 $dbw->endAtomic( __METHOD__ );
238 return $status;
239 }
240
241 // Save success count
242 $successCount = $status->successCount;
243
244 // Move files, if there are any
245 $status->merge( $this->doPreCommitUpdates() );
246 if ( !$status->isOK() ) {
247 // Fatal error, such as no configured archive directory or I/O failures
248 wfGetLBFactory()->rollbackMasterChanges( __METHOD__ );
249 return $status;
250 }
251
252 // Log it
253 $this->updateLog(
254 $logType,
255 [
256 'title' => $this->title,
257 'count' => $successCount,
258 'newBits' => $virtualNewBits,
259 'oldBits' => $virtualOldBits,
260 'comment' => $comment,
261 'ids' => $idsForLog,
262 'authorIds' => $authorIds,
263 'authorIPs' => $authorIPs
264 ]
265 );
266
267 // Clear caches after commit
268 DeferredUpdates::addCallableUpdate(
269 function () use ( $visibilityChangeMap ) {
270 $this->doPostCommitUpdates( $visibilityChangeMap );
271 },
272 DeferredUpdates::PRESEND
273 );
274
275 $dbw->endAtomic( __METHOD__ );
276
277 return $status;
278 }
279
280 /**
281 * Reload the list data from the master DB. This can be done after setVisibility()
282 * to allow $item->getHTML() to show the new data.
283 */
284 function reloadFromMaster() {
285 $dbw = wfGetDB( DB_MASTER );
286 $this->res = $this->doQuery( $dbw );
287 }
288
289 /**
290 * Record a log entry on the action
291 * @param string $logType One of (delete,suppress)
292 * @param array $params Associative array of parameters:
293 * newBits: The new value of the *_deleted bitfield
294 * oldBits: The old value of the *_deleted bitfield.
295 * title: The target title
296 * ids: The ID list
297 * comment: The log comment
298 * authorsIds: The array of the user IDs of the offenders
299 * authorsIPs: The array of the IP/anon user offenders
300 * @throws MWException
301 */
302 private function updateLog( $logType, $params ) {
303 // Get the URL param's corresponding DB field
304 $field = RevisionDeleter::getRelationType( $this->getType() );
305 if ( !$field ) {
306 throw new MWException( "Bad log URL param type!" );
307 }
308 // Add params for affected page and ids
309 $logParams = $this->getLogParams( $params );
310 // Actually add the deletion log entry
311 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
312 $logEntry->setTarget( $params['title'] );
313 $logEntry->setComment( $params['comment'] );
314 $logEntry->setParameters( $logParams );
315 $logEntry->setPerformer( $this->getUser() );
316 // Allow for easy searching of deletion log items for revision/log items
317 $logEntry->setRelations( [
318 $field => $params['ids'],
319 'target_author_id' => $params['authorIds'],
320 'target_author_ip' => $params['authorIPs'],
321 ] );
322 $logId = $logEntry->insert();
323 $logEntry->publish( $logId );
324 }
325
326 /**
327 * Get the log action for this list type
328 * @return string
329 */
330 public function getLogAction() {
331 return 'revision';
332 }
333
334 /**
335 * Get log parameter array.
336 * @param array $params Associative array of log parameters, same as updateLog()
337 * @return array
338 */
339 public function getLogParams( $params ) {
340 return [
341 '4::type' => $this->getType(),
342 '5::ids' => $params['ids'],
343 '6::ofield' => $params['oldBits'],
344 '7::nfield' => $params['newBits'],
345 ];
346 }
347
348 /**
349 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
350 * STUB
351 */
352 public function clearFileOps() {
353 }
354
355 /**
356 * A hook for setVisibility(): do batch updates pre-commit.
357 * STUB
358 * @return Status
359 */
360 public function doPreCommitUpdates() {
361 return Status::newGood();
362 }
363
364 /**
365 * A hook for setVisibility(): do any necessary updates post-commit.
366 * STUB
367 * @param array [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
368 * @return Status
369 */
370 public function doPostCommitUpdates( array $visibilityChangeMap ) {
371 return Status::newGood();
372 }
373
374 /**
375 * Get the integer value of the flag used for suppression
376 */
377 abstract public function getSuppressBit();
378 }