Debian Codegraph @

includes/revisiondelete/RevDelList.php

raw symbols SCIP
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup RevisionDelete
20 */
21
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;
30
31 /**
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.
36 *
37 * @property RevDelItem $current
38 * @method RevDelItem next()
39 * @method RevDelItem reset()
40 * @method RevDelItem current()
41 */
42 abstract class RevDelList extends RevisionListBase {
43
44 /** Flag used for suppression, depending on the type of log */
45 protected const SUPPRESS_BIT = RevisionRecord::DELETED_RESTRICTED;
46
47 /** @var LBFactory */
48 private $lbFactory;
49
50 /**
51 * @param IContextSource $context
52 * @param PageIdentity $page
53 * @param array $ids
54 * @param LBFactory $lbFactory
55 */
56 public function __construct(
57 IContextSource $context,
58 PageIdentity $page,
59 array $ids,
60 LBFactory $lbFactory
61 ) {
62 parent::__construct( $context, $page );
63
64 // ids is a protected variable in RevisionListBase
65 $this->ids = $ids;
66 $this->lbFactory = $lbFactory;
67 }
68
69 /**
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.
73 * @return string|null
74 */
75 public static function getRelationType() {
76 return null;
77 }
78
79 /**
80 * Get the user right required for this list type
81 * Override this function.
82 * @since 1.22
83 * @return string|null
84 */
85 public static function getRestriction() {
86 return null;
87 }
88
89 /**
90 * Get the revision deletion constant for this list type
91 * Override this function.
92 * @since 1.22
93 * @return int|null
94 */
95 public static function getRevdelConstant() {
96 return null;
97 }
98
99 /**
100 * Suggest a target for the revision deletion
101 * Optionally override this function.
102 * @since 1.22
103 * @param Title|null $target User-supplied target
104 * @param array $ids
105 * @return Title|null
106 */
107 public static function suggestTarget( $target, array $ids ) {
108 return $target;
109 }
110
111 /**
112 * Indicate whether any item in this list is suppressed
113 * @since 1.25
114 * @return bool
115 */
116 public function areAnySuppressed() {
117 /** @var RevDelItem $item */
118 foreach ( $this as $item ) {
119 if ( $item->getBits() & self::SUPPRESS_BIT ) {
120 return true;
121 }
122 }
123
124 return false;
125 }
126
127 /**
128 * Set the visibility for the revisions in this list. Logging and
129 * transactions are done here.
130 *
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
136 * @return Status
137 * @since 1.23 Added 'perItemStatus' param
138 */
139 public function setVisibility( array $params ) {
140 $status = Status::newGood();
141
142 $bitPars = $params['value'];
143 $comment = $params['comment'];
144 $perItemStatus = $params['perItemStatus'] ?? false;
145
146 // T387638 - Always ensure ->value['itemStatuses'] is set if requested
147 if ( $perItemStatus ) {
148 $status->value['itemStatuses'] = [];
149 }
150
151 // CAS-style checks are done on the _deleted fields so the select
152 // does not need to use FOR UPDATE nor be in the atomic section
153 $dbw = $this->lbFactory->getPrimaryDatabase();
154 $this->res = $this->doQuery( $dbw );
155
156 $status->merge( $this->acquireItemLocks() );
157 if ( !$status->isGood() ) {
158 return $status;
159 }
160
161 $dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE );
162 $dbw->onTransactionResolution(
163 function () {
164 // Release locks on commit or error
165 $this->releaseItemLocks();
166 },
167 __METHOD__
168 );
169
170 $missing = array_fill_keys( $this->ids, true );
171 $this->clearFileOps();
172 $idsForLog = [];
173 $authorActors = [];
174
175 // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
176 // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
177 // form does not let the same field get hidden and unhidden in different items at once.
178 $virtualOldBits = 0;
179 $virtualNewBits = 0;
180 $logType = 'delete';
181
182 // Will be filled with id => [old, new bits] information and
183 // passed to doPostCommitUpdates().
184 $visibilityChangeMap = [];
185
186 /** @var RevDelItem $item */
187 foreach ( $this as $item ) {
188 unset( $missing[$item->getId()] );
189
190 if ( $perItemStatus ) {
191 $itemStatus = Status::newGood();
192 $status->value['itemStatuses'][$item->getId()] = $itemStatus;
193 } else {
194 $itemStatus = $status;
195 }
196
197 $oldBits = $item->getBits();
198 // Build the actual new rev_deleted bitfield
199 $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
200
201 if ( $oldBits == $newBits ) {
202 $itemStatus->warning(
203 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
204 $status->failCount++;
205 continue;
206 } elseif ( $oldBits == 0 && $newBits != 0 ) {
207 $opType = 'hide';
208 } elseif ( $oldBits != 0 && $newBits == 0 ) {
209 $opType = 'show';
210 } else {
211 $opType = 'modify';
212 }
213
214 if ( $item->isHideCurrentOp( $newBits ) ) {
215 // Cannot hide current version text
216 $itemStatus->error(
217 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
218 $status->failCount++;
219 continue;
220 } elseif ( !$item->canView() ) {
221 // Cannot access this revision
222 $msg = ( $opType == 'show' ) ?
223 'revdelete-show-no-access' : 'revdelete-modify-no-access';
224 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
225 $status->failCount++;
226 continue;
227 // Cannot just "hide from Sysops" without hiding any fields
228 } elseif ( $newBits == self::SUPPRESS_BIT ) {
229 $itemStatus->warning(
230 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
231 $status->failCount++;
232 continue;
233 }
234
235 // Update the revision
236 $ok = $item->setBits( $newBits );
237
238 if ( $ok ) {
239 $idsForLog[] = $item->getId();
240 // If any item field was suppressed or unsuppressed
241 if ( ( $oldBits | $newBits ) & self::SUPPRESS_BIT ) {
242 $logType = 'suppress';
243 }
244 // Track which fields where (un)hidden for each item
245 $addedBits = ( $oldBits ^ $newBits ) & $newBits;
246 $removedBits = ( $oldBits ^ $newBits ) & $oldBits;
247 $virtualNewBits |= $addedBits;
248 $virtualOldBits |= $removedBits;
249
250 $status->successCount++;
251 $authorActors[] = $item->getAuthorActor();
252
253 // Save the old and new bits in $visibilityChangeMap for
254 // later use.
255 $visibilityChangeMap[$item->getId()] = [
256 'oldBits' => $oldBits,
257 'newBits' => $newBits,
258 ];
259 } else {
260 $itemStatus->error(
261 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
262 $status->failCount++;
263 }
264 }
265
266 // Handle missing revisions
267 foreach ( $missing as $id => $unused ) {
268 if ( $perItemStatus ) {
269 $status->value['itemStatuses'][$id] = Status::newFatal( 'revdelete-modify-missing', $id );
270 } else {
271 $status->error( 'revdelete-modify-missing', $id );
272 }
273 $status->failCount++;
274 }
275
276 if ( $status->successCount == 0 ) {
277 $dbw->endAtomic( __METHOD__ );
278 return $status;
279 }
280
281 // Save success count
282 $successCount = $status->successCount;
283
284 // Move files, if there are any
285 $status->merge( $this->doPreCommitUpdates() );
286 if ( !$status->isOK() ) {
287 // Fatal error, such as no configured archive directory or I/O failures
288 $dbw->cancelAtomic( __METHOD__ );
289 return $status;
290 }
291
292 // Log it
293 $authorFields = [];
294 $authorFields['authorActors'] = $authorActors;
295 $this->updateLog(
296 $logType,
297 [
298 'page' => $this->page,
299 'count' => $successCount,
300 'newBits' => $virtualNewBits,
301 'oldBits' => $virtualOldBits,
302 'comment' => $comment,
303 'ids' => $idsForLog,
304 'tags' => $params['tags'] ?? [],
305 ] + $authorFields
306 );
307
308 // Clear caches after commit
309 DeferredUpdates::addCallableUpdate(
310 function () use ( $visibilityChangeMap ) {
311 $this->doPostCommitUpdates( $visibilityChangeMap );
312 },
313 DeferredUpdates::PRESEND,
314 $dbw
315 );
316
317 $dbw->endAtomic( __METHOD__ );
318
319 return $status;
320 }
321
322 final protected function acquireItemLocks() {
323 $status = Status::newGood();
324 /** @var RevDelItem $item */
325 foreach ( $this as $item ) {
326 $status->merge( $item->lock() );
327 }
328
329 return $status;
330 }
331
332 final protected function releaseItemLocks() {
333 $status = Status::newGood();
334 /** @var RevDelItem $item */
335 foreach ( $this as $item ) {
336 $status->merge( $item->unlock() );
337 }
338
339 return $status;
340 }
341
342 /**
343 * Reload the list data from the primary DB. This can be done after setVisibility()
344 * to allow $item->getHTML() to show the new data.
345 * @since 1.37
346 */
347 public function reloadFromPrimary() {
348 $dbw = $this->lbFactory->getPrimaryDatabase();
349 $this->res = $this->doQuery( $dbw );
350 }
351
352 /**
353 * Record a log entry on the action
354 * @param string $logType One of (delete,suppress)
355 * @param array $params Associative array of parameters:
356 * newBits: The new value of the *_deleted bitfield
357 * oldBits: The old value of the *_deleted bitfield.
358 * page: The target page reference
359 * ids: The ID list
360 * comment: The log comment
361 * authorActors: The array of the actor IDs of the offenders
362 * tags: The array of change tags to apply to the log entry
363 */
364 private function updateLog( $logType, $params ) {
365 // Get the URL param's corresponding DB field
366 $field = RevisionDeleter::getRelationType( $this->getType() );
367 if ( !$field ) {
368 throw new UnexpectedValueException( "Bad log URL param type!" );
369 }
370 // Add params for affected page and ids
371 $logParams = $this->getLogParams( $params );
372 // Actually add the deletion log entry
373 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
374 $logEntry->setTarget( $params['page'] );
375 $logEntry->setComment( $params['comment'] );
376 $logEntry->setParameters( $logParams );
377 $logEntry->setPerformer( $this->getUser() );
378 // Allow for easy searching of deletion log items for revision/log items
379 $relations = [
380 $field => $params['ids'],
381 ];
382 if ( isset( $params['authorActors'] ) ) {
383 $relations += [
384 'target_author_actor' => $params['authorActors'],
385 ];
386 }
387 $logEntry->setRelations( $relations );
388 // Apply change tags to the log entry
389 $logEntry->addTags( $params['tags'] );
390 $logId = $logEntry->insert();
391 $logEntry->publish( $logId );
392 }
393
394 /**
395 * Get the log action for this list type
396 * @return string
397 */
398 public function getLogAction() {
399 return 'revision';
400 }
401
402 /**
403 * Get log parameter array.
404 * @param array $params Associative array of log parameters, same as updateLog()
405 * @return array
406 */
407 public function getLogParams( $params ) {
408 return [
409 '4::type' => $this->getType(),
410 '5::ids' => $params['ids'],
411 '6::ofield' => $params['oldBits'],
412 '7::nfield' => $params['newBits'],
413 ];
414 }
415
416 /**
417 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
418 * STUB
419 */
420 public function clearFileOps() {
421 }
422
423 /**
424 * A hook for setVisibility(): do batch updates pre-commit.
425 * STUB
426 * @return Status
427 */
428 public function doPreCommitUpdates() {
429 return Status::newGood();
430 }
431
432 /**
433 * A hook for setVisibility(): do any necessary updates post-commit.
434 * STUB
435 * @param array $visibilityChangeMap [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
436 * @return Status
437 */
438 public function doPostCommitUpdates( array $visibilityChangeMap ) {
439 return Status::newGood();
440 }
441
442 }