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
);
143 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
146 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
147 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
149 $this->typeLabels
= self
::$UILabels[$this->typeName
];
150 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
152 # Allow the list type to adjust the passed target
153 $this->targetObj
= RevisionDeleter
::suggestTarget( $this->typeName
, $this->targetObj
, $this->ids
);
155 $this->otherReason
= $request->getVal( 'wpReason' );
156 # We need a target page!
157 if ( is_null( $this->targetObj
) ) {
158 $output->addWikiMsg( 'undelete-header' );
161 # Give a link to the logs/hist for this page
162 $this->showConvenienceLinks();
164 # Initialise checkboxes
165 $this->checks
= array(
166 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
167 RevisionDeleter
::getRevdelConstant( $this->typeName
)
169 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
170 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
172 if ( $user->isAllowed( 'suppressrevision' ) ) {
173 $this->checks
[] = array( 'revdelete-hide-restricted',
174 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
177 # Either submit or create our form
178 if ( $this->mIsAllowed
&& $this->submitClicked
) {
179 $this->submit( $request );
184 $qc = $this->getLogQueryCond();
185 # Show relevant lines from the deletion log
186 $deleteLogPage = new LogPage( 'delete' );
187 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
188 LogEventsList
::showLogExtract(
193 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
195 # Show relevant lines from the suppression log
196 if ( $user->isAllowed( 'suppressionlog' ) ) {
197 $suppressLogPage = new LogPage( 'suppress' );
198 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
199 LogEventsList
::showLogExtract(
204 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
210 * Show some useful links in the subtitle
212 protected function showConvenienceLinks() {
213 # Give a link to the logs/hist for this page
214 if ( $this->targetObj
) {
215 // Also set header tabs to be for the target.
216 $this->getSkin()->setRelevantTitle( $this->targetObj
);
219 $links[] = Linker
::linkKnown(
220 SpecialPage
::getTitleFor( 'Log' ),
221 $this->msg( 'viewpagelogs' )->escaped(),
223 array( 'page' => $this->targetObj
->getPrefixedText() )
225 if ( !$this->targetObj
->isSpecialPage() ) {
226 # Give a link to the page history
227 $links[] = Linker
::linkKnown(
229 $this->msg( 'pagehist' )->escaped(),
231 array( 'action' => 'history' )
233 # Link to deleted edits
234 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
235 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
236 $links[] = Linker
::linkKnown(
238 $this->msg( 'deletedhist' )->escaped(),
240 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
244 # Logs themselves don't have histories or archived revisions
245 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
250 * Get the condition used for fetching log snippets
253 protected function getLogQueryCond() {
255 // Revision delete logs for these item
256 $conds['log_type'] = array( 'delete', 'suppress' );
257 $conds['log_action'] = $this->getList()->getLogAction();
258 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
259 $conds['ls_value'] = $this->ids
;
264 * Show a deleted file version requested by the visitor.
265 * TODO Mostly copied from Special:Undelete. Refactor.
267 protected function tryShowFile( $archiveName ) {
268 $repo = RepoGroup
::singleton()->getLocalRepo();
269 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
271 // Check if user is allowed to see this file
272 if ( !$oimage->exists() ) {
273 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
276 $user = $this->getUser();
277 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
278 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
279 throw new PermissionsError( 'suppressrevision' );
281 throw new PermissionsError( 'deletedtext' );
284 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
285 $lang = $this->getLanguage();
286 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
287 $this->targetObj
->getText(),
288 $lang->userDate( $oimage->getTimestamp(), $user ),
289 $lang->userTime( $oimage->getTimestamp(), $user ) );
290 $this->getOutput()->addHTML(
291 Xml
::openElement( 'form', array(
293 'action' => $this->getPageTitle()->getLocalURL( array(
294 'target' => $this->targetObj
->getPrefixedDBkey(),
295 'file' => $archiveName,
296 'token' => $user->getEditToken( $archiveName ),
300 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
305 $this->getOutput()->disable();
306 # We mustn't allow the output to be Squid cached, otherwise
307 # if an admin previews a deleted image, and it's cached, then
308 # a user without appropriate permissions can toddle off and
309 # nab the image, and Squid will serve it
310 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
311 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
312 $this->getRequest()->response()->header( 'Pragma: no-cache' );
314 $key = $oimage->getStorageKey();
315 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
316 $repo->streamFile( $path );
320 * Get the list object for this request
322 protected function getList() {
323 if ( is_null( $this->list ) ) {
324 $this->list = RevisionDeleter
::createList(
325 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
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() {
338 if ( $this->typeName
== 'logging' ) {
339 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLanguage()->formatNum( count( $this->ids
) ) );
341 $this->getOutput()->addWikiMsg( 'revdelete-selected',
342 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
345 $this->getOutput()->addHTML( "<ul>" );
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 throw new PermissionsError( 'suppressrevision' );
356 $UserAllowed = false;
359 $this->getOutput()->addHTML( $item->getHTML() );
362 if ( !$numRevisions ) {
363 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
366 $this->getOutput()->addHTML( "</ul>" );
368 $this->addUsageText();
370 // Normal sysops can always see what they did, but can't always change it
371 if ( !$UserAllowed ) {
375 // Show form if the user can submit
376 if ( $this->mIsAllowed
) {
377 $out = Xml
::openElement( 'form', array( 'method' => 'post',
378 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
379 'id' => 'mw-revdel-form-revisions' ) ) .
380 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
381 $this->buildCheckBoxes() .
382 Xml
::openElement( 'table' ) .
384 '<td class="mw-label">' .
385 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
387 '<td class="mw-input">' .
388 Xml
::listDropDown( 'wpRevDeleteReasonList',
389 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
390 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
391 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
395 '<td class="mw-label">' .
396 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
398 '<td class="mw-input">' .
399 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
403 '<td class="mw-submit">' .
404 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
405 array( 'name' => 'wpSubmit' ) ) .
408 Xml
::closeElement( 'table' ) .
409 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
410 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
411 Html
::hidden( 'type', $this->typeName
) .
412 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
413 Xml
::closeElement( 'fieldset' ) . "\n";
417 if ( $this->mIsAllowed
) {
418 $out .= Xml
::closeElement( 'form' ) . "\n";
419 // Show link to edit the dropdown reasons
420 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
421 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
422 $link = Linker
::link(
424 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
426 array( 'action' => 'edit' )
428 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
431 $this->getOutput()->addHTML( $out );
435 * Show some introductory text
436 * @todo FIXME: Wikimedia-specific policy text
438 protected function addUsageText() {
439 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>\n$2", $this->typeLabels
['text'], 'revdelete-text-others' );
440 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
441 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
443 if ( $this->mIsAllowed
) {
444 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
449 * @return String: HTML
451 protected function buildCheckBoxes() {
453 // If there is just one item, use checkboxes
454 $list = $this->getList();
455 if ( $list->length() == 1 ) {
457 $bitfield = $list->current()->getBits(); // existing field
458 if ( $this->submitClicked
) {
459 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
461 foreach ( $this->checks
as $item ) {
462 list( $message, $name, $field ) = $item;
463 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
464 if ( $field == Revision
::DELETED_RESTRICTED
) {
465 $innerHTML = "<b>$innerHTML</b>";
467 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
468 $html .= "<tr>$line</tr>\n";
470 // Otherwise, use tri-state radios
473 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
474 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
475 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
476 $html .= "<th></th></tr>\n";
477 foreach ( $this->checks
as $item ) {
478 list( $message, $name, $field ) = $item;
479 // If there are several items, use third state by default...
480 if ( $this->submitClicked
) {
481 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
483 $selected = -1; // use existing field
485 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
486 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
487 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
488 $label = $this->msg( $message )->escaped();
489 if ( $field == Revision
::DELETED_RESTRICTED
) {
490 $label = "<b>$label</b>";
492 $line .= "<td>$label</td>";
493 $html .= "<tr>$line</tr>\n";
502 * UI entry point for form submission.
503 * @throws PermissionsError
506 protected function submit() {
507 # Check edit token on submission
508 $token = $this->getRequest()->getVal( 'wpEditToken' );
509 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
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 .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $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 throw new PermissionsError( 'suppressrevision' );
526 # If the save went through, go to success message...
527 $status = $this->save( $bitParams, $comment, $this->targetObj
);
528 if ( $status->isGood() ) {
531 # ...otherwise, bounce back to form...
533 $this->failure( $status );
539 * Report that the submit operation succeeded
541 protected function success() {
542 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
543 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeLabels
['success'] );
544 $this->wasSaved
= true;
545 $this->list->reloadFromMaster();
550 * Report that the submit operation failed
552 protected function failure( $status ) {
553 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
554 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['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 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
586 protected function save( $bitfield, $reason, $title ) {
587 return $this->getList()->setVisibility(
588 array( 'value' => $bitfield, 'comment' => $reason )
592 protected function getGroupName() {