Add tagging support to imagerotate, revisiondelete, and tag API modules
[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 * tags: The array of change tags to apply to the log entry
103 * @return Status
104 * @since 1.23 Added 'perItemStatus' param
105 */
106 public function setVisibility( array $params ) {
107 $status = Status::newGood();
108
109 $bitPars = $params['value'];
110 $comment = $params['comment'];
111 $perItemStatus = isset( $params['perItemStatus'] ) ? $params['perItemStatus'] : false;
112
113 // CAS-style checks are done on the _deleted fields so the select
114 // does not need to use FOR UPDATE nor be in the atomic section
115 $dbw = wfGetDB( DB_MASTER );
116 $this->res = $this->doQuery( $dbw );
117
118 $status->merge( $this->acquireItemLocks() );
119 if ( !$status->isGood() ) {
120 return $status;
121 }
122
123 $dbw->startAtomic( __METHOD__ );
124 $dbw->onTransactionResolution(
125 function () {
126 // Release locks on commit or error
127 $this->releaseItemLocks();
128 },
129 __METHOD__
130 );
131
132 $missing = array_flip( $this->ids );
133 $this->clearFileOps();
134 $idsForLog = [];
135 $authorIds = $authorIPs = [];
136
137 if ( $perItemStatus ) {
138 $status->itemStatuses = [];
139 }
140
141 // For multi-item deletions, set the old/new bitfields in log_params such that "hid X"
142 // shows in logs if field X was hidden from ANY item and likewise for "unhid Y". Note the
143 // form does not let the same field get hidden and unhidden in different items at once.
144 $virtualOldBits = 0;
145 $virtualNewBits = 0;
146 $logType = 'delete';
147
148 // Will be filled with id => [old, new bits] information and
149 // passed to doPostCommitUpdates().
150 $visibilityChangeMap = [];
151
152 /** @var $item RevDelItem */
153 foreach ( $this as $item ) {
154 unset( $missing[$item->getId()] );
155
156 if ( $perItemStatus ) {
157 $itemStatus = Status::newGood();
158 $status->itemStatuses[$item->getId()] = $itemStatus;
159 } else {
160 $itemStatus = $status;
161 }
162
163 $oldBits = $item->getBits();
164 // Build the actual new rev_deleted bitfield
165 $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
166
167 if ( $oldBits == $newBits ) {
168 $itemStatus->warning(
169 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
170 $status->failCount++;
171 continue;
172 } elseif ( $oldBits == 0 && $newBits != 0 ) {
173 $opType = 'hide';
174 } elseif ( $oldBits != 0 && $newBits == 0 ) {
175 $opType = 'show';
176 } else {
177 $opType = 'modify';
178 }
179
180 if ( $item->isHideCurrentOp( $newBits ) ) {
181 // Cannot hide current version text
182 $itemStatus->error(
183 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
184 $status->failCount++;
185 continue;
186 } elseif ( !$item->canView() ) {
187 // Cannot access this revision
188 $msg = ( $opType == 'show' ) ?
189 'revdelete-show-no-access' : 'revdelete-modify-no-access';
190 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
191 $status->failCount++;
192 continue;
193 // Cannot just "hide from Sysops" without hiding any fields
194 } elseif ( $newBits == Revision::DELETED_RESTRICTED ) {
195 $itemStatus->warning(
196 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
197 $status->failCount++;
198 continue;
199 }
200
201 // Update the revision
202 $ok = $item->setBits( $newBits );
203
204 if ( $ok ) {
205 $idsForLog[] = $item->getId();
206 // If any item field was suppressed or unsupressed
207 if ( ( $oldBits | $newBits ) & $this->getSuppressBit() ) {
208 $logType = 'suppress';
209 }
210 // Track which fields where (un)hidden for each item
211 $addedBits = ( $oldBits ^ $newBits ) & $newBits;
212 $removedBits = ( $oldBits ^ $newBits ) & $oldBits;
213 $virtualNewBits |= $addedBits;
214 $virtualOldBits |= $removedBits;
215
216 $status->successCount++;
217 if ( $item->getAuthorId() > 0 ) {
218 $authorIds[] = $item->getAuthorId();
219 } elseif ( IP::isIPAddress( $item->getAuthorName() ) ) {
220 $authorIPs[] = $item->getAuthorName();
221 }
222
223 // Save the old and new bits in $visibilityChangeMap for
224 // later use.
225 $visibilityChangeMap[$item->getId()] = [
226 'oldBits' => $oldBits,
227 'newBits' => $newBits,
228 ];
229 } else {
230 $itemStatus->error(
231 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
232 $status->failCount++;
233 }
234 }
235
236 // Handle missing revisions
237 foreach ( $missing as $id => $unused ) {
238 if ( $perItemStatus ) {
239 $status->itemStatuses[$id] = Status::newFatal( 'revdelete-modify-missing', $id );
240 } else {
241 $status->error( 'revdelete-modify-missing', $id );
242 }
243 $status->failCount++;
244 }
245
246 if ( $status->successCount == 0 ) {
247 $dbw->endAtomic( __METHOD__ );
248 return $status;
249 }
250
251 // Save success count
252 $successCount = $status->successCount;
253
254 // Move files, if there are any
255 $status->merge( $this->doPreCommitUpdates() );
256 if ( !$status->isOK() ) {
257 // Fatal error, such as no configured archive directory or I/O failures
258 wfGetLBFactory()->rollbackMasterChanges( __METHOD__ );
259 return $status;
260 }
261
262 // Log it
263 $this->updateLog(
264 $logType,
265 [
266 'title' => $this->title,
267 'count' => $successCount,
268 'newBits' => $virtualNewBits,
269 'oldBits' => $virtualOldBits,
270 'comment' => $comment,
271 'ids' => $idsForLog,
272 'authorIds' => $authorIds,
273 'authorIPs' => $authorIPs,
274 'tags' => isset( $params['tags'] ) ? $params['tags'] : [],
275 ]
276 );
277
278 // Clear caches after commit
279 DeferredUpdates::addCallableUpdate(
280 function () use ( $visibilityChangeMap ) {
281 $this->doPostCommitUpdates( $visibilityChangeMap );
282 },
283 DeferredUpdates::PRESEND,
284 $dbw
285 );
286
287 $dbw->endAtomic( __METHOD__ );
288
289 return $status;
290 }
291
292 final protected function acquireItemLocks() {
293 $status = Status::newGood();
294 /** @var $item RevDelItem */
295 foreach ( $this as $item ) {
296 $status->merge( $item->lock() );
297 }
298
299 return $status;
300 }
301
302 final protected function releaseItemLocks() {
303 $status = Status::newGood();
304 /** @var $item RevDelItem */
305 foreach ( $this as $item ) {
306 $status->merge( $item->unlock() );
307 }
308
309 return $status;
310 }
311
312 /**
313 * Reload the list data from the master DB. This can be done after setVisibility()
314 * to allow $item->getHTML() to show the new data.
315 */
316 function reloadFromMaster() {
317 $dbw = wfGetDB( DB_MASTER );
318 $this->res = $this->doQuery( $dbw );
319 }
320
321 /**
322 * Record a log entry on the action
323 * @param string $logType One of (delete,suppress)
324 * @param array $params Associative array of parameters:
325 * newBits: The new value of the *_deleted bitfield
326 * oldBits: The old value of the *_deleted bitfield.
327 * title: The target title
328 * ids: The ID list
329 * comment: The log comment
330 * authorsIds: The array of the user IDs of the offenders
331 * authorsIPs: The array of the IP/anon user offenders
332 * tags: The array of change tags to apply to the log entry
333 * @throws MWException
334 */
335 private function updateLog( $logType, $params ) {
336 // Get the URL param's corresponding DB field
337 $field = RevisionDeleter::getRelationType( $this->getType() );
338 if ( !$field ) {
339 throw new MWException( "Bad log URL param type!" );
340 }
341 // Add params for affected page and ids
342 $logParams = $this->getLogParams( $params );
343 // Actually add the deletion log entry
344 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
345 $logEntry->setTarget( $params['title'] );
346 $logEntry->setComment( $params['comment'] );
347 $logEntry->setParameters( $logParams );
348 $logEntry->setPerformer( $this->getUser() );
349 // Allow for easy searching of deletion log items for revision/log items
350 $logEntry->setRelations( [
351 $field => $params['ids'],
352 'target_author_id' => $params['authorIds'],
353 'target_author_ip' => $params['authorIPs'],
354 ] );
355 // Apply change tags to the log entry
356 $logEntry->setTags( $params['tags'] );
357 $logId = $logEntry->insert();
358 $logEntry->publish( $logId );
359 }
360
361 /**
362 * Get the log action for this list type
363 * @return string
364 */
365 public function getLogAction() {
366 return 'revision';
367 }
368
369 /**
370 * Get log parameter array.
371 * @param array $params Associative array of log parameters, same as updateLog()
372 * @return array
373 */
374 public function getLogParams( $params ) {
375 return [
376 '4::type' => $this->getType(),
377 '5::ids' => $params['ids'],
378 '6::ofield' => $params['oldBits'],
379 '7::nfield' => $params['newBits'],
380 ];
381 }
382
383 /**
384 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
385 * STUB
386 */
387 public function clearFileOps() {
388 }
389
390 /**
391 * A hook for setVisibility(): do batch updates pre-commit.
392 * STUB
393 * @return Status
394 */
395 public function doPreCommitUpdates() {
396 return Status::newGood();
397 }
398
399 /**
400 * A hook for setVisibility(): do any necessary updates post-commit.
401 * STUB
402 * @param array [id => ['oldBits' => $oldBits, 'newBits' => $newBits], ... ]
403 * @return Status
404 */
405 public function doPostCommitUpdates( array $visibilityChangeMap ) {
406 return Status::newGood();
407 }
408
409 /**
410 * Get the integer value of the flag used for suppression
411 */
412 abstract public function getSuppressBit();
413 }