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.
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.
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
19 * @ingroup RevisionDelete
22 use MediaWiki\MediaWikiServices
;
23 use MediaWiki\Storage\RevisionRecord
;
26 * Abstract base class for a list of deletable items. The list class
27 * needs to be able to make a query from a set of identifiers to pull
28 * relevant rows, to return RevDelItem subclasses wrapping them, and
29 * to wrap bulk update operations.
31 * @property RevDelItem $current
32 * @method RevDelItem next()
33 * @method RevDelItem reset()
34 * @method RevDelItem current()
35 * @phan-file-suppress PhanParamSignatureMismatch
37 abstract class RevDelList
extends RevisionListBase
{
38 function __construct( IContextSource
$context, Title
$title, array $ids ) {
39 parent
::__construct( $context, $title );
44 * Get the DB field name associated with the ID list.
45 * This used to populate the log_search table for finding log entries.
46 * Override this function.
49 public static function getRelationType() {
54 * Get the user right required for this list type
55 * Override this function.
59 public static function getRestriction() {
64 * Get the revision deletion constant for this list type
65 * Override this function.
69 public static function getRevdelConstant() {
74 * Suggest a target for the revision deletion
75 * Optionally override this function.
77 * @param Title|null $target User-supplied target
81 public static function suggestTarget( $target, array $ids ) {
86 * Indicate whether any item in this list is suppressed
90 public function areAnySuppressed() {
91 $bit = $this->getSuppressBit();
93 /** @var RevDelItem $item */
94 foreach ( $this as $item ) {
95 if ( $item->getBits() & $bit ) {
104 * Set the visibility for the revisions in this list. Logging and
105 * transactions are done here.
107 * @param array $params Associative array of parameters. Members are:
108 * value: ExtractBitParams() bitfield array
109 * comment: The log comment
110 * perItemStatus: Set if you want per-item status reports
111 * tags: The array of change tags to apply to the log entry
113 * @since 1.23 Added 'perItemStatus' param
115 public function setVisibility( array $params ) {
116 $status = Status
::newGood();
118 $bitPars = $params['value'];
119 $comment = $params['comment'];
120 $perItemStatus = $params['perItemStatus'] ??
false;
122 // CAS-style checks are done on the _deleted fields so the select
123 // does not need to use FOR UPDATE nor be in the atomic section
124 $dbw = wfGetDB( DB_MASTER
);
125 $this->res
= $this->doQuery( $dbw );
127 $status->merge( $this->acquireItemLocks() );
128 if ( !$status->isGood() ) {
132 $dbw->startAtomic( __METHOD__
);
133 $dbw->onTransactionResolution(
135 // Release locks on commit or error
136 $this->releaseItemLocks();
141 $missing = array_flip( $this->ids
);
142 $this->clearFileOps();
146 if ( $perItemStatus ) {
147 $status->itemStatuses
= [];
150 // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
151 // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
152 // form does not let the same field get hidden and unhidden in different items at once.
157 // Will be filled with id => [old, new bits] information and
158 // passed to doPostCommitUpdates().
159 $visibilityChangeMap = [];
161 /** @var RevDelItem $item */
162 foreach ( $this as $item ) {
163 unset( $missing[$item->getId()] );
165 if ( $perItemStatus ) {
166 $itemStatus = Status
::newGood();
167 $status->itemStatuses
[$item->getId()] = $itemStatus;
169 $itemStatus = $status;
172 $oldBits = $item->getBits();
173 // Build the actual new rev_deleted bitfield
174 $newBits = RevisionDeleter
::extractBitfield( $bitPars, $oldBits );
176 if ( $oldBits == $newBits ) {
177 $itemStatus->warning(
178 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
179 $status->failCount++
;
181 } elseif ( $oldBits == 0 && $newBits != 0 ) {
183 } elseif ( $oldBits != 0 && $newBits == 0 ) {
189 if ( $item->isHideCurrentOp( $newBits ) ) {
190 // Cannot hide current version text
192 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
193 $status->failCount++
;
195 } elseif ( !$item->canView() ) {
196 // Cannot access this revision
197 $msg = ( $opType == 'show' ) ?
198 'revdelete-show-no-access' : 'revdelete-modify-no-access';
199 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
200 $status->failCount++
;
202 // Cannot just "hide from Sysops" without hiding any fields
203 } elseif ( $newBits == RevisionRecord
::DELETED_RESTRICTED
) {
204 $itemStatus->warning(
205 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
206 $status->failCount++
;
210 // Update the revision
211 $ok = $item->setBits( $newBits );
214 $idsForLog[] = $item->getId();
215 // If any item field was suppressed or unsuppressed
216 if ( ( $oldBits |
$newBits ) & $this->getSuppressBit() ) {
217 $logType = 'suppress';
219 // Track which fields where (un)hidden for each item
220 $addedBits = ( $oldBits ^
$newBits ) & $newBits;
221 $removedBits = ( $oldBits ^
$newBits ) & $oldBits;
222 $virtualNewBits |
= $addedBits;
223 $virtualOldBits |
= $removedBits;
225 $status->successCount++
;
226 $authorActors[] = $item->getAuthorActor();
228 // Save the old and new bits in $visibilityChangeMap for
230 $visibilityChangeMap[$item->getId()] = [
231 'oldBits' => $oldBits,
232 'newBits' => $newBits,
236 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
237 $status->failCount++
;
241 // Handle missing revisions
242 foreach ( $missing as $id => $unused ) {
243 if ( $perItemStatus ) {
244 $status->itemStatuses
[$id] = Status
::newFatal( 'revdelete-modify-missing', $id );
246 $status->error( 'revdelete-modify-missing', $id );
248 $status->failCount++
;
251 if ( $status->successCount
== 0 ) {
252 $dbw->endAtomic( __METHOD__
);
256 // Save success count
257 $successCount = $status->successCount
;
259 // Move files, if there are any
260 $status->merge( $this->doPreCommitUpdates() );
261 if ( !$status->isOK() ) {
262 // Fatal error, such as no configured archive directory or I/O failures
263 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
264 $lbFactory->rollbackMasterChanges( __METHOD__
);
270 $authorFields['authorActors'] = $authorActors;
274 'title' => $this->title
,
275 'count' => $successCount,
276 'newBits' => $virtualNewBits,
277 'oldBits' => $virtualOldBits,
278 'comment' => $comment,
280 'tags' => $params['tags'] ??
[],
284 // Clear caches after commit
285 DeferredUpdates
::addCallableUpdate(
286 function () use ( $visibilityChangeMap ) {
287 $this->doPostCommitUpdates( $visibilityChangeMap );
289 DeferredUpdates
::PRESEND
,
293 $dbw->endAtomic( __METHOD__
);
298 final protected function acquireItemLocks() {
299 $status = Status
::newGood();
300 /** @var RevDelItem $item */
301 foreach ( $this as $item ) {
302 $status->merge( $item->lock() );
308 final protected function releaseItemLocks() {
309 $status = Status
::newGood();
310 /** @var RevDelItem $item */
311 foreach ( $this as $item ) {
312 $status->merge( $item->unlock() );
319 * Reload the list data from the master DB. This can be done after setVisibility()
320 * to allow $item->getHTML() to show the new data.
322 function reloadFromMaster() {
323 $dbw = wfGetDB( DB_MASTER
);
324 $this->res
= $this->doQuery( $dbw );
328 * Record a log entry on the action
329 * @param string $logType One of (delete,suppress)
330 * @param array $params Associative array of parameters:
331 * newBits: The new value of the *_deleted bitfield
332 * oldBits: The old value of the *_deleted bitfield.
333 * title: The target title
335 * comment: The log comment
336 * authorActors: The array of the actor IDs of the offenders
337 * tags: The array of change tags to apply to the log entry
338 * @throws MWException
340 private function updateLog( $logType, $params ) {
341 // Get the URL param's corresponding DB field
342 $field = RevisionDeleter
::getRelationType( $this->getType() );
344 throw new MWException( "Bad log URL param type!" );
346 // Add params for affected page and ids
347 $logParams = $this->getLogParams( $params );
348 // Actually add the deletion log entry
349 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
350 $logEntry->setTarget( $params['title'] );
351 $logEntry->setComment( $params['comment'] );
352 $logEntry->setParameters( $logParams );
353 $logEntry->setPerformer( $this->getUser() );
354 // Allow for easy searching of deletion log items for revision/log items
356 $field => $params['ids'],
358 if ( isset( $params['authorActors'] ) ) {
360 'target_author_actor' => $params['authorActors'],
363 $logEntry->setRelations( $relations );
364 // Apply change tags to the log entry
365 $logEntry->addTags( $params['tags'] );
366 $logId = $logEntry->insert();
367 $logEntry->publish( $logId );
371 * Get the log action for this list type
374 public function getLogAction() {
379 * Get log parameter array.
380 * @param array $params Associative array of log parameters, same as updateLog()
383 public function getLogParams( $params ) {
385 '4::type' => $this->getType(),
386 '5::ids' => $params['ids'],
387 '6::ofield' => $params['oldBits'],
388 '7::nfield' => $params['newBits'],
393 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
396 public function clearFileOps() {
400 * A hook for setVisibility(): do batch updates pre-commit.
404 public function doPreCommitUpdates() {
405 return Status
::newGood();
409 * A hook for setVisibility(): do any necessary updates post-commit.
411 * @param array $visibilityChangeMap [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
414 public function doPostCommitUpdates( array $visibilityChangeMap ) {
415 return Status
::newGood();
419 * Get the integer value of the flag used for suppression
421 abstract public function getSuppressBit();