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 /** UI Labels about the current type */
55 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
58 /** Was the DB modified in this request */
59 protected $wasSaved = false;
62 * UI labels for each type.
64 static $UILabels = array(
66 'check-label' => 'revdelete-hide-text',
67 'success' => 'revdelete-success',
68 'failure' => 'revdelete-failure',
69 'text' => 'revdelete-text-text',
72 'check-label' => 'revdelete-hide-text',
73 'success' => 'revdelete-success',
74 'failure' => 'revdelete-failure',
75 'text' => 'revdelete-text-text',
78 'check-label' => 'revdelete-hide-image',
79 'success' => 'revdelete-success',
80 'failure' => 'revdelete-failure',
81 'text' => 'revdelete-text-file',
83 'filearchive' => array(
84 'check-label' => 'revdelete-hide-image',
85 'success' => 'revdelete-success',
86 'failure' => 'revdelete-failure',
87 'text' => 'revdelete-text-file',
90 'check-label' => 'revdelete-hide-name',
91 'success' => 'logdelete-success',
92 'failure' => 'logdelete-failure',
93 'text' => 'logdelete-text',
97 public function __construct() {
98 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
101 public function execute( $par ) {
102 $this->checkPermissions();
103 $this->checkReadOnly();
105 $output = $this->getOutput();
106 $user = $this->getUser();
109 $this->outputHeader();
110 $request = $this->getRequest();
111 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
112 # Handle our many different possible input types.
113 $ids = $request->getVal( 'ids' );
114 if ( !is_null( $ids ) ) {
115 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
116 $this->ids
= explode( ',', $ids );
119 $this->ids
= array_keys( $request->getArray( 'ids', array() ) );
121 // $this->ids = array_map( 'intval', $this->ids );
122 $this->ids
= array_unique( array_filter( $this->ids
) );
124 if ( $request->getVal( 'action' ) == 'historysubmit' ||
$request->getVal( 'action' ) == 'revisiondelete' ) {
125 // For show/hide form submission from history page
126 // Since we are access through index.php?title=XXX&action=historysubmit
127 // getFullTitle() will contain the target title and not our title
128 $this->targetObj
= $this->getFullTitle();
129 $this->typeName
= 'revision';
131 $this->typeName
= $request->getVal( 'type' );
132 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
135 # For reviewing deleted files...
136 $this->archiveName
= $request->getVal( 'file' );
137 $this->token
= $request->getVal( 'token' );
138 if ( $this->archiveName
&& $this->targetObj
) {
139 $this->tryShowFile( $this->archiveName
);
144 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
147 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
148 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
150 $this->typeLabels
= self
::$UILabels[$this->typeName
];
151 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
153 # Allow the list type to adjust the passed target
154 $this->targetObj
= RevisionDeleter
::suggestTarget( $this->typeName
, $this->targetObj
, $this->ids
);
156 $this->otherReason
= $request->getVal( 'wpReason' );
157 # We need a target page!
158 if ( is_null( $this->targetObj
) ) {
159 $output->addWikiMsg( 'undelete-header' );
163 # Give a link to the logs/hist for this page
164 $this->showConvenienceLinks();
166 # Initialise checkboxes
167 $this->checks
= array(
168 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
169 RevisionDeleter
::getRevdelConstant( $this->typeName
)
171 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
172 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
174 if ( $user->isAllowed( 'suppressrevision' ) ) {
175 $this->checks
[] = array( 'revdelete-hide-restricted',
176 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
179 # Either submit or create our form
180 if ( $this->mIsAllowed
&& $this->submitClicked
) {
181 $this->submit( $request );
186 $qc = $this->getLogQueryCond();
187 # Show relevant lines from the deletion log
188 $deleteLogPage = new LogPage( 'delete' );
189 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
190 LogEventsList
::showLogExtract(
195 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
197 # Show relevant lines from the suppression log
198 if ( $user->isAllowed( 'suppressionlog' ) ) {
199 $suppressLogPage = new LogPage( 'suppress' );
200 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
201 LogEventsList
::showLogExtract(
206 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
212 * Show some useful links in the subtitle
214 protected function showConvenienceLinks() {
215 # Give a link to the logs/hist for this page
216 if ( $this->targetObj
) {
217 // Also set header tabs to be for the target.
218 $this->getSkin()->setRelevantTitle( $this->targetObj
);
221 $links[] = Linker
::linkKnown(
222 SpecialPage
::getTitleFor( 'Log' ),
223 $this->msg( 'viewpagelogs' )->escaped(),
225 array( 'page' => $this->targetObj
->getPrefixedText() )
227 if ( !$this->targetObj
->isSpecialPage() ) {
228 # Give a link to the page history
229 $links[] = Linker
::linkKnown(
231 $this->msg( 'pagehist' )->escaped(),
233 array( 'action' => 'history' )
235 # Link to deleted edits
236 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
237 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
238 $links[] = Linker
::linkKnown(
240 $this->msg( 'deletedhist' )->escaped(),
242 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
246 # Logs themselves don't have histories or archived revisions
247 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
252 * Get the condition used for fetching log snippets
255 protected function getLogQueryCond() {
257 // Revision delete logs for these item
258 $conds['log_type'] = array( 'delete', 'suppress' );
259 $conds['log_action'] = $this->getList()->getLogAction();
260 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
261 $conds['ls_value'] = $this->ids
;
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 );
274 // Check if user is allowed to see this file
275 if ( !$oimage->exists() ) {
276 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
280 $user = $this->getUser();
281 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
282 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
283 throw new PermissionsError( 'suppressrevision' );
285 throw new PermissionsError( 'deletedtext' );
288 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
289 $lang = $this->getLanguage();
290 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
291 $this->targetObj
->getText(),
292 $lang->userDate( $oimage->getTimestamp(), $user ),
293 $lang->userTime( $oimage->getTimestamp(), $user ) );
294 $this->getOutput()->addHTML(
295 Xml
::openElement( 'form', array(
297 'action' => $this->getPageTitle()->getLocalURL( array(
298 'target' => $this->targetObj
->getPrefixedDBkey(),
299 'file' => $archiveName,
300 'token' => $user->getEditToken( $archiveName ),
304 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
310 $this->getOutput()->disable();
311 # We mustn't allow the output to be Squid cached, otherwise
312 # if an admin previews a deleted image, and it's cached, then
313 # a user without appropriate permissions can toddle off and
314 # nab the image, and Squid will serve it
315 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
316 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
317 $this->getRequest()->response()->header( 'Pragma: no-cache' );
319 $key = $oimage->getStorageKey();
320 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
321 $repo->streamFile( $path );
325 * Get the list object for this request
327 protected function getList() {
328 if ( is_null( $this->list ) ) {
329 $this->list = RevisionDeleter
::createList(
330 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
338 * Show a list of items that we will operate on, and show a form with checkboxes
339 * which will allow the user to choose new visibility settings.
341 protected function showForm() {
344 if ( $this->typeName
== 'logging' ) {
345 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLanguage()->formatNum( count( $this->ids
) ) );
347 $this->getOutput()->addWikiMsg( 'revdelete-selected',
348 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
351 $this->getOutput()->addHTML( "<ul>" );
355 $list = $this->getList();
356 for ( $list->reset(); $list->current(); $list->next() ) {
357 $item = $list->current();
358 if ( !$item->canView() ) {
359 if ( !$this->submitClicked
) {
360 throw new PermissionsError( 'suppressrevision' );
362 $userAllowed = false;
365 $this->getOutput()->addHTML( $item->getHTML() );
368 if ( !$numRevisions ) {
369 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
372 $this->getOutput()->addHTML( "</ul>" );
374 $this->addUsageText();
376 // Normal sysops can always see what they did, but can't always change it
377 if ( !$userAllowed ) {
381 // Show form if the user can submit
382 if ( $this->mIsAllowed
) {
383 $out = Xml
::openElement( 'form', array( 'method' => 'post',
384 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
385 'id' => 'mw-revdel-form-revisions' ) ) .
386 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
387 $this->buildCheckBoxes() .
388 Xml
::openElement( 'table' ) .
390 '<td class="mw-label">' .
391 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
393 '<td class="mw-input">' .
394 Xml
::listDropDown( 'wpRevDeleteReasonList',
395 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
396 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
397 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
401 '<td class="mw-label">' .
402 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
404 '<td class="mw-input">' .
405 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
409 '<td class="mw-submit">' .
410 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
411 array( 'name' => 'wpSubmit' ) ) .
414 Xml
::closeElement( 'table' ) .
415 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
416 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
417 Html
::hidden( 'type', $this->typeName
) .
418 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
419 Xml
::closeElement( 'fieldset' ) . "\n";
423 if ( $this->mIsAllowed
) {
424 $out .= Xml
::closeElement( 'form' ) . "\n";
425 // Show link to edit the dropdown reasons
426 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
427 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
428 $link = Linker
::link(
430 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
432 array( 'action' => 'edit' )
434 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
437 $this->getOutput()->addHTML( $out );
441 * Show some introductory text
442 * @todo FIXME: Wikimedia-specific policy text
444 protected function addUsageText() {
445 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>\n$2", $this->typeLabels
['text'], 'revdelete-text-others' );
446 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
447 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
449 if ( $this->mIsAllowed
) {
450 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
455 * @return String: HTML
457 protected function buildCheckBoxes() {
459 // If there is just one item, use checkboxes
460 $list = $this->getList();
461 if ( $list->length() == 1 ) {
463 $bitfield = $list->current()->getBits(); // existing field
464 if ( $this->submitClicked
) {
465 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
467 foreach ( $this->checks
as $item ) {
468 list( $message, $name, $field ) = $item;
469 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
470 if ( $field == Revision
::DELETED_RESTRICTED
) {
471 $innerHTML = "<b>$innerHTML</b>";
473 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
474 $html .= "<tr>$line</tr>\n";
477 // Otherwise, use tri-state radios
479 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
480 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
481 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
482 $html .= "<th></th></tr>\n";
483 foreach ( $this->checks
as $item ) {
484 list( $message, $name, $field ) = $item;
485 // If there are several items, use third state by default...
486 if ( $this->submitClicked
) {
487 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
489 $selected = -1; // use existing field
491 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
492 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
493 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
494 $label = $this->msg( $message )->escaped();
495 if ( $field == Revision
::DELETED_RESTRICTED
) {
496 $label = "<b>$label</b>";
498 $line .= "<td>$label</td>";
499 $html .= "<tr>$line</tr>\n";
509 * UI entry point for form submission.
510 * @throws PermissionsError
513 protected function submit() {
514 # Check edit token on submission
515 $token = $this->getRequest()->getVal( 'wpEditToken' );
516 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
517 $this->getOutput()->addWikiMsg( 'sessionfailure' );
521 $bitParams = $this->extractBitParams();
522 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
523 $comment = $listReason;
524 if ( $comment != 'other' && $this->otherReason
!= '' ) {
525 // Entry from drop down menu + additional comment
526 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
527 } elseif ( $comment == 'other' ) {
528 $comment = $this->otherReason
;
530 # Can the user set this field?
531 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1 && !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
532 throw new PermissionsError( 'suppressrevision' );
534 # If the save went through, go to success message...
535 $status = $this->save( $bitParams, $comment, $this->targetObj
);
536 if ( $status->isGood() ) {
541 # ...otherwise, bounce back to form...
542 $this->failure( $status );
549 * Report that the submit operation succeeded
551 protected function success() {
552 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
553 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeLabels
['success'] );
554 $this->wasSaved
= true;
555 $this->list->reloadFromMaster();
560 * Report that the submit operation failed
562 protected function failure( $status ) {
563 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
564 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
569 * Put together an array that contains -1, 0, or the *_deleted const for each bit
573 protected function extractBitParams() {
575 foreach ( $this->checks
as $item ) {
576 list( /* message */, $name, $field ) = $item;
577 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
578 if ( $val < -1 ||
$val > 1 ) {
579 $val = -1; // -1 for existing value
581 $bitfield[$field] = $val;
583 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
584 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
591 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
597 protected function save( $bitfield, $reason, $title ) {
598 return $this->getList()->setVisibility(
599 array( 'value' => $bitfield, 'comment' => $reason )
603 protected function getGroupName() {