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