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.
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.
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
19 * @ingroup RevisionDelete
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.
28 abstract class RevDelList
extends RevisionListBase
{
29 function __construct( IContextSource
$context, Title
$title, array $ids ) {
30 parent
::__construct( $context, $title );
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.
40 public static function getRelationType() {
45 * Get the user right required for this list type
46 * Override this function.
50 public static function getRestriction() {
55 * Get the revision deletion constant for this list type
56 * Override this function.
60 public static function getRevdelConstant() {
65 * Suggest a target for the revision deletion
66 * Optionally override this function.
68 * @param Title|null $target User-supplied target
72 public static function suggestTarget( $target, array $ids ) {
77 * Indicate whether any item in this list is suppressed
81 public function areAnySuppressed() {
82 $bit = $this->getSuppressBit();
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 ) {
96 * Set the visibility for the revisions in this list. Logging and
97 * transactions are done here.
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
104 * @since 1.23 Added 'perItemStatus' param
106 public function setVisibility( array $params ) {
107 $bitPars = $params['value'];
108 $comment = $params['comment'];
109 $perItemStatus = isset( $params['perItemStatus'] ) ?
$params['perItemStatus'] : false;
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 );
116 $dbw->startAtomic( __METHOD__
);
118 $status = Status
::newGood();
119 $missing = array_flip( $this->ids
);
120 $this->clearFileOps();
122 $authorIds = $authorIPs = [];
124 if ( $perItemStatus ) {
125 $status->itemStatuses
= [];
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.
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()] );
142 if ( $perItemStatus ) {
143 $itemStatus = Status
::newGood();
144 $status->itemStatuses
[$item->getId()] = $itemStatus;
146 $itemStatus = $status;
149 $oldBits = $item->getBits();
150 // Build the actual new rev_deleted bitfield
151 $newBits = RevisionDeleter
::extractBitfield( $bitPars, $oldBits );
153 if ( $oldBits == $newBits ) {
154 $itemStatus->warning(
155 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
156 $status->failCount++
;
158 } elseif ( $oldBits == 0 && $newBits != 0 ) {
160 } elseif ( $oldBits != 0 && $newBits == 0 ) {
166 if ( $item->isHideCurrentOp( $newBits ) ) {
167 // Cannot hide current version text
169 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
170 $status->failCount++
;
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++
;
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++
;
187 // Update the revision
188 $ok = $item->setBits( $newBits );
191 $idsForLog[] = $item->getId();
192 // If any item field was suppressed or unsupressed
193 if ( ( $oldBits |
$newBits ) & $this->getSuppressBit() ) {
194 $logType = 'suppress';
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;
202 $status->successCount++
;
203 if ( $item->getAuthorId() > 0 ) {
204 $authorIds[] = $item->getAuthorId();
205 } elseif ( IP
::isIPAddress( $item->getAuthorName() ) ) {
206 $authorIPs[] = $item->getAuthorName();
210 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
211 $status->failCount++
;
215 // Handle missing revisions
216 foreach ( $missing as $id => $unused ) {
217 if ( $perItemStatus ) {
218 $status->itemStatuses
[$id] = Status
::newFatal( 'revdelete-modify-missing', $id );
220 $status->error( 'revdelete-modify-missing', $id );
222 $status->failCount++
;
225 if ( $status->successCount
== 0 ) {
226 $dbw->rollback( __METHOD__
);
230 // Save success count
231 $successCount = $status->successCount
;
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__
);
245 'title' => $this->title
,
246 'count' => $successCount,
247 'newBits' => $virtualNewBits,
248 'oldBits' => $virtualOldBits,
249 'comment' => $comment,
251 'authorIds' => $authorIds,
252 'authorIPs' => $authorIPs
258 $dbw->onTransactionIdle( function() use ( $that ) {
259 $that->doPostCommitUpdates();
262 $dbw->endAtomic( __METHOD__
);
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.
271 function reloadFromMaster() {
272 $dbw = wfGetDB( DB_MASTER
);
273 $this->res
= $this->doQuery( $dbw );
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
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
289 private function updateLog( $logType, $params ) {
290 // Get the URL param's corresponding DB field
291 $field = RevisionDeleter
::getRelationType( $this->getType() );
293 throw new MWException( "Bad log URL param type!" );
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'],
309 $logId = $logEntry->insert();
310 $logEntry->publish( $logId );
314 * Get the log action for this list type
317 public function getLogAction() {
322 * Get log parameter array.
323 * @param array $params Associative array of log parameters, same as updateLog()
326 public function getLogParams( $params ) {
328 '4::type' => $this->getType(),
329 '5::ids' => $params['ids'],
330 '6::ofield' => $params['oldBits'],
331 '7::nfield' => $params['newBits'],
336 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
339 public function clearFileOps() {
343 * A hook for setVisibility(): do batch updates pre-commit.
347 public function doPreCommitUpdates() {
348 return Status
::newGood();
352 * A hook for setVisibility(): do any necessary updates post-commit.
356 public function doPostCommitUpdates() {
357 return Status
::newGood();
361 * Get the integer value of the flag used for suppression
363 abstract public function getSuppressBit();