4 * Abstract base class for a list of deletable items
6 abstract class RevDel_List
{
13 var $special, $ids, $res, $current;
14 var $type = null; // override this
15 var $idField = null; // override this
16 var $dateField = false; // override this
17 var $authorIdField = false; // override this
18 var $authorNameField = false; // override this
21 * @param $special The parent SpecialPage
22 * @param $title The target title
23 * @param $ids Array of IDs
25 public function __construct( $special, $title, $ids ) {
26 $this->special
= $special;
27 $this->title
= $title;
32 * Get the internal type name of this list. Equal to the table name.
34 public function getType() {
39 * Get the DB field name associated with the ID list
41 public function getIdField() {
42 return $this->idField
;
46 * Get the DB field name storing timestamps
48 public function getTimestampField() {
49 return $this->dateField
;
53 * Get the DB field name storing user ids
55 public function getAuthorIdField() {
56 return $this->authorIdField
;
60 * Get the DB field name storing user names
62 public function getAuthorNameField() {
63 return $this->authorNameField
;
66 * Set the visibility for the revisions in this list. Logging and
67 * transactions are done here.
69 * @param $params Associative array of parameters. Members are:
70 * value: The integer value to set the visibility to
71 * comment: The log comment.
74 public function setVisibility( $params ) {
75 $bitPars = $params['value'];
76 $comment = $params['comment'];
79 $dbw = wfGetDB( DB_MASTER
);
80 $this->doQuery( $dbw );
82 $status = Status
::newGood();
83 $missing = array_flip( $this->ids
);
84 $this->clearFileOps();
86 $authorIds = $authorIPs = array();
88 for ( $this->reset(); $this->current(); $this->next() ) {
89 $item = $this->current();
90 unset( $missing[ $item->getId() ] );
92 $oldBits = $item->getBits();
93 // Build the actual new rev_deleted bitfield
94 $newBits = SpecialRevisionDelete
::extractBitfield( $bitPars, $oldBits );
96 if ( $oldBits == $newBits ) {
97 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
100 } elseif ( $oldBits == 0 && $newBits != 0 ) {
102 } elseif ( $oldBits != 0 && $newBits == 0 ) {
108 if ( $item->isHideCurrentOp( $newBits ) ) {
109 // Cannot hide current version text
110 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
111 $status->failCount++
;
114 if ( !$item->canView() ) {
115 // Cannot access this revision
116 $msg = ($opType == 'show') ?
117 'revdelete-show-no-access' : 'revdelete-modify-no-access';
118 $status->error( $msg, $item->formatDate(), $item->formatTime() );
119 $status->failCount++
;
122 // Cannot just "hide from Sysops" without hiding any fields
123 if( $newBits == Revision
::DELETED_RESTRICTED
) {
124 $status->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
125 $status->failCount++
;
129 // Update the revision
130 $ok = $item->setBits( $newBits );
133 $idsForLog[] = $item->getId();
134 $status->successCount++
;
135 if( $item->getAuthorId() > 0 ) {
136 $authorIds[] = $item->getAuthorId();
137 } else if( IP
::isIPAddress( $item->getAuthorName() ) ) {
138 $authorIPs[] = $item->getAuthorName();
141 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
142 $status->failCount++
;
146 // Handle missing revisions
147 foreach ( $missing as $id => $unused ) {
148 $status->error( 'revdelete-modify-missing', $id );
149 $status->failCount++
;
152 if ( $status->successCount
== 0 ) {
158 // Save success count
159 $successCount = $status->successCount
;
161 // Move files, if there are any
162 $status->merge( $this->doPreCommitUpdates() );
163 if ( !$status->isOK() ) {
164 // Fatal error, such as no configured archive directory
170 $this->updateLog( array(
171 'title' => $this->title
,
172 'count' => $successCount,
173 'newBits' => $newBits,
174 'oldBits' => $oldBits,
175 'comment' => $comment,
177 'authorIds' => $authorIds,
178 'authorIPs' => $authorIPs
183 $status->merge( $this->doPostCommitUpdates() );
188 * Reload the list data from the master DB. This can be done after setVisibility()
189 * to allow $item->getHTML() to show the new data.
191 function reloadFromMaster() {
192 $dbw = wfGetDB( DB_MASTER
);
193 $this->res
= $this->doQuery( $dbw );
197 * Record a log entry on the action
198 * @param $params Associative array of parameters:
199 * newBits: The new value of the *_deleted bitfield
200 * oldBits: The old value of the *_deleted bitfield.
201 * title: The target title
203 * comment: The log comment
204 * authorsIds: The array of the user IDs of the offenders
205 * authorsIPs: The array of the IP/anon user offenders
207 protected function updateLog( $params ) {
208 // Get the URL param's corresponding DB field
209 $field = RevisionDeleter
::getRelationType( $this->getType() );
211 throw new MWException( "Bad log URL param type!" );
213 // Put things hidden from sysops in the oversight log
214 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
215 $logType = 'suppress';
219 // Add params for effected page and ids
220 $logParams = $this->getLogParams( $params );
221 // Actually add the deletion log entry
222 $log = new LogPage( $logType );
223 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
224 $params['comment'], $logParams );
225 // Allow for easy searching of deletion log items for revision/log items
226 $log->addRelations( $field, $params['ids'], $logid );
227 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
228 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
232 * Get the log action for this list type
234 public function getLogAction() {
239 * Get log parameter array.
240 * @param $params Associative array of log parameters, same as updateLog()
243 public function getLogParams( $params ) {
246 implode( ',', $params['ids'] ),
247 "ofield={$params['oldBits']}",
248 "nfield={$params['newBits']}"
253 * Initialise the current iteration pointer
255 protected function initCurrent() {
256 $row = $this->res
->current();
258 $this->current
= $this->newItem( $row );
260 $this->current
= false;
265 * Start iteration. This must be called before current() or next().
266 * @return First list item
268 public function reset() {
270 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
272 $this->res
->rewind();
274 $this->initCurrent();
275 return $this->current
;
279 * Get the current list item, or false if we are at the end
281 public function current() {
282 return $this->current
;
286 * Move the iteration pointer to the next list item, and return it.
288 public function next() {
290 $this->initCurrent();
291 return $this->current
;
295 * Get the number of items in the list.
297 public function length() {
301 return $this->res
->numRows();
306 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
309 public function clearFileOps() {
313 * A hook for setVisibility(): do batch updates pre-commit.
317 public function doPreCommitUpdates() {
318 return Status
::newGood();
322 * A hook for setVisibility(): do any necessary updates post-commit.
326 public function doPostCommitUpdates() {
327 return Status
::newGood();
331 * Create an item object from a DB result row
332 * @param $row stdclass
334 abstract public function newItem( $row );
337 * Do the DB query to iterate through the objects.
338 * @param $db DatabaseBase object to use for the query
340 abstract public function doQuery( $db );
343 * Get the integer value of the flag used for suppression
345 abstract public function getSuppressBit();
349 * Abstract base class for deletable items
351 abstract class RevDel_Item
{
352 /** The parent SpecialPage */
355 /** The parent RevDel_List */
358 /** The DB result row */
362 * @param $list RevDel_List
363 * @param $row DB result row
365 public function __construct( $list, $row ) {
366 $this->special
= $list->special
;
372 * Get the ID, as it would appear in the ids URL parameter
374 public function getId() {
375 $field = $this->list->getIdField();
376 return $this->row
->$field;
380 * Get the date, formatted with $wgLang
382 public function formatDate() {
384 return $wgLang->date( $this->getTimestamp() );
388 * Get the time, formatted with $wgLang
390 public function formatTime() {
392 return $wgLang->time( $this->getTimestamp() );
396 * Get the timestamp in MW 14-char form
398 public function getTimestamp() {
399 $field = $this->list->getTimestampField();
400 return wfTimestamp( TS_MW
, $this->row
->$field );
404 * Get the author user ID
406 public function getAuthorId() {
407 $field = $this->list->getAuthorIdField();
408 return intval( $this->row
->$field );
412 * Get the author user name
414 public function getAuthorName() {
415 $field = $this->list->getAuthorNameField();
416 return strval( $this->row
->$field );
420 * Returns true if the item is "current", and the operation to set the given
421 * bits can't be executed for that reason
424 public function isHideCurrentOp( $newBits ) {
429 * Returns true if the current user can view the item
431 abstract public function canView();
434 * Returns true if the current user can view the item text/file
436 abstract public function canViewContent();
439 * Get the current deletion bitfield value
441 abstract public function getBits();
444 * Get the HTML of the list item. Should be include <li></li> tags.
445 * This is used to show the list in HTML form, by the special page.
447 abstract public function getHTML();
450 * Set the visibility of the item. This should do any necessary DB queries.
452 * The DB update query should have a condition which forces it to only update
453 * if the value in the DB matches the value fetched earlier with the SELECT.
454 * If the update fails because it did not match, the function should return
455 * false. This prevents concurrency problems.
457 * @return boolean success
459 abstract public function setBits( $newBits );