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\Context\IContextSource
;
23 use MediaWiki\Deferred\DeferredUpdates
;
24 use MediaWiki\Page\PageIdentity
;
25 use MediaWiki\Revision\RevisionRecord
;
26 use MediaWiki\RevisionList\RevisionListBase
;
27 use MediaWiki\Status\Status
;
28 use MediaWiki\Title\Title
;
29 use Wikimedia\Rdbms\LBFactory
;
32 * Abstract base class for a list of deletable items. The list class
33 * needs to be able to make a query from a set of identifiers to pull
34 * relevant rows, to return RevDelItem subclasses wrapping them, and
35 * to wrap bulk update operations.
37 * @property RevDelItem $current
38 * @method RevDelItem next()
39 * @method RevDelItem reset()
40 * @method RevDelItem current()
42 abstract class RevDelList
extends RevisionListBase
{
44 /** Flag used for suppression, depending on the type of log */
45 protected const SUPPRESS_BIT
= RevisionRecord
::DELETED_RESTRICTED
;
51 * @param IContextSource $context
52 * @param PageIdentity $page
54 * @param LBFactory $lbFactory
56 public function __construct(
57 IContextSource
$context,
62 parent
::__construct( $context, $page );
64 // ids is a protected variable in RevisionListBase
66 $this->lbFactory
= $lbFactory;
70 * Get the DB field name associated with the ID list.
71 * This used to populate the log_search table for finding log entries.
72 * Override this function.
75 public static function getRelationType() {
80 * Get the user right required for this list type
81 * Override this function.
85 public static function getRestriction() {
90 * Get the revision deletion constant for this list type
91 * Override this function.
95 public static function getRevdelConstant() {
100 * Suggest a target for the revision deletion
101 * Optionally override this function.
103 * @param Title|null $target User-supplied target
107 public static function suggestTarget( $target, array $ids ) {
112 * Indicate whether any item in this list is suppressed
116 public function areAnySuppressed() {
117 /** @var RevDelItem $item */
118 foreach ( $this as $item ) {
119 if ( $item->getBits() & self
::SUPPRESS_BIT
) {
128 * Set the visibility for the revisions in this list. Logging and
129 * transactions are done here.
131 * @param array $params Associative array of parameters. Members are:
132 * value: ExtractBitParams() bitfield array
133 * comment: The log comment
134 * perItemStatus: Set if you want per-item status reports
135 * tags: The array of change tags to apply to the log entry
137 * @since 1.23 Added 'perItemStatus' param
139 public function setVisibility( array $params ) {
140 $status = Status
::newGood();
142 $bitPars = $params['value'];
143 $comment = $params['comment'];
144 $perItemStatus = $params['perItemStatus'] ??
false;
146 // CAS-style checks are done on the _deleted fields so the select
147 // does not need to use FOR UPDATE nor be in the atomic section
148 $dbw = $this->lbFactory
->getPrimaryDatabase();
149 $this->res
= $this->doQuery( $dbw );
151 $status->merge( $this->acquireItemLocks() );
152 if ( !$status->isGood() ) {
156 $dbw->startAtomic( __METHOD__
, $dbw::ATOMIC_CANCELABLE
);
157 $dbw->onTransactionResolution(
159 // Release locks on commit or error
160 $this->releaseItemLocks();
165 $missing = array_fill_keys( $this->ids
, true );
166 $this->clearFileOps();
170 if ( $perItemStatus ) {
171 $status->value
['itemStatuses'] = [];
174 // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
175 // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
176 // form does not let the same field get hidden and unhidden in different items at once.
181 // Will be filled with id => [old, new bits] information and
182 // passed to doPostCommitUpdates().
183 $visibilityChangeMap = [];
185 /** @var RevDelItem $item */
186 foreach ( $this as $item ) {
187 unset( $missing[$item->getId()] );
189 if ( $perItemStatus ) {
190 $itemStatus = Status
::newGood();
191 $status->value
['itemStatuses'][$item->getId()] = $itemStatus;
193 $itemStatus = $status;
196 $oldBits = $item->getBits();
197 // Build the actual new rev_deleted bitfield
198 $newBits = RevisionDeleter
::extractBitfield( $bitPars, $oldBits );
200 if ( $oldBits == $newBits ) {
201 $itemStatus->warning(
202 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
203 $status->failCount++
;
205 } elseif ( $oldBits == 0 && $newBits != 0 ) {
207 } elseif ( $oldBits != 0 && $newBits == 0 ) {
213 if ( $item->isHideCurrentOp( $newBits ) ) {
214 // Cannot hide current version text
216 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
217 $status->failCount++
;
219 } elseif ( !$item->canView() ) {
220 // Cannot access this revision
221 $msg = ( $opType == 'show' ) ?
222 'revdelete-show-no-access' : 'revdelete-modify-no-access';
223 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
224 $status->failCount++
;
226 // Cannot just "hide from Sysops" without hiding any fields
227 } elseif ( $newBits == self
::SUPPRESS_BIT
) {
228 $itemStatus->warning(
229 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
230 $status->failCount++
;
234 // Update the revision
235 $ok = $item->setBits( $newBits );
238 $idsForLog[] = $item->getId();
239 // If any item field was suppressed or unsuppressed
240 if ( ( $oldBits |
$newBits ) & self
::SUPPRESS_BIT
) {
241 $logType = 'suppress';
243 // Track which fields where (un)hidden for each item
244 $addedBits = ( $oldBits ^
$newBits ) & $newBits;
245 $removedBits = ( $oldBits ^
$newBits ) & $oldBits;
246 $virtualNewBits |
= $addedBits;
247 $virtualOldBits |
= $removedBits;
249 $status->successCount++
;
250 $authorActors[] = $item->getAuthorActor();
252 // Save the old and new bits in $visibilityChangeMap for
254 $visibilityChangeMap[$item->getId()] = [
255 'oldBits' => $oldBits,
256 'newBits' => $newBits,
260 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
261 $status->failCount++
;
265 // Handle missing revisions
266 foreach ( $missing as $id => $unused ) {
267 if ( $perItemStatus ) {
268 $status->value
['itemStatuses'][$id] = Status
::newFatal( 'revdelete-modify-missing', $id );
270 $status->error( 'revdelete-modify-missing', $id );
272 $status->failCount++
;
275 if ( $status->successCount
== 0 ) {
276 $dbw->endAtomic( __METHOD__
);
280 // Save success count
281 $successCount = $status->successCount
;
283 // Move files, if there are any
284 $status->merge( $this->doPreCommitUpdates() );
285 if ( !$status->isOK() ) {
286 // Fatal error, such as no configured archive directory or I/O failures
287 $dbw->cancelAtomic( __METHOD__
);
293 $authorFields['authorActors'] = $authorActors;
297 'page' => $this->page
,
298 'count' => $successCount,
299 'newBits' => $virtualNewBits,
300 'oldBits' => $virtualOldBits,
301 'comment' => $comment,
303 'tags' => $params['tags'] ??
[],
307 // Clear caches after commit
308 DeferredUpdates
::addCallableUpdate(
309 function () use ( $visibilityChangeMap ) {
310 $this->doPostCommitUpdates( $visibilityChangeMap );
312 DeferredUpdates
::PRESEND
,
316 $dbw->endAtomic( __METHOD__
);
321 final protected function acquireItemLocks() {
322 $status = Status
::newGood();
323 /** @var RevDelItem $item */
324 foreach ( $this as $item ) {
325 $status->merge( $item->lock() );
331 final protected function releaseItemLocks() {
332 $status = Status
::newGood();
333 /** @var RevDelItem $item */
334 foreach ( $this as $item ) {
335 $status->merge( $item->unlock() );
342 * Reload the list data from the primary DB. This can be done after setVisibility()
343 * to allow $item->getHTML() to show the new data.
346 public function reloadFromPrimary() {
347 $dbw = $this->lbFactory
->getPrimaryDatabase();
348 $this->res
= $this->doQuery( $dbw );
352 * Record a log entry on the action
353 * @param string $logType One of (delete,suppress)
354 * @param array $params Associative array of parameters:
355 * newBits: The new value of the *_deleted bitfield
356 * oldBits: The old value of the *_deleted bitfield.
357 * page: The target page reference
359 * comment: The log comment
360 * authorActors: The array of the actor IDs of the offenders
361 * tags: The array of change tags to apply to the log entry
363 private function updateLog( $logType, $params ) {
364 // Get the URL param's corresponding DB field
365 $field = RevisionDeleter
::getRelationType( $this->getType() );
367 throw new UnexpectedValueException( "Bad log URL param type!" );
369 // Add params for affected page and ids
370 $logParams = $this->getLogParams( $params );
371 // Actually add the deletion log entry
372 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
373 $logEntry->setTarget( $params['page'] );
374 $logEntry->setComment( $params['comment'] );
375 $logEntry->setParameters( $logParams );
376 $logEntry->setPerformer( $this->getUser() );
377 // Allow for easy searching of deletion log items for revision/log items
379 $field => $params['ids'],
381 if ( isset( $params['authorActors'] ) ) {
383 'target_author_actor' => $params['authorActors'],
386 $logEntry->setRelations( $relations );
387 // Apply change tags to the log entry
388 $logEntry->addTags( $params['tags'] );
389 $logId = $logEntry->insert();
390 $logEntry->publish( $logId );
394 * Get the log action for this list type
397 public function getLogAction() {
402 * Get log parameter array.
403 * @param array $params Associative array of log parameters, same as updateLog()
406 public function getLogParams( $params ) {
408 '4::type' => $this->getType(),
409 '5::ids' => $params['ids'],
410 '6::ofield' => $params['oldBits'],
411 '7::nfield' => $params['newBits'],
416 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
419 public function clearFileOps() {
423 * A hook for setVisibility(): do batch updates pre-commit.
427 public function doPreCommitUpdates() {
428 return Status
::newGood();
432 * A hook for setVisibility(): do any necessary updates post-commit.
434 * @param array $visibilityChangeMap [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
437 public function doPostCommitUpdates( array $visibilityChangeMap ) {
438 return Status
::newGood();