MediaWiki  master
RevDelList.php
Go to the documentation of this file.
1 <?php
24 
36 abstract class RevDelList extends RevisionListBase {
37 
39  private $lbFactory;
40 
47  public function __construct(
49  Title $title,
50  array $ids,
52  ) {
53  parent::__construct( $context, $title );
54 
55  // ids is a protected variable in RevisionListBase
56  $this->ids = $ids;
57  $this->lbFactory = $lbFactory;
58  }
59 
66  public static function getRelationType() {
67  return null;
68  }
69 
76  public static function getRestriction() {
77  return null;
78  }
79 
86  public static function getRevdelConstant() {
87  return null;
88  }
89 
98  public static function suggestTarget( $target, array $ids ) {
99  return $target;
100  }
101 
107  public function areAnySuppressed() {
108  $bit = $this->getSuppressBit();
109 
111  foreach ( $this as $item ) {
112  if ( $item->getBits() & $bit ) {
113  return true;
114  }
115  }
116 
117  return false;
118  }
119 
132  public function setVisibility( array $params ) {
133  $status = Status::newGood();
134 
135  $bitPars = $params['value'];
136  $comment = $params['comment'];
137  $perItemStatus = $params['perItemStatus'] ?? false;
138 
139  // CAS-style checks are done on the _deleted fields so the select
140  // does not need to use FOR UPDATE nor be in the atomic section
141  $dbw = $this->lbFactory->getMainLB()->getConnectionRef( DB_MASTER );
142  $this->res = $this->doQuery( $dbw );
143 
144  $status->merge( $this->acquireItemLocks() );
145  if ( !$status->isGood() ) {
146  return $status;
147  }
148 
149  $dbw->startAtomic( __METHOD__ );
150  $dbw->onTransactionResolution(
151  function () {
152  // Release locks on commit or error
153  $this->releaseItemLocks();
154  },
155  __METHOD__
156  );
157 
158  $missing = array_flip( $this->ids );
159  $this->clearFileOps();
160  $idsForLog = [];
161  $authorActors = [];
162 
163  if ( $perItemStatus ) {
164  $status->itemStatuses = [];
165  }
166 
167  // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
168  // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
169  // form does not let the same field get hidden and unhidden in different items at once.
170  $virtualOldBits = 0;
171  $virtualNewBits = 0;
172  $logType = 'delete';
173 
174  // Will be filled with id => [old, new bits] information and
175  // passed to doPostCommitUpdates().
176  $visibilityChangeMap = [];
177 
179  foreach ( $this as $item ) {
180  unset( $missing[$item->getId()] );
181 
182  if ( $perItemStatus ) {
183  $itemStatus = Status::newGood();
184  $status->itemStatuses[$item->getId()] = $itemStatus;
185  } else {
186  $itemStatus = $status;
187  }
188 
189  $oldBits = $item->getBits();
190  // Build the actual new rev_deleted bitfield
191  $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
192 
193  if ( $oldBits == $newBits ) {
194  $itemStatus->warning(
195  'revdelete-no-change', $item->formatDate(), $item->formatTime() );
196  $status->failCount++;
197  continue;
198  } elseif ( $oldBits == 0 && $newBits != 0 ) {
199  $opType = 'hide';
200  } elseif ( $oldBits != 0 && $newBits == 0 ) {
201  $opType = 'show';
202  } else {
203  $opType = 'modify';
204  }
205 
206  if ( $item->isHideCurrentOp( $newBits ) ) {
207  // Cannot hide current version text
208  $itemStatus->error(
209  'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
210  $status->failCount++;
211  continue;
212  } elseif ( !$item->canView() ) {
213  // Cannot access this revision
214  $msg = ( $opType == 'show' ) ?
215  'revdelete-show-no-access' : 'revdelete-modify-no-access';
216  $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
217  $status->failCount++;
218  continue;
219  // Cannot just "hide from Sysops" without hiding any fields
220  } elseif ( $newBits == RevisionRecord::DELETED_RESTRICTED ) {
221  $itemStatus->warning(
222  'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
223  $status->failCount++;
224  continue;
225  }
226 
227  // Update the revision
228  $ok = $item->setBits( $newBits );
229 
230  if ( $ok ) {
231  $idsForLog[] = $item->getId();
232  // If any item field was suppressed or unsuppressed
233  if ( ( $oldBits | $newBits ) & $this->getSuppressBit() ) {
234  $logType = 'suppress';
235  }
236  // Track which fields where (un)hidden for each item
237  $addedBits = ( $oldBits ^ $newBits ) & $newBits;
238  $removedBits = ( $oldBits ^ $newBits ) & $oldBits;
239  $virtualNewBits |= $addedBits;
240  $virtualOldBits |= $removedBits;
241 
242  $status->successCount++;
243  $authorActors[] = $item->getAuthorActor();
244 
245  // Save the old and new bits in $visibilityChangeMap for
246  // later use.
247  $visibilityChangeMap[$item->getId()] = [
248  'oldBits' => $oldBits,
249  'newBits' => $newBits,
250  ];
251  } else {
252  $itemStatus->error(
253  'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
254  $status->failCount++;
255  }
256  }
257 
258  // Handle missing revisions
259  foreach ( $missing as $id => $unused ) {
260  if ( $perItemStatus ) {
261  $status->itemStatuses[$id] = Status::newFatal( 'revdelete-modify-missing', $id );
262  } else {
263  $status->error( 'revdelete-modify-missing', $id );
264  }
265  $status->failCount++;
266  }
267 
268  if ( $status->successCount == 0 ) {
269  $dbw->endAtomic( __METHOD__ );
270  return $status;
271  }
272 
273  // Save success count
274  $successCount = $status->successCount;
275 
276  // Move files, if there are any
277  $status->merge( $this->doPreCommitUpdates() );
278  if ( !$status->isOK() ) {
279  // Fatal error, such as no configured archive directory or I/O failures
280  $this->lbFactory->rollbackMasterChanges( __METHOD__ );
281  return $status;
282  }
283 
284  // Log it
285  $authorFields = [];
286  $authorFields['authorActors'] = $authorActors;
287  $this->updateLog(
288  $logType,
289  [
290  'title' => $this->title,
291  'count' => $successCount,
292  'newBits' => $virtualNewBits,
293  'oldBits' => $virtualOldBits,
294  'comment' => $comment,
295  'ids' => $idsForLog,
296  'tags' => $params['tags'] ?? [],
297  ] + $authorFields
298  );
299 
300  // Clear caches after commit
302  function () use ( $visibilityChangeMap ) {
303  $this->doPostCommitUpdates( $visibilityChangeMap );
304  },
305  DeferredUpdates::PRESEND,
306  $dbw
307  );
308 
309  $dbw->endAtomic( __METHOD__ );
310 
311  return $status;
312  }
313 
314  final protected function acquireItemLocks() {
315  $status = Status::newGood();
317  foreach ( $this as $item ) {
318  $status->merge( $item->lock() );
319  }
320 
321  return $status;
322  }
323 
324  final protected function releaseItemLocks() {
325  $status = Status::newGood();
327  foreach ( $this as $item ) {
328  $status->merge( $item->unlock() );
329  }
330 
331  return $status;
332  }
333 
338  public function reloadFromMaster() {
339  $dbw = $this->lbFactory->getMainLB()->getConnectionRef( DB_MASTER );
340  $this->res = $this->doQuery( $dbw );
341  }
342 
356  private function updateLog( $logType, $params ) {
357  // Get the URL param's corresponding DB field
358  $field = RevisionDeleter::getRelationType( $this->getType() );
359  if ( !$field ) {
360  throw new MWException( "Bad log URL param type!" );
361  }
362  // Add params for affected page and ids
363  $logParams = $this->getLogParams( $params );
364  // Actually add the deletion log entry
365  $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
366  $logEntry->setTarget( $params['title'] );
367  $logEntry->setComment( $params['comment'] );
368  $logEntry->setParameters( $logParams );
369  $logEntry->setPerformer( $this->getUser() );
370  // Allow for easy searching of deletion log items for revision/log items
371  $relations = [
372  $field => $params['ids'],
373  ];
374  if ( isset( $params['authorActors'] ) ) {
375  $relations += [
376  'target_author_actor' => $params['authorActors'],
377  ];
378  }
379  $logEntry->setRelations( $relations );
380  // Apply change tags to the log entry
381  $logEntry->addTags( $params['tags'] );
382  $logId = $logEntry->insert();
383  $logEntry->publish( $logId );
384  }
385 
390  public function getLogAction() {
391  return 'revision';
392  }
393 
399  public function getLogParams( $params ) {
400  return [
401  '4::type' => $this->getType(),
402  '5::ids' => $params['ids'],
403  '6::ofield' => $params['oldBits'],
404  '7::nfield' => $params['newBits'],
405  ];
406  }
407 
412  public function clearFileOps() {
413  }
414 
420  public function doPreCommitUpdates() {
421  return Status::newGood();
422  }
423 
430  public function doPostCommitUpdates( array $visibilityChangeMap ) {
431  return Status::newGood();
432  }
433 
437  abstract public function getSuppressBit();
438 }
RevDelList\getSuppressBit
getSuppressBit()
Get the integer value of the flag used for suppression.
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:38
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
RevisionListBase
List for revision table items for a single page.
Definition: RevisionListBase.php:29
RevDelList\clearFileOps
clearFileOps()
Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates() STUB.
Definition: RevDelList.php:412
RevDelList\updateLog
updateLog( $logType, $params)
Record a log entry on the action.
Definition: RevDelList.php:356
RevDelList\getLogParams
getLogParams( $params)
Get log parameter array.
Definition: RevDelList.php:399
RevDelList\reloadFromMaster
reloadFromMaster()
Reload the list data from the master DB.
Definition: RevDelList.php:338
ContextSource\getUser
getUser()
Definition: ContextSource.php:135
RevDelList\doPreCommitUpdates
doPreCommitUpdates()
A hook for setVisibility(): do batch updates pre-commit.
Definition: RevDelList.php:420
RevDelList\releaseItemLocks
releaseItemLocks()
Definition: RevDelList.php:324
RevDelList\setVisibility
setVisibility(array $params)
Set the visibility for the revisions in this list.
Definition: RevDelList.php:132
MWException
MediaWiki exception.
Definition: MWException.php:29
RevisionListBase\doQuery
doQuery( $db)
Do the DB query to iterate through the objects.
RevDelList\getRevdelConstant
static getRevdelConstant()
Get the revision deletion constant for this list type Override this function.
Definition: RevDelList.php:86
RevDelList\$lbFactory
LBFactory $lbFactory
Definition: RevDelList.php:39
RevisionListBase\$title
Title $title
Definition: RevisionListBase.php:31
RevDelList
Definition: RevDelList.php:36
DB_MASTER
const DB_MASTER
Definition: defines.php:26
RevDelList\areAnySuppressed
areAnySuppressed()
Indicate whether any item in this list is suppressed.
Definition: RevDelList.php:107
RevDelList\getRelationType
static getRelationType()
Get the DB field name associated with the ID list.
Definition: RevDelList.php:66
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:82
RevDelList\getLogAction
getLogAction()
Get the log action for this list type.
Definition: RevDelList.php:390
RevisionListBase\$ids
int[] null $ids
Definition: RevisionListBase.php:34
RevDelList\getRestriction
static getRestriction()
Get the user right required for this list type Override this function.
Definition: RevDelList.php:76
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:57
RevDelList\acquireItemLocks
acquireItemLocks()
Definition: RevDelList.php:314
Title
Represents a title within MediaWiki.
Definition: Title.php:46
RevDelList\suggestTarget
static suggestTarget( $target, array $ids)
Suggest a target for the revision deletion Optionally override this function.
Definition: RevDelList.php:98
Wikimedia\Rdbms\LBFactory
An interface for generating database load balancers.
Definition: LBFactory.php:41
RevisionDeleter\getRelationType
static getRelationType( $typeName)
Get DB field name for URL param...
Definition: RevisionDeleter.php:210
RevisionListBase\getType
getType()
Get the internal type name of this list.
Definition: RevisionListBase.php:65
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:43
RevisionDeleter\extractBitfield
static extractBitfield(array $bitPars, $oldfield)
Put together a rev_deleted bitfield.
Definition: RevisionDeleter.php:273
RevDelList\doPostCommitUpdates
doPostCommitUpdates(array $visibilityChangeMap)
A hook for setVisibility(): do any necessary updates post-commit.
Definition: RevDelList.php:430
DeferredUpdates\addCallableUpdate
static addCallableUpdate( $callable, $stage=self::POSTSEND, $dbw=null)
Add an update to the pending update queue that invokes the specified callback when run.
Definition: DeferredUpdates.php:145
RevDelList\__construct
__construct(IContextSource $context, Title $title, array $ids, LBFactory $lbFactory)
Definition: RevDelList.php:47
RevDelItem
Abstract base class for deletable items.
Definition: RevDelItem.php:25