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