(bug 35565) Special:Log/patrol doesn't indicate whether patrolling was automatic
[mediawiki.git] / includes / specials / SpecialRevisiondelete.php
blob0388f5b912ecd936216d005e8b0fcd8276b49a98
1 <?php
2 /**
3 * Implements Special:Revisiondelete
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Special page allowing users with the appropriate permissions to view
26 * and hide revisions. Log items can also be hidden.
28 * @ingroup SpecialPage
30 class SpecialRevisionDelete extends UnlistedSpecialPage {
31 /** True if the submit button was clicked, and the form was posted */
32 var $submitClicked;
34 /** Target ID list */
35 var $ids;
37 /** Archive name, for reviewing deleted files */
38 var $archiveName;
40 /** Edit token for securing image views against XSS */
41 var $token;
43 /** Title object for target parameter */
44 var $targetObj;
46 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
47 var $typeName;
49 /** Array of checkbox specs (message, name, deletion bits) */
50 var $checks;
52 /** Information about the current type */
53 var $typeInfo;
55 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
56 var $list;
58 /**
59 * Assorted information about each type, needed by the special page.
60 * TODO Move some of this to the list class
62 static $allowedTypes = array(
63 'revision' => array(
64 'check-label' => 'revdelete-hide-text',
65 'deletion-bits' => Revision::DELETED_TEXT,
66 'success' => 'revdelete-success',
67 'failure' => 'revdelete-failure',
68 'list-class' => 'RevDel_RevisionList',
70 'archive' => array(
71 'check-label' => 'revdelete-hide-text',
72 'deletion-bits' => Revision::DELETED_TEXT,
73 'success' => 'revdelete-success',
74 'failure' => 'revdelete-failure',
75 'list-class' => 'RevDel_ArchiveList',
77 'oldimage'=> array(
78 'check-label' => 'revdelete-hide-image',
79 'deletion-bits' => File::DELETED_FILE,
80 'success' => 'revdelete-success',
81 'failure' => 'revdelete-failure',
82 'list-class' => 'RevDel_FileList',
84 'filearchive' => array(
85 'check-label' => 'revdelete-hide-image',
86 'deletion-bits' => File::DELETED_FILE,
87 'success' => 'revdelete-success',
88 'failure' => 'revdelete-failure',
89 'list-class' => 'RevDel_ArchivedFileList',
91 'logging' => array(
92 'check-label' => 'revdelete-hide-name',
93 'deletion-bits' => LogPage::DELETED_ACTION,
94 'success' => 'logdelete-success',
95 'failure' => 'logdelete-failure',
96 'list-class' => 'RevDel_LogList',
100 /** Type map to support old log entries */
101 static $deprecatedTypeMap = array(
102 'oldid' => 'revision',
103 'artimestamp' => 'archive',
104 'oldimage' => 'oldimage',
105 'fileid' => 'filearchive',
106 'logid' => 'logging',
109 public function __construct() {
110 parent::__construct( 'Revisiondelete', 'deletedhistory' );
113 public function execute( $par ) {
114 $this->checkPermissions();
115 $this->checkReadOnly();
117 $output = $this->getOutput();
118 $user = $this->getUser();
120 $this->mIsAllowed = $user->isAllowed('deleterevision'); // for changes
121 $this->setHeaders();
122 $this->outputHeader();
123 $request = $this->getRequest();
124 $this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' );
125 # Handle our many different possible input types.
126 $ids = $request->getVal( 'ids' );
127 if ( !is_null( $ids ) ) {
128 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
129 $this->ids = explode( ',', $ids );
130 } else {
131 # Array input
132 $this->ids = array_keys( $request->getArray('ids',array()) );
134 // $this->ids = array_map( 'intval', $this->ids );
135 $this->ids = array_unique( array_filter( $this->ids ) );
137 if ( $request->getVal( 'action' ) == 'historysubmit' || $request->getVal( 'action' ) == 'revisiondelete' ) {
138 // For show/hide form submission from history page
139 // Since we are access through index.php?title=XXX&action=historysubmit
140 // getFullTitle() will contain the target title and not our title
141 $this->targetObj = $this->getFullTitle();
142 $this->typeName = 'revision';
143 } else {
144 $this->typeName = $request->getVal( 'type' );
145 $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
148 # For reviewing deleted files...
149 $this->archiveName = $request->getVal( 'file' );
150 $this->token = $request->getVal( 'token' );
151 if ( $this->archiveName && $this->targetObj ) {
152 $this->tryShowFile( $this->archiveName );
153 return;
156 if ( isset( self::$deprecatedTypeMap[$this->typeName] ) ) {
157 $this->typeName = self::$deprecatedTypeMap[$this->typeName];
160 # No targets?
161 if( !isset( self::$allowedTypes[$this->typeName] ) || count( $this->ids ) == 0 ) {
162 $output->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
163 return;
165 $this->typeInfo = self::$allowedTypes[$this->typeName];
167 # If we have revisions, get the title from the first one
168 # since they should all be from the same page. This allows
169 # for more flexibility with page moves...
170 if( $this->typeName == 'revision' ) {
171 $rev = Revision::newFromId( $this->ids[0] );
172 $this->targetObj = $rev ? $rev->getTitle() : $this->targetObj;
175 $this->otherReason = $request->getVal( 'wpReason' );
176 # We need a target page!
177 if( is_null($this->targetObj) ) {
178 $output->addWikiMsg( 'undelete-header' );
179 return;
181 # Give a link to the logs/hist for this page
182 $this->showConvenienceLinks();
184 # Initialise checkboxes
185 $this->checks = array(
186 array( $this->typeInfo['check-label'], 'wpHidePrimary', $this->typeInfo['deletion-bits'] ),
187 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
188 array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER )
190 if( $user->isAllowed('suppressrevision') ) {
191 $this->checks[] = array( 'revdelete-hide-restricted',
192 'wpHideRestricted', Revision::DELETED_RESTRICTED );
195 # Either submit or create our form
196 if( $this->mIsAllowed && $this->submitClicked ) {
197 $this->submit( $request );
198 } else {
199 $this->showForm();
202 $qc = $this->getLogQueryCond();
203 # Show relevant lines from the deletion log
204 $output->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
205 LogEventsList::showLogExtract( $output, 'delete',
206 $this->targetObj, '', array( 'lim' => 25, 'conds' => $qc ) );
207 # Show relevant lines from the suppression log
208 if( $user->isAllowed( 'suppressionlog' ) ) {
209 $output->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'suppress' ) ) . "</h2>\n" );
210 LogEventsList::showLogExtract( $output, 'suppress',
211 $this->targetObj, '', array( 'lim' => 25, 'conds' => $qc ) );
216 * Show some useful links in the subtitle
218 protected function showConvenienceLinks() {
219 # Give a link to the logs/hist for this page
220 if( $this->targetObj ) {
221 $links = array();
222 $links[] = Linker::linkKnown(
223 SpecialPage::getTitleFor( 'Log' ),
224 wfMsgHtml( 'viewpagelogs' ),
225 array(),
226 array( 'page' => $this->targetObj->getPrefixedText() )
228 if ( !$this->targetObj->isSpecialPage() ) {
229 # Give a link to the page history
230 $links[] = Linker::linkKnown(
231 $this->targetObj,
232 wfMsgHtml( 'pagehist' ),
233 array(),
234 array( 'action' => 'history' )
236 # Link to deleted edits
237 if( $this->getUser()->isAllowed('undelete') ) {
238 $undelete = SpecialPage::getTitleFor( 'Undelete' );
239 $links[] = Linker::linkKnown(
240 $undelete,
241 wfMsgHtml( 'deletedhist' ),
242 array(),
243 array( 'target' => $this->targetObj->getPrefixedDBkey() )
247 # Logs themselves don't have histories or archived revisions
248 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
253 * Get the condition used for fetching log snippets
254 * @return array
256 protected function getLogQueryCond() {
257 $conds = array();
258 // Revision delete logs for these item
259 $conds['log_type'] = array( 'delete', 'suppress' );
260 $conds['log_action'] = $this->getList()->getLogAction();
261 $conds['ls_field'] = RevisionDeleter::getRelationType( $this->typeName );
262 $conds['ls_value'] = $this->ids;
263 return $conds;
267 * Show a deleted file version requested by the visitor.
268 * TODO Mostly copied from Special:Undelete. Refactor.
270 protected function tryShowFile( $archiveName ) {
271 $repo = RepoGroup::singleton()->getLocalRepo();
272 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
273 $oimage->load();
274 // Check if user is allowed to see this file
275 if ( !$oimage->exists() ) {
276 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
277 return;
279 if( !$oimage->userCan( File::DELETED_FILE, $this->getUser() ) ) {
280 if( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
281 $this->getOutput()->permissionRequired( 'suppressrevision' );
282 } else {
283 $this->getOutput()->permissionRequired( 'deletedtext' );
285 return;
287 if ( !$this->getUser()->matchEditToken( $this->token, $archiveName ) ) {
288 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
289 $this->targetObj->getText(),
290 $this->getLanguage()->date( $oimage->getTimestamp() ),
291 $this->getLanguage()->time( $oimage->getTimestamp() ) );
292 $this->getOutput()->addHTML(
293 Xml::openElement( 'form', array(
294 'method' => 'POST',
295 'action' => $this->getTitle()->getLocalUrl(
296 'target=' . urlencode( $oimage->getName() ) .
297 '&file=' . urlencode( $archiveName ) .
298 '&token=' . urlencode( $this->getUser()->getEditToken( $archiveName ) ) )
301 Xml::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
302 '</form>'
304 return;
306 $this->getOutput()->disable();
307 # We mustn't allow the output to be Squid cached, otherwise
308 # if an admin previews a deleted image, and it's cached, then
309 # a user without appropriate permissions can toddle off and
310 # nab the image, and Squid will serve it
311 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
312 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
313 $this->getRequest()->response()->header( 'Pragma: no-cache' );
315 $key = $oimage->getStorageKey();
316 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
317 $repo->streamFile( $path );
321 * Get the list object for this request
323 protected function getList() {
324 if ( is_null( $this->list ) ) {
325 $class = $this->typeInfo['list-class'];
326 $this->list = new $class( $this->getContext(), $this->targetObj, $this->ids );
328 return $this->list;
332 * Show a list of items that we will operate on, and show a form with checkboxes
333 * which will allow the user to choose new visibility settings.
335 protected function showForm() {
336 $UserAllowed = true;
338 if ( $this->typeName == 'logging' ) {
339 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLanguage()->formatNum( count($this->ids) ) );
340 } else {
341 $this->getOutput()->addWikiMsg( 'revdelete-selected',
342 $this->targetObj->getPrefixedText(), count( $this->ids ) );
345 $this->getOutput()->addHTML( "<ul>" );
347 $numRevisions = 0;
348 // Live revisions...
349 $list = $this->getList();
350 for ( $list->reset(); $list->current(); $list->next() ) {
351 $item = $list->current();
352 if ( !$item->canView() ) {
353 if( !$this->submitClicked ) {
354 $this->getOutput()->permissionRequired( 'suppressrevision' );
355 return;
357 $UserAllowed = false;
359 $numRevisions++;
360 $this->getOutput()->addHTML( $item->getHTML() );
363 if( !$numRevisions ) {
364 $this->getOutput()->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
365 return;
368 $this->getOutput()->addHTML( "</ul>" );
369 // Explanation text
370 $this->addUsageText();
372 // Normal sysops can always see what they did, but can't always change it
373 if( !$UserAllowed ) return;
375 // Show form if the user can submit
376 if( $this->mIsAllowed ) {
377 $out = Xml::openElement( 'form', array( 'method' => 'post',
378 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
379 'id' => 'mw-revdel-form-revisions' ) ) .
380 Xml::fieldset( wfMsg( 'revdelete-legend' ) ) .
381 $this->buildCheckBoxes() .
382 Xml::openElement( 'table' ) .
383 "<tr>\n" .
384 '<td class="mw-label">' .
385 Xml::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
386 '</td>' .
387 '<td class="mw-input">' .
388 Xml::listDropDown( 'wpRevDeleteReasonList',
389 wfMsgForContent( 'revdelete-reason-dropdown' ),
390 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
392 '</td>' .
393 "</tr><tr>\n" .
394 '<td class="mw-label">' .
395 Xml::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
396 '</td>' .
397 '<td class="mw-input">' .
398 Xml::input( 'wpReason', 60, $this->otherReason, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
399 '</td>' .
400 "</tr><tr>\n" .
401 '<td></td>' .
402 '<td class="mw-submit">' .
403 Xml::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
404 array( 'name' => 'wpSubmit' ) ) .
405 '</td>' .
406 "</tr>\n" .
407 Xml::closeElement( 'table' ) .
408 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
409 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
410 Html::hidden( 'type', $this->typeName ) .
411 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
412 Xml::closeElement( 'fieldset' ) . "\n";
413 } else {
414 $out = '';
416 if( $this->mIsAllowed ) {
417 $out .= Xml::closeElement( 'form' ) . "\n";
418 // Show link to edit the dropdown reasons
419 if( $this->getUser()->isAllowed( 'editinterface' ) ) {
420 $title = Title::makeTitle( NS_MEDIAWIKI, 'revdelete-reason-dropdown' );
421 $link = Linker::link(
422 $title,
423 wfMsgHtml( 'revdelete-edit-reasonlist' ),
424 array(),
425 array( 'action' => 'edit' )
427 $out .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
430 $this->getOutput()->addHTML( $out );
434 * Show some introductory text
435 * @todo FIXME: Wikimedia-specific policy text
437 protected function addUsageText() {
438 $this->getOutput()->addWikiMsg( 'revdelete-text' );
439 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
440 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
442 if( $this->mIsAllowed ) {
443 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
448 * @return String: HTML
450 protected function buildCheckBoxes() {
451 $html = '<table>';
452 // If there is just one item, use checkboxes
453 $list = $this->getList();
454 if( $list->length() == 1 ) {
455 $list->reset();
456 $bitfield = $list->current()->getBits(); // existing field
457 if( $this->submitClicked ) {
458 $bitfield = $this->extractBitfield( $this->extractBitParams(), $bitfield );
460 foreach( $this->checks as $item ) {
461 list( $message, $name, $field ) = $item;
462 $innerHTML = Xml::checkLabel( wfMsg($message), $name, $name, $bitfield & $field );
463 if( $field == Revision::DELETED_RESTRICTED )
464 $innerHTML = "<b>$innerHTML</b>";
465 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
466 $html .= "<tr>$line</tr>\n";
468 // Otherwise, use tri-state radios
469 } else {
470 $html .= '<tr>';
471 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-same').'</th>';
472 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
473 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</th>';
474 $html .= "<th></th></tr>\n";
475 foreach( $this->checks as $item ) {
476 list( $message, $name, $field ) = $item;
477 // If there are several items, use third state by default...
478 if( $this->submitClicked ) {
479 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
480 } else {
481 $selected = -1; // use existing field
483 $line = '<td class="mw-revdel-checkbox">' . Xml::radio( $name, -1, $selected == -1 ) . '</td>';
484 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 0, $selected == 0 ) . '</td>';
485 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 1, $selected == 1 ) . '</td>';
486 $label = wfMsgHtml($message);
487 if( $field == Revision::DELETED_RESTRICTED ) {
488 $label = "<b>$label</b>";
490 $line .= "<td>$label</td>";
491 $html .= "<tr>$line</tr>\n";
495 $html .= '</table>';
496 return $html;
500 * UI entry point for form submission.
501 * @return bool
503 protected function submit() {
504 # Check edit token on submission
505 $token = $this->getRequest()->getVal('wpEditToken');
506 if( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
507 $this->getOutput()->addWikiMsg( 'sessionfailure' );
508 return false;
510 $bitParams = $this->extractBitParams();
511 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
512 $comment = $listReason;
513 if( $comment != 'other' && $this->otherReason != '' ) {
514 // Entry from drop down menu + additional comment
515 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason;
516 } elseif( $comment == 'other' ) {
517 $comment = $this->otherReason;
519 # Can the user set this field?
520 if( $bitParams[Revision::DELETED_RESTRICTED]==1 && !$this->getUser()->isAllowed('suppressrevision') ) {
521 $this->getOutput()->permissionRequired( 'suppressrevision' );
522 return false;
524 # If the save went through, go to success message...
525 $status = $this->save( $bitParams, $comment, $this->targetObj );
526 if ( $status->isGood() ) {
527 $this->success();
528 return true;
529 # ...otherwise, bounce back to form...
530 } else {
531 $this->failure( $status );
533 return false;
537 * Report that the submit operation succeeded
539 protected function success() {
540 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
541 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo['success'] );
542 $this->list->reloadFromMaster();
543 $this->showForm();
547 * Report that the submit operation failed
549 protected function failure( $status ) {
550 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
551 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeInfo['failure'] ) );
552 $this->showForm();
556 * Put together an array that contains -1, 0, or the *_deleted const for each bit
558 * @return array
560 protected function extractBitParams() {
561 $bitfield = array();
562 foreach( $this->checks as $item ) {
563 list( /* message */ , $name, $field ) = $item;
564 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
565 if( $val < -1 || $val > 1) {
566 $val = -1; // -1 for existing value
568 $bitfield[$field] = $val;
570 if( !isset($bitfield[Revision::DELETED_RESTRICTED]) ) {
571 $bitfield[Revision::DELETED_RESTRICTED] = 0;
573 return $bitfield;
577 * Put together a rev_deleted bitfield
578 * @param $bitPars array extractBitParams() params
579 * @param $oldfield int current bitfield
580 * @return array
582 public static function extractBitfield( $bitPars, $oldfield ) {
583 // Build the actual new rev_deleted bitfield
584 $newBits = 0;
585 foreach( $bitPars as $const => $val ) {
586 if( $val == 1 ) {
587 $newBits |= $const; // $const is the *_deleted const
588 } elseif( $val == -1 ) {
589 $newBits |= ($oldfield & $const); // use existing
592 return $newBits;
596 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
597 * @return
599 protected function save( $bitfield, $reason, $title ) {
600 return $this->getList()->setVisibility(
601 array( 'value' => $bitfield, 'comment' => $reason )