* Split off deletedrevision (view only) right and give it to sysops
[lhc/web/wiklou.git] / includes / specials / SpecialRevisiondelete.php
1 <?php
2 /**
3 * Special page allowing users with the appropriate permissions to view
4 * and hide revisions. Log items can also be hidden.
5 *
6 * @file
7 * @ingroup SpecialPage
8 */
9
10 class SpecialRevisionDelete extends UnlistedSpecialPage {
11 /** Skin object */
12 var $skin;
13
14 /** True if the submit button was clicked, and the form was posted */
15 var $submitClicked;
16
17 /** Target ID list */
18 var $ids;
19
20 /** Archive name, for reviewing deleted files */
21 var $archiveName;
22
23 /** Edit token for securing image views against XSS */
24 var $token;
25
26 /** Title object for target parameter */
27 var $targetObj;
28
29 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
30 var $typeName;
31
32 /** Array of checkbox specs (message, name, deletion bits) */
33 var $checks;
34
35 /** Information about the current type */
36 var $typeInfo;
37
38 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
39 var $list;
40
41 /**
42 * Assorted information about each type, needed by the special page.
43 * TODO Move some of this to the list class
44 */
45 static $allowedTypes = array(
46 'revision' => array(
47 'check-label' => 'revdelete-hide-text',
48 'deletion-bits' => Revision::DELETED_TEXT,
49 'success' => 'revdelete-success',
50 'failure' => 'revdelete-failure',
51 'list-class' => 'RevDel_RevisionList',
52 ),
53 'archive' => array(
54 'check-label' => 'revdelete-hide-text',
55 'deletion-bits' => Revision::DELETED_TEXT,
56 'success' => 'revdelete-success',
57 'failure' => 'revdelete-failure',
58 'list-class' => 'RevDel_ArchiveList',
59 ),
60 'oldimage'=> array(
61 'check-label' => 'revdelete-hide-image',
62 'deletion-bits' => File::DELETED_FILE,
63 'success' => 'revdelete-success',
64 'failure' => 'revdelete-failure',
65 'list-class' => 'RevDel_FileList',
66 ),
67 'filearchive' => array(
68 'check-label' => 'revdelete-hide-image',
69 'deletion-bits' => File::DELETED_FILE,
70 'success' => 'revdelete-success',
71 'failure' => 'revdelete-failure',
72 'list-class' => 'RevDel_ArchivedFileList',
73 ),
74 'logging' => array(
75 'check-label' => 'revdelete-hide-name',
76 'deletion-bits' => LogPage::DELETED_ACTION,
77 'success' => 'logdelete-success',
78 'failure' => 'logdelete-failure',
79 'list-class' => 'RevDel_LogList',
80 ),
81 );
82
83 /** Type map to support old log entries */
84 static $deprecatedTypeMap = array(
85 'oldid' => 'revision',
86 'artimestamp' => 'archive',
87 'oldimage' => 'oldimage',
88 'fileid' => 'filearchive',
89 'logid' => 'logging',
90 );
91
92 public function __construct() {
93 parent::__construct( 'Revisiondelete', 'deletedrevision' );
94 }
95
96 public function execute( $par ) {
97 global $wgOut, $wgUser, $wgRequest;
98 if( !$wgUser->isAllowed( 'deletedrevision' ) ) {
99 $wgOut->permissionRequired( 'deletedrevision' );
100 return;
101 } else if( wfReadOnly() ) {
102 $wgOut->readOnlyPage();
103 return;
104 }
105 $this->mIsAllowed = $wgUser->isAllowed('deleterevision'); // for changes
106 $this->skin = $wgUser->getSkin();
107 $this->setHeaders();
108 $this->outputHeader();
109 $this->submitClicked = $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
110 # Handle our many different possible input types.
111 $ids = $wgRequest->getVal( 'ids' );
112 if ( !is_null( $ids ) ) {
113 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
114 $this->ids = explode( ',', $ids );
115 } else {
116 # Array input
117 $this->ids = array_keys( $wgRequest->getArray('ids',array()) );
118 }
119 // $this->ids = array_map( 'intval', $this->ids );
120 $this->ids = array_unique( array_filter( $this->ids ) );
121
122 if ( $wgRequest->getVal( 'action' ) == 'revisiondelete' ) {
123 # For show/hide form submission from history page
124 $this->targetObj = $GLOBALS['wgTitle'];
125 $this->typeName = 'revision';
126 } else {
127 $this->typeName = $wgRequest->getVal( 'type' );
128 $this->targetObj = Title::newFromText( $wgRequest->getText( 'target' ) );
129 }
130
131 # For reviewing deleted files...
132 $this->archiveName = $wgRequest->getVal( 'file' );
133 $this->token = $wgRequest->getVal( 'token' );
134 if ( $this->archiveName && $this->targetObj ) {
135 $this->tryShowFile( $this->archiveName );
136 return;
137 }
138
139 if ( isset( self::$deprecatedTypeMap[$this->typeName] ) ) {
140 $this->typeName = self::$deprecatedTypeMap[$this->typeName];
141 }
142
143 # No targets?
144 if( !isset( self::$allowedTypes[$this->typeName] ) || count( $this->ids ) == 0 ) {
145 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
146 return;
147 }
148 $this->typeInfo = self::$allowedTypes[$this->typeName];
149
150 # If we have revisions, get the title from the first one
151 # since they should all be from the same page. This allows
152 # for more flexibility with page moves...
153 if( $this->typeName == 'revision' ) {
154 $rev = Revision::newFromId( $this->ids[0] );
155 $this->targetObj = $rev ? $rev->getTitle() : $this->targetObj;
156 }
157
158 $this->otherReason = $wgRequest->getVal( 'wpReason' );
159 # We need a target page!
160 if( is_null($this->targetObj) ) {
161 $wgOut->addWikiMsg( 'undelete-header' );
162 return;
163 }
164 # Give a link to the logs/hist for this page
165 $this->showConvenienceLinks();
166
167 # Initialise checkboxes
168 $this->checks = array(
169 array( $this->typeInfo['check-label'], 'wpHidePrimary', $this->typeInfo['deletion-bits'] ),
170 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
171 array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER )
172 );
173 if( $wgUser->isAllowed('suppressrevision') ) {
174 $this->checks[] = array( 'revdelete-hide-restricted',
175 'wpHideRestricted', Revision::DELETED_RESTRICTED );
176 }
177
178 # Either submit or create our form
179 if( $this->mIsAllowed && $this->submitClicked ) {
180 $this->submit( $wgRequest );
181 } else {
182 $this->showForm();
183 }
184 $qc = $this->getLogQueryCond();
185 # Show relevant lines from the deletion log
186 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
187 LogEventsList::showLogExtract( $wgOut, 'delete',
188 $this->targetObj->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
189 # Show relevant lines from the suppression log
190 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
191 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'suppress' ) ) . "</h2>\n" );
192 LogEventsList::showLogExtract( $wgOut, 'suppress',
193 $this->targetObj->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
194 }
195 }
196
197 /**
198 * Show some useful links in the subtitle
199 */
200 protected function showConvenienceLinks() {
201 global $wgOut, $wgUser, $wgLang;
202 # Give a link to the logs/hist for this page
203 if( $this->targetObj ) {
204 $links = array();
205 $links[] = $this->skin->linkKnown(
206 SpecialPage::getTitleFor( 'Log' ),
207 wfMsgHtml( 'viewpagelogs' ),
208 array(),
209 array( 'page' => $this->targetObj->getPrefixedText() )
210 );
211 if ( $this->targetObj->getNamespace() != NS_SPECIAL ) {
212 # Give a link to the page history
213 $links[] = $this->skin->linkKnown(
214 $this->targetObj,
215 wfMsgHtml( 'pagehist' ),
216 array(),
217 array( 'action' => 'history' )
218 );
219 # Link to deleted edits
220 if( $wgUser->isAllowed('undelete') ) {
221 $undelete = SpecialPage::getTitleFor( 'Undelete' );
222 $links[] = $this->skin->linkKnown(
223 $undelete,
224 wfMsgHtml( 'deletedhist' ),
225 array(),
226 array( 'target' => $this->targetObj->getPrefixedDBkey() )
227 );
228 }
229 }
230 # Logs themselves don't have histories or archived revisions
231 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
232 }
233 }
234
235 /**
236 * Get the condition used for fetching log snippets
237 */
238 protected function getLogQueryCond() {
239 $conds = array();
240 // Revision delete logs for these item
241 $conds['log_type'] = array('delete','suppress');
242 $conds['log_action'] = $this->getList()->getLogAction();
243 $conds['ls_field'] = RevisionDeleter::getRelationType( $this->typeName );
244 $conds['ls_value'] = $this->ids;
245 return $conds;
246 }
247
248 /**
249 * Show a deleted file version requested by the visitor.
250 * TODO Mostly copied from Special:Undelete. Refactor.
251 */
252 protected function tryShowFile( $archiveName ) {
253 global $wgOut, $wgRequest, $wgUser, $wgLang;
254
255 $repo = RepoGroup::singleton()->getLocalRepo();
256 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
257 $oimage->load();
258 // Check if user is allowed to see this file
259 if ( !$oimage->exists() ) {
260 $wgOut->addWikiMsg( 'revdelete-no-file' );
261 return;
262 }
263 if( !$oimage->userCan(File::DELETED_FILE) ) {
264 $wgOut->permissionRequired( 'suppressrevision' );
265 return;
266 }
267 if ( !$wgUser->matchEditToken( $this->token, $archiveName ) ) {
268 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
269 $this->targetObj->getText(),
270 $wgLang->date( $oimage->getTimestamp() ),
271 $wgLang->time( $oimage->getTimestamp() ) );
272 $wgOut->addHTML(
273 Xml::openElement( 'form', array(
274 'method' => 'POST',
275 'action' => $this->getTitle()->getLocalUrl(
276 'target=' . urlencode( $oimage->getName() ) .
277 '&file=' . urlencode( $archiveName ) .
278 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
279 )
280 ) .
281 Xml::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
282 '</form>'
283 );
284 return;
285 }
286 $wgOut->disable();
287 # We mustn't allow the output to be Squid cached, otherwise
288 # if an admin previews a deleted image, and it's cached, then
289 # a user without appropriate permissions can toddle off and
290 # nab the image, and Squid will serve it
291 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
292 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
293 $wgRequest->response()->header( 'Pragma: no-cache' );
294
295 # Stream the file to the client
296 global $IP;
297 require_once( "$IP/includes/StreamFile.php" );
298 $key = $oimage->getStorageKey();
299 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
300 wfStreamFile( $path );
301 }
302
303 /**
304 * Get the list object for this request
305 */
306 protected function getList() {
307 if ( is_null( $this->list ) ) {
308 $class = $this->typeInfo['list-class'];
309 $this->list = new $class( $this, $this->targetObj, $this->ids );
310 }
311 return $this->list;
312 }
313
314 /**
315 * Show a list of items that we will operate on, and show a form with checkboxes
316 * which will allow the user to choose new visibility settings.
317 */
318 protected function showForm() {
319 global $wgOut, $wgUser, $wgLang;
320 $UserAllowed = true;
321
322 if ( $this->typeName == 'logging' ) {
323 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids) ) );
324 } else {
325 $wgOut->addWikiMsg( 'revdelete-selected',
326 $this->targetObj->getPrefixedText(), count( $this->ids ) );
327 }
328
329 $bitfields = 0;
330 $wgOut->addHTML( "<ul>" );
331
332 $where = $revObjs = array();
333
334 $numRevisions = 0;
335 // Live revisions...
336 $list = $this->getList();
337 for ( $list->reset(); $list->current(); $list->next() ) {
338 $item = $list->current();
339 if ( !$item->canView() ) {
340 if( !$this->submitClicked ) {
341 $wgOut->permissionRequired( 'suppressrevision' );
342 return;
343 }
344 $UserAllowed = false;
345 }
346 $numRevisions++;
347 $bitfields |= $item->getBits();
348 $wgOut->addHTML( $item->getHTML() );
349 }
350
351 if( !$numRevisions ) {
352 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
353 return;
354 }
355
356 $wgOut->addHTML( "</ul>" );
357 // Explanation text
358 $this->addUsageText();
359
360 // Normal sysops can always see what they did, but can't always change it
361 if( !$UserAllowed ) return;
362
363 // Show form if the user can submit
364 if( $this->mIsAllowed ) {
365 $out = Xml::openElement( 'form', array( 'method' => 'post',
366 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
367 'id' => 'mw-revdel-form-revisions' ) ) .
368 Xml::fieldset( wfMsg( 'revdelete-legend' ) ) .
369 $this->buildCheckBoxes( $bitfields ) .
370 Xml::openElement( 'table' ) .
371 "<tr>\n" .
372 '<td class="mw-label">' .
373 Xml::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
374 '</td>' .
375 '<td class="mw-input">' .
376 Xml::listDropDown( 'wpRevDeleteReasonList',
377 wfMsgForContent( 'revdelete-reason-dropdown' ),
378 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
379 ) .
380 '</td>' .
381 "</tr><tr>\n" .
382 '<td class="mw-label">' .
383 Xml::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
384 '</td>' .
385 '<td class="mw-input">' .
386 Xml::input( 'wpReason', 60, $this->otherReason, array( 'id' => 'wpReason' ) ) .
387 '</td>' .
388 "</tr><tr>\n" .
389 '<td></td>' .
390 '<td class="mw-submit">' .
391 Xml::submitButton( wfMsg( 'revdelete-submit' ), array( 'name' => 'wpSubmit' ) ) .
392 '</td>' .
393 "</tr>\n" .
394 Xml::closeElement( 'table' ) .
395 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
396 Xml::hidden( 'target', $this->targetObj->getPrefixedText() ) .
397 Xml::hidden( 'type', $this->typeName ) .
398 Xml::hidden( 'ids', implode( ',', $this->ids ) ) .
399 Xml::closeElement( 'fieldset' ) . "\n";
400 } else {
401 $out = '';
402 }
403 // Show link to edit the dropdown reasons
404 if( $wgUser->isAllowed( 'editinterface' ) ) {
405 $title = Title::makeTitle( NS_MEDIAWIKI, 'revdelete-reason-dropdown' );
406 $link = $wgUser->getSkin()->link(
407 $title,
408 wfMsgHtml( 'revdelete-edit-reasonlist' ),
409 array(),
410 array( 'action' => 'edit' )
411 );
412 $out .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
413 }
414 if( $this->mIsAllowed ) {
415 $out .= Xml::closeElement( 'form' ) . "\n";
416 }
417 $wgOut->addHTML( $out );
418 }
419
420 /**
421 * Show some introductory text
422 * FIXME Wikimedia-specific policy text
423 */
424 protected function addUsageText() {
425 global $wgOut, $wgUser;
426 $wgOut->addWikiMsg( 'revdelete-text' );
427 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
428 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
429 }
430 }
431
432 /**
433 * @param $bitfields Interger: aggregate bitfield of all the bitfields
434 * @return String: HTML
435 */
436 protected function buildCheckBoxes( $bitfields ) {
437 $html = '<table>';
438 // FIXME: all items checked for just one rev are checked, even if not set for the others
439 foreach( $this->checks as $item ) {
440 list( $message, $name, $field ) = $item;
441 $innerHTML = Xml::checkLabel( wfMsg($message), $name, $name, $bitfields & $field );
442 if( $field == Revision::DELETED_RESTRICTED )
443 $innerHTML = "<b>$innerHTML</b>";
444 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
445 $html .= '<tr>' . $line . "</tr>\n";
446 }
447 $html .= '</table>';
448 return $html;
449 }
450
451 /**
452 * UI entry point for form submission.
453 * @param $request WebRequest
454 */
455 protected function submit( $request ) {
456 global $wgUser, $wgOut;
457 # Check edit token on submission
458 if( $this->submitClicked && !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
459 $wgOut->addWikiMsg( 'sessionfailure' );
460 return false;
461 }
462 $bitfield = $this->extractBitfield( $request );
463 $listReason = $request->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
464 $comment = $listReason;
465 if( $comment != 'other' && $this->otherReason != '' ) {
466 // Entry from drop down menu + additional comment
467 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason;
468 } elseif( $comment == 'other' ) {
469 $comment = $this->otherReason;
470 }
471 # Can the user set this field?
472 if( $bitfield & Revision::DELETED_RESTRICTED && !$wgUser->isAllowed('suppressrevision') ) {
473 $wgOut->permissionRequired( 'suppressrevision' );
474 return false;
475 }
476 # If the save went through, go to success message...
477 $status = $this->save( $bitfield, $comment, $this->targetObj );
478 if ( $status->isGood() ) {
479 $this->success();
480 return true;
481 # ...otherwise, bounce back to form...
482 } else {
483 $this->failure( $status );
484 }
485 return false;
486 }
487
488 /**
489 * Report that the submit operation succeeded
490 */
491 protected function success() {
492 global $wgOut;
493 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
494 $wgOut->wrapWikiMsg( '<span class="success">$1</span>', $this->typeInfo['success'] );
495 $this->list->reloadFromMaster();
496 $this->showForm();
497 }
498
499 /**
500 * Report that the submit operation failed
501 */
502 protected function failure( $status ) {
503 global $wgOut;
504 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
505 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo['failure'] ) );
506 $this->showForm();
507 }
508
509 /**
510 * Put together a rev_deleted bitfield from the submitted checkboxes
511 * @param $request WebRequest
512 * @return Integer
513 */
514 protected function extractBitfield( $request ) {
515 $bitfield = 0;
516 foreach( $this->checks as $item ) {
517 list( /* message */ , $name, $field ) = $item;
518 if( $request->getCheck( $name ) ) {
519 $bitfield |= $field;
520 }
521 }
522 return $bitfield;
523 }
524
525 /**
526 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
527 */
528 protected function save( $bitfield, $reason, $title ) {
529 // Don't allow simply locking the interface for no reason
530 if( $bitfield == Revision::DELETED_RESTRICTED ) {
531 return Status::newFatal( 'revdelete-only-restricted' );
532 }
533 return $this->getList()->setVisibility( array(
534 'value' => $bitfield,
535 'comment' => $reason ) );
536 }
537 }
538
539 /**
540 * Temporary b/c interface, collection of static functions.
541 * @ingroup SpecialPage
542 */
543 class RevisionDeleter {
544 /**
545 * Checks for a change in the bitfield for a certain option and updates the
546 * provided array accordingly.
547 *
548 * @param $desc String: description to add to the array if the option was
549 * enabled / disabled.
550 * @param $field Integer: the bitmask describing the single option.
551 * @param $diff Integer: the xor of the old and new bitfields.
552 * @param $new Integer: the new bitfield
553 * @param $arr Array: the array to update.
554 */
555 protected static function checkItem( $desc, $field, $diff, $new, &$arr ) {
556 if( $diff & $field ) {
557 $arr[ ( $new & $field ) ? 0 : 1 ][] = $desc;
558 }
559 }
560
561 /**
562 * Gets an array describing the changes made to the visibilit of the revision.
563 * If the resulting array is $arr, then $arr[0] will contain an array of strings
564 * describing the items that were hidden, $arr[2] will contain an array of strings
565 * describing the items that were unhidden, and $arr[3] will contain an array with
566 * a single string, which can be one of "applied restrictions to sysops",
567 * "removed restrictions from sysops", or null.
568 *
569 * @param $n Integer: the new bitfield.
570 * @param $o Integer: the old bitfield.
571 * @return An array as described above.
572 */
573 protected static function getChanges( $n, $o ) {
574 $diff = $n ^ $o;
575 $ret = array( 0 => array(), 1 => array(), 2 => array() );
576 // Build bitfield changes in language
577 self::checkItem( wfMsgForContent( 'revdelete-content' ),
578 Revision::DELETED_TEXT, $diff, $n, $ret );
579 self::checkItem( wfMsgForContent( 'revdelete-summary' ),
580 Revision::DELETED_COMMENT, $diff, $n, $ret );
581 self::checkItem( wfMsgForContent( 'revdelete-uname' ),
582 Revision::DELETED_USER, $diff, $n, $ret );
583 // Restriction application to sysops
584 if( $diff & Revision::DELETED_RESTRICTED ) {
585 if( $n & Revision::DELETED_RESTRICTED )
586 $ret[2][] = wfMsgForContent( 'revdelete-restricted' );
587 else
588 $ret[2][] = wfMsgForContent( 'revdelete-unrestricted' );
589 }
590 return $ret;
591 }
592
593 /**
594 * Gets a log message to describe the given revision visibility change. This
595 * message will be of the form "[hid {content, edit summary, username}];
596 * [unhid {...}][applied restrictions to sysops] for $count revisions: $comment".
597 *
598 * @param $count Integer: The number of effected revisions.
599 * @param $nbitfield Integer: The new bitfield for the revision.
600 * @param $obitfield Integer: The old bitfield for the revision.
601 * @param $isForLog Boolean
602 */
603 public static function getLogMessage( $count, $nbitfield, $obitfield, $isForLog = false ) {
604 global $wgLang;
605 $s = '';
606 $changes = self::getChanges( $nbitfield, $obitfield );
607 if( count( $changes[0] ) ) {
608 $s .= wfMsgForContent( 'revdelete-hid', implode( ', ', $changes[0] ) );
609 }
610 if( count( $changes[1] ) ) {
611 if ($s) $s .= '; ';
612 $s .= wfMsgForContent( 'revdelete-unhid', implode( ', ', $changes[1] ) );
613 }
614 if( count( $changes[2] ) ) {
615 $s .= $s ? ' (' . $changes[2][0] . ')' : $changes[2][0];
616 }
617 $msg = $isForLog ? 'logdelete-log-message' : 'revdelete-log-message';
618 return wfMsgExt( $msg, array( 'parsemag', 'content' ), $s, $wgLang->formatNum($count) );
619
620 }
621
622 // Get DB field name for URL param...
623 // Future code for other things may also track
624 // other types of revision-specific changes.
625 // @returns string One of log_id/rev_id/fa_id/ar_timestamp/oi_archive_name
626 public static function getRelationType( $typeName ) {
627 if ( isset( SpecialRevisionDelete::$deprecatedTypeMap[$typeName] ) ) {
628 $typeName = SpecialRevisionDelete::$deprecatedTypeMap[$typeName];
629 }
630 if ( isset( SpecialRevisionDelete::$allowedTypes[$typeName] ) ) {
631 $class = SpecialRevisionDelete::$allowedTypes[$typeName]['list-class'];
632 $list = new $class( null, null, null );
633 return $list->getIdField();
634 } else {
635 return null;
636 }
637 }
638 }
639
640 /**
641 * Abstract base class for a list of deletable items
642 */
643 abstract class RevDel_List {
644 var $special, $title, $ids, $res, $current;
645 var $type = null; // override this
646 var $idField = null; // override this
647 var $dateField = false; // override this
648 var $authorIdField = false; // override this
649 var $authorNameField = false; // override this
650
651 /**
652 * @param $special The parent SpecialPage
653 * @param $title The target title
654 * @param $ids Array of IDs
655 */
656 public function __construct( $special, $title, $ids ) {
657 $this->special = $special;
658 $this->title = $title;
659 $this->ids = $ids;
660 }
661
662 /**
663 * Get the internal type name of this list. Equal to the table name.
664 */
665 public function getType() {
666 return $this->type;
667 }
668
669 /**
670 * Get the DB field name associated with the ID list
671 */
672 public function getIdField() {
673 return $this->idField;
674 }
675
676 /**
677 * Get the DB field name storing timestamps
678 */
679 public function getTimestampField() {
680 return $this->dateField;
681 }
682
683 /**
684 * Get the DB field name storing user ids
685 */
686 public function getAuthorIdField() {
687 return $this->authorIdField;
688 }
689
690 /**
691 * Get the DB field name storing user names
692 */
693 public function getAuthorNameField() {
694 return $this->authorNameField;
695 }
696 /**
697 * Set the visibility for the revisions in this list. Logging and
698 * transactions are done here.
699 *
700 * @param $params Associative array of parameters. Members are:
701 * value: The integer value to set the visibility to
702 * comment: The log comment.
703 * @return Status
704 */
705 public function setVisibility( $params ) {
706 $newBits = $params['value'];
707 $comment = $params['comment'];
708
709 $this->res = false;
710 $dbw = wfGetDB( DB_MASTER );
711 $this->doQuery( $dbw );
712 $dbw->begin();
713 $status = Status::newGood();
714 $missing = array_flip( $this->ids );
715 $this->clearFileOps();
716 $idsForLog = array();
717 $authorIds = $authorIPs = array();
718
719 for ( $this->reset(); $this->current(); $this->next() ) {
720 $item = $this->current();
721 unset( $missing[ $item->getId() ] );
722
723 // Make error messages less vague
724 $oldBits = $item->getBits();
725 if ( $oldBits == $newBits ) {
726 $status->warning( 'revdelete-no-change', $item->formatDate(), $item->formatTime() );
727 $status->failCount++;
728 continue;
729 } elseif ( $oldBits == 0 && $newBits != 0 ) {
730 $opType = 'hide';
731 } elseif ( $oldBits != 0 && $newBits == 0 ) {
732 $opType = 'show';
733 } else {
734 $opType = 'modify';
735 }
736
737 if ( $item->isHideCurrentOp( $newBits ) ) {
738 // Cannot hide current version text
739 $status->error( 'revdelete-hide-current', $item->formatDate(), $item->formatTime() );
740 $status->failCount++;
741 continue;
742 }
743 if ( !$item->canView() ) {
744 // Cannot access this revision
745 $msg = $opType == 'show' ? 'revdelete-show-no-access' : 'revdelete-modify-no-access';
746 $status->error( $msg, $item->formatDate(), $item->formatTime() );
747 $status->failCount++;
748 continue;
749 }
750
751 // Update the revision
752 $ok = $item->setBits( $newBits );
753
754 if ( $ok ) {
755 $idsForLog[] = $item->getId();
756 $status->successCount++;
757 if( $item->getAuthorId() > 0 ) {
758 $authorIds[] = $item->getAuthorId();
759 } else if( IP::isIPAddress( $item->getAuthorName() ) ) {
760 $authorIPs[] = $item->getAuthorName();
761 }
762 } else {
763 $status->error( 'revdelete-concurrent-change', $item->formatDate(), $item->formatTime() );
764 $status->failCount++;
765 }
766 }
767
768 // Handle missing revisions
769 foreach ( $missing as $id => $unused ) {
770 $status->error( 'revdelete-modify-missing', $id );
771 $status->failCount++;
772 }
773
774 if ( $status->successCount == 0 ) {
775 $status->ok = false;
776 $dbw->rollback();
777 return $status;
778 }
779
780 // Save success count
781 $successCount = $status->successCount;
782
783 // Move files, if there are any
784 $status->merge( $this->doPreCommitUpdates() );
785 if ( !$status->isOK() ) {
786 // Fatal error, such as no configured archive directory
787 $dbw->rollback();
788 return $status;
789 }
790
791 // Log it
792 $this->updateLog( array(
793 'title' => $this->title,
794 'count' => $successCount,
795 'newBits' => $newBits,
796 'oldBits' => $oldBits,
797 'comment' => $comment,
798 'ids' => $idsForLog,
799 'authorIds' => $authorIds,
800 'authorIPs' => $authorIPs
801 ) );
802 $dbw->commit();
803
804 // Clear caches
805 $status->merge( $this->doPostCommitUpdates() );
806 return $status;
807 }
808
809 /**
810 * Reload the list data from the master DB. This can be done after setVisibility()
811 * to allow $item->getHTML() to show the new data.
812 */
813 function reloadFromMaster() {
814 $dbw = wfGetDB( DB_MASTER );
815 $this->res = $this->doQuery( $dbw );
816 }
817
818 /**
819 * Record a log entry on the action
820 * @param $params Associative array of parameters:
821 * newBits: The new value of the *_deleted bitfield
822 * oldBits: The old value of the *_deleted bitfield.
823 * title: The target title
824 * ids: The ID list
825 * comment: The log comment
826 * authorsIds: The array of the user IDs of the offenders
827 * authorsIPs: The array of the IP/anon user offenders
828 */
829 protected function updateLog( $params ) {
830 // Get the URL param's corresponding DB field
831 $field = RevisionDeleter::getRelationType( $this->getType() );
832 if( !$field ) {
833 throw new MWException( "Bad log URL param type!" );
834 }
835 // Put things hidden from sysops in the oversight log
836 if ( ( $params['newBits'] | $params['oldBits'] ) & $this->getSuppressBit() ) {
837 $logType = 'suppress';
838 } else {
839 $logType = 'delete';
840 }
841 // Add params for effected page and ids
842 $logParams = $this->getLogParams( $params );
843 // Actually add the deletion log entry
844 $log = new LogPage( $logType );
845 $logid = $log->addEntry( $this->getLogAction(), $params['title'],
846 $params['comment'], $logParams );
847 // Allow for easy searching of deletion log items for revision/log items
848 $log->addRelations( $field, $params['ids'], $logid );
849 $log->addRelations( 'target_author_id', $params['authorIds'], $logid );
850 $log->addRelations( 'target_author_ip', $params['authorIPs'], $logid );
851 }
852
853 /**
854 * Get the log action for this list type
855 */
856 public function getLogAction() {
857 return 'revision';
858 }
859
860 /**
861 * Get log parameter array.
862 * @param $params Associative array of log parameters, same as updateLog()
863 * @return array
864 */
865 public function getLogParams( $params ) {
866 return array(
867 $this->getType(),
868 implode( ',', $params['ids'] ),
869 "ofield={$params['oldBits']}",
870 "nfield={$params['newBits']}"
871 );
872 }
873
874 /**
875 * Initialise the current iteration pointer
876 */
877 protected function initCurrent() {
878 $row = $this->res->current();
879 if ( $row ) {
880 $this->current = $this->newItem( $row );
881 } else {
882 $this->current = false;
883 }
884 }
885
886 /**
887 * Start iteration. This must be called before current() or next().
888 * @return First list item
889 */
890 public function reset() {
891 if ( !$this->res ) {
892 $this->res = $this->doQuery( wfGetDB( DB_SLAVE ) );
893 } else {
894 $this->res->rewind();
895 }
896 $this->initCurrent();
897 return $this->current;
898 }
899
900 /**
901 * Get the current list item, or false if we are at the end
902 */
903 public function current() {
904 return $this->current;
905 }
906
907 /**
908 * Move the iteration pointer to the next list item, and return it.
909 */
910 public function next() {
911 $this->res->next();
912 $this->initCurrent();
913 return $this->current;
914 }
915
916 /**
917 * Clear any data structures needed for doPreCommitUpdates() and doPostCommitUpdates()
918 * STUB
919 */
920 public function clearFileOps() {
921 }
922
923 /**
924 * A hook for setVisibility(): do batch updates pre-commit.
925 * STUB
926 * @return Status
927 */
928 public function doPreCommitUpdates() {
929 return Status::newGood();
930 }
931
932 /**
933 * A hook for setVisibility(): do any necessary updates post-commit.
934 * STUB
935 * @return Status
936 */
937 public function doPostCommitUpdates() {
938 return Status::newGood();
939 }
940
941 /**
942 * Create an item object from a DB result row
943 * @param $row stdclass
944 */
945 abstract public function newItem( $row );
946
947 /**
948 * Do the DB query to iterate through the objects.
949 * @param $db Database object to use for the query
950 */
951 abstract public function doQuery( $db );
952
953 /**
954 * Get the integer value of the flag used for suppression
955 */
956 abstract public function getSuppressBit();
957 }
958
959 /**
960 * Abstract base class for deletable items
961 */
962 abstract class RevDel_Item {
963 /** The parent SpecialPage */
964 var $special;
965
966 /** The parent RevDel_List */
967 var $list;
968
969 /** The DB result row */
970 var $row;
971
972 /**
973 * @param $list RevDel_List
974 * @param $row DB result row
975 */
976 public function __construct( $list, $row ) {
977 $this->special = $list->special;
978 $this->list = $list;
979 $this->row = $row;
980 }
981
982 /**
983 * Get the ID, as it would appear in the ids URL parameter
984 */
985 public function getId() {
986 $field = $this->list->getIdField();
987 return $this->row->$field;
988 }
989
990 /**
991 * Get the date, formatted with $wgLang
992 */
993 public function formatDate() {
994 global $wgLang;
995 return $wgLang->date( $this->getTimestamp() );
996 }
997
998 /**
999 * Get the time, formatted with $wgLang
1000 */
1001 public function formatTime() {
1002 global $wgLang;
1003 return $wgLang->time( $this->getTimestamp() );
1004 }
1005
1006 /**
1007 * Get the timestamp in MW 14-char form
1008 */
1009 public function getTimestamp() {
1010 $field = $this->list->getTimestampField();
1011 return wfTimestamp( TS_MW, $this->row->$field );
1012 }
1013
1014 /**
1015 * Get the author user ID
1016 */
1017 public function getAuthorId() {
1018 $field = $this->list->getAuthorIdField();
1019 return intval( $this->row->$field );
1020 }
1021
1022 /**
1023 * Get the author user name
1024 */
1025 public function getAuthorName() {
1026 $field = $this->list->getAuthorNameField();
1027 return strval( $this->row->$field );
1028 }
1029
1030 /**
1031 * Returns true if the item is "current", and the operation to set the given
1032 * bits can't be executed for that reason
1033 * STUB
1034 */
1035 public function isHideCurrentOp( $newBits ) {
1036 return false;
1037 }
1038
1039 /**
1040 * Returns true if the current user can view the item
1041 */
1042 abstract public function canView();
1043
1044 /**
1045 * Get the current deletion bitfield value
1046 */
1047 abstract public function getBits();
1048
1049 /**
1050 * Get the HTML of the list item. Should be include <li></li> tags.
1051 * This is used to show the list in HTML form, by the special page.
1052 */
1053 abstract public function getHTML();
1054
1055 /**
1056 * Set the visibility of the item. This should do any necessary DB queries.
1057 *
1058 * The DB update query should have a condition which forces it to only update
1059 * if the value in the DB matches the value fetched earlier with the SELECT.
1060 * If the update fails because it did not match, the function should return
1061 * false. This prevents concurrency problems.
1062 *
1063 * @return boolean success
1064 */
1065 abstract public function setBits( $newBits );
1066 }
1067
1068 /**
1069 * List for revision table items
1070 */
1071 class RevDel_RevisionList extends RevDel_List {
1072 var $currentRevId;
1073 var $type = 'revision';
1074 var $idField = 'rev_id';
1075 var $dateField = 'rev_timestamp';
1076 var $authorIdField = 'rev_user';
1077 var $authorNameField = 'rev_user_text';
1078
1079 public function doQuery( $db ) {
1080 $ids = array_map( 'intval', $this->ids );
1081 return $db->select( array('revision','page'), '*',
1082 array(
1083 'rev_page' => $this->title->getArticleID(),
1084 'rev_id' => $ids,
1085 'rev_page = page_id'
1086 ),
1087 __METHOD__
1088 );
1089 }
1090
1091 public function newItem( $row ) {
1092 return new RevDel_RevisionItem( $this, $row );
1093 }
1094
1095 public function getCurrent() {
1096 if ( is_null( $this->currentRevId ) ) {
1097 $dbw = wfGetDB( DB_MASTER );
1098 $this->currentRevId = $dbw->selectField(
1099 'page', 'page_latest', $this->title->pageCond(), __METHOD__ );
1100 }
1101 return $this->currentRevId;
1102 }
1103
1104 public function getSuppressBit() {
1105 return Revision::DELETED_RESTRICTED;
1106 }
1107
1108 public function doPreCommitUpdates() {
1109 $this->title->invalidateCache();
1110 return Status::newGood();
1111 }
1112
1113 public function doPostCommitUpdates() {
1114 $this->title->purgeSquid();
1115 // Extensions that require referencing previous revisions may need this
1116 wfRunHooks( 'ArticleRevisionVisiblitySet', array( &$this->title ) );
1117 return Status::newGood();
1118 }
1119 }
1120
1121 /**
1122 * Item class for a revision table row
1123 */
1124 class RevDel_RevisionItem extends RevDel_Item {
1125 var $revision;
1126
1127 public function __construct( $list, $row ) {
1128 parent::__construct( $list, $row );
1129 $this->revision = new Revision( $row );
1130 }
1131
1132 public function canView() {
1133 return $this->revision->userCan( Revision::DELETED_RESTRICTED );
1134 }
1135
1136 public function getBits() {
1137 return $this->revision->mDeleted;
1138 }
1139
1140 public function setBits( $bits ) {
1141 $dbw = wfGetDB( DB_MASTER );
1142 // Update revision table
1143 $dbw->update( 'revision',
1144 array( 'rev_deleted' => $bits ),
1145 array(
1146 'rev_id' => $this->revision->getId(),
1147 'rev_page' => $this->revision->getPage(),
1148 'rev_deleted' => $this->getBits()
1149 ),
1150 __METHOD__
1151 );
1152 if ( !$dbw->affectedRows() ) {
1153 // Concurrent fail!
1154 return false;
1155 }
1156 // Update recentchanges table
1157 $dbw->update( 'recentchanges',
1158 array(
1159 'rc_deleted' => $bits,
1160 'rc_patrolled' => 1
1161 ),
1162 array(
1163 'rc_this_oldid' => $this->revision->getId(), // condition
1164 // non-unique timestamp index
1165 'rc_timestamp' => $dbw->timestamp( $this->revision->getTimestamp() ),
1166 ),
1167 __METHOD__
1168 );
1169 return true;
1170 }
1171
1172 public function isDeleted() {
1173 return $this->revision->isDeleted( Revision::DELETED_TEXT );
1174 }
1175
1176 public function isHideCurrentOp( $newBits ) {
1177 return ( $newBits & Revision::DELETED_TEXT )
1178 && $this->list->getCurrent() == $this->getId();
1179 }
1180
1181 /**
1182 * Get the HTML link to the revision text.
1183 * Overridden by RevDel_ArchiveItem.
1184 */
1185 protected function getRevisionLink() {
1186 global $wgLang;
1187 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
1188 if ( $this->isDeleted() && !$this->canView() ) {
1189 return $date;
1190 }
1191 return $this->special->skin->link(
1192 $this->list->title,
1193 $date,
1194 array(),
1195 array(
1196 'oldid' => $this->revision->getId(),
1197 'unhide' => 1
1198 )
1199 );
1200 }
1201
1202 /**
1203 * Get the HTML link to the diff.
1204 * Overridden by RevDel_ArchiveItem
1205 */
1206 protected function getDiffLink() {
1207 if ( $this->isDeleted() && !$this->canView() ) {
1208 return wfMsgHtml('diff');
1209 } else {
1210 return
1211 $this->special->skin->link(
1212 $this->list->title,
1213 wfMsgHtml('diff'),
1214 array(),
1215 array(
1216 'diff' => $this->revision->getId(),
1217 'oldid' => 'prev',
1218 'unhide' => 1
1219 ),
1220 array(
1221 'known',
1222 'noclasses'
1223 )
1224 );
1225 }
1226 }
1227
1228 public function getHTML() {
1229 $difflink = $this->getDiffLink();
1230 $revlink = $this->getRevisionLink();
1231 $userlink = $this->special->skin->revUserLink( $this->revision );
1232 $comment = $this->special->skin->revComment( $this->revision );
1233 if ( $this->isDeleted() ) {
1234 $revlink = "<span class=\"history-deleted\">$revlink</span>";
1235 }
1236 return "<li>($difflink) $revlink $userlink $comment</li>";
1237 }
1238 }
1239
1240 /**
1241 * List for archive table items, i.e. revisions deleted via action=delete
1242 */
1243 class RevDel_ArchiveList extends RevDel_RevisionList {
1244 var $type = 'archive';
1245 var $idField = 'ar_timestamp';
1246 var $dateField = 'ar_timestamp';
1247 var $authorIdField = 'ar_user';
1248 var $authorNameField = 'ar_user_text';
1249
1250 public function doQuery( $db ) {
1251 $timestamps = array();
1252 foreach ( $this->ids as $id ) {
1253 $timestamps[] = $db->timestamp( $id );
1254 }
1255 return $db->select( 'archive', '*',
1256 array(
1257 'ar_namespace' => $this->title->getNamespace(),
1258 'ar_title' => $this->title->getDBkey(),
1259 'ar_timestamp' => $timestamps
1260 ),
1261 __METHOD__
1262 );
1263 }
1264
1265 public function newItem( $row ) {
1266 return new RevDel_ArchiveItem( $this, $row );
1267 }
1268
1269 public function doPreCommitUpdates() {
1270 return Status::newGood();
1271 }
1272
1273 public function doPostCommitUpdates() {
1274 return Status::newGood();
1275 }
1276 }
1277
1278 /**
1279 * Item class for a archive table row
1280 */
1281 class RevDel_ArchiveItem extends RevDel_RevisionItem {
1282 public function __construct( $list, $row ) {
1283 RevDel_Item::__construct( $list, $row );
1284 $this->revision = Revision::newFromArchiveRow( $row,
1285 array( 'page' => $this->list->title->getArticleId() ) );
1286 }
1287
1288 public function getId() {
1289 # Convert DB timestamp to MW timestamp
1290 return $this->revision->getTimestamp();
1291 }
1292
1293 public function setBits( $bits ) {
1294 $dbw = wfGetDB( DB_MASTER );
1295 $dbw->update( 'archive',
1296 array( 'ar_deleted' => $bits ),
1297 array( 'ar_namespace' => $this->list->title->getNamespace(),
1298 'ar_title' => $this->list->title->getDBkey(),
1299 // use timestamp for index
1300 'ar_timestamp' => $this->row->ar_timestamp,
1301 'ar_rev_id' => $this->row->ar_rev_id,
1302 'ar_deleted' => $this->getBits()
1303 ),
1304 __METHOD__ );
1305 return (bool)$dbw->affectedRows();
1306 }
1307
1308 protected function getRevisionLink() {
1309 global $wgLang;
1310 $undelete = SpecialPage::getTitleFor( 'Undelete' );
1311 $date = $wgLang->timeanddate( $this->revision->getTimestamp(), true );
1312 if ( $this->isDeleted() && !$this->canView() ) {
1313 return $date;
1314 }
1315 return $this->special->skin->link( $undelete, $date, array(),
1316 array(
1317 'target' => $this->list->title->getPrefixedText(),
1318 'timestamp' => $this->revision->getTimestamp()
1319 ) );
1320 }
1321
1322 protected function getDiffLink() {
1323 if ( $this->isDeleted() && !$this->canView() ) {
1324 return wfMsgHtml( 'diff' );
1325 }
1326 $undelete = SpecialPage::getTitleFor( 'Undelete' );
1327 return $this->special->skin->link( $undelete, wfMsgHtml('diff'), array(),
1328 array(
1329 'target' => $this->list->title->getPrefixedText(),
1330 'diff' => 'prev',
1331 'timestamp' => $this->revision->getTimestamp()
1332 ) );
1333 }
1334 }
1335
1336 /**
1337 * List for oldimage table items
1338 */
1339 class RevDel_FileList extends RevDel_List {
1340 var $type = 'oldimage';
1341 var $idField = 'oi_archive_name';
1342 var $dateField = 'oi_timestamp';
1343 var $authorIdField = 'oi_user';
1344 var $authorNameField = 'oi_user_text';
1345 var $storeBatch, $deleteBatch, $cleanupBatch;
1346
1347 public function doQuery( $db ) {
1348 $archiveName = array();
1349 foreach( $this->ids as $timestamp ) {
1350 $archiveNames[] = $timestamp . '!' . $this->title->getDBkey();
1351 }
1352 return $db->select( 'oldimage', '*',
1353 array(
1354 'oi_name' => $this->title->getDBkey(),
1355 'oi_archive_name' => $archiveNames
1356 ),
1357 __METHOD__
1358 );
1359 }
1360
1361 public function newItem( $row ) {
1362 return new RevDel_FileItem( $this, $row );
1363 }
1364
1365 public function clearFileOps() {
1366 $this->deleteBatch = array();
1367 $this->storeBatch = array();
1368 $this->cleanupBatch = array();
1369 }
1370
1371 public function doPreCommitUpdates() {
1372 $status = Status::newGood();
1373 $repo = RepoGroup::singleton()->getLocalRepo();
1374 if ( $this->storeBatch ) {
1375 $status->merge( $repo->storeBatch( $this->storeBatch, FileRepo::OVERWRITE_SAME ) );
1376 }
1377 if ( !$status->isOK() ) {
1378 return $status;
1379 }
1380 if ( $this->deleteBatch ) {
1381 $status->merge( $repo->deleteBatch( $this->deleteBatch ) );
1382 }
1383 if ( !$status->isOK() ) {
1384 // Running cleanupDeletedBatch() after a failed storeBatch() with the DB already
1385 // modified (but destined for rollback) causes data loss
1386 return $status;
1387 }
1388 if ( $this->cleanupBatch ) {
1389 $status->merge( $repo->cleanupDeletedBatch( $this->cleanupBatch ) );
1390 }
1391 return $status;
1392 }
1393
1394 public function doPostCommitUpdates() {
1395 $file = wfLocalFile( $this->title );
1396 $file->purgeCache();
1397 $file->purgeDescription();
1398 return Status::newGood();
1399 }
1400
1401 public function getSuppressBit() {
1402 return File::DELETED_RESTRICTED;
1403 }
1404 }
1405
1406 /**
1407 * Item class for an oldimage table row
1408 */
1409 class RevDel_FileItem extends RevDel_Item {
1410 var $file;
1411
1412 public function __construct( $list, $row ) {
1413 parent::__construct( $list, $row );
1414 $this->file = RepoGroup::singleton()->getLocalRepo()->newFileFromRow( $row );
1415 }
1416
1417 public function getId() {
1418 $parts = explode( '!', $this->row->oi_archive_name );
1419 return $parts[0];
1420 }
1421
1422 public function canView() {
1423 return $this->file->userCan( File::DELETED_RESTRICTED );
1424 }
1425
1426 public function getBits() {
1427 return $this->file->getVisibility();
1428 }
1429
1430 public function setBits( $bits ) {
1431 # Queue the file op
1432 # FIXME: move to LocalFile.php
1433 if ( $this->isDeleted() ) {
1434 if ( $bits & File::DELETED_FILE ) {
1435 # Still deleted
1436 } else {
1437 # Newly undeleted
1438 $key = $this->file->getStorageKey();
1439 $srcRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
1440 $this->list->storeBatch[] = array(
1441 $this->file->repo->getVirtualUrl( 'deleted' ) . '/' . $srcRel,
1442 'public',
1443 $this->file->getRel()
1444 );
1445 $this->list->cleanupBatch[] = $key;
1446 }
1447 } elseif ( $bits & File::DELETED_FILE ) {
1448 # Newly deleted
1449 $key = $this->file->getStorageKey();
1450 $dstRel = $this->file->repo->getDeletedHashPath( $key ) . $key;
1451 $this->list->deleteBatch[] = array( $this->file->getRel(), $dstRel );
1452 }
1453
1454 # Do the database operations
1455 $dbw = wfGetDB( DB_MASTER );
1456 $dbw->update( 'oldimage',
1457 array( 'oi_deleted' => $bits ),
1458 array(
1459 'oi_name' => $this->row->oi_name,
1460 'oi_timestamp' => $this->row->oi_timestamp,
1461 'oi_deleted' => $this->getBits()
1462 ),
1463 __METHOD__
1464 );
1465 return (bool)$dbw->affectedRows();
1466 }
1467
1468 public function isDeleted() {
1469 return $this->file->isDeleted( File::DELETED_FILE );
1470 }
1471
1472 /**
1473 * Get the link to the file.
1474 * Overridden by RevDel_ArchivedFileItem.
1475 */
1476 protected function getLink() {
1477 global $wgLang;
1478 $date = $wgLang->timeanddate( $this->file->getTimestamp(), true );
1479 if ( $this->isDeleted() ) {
1480 # Hidden files...
1481 if ( !$this->canView() ) {
1482 $link = $date;
1483 } else {
1484 $link = $this->special->skin->link(
1485 $this->special->getTitle(),
1486 $date, array(),
1487 array(
1488 'target' => $this->list->title->getPrefixedText(),
1489 'file' => $this->file->sha1 . '.' . $this->file->getExtension()
1490 )
1491 );
1492 }
1493 return '<span class="history-deleted">' . $link . '</span>';
1494 } else {
1495 # Regular files...
1496 $url = $this->file->getUrl();
1497 return Xml::element( 'a', array( 'href' => $this->file->getUrl() ), $date );
1498 }
1499 }
1500 /**
1501 * Generate a user tool link cluster if the current user is allowed to view it
1502 * @return string HTML
1503 */
1504 protected function getUserTools() {
1505 if( $this->file->userCan( Revision::DELETED_USER ) ) {
1506 $link = $this->special->skin->userLink( $this->file->user, $this->file->user_text ) .
1507 $this->special->skin->userToolLinks( $this->file->user, $this->file->user_text );
1508 } else {
1509 $link = wfMsgHtml( 'rev-deleted-user' );
1510 }
1511 if( $this->file->isDeleted( Revision::DELETED_USER ) ) {
1512 return '<span class="history-deleted">' . $link . '</span>';
1513 }
1514 return $link;
1515 }
1516
1517 /**
1518 * Wrap and format the file's comment block, if the current
1519 * user is allowed to view it.
1520 *
1521 * @return string HTML
1522 */
1523 protected function getComment() {
1524 if( $this->file->userCan( File::DELETED_COMMENT ) ) {
1525 $block = $this->special->skin->commentBlock( $this->file->description );
1526 } else {
1527 $block = ' ' . wfMsgHtml( 'rev-deleted-comment' );
1528 }
1529 if( $this->file->isDeleted( File::DELETED_COMMENT ) ) {
1530 return "<span class=\"history-deleted\">$block</span>";
1531 }
1532 return $block;
1533 }
1534
1535 public function getHTML() {
1536 global $wgLang;
1537 $data =
1538 wfMsg(
1539 'widthheight',
1540 $wgLang->formatNum( $this->file->getWidth() ),
1541 $wgLang->formatNum( $this->file->getHeight() )
1542 ) .
1543 ' (' .
1544 wfMsgExt( 'nbytes', 'parsemag', $wgLang->formatNum( $this->file->getSize() ) ) .
1545 ')';
1546 $pageLink = $this->getLink();
1547
1548 return '<li>' . $this->getLink() . ' ' . $this->getUserTools() . ' ' .
1549 $data . ' ' . $this->getComment(). '</li>';
1550 }
1551 }
1552
1553 /**
1554 * List for filearchive table items
1555 */
1556 class RevDel_ArchivedFileList extends RevDel_FileList {
1557 var $type = 'filearchive';
1558 var $idField = 'fa_id';
1559 var $dateField = 'fa_timestamp';
1560 var $authorIdField = 'fa_user';
1561 var $authorNameField = 'fa_user_text';
1562
1563 public function doQuery( $db ) {
1564 $ids = array_map( 'intval', $this->ids );
1565 return $db->select( 'filearchive', '*',
1566 array(
1567 'fa_name' => $this->title->getDBkey(),
1568 'fa_id' => $ids
1569 ),
1570 __METHOD__
1571 );
1572 }
1573
1574 public function newItem( $row ) {
1575 return new RevDel_ArchivedFileItem( $this, $row );
1576 }
1577 }
1578
1579 /**
1580 * Item class for a filearchive table row
1581 */
1582 class RevDel_ArchivedFileItem extends RevDel_FileItem {
1583 public function __construct( $list, $row ) {
1584 RevDel_Item::__construct( $list, $row );
1585 $this->file = ArchivedFile::newFromRow( $row );
1586 }
1587
1588 public function getId() {
1589 return $this->row->fa_id;
1590 }
1591
1592 public function setBits( $bits ) {
1593 $dbw = wfGetDB( DB_MASTER );
1594 $dbw->update( 'filearchive',
1595 array( 'fa_deleted' => $bits ),
1596 array(
1597 'fa_id' => $this->row->fa_id,
1598 'fa_deleted' => $this->getBits(),
1599 ),
1600 __METHOD__
1601 );
1602 return (bool)$dbw->affectedRows();
1603 }
1604
1605 protected function getLink() {
1606 global $wgLang, $wgUser;
1607 $date = $wgLang->timeanddate( $this->file->getTimestamp(), true );
1608 $undelete = SpecialPage::getTitleFor( 'Undelete' );
1609 $key = $this->file->getKey();
1610 return $this->special->skin->link( $undelete, $date, array(),
1611 array(
1612 'target' => $this->list->title->getPrefixedText(),
1613 'file' => $key,
1614 'token' => $wgUser->editToken( $key )
1615 )
1616 );
1617 }
1618 }
1619
1620 /**
1621 * List for logging table items
1622 */
1623 class RevDel_LogList extends RevDel_List {
1624 var $type = 'logging';
1625 var $idField = 'log_id';
1626 var $dateField = 'log_timestamp';
1627 var $authorIdField = 'log_user';
1628 var $authorNameField = 'log_user_text';
1629
1630 public function doQuery( $db ) {
1631 global $wgMessageCache;
1632 $wgMessageCache->loadAllMessages();
1633 $ids = array_map( 'intval', $this->ids );
1634 return $db->select( 'logging', '*',
1635 array( 'log_id' => $ids ),
1636 __METHOD__
1637 );
1638 }
1639
1640 public function newItem( $row ) {
1641 return new RevDel_LogItem( $this, $row );
1642 }
1643
1644 public function getSuppressBit() {
1645 return Revision::DELETED_RESTRICTED;
1646 }
1647
1648 public function getLogAction() {
1649 return 'event';
1650 }
1651
1652 public function getLogParams( $params ) {
1653 return array(
1654 implode( ',', $params['ids'] ),
1655 "ofield={$params['oldBits']}",
1656 "nfield={$params['newBits']}"
1657 );
1658 }
1659 }
1660
1661 /**
1662 * Item class for a logging table row
1663 */
1664 class RevDel_LogItem extends RevDel_Item {
1665 public function canView() {
1666 return LogEventsList::userCan( $this->row, Revision::DELETED_RESTRICTED );
1667 }
1668
1669 public function getBits() {
1670 return $this->row->log_deleted;
1671 }
1672
1673 public function setBits( $bits ) {
1674 $dbw = wfGetDB( DB_MASTER );
1675 $dbw->update( 'recentchanges',
1676 array(
1677 'rc_deleted' => $bits,
1678 'rc_patrolled' => 1
1679 ),
1680 array(
1681 'rc_logid' => $this->row->log_id,
1682 'rc_timestamp' => $this->row->log_timestamp // index
1683 ),
1684 __METHOD__
1685 );
1686 $dbw->update( 'logging',
1687 array( 'log_deleted' => $bits ),
1688 array(
1689 'log_id' => $this->row->log_id,
1690 'log_deleted' => $this->getBits()
1691 ),
1692 __METHOD__
1693 );
1694 return (bool)$dbw->affectedRows();
1695 }
1696
1697 public function getHTML() {
1698 global $wgLang;
1699
1700 $date = htmlspecialchars( $wgLang->timeanddate( $this->row->log_timestamp ) );
1701 $paramArray = LogPage::extractParams( $this->row->log_params );
1702 $title = Title::makeTitle( $this->row->log_namespace, $this->row->log_title );
1703
1704 // Log link for this page
1705 $loglink = $this->special->skin->link(
1706 SpecialPage::getTitleFor( 'Log' ),
1707 wfMsgHtml( 'log' ),
1708 array(),
1709 array( 'page' => $title->getPrefixedText() )
1710 );
1711 // Action text
1712 if( !$this->canView() ) {
1713 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1714 } else {
1715 $action = LogPage::actionText( $this->row->log_type, $this->row->log_action, $title,
1716 $this->special->skin, $paramArray, true, true );
1717 if( $this->row->log_deleted & LogPage::DELETED_ACTION )
1718 $action = '<span class="history-deleted">' . $action . '</span>';
1719 }
1720 // User links
1721 $userLink = $this->special->skin->userLink( $this->row->log_user,
1722 User::WhoIs( $this->row->log_user ) );
1723 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_USER) ) {
1724 $userLink = '<span class="history-deleted">' . $userLink . '</span>';
1725 }
1726 // Comment
1727 $comment = $wgLang->getDirMark() . $this->special->skin->commentBlock( $this->row->log_comment );
1728 if( LogEventsList::isDeleted($this->row,LogPage::DELETED_COMMENT) ) {
1729 $comment = '<span class="history-deleted">' . $comment . '</span>';
1730 }
1731 return "<li>($loglink) $date $userLink $action $comment</li>";
1732 }
1733 }