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