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
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.
28 abstract class RevDelList
extends RevisionListBase
{
29 function __construct( IContextSource
$context, Title
$title, array $ids ) {
30 parent
::__construct( $context, $title );
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.
40 public static function getRelationType() {
45 * Get the user right required for this list type
46 * Override this function.
50 public static function getRestriction() {
55 * Get the revision deletion constant for this list type
56 * Override this function.
60 public static function getRevdelConstant() {
65 * Suggest a target for the revision deletion
66 * Optionally override this function.
68 * @param Title|null $target User-supplied target
72 public static function suggestTarget( $target, array $ids ) {
77 * Indicate whether any item in this list is suppressed
81 public function areAnySuppressed() {
82 $bit = $this->getSuppressBit();
84 /** @var $item RevDelItem */
85 foreach ( $this as $item ) {
86 if ( $item->getBits() & $bit ) {
95 * Set the visibility for the revisions in this list. Logging and
96 * transactions are done here.
98 * @param array $params Associative array of parameters. Members are:
99 * value: ExtractBitParams() bitfield array
100 * comment: The log comment.
101 * perItemStatus: Set if you want per-item status reports
103 * @since 1.23 Added 'perItemStatus' param
105 public function setVisibility( array $params ) {
106 $status = Status
::newGood();
108 $bitPars = $params['value'];
109 $comment = $params['comment'];
110 $perItemStatus = isset( $params['perItemStatus'] ) ?
$params['perItemStatus'] : false;
112 // CAS-style checks are done on the _deleted fields so the select
113 // does not need to use FOR UPDATE nor be in the atomic section
114 $dbw = wfGetDB( DB_MASTER
);
115 $this->res
= $this->doQuery( $dbw );
117 $status->merge( $this->acquireItemLocks() );
118 if ( !$status->isGood() ) {
122 $dbw->startAtomic( __METHOD__
);
123 $dbw->onTransactionResolution( function () {
124 // Release locks on commit or error
125 $this->releaseItemLocks();
128 $missing = array_flip( $this->ids
);
129 $this->clearFileOps();
131 $authorIds = $authorIPs = [];
133 if ( $perItemStatus ) {
134 $status->itemStatuses
= [];
137 // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
138 // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
139 // form does not let the same field get hidden and unhidden in different items at once.
144 // Will be filled with id => [old, new bits] information and
145 // passed to doPostCommitUpdates().
146 $visibilityChangeMap = [];
148 /** @var $item RevDelItem */
149 foreach ( $this as $item ) {
150 unset( $missing[$item->getId()] );
152 if ( $perItemStatus ) {
153 $itemStatus = Status
::newGood();
154 $status->itemStatuses
[$item->getId()] = $itemStatus;
156 $itemStatus = $status;
159 $oldBits = $item->getBits();
160 // Build the actual new rev_deleted bitfield
161 $newBits = RevisionDeleter
::extractBitfield( $bitPars, $oldBits );
163 if ( $oldBits == $newBits ) {
164 $itemStatus->warning(
165 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
166 $status->failCount++
;
168 } elseif ( $oldBits == 0 && $newBits != 0 ) {
170 } elseif ( $oldBits != 0 && $newBits == 0 ) {
176 if ( $item->isHideCurrentOp( $newBits ) ) {
177 // Cannot hide current version text
179 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
180 $status->failCount++
;
182 } elseif ( !$item->canView() ) {
183 // Cannot access this revision
184 $msg = ( $opType == 'show' ) ?
185 'revdelete-show-no-access' : 'revdelete-modify-no-access';
186 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
187 $status->failCount++
;
189 // Cannot just "hide from Sysops" without hiding any fields
190 } elseif ( $newBits == Revision
::DELETED_RESTRICTED
) {
191 $itemStatus->warning(
192 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
193 $status->failCount++
;
197 // Update the revision
198 $ok = $item->setBits( $newBits );
201 $idsForLog[] = $item->getId();
202 // If any item field was suppressed or unsupressed
203 if ( ( $oldBits |
$newBits ) & $this->getSuppressBit() ) {
204 $logType = 'suppress';
206 // Track which fields where (un)hidden for each item
207 $addedBits = ( $oldBits ^
$newBits ) & $newBits;
208 $removedBits = ( $oldBits ^
$newBits ) & $oldBits;
209 $virtualNewBits |
= $addedBits;
210 $virtualOldBits |
= $removedBits;
212 $status->successCount++
;
213 if ( $item->getAuthorId() > 0 ) {
214 $authorIds[] = $item->getAuthorId();
215 } elseif ( IP
::isIPAddress( $item->getAuthorName() ) ) {
216 $authorIPs[] = $item->getAuthorName();
219 // Save the old and new bits in $visibilityChangeMap for
221 $visibilityChangeMap[$item->getId()] = [
222 'oldBits' => $oldBits,
223 'newBits' => $newBits,
227 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
228 $status->failCount++
;
232 // Handle missing revisions
233 foreach ( $missing as $id => $unused ) {
234 if ( $perItemStatus ) {
235 $status->itemStatuses
[$id] = Status
::newFatal( 'revdelete-modify-missing', $id );
237 $status->error( 'revdelete-modify-missing', $id );
239 $status->failCount++
;
242 if ( $status->successCount
== 0 ) {
243 $dbw->endAtomic( __METHOD__
);
247 // Save success count
248 $successCount = $status->successCount
;
250 // Move files, if there are any
251 $status->merge( $this->doPreCommitUpdates() );
252 if ( !$status->isOK() ) {
253 // Fatal error, such as no configured archive directory or I/O failures
254 wfGetLBFactory()->rollbackMasterChanges( __METHOD__
);
262 'title' => $this->title
,
263 'count' => $successCount,
264 'newBits' => $virtualNewBits,
265 'oldBits' => $virtualOldBits,
266 'comment' => $comment,
268 'authorIds' => $authorIds,
269 'authorIPs' => $authorIPs
273 // Clear caches after commit
274 DeferredUpdates
::addCallableUpdate(
275 function () use ( $visibilityChangeMap ) {
276 $this->doPostCommitUpdates( $visibilityChangeMap );
278 DeferredUpdates
::PRESEND
,
282 $dbw->endAtomic( __METHOD__
);
287 final protected function acquireItemLocks() {
288 $status = Status
::newGood();
289 /** @var $item RevDelItem */
290 foreach ( $this as $item ) {
291 $status->merge( $item->lock() );
297 final protected function releaseItemLocks() {
298 $status = Status
::newGood();
299 /** @var $item RevDelItem */
300 foreach ( $this as $item ) {
301 $status->merge( $item->unlock() );
308 * Reload the list data from the master DB. This can be done after setVisibility()
309 * to allow $item->getHTML() to show the new data.
311 function reloadFromMaster() {
312 $dbw = wfGetDB( DB_MASTER
);
313 $this->res
= $this->doQuery( $dbw );
317 * Record a log entry on the action
318 * @param string $logType One of (delete,suppress)
319 * @param array $params Associative array of parameters:
320 * newBits: The new value of the *_deleted bitfield
321 * oldBits: The old value of the *_deleted bitfield.
322 * title: The target title
324 * comment: The log comment
325 * authorsIds: The array of the user IDs of the offenders
326 * authorsIPs: The array of the IP/anon user offenders
327 * @throws MWException
329 private function updateLog( $logType, $params ) {
330 // Get the URL param's corresponding DB field
331 $field = RevisionDeleter
::getRelationType( $this->getType() );
333 throw new MWException( "Bad log URL param type!" );
335 // Add params for affected page and ids
336 $logParams = $this->getLogParams( $params );
337 // Actually add the deletion log entry
338 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
339 $logEntry->setTarget( $params['title'] );
340 $logEntry->setComment( $params['comment'] );
341 $logEntry->setParameters( $logParams );
342 $logEntry->setPerformer( $this->getUser() );
343 // Allow for easy searching of deletion log items for revision/log items
344 $logEntry->setRelations( [
345 $field => $params['ids'],
346 'target_author_id' => $params['authorIds'],
347 'target_author_ip' => $params['authorIPs'],
349 $logId = $logEntry->insert();
350 $logEntry->publish( $logId );
354 * Get the log action for this list type
357 public function getLogAction() {
362 * Get log parameter array.
363 * @param array $params Associative array of log parameters, same as updateLog()
366 public function getLogParams( $params ) {
368 '4::type' => $this->getType(),
369 '5::ids' => $params['ids'],
370 '6::ofield' => $params['oldBits'],
371 '7::nfield' => $params['newBits'],
376 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
379 public function clearFileOps() {
383 * A hook for setVisibility(): do batch updates pre-commit.
387 public function doPreCommitUpdates() {
388 return Status
::newGood();
392 * A hook for setVisibility(): do any necessary updates post-commit.
394 * @param array [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
397 public function doPostCommitUpdates( array $visibilityChangeMap ) {
398 return Status
::newGood();
402 * Get the integer value of the flag used for suppression
404 abstract public function getSuppressBit();