Merge "Move bottomScripts() call in SkinTemplate"
[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 );
280
281 $dbw->endAtomic( __METHOD__ );
282
283 return $status;
284 }
285
286 final protected function acquireItemLocks() {
287 $status = Status::newGood();
288 /** @var $item RevDelItem */
289 foreach ( $this as $item ) {
290 $status->merge( $item->lock() );
291 }
292
293 return $status;
294 }
295
296 final protected function releaseItemLocks() {
297 $status = Status::newGood();
298 /** @var $item RevDelItem */
299 foreach ( $this as $item ) {
300 $status->merge( $item->unlock() );
301 }
302
303 return $status;
304 }
305
306 /**
307 * Reload the list data from the master DB. This can be done after setVisibility()
308 * to allow $item->getHTML() to show the new data.
309 */
310 function reloadFromMaster() {
311 $dbw = wfGetDB( DB_MASTER );
312 $this->res = $this->doQuery( $dbw );
313 }
314
315 /**
316 * Record a log entry on the action
317 * @param string $logType One of (delete,suppress)
318 * @param array $params Associative array of parameters:
319 * newBits: The new value of the *_deleted bitfield
320 * oldBits: The old value of the *_deleted bitfield.
321 * title: The target title
322 * ids: The ID list
323 * comment: The log comment
324 * authorsIds: The array of the user IDs of the offenders
325 * authorsIPs: The array of the IP/anon user offenders
326 * @throws MWException
327 */
328 private function updateLog( $logType, $params ) {
329 // Get the URL param's corresponding DB field
330 $field = RevisionDeleter::getRelationType( $this->getType() );
331 if ( !$field ) {
332 throw new MWException( "Bad log URL param type!" );
333 }
334 // Add params for affected page and ids
335 $logParams = $this->getLogParams( $params );
336 // Actually add the deletion log entry
337 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
338 $logEntry->setTarget( $params['title'] );
339 $logEntry->setComment( $params['comment'] );
340 $logEntry->setParameters( $logParams );
341 $logEntry->setPerformer( $this->getUser() );
342 // Allow for easy searching of deletion log items for revision/log items
343 $logEntry->setRelations( [
344 $field => $params['ids'],
345 'target_author_id' => $params['authorIds'],
346 'target_author_ip' => $params['authorIPs'],
347 ] );
348 $logId = $logEntry->insert();
349 $logEntry->publish( $logId );
350 }
351
352 /**
353 * Get the log action for this list type
354 * @return string
355 */
356 public function getLogAction() {
357 return 'revision';
358 }
359
360 /**
361 * Get log parameter array.
362 * @param array $params Associative array of log parameters, same as updateLog()
363 * @return array
364 */
365 public function getLogParams( $params ) {
366 return [
367 '4::type' => $this->getType(),
368 '5::ids' => $params['ids'],
369 '6::ofield' => $params['oldBits'],
370 '7::nfield' => $params['newBits'],
371 ];
372 }
373
374 /**
375 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
376 * STUB
377 */
378 public function clearFileOps() {
379 }
380
381 /**
382 * A hook for setVisibility(): do batch updates pre-commit.
383 * STUB
384 * @return Status
385 */
386 public function doPreCommitUpdates() {
387 return Status::newGood();
388 }
389
390 /**
391 * A hook for setVisibility(): do any necessary updates post-commit.
392 * STUB
393 * @param array [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
394 * @return Status
395 */
396 public function doPostCommitUpdates( array $visibilityChangeMap ) {
397 return Status::newGood();
398 }
399
400 /**
401 * Get the integer value of the flag used for suppression
402 */
403 abstract public function getSuppressBit();
404 }