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