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