Followup r78924: keep track of exception/warning comments separately, to prevent...
[lhc/web/wiklou.git] / includes / revisiondelete / RevisionDeleteAbstracts.php
1 <?php
2
3 /**
4 * Abstract base class for a list of deletable items
5 */
6 abstract class RevDel_List {
7 var $special, $title, $ids, $res, $current;
8 var $type = null; // override this
9 var $idField = null; // override this
10 var $dateField = false; // override this
11 var $authorIdField = false; // override this
12 var $authorNameField = false; // override this
13
14 /**
15 * @param $special The parent SpecialPage
16 * @param $title The target title
17 * @param $ids Array of IDs
18 */
19 public function __construct( $special, $title, $ids ) {
20 $this->special = $special;
21 $this->title = $title;
22 $this->ids = $ids;
23 }
24
25 /**
26 * Get the internal type name of this list. Equal to the table name.
27 */
28 public function getType() {
29 return $this->type;
30 }
31
32 /**
33 * Get the DB field name associated with the ID list
34 */
35 public function getIdField() {
36 return $this->idField;
37 }
38
39 /**
40 * Get the DB field name storing timestamps
41 */
42 public function getTimestampField() {
43 return $this->dateField;
44 }
45
46 /**
47 * Get the DB field name storing user ids
48 */
49 public function getAuthorIdField() {
50 return $this->authorIdField;
51 }
52
53 /**
54 * Get the DB field name storing user names
55 */
56 public function getAuthorNameField() {
57 return $this->authorNameField;
58 }
59 /**
60 * Set the visibility for the revisions in this list. Logging and
61 * transactions are done here.
62 *
63 * @param $params Associative array of parameters. Members are:
64 * value: The integer value to set the visibility to
65 * comment: The log comment.
66 * @return Status
67 */
68 public function setVisibility( $params ) {
69 $bitPars = $params['value'];
70 $comment = $params['comment'];
71
72 $this->res = false;
73 $dbw = wfGetDB( DB_MASTER );
74 $this->doQuery( $dbw );
75 $dbw->begin();
76 $status = Status::newGood();
77 $missing = array_flip( $this->ids );
78 $this->clearFileOps();
79 $idsForLog = array();
80 $authorIds = $authorIPs = array();
81
82 for ( $this->reset(); $this->current(); $this->next() ) {
83 $item = $this->current();
84 unset( $missing[ $item->getId() ] );
85
86 $oldBits = $item->getBits();
87 // Build the actual new rev_deleted bitfield
88 $newBits = SpecialRevisionDelete::extractBitfield( $bitPars, $oldBits );
89
90 if ( $oldBits == $newBits ) {
91 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
92 $status->failCount++;
93 continue;
94 } elseif ( $oldBits == 0 && $newBits != 0 ) {
95 $opType = 'hide';
96 } elseif ( $oldBits != 0 && $newBits == 0 ) {
97 $opType = 'show';
98 } else {
99 $opType = 'modify';
100 }
101
102 if ( $item->isHideCurrentOp( $newBits ) ) {
103 // Cannot hide current version text
104 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
105 $status->failCount++;
106 continue;
107 }
108 if ( !$item->canView() ) {
109 // Cannot access this revision
110 $msg = ($opType == 'show') ?
111 'revdelete-show-no-access' : 'revdelete-modify-no-access';
112 $status->error( $msg, $item->formatDate(), $item->formatTime() );
113 $status->failCount++;
114 continue;
115 }
116 // Cannot just "hide from Sysops" without hiding any fields
117 if( $newBits == Revision::DELETED_RESTRICTED ) {
118 $status->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
119 $status->failCount++;
120 continue;
121 }
122
123 // Update the revision
124 $ok = $item->setBits( $newBits );
125
126 if ( $ok ) {
127 $idsForLog[] = $item->getId();
128 $status->successCount++;
129 if( $item->getAuthorId() > 0 ) {
130 $authorIds[] = $item->getAuthorId();
131 } else if( IP::isIPAddress( $item->getAuthorName() ) ) {
132 $authorIPs[] = $item->getAuthorName();
133 }
134 } else {
135 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
136 $status->failCount++;
137 }
138 }
139
140 // Handle missing revisions
141 foreach ( $missing as $id => $unused ) {
142 $status->error( 'revdelete-modify-missing', $id );
143 $status->failCount++;
144 }
145
146 if ( $status->successCount == 0 ) {
147 $status->ok = false;
148 $dbw->rollback();
149 return $status;
150 }
151
152 // Save success count
153 $successCount = $status->successCount;
154
155 // Move files, if there are any
156 $status->merge( $this->doPreCommitUpdates() );
157 if ( !$status->isOK() ) {
158 // Fatal error, such as no configured archive directory
159 $dbw->rollback();
160 return $status;
161 }
162
163 // Log it
164 $this->updateLog( array(
165 'title' => $this->title,
166 'count' => $successCount,
167 'newBits' => $newBits,
168 'oldBits' => $oldBits,
169 'comment' => $comment,
170 'ids' => $idsForLog,
171 'authorIds' => $authorIds,
172 'authorIPs' => $authorIPs
173 ) );
174 $dbw->commit();
175
176 // Clear caches
177 $status->merge( $this->doPostCommitUpdates() );
178 return $status;
179 }
180
181 /**
182 * Reload the list data from the master DB. This can be done after setVisibility()
183 * to allow $item->getHTML() to show the new data.
184 */
185 function reloadFromMaster() {
186 $dbw = wfGetDB( DB_MASTER );
187 $this->res = $this->doQuery( $dbw );
188 }
189
190 /**
191 * Record a log entry on the action
192 * @param $params Associative array of parameters:
193 * newBits: The new value of the *_deleted bitfield
194 * oldBits: The old value of the *_deleted bitfield.
195 * title: The target title
196 * ids: The ID list
197 * comment: The log comment
198 * authorsIds: The array of the user IDs of the offenders
199 * authorsIPs: The array of the IP/anon user offenders
200 */
201 protected function updateLog( $params ) {
202 // Get the URL param's corresponding DB field
203 $field = RevisionDeleter::getRelationType( $this->getType() );
204 if( !$field ) {
205 throw new MWException( "Bad log URL param type!" );
206 }
207 // Put things hidden from sysops in the oversight log
208 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
209 $logType = 'suppress';
210 } else {
211 $logType = 'delete';
212 }
213 // Add params for effected page and ids
214 $logParams = $this->getLogParams( $params );
215 // Actually add the deletion log entry
216 $log = new LogPage( $logType );
217 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
218 $params['comment'], $logParams );
219 // Allow for easy searching of deletion log items for revision/log items
220 $log->addRelations( $field, $params['ids'], $logid );
221 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
222 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
223 }
224
225 /**
226 * Get the log action for this list type
227 */
228 public function getLogAction() {
229 return 'revision';
230 }
231
232 /**
233 * Get log parameter array.
234 * @param $params Associative array of log parameters, same as updateLog()
235 * @return array
236 */
237 public function getLogParams( $params ) {
238 return array(
239 $this->getType(),
240 implode( ',', $params['ids'] ),
241 "ofield={$params['oldBits']}",
242 "nfield={$params['newBits']}"
243 );
244 }
245
246 /**
247 * Initialise the current iteration pointer
248 */
249 protected function initCurrent() {
250 $row = $this->res->current();
251 if ( $row ) {
252 $this->current = $this->newItem( $row );
253 } else {
254 $this->current = false;
255 }
256 }
257
258 /**
259 * Start iteration. This must be called before current() or next().
260 * @return First list item
261 */
262 public function reset() {
263 if ( !$this->res ) {
264 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
265 } else {
266 $this->res->rewind();
267 }
268 $this->initCurrent();
269 return $this->current;
270 }
271
272 /**
273 * Get the current list item, or false if we are at the end
274 */
275 public function current() {
276 return $this->current;
277 }
278
279 /**
280 * Move the iteration pointer to the next list item, and return it.
281 */
282 public function next() {
283 $this->res->next();
284 $this->initCurrent();
285 return $this->current;
286 }
287
288 /**
289 * Get the number of items in the list.
290 */
291 public function length() {
292 if( !$this->res ) {
293 return 0;
294 } else {
295 return $this->res->numRows();
296 }
297 }
298
299 /**
300 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
301 * STUB
302 */
303 public function clearFileOps() {
304 }
305
306 /**
307 * A hook for setVisibility(): do batch updates pre-commit.
308 * STUB
309 * @return Status
310 */
311 public function doPreCommitUpdates() {
312 return Status::newGood();
313 }
314
315 /**
316 * A hook for setVisibility(): do any necessary updates post-commit.
317 * STUB
318 * @return Status
319 */
320 public function doPostCommitUpdates() {
321 return Status::newGood();
322 }
323
324 /**
325 * Create an item object from a DB result row
326 * @param $row stdclass
327 */
328 abstract public function newItem( $row );
329
330 /**
331 * Do the DB query to iterate through the objects.
332 * @param $db Database object to use for the query
333 */
334 abstract public function doQuery( $db );
335
336 /**
337 * Get the integer value of the flag used for suppression
338 */
339 abstract public function getSuppressBit();
340 }
341
342 /**
343 * Abstract base class for deletable items
344 */
345 abstract class RevDel_Item {
346 /** The parent SpecialPage */
347 var $special;
348
349 /** The parent RevDel_List */
350 var $list;
351
352 /** The DB result row */
353 var $row;
354
355 /**
356 * @param $list RevDel_List
357 * @param $row DB result row
358 */
359 public function __construct( $list, $row ) {
360 $this->special = $list->special;
361 $this->list = $list;
362 $this->row = $row;
363 }
364
365 /**
366 * Get the ID, as it would appear in the ids URL parameter
367 */
368 public function getId() {
369 $field = $this->list->getIdField();
370 return $this->row->$field;
371 }
372
373 /**
374 * Get the date, formatted with $wgLang
375 */
376 public function formatDate() {
377 global $wgLang;
378 return $wgLang->date( $this->getTimestamp() );
379 }
380
381 /**
382 * Get the time, formatted with $wgLang
383 */
384 public function formatTime() {
385 global $wgLang;
386 return $wgLang->time( $this->getTimestamp() );
387 }
388
389 /**
390 * Get the timestamp in MW 14-char form
391 */
392 public function getTimestamp() {
393 $field = $this->list->getTimestampField();
394 return wfTimestamp( TS_MW, $this->row->$field );
395 }
396
397 /**
398 * Get the author user ID
399 */
400 public function getAuthorId() {
401 $field = $this->list->getAuthorIdField();
402 return intval( $this->row->$field );
403 }
404
405 /**
406 * Get the author user name
407 */
408 public function getAuthorName() {
409 $field = $this->list->getAuthorNameField();
410 return strval( $this->row->$field );
411 }
412
413 /**
414 * Returns true if the item is "current", and the operation to set the given
415 * bits can't be executed for that reason
416 * STUB
417 */
418 public function isHideCurrentOp( $newBits ) {
419 return false;
420 }
421
422 /**
423 * Returns true if the current user can view the item
424 */
425 abstract public function canView();
426
427 /**
428 * Returns true if the current user can view the item text/file
429 */
430 abstract public function canViewContent();
431
432 /**
433 * Get the current deletion bitfield value
434 */
435 abstract public function getBits();
436
437 /**
438 * Get the HTML of the list item. Should be include <li></li> tags.
439 * This is used to show the list in HTML form, by the special page.
440 */
441 abstract public function getHTML();
442
443 /**
444 * Set the visibility of the item. This should do any necessary DB queries.
445 *
446 * The DB update query should have a condition which forces it to only update
447 * if the value in the DB matches the value fetched earlier with the SELECT.
448 * If the update fails because it did not match, the function should return
449 * false. This prevents concurrency problems.
450 *
451 * @return boolean success
452 */
453 abstract public function setBits( $newBits );
454 }