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 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
180 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
181 RevisionDeleter
::getRevdelConstant( $this->typeName
)
183 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
184 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
186 if ( $user->isAllowed( 'suppressrevision' ) ) {
187 $this->checks
[] = array( 'revdelete-hide-restricted',
188 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
191 # Either submit or create our form
192 if ( $this->mIsAllowed
&& $this->submitClicked
) {
193 $this->submit( $request );
198 $qc = $this->getLogQueryCond();
199 # Show relevant lines from the deletion log
200 $deleteLogPage = new LogPage( 'delete' );
201 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
202 LogEventsList
::showLogExtract(
207 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
209 # Show relevant lines from the suppression log
210 if ( $user->isAllowed( 'suppressionlog' ) ) {
211 $suppressLogPage = new LogPage( 'suppress' );
212 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
213 LogEventsList
::showLogExtract(
218 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
224 * Show some useful links in the subtitle
226 protected function showConvenienceLinks() {
227 # Give a link to the logs/hist for this page
228 if ( $this->targetObj
) {
229 // Also set header tabs to be for the target.
230 $this->getSkin()->setRelevantTitle( $this->targetObj
);
233 $links[] = Linker
::linkKnown(
234 SpecialPage
::getTitleFor( 'Log' ),
235 $this->msg( 'viewpagelogs' )->escaped(),
237 array( 'page' => $this->targetObj
->getPrefixedText() )
239 if ( !$this->targetObj
->isSpecialPage() ) {
240 # Give a link to the page history
241 $links[] = Linker
::linkKnown(
243 $this->msg( 'pagehist' )->escaped(),
245 array( 'action' => 'history' )
247 # Link to deleted edits
248 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
249 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
250 $links[] = Linker
::linkKnown(
252 $this->msg( 'deletedhist' )->escaped(),
254 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
258 # Logs themselves don't have histories or archived revisions
259 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
264 * Get the condition used for fetching log snippets
267 protected function getLogQueryCond() {
269 // Revision delete logs for these item
270 $conds['log_type'] = array( 'delete', 'suppress' );
271 $conds['log_action'] = $this->getList()->getLogAction();
272 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
273 $conds['ls_value'] = $this->ids
;
279 * Show a deleted file version requested by the visitor.
280 * TODO Mostly copied from Special:Undelete. Refactor.
282 protected function tryShowFile( $archiveName ) {
283 $repo = RepoGroup
::singleton()->getLocalRepo();
284 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
286 // Check if user is allowed to see this file
287 if ( !$oimage->exists() ) {
288 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
292 $user = $this->getUser();
293 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
294 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
295 throw new PermissionsError( 'suppressrevision' );
297 throw new PermissionsError( 'deletedtext' );
300 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
301 $lang = $this->getLanguage();
302 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
303 $this->targetObj
->getText(),
304 $lang->userDate( $oimage->getTimestamp(), $user ),
305 $lang->userTime( $oimage->getTimestamp(), $user ) );
306 $this->getOutput()->addHTML(
307 Xml
::openElement( 'form', array(
309 'action' => $this->getPageTitle()->getLocalURL( array(
310 'target' => $this->targetObj
->getPrefixedDBkey(),
311 'file' => $archiveName,
312 'token' => $user->getEditToken( $archiveName ),
316 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
322 $this->getOutput()->disable();
323 # We mustn't allow the output to be Squid cached, otherwise
324 # if an admin previews a deleted image, and it's cached, then
325 # a user without appropriate permissions can toddle off and
326 # nab the image, and Squid will serve it
327 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
328 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
329 $this->getRequest()->response()->header( 'Pragma: no-cache' );
331 $key = $oimage->getStorageKey();
332 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
333 $repo->streamFile( $path );
337 * Get the list object for this request
339 protected function getList() {
340 if ( is_null( $this->revDelList
) ) {
341 $this->revDelList
= RevisionDeleter
::createList(
342 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
346 return $this->revDelList
;
350 * Show a list of items that we will operate on, and show a form with checkboxes
351 * which will allow the user to choose new visibility settings.
353 protected function showForm() {
356 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
357 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels
['selected'],
358 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ) );
360 $this->getOutput()->addHTML( "<ul>" );
364 $list = $this->getList();
365 for ( $list->reset(); $list->current(); $list->next() ) {
366 $item = $list->current();
367 if ( !$item->canView() ) {
368 if ( !$this->submitClicked
) {
369 throw new PermissionsError( 'suppressrevision' );
371 $userAllowed = false;
374 $this->getOutput()->addHTML( $item->getHTML() );
377 if ( !$numRevisions ) {
378 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
381 $this->getOutput()->addHTML( "</ul>" );
383 $this->addUsageText();
385 // Normal sysops can always see what they did, but can't always change it
386 if ( !$userAllowed ) {
390 // Show form if the user can submit
391 if ( $this->mIsAllowed
) {
392 $out = Xml
::openElement( 'form', array( 'method' => 'post',
393 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
394 'id' => 'mw-revdel-form-revisions' ) ) .
395 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
396 $this->buildCheckBoxes() .
397 Xml
::openElement( 'table' ) .
399 '<td class="mw-label">' .
400 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
402 '<td class="mw-input">' .
403 Xml
::listDropDown( 'wpRevDeleteReasonList',
404 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
405 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
406 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
410 '<td class="mw-label">' .
411 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
413 '<td class="mw-input">' .
414 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
418 '<td class="mw-submit">' .
419 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
420 array( 'name' => 'wpSubmit' ) ) .
423 Xml
::closeElement( 'table' ) .
424 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
425 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
426 Html
::hidden( 'type', $this->typeName
) .
427 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
428 Xml
::closeElement( 'fieldset' ) . "\n";
432 if ( $this->mIsAllowed
) {
433 $out .= Xml
::closeElement( 'form' ) . "\n";
434 // Show link to edit the dropdown reasons
435 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
436 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
437 $link = Linker
::link(
439 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
441 array( 'action' => 'edit' )
443 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
446 $this->getOutput()->addHTML( $out );
450 * Show some introductory text
451 * @todo FIXME: Wikimedia-specific policy text
453 protected function addUsageText() {
454 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
455 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>\n$2", $this->typeLabels
['text'], 'revdelete-text-others' );
456 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
457 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
459 if ( $this->mIsAllowed
) {
460 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
465 * @return String: HTML
467 protected function buildCheckBoxes() {
469 // If there is just one item, use checkboxes
470 $list = $this->getList();
471 if ( $list->length() == 1 ) {
473 $bitfield = $list->current()->getBits(); // existing field
474 if ( $this->submitClicked
) {
475 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
477 foreach ( $this->checks
as $item ) {
478 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
479 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
480 list( $message, $name, $field ) = $item;
481 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
482 if ( $field == Revision
::DELETED_RESTRICTED
) {
483 $innerHTML = "<b>$innerHTML</b>";
485 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
486 $html .= "<tr>$line</tr>\n";
489 // Otherwise, use tri-state radios
491 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
492 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
493 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
494 $html .= "<th></th></tr>\n";
495 foreach ( $this->checks
as $item ) {
496 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
497 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
498 list( $message, $name, $field ) = $item;
499 // If there are several items, use third state by default...
500 if ( $this->submitClicked
) {
501 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
503 $selected = -1; // use existing field
505 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
506 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
507 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
508 $label = $this->msg( $message )->escaped();
509 if ( $field == Revision
::DELETED_RESTRICTED
) {
510 $label = "<b>$label</b>";
512 $line .= "<td>$label</td>";
513 $html .= "<tr>$line</tr>\n";
523 * UI entry point for form submission.
524 * @throws PermissionsError
527 protected function submit() {
528 # Check edit token on submission
529 $token = $this->getRequest()->getVal( 'wpEditToken' );
530 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
531 $this->getOutput()->addWikiMsg( 'sessionfailure' );
535 $bitParams = $this->extractBitParams();
536 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
537 $comment = $listReason;
538 if ( $comment != 'other' && $this->otherReason
!= '' ) {
539 // Entry from drop down menu + additional comment
540 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
541 } elseif ( $comment == 'other' ) {
542 $comment = $this->otherReason
;
544 # Can the user set this field?
545 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1 && !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
546 throw new PermissionsError( 'suppressrevision' );
548 # If the save went through, go to success message...
549 $status = $this->save( $bitParams, $comment, $this->targetObj
);
550 if ( $status->isGood() ) {
555 # ...otherwise, bounce back to form...
556 $this->failure( $status );
563 * Report that the submit operation succeeded
565 protected function success() {
566 // Messages: revdelete-success, logdelete-success
567 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
568 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeLabels
['success'] );
569 $this->wasSaved
= true;
570 $this->revDelList
->reloadFromMaster();
575 * Report that the submit operation failed
577 protected function failure( $status ) {
578 // Messages: revdelete-failure, logdelete-failure
579 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
580 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
585 * Put together an array that contains -1, 0, or the *_deleted const for each bit
589 protected function extractBitParams() {
591 foreach ( $this->checks
as $item ) {
592 list( /* message */, $name, $field ) = $item;
593 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
594 if ( $val < -1 ||
$val > 1 ) {
595 $val = -1; // -1 for existing value
597 $bitfield[$field] = $val;
599 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
600 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
607 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
613 protected function save( $bitfield, $reason, $title ) {
614 return $this->getList()->setVisibility(
615 array( 'value' => $bitfield, 'comment' => $reason )
619 protected function getGroupName() {