Return '' on RevisionDeleter.getLogLinks() if count($paramArray) < 2
[lhc/web/wiklou.git] / includes / RevisionDelete.php
1 <?php
2 /**
3 * Temporary b/c interface, collection of static functions.
4 * @ingroup SpecialPage
5 */
6 class RevisionDeleter {
7 /**
8 * Checks for a change in the bitfield for a certain option and updates the
9 * provided array accordingly.
10 *
11 * @param $desc String: description to add to the array if the option was
12 * enabled / disabled.
13 * @param $field Integer: the bitmask describing the single option.
14 * @param $diff Integer: the xor of the old and new bitfields.
15 * @param $new Integer: the new bitfield
16 * @param $arr Array: the array to update.
17 */
18 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
19 if( $diff & $field ) {
20 $arr[ ( $new & $field ) ? 0 : 1 ][] = $desc;
21 }
22 }
23
24 /**
25 * Gets an array of message keys describing the changes made to the visibility
26 * of the revision. If the resulting array is $arr, then $arr[0] will contain an
27 * array of strings describing the items that were hidden, $arr[2] will contain
28 * an array of strings describing the items that were unhidden, and $arr[3] will
29 * contain an array with a single string, which can be one of "applied
30 * restrictions to sysops", "removed restrictions from sysops", or null.
31 *
32 * @param $n Integer: the new bitfield.
33 * @param $o Integer: the old bitfield.
34 * @return An array as described above.
35 */
36 protected static function getChanges( $n, $o ) {
37 $diff = $n ^ $o;
38 $ret = array( 0 => array(), 1 => array(), 2 => array() );
39 // Build bitfield changes in language
40 self::checkItem( 'revdelete-content',
41 Revision::DELETED_TEXT, $diff, $n, $ret );
42 self::checkItem( 'revdelete-summary',
43 Revision::DELETED_COMMENT, $diff, $n, $ret );
44 self::checkItem( 'revdelete-uname',
45 Revision::DELETED_USER, $diff, $n, $ret );
46 // Restriction application to sysops
47 if( $diff & Revision::DELETED_RESTRICTED ) {
48 if( $n & Revision::DELETED_RESTRICTED )
49 $ret[2][] = 'revdelete-restricted';
50 else
51 $ret[2][] = 'revdelete-unrestricted';
52 }
53 return $ret;
54 }
55
56 /**
57 * Gets a log message to describe the given revision visibility change. This
58 * message will be of the form "[hid {content, edit summary, username}];
59 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
60 *
61 * @param $count Integer: The number of effected revisions.
62 * @param $nbitfield Integer: The new bitfield for the revision.
63 * @param $obitfield Integer: The old bitfield for the revision.
64 * @param $isForLog Boolean
65 * @param $forContent Boolean
66 */
67 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false, $forContent = false ) {
68 global $wgLang, $wgContLang;
69
70 $lang = $forContent ? $wgContLang : $wgLang;
71 $msgFunc = $forContent ? "wfMsgForContent" : "wfMsg";
72
73 $s = '';
74 $changes = self::getChanges( $nbitfield, $obitfield );
75 array_walk($changes, 'RevisionDeleter::expandMessageArray', $forContent);
76
77 $changesText = array();
78
79 if( count( $changes[0] ) ) {
80 $changesText[] = $msgFunc( 'revdelete-hid', $lang->commaList( $changes[0] ) );
81 }
82 if( count( $changes[1] ) ) {
83 $changesText[] = $msgFunc( 'revdelete-unhid', $lang->commaList( $changes[1] ) );
84 }
85
86 $s = $lang->semicolonList( $changesText );
87 if( count( $changes[2] ) ) {
88 $s .= $s ? ' (' . $changes[2][0] . ')' : ' ' . $changes[2][0];
89 }
90
91 $msg = $isForLog ? 'logdelete-log-message' : 'revdelete-log-message';
92 return wfMsgExt( $msg, $forContent ? array( 'parsemag', 'content' ) : array( 'parsemag' ), $s, $lang->formatNum($count) );
93 }
94
95 private static function expandMessageArray(& $msg, $key, $forContent) {
96 if ( is_array ($msg) ) {
97 array_walk($msg, 'RevisionDeleter::expandMessageArray', $forContent);
98 } else {
99 if ( $forContent ) {
100 $msg = wfMsgForContent($msg);
101 } else {
102 $msg = wfMsg($msg);
103 }
104 }
105 }
106
107 // Get DB field name for URL param...
108 // Future code for other things may also track
109 // other types of revision-specific changes.
110 // @returns string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
111 public static function getRelationType( $typeName ) {
112 if ( isset( SpecialRevisionDelete::$deprecatedTypeMap[$typeName] ) ) {
113 $typeName = SpecialRevisionDelete::$deprecatedTypeMap[$typeName];
114 }
115 if ( isset( SpecialRevisionDelete::$allowedTypes[$typeName] ) ) {
116 $class = SpecialRevisionDelete::$allowedTypes[$typeName]['list-class'];
117 $list = new $class( null, null, null );
118 return $list->getIdField();
119 } else {
120 return null;
121 }
122 }
123
124 // Checks if a revision still exists in the revision table.
125 // If it doesn't, returns the corresponding ar_timestamp field
126 // so that this key can be used instead.
127 public static function checkRevisionExistence( $title, $revid ) {
128 $dbr = wfGetDB( DB_SLAVE );
129 $exists = $dbr->selectField( 'revision', '1',
130 array( 'rev_id' => $revid ), __METHOD__ );
131
132 if ( $exists ) {
133 return true;
134 }
135
136 $timestamp = $dbr->selectField( 'archive', 'ar_timestamp',
137 array( 'ar_namespace' => $title->getNamespace(),
138 'ar_title' => $title->getDBkey(),
139 'ar_rev_id' => $revid ), __METHOD__ );
140
141 return $timestamp;
142 }
143
144 // Creates utility links for log entries.
145 public static function getLogLinks( $title, $paramArray, $skin, $messages ) {
146 global $wgLang;
147
148 if( count($paramArray) >= 2 ) {
149 // Different revision types use different URL params...
150 $originalKey = $key = $paramArray[0];
151 // $paramArray[1] is a CSV of the IDs
152 $Ids = explode( ',', $paramArray[1] );
153 $query = $paramArray[1];
154 $revert = array();
155
156 // For if undeleted revisions are found amidst deleted ones.
157 $undeletedRevisions = array();
158
159 // This is not going to work if some revs are deleted and some
160 // aren't.
161 if ($key == 'revision') {
162 foreach( $Ids as $k => $id ) {
163 $existResult =
164 self::checkRevisionExistence( $title, $id );
165
166 if ($existResult !== true) {
167 $key = 'archive';
168 $Ids[$k] = $existResult;
169 } else {
170 // Undeleted revision amidst deleted ones
171 unset($Ids[$k]);
172 $undeletedRevisions[] = $id;
173 }
174 }
175
176 if ( $key == $originalKey ) {
177 $Ids = $undeletedRevisions;
178 $undeletedRevisions = array();
179 }
180 }
181
182 // Diff link for single rev deletions
183 if( count($Ids) == 1 && !count($undeletedRevisions) ) {
184 // Live revision diffs...
185 if( in_array( $key, array( 'oldid', 'revision' ) ) ) {
186 $revert[] = $skin->link(
187 $title,
188 $messages['diff'],
189 array(),
190 array(
191 'diff' => intval( $Ids[0] ),
192 'unhide' => 1
193 ),
194 array( 'known', 'noclasses' )
195 );
196 // Deleted revision diffs...
197 } else if( in_array( $key, array( 'artimestamp','archive' ) ) ) {
198 $revert[] = $skin->link(
199 SpecialPage::getTitleFor( 'Undelete' ),
200 $messages['diff'],
201 array(),
202 array(
203 'target' => $title->getPrefixedDBKey(),
204 'diff' => 'prev',
205 'timestamp' => $Ids[0]
206 ),
207 array( 'known', 'noclasses' )
208 );
209 }
210 }
211
212 // View/modify link...
213 if ( count($undeletedRevisions) ) {
214 // FIXME THIS IS A HORRIBLE HORRIBLE HACK AND SHOULD DIE
215 // It's not possible to pass a list of both deleted and
216 // undeleted revisions to SpecialRevisionDelete, so we're
217 // stuck with two links. See bug 23363.
218 $restoreLinks = array();
219
220 $restoreLinks[] = $skin->link(
221 SpecialPage::getTitleFor( 'Revisiondelete' ),
222 $messages['revdel-restore-visible'],
223 array(),
224 array(
225 'target' => $title->getPrefixedText(),
226 'type' => $originalKey,
227 'ids' => implode(',', $undeletedRevisions),
228 ),
229 array( 'known', 'noclasses' )
230 );
231
232 $restoreLinks[] = $skin->link(
233 SpecialPage::getTitleFor( 'Revisiondelete' ),
234 $messages['revdel-restore-deleted'],
235 array(),
236 array(
237 'target' => $title->getPrefixedText(),
238 'type' => $key,
239 'ids' => implode(',', $Ids),
240 ),
241 array( 'known', 'noclasses' )
242 );
243
244 $revert[] = $messages['revdel-restore'] . ' [' .
245 $wgLang->pipeList( $restoreLinks ) . ']';
246 } else {
247 $revert[] = $skin->link(
248 SpecialPage::getTitleFor( 'Revisiondelete' ),
249 $messages['revdel-restore'],
250 array(),
251 array(
252 'target' => $title->getPrefixedText(),
253 'type' => $key,
254 'ids' => implode(',', $Ids),
255 ),
256 array( 'known', 'noclasses' )
257 );
258 }
259
260 // Pipe links
261 return wfMsg( 'parentheses', $wgLang->pipeList( $revert ) );
262 }
263 return '';
264 }
265 }
266
267 /**
268 * Abstract base class for a list of deletable items
269 */
270 abstract class RevDel_List {
271 var $special, $title, $ids, $res, $current;
272 var $type = null; // override this
273 var $idField = null; // override this
274 var $dateField = false; // override this
275 var $authorIdField = false; // override this
276 var $authorNameField = false; // override this
277
278 /**
279 * @param $special The parent SpecialPage
280 * @param $title The target title
281 * @param $ids Array of IDs
282 */
283 public function __construct( $special, $title, $ids ) {
284 $this->special = $special;
285 $this->title = $title;
286 $this->ids = $ids;
287 }
288
289 /**
290 * Get the internal type name of this list. Equal to the table name.
291 */
292 public function getType() {
293 return $this->type;
294 }
295
296 /**
297 * Get the DB field name associated with the ID list
298 */
299 public function getIdField() {
300 return $this->idField;
301 }
302
303 /**
304 * Get the DB field name storing timestamps
305 */
306 public function getTimestampField() {
307 return $this->dateField;
308 }
309
310 /**
311 * Get the DB field name storing user ids
312 */
313 public function getAuthorIdField() {
314 return $this->authorIdField;
315 }
316
317 /**
318 * Get the DB field name storing user names
319 */
320 public function getAuthorNameField() {
321 return $this->authorNameField;
322 }
323 /**
324 * Set the visibility for the revisions in this list. Logging and
325 * transactions are done here.
326 *
327 * @param $params Associative array of parameters. Members are:
328 * value: The integer value to set the visibility to
329 * comment: The log comment.
330 * @return Status
331 */
332 public function setVisibility( $params ) {
333 $bitPars = $params['value'];
334 $comment = $params['comment'];
335
336 $this->res = false;
337 $dbw = wfGetDB( DB_MASTER );
338 $this->doQuery( $dbw );
339 $dbw->begin();
340 $status = Status::newGood();
341 $missing = array_flip( $this->ids );
342 $this->clearFileOps();
343 $idsForLog = array();
344 $authorIds = $authorIPs = array();
345
346 for ( $this->reset(); $this->current(); $this->next() ) {
347 $item = $this->current();
348 unset( $missing[ $item->getId() ] );
349
350 $oldBits = $item->getBits();
351 // Build the actual new rev_deleted bitfield
352 $newBits = SpecialRevisionDelete::extractBitfield( $bitPars, $oldBits );
353
354 if ( $oldBits == $newBits ) {
355 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
356 $status->failCount++;
357 continue;
358 } elseif ( $oldBits == 0 && $newBits != 0 ) {
359 $opType = 'hide';
360 } elseif ( $oldBits != 0 && $newBits == 0 ) {
361 $opType = 'show';
362 } else {
363 $opType = 'modify';
364 }
365
366 if ( $item->isHideCurrentOp( $newBits ) ) {
367 // Cannot hide current version text
368 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
369 $status->failCount++;
370 continue;
371 }
372 if ( !$item->canView() ) {
373 // Cannot access this revision
374 $msg = ($opType == 'show') ?
375 'revdelete-show-no-access' : 'revdelete-modify-no-access';
376 $status->error( $msg, $item->formatDate(), $item->formatTime() );
377 $status->failCount++;
378 continue;
379 }
380 // Cannot just "hide from Sysops" without hiding any fields
381 if( $newBits == Revision::DELETED_RESTRICTED ) {
382 $status->warning( 'revdelete-only-restricted', $item->formatDate(), $item->formatTime() );
383 $status->failCount++;
384 continue;
385 }
386
387 // Update the revision
388 $ok = $item->setBits( $newBits );
389
390 if ( $ok ) {
391 $idsForLog[] = $item->getId();
392 $status->successCount++;
393 if( $item->getAuthorId() > 0 ) {
394 $authorIds[] = $item->getAuthorId();
395 } else if( IP::isIPAddress( $item->getAuthorName() ) ) {
396 $authorIPs[] = $item->getAuthorName();
397 }
398 } else {
399 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
400 $status->failCount++;
401 }
402 }
403
404 // Handle missing revisions
405 foreach ( $missing as $id => $unused ) {
406 $status->error( 'revdelete-modify-missing', $id );
407 $status->failCount++;
408 }
409
410 if ( $status->successCount == 0 ) {
411 $status->ok = false;
412 $dbw->rollback();
413 return $status;
414 }
415
416 // Save success count
417 $successCount = $status->successCount;
418
419 // Move files, if there are any
420 $status->merge( $this->doPreCommitUpdates() );
421 if ( !$status->isOK() ) {
422 // Fatal error, such as no configured archive directory
423 $dbw->rollback();
424 return $status;
425 }
426
427 // Log it
428 $this->updateLog( array(
429 'title' => $this->title,
430 'count' => $successCount,
431 'newBits' => $newBits,
432 'oldBits' => $oldBits,
433 'comment' => $comment,
434 'ids' => $idsForLog,
435 'authorIds' => $authorIds,
436 'authorIPs' => $authorIPs
437 ) );
438 $dbw->commit();
439
440 // Clear caches
441 $status->merge( $this->doPostCommitUpdates() );
442 return $status;
443 }
444
445 /**
446 * Reload the list data from the master DB. This can be done after setVisibility()
447 * to allow $item->getHTML() to show the new data.
448 */
449 function reloadFromMaster() {
450 $dbw = wfGetDB( DB_MASTER );
451 $this->res = $this->doQuery( $dbw );
452 }
453
454 /**
455 * Record a log entry on the action
456 * @param $params Associative array of parameters:
457 * newBits: The new value of the *_deleted bitfield
458 * oldBits: The old value of the *_deleted bitfield.
459 * title: The target title
460 * ids: The ID list
461 * comment: The log comment
462 * authorsIds: The array of the user IDs of the offenders
463 * authorsIPs: The array of the IP/anon user offenders
464 */
465 protected function updateLog( $params ) {
466 // Get the URL param's corresponding DB field
467 $field = RevisionDeleter::getRelationType( $this->getType() );
468 if( !$field ) {
469 throw new MWException( "Bad log URL param type!" );
470 }
471 // Put things hidden from sysops in the oversight log
472 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
473 $logType = 'suppress';
474 } else {
475 $logType = 'delete';
476 }
477 // Add params for effected page and ids
478 $logParams = $this->getLogParams( $params );
479 // Actually add the deletion log entry
480 $log = new LogPage( $logType );
481 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
482 $params['comment'], $logParams );
483 // Allow for easy searching of deletion log items for revision/log items
484 $log->addRelations( $field, $params['ids'], $logid );
485 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
486 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
487 }
488
489 /**
490 * Get the log action for this list type
491 */
492 public function getLogAction() {
493 return 'revision';
494 }
495
496 /**
497 * Get log parameter array.
498 * @param $params Associative array of log parameters, same as updateLog()
499 * @return array
500 */
501 public function getLogParams( $params ) {
502 return array(
503 $this->getType(),
504 implode( ',', $params['ids'] ),
505 "ofield={$params['oldBits']}",
506 "nfield={$params['newBits']}"
507 );
508 }
509
510 /**
511 * Initialise the current iteration pointer
512 */
513 protected function initCurrent() {
514 $row = $this->res->current();
515 if ( $row ) {
516 $this->current = $this->newItem( $row );
517 } else {
518 $this->current = false;
519 }
520 }
521
522 /**
523 * Start iteration. This must be called before current() or next().
524 * @return First list item
525 */
526 public function reset() {
527 if ( !$this->res ) {
528 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
529 } else {
530 $this->res->rewind();
531 }
532 $this->initCurrent();
533 return $this->current;
534 }
535
536 /**
537 * Get the current list item, or false if we are at the end
538 */
539 public function current() {
540 return $this->current;
541 }
542
543 /**
544 * Move the iteration pointer to the next list item, and return it.
545 */
546 public function next() {
547 $this->res->next();
548 $this->initCurrent();
549 return $this->current;
550 }
551
552 /**
553 * Get the number of items in the list.
554 */
555 public function length() {
556 if( !$this->res ) {
557 return 0;
558 } else {
559 return $this->res->numRows();
560 }
561 }
562
563 /**
564 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
565 * STUB
566 */
567 public function clearFileOps() {
568 }
569
570 /**
571 * A hook for setVisibility(): do batch updates pre-commit.
572 * STUB
573 * @return Status
574 */
575 public function doPreCommitUpdates() {
576 return Status::newGood();
577 }
578
579 /**
580 * A hook for setVisibility(): do any necessary updates post-commit.
581 * STUB
582 * @return Status
583 */
584 public function doPostCommitUpdates() {
585 return Status::newGood();
586 }
587
588 /**
589 * Create an item object from a DB result row
590 * @param $row stdclass
591 */
592 abstract public function newItem( $row );
593
594 /**
595 * Do the DB query to iterate through the objects.
596 * @param $db Database object to use for the query
597 */
598 abstract public function doQuery( $db );
599
600 /**
601 * Get the integer value of the flag used for suppression
602 */
603 abstract public function getSuppressBit();
604 }
605
606 /**
607 * Abstract base class for deletable items
608 */
609 abstract class RevDel_Item {
610 /** The parent SpecialPage */
611 var $special;
612
613 /** The parent RevDel_List */
614 var $list;
615
616 /** The DB result row */
617 var $row;
618
619 /**
620 * @param $list RevDel_List
621 * @param $row DB result row
622 */
623 public function __construct( $list, $row ) {
624 $this->special = $list->special;
625 $this->list = $list;
626 $this->row = $row;
627 }
628
629 /**
630 * Get the ID, as it would appear in the ids URL parameter
631 */
632 public function getId() {
633 $field = $this->list->getIdField();
634 return $this->row->$field;
635 }
636
637 /**
638 * Get the date, formatted with $wgLang
639 */
640 public function formatDate() {
641 global $wgLang;
642 return $wgLang->date( $this->getTimestamp() );
643 }
644
645 /**
646 * Get the time, formatted with $wgLang
647 */
648 public function formatTime() {
649 global $wgLang;
650 return $wgLang->time( $this->getTimestamp() );
651 }
652
653 /**
654 * Get the timestamp in MW 14-char form
655 */
656 public function getTimestamp() {
657 $field = $this->list->getTimestampField();
658 return wfTimestamp( TS_MW, $this->row->$field );
659 }
660
661 /**
662 * Get the author user ID
663 */
664 public function getAuthorId() {
665 $field = $this->list->getAuthorIdField();
666 return intval( $this->row->$field );
667 }
668
669 /**
670 * Get the author user name
671 */
672 public function getAuthorName() {
673 $field = $this->list->getAuthorNameField();
674 return strval( $this->row->$field );
675 }
676
677 /**
678 * Returns true if the item is "current", and the operation to set the given
679 * bits can't be executed for that reason
680 * STUB
681 */
682 public function isHideCurrentOp( $newBits ) {
683 return false;
684 }
685
686 /**
687 * Returns true if the current user can view the item
688 */
689 abstract public function canView();
690
691 /**
692 * Returns true if the current user can view the item text/file
693 */
694 abstract public function canViewContent();
695
696 /**
697 * Get the current deletion bitfield value
698 */
699 abstract public function getBits();
700
701 /**
702 * Get the HTML of the list item. Should be include <li></li> tags.
703 * This is used to show the list in HTML form, by the special page.
704 */
705 abstract public function getHTML();
706
707 /**
708 * Set the visibility of the item. This should do any necessary DB queries.
709 *
710 * The DB update query should have a condition which forces it to only update
711 * if the value in the DB matches the value fetched earlier with the SELECT.
712 * If the update fails because it did not match, the function should return
713 * false. This prevents concurrency problems.
714 *
715 * @return boolean success
716 */
717 abstract public function setBits( $newBits );
718 }
719
720 /**
721 * List for revision table items
722 */
723 class RevDel_RevisionList extends RevDel_List {
724 var $currentRevId;
725 var $type = 'revision';
726 var $idField = 'rev_id';
727 var $dateField = 'rev_timestamp';
728 var $authorIdField = 'rev_user';
729 var $authorNameField = 'rev_user_text';
730
731 public function doQuery( $db ) {
732 $ids = array_map( 'intval', $this->ids );
733 return $db->select( array('revision','page'), '*',
734 array(
735 'rev_page' => $this->title->getArticleID(),
736 'rev_id' => $ids,
737 'rev_page = page_id'
738 ),
739 __METHOD__,
740 array( 'ORDER BY' => 'rev_id DESC' )
741 );
742 }
743
744 public function newItem( $row ) {
745 return new RevDel_RevisionItem( $this, $row );
746 }
747
748 public function getCurrent() {
749 if ( is_null( $this->currentRevId ) ) {
750 $dbw = wfGetDB( DB_MASTER );
751 $this->currentRevId = $dbw->selectField(
752 'page', 'page_latest', $this->title->pageCond(), __METHOD__ );
753 }
754 return $this->currentRevId;
755 }
756
757 public function getSuppressBit() {
758 return Revision::DELETED_RESTRICTED;
759 }
760
761 public function doPreCommitUpdates() {
762 $this->title->invalidateCache();
763 return Status::newGood();
764 }
765
766 public function doPostCommitUpdates() {
767 $this->title->purgeSquid();
768 // Extensions that require referencing previous revisions may need this
769 wfRunHooks( 'ArticleRevisionVisibilitySet', array( &$this->title ) );
770 return Status::newGood();
771 }
772 }
773
774 /**
775 * Item class for a revision table row
776 */
777 class RevDel_RevisionItem extends RevDel_Item {
778 var $revision;
779
780 public function __construct( $list, $row ) {
781 parent::__construct( $list, $row );
782 $this->revision = new Revision( $row );
783 }
784
785 public function canView() {
786 return $this->revision->userCan( Revision::DELETED_RESTRICTED );
787 }
788
789 public function canViewContent() {
790 return $this->revision->userCan( Revision::DELETED_TEXT );
791 }
792
793 public function getBits() {
794 return $this->revision->mDeleted;
795 }
796
797 public function setBits( $bits ) {
798 $dbw = wfGetDB( DB_MASTER );
799 // Update revision table
800 $dbw->update( 'revision',
801 array( 'rev_deleted' => $bits ),
802 array(
803 'rev_id' => $this->revision->getId(),
804 'rev_page' => $this->revision->getPage(),
805 'rev_deleted' => $this->getBits()
806 ),
807 __METHOD__
808 );
809 if ( !$dbw->affectedRows() ) {
810 // Concurrent fail!
811 return false;
812 }
813 // Update recentchanges table
814 $dbw->update( 'recentchanges',
815 array(
816 'rc_deleted' => $bits,
817 'rc_patrolled' => 1
818 ),
819 array(
820 'rc_this_oldid' => $this->revision->getId(), // condition
821 // non-unique timestamp index
822 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
823 ),
824 __METHOD__
825 );
826 return true;
827 }
828
829 public function isDeleted() {
830 return $this->revision->isDeleted( Revision::DELETED_TEXT );
831 }
832
833 public function isHideCurrentOp( $newBits ) {
834 return ( $newBits & Revision::DELETED_TEXT )
835 && $this->list->getCurrent() == $this->getId();
836 }
837
838 /**
839 * Get the HTML link to the revision text.
840 * Overridden by RevDel_ArchiveItem.
841 */
842 protected function getRevisionLink() {
843 global $wgLang;
844 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
845 if ( $this->isDeleted() && !$this->canViewContent() ) {
846 return $date;
847 }
848 return $this->special->skin->link(
849 $this->list->title,
850 $date,
851 array(),
852 array(
853 'oldid' => $this->revision->getId(),
854 'unhide' => 1
855 )
856 );
857 }
858
859 /**
860 * Get the HTML link to the diff.
861 * Overridden by RevDel_ArchiveItem
862 */
863 protected function getDiffLink() {
864 if ( $this->isDeleted() && !$this->canViewContent() ) {
865 return wfMsgHtml('diff');
866 } else {
867 return
868 $this->special->skin->link(
869 $this->list->title,
870 wfMsgHtml('diff'),
871 array(),
872 array(
873 'diff' => $this->revision->getId(),
874 'oldid' => 'prev',
875 'unhide' => 1
876 ),
877 array(
878 'known',
879 'noclasses'
880 )
881 );
882 }
883 }
884
885 public function getHTML() {
886 $difflink = $this->getDiffLink();
887 $revlink = $this->getRevisionLink();
888 $userlink = $this->special->skin->revUserLink( $this->revision );
889 $comment = $this->special->skin->revComment( $this->revision );
890 if ( $this->isDeleted() ) {
891 $revlink = "<span class=\"history-deleted\">$revlink</span>";
892 }
893 return "<li>($difflink) $revlink $userlink $comment</li>";
894 }
895 }
896
897 /**
898 * List for archive table items, i.e. revisions deleted via action=delete
899 */
900 class RevDel_ArchiveList extends RevDel_RevisionList {
901 var $type = 'archive';
902 var $idField = 'ar_timestamp';
903 var $dateField = 'ar_timestamp';
904 var $authorIdField = 'ar_user';
905 var $authorNameField = 'ar_user_text';
906
907 public function doQuery( $db ) {
908 $timestamps = array();
909 foreach ( $this->ids as $id ) {
910 $timestamps[] = $db->timestamp( $id );
911 }
912 return $db->select( 'archive', '*',
913 array(
914 'ar_namespace' => $this->title->getNamespace(),
915 'ar_title' => $this->title->getDBkey(),
916 'ar_timestamp' => $timestamps
917 ),
918 __METHOD__,
919 array( 'ORDER BY' => 'ar_timestamp DESC' )
920 );
921 }
922
923 public function newItem( $row ) {
924 return new RevDel_ArchiveItem( $this, $row );
925 }
926
927 public function doPreCommitUpdates() {
928 return Status::newGood();
929 }
930
931 public function doPostCommitUpdates() {
932 return Status::newGood();
933 }
934 }
935
936 /**
937 * Item class for a archive table row
938 */
939 class RevDel_ArchiveItem extends RevDel_RevisionItem {
940 public function __construct( $list, $row ) {
941 RevDel_Item::__construct( $list, $row );
942 $this->revision = Revision::newFromArchiveRow( $row,
943 array( 'page' => $this->list->title->getArticleId() ) );
944 }
945
946 public function getId() {
947 # Convert DB timestamp to MW timestamp
948 return $this->revision->getTimestamp();
949 }
950
951 public function setBits( $bits ) {
952 $dbw = wfGetDB( DB_MASTER );
953 $dbw->update( 'archive',
954 array( 'ar_deleted' => $bits ),
955 array( 'ar_namespace' => $this->list->title->getNamespace(),
956 'ar_title' => $this->list->title->getDBkey(),
957 // use timestamp for index
958 'ar_timestamp' => $this->row->ar_timestamp,
959 'ar_rev_id' => $this->row->ar_rev_id,
960 'ar_deleted' => $this->getBits()
961 ),
962 __METHOD__ );
963 return (bool)$dbw->affectedRows();
964 }
965
966 protected function getRevisionLink() {
967 global $wgLang;
968 $undelete = SpecialPage::getTitleFor( 'Undelete' );
969 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
970 if ( $this->isDeleted() && !$this->canViewContent() ) {
971 return $date;
972 }
973 return $this->special->skin->link( $undelete, $date, array(),
974 array(
975 'target' => $this->list->title->getPrefixedText(),
976 'timestamp' => $this->revision->getTimestamp()
977 ) );
978 }
979
980 protected function getDiffLink() {
981 if ( $this->isDeleted() && !$this->canViewContent() ) {
982 return wfMsgHtml( 'diff' );
983 }
984 $undelete = SpecialPage::getTitleFor( 'Undelete' );
985 return $this->special->skin->link( $undelete, wfMsgHtml('diff'), array(),
986 array(
987 'target' => $this->list->title->getPrefixedText(),
988 'diff' => 'prev',
989 'timestamp' => $this->revision->getTimestamp()
990 ) );
991 }
992 }
993
994 /**
995 * List for oldimage table items
996 */
997 class RevDel_FileList extends RevDel_List {
998 var $type = 'oldimage';
999 var $idField = 'oi_archive_name';
1000 var $dateField = 'oi_timestamp';
1001 var $authorIdField = 'oi_user';
1002 var $authorNameField = 'oi_user_text';
1003 var $storeBatch, $deleteBatch, $cleanupBatch;
1004
1005 public function doQuery( $db ) {
1006 $archiveName = array();
1007 foreach( $this->ids as $timestamp ) {
1008 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
1009 }
1010 return $db->select( 'oldimage', '*',
1011 array(
1012 'oi_name' => $this->title->getDBkey(),
1013 'oi_archive_name' => $archiveNames
1014 ),
1015 __METHOD__,
1016 array( 'ORDER BY' => 'oi_timestamp DESC' )
1017 );
1018 }
1019
1020 public function newItem( $row ) {
1021 return new RevDel_FileItem( $this, $row );
1022 }
1023
1024 public function clearFileOps() {
1025 $this->deleteBatch = array();
1026 $this->storeBatch = array();
1027 $this->cleanupBatch = array();
1028 }
1029
1030 public function doPreCommitUpdates() {
1031 $status = Status::newGood();
1032 $repo = RepoGroup::singleton()->getLocalRepo();
1033 if ( $this->storeBatch ) {
1034 $status->merge( $repo->storeBatch( $this->storeBatch, FileRepo::OVERWRITE_SAME ) );
1035 }
1036 if ( !$status->isOK() ) {
1037 return $status;
1038 }
1039 if ( $this->deleteBatch ) {
1040 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
1041 }
1042 if ( !$status->isOK() ) {
1043 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1044 // modified (but destined for rollback) causes data loss
1045 return $status;
1046 }
1047 if ( $this->cleanupBatch ) {
1048 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
1049 }
1050 return $status;
1051 }
1052
1053 public function doPostCommitUpdates() {
1054 $file = wfLocalFile( $this->title );
1055 $file->purgeCache();
1056 $file->purgeDescription();
1057 return Status::newGood();
1058 }
1059
1060 public function getSuppressBit() {
1061 return File::DELETED_RESTRICTED;
1062 }
1063 }
1064
1065 /**
1066 * Item class for an oldimage table row
1067 */
1068 class RevDel_FileItem extends RevDel_Item {
1069 var $file;
1070
1071 public function __construct( $list, $row ) {
1072 parent::__construct( $list, $row );
1073 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
1074 }
1075
1076 public function getId() {
1077 $parts = explode( '!', $this->row->oi_archive_name );
1078 return $parts[0];
1079 }
1080
1081 public function canView() {
1082 return $this->file->userCan( File::DELETED_RESTRICTED );
1083 }
1084
1085 public function canViewContent() {
1086 return $this->file->userCan( File::DELETED_FILE );
1087 }
1088
1089 public function getBits() {
1090 return $this->file->getVisibility();
1091 }
1092
1093 public function setBits( $bits ) {
1094 # Queue the file op
1095 # FIXME: move to LocalFile.php
1096 if ( $this->isDeleted() ) {
1097 if ( $bits & File::DELETED_FILE ) {
1098 # Still deleted
1099 } else {
1100 # Newly undeleted
1101 $key = $this->file->getStorageKey();
1102 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
1103 $this->list->storeBatch[] = array(
1104 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1105 'public',
1106 $this->file->getRel()
1107 );
1108 $this->list->cleanupBatch[] = $key;
1109 }
1110 } elseif ( $bits & File::DELETED_FILE ) {
1111 # Newly deleted
1112 $key = $this->file->getStorageKey();
1113 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
1114 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
1115 }
1116
1117 # Do the database operations
1118 $dbw = wfGetDB( DB_MASTER );
1119 $dbw->update( 'oldimage',
1120 array( 'oi_deleted' => $bits ),
1121 array(
1122 'oi_name' => $this->row->oi_name,
1123 'oi_timestamp' => $this->row->oi_timestamp,
1124 'oi_deleted' => $this->getBits()
1125 ),
1126 __METHOD__
1127 );
1128 return (bool)$dbw->affectedRows();
1129 }
1130
1131 public function isDeleted() {
1132 return $this->file->isDeleted( File::DELETED_FILE );
1133 }
1134
1135 /**
1136 * Get the link to the file.
1137 * Overridden by RevDel_ArchivedFileItem.
1138 */
1139 protected function getLink() {
1140 global $wgLang, $wgUser;
1141 $date = $wgLang->timeanddate( $this->file->getTimestamp(), true );
1142 if ( $this->isDeleted() ) {
1143 # Hidden files...
1144 if ( !$this->canViewContent() ) {
1145 $link = $date;
1146 } else {
1147 $link = $this->special->skin->link(
1148 $this->special->getTitle(),
1149 $date, array(),
1150 array(
1151 'target' => $this->list->title->getPrefixedText(),
1152 'file' => $this->file->getArchiveName(),
1153 'token' => $wgUser->editToken( $this->file->getArchiveName() )
1154 )
1155 );
1156 }
1157 return '<span class="history-deleted">' . $link . '</span>';
1158 } else {
1159 # Regular files...
1160 $url = $this->file->getUrl();
1161 return Xml::element( 'a', array( 'href' => $this->file->getUrl() ), $date );
1162 }
1163 }
1164 /**
1165 * Generate a user tool link cluster if the current user is allowed to view it
1166 * @return string HTML
1167 */
1168 protected function getUserTools() {
1169 if( $this->file->userCan( Revision::DELETED_USER ) ) {
1170 $link = $this->special->skin->userLink( $this->file->user, $this->file->user_text ) .
1171 $this->special->skin->userToolLinks( $this->file->user, $this->file->user_text );
1172 } else {
1173 $link = wfMsgHtml( 'rev-deleted-user' );
1174 }
1175 if( $this->file->isDeleted( Revision::DELETED_USER ) ) {
1176 return '<span class="history-deleted">' . $link . '</span>';
1177 }
1178 return $link;
1179 }
1180
1181 /**
1182 * Wrap and format the file's comment block, if the current
1183 * user is allowed to view it.
1184 *
1185 * @return string HTML
1186 */
1187 protected function getComment() {
1188 if( $this->file->userCan( File::DELETED_COMMENT ) ) {
1189 $block = $this->special->skin->commentBlock( $this->file->description );
1190 } else {
1191 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1192 }
1193 if( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
1194 return "<span class=\"history-deleted\">$block</span>";
1195 }
1196 return $block;
1197 }
1198
1199 public function getHTML() {
1200 global $wgLang;
1201 $data =
1202 wfMsg(
1203 'widthheight',
1204 $wgLang->formatNum( $this->file->getWidth() ),
1205 $wgLang->formatNum( $this->file->getHeight() )
1206 ) .
1207 ' (' .
1208 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file->getSize() ) ) .
1209 ')';
1210 $pageLink = $this->getLink();
1211
1212 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1213 $data . ' ' . $this->getComment(). '</li>';
1214 }
1215 }
1216
1217 /**
1218 * List for filearchive table items
1219 */
1220 class RevDel_ArchivedFileList extends RevDel_FileList {
1221 var $type = 'filearchive';
1222 var $idField = 'fa_id';
1223 var $dateField = 'fa_timestamp';
1224 var $authorIdField = 'fa_user';
1225 var $authorNameField = 'fa_user_text';
1226
1227 public function doQuery( $db ) {
1228 $ids = array_map( 'intval', $this->ids );
1229 return $db->select( 'filearchive', '*',
1230 array(
1231 'fa_name' => $this->title->getDBkey(),
1232 'fa_id' => $ids
1233 ),
1234 __METHOD__,
1235 array( 'ORDER BY' => 'fa_id DESC' )
1236 );
1237 }
1238
1239 public function newItem( $row ) {
1240 return new RevDel_ArchivedFileItem( $this, $row );
1241 }
1242 }
1243
1244 /**
1245 * Item class for a filearchive table row
1246 */
1247 class RevDel_ArchivedFileItem extends RevDel_FileItem {
1248 public function __construct( $list, $row ) {
1249 RevDel_Item::__construct( $list, $row );
1250 $this->file = ArchivedFile::newFromRow( $row );
1251 }
1252
1253 public function getId() {
1254 return $this->row->fa_id;
1255 }
1256
1257 public function setBits( $bits ) {
1258 $dbw = wfGetDB( DB_MASTER );
1259 $dbw->update( 'filearchive',
1260 array( 'fa_deleted' => $bits ),
1261 array(
1262 'fa_id' => $this->row->fa_id,
1263 'fa_deleted' => $this->getBits(),
1264 ),
1265 __METHOD__
1266 );
1267 return (bool)$dbw->affectedRows();
1268 }
1269
1270 protected function getLink() {
1271 global $wgLang, $wgUser;
1272 $date = $wgLang->timeanddate( $this->file->getTimestamp(), true );
1273 $undelete = SpecialPage::getTitleFor( 'Undelete' );
1274 $key = $this->file->getKey();
1275 # Hidden files...
1276 if( !$this->canViewContent() ) {
1277 $link = $date;
1278 } else {
1279 $link = $this->special->skin->link( $undelete, $date, array(),
1280 array(
1281 'target' => $this->list->title->getPrefixedText(),
1282 'file' => $key,
1283 'token' => $wgUser->editToken( $key )
1284 )
1285 );
1286 }
1287 if( $this->isDeleted() ) {
1288 $link = '<span class="history-deleted">' . $link . '</span>';
1289 }
1290 return $link;
1291 }
1292 }
1293
1294 /**
1295 * List for logging table items
1296 */
1297 class RevDel_LogList extends RevDel_List {
1298 var $type = 'logging';
1299 var $idField = 'log_id';
1300 var $dateField = 'log_timestamp';
1301 var $authorIdField = 'log_user';
1302 var $authorNameField = 'log_user_text';
1303
1304 public function doQuery( $db ) {
1305 global $wgMessageCache;
1306 $wgMessageCache->loadAllMessages();
1307 $ids = array_map( 'intval', $this->ids );
1308 return $db->select( 'logging', '*',
1309 array( 'log_id' => $ids ),
1310 __METHOD__,
1311 array( 'ORDER BY' => 'log_id DESC' )
1312 );
1313 }
1314
1315 public function newItem( $row ) {
1316 return new RevDel_LogItem( $this, $row );
1317 }
1318
1319 public function getSuppressBit() {
1320 return Revision::DELETED_RESTRICTED;
1321 }
1322
1323 public function getLogAction() {
1324 return 'event';
1325 }
1326
1327 public function getLogParams( $params ) {
1328 return array(
1329 implode( ',', $params['ids'] ),
1330 "ofield={$params['oldBits']}",
1331 "nfield={$params['newBits']}"
1332 );
1333 }
1334 }
1335
1336 /**
1337 * Item class for a logging table row
1338 */
1339 class RevDel_LogItem extends RevDel_Item {
1340 public function canView() {
1341 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED );
1342 }
1343
1344 public function canViewContent() {
1345 return true; // none
1346 }
1347
1348 public function getBits() {
1349 return $this->row->log_deleted;
1350 }
1351
1352 public function setBits( $bits ) {
1353 $dbw = wfGetDB( DB_MASTER );
1354 $dbw->update( 'recentchanges',
1355 array(
1356 'rc_deleted' => $bits,
1357 'rc_patrolled' => 1
1358 ),
1359 array(
1360 'rc_logid' => $this->row->log_id,
1361 'rc_timestamp' => $this->row->log_timestamp // index
1362 ),
1363 __METHOD__
1364 );
1365 $dbw->update( 'logging',
1366 array( 'log_deleted' => $bits ),
1367 array(
1368 'log_id' => $this->row->log_id,
1369 'log_deleted' => $this->getBits()
1370 ),
1371 __METHOD__
1372 );
1373 return (bool)$dbw->affectedRows();
1374 }
1375
1376 public function getHTML() {
1377 global $wgLang;
1378
1379 $date = htmlspecialchars( $wgLang->timeanddate( $this->row->log_timestamp ) );
1380 $paramArray = LogPage::extractParams( $this->row->log_params );
1381 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
1382
1383 // Log link for this page
1384 $loglink = $this->special->skin->link(
1385 SpecialPage::getTitleFor( 'Log' ),
1386 wfMsgHtml( 'log' ),
1387 array(),
1388 array( 'page' => $title->getPrefixedText() )
1389 );
1390 // Action text
1391 if( !$this->canView() ) {
1392 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1393 } else {
1394 $action = LogPage::actionText( $this->row->log_type, $this->row->log_action, $title,
1395 $this->special->skin, $paramArray, true, true );
1396 if( $this->row->log_deleted & LogPage::DELETED_ACTION )
1397 $action = '<span class="history-deleted">' . $action . '</span>';
1398 }
1399 // User links
1400 $userLink = $this->special->skin->userLink( $this->row->log_user,
1401 User::WhoIs( $this->row->log_user ) );
1402 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_USER) ) {
1403 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1404 }
1405 // Comment
1406 $comment = $wgLang->getDirMark() . $this->special->skin->commentBlock( $this->row->log_comment );
1407 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_COMMENT) ) {
1408 $comment = '<span class="history-deleted">' . $comment . '</span>';
1409 }
1410 return "<li>($loglink) $date $userLink $action $comment</li>";
1411 }
1412 }