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',
69 'permission' => 'deleterevision',
72 'check-label' => 'revdelete-hide-text',
73 'deletion-bits' => Revision
::DELETED_TEXT
,
74 'success' => 'revdelete-success',
75 'failure' => 'revdelete-failure',
76 'list-class' => 'RevDel_ArchiveList',
77 'permission' => 'deleterevision',
80 'check-label' => 'revdelete-hide-image',
81 'deletion-bits' => File
::DELETED_FILE
,
82 'success' => 'revdelete-success',
83 'failure' => 'revdelete-failure',
84 'list-class' => 'RevDel_FileList',
85 'permission' => 'deleterevision',
87 'filearchive' => array(
88 'check-label' => 'revdelete-hide-image',
89 'deletion-bits' => File
::DELETED_FILE
,
90 'success' => 'revdelete-success',
91 'failure' => 'revdelete-failure',
92 'list-class' => 'RevDel_ArchivedFileList',
93 'permission' => 'deleterevision',
96 'check-label' => 'revdelete-hide-name',
97 'deletion-bits' => LogPage
::DELETED_ACTION
,
98 'success' => 'logdelete-success',
99 'failure' => 'logdelete-failure',
100 'list-class' => 'RevDel_LogList',
101 'permission' => 'deletelogentry',
105 /** Type map to support old log entries */
106 static $deprecatedTypeMap = array(
107 'oldid' => 'revision',
108 'artimestamp' => 'archive',
109 'oldimage' => 'oldimage',
110 'fileid' => 'filearchive',
111 'logid' => 'logging',
114 public function __construct() {
115 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
118 public function execute( $par ) {
119 $this->checkPermissions();
120 $this->checkReadOnly();
122 $output = $this->getOutput();
123 $user = $this->getUser();
126 $this->outputHeader();
127 $request = $this->getRequest();
128 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
129 # Handle our many different possible input types.
130 $ids = $request->getVal( 'ids' );
131 if ( !is_null( $ids ) ) {
132 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
133 $this->ids
= explode( ',', $ids );
136 $this->ids
= array_keys( $request->getArray( 'ids', array() ) );
138 // $this->ids = array_map( 'intval', $this->ids );
139 $this->ids
= array_unique( array_filter( $this->ids
) );
141 if ( $request->getVal( 'action' ) == 'historysubmit' ||
$request->getVal( 'action' ) == 'revisiondelete' ) {
142 // For show/hide form submission from history page
143 // Since we are access through index.php?title=XXX&action=historysubmit
144 // getFullTitle() will contain the target title and not our title
145 $this->targetObj
= $this->getFullTitle();
146 $this->typeName
= 'revision';
148 $this->typeName
= $request->getVal( 'type' );
149 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
150 if ( $this->targetObj
&& $this->targetObj
->isSpecial( 'Log' ) && count( $this->ids
) !== 0 ) {
151 $result = wfGetDB( DB_SLAVE
)->select( 'logging',
153 array( 'log_id' => $this->ids
),
158 if ( $result->numRows() == 1 ) {
159 // If there's only one type, the target can be set to include it.
160 $this->targetObj
= SpecialPage
::getTitleFor( 'Log', $result->current()->log_type
);
165 # For reviewing deleted files...
166 $this->archiveName
= $request->getVal( 'file' );
167 $this->token
= $request->getVal( 'token' );
168 if ( $this->archiveName
&& $this->targetObj
) {
169 $this->tryShowFile( $this->archiveName
);
173 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
174 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
178 if ( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
179 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
181 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
182 $this->mIsAllowed
= $user->isAllowed( $this->typeInfo
['permission'] );
184 # If we have revisions, get the title from the first one
185 # since they should all be from the same page. This allows
186 # for more flexibility with page moves...
187 if ( $this->typeName
== 'revision' ) {
188 $rev = Revision
::newFromId( $this->ids
[0] );
189 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
192 $this->otherReason
= $request->getVal( 'wpReason' );
193 # We need a target page!
194 if ( is_null( $this->targetObj
) ) {
195 $output->addWikiMsg( 'undelete-header' );
198 # Give a link to the logs/hist for this page
199 $this->showConvenienceLinks();
201 # Initialise checkboxes
202 $this->checks
= array(
203 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
204 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
205 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
207 if ( $user->isAllowed( 'suppressrevision' ) ) {
208 $this->checks
[] = array( 'revdelete-hide-restricted',
209 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
212 # Either submit or create our form
213 if ( $this->mIsAllowed
&& $this->submitClicked
) {
214 $this->submit( $request );
219 $qc = $this->getLogQueryCond();
220 # Show relevant lines from the deletion log
221 $deleteLogPage = new LogPage( 'delete' );
222 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
223 LogEventsList
::showLogExtract( $output, 'delete',
224 $this->targetObj
, '', array( 'lim' => 25, 'conds' => $qc ) );
225 # Show relevant lines from the suppression log
226 if ( $user->isAllowed( 'suppressionlog' ) ) {
227 $suppressLogPage = new LogPage( 'suppress' );
228 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
229 LogEventsList
::showLogExtract( $output, 'suppress',
230 $this->targetObj
, '', array( 'lim' => 25, 'conds' => $qc ) );
235 * Show some useful links in the subtitle
237 protected function showConvenienceLinks() {
238 # Give a link to the logs/hist for this page
239 if ( $this->targetObj
) {
241 $links[] = Linker
::linkKnown(
242 SpecialPage
::getTitleFor( 'Log' ),
243 $this->msg( 'viewpagelogs' )->escaped(),
245 array( 'page' => $this->targetObj
->getPrefixedText() )
247 if ( !$this->targetObj
->isSpecialPage() ) {
248 # Give a link to the page history
249 $links[] = Linker
::linkKnown(
251 $this->msg( 'pagehist' )->escaped(),
253 array( 'action' => 'history' )
255 # Link to deleted edits
256 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
257 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
258 $links[] = Linker
::linkKnown(
260 $this->msg( 'deletedhist' )->escaped(),
262 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
266 # Logs themselves don't have histories or archived revisions
267 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
272 * Get the condition used for fetching log snippets
275 protected function getLogQueryCond() {
277 // Revision delete logs for these item
278 $conds['log_type'] = array( 'delete', 'suppress' );
279 $conds['log_action'] = $this->getList()->getLogAction();
280 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
281 $conds['ls_value'] = $this->ids
;
286 * Show a deleted file version requested by the visitor.
287 * TODO Mostly copied from Special:Undelete. Refactor.
289 protected function tryShowFile( $archiveName ) {
290 $repo = RepoGroup
::singleton()->getLocalRepo();
291 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
293 // Check if user is allowed to see this file
294 if ( !$oimage->exists() ) {
295 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
298 $user = $this->getUser();
299 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
300 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
301 throw new PermissionsError( 'suppressrevision' );
303 throw new PermissionsError( 'deletedtext' );
306 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
307 $lang = $this->getLanguage();
308 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
309 $this->targetObj
->getText(),
310 $lang->userDate( $oimage->getTimestamp(), $user ),
311 $lang->userTime( $oimage->getTimestamp(), $user ) );
312 $this->getOutput()->addHTML(
313 Xml
::openElement( 'form', array(
315 'action' => $this->getTitle()->getLocalURL( array(
316 'target' => $this->targetObj
->getPrefixedDBkey(),
317 'file' => $archiveName,
318 'token' => $user->getEditToken( $archiveName ),
322 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
327 $this->getOutput()->disable();
328 # We mustn't allow the output to be Squid cached, otherwise
329 # if an admin previews a deleted image, and it's cached, then
330 # a user without appropriate permissions can toddle off and
331 # nab the image, and Squid will serve it
332 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
333 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
334 $this->getRequest()->response()->header( 'Pragma: no-cache' );
336 $key = $oimage->getStorageKey();
337 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
338 $repo->streamFile( $path );
342 * Get the list object for this request
344 protected function getList() {
345 if ( is_null( $this->list ) ) {
346 $class = $this->typeInfo
['list-class'];
347 $this->list = new $class( $this->getContext(), $this->targetObj
, $this->ids
);
353 * Show a list of items that we will operate on, and show a form with checkboxes
354 * which will allow the user to choose new visibility settings.
356 protected function showForm() {
359 if ( $this->typeName
== 'logging' ) {
360 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLanguage()->formatNum( count( $this->ids
) ) );
362 $this->getOutput()->addWikiMsg( 'revdelete-selected',
363 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
366 $this->getOutput()->addHTML( "<ul>" );
370 $list = $this->getList();
371 for ( $list->reset(); $list->current(); $list->next() ) {
372 $item = $list->current();
373 if ( !$item->canView() ) {
374 if ( !$this->submitClicked
) {
375 throw new PermissionsError( 'suppressrevision' );
377 $UserAllowed = false;
380 $this->getOutput()->addHTML( $item->getHTML() );
383 if ( !$numRevisions ) {
384 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
387 $this->getOutput()->addHTML( "</ul>" );
389 $this->addUsageText();
391 // Normal sysops can always see what they did, but can't always change it
392 if ( !$UserAllowed ) {
396 // Show form if the user can submit
397 if ( $this->mIsAllowed
) {
398 $out = Xml
::openElement( 'form', array( 'method' => 'post',
399 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ),
400 'id' => 'mw-revdel-form-revisions' ) ) .
401 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
402 $this->buildCheckBoxes() .
403 Xml
::openElement( 'table' ) .
405 '<td class="mw-label">' .
406 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
408 '<td class="mw-input">' .
409 Xml
::listDropDown( 'wpRevDeleteReasonList',
410 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
411 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
412 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
416 '<td class="mw-label">' .
417 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
419 '<td class="mw-input">' .
420 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
424 '<td class="mw-submit">' .
425 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
426 array( 'name' => 'wpSubmit' ) ) .
429 Xml
::closeElement( 'table' ) .
430 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
431 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
432 Html
::hidden( 'type', $this->typeName
) .
433 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
434 Xml
::closeElement( 'fieldset' ) . "\n";
438 if ( $this->mIsAllowed
) {
439 $out .= Xml
::closeElement( 'form' ) . "\n";
440 // Show link to edit the dropdown reasons
441 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
442 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
443 $link = Linker
::link(
445 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
447 array( 'action' => 'edit' )
449 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
452 $this->getOutput()->addHTML( $out );
456 * Show some introductory text
457 * @todo FIXME: Wikimedia-specific policy text
459 protected function addUsageText() {
460 $this->getOutput()->addWikiMsg( 'revdelete-text' );
461 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
462 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
464 if ( $this->mIsAllowed
) {
465 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
470 * @return String: HTML
472 protected function buildCheckBoxes() {
474 // If there is just one item, use checkboxes
475 $list = $this->getList();
476 if ( $list->length() == 1 ) {
478 $bitfield = $list->current()->getBits(); // existing field
479 if ( $this->submitClicked
) {
480 $bitfield = $this->extractBitfield( $this->extractBitParams(), $bitfield );
482 foreach ( $this->checks
as $item ) {
483 list( $message, $name, $field ) = $item;
484 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
485 if ( $field == Revision
::DELETED_RESTRICTED
) {
486 $innerHTML = "<b>$innerHTML</b>";
488 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
489 $html .= "<tr>$line</tr>\n";
491 // Otherwise, use tri-state radios
494 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
495 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
496 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
497 $html .= "<th></th></tr>\n";
498 foreach ( $this->checks
as $item ) {
499 list( $message, $name, $field ) = $item;
500 // If there are several items, use third state by default...
501 if ( $this->submitClicked
) {
502 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
504 $selected = -1; // use existing field
506 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
507 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
508 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
509 $label = $this->msg( $message )->escaped();
510 if ( $field == Revision
::DELETED_RESTRICTED
) {
511 $label = "<b>$label</b>";
513 $line .= "<td>$label</td>";
514 $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' );
534 $bitParams = $this->extractBitParams();
535 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
536 $comment = $listReason;
537 if ( $comment != 'other' && $this->otherReason
!= '' ) {
538 // Entry from drop down menu + additional comment
539 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
540 } elseif ( $comment == 'other' ) {
541 $comment = $this->otherReason
;
543 # Can the user set this field?
544 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1 && !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
545 throw new PermissionsError( 'suppressrevision' );
547 # If the save went through, go to success message...
548 $status = $this->save( $bitParams, $comment, $this->targetObj
);
549 if ( $status->isGood() ) {
552 # ...otherwise, bounce back to form...
554 $this->failure( $status );
560 * Report that the submit operation succeeded
562 protected function success() {
563 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
564 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo
['success'] );
565 $this->list->reloadFromMaster();
570 * Report that the submit operation failed
572 protected function failure( $status ) {
573 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
574 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
579 * Put together an array that contains -1, 0, or the *_deleted const for each bit
583 protected function extractBitParams() {
585 foreach ( $this->checks
as $item ) {
586 list( /* message */, $name, $field ) = $item;
587 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
588 if ( $val < -1 ||
$val > 1 ) {
589 $val = -1; // -1 for existing value
591 $bitfield[$field] = $val;
593 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
594 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
600 * Put together a rev_deleted bitfield
601 * @param array $bitPars extractBitParams() params
602 * @param int $oldfield current bitfield
605 public static function extractBitfield( $bitPars, $oldfield ) {
606 // Build the actual new rev_deleted bitfield
608 foreach ( $bitPars as $const => $val ) {
610 $newBits |
= $const; // $const is the *_deleted const
611 } elseif ( $val == -1 ) {
612 $newBits |
= ( $oldfield & $const ); // use existing
619 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
625 protected function save( $bitfield, $reason, $title ) {
626 return $this->getList()->setVisibility(
627 array( 'value' => $bitfield, 'comment' => $reason )
631 protected function getGroupName() {