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 /** @var bool Was the DB modified in this request */
32 protected $wasSaved = false;
34 /** @var bool True if the submit button was clicked, and the form was posted */
35 private $submitClicked;
37 /** @var array Target ID list */
40 /** @var string Archive name, for reviewing deleted files */
43 /** @var string Edit token for securing image views against XSS */
46 /** @var Title object for target parameter */
49 /** @var string Deletion type, may be revision, archive, oldimage, filearchive, logging. */
52 /** @var array of checkbox specs (message, name, deletion bits) */
55 /** @var array UI Labels about the current type */
58 /** @var RevDel_List object, storing the list of items to be deleted/undeleted */
61 /** @var bool Whether user is allowed to perform the action */
68 * UI labels for each type.
70 private static $UILabels = array(
72 'check-label' => 'revdelete-hide-text',
73 'success' => 'revdelete-success',
74 'failure' => 'revdelete-failure',
75 'text' => 'revdelete-text-text',
76 'selected'=> 'revdelete-selected-text',
79 'check-label' => 'revdelete-hide-text',
80 'success' => 'revdelete-success',
81 'failure' => 'revdelete-failure',
82 'text' => 'revdelete-text-text',
83 'selected'=> 'revdelete-selected-text',
86 'check-label' => 'revdelete-hide-image',
87 'success' => 'revdelete-success',
88 'failure' => 'revdelete-failure',
89 'text' => 'revdelete-text-file',
90 'selected'=> 'revdelete-selected-file',
92 'filearchive' => array(
93 'check-label' => 'revdelete-hide-image',
94 'success' => 'revdelete-success',
95 'failure' => 'revdelete-failure',
96 'text' => 'revdelete-text-file',
97 'selected'=> 'revdelete-selected-file',
100 'check-label' => 'revdelete-hide-name',
101 'success' => 'logdelete-success',
102 'failure' => 'logdelete-failure',
103 'text' => 'logdelete-text',
104 'selected' => 'logdelete-selected',
108 public function __construct() {
109 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
112 public function execute( $par ) {
113 $this->checkPermissions();
114 $this->checkReadOnly();
116 $output = $this->getOutput();
117 $user = $this->getUser();
120 $this->outputHeader();
121 $request = $this->getRequest();
122 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
123 # Handle our many different possible input types.
124 $ids = $request->getVal( 'ids' );
125 if ( !is_null( $ids ) ) {
126 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
127 $this->ids
= explode( ',', $ids );
130 $this->ids
= array_keys( $request->getArray( 'ids', array() ) );
132 // $this->ids = array_map( 'intval', $this->ids );
133 $this->ids
= array_unique( array_filter( $this->ids
) );
135 if ( $request->getVal( 'action' ) == 'historysubmit' ||
$request->getVal( 'action' ) == 'revisiondelete' ) {
136 // For show/hide form submission from history page
137 // Since we are access through index.php?title=XXX&action=historysubmit
138 // getFullTitle() will contain the target title and not our title
139 $this->targetObj
= $this->getFullTitle();
140 $this->typeName
= 'revision';
142 $this->typeName
= $request->getVal( 'type' );
143 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
146 # For reviewing deleted files...
147 $this->archiveName
= $request->getVal( 'file' );
148 $this->token
= $request->getVal( 'token' );
149 if ( $this->archiveName
&& $this->targetObj
) {
150 $this->tryShowFile( $this->archiveName
);
155 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
158 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
159 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
161 $this->typeLabels
= self
::$UILabels[$this->typeName
];
162 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
164 # Allow the list type to adjust the passed target
165 $this->targetObj
= RevisionDeleter
::suggestTarget( $this->typeName
, $this->targetObj
, $this->ids
);
167 $this->otherReason
= $request->getVal( 'wpReason' );
168 # We need a target page!
169 if ( is_null( $this->targetObj
) ) {
170 $output->addWikiMsg( 'undelete-header' );
174 # Give a link to the logs/hist for this page
175 $this->showConvenienceLinks();
177 # Initialise checkboxes
178 $this->checks
= array(
179 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
180 RevisionDeleter
::getRevdelConstant( $this->typeName
)
182 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
183 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
185 if ( $user->isAllowed( 'suppressrevision' ) ) {
186 $this->checks
[] = array( 'revdelete-hide-restricted',
187 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
190 # Either submit or create our form
191 if ( $this->mIsAllowed
&& $this->submitClicked
) {
192 $this->submit( $request );
197 $qc = $this->getLogQueryCond();
198 # Show relevant lines from the deletion log
199 $deleteLogPage = new LogPage( 'delete' );
200 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
201 LogEventsList
::showLogExtract(
206 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
208 # Show relevant lines from the suppression log
209 if ( $user->isAllowed( 'suppressionlog' ) ) {
210 $suppressLogPage = new LogPage( 'suppress' );
211 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
212 LogEventsList
::showLogExtract(
217 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
223 * Show some useful links in the subtitle
225 protected function showConvenienceLinks() {
226 # Give a link to the logs/hist for this page
227 if ( $this->targetObj
) {
228 // Also set header tabs to be for the target.
229 $this->getSkin()->setRelevantTitle( $this->targetObj
);
232 $links[] = Linker
::linkKnown(
233 SpecialPage
::getTitleFor( 'Log' ),
234 $this->msg( 'viewpagelogs' )->escaped(),
236 array( 'page' => $this->targetObj
->getPrefixedText() )
238 if ( !$this->targetObj
->isSpecialPage() ) {
239 # Give a link to the page history
240 $links[] = Linker
::linkKnown(
242 $this->msg( 'pagehist' )->escaped(),
244 array( 'action' => 'history' )
246 # Link to deleted edits
247 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
248 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
249 $links[] = Linker
::linkKnown(
251 $this->msg( 'deletedhist' )->escaped(),
253 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
257 # Logs themselves don't have histories or archived revisions
258 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
263 * Get the condition used for fetching log snippets
266 protected function getLogQueryCond() {
268 // Revision delete logs for these item
269 $conds['log_type'] = array( 'delete', 'suppress' );
270 $conds['log_action'] = $this->getList()->getLogAction();
271 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
272 $conds['ls_value'] = $this->ids
;
278 * Show a deleted file version requested by the visitor.
279 * TODO Mostly copied from Special:Undelete. Refactor.
281 protected function tryShowFile( $archiveName ) {
282 $repo = RepoGroup
::singleton()->getLocalRepo();
283 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
285 // Check if user is allowed to see this file
286 if ( !$oimage->exists() ) {
287 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
291 $user = $this->getUser();
292 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
293 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
294 throw new PermissionsError( 'suppressrevision' );
296 throw new PermissionsError( 'deletedtext' );
299 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
300 $lang = $this->getLanguage();
301 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
302 $this->targetObj
->getText(),
303 $lang->userDate( $oimage->getTimestamp(), $user ),
304 $lang->userTime( $oimage->getTimestamp(), $user ) );
305 $this->getOutput()->addHTML(
306 Xml
::openElement( 'form', array(
308 'action' => $this->getPageTitle()->getLocalURL( array(
309 'target' => $this->targetObj
->getPrefixedDBkey(),
310 'file' => $archiveName,
311 'token' => $user->getEditToken( $archiveName ),
315 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
321 $this->getOutput()->disable();
322 # We mustn't allow the output to be Squid cached, otherwise
323 # if an admin previews a deleted image, and it's cached, then
324 # a user without appropriate permissions can toddle off and
325 # nab the image, and Squid will serve it
326 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
327 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
328 $this->getRequest()->response()->header( 'Pragma: no-cache' );
330 $key = $oimage->getStorageKey();
331 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
332 $repo->streamFile( $path );
336 * Get the list object for this request
338 protected function getList() {
339 if ( is_null( $this->revDelList
) ) {
340 $this->revDelList
= RevisionDeleter
::createList(
341 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
345 return $this->revDelList
;
349 * Show a list of items that we will operate on, and show a form with checkboxes
350 * which will allow the user to choose new visibility settings.
352 protected function showForm() {
355 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels
['selected'],
356 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ) );
358 $this->getOutput()->addHTML( "<ul>" );
362 $list = $this->getList();
363 for ( $list->reset(); $list->current(); $list->next() ) {
364 $item = $list->current();
365 if ( !$item->canView() ) {
366 if ( !$this->submitClicked
) {
367 throw new PermissionsError( 'suppressrevision' );
369 $userAllowed = false;
372 $this->getOutput()->addHTML( $item->getHTML() );
375 if ( !$numRevisions ) {
376 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
379 $this->getOutput()->addHTML( "</ul>" );
381 $this->addUsageText();
383 // Normal sysops can always see what they did, but can't always change it
384 if ( !$userAllowed ) {
388 // Show form if the user can submit
389 if ( $this->mIsAllowed
) {
390 $out = Xml
::openElement( 'form', array( 'method' => 'post',
391 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
392 'id' => 'mw-revdel-form-revisions' ) ) .
393 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
394 $this->buildCheckBoxes() .
395 Xml
::openElement( 'table' ) .
397 '<td class="mw-label">' .
398 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
400 '<td class="mw-input">' .
401 Xml
::listDropDown( 'wpRevDeleteReasonList',
402 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
403 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
404 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
408 '<td class="mw-label">' .
409 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
411 '<td class="mw-input">' .
412 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
416 '<td class="mw-submit">' .
417 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
418 array( 'name' => 'wpSubmit' ) ) .
421 Xml
::closeElement( 'table' ) .
422 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
423 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
424 Html
::hidden( 'type', $this->typeName
) .
425 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
426 Xml
::closeElement( 'fieldset' ) . "\n";
430 if ( $this->mIsAllowed
) {
431 $out .= Xml
::closeElement( 'form' ) . "\n";
432 // Show link to edit the dropdown reasons
433 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
434 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
435 $link = Linker
::link(
437 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
439 array( 'action' => 'edit' )
441 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
444 $this->getOutput()->addHTML( $out );
448 * Show some introductory text
449 * @todo FIXME: Wikimedia-specific policy text
451 protected function addUsageText() {
452 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>\n$2", $this->typeLabels
['text'], 'revdelete-text-others' );
453 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
454 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
456 if ( $this->mIsAllowed
) {
457 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
462 * @return String: HTML
464 protected function buildCheckBoxes() {
466 // If there is just one item, use checkboxes
467 $list = $this->getList();
468 if ( $list->length() == 1 ) {
470 $bitfield = $list->current()->getBits(); // existing field
471 if ( $this->submitClicked
) {
472 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
474 foreach ( $this->checks
as $item ) {
475 list( $message, $name, $field ) = $item;
476 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
477 if ( $field == Revision
::DELETED_RESTRICTED
) {
478 $innerHTML = "<b>$innerHTML</b>";
480 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
481 $html .= "<tr>$line</tr>\n";
484 // Otherwise, use tri-state radios
486 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
487 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
488 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
489 $html .= "<th></th></tr>\n";
490 foreach ( $this->checks
as $item ) {
491 list( $message, $name, $field ) = $item;
492 // If there are several items, use third state by default...
493 if ( $this->submitClicked
) {
494 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
496 $selected = -1; // use existing field
498 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
499 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
500 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
501 $label = $this->msg( $message )->escaped();
502 if ( $field == Revision
::DELETED_RESTRICTED
) {
503 $label = "<b>$label</b>";
505 $line .= "<td>$label</td>";
506 $html .= "<tr>$line</tr>\n";
516 * UI entry point for form submission.
517 * @throws PermissionsError
520 protected function submit() {
521 # Check edit token on submission
522 $token = $this->getRequest()->getVal( 'wpEditToken' );
523 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
524 $this->getOutput()->addWikiMsg( 'sessionfailure' );
528 $bitParams = $this->extractBitParams();
529 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
530 $comment = $listReason;
531 if ( $comment != 'other' && $this->otherReason
!= '' ) {
532 // Entry from drop down menu + additional comment
533 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
534 } elseif ( $comment == 'other' ) {
535 $comment = $this->otherReason
;
537 # Can the user set this field?
538 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1 && !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
539 throw new PermissionsError( 'suppressrevision' );
541 # If the save went through, go to success message...
542 $status = $this->save( $bitParams, $comment, $this->targetObj
);
543 if ( $status->isGood() ) {
548 # ...otherwise, bounce back to form...
549 $this->failure( $status );
556 * Report that the submit operation succeeded
558 protected function success() {
559 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
560 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeLabels
['success'] );
561 $this->wasSaved
= true;
562 $this->revDelList
->reloadFromMaster();
567 * Report that the submit operation failed
569 protected function failure( $status ) {
570 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
571 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
576 * Put together an array that contains -1, 0, or the *_deleted const for each bit
580 protected function extractBitParams() {
582 foreach ( $this->checks
as $item ) {
583 list( /* message */, $name, $field ) = $item;
584 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
585 if ( $val < -1 ||
$val > 1 ) {
586 $val = -1; // -1 for existing value
588 $bitfield[$field] = $val;
590 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
591 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
598 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
604 protected function save( $bitfield, $reason, $title ) {
605 return $this->getList()->setVisibility(
606 array( 'value' => $bitfield, 'comment' => $reason )
610 protected function getGroupName() {