26e4b17e8a9fced1da3e039b256547a2343e2e53
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( $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 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
129 for ( $this->reset(); $this->current(); $this->next() ) {
130 // @codingStandardsIgnoreEnd
131 /** @var $item RevDelItem */
132 $item = $this->current();
133 unset( $missing[$item->getId()] );
135 if ( $perItemStatus ) {
136 $itemStatus = Status
::newGood();
137 $status->itemStatuses
[$item->getId()] = $itemStatus;
139 $itemStatus = $status;
142 $oldBits = $item->getBits();
143 // Build the actual new rev_deleted bitfield
144 $newBits = RevisionDeleter
::extractBitfield( $bitPars, $oldBits );
146 if ( $oldBits == $newBits ) {
147 $itemStatus->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
148 $status->failCount++
;
150 } elseif ( $oldBits == 0 && $newBits != 0 ) {
152 } elseif ( $oldBits != 0 && $newBits == 0 ) {
158 if ( $item->isHideCurrentOp( $newBits ) ) {
159 // Cannot hide current version text
160 $itemStatus->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
161 $status->failCount++
;
164 if ( !$item->canView() ) {
165 // Cannot access this revision
166 $msg = ( $opType == 'show' ) ?
167 'revdelete-show-no-access' : 'revdelete-modify-no-access';
168 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
169 $status->failCount++
;
172 // Cannot just "hide from Sysops" without hiding any fields
173 if ( $newBits == Revision
::DELETED_RESTRICTED
) {
174 $itemStatus->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
175 $status->failCount++
;
179 // Update the revision
180 $ok = $item->setBits( $newBits );
183 $idsForLog[] = $item->getId();
184 $status->successCount++
;
185 if ( $item->getAuthorId() > 0 ) {
186 $authorIds[] = $item->getAuthorId();
187 } elseif ( IP
::isIPAddress( $item->getAuthorName() ) ) {
188 $authorIPs[] = $item->getAuthorName();
191 $itemStatus->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
192 $status->failCount++
;
196 // Handle missing revisions
197 foreach ( $missing as $id => $unused ) {
198 if ( $perItemStatus ) {
199 $status->itemStatuses
[$id] = Status
::newFatal( 'revdelete-modify-missing', $id );
201 $status->error( 'revdelete-modify-missing', $id );
203 $status->failCount++
;
206 if ( $status->successCount
== 0 ) {
207 $dbw->rollback( __METHOD__
);
211 // Save success count
212 $successCount = $status->successCount
;
214 // Move files, if there are any
215 $status->merge( $this->doPreCommitUpdates() );
216 if ( !$status->isOK() ) {
217 // Fatal error, such as no configured archive directory
218 $dbw->rollback( __METHOD__
);
223 // @FIXME: $newBits/$oldBits set in for loop, makes IDE warnings too
225 'title' => $this->title
,
226 'count' => $successCount,
227 'newBits' => $newBits,
228 'oldBits' => $oldBits,
229 'comment' => $comment,
231 'authorIds' => $authorIds,
232 'authorIPs' => $authorIPs
237 $dbw->onTransactionIdle( function() use ( $that ) {
238 $that->doPostCommitUpdates();
241 $dbw->endAtomic( __METHOD__
);
247 * Reload the list data from the master DB. This can be done after setVisibility()
248 * to allow $item->getHTML() to show the new data.
250 function reloadFromMaster() {
251 $dbw = wfGetDB( DB_MASTER
);
252 $this->res
= $this->doQuery( $dbw );
256 * Record a log entry on the action
257 * @param array $params Associative array of parameters:
258 * newBits: The new value of the *_deleted bitfield
259 * oldBits: The old value of the *_deleted bitfield.
260 * title: The target title
262 * comment: The log comment
263 * authorsIds: The array of the user IDs of the offenders
264 * authorsIPs: The array of the IP/anon user offenders
265 * @throws MWException
267 protected function updateLog( $params ) {
268 // Get the URL param's corresponding DB field
269 $field = RevisionDeleter
::getRelationType( $this->getType() );
271 throw new MWException( "Bad log URL param type!" );
273 // Put things hidden from sysops in the suppression log
274 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
275 $logType = 'suppress';
279 // Add params for affected page and ids
280 $logParams = $this->getLogParams( $params );
281 // Actually add the deletion log entry
282 $logEntry = new ManualLogEntry( $logType, $this->getLogAction() );
283 $logEntry->setTarget( $params['title'] );
284 $logEntry->setComment( $params['comment'] );
285 $logEntry->setParameters( $logParams );
286 $logEntry->setPerformer( $this->getUser() );
287 // Allow for easy searching of deletion log items for revision/log items
288 $logEntry->setRelations( [
289 $field => $params['ids'],
290 'target_author_id' => $params['authorIds'],
291 'target_author_ip' => $params['authorIPs'],
293 $logId = $logEntry->insert();
294 $logEntry->publish( $logId );
298 * Get the log action for this list type
301 public function getLogAction() {
306 * Get log parameter array.
307 * @param array $params Associative array of log parameters, same as updateLog()
310 public function getLogParams( $params ) {
312 '4::type' => $this->getType(),
313 '5::ids' => $params['ids'],
314 '6::ofield' => $params['oldBits'],
315 '7::nfield' => $params['newBits'],
320 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
323 public function clearFileOps() {
327 * A hook for setVisibility(): do batch updates pre-commit.
331 public function doPreCommitUpdates() {
332 return Status
::newGood();
336 * A hook for setVisibility(): do any necessary updates post-commit.
340 public function doPostCommitUpdates() {
341 return Status
::newGood();
345 * Get the integer value of the flag used for suppression
347 abstract public function getSuppressBit();