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
21 * @ingroup SpecialPage
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 */
37 /** Archive name, for reviewing deleted files */
40 /** Edit token for securing image views against XSS */
43 /** Title object for target parameter */
46 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
49 /** Array of checkbox specs (message, name, deletion bits) */
52 /** Information about the current type */
55 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
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(
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',
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',
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',
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 $output = $this->getOutput();
115 $user = $this->getUser();
116 if( !$user->isAllowed( 'deletedhistory' ) ) {
117 $output->permissionRequired( 'deletedhistory' );
119 } else if( wfReadOnly() ) {
120 $output->readOnlyPage();
123 $this->mIsAllowed
= $user->isAllowed('deleterevision'); // for changes
125 $this->outputHeader();
126 $request = $this->getRequest();
127 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
128 # Handle our many different possible input types.
129 $ids = $request->getVal( 'ids' );
130 if ( !is_null( $ids ) ) {
131 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
132 $this->ids
= explode( ',', $ids );
135 $this->ids
= array_keys( $request->getArray('ids',array()) );
137 // $this->ids = array_map( 'intval', $this->ids );
138 $this->ids
= array_unique( array_filter( $this->ids
) );
140 if ( $request->getVal( 'action' ) == 'historysubmit' ) {
141 // For show/hide form submission from history page
142 // Since we are access through index.php?title=XXX&action=historysubmit
143 // getFullTitle() will contain the target title and not our title
144 $this->targetObj
= $this->getFullTitle();
145 $this->typeName
= 'revision';
147 $this->typeName
= $request->getVal( 'type' );
148 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
151 # For reviewing deleted files...
152 $this->archiveName
= $request->getVal( 'file' );
153 $this->token
= $request->getVal( 'token' );
154 if ( $this->archiveName
&& $this->targetObj
) {
155 $this->tryShowFile( $this->archiveName
);
159 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
160 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
164 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
165 $output->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
168 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
170 # If we have revisions, get the title from the first one
171 # since they should all be from the same page. This allows
172 # for more flexibility with page moves...
173 if( $this->typeName
== 'revision' ) {
174 $rev = Revision
::newFromId( $this->ids
[0] );
175 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
178 $this->otherReason
= $request->getVal( 'wpReason' );
179 # We need a target page!
180 if( is_null($this->targetObj
) ) {
181 $output->addWikiMsg( 'undelete-header' );
184 # Give a link to the logs/hist for this page
185 $this->showConvenienceLinks();
187 # Initialise checkboxes
188 $this->checks
= array(
189 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
190 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
191 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
193 if( $user->isAllowed('suppressrevision') ) {
194 $this->checks
[] = array( 'revdelete-hide-restricted',
195 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
198 # Either submit or create our form
199 if( $this->mIsAllowed
&& $this->submitClicked
) {
200 $this->submit( $request );
205 $qc = $this->getLogQueryCond();
206 # Show relevant lines from the deletion log
207 $output->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
208 LogEventsList
::showLogExtract( $output, 'delete',
209 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
210 # Show relevant lines from the suppression log
211 if( $user->isAllowed( 'suppressionlog' ) ) {
212 $output->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
213 LogEventsList
::showLogExtract( $output, 'suppress',
214 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
219 * Show some useful links in the subtitle
221 protected function showConvenienceLinks() {
222 # Give a link to the logs/hist for this page
223 if( $this->targetObj
) {
225 $links[] = Linker
::linkKnown(
226 SpecialPage
::getTitleFor( 'Log' ),
227 wfMsgHtml( 'viewpagelogs' ),
229 array( 'page' => $this->targetObj
->getPrefixedText() )
231 if ( $this->targetObj
->getNamespace() != NS_SPECIAL
) {
232 # Give a link to the page history
233 $links[] = Linker
::linkKnown(
235 wfMsgHtml( 'pagehist' ),
237 array( 'action' => 'history' )
239 # Link to deleted edits
240 if( $this->getUser()->isAllowed('undelete') ) {
241 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
242 $links[] = Linker
::linkKnown(
244 wfMsgHtml( 'deletedhist' ),
246 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
250 # Logs themselves don't have histories or archived revisions
251 $this->getOutput()->setSubtitle( '<p>' . $this->getLang()->pipeList( $links ) . '</p>' );
256 * Get the condition used for fetching log snippets
258 protected function getLogQueryCond() {
260 // Revision delete logs for these item
261 $conds['log_type'] = array('delete','suppress');
262 $conds['log_action'] = $this->getList()->getLogAction();
263 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
264 $conds['ls_value'] = $this->ids
;
269 * Show a deleted file version requested by the visitor.
270 * TODO Mostly copied from Special:Undelete. Refactor.
272 protected function tryShowFile( $archiveName ) {
273 $repo = RepoGroup
::singleton()->getLocalRepo();
274 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
276 // Check if user is allowed to see this file
277 if ( !$oimage->exists() ) {
278 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
281 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
282 if( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
283 $this->getOutput()->permissionRequired( 'suppressrevision' );
285 $this->getOutput()->permissionRequired( 'deletedtext' );
289 if ( !$this->getUser()->matchEditToken( $this->token
, $archiveName ) ) {
290 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
291 $this->targetObj
->getText(),
292 $this->getLang()->date( $oimage->getTimestamp() ),
293 $this->getLang()->time( $oimage->getTimestamp() ) );
294 $this->getOutput()->addHTML(
295 Xml
::openElement( 'form', array(
297 'action' => $this->getTitle()->getLocalUrl(
298 'target=' . urlencode( $oimage->getName() ) .
299 '&file=' . urlencode( $archiveName ) .
300 '&token=' . urlencode( $this->getUser()->editToken( $archiveName ) ) )
303 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
308 $this->getOutput()->disable();
309 # We mustn't allow the output to be Squid cached, otherwise
310 # if an admin previews a deleted image, and it's cached, then
311 # a user without appropriate permissions can toddle off and
312 # nab the image, and Squid will serve it
313 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
314 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
315 $this->getRequest()->response()->header( 'Pragma: no-cache' );
317 # Stream the file to the client
319 require_once( "$IP/includes/StreamFile.php" );
320 $key = $oimage->getStorageKey();
321 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
322 wfStreamFile( $path );
326 * Get the list object for this request
328 protected function getList() {
329 if ( is_null( $this->list ) ) {
330 $class = $this->typeInfo
['list-class'];
331 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
337 * Show a list of items that we will operate on, and show a form with checkboxes
338 * which will allow the user to choose new visibility settings.
340 protected function showForm() {
343 if ( $this->typeName
== 'logging' ) {
344 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLang()->formatNum( count($this->ids
) ) );
346 $this->getOutput()->addWikiMsg( 'revdelete-selected',
347 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
350 $this->getOutput()->addHTML( "<ul>" );
354 $list = $this->getList();
355 for ( $list->reset(); $list->current(); $list->next() ) {
356 $item = $list->current();
357 if ( !$item->canView() ) {
358 if( !$this->submitClicked
) {
359 $this->getOutput()->permissionRequired( 'suppressrevision' );
362 $UserAllowed = false;
365 $this->getOutput()->addHTML( $item->getHTML() );
368 if( !$numRevisions ) {
369 $this->getOutput()->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
373 $this->getOutput()->addHTML( "</ul>" );
375 $this->addUsageText();
377 // Normal sysops can always see what they did, but can't always change it
378 if( !$UserAllowed ) return;
380 // Show form if the user can submit
381 if( $this->mIsAllowed
) {
382 $out = Xml
::openElement( 'form', array( 'method' => 'post',
383 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
384 'id' => 'mw-revdel-form-revisions' ) ) .
385 Xml
::fieldset( wfMsg( 'revdelete-legend' ) ) .
386 $this->buildCheckBoxes() .
387 Xml
::openElement( 'table' ) .
389 '<td class="mw-label">' .
390 Xml
::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
392 '<td class="mw-input">' .
393 Xml
::listDropDown( 'wpRevDeleteReasonList',
394 wfMsgForContent( 'revdelete-reason-dropdown' ),
395 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
399 '<td class="mw-label">' .
400 Xml
::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
402 '<td class="mw-input">' .
403 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
407 '<td class="mw-submit">' .
408 Xml
::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
409 array( 'name' => 'wpSubmit' ) ) .
412 Xml
::closeElement( 'table' ) .
413 Html
::hidden( 'wpEditToken', $this->getUser()->editToken() ) .
414 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
415 Html
::hidden( 'type', $this->typeName
) .
416 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
417 Xml
::closeElement( 'fieldset' ) . "\n";
421 if( $this->mIsAllowed
) {
422 $out .= Xml
::closeElement( 'form' ) . "\n";
423 // Show link to edit the dropdown reasons
424 if( $this->getUser()->isAllowed( 'editinterface' ) ) {
425 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'revdelete-reason-dropdown' );
426 $link = Linker
::link(
428 wfMsgHtml( 'revdelete-edit-reasonlist' ),
430 array( 'action' => 'edit' )
432 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
435 $this->getOutput()->addHTML( $out );
439 * Show some introductory text
440 * @todo FIXME: Wikimedia-specific policy text
442 protected function addUsageText() {
443 $this->getOutput()->addWikiMsg( 'revdelete-text' );
444 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
445 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
447 if( $this->mIsAllowed
) {
448 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
453 * @return String: HTML
455 protected function buildCheckBoxes() {
457 // If there is just one item, use checkboxes
458 $list = $this->getList();
459 if( $list->length() == 1 ) {
461 $bitfield = $list->current()->getBits(); // existing field
462 if( $this->submitClicked
) {
463 $bitfield = $this->extractBitfield( $this->extractBitParams(), $bitfield );
465 foreach( $this->checks
as $item ) {
466 list( $message, $name, $field ) = $item;
467 $innerHTML = Xml
::checkLabel( wfMsg($message), $name, $name, $bitfield & $field );
468 if( $field == Revision
::DELETED_RESTRICTED
)
469 $innerHTML = "<b>$innerHTML</b>";
470 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
471 $html .= "<tr>$line</tr>\n";
473 // Otherwise, use tri-state radios
476 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-same').'</th>';
477 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
478 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</th>';
479 $html .= "<th></th></tr>\n";
480 foreach( $this->checks
as $item ) {
481 list( $message, $name, $field ) = $item;
482 // If there are several items, use third state by default...
483 if( $this->submitClicked
) {
484 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
486 $selected = -1; // use existing field
488 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
489 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
490 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
491 $label = wfMsgHtml($message);
492 if( $field == Revision
::DELETED_RESTRICTED
) {
493 $label = "<b>$label</b>";
495 $line .= "<td>$label</td>";
496 $html .= "<tr>$line</tr>\n";
505 * UI entry point for form submission.
507 protected function submit() {
508 # Check edit token on submission
509 if( $this->submitClicked
&& !$this->getUser()->matchEditToken( $this->getRequest()->getVal('wpEditToken') ) ) {
510 $this->getOutput()->addWikiMsg( 'sessionfailure' );
513 $bitParams = $this->extractBitParams();
514 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
515 $comment = $listReason;
516 if( $comment != 'other' && $this->otherReason
!= '' ) {
517 // Entry from drop down menu + additional comment
518 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason
;
519 } elseif( $comment == 'other' ) {
520 $comment = $this->otherReason
;
522 # Can the user set this field?
523 if( $bitParams[Revision
::DELETED_RESTRICTED
]==1 && !$this->getUser()->isAllowed('suppressrevision') ) {
524 $this->getOutput()->permissionRequired( 'suppressrevision' );
527 # If the save went through, go to success message...
528 $status = $this->save( $bitParams, $comment, $this->targetObj
);
529 if ( $status->isGood() ) {
532 # ...otherwise, bounce back to form...
534 $this->failure( $status );
540 * Report that the submit operation succeeded
542 protected function success() {
543 $this->getOutput()->setPagetitle( wfMsg( 'actioncomplete' ) );
544 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo
['success'] );
545 $this->list->reloadFromMaster();
550 * Report that the submit operation failed
552 protected function failure( $status ) {
553 $this->getOutput()->setPagetitle( wfMsg( 'actionfailed' ) );
554 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
559 * Put together an array that contains -1, 0, or the *_deleted const for each bit
563 protected function extractBitParams() {
565 foreach( $this->checks
as $item ) {
566 list( /* message */ , $name, $field ) = $item;
567 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
568 if( $val < -1 ||
$val > 1) {
569 $val = -1; // -1 for existing value
571 $bitfield[$field] = $val;
573 if( !isset($bitfield[Revision
::DELETED_RESTRICTED
]) ) {
574 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
580 * Put together a rev_deleted bitfield
581 * @param $bitPars array extractBitParams() params
582 * @param $oldfield int current bitfield
585 public static function extractBitfield( $bitPars, $oldfield ) {
586 // Build the actual new rev_deleted bitfield
588 foreach( $bitPars as $const => $val ) {
590 $newBits |
= $const; // $const is the *_deleted const
591 } else if( $val == -1 ) {
592 $newBits |
= ($oldfield & $const); // use existing
599 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
601 protected function save( $bitfield, $reason, $title ) {
602 return $this->getList()->setVisibility(
603 array( 'value' => $bitfield, 'comment' => $reason )