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