Use Agora oojs-ui theme on mobile
[mediawiki.git] / includes / revisiondelete / RevisionDeleteAbstracts.php
blob3874602f7bafa8413c71354964751947d87699f4
1 <?php
2 /**
3 * Interface definition for deletable items.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup RevisionDelete
24 /**
25 * Abstract base class for a list of deletable items. The list class
26 * needs to be able to make a query from a set of identifiers to pull
27 * relevant rows, to return RevDel_Item subclasses wrapping them, and
28 * to wrap bulk update operations.
30 abstract class RevDel_List extends RevisionListBase {
31 function __construct( IContextSource $context, Title $title, array $ids ) {
32 parent::__construct( $context, $title );
33 $this->ids = $ids;
36 /**
37 * Get the DB field name associated with the ID list.
38 * This used to populate the log_search table for finding log entries.
39 * Override this function.
40 * @return string|null
42 public static function getRelationType() {
43 return null;
46 /**
47 * Get the user right required for this list type
48 * Override this function.
49 * @since 1.22
50 * @return string|null
52 public static function getRestriction() {
53 return null;
56 /**
57 * Get the revision deletion constant for this list type
58 * Override this function.
59 * @since 1.22
60 * @return int|null
62 public static function getRevdelConstant() {
63 return null;
66 /**
67 * Suggest a target for the revision deletion
68 * Optionally override this function.
69 * @since 1.22
70 * @param Title|null $target User-supplied target
71 * @param array $ids
72 * @return Title|null
74 public static function suggestTarget( $target, array $ids ) {
75 return $target;
78 /**
79 * Set the visibility for the revisions in this list. Logging and
80 * transactions are done here.
82 * @param array $params Associative array of parameters. Members are:
83 * value: The integer value to set the visibility to
84 * comment: The log comment.
85 * perItemStatus: Set if you want per-item status reports
86 * @return Status
87 * @since 1.23 Added 'perItemStatus' param
89 public function setVisibility( $params ) {
90 $bitPars = $params['value'];
91 $comment = $params['comment'];
92 $perItemStatus = isset( $params['perItemStatus'] ) ? $params['perItemStatus'] : false;
94 $this->res = false;
95 $dbw = wfGetDB( DB_MASTER );
96 $this->doQuery( $dbw );
97 $dbw->begin( __METHOD__ );
98 $status = Status::newGood();
99 $missing = array_flip( $this->ids );
100 $this->clearFileOps();
101 $idsForLog = array();
102 $authorIds = $authorIPs = array();
104 if ( $perItemStatus ) {
105 $status->itemStatuses = array();
108 for ( $this->reset(); $this->current(); $this->next() ) {
109 $item = $this->current();
110 unset( $missing[$item->getId()] );
112 if ( $perItemStatus ) {
113 $itemStatus = Status::newGood();
114 $status->itemStatuses[$item->getId()] = $itemStatus;
115 } else {
116 $itemStatus = $status;
119 $oldBits = $item->getBits();
120 // Build the actual new rev_deleted bitfield
121 $newBits = RevisionDeleter::extractBitfield( $bitPars, $oldBits );
123 if ( $oldBits == $newBits ) {
124 $itemStatus->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
125 $status->failCount++;
126 continue;
127 } elseif ( $oldBits == 0 && $newBits != 0 ) {
128 $opType = 'hide';
129 } elseif ( $oldBits != 0 && $newBits == 0 ) {
130 $opType = 'show';
131 } else {
132 $opType = 'modify';
135 if ( $item->isHideCurrentOp( $newBits ) ) {
136 // Cannot hide current version text
137 $itemStatus->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
138 $status->failCount++;
139 continue;
141 if ( !$item->canView() ) {
142 // Cannot access this revision
143 $msg = ( $opType == 'show' ) ?
144 'revdelete-show-no-access' : 'revdelete-modify-no-access';
145 $itemStatus->error( $msg, $item->formatDate(), $item->formatTime() );
146 $status->failCount++;
147 continue;
149 // Cannot just "hide from Sysops" without hiding any fields
150 if ( $newBits == Revision::DELETED_RESTRICTED ) {
151 $itemStatus->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
152 $status->failCount++;
153 continue;
156 // Update the revision
157 $ok = $item->setBits( $newBits );
159 if ( $ok ) {
160 $idsForLog[] = $item->getId();
161 $status->successCount++;
162 if ( $item->getAuthorId() > 0 ) {
163 $authorIds[] = $item->getAuthorId();
164 } elseif ( IP::isIPAddress( $item->getAuthorName() ) ) {
165 $authorIPs[] = $item->getAuthorName();
167 } else {
168 $itemStatus->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
169 $status->failCount++;
173 // Handle missing revisions
174 foreach ( $missing as $id => $unused ) {
175 if ( $perItemStatus ) {
176 $status->itemStatuses[$id] = Status::newFatal( 'revdelete-modify-missing', $id );
177 } else {
178 $status->error( 'revdelete-modify-missing', $id );
180 $status->failCount++;
183 if ( $status->successCount == 0 ) {
184 $dbw->rollback( __METHOD__ );
185 return $status;
188 // Save success count
189 $successCount = $status->successCount;
191 // Move files, if there are any
192 $status->merge( $this->doPreCommitUpdates() );
193 if ( !$status->isOK() ) {
194 // Fatal error, such as no configured archive directory
195 $dbw->rollback( __METHOD__ );
196 return $status;
199 // Log it
200 $this->updateLog( array(
201 'title' => $this->title,
202 'count' => $successCount,
203 'newBits' => $newBits,
204 'oldBits' => $oldBits,
205 'comment' => $comment,
206 'ids' => $idsForLog,
207 'authorIds' => $authorIds,
208 'authorIPs' => $authorIPs
209 ) );
210 $dbw->commit( __METHOD__ );
212 // Clear caches
213 $status->merge( $this->doPostCommitUpdates() );
214 return $status;
218 * Reload the list data from the master DB. This can be done after setVisibility()
219 * to allow $item->getHTML() to show the new data.
221 function reloadFromMaster() {
222 $dbw = wfGetDB( DB_MASTER );
223 $this->res = $this->doQuery( $dbw );
227 * Record a log entry on the action
228 * @param array $params Associative array of parameters:
229 * newBits: The new value of the *_deleted bitfield
230 * oldBits: The old value of the *_deleted bitfield.
231 * title: The target title
232 * ids: The ID list
233 * comment: The log comment
234 * authorsIds: The array of the user IDs of the offenders
235 * authorsIPs: The array of the IP/anon user offenders
236 * @throws MWException
238 protected function updateLog( $params ) {
239 // Get the URL param's corresponding DB field
240 $field = RevisionDeleter::getRelationType( $this->getType() );
241 if ( !$field ) {
242 throw new MWException( "Bad log URL param type!" );
244 // Put things hidden from sysops in the oversight log
245 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
246 $logType = 'suppress';
247 } else {
248 $logType = 'delete';
250 // Add params for effected page and ids
251 $logParams = $this->getLogParams( $params );
252 // Actually add the deletion log entry
253 $log = new LogPage( $logType );
254 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
255 $params['comment'], $logParams, $this->getUser() );
256 // Allow for easy searching of deletion log items for revision/log items
257 $log->addRelations( $field, $params['ids'], $logid );
258 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
259 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
263 * Get the log action for this list type
264 * @return string
266 public function getLogAction() {
267 return 'revision';
271 * Get log parameter array.
272 * @param array $params Associative array of log parameters, same as updateLog()
273 * @return array
275 public function getLogParams( $params ) {
276 return array(
277 $this->getType(),
278 implode( ',', $params['ids'] ),
279 "ofield={$params['oldBits']}",
280 "nfield={$params['newBits']}"
285 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
286 * STUB
288 public function clearFileOps() {
292 * A hook for setVisibility(): do batch updates pre-commit.
293 * STUB
294 * @return Status
296 public function doPreCommitUpdates() {
297 return Status::newGood();
301 * A hook for setVisibility(): do any necessary updates post-commit.
302 * STUB
303 * @return Status
305 public function doPostCommitUpdates() {
306 return Status::newGood();
310 * Get the integer value of the flag used for suppression
312 abstract public function getSuppressBit();
316 * Abstract base class for deletable items
318 abstract class RevDel_Item extends RevisionItemBase {
320 * Returns true if the item is "current", and the operation to set the given
321 * bits can't be executed for that reason
322 * STUB
323 * @return bool
325 public function isHideCurrentOp( $newBits ) {
326 return false;
330 * Get the current deletion bitfield value
332 abstract public function getBits();
335 * Set the visibility of the item. This should do any necessary DB queries.
337 * The DB update query should have a condition which forces it to only update
338 * if the value in the DB matches the value fetched earlier with the SELECT.
339 * If the update fails because it did not match, the function should return
340 * false. This prevents concurrency problems.
342 * @return boolean success
344 abstract public function setBits( $newBits );
347 * Get the return information about the revision for the API
348 * @since 1.23
349 * @param ApiResult $result API result object
350 * @return array Data for the API result
352 abstract public function getApiData( ApiResult $result );