4436a87d918e22dc2aabe2b011b8e03cd8cc53a5
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.
9 abstract class RevDel_List
{
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
24 * @param $special The parent SpecialPage
25 * @param $title The target title
26 * @param $ids Array of IDs
28 public function __construct( $special, $title, $ids ) {
29 $this->special
= $special;
30 $this->title
= $title;
35 * Get the internal type name of this list. Equal to the table name.
37 public function getType() {
42 * Get the DB field name associated with the ID list
44 public function getIdField() {
45 return $this->idField
;
49 * Get the DB field name storing timestamps
51 public function getTimestampField() {
52 return $this->dateField
;
56 * Get the DB field name storing user ids
58 public function getAuthorIdField() {
59 return $this->authorIdField
;
63 * Get the DB field name storing user names
65 public function getAuthorNameField() {
66 return $this->authorNameField
;
69 * Set the visibility for the revisions in this list. Logging and
70 * transactions are done here.
72 * @param $params Associative array of parameters. Members are:
73 * value: The integer value to set the visibility to
74 * comment: The log comment.
77 public function setVisibility( $params ) {
78 $bitPars = $params['value'];
79 $comment = $params['comment'];
82 $dbw = wfGetDB( DB_MASTER
);
83 $this->doQuery( $dbw );
85 $status = Status
::newGood();
86 $missing = array_flip( $this->ids
);
87 $this->clearFileOps();
89 $authorIds = $authorIPs = array();
91 for ( $this->reset(); $this->current(); $this->next() ) {
92 $item = $this->current();
93 unset( $missing[ $item->getId() ] );
95 $oldBits = $item->getBits();
96 // Build the actual new rev_deleted bitfield
97 $newBits = SpecialRevisionDelete
::extractBitfield( $bitPars, $oldBits );
99 if ( $oldBits == $newBits ) {
100 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
101 $status->failCount++
;
103 } elseif ( $oldBits == 0 && $newBits != 0 ) {
105 } elseif ( $oldBits != 0 && $newBits == 0 ) {
111 if ( $item->isHideCurrentOp( $newBits ) ) {
112 // Cannot hide current version text
113 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
114 $status->failCount++
;
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++
;
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++
;
132 // Update the revision
133 $ok = $item->setBits( $newBits );
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();
144 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
145 $status->failCount++
;
149 // Handle missing revisions
150 foreach ( $missing as $id => $unused ) {
151 $status->error( 'revdelete-modify-missing', $id );
152 $status->failCount++
;
155 if ( $status->successCount
== 0 ) {
161 // Save success count
162 $successCount = $status->successCount
;
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
173 $this->updateLog( array(
174 'title' => $this->title
,
175 'count' => $successCount,
176 'newBits' => $newBits,
177 'oldBits' => $oldBits,
178 'comment' => $comment,
180 'authorIds' => $authorIds,
181 'authorIPs' => $authorIPs
186 $status->merge( $this->doPostCommitUpdates() );
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.
194 function reloadFromMaster() {
195 $dbw = wfGetDB( DB_MASTER
);
196 $this->res
= $this->doQuery( $dbw );
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
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
210 protected function updateLog( $params ) {
211 // Get the URL param's corresponding DB field
212 $field = RevisionDeleter
::getRelationType( $this->getType() );
214 throw new MWException( "Bad log URL param type!" );
216 // Put things hidden from sysops in the oversight log
217 if ( ( $params['newBits'] |
$params['oldBits'] ) & $this->getSuppressBit() ) {
218 $logType = 'suppress';
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 );
235 * Get the log action for this list type
237 public function getLogAction() {
242 * Get log parameter array.
243 * @param $params Associative array of log parameters, same as updateLog()
246 public function getLogParams( $params ) {
249 implode( ',', $params['ids'] ),
250 "ofield={$params['oldBits']}",
251 "nfield={$params['newBits']}"
256 * Initialise the current iteration pointer
258 protected function initCurrent() {
259 $row = $this->res
->current();
261 $this->current
= $this->newItem( $row );
263 $this->current
= false;
268 * Start iteration. This must be called before current() or next().
269 * @return First list item
271 public function reset() {
273 $this->res
= $this->doQuery( wfGetDB( DB_SLAVE
) );
275 $this->res
->rewind();
277 $this->initCurrent();
278 return $this->current
;
282 * Get the current list item, or false if we are at the end
284 public function current() {
285 return $this->current
;
289 * Move the iteration pointer to the next list item, and return it.
291 public function next() {
293 $this->initCurrent();
294 return $this->current
;
298 * Get the number of items in the list.
300 public function length() {
304 return $this->res
->numRows();
309 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
312 public function clearFileOps() {
316 * A hook for setVisibility(): do batch updates pre-commit.
320 public function doPreCommitUpdates() {
321 return Status
::newGood();
325 * A hook for setVisibility(): do any necessary updates post-commit.
329 public function doPostCommitUpdates() {
330 return Status
::newGood();
334 * Create an item object from a DB result row
335 * @param $row stdclass
337 abstract public function newItem( $row );
340 * Do the DB query to iterate through the objects.
341 * @param $db DatabaseBase object to use for the query
343 abstract public function doQuery( $db );
346 * Get the integer value of the flag used for suppression
348 abstract public function getSuppressBit();
352 * Abstract base class for deletable items
354 abstract class RevDel_Item
{
355 /** The parent SpecialPage */
358 /** The parent RevDel_List */
361 /** The DB result row */
365 * @param $list RevDel_List
366 * @param $row DB result row
368 public function __construct( $list, $row ) {
369 $this->special
= $list->special
;
375 * Get the ID, as it would appear in the ids URL parameter
377 public function getId() {
378 $field = $this->list->getIdField();
379 return $this->row
->$field;
383 * Get the date, formatted with $wgLang
385 public function formatDate() {
387 return $wgLang->date( $this->getTimestamp() );
391 * Get the time, formatted with $wgLang
393 public function formatTime() {
395 return $wgLang->time( $this->getTimestamp() );
399 * Get the timestamp in MW 14-char form
401 public function getTimestamp() {
402 $field = $this->list->getTimestampField();
403 return wfTimestamp( TS_MW
, $this->row
->$field );
407 * Get the author user ID
409 public function getAuthorId() {
410 $field = $this->list->getAuthorIdField();
411 return intval( $this->row
->$field );
415 * Get the author user name
417 public function getAuthorName() {
418 $field = $this->list->getAuthorNameField();
419 return strval( $this->row
->$field );
423 * Returns true if the item is "current", and the operation to set the given
424 * bits can't be executed for that reason
427 public function isHideCurrentOp( $newBits ) {
432 * Returns true if the current user can view the item
434 abstract public function canView();
437 * Returns true if the current user can view the item text/file
439 abstract public function canViewContent();
442 * Get the current deletion bitfield value
444 abstract public function getBits();
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.
450 abstract public function getHTML();
453 * Set the visibility of the item. This should do any necessary DB queries.
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.
460 * @return boolean success
462 abstract public function setBits( $newBits );