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(
125 // Release locks on commit or error
126 $this->releaseItemLocks();
131 $missing = array_flip( $this->ids
);
132 $this->clearFileOps();
134 $authorIds = $authorIPs = [];
136 if ( $perItemStatus ) {
137 $status->itemStatuses
= [];
140 // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
141 // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
142 // form does not let the same field get hidden and unhidden in different items at once.
147 // Will be filled with id => [old, new bits] information and
148 // passed to doPostCommitUpdates().
149 $visibilityChangeMap = [];
151 /** @var $item RevDelItem */
152 foreach ( $this as $item ) {
153 unset( $missing[$item->getId()] );
155 if ( $perItemStatus ) {
156 $itemStatus = Status
::newGood();
157 $status->itemStatuses
[$item->getId()] = $itemStatus;
159 $itemStatus = $status;
162 $oldBits = $item->getBits();
163 // Build the actual new rev_deleted bitfield
164 $newBits = RevisionDeleter
::extractBitfield( $bitPars, $oldBits );
166 if ( $oldBits == $newBits ) {
167 $itemStatus->warning(
168 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
169 $status->failCount++
;
171 } elseif ( $oldBits == 0 && $newBits != 0 ) {
173 } elseif ( $oldBits != 0 && $newBits == 0 ) {
179 if ( $item->isHideCurrentOp( $newBits ) ) {
180 // Cannot hide current version text
182 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
183 $status->failCount++
;
185 } elseif ( !$item->canView() ) {
186 // Cannot access this revision
187 $msg = ( $opType == 'show' ) ?
188 'revdelete-show-no-access' : 'revdelete-modify-no-access';
189 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
190 $status->failCount++
;
192 // Cannot just "hide from Sysops" without hiding any fields
193 } elseif ( $newBits == Revision
::DELETED_RESTRICTED
) {
194 $itemStatus->warning(
195 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
196 $status->failCount++
;
200 // Update the revision
201 $ok = $item->setBits( $newBits );
204 $idsForLog[] = $item->getId();
205 // If any item field was suppressed or unsupressed
206 if ( ( $oldBits |
$newBits ) & $this->getSuppressBit() ) {
207 $logType = 'suppress';
209 // Track which fields where (un)hidden for each item
210 $addedBits = ( $oldBits ^
$newBits ) & $newBits;
211 $removedBits = ( $oldBits ^
$newBits ) & $oldBits;
212 $virtualNewBits |
= $addedBits;
213 $virtualOldBits |
= $removedBits;
215 $status->successCount++
;
216 if ( $item->getAuthorId() > 0 ) {
217 $authorIds[] = $item->getAuthorId();
218 } elseif ( IP
::isIPAddress( $item->getAuthorName() ) ) {
219 $authorIPs[] = $item->getAuthorName();
222 // Save the old and new bits in $visibilityChangeMap for
224 $visibilityChangeMap[$item->getId()] = [
225 'oldBits' => $oldBits,
226 'newBits' => $newBits,
230 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
231 $status->failCount++
;
235 // Handle missing revisions
236 foreach ( $missing as $id => $unused ) {
237 if ( $perItemStatus ) {
238 $status->itemStatuses
[$id] = Status
::newFatal( 'revdelete-modify-missing', $id );
240 $status->error( 'revdelete-modify-missing', $id );
242 $status->failCount++
;
245 if ( $status->successCount
== 0 ) {
246 $dbw->endAtomic( __METHOD__
);
250 // Save success count
251 $successCount = $status->successCount
;
253 // Move files, if there are any
254 $status->merge( $this->doPreCommitUpdates() );
255 if ( !$status->isOK() ) {
256 // Fatal error, such as no configured archive directory or I/O failures
257 wfGetLBFactory()->rollbackMasterChanges( __METHOD__
);
265 'title' => $this->title
,
266 'count' => $successCount,
267 'newBits' => $virtualNewBits,
268 'oldBits' => $virtualOldBits,
269 'comment' => $comment,
271 'authorIds' => $authorIds,
272 'authorIPs' => $authorIPs
276 // Clear caches after commit
277 DeferredUpdates
::addCallableUpdate(
278 function () use ( $visibilityChangeMap ) {
279 $this->doPostCommitUpdates( $visibilityChangeMap );
281 DeferredUpdates
::PRESEND
,
285 $dbw->endAtomic( __METHOD__
);
290 final protected function acquireItemLocks() {
291 $status = Status
::newGood();
292 /** @var $item RevDelItem */
293 foreach ( $this as $item ) {
294 $status->merge( $item->lock() );
300 final protected function releaseItemLocks() {
301 $status = Status
::newGood();
302 /** @var $item RevDelItem */
303 foreach ( $this as $item ) {
304 $status->merge( $item->unlock() );
311 * Reload the list data from the master DB. This can be done after setVisibility()
312 * to allow $item->getHTML() to show the new data.
314 function reloadFromMaster() {
315 $dbw = wfGetDB( DB_MASTER
);
316 $this->res
= $this->doQuery( $dbw );
320 * Record a log entry on the action
321 * @param string $logType One of (delete,suppress)
322 * @param array $params Associative array of parameters:
323 * newBits: The new value of the *_deleted bitfield
324 * oldBits: The old value of the *_deleted bitfield.
325 * title: The target title
327 * comment: The log comment
328 * authorsIds: The array of the user IDs of the offenders
329 * authorsIPs: The array of the IP/anon user offenders
330 * @throws MWException
332 private function updateLog( $logType, $params ) {
333 // Get the URL param's corresponding DB field
334 $field = RevisionDeleter
::getRelationType( $this->getType() );
336 throw new MWException( "Bad log URL param type!" );
338 // Add params for affected page and ids
339 $logParams = $this->getLogParams( $params );
340 // Actually add the deletion log entry
341 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
342 $logEntry->setTarget( $params['title'] );
343 $logEntry->setComment( $params['comment'] );
344 $logEntry->setParameters( $logParams );
345 $logEntry->setPerformer( $this->getUser() );
346 // Allow for easy searching of deletion log items for revision/log items
347 $logEntry->setRelations( [
348 $field => $params['ids'],
349 'target_author_id' => $params['authorIds'],
350 'target_author_ip' => $params['authorIPs'],
352 $logId = $logEntry->insert();
353 $logEntry->publish( $logId );
357 * Get the log action for this list type
360 public function getLogAction() {
365 * Get log parameter array.
366 * @param array $params Associative array of log parameters, same as updateLog()
369 public function getLogParams( $params ) {
371 '4::type' => $this->getType(),
372 '5::ids' => $params['ids'],
373 '6::ofield' => $params['oldBits'],
374 '7::nfield' => $params['newBits'],
379 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
382 public function clearFileOps() {
386 * A hook for setVisibility(): do batch updates pre-commit.
390 public function doPreCommitUpdates() {
391 return Status
::newGood();
395 * A hook for setVisibility(): do any necessary updates post-commit.
397 * @param array [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
400 public function doPostCommitUpdates( array $visibilityChangeMap ) {
401 return Status
::newGood();
405 * Get the integer value of the flag used for suppression
407 abstract public function getSuppressBit();