4 * Abstract base class for a list of deletable items
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
15 * @param $special The parent SpecialPage
16 * @param $title The target title
17 * @param $ids Array of IDs
19 public function __construct( $special, $title, $ids ) {
20 $this->special
= $special;
21 $this->title
= $title;
26 * Get the internal type name of this list. Equal to the table name.
28 public function getType() {
33 * Get the DB field name associated with the ID list
35 public function getIdField() {
36 return $this->idField
;
40 * Get the DB field name storing timestamps
42 public function getTimestampField() {
43 return $this->dateField
;
47 * Get the DB field name storing user ids
49 public function getAuthorIdField() {
50 return $this->authorIdField
;
54 * Get the DB field name storing user names
56 public function getAuthorNameField() {
57 return $this->authorNameField
;
60 * Set the visibility for the revisions in this list. Logging and
61 * transactions are done here.
63 * @param $params Associative array of parameters. Members are:
64 * value: The integer value to set the visibility to
65 * comment: The log comment.
68 public function setVisibility( $params ) {
69 $bitPars = $params['value'];
70 $comment = $params['comment'];
73 $dbw = wfGetDB( DB_MASTER
);
74 $this->doQuery( $dbw );
76 $status = Status
::newGood();
77 $missing = array_flip( $this->ids
);
78 $this->clearFileOps();
80 $authorIds = $authorIPs = array();
82 for ( $this->reset(); $this->current(); $this->next() ) {
83 $item = $this->current();
84 unset( $missing[ $item->getId() ] );
86 $oldBits = $item->getBits();
87 // Build the actual new rev_deleted bitfield
88 $newBits = SpecialRevisionDelete
::extractBitfield( $bitPars, $oldBits );
90 if ( $oldBits == $newBits ) {
91 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
94 } elseif ( $oldBits == 0 && $newBits != 0 ) {
96 } elseif ( $oldBits != 0 && $newBits == 0 ) {
102 if ( $item->isHideCurrentOp( $newBits ) ) {
103 // Cannot hide current version text
104 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
105 $status->failCount++
;
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++
;
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++
;
123 // Update the revision
124 $ok = $item->setBits( $newBits );
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();
135 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
136 $status->failCount++
;
140 // Handle missing revisions
141 foreach ( $missing as $id => $unused ) {
142 $status->error( 'revdelete-modify-missing', $id );
143 $status->failCount++
;
146 if ( $status->successCount
== 0 ) {
152 // Save success count
153 $successCount = $status->successCount
;
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
164 $this->updateLog( array(
165 'title' => $this->title
,
166 'count' => $successCount,
167 'newBits' => $newBits,
168 'oldBits' => $oldBits,
169 'comment' => $comment,
171 'authorIds' => $authorIds,
172 'authorIPs' => $authorIPs
177 $status->merge( $this->doPostCommitUpdates() );
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.
185 function reloadFromMaster() {
186 $dbw = wfGetDB( DB_MASTER
);
187 $this->res
= $this->doQuery( $dbw );
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
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
201 protected function updateLog( $params ) {
202 // Get the URL param's corresponding DB field
203 $field = RevisionDeleter
::getRelationType( $this->getType() );
205 throw new MWException( "Bad log URL param type!" );
207 // Put things hidden from sysops in the oversight log
208 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
209 $logType = 'suppress';
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 );
226 * Get the log action for this list type
228 public function getLogAction() {
233 * Get log parameter array.
234 * @param $params Associative array of log parameters, same as updateLog()
237 public function getLogParams( $params ) {
240 implode( ',', $params['ids'] ),
241 "ofield={$params['oldBits']}",
242 "nfield={$params['newBits']}"
247 * Initialise the current iteration pointer
249 protected function initCurrent() {
250 $row = $this->res
->current();
252 $this->current
= $this->newItem( $row );
254 $this->current
= false;
259 * Start iteration. This must be called before current() or next().
260 * @return First list item
262 public function reset() {
264 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
266 $this->res
->rewind();
268 $this->initCurrent();
269 return $this->current
;
273 * Get the current list item, or false if we are at the end
275 public function current() {
276 return $this->current
;
280 * Move the iteration pointer to the next list item, and return it.
282 public function next() {
284 $this->initCurrent();
285 return $this->current
;
289 * Get the number of items in the list.
291 public function length() {
295 return $this->res
->numRows();
300 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
303 public function clearFileOps() {
307 * A hook for setVisibility(): do batch updates pre-commit.
311 public function doPreCommitUpdates() {
312 return Status
::newGood();
316 * A hook for setVisibility(): do any necessary updates post-commit.
320 public function doPostCommitUpdates() {
321 return Status
::newGood();
325 * Create an item object from a DB result row
326 * @param $row stdclass
328 abstract public function newItem( $row );
331 * Do the DB query to iterate through the objects.
332 * @param $db Database object to use for the query
334 abstract public function doQuery( $db );
337 * Get the integer value of the flag used for suppression
339 abstract public function getSuppressBit();
343 * Abstract base class for deletable items
345 abstract class RevDel_Item
{
346 /** The parent SpecialPage */
349 /** The parent RevDel_List */
352 /** The DB result row */
356 * @param $list RevDel_List
357 * @param $row DB result row
359 public function __construct( $list, $row ) {
360 $this->special
= $list->special
;
366 * Get the ID, as it would appear in the ids URL parameter
368 public function getId() {
369 $field = $this->list->getIdField();
370 return $this->row
->$field;
374 * Get the date, formatted with $wgLang
376 public function formatDate() {
378 return $wgLang->date( $this->getTimestamp() );
382 * Get the time, formatted with $wgLang
384 public function formatTime() {
386 return $wgLang->time( $this->getTimestamp() );
390 * Get the timestamp in MW 14-char form
392 public function getTimestamp() {
393 $field = $this->list->getTimestampField();
394 return wfTimestamp( TS_MW
, $this->row
->$field );
398 * Get the author user ID
400 public function getAuthorId() {
401 $field = $this->list->getAuthorIdField();
402 return intval( $this->row
->$field );
406 * Get the author user name
408 public function getAuthorName() {
409 $field = $this->list->getAuthorNameField();
410 return strval( $this->row
->$field );
414 * Returns true if the item is "current", and the operation to set the given
415 * bits can't be executed for that reason
418 public function isHideCurrentOp( $newBits ) {
423 * Returns true if the current user can view the item
425 abstract public function canView();
428 * Returns true if the current user can view the item text/file
430 abstract public function canViewContent();
433 * Get the current deletion bitfield value
435 abstract public function getBits();
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.
441 abstract public function getHTML();
444 * Set the visibility of the item. This should do any necessary DB queries.
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.
451 * @return boolean success
453 abstract public function setBits( $newBits );