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