4e1bbc2d4febfbb931c21a0e70b51bdf8ee3b9cb
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' ) );
152 # For reviewing deleted files...
153 $this->archiveName
= $request->getVal( 'file' );
154 $this->token
= $request->getVal( 'token' );
155 if ( $this->archiveName
&& $this->targetObj
) {
156 $this->tryShowFile( $this->archiveName
);
160 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
161 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
165 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
166 $output->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
169 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
170 $this->mIsAllowed
= $user->isAllowed( $this->typeInfo
['permission'] );
172 # If we have revisions, get the title from the first one
173 # since they should all be from the same page. This allows
174 # for more flexibility with page moves...
175 if( $this->typeName
== 'revision' ) {
176 $rev = Revision
::newFromId( $this->ids
[0] );
177 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
180 $this->otherReason
= $request->getVal( 'wpReason' );
181 # We need a target page!
182 if( is_null($this->targetObj
) ) {
183 $output->addWikiMsg( 'undelete-header' );
186 # Give a link to the logs/hist for this page
187 $this->showConvenienceLinks();
189 # Initialise checkboxes
190 $this->checks
= array(
191 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
192 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
193 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
195 if( $user->isAllowed('suppressrevision') ) {
196 $this->checks
[] = array( 'revdelete-hide-restricted',
197 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
200 # Either submit or create our form
201 if( $this->mIsAllowed
&& $this->submitClicked
) {
202 $this->submit( $request );
207 $qc = $this->getLogQueryCond();
208 # Show relevant lines from the deletion log
209 $deleteLogPage = new LogPage( 'delete' );
210 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
211 LogEventsList
::showLogExtract( $output, 'delete',
212 $this->targetObj
, '', array( 'lim' => 25, 'conds' => $qc ) );
213 # Show relevant lines from the suppression log
214 if( $user->isAllowed( 'suppressionlog' ) ) {
215 $suppressLogPage = new LogPage( 'suppress' );
216 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
217 LogEventsList
::showLogExtract( $output, 'suppress',
218 $this->targetObj
, '', array( 'lim' => 25, 'conds' => $qc ) );
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
) {
229 $links[] = Linker
::linkKnown(
230 SpecialPage
::getTitleFor( 'Log' ),
231 $this->msg( 'viewpagelogs' )->escaped(),
233 array( 'page' => $this->targetObj
->getPrefixedText() )
235 if ( !$this->targetObj
->isSpecialPage() ) {
236 # Give a link to the page history
237 $links[] = Linker
::linkKnown(
239 $this->msg( 'pagehist' )->escaped(),
241 array( 'action' => 'history' )
243 # Link to deleted edits
244 if( $this->getUser()->isAllowed('undelete') ) {
245 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
246 $links[] = Linker
::linkKnown(
248 $this->msg( 'deletedhist' )->escaped(),
250 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
254 # Logs themselves don't have histories or archived revisions
255 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
260 * Get the condition used for fetching log snippets
263 protected function getLogQueryCond() {
265 // Revision delete logs for these item
266 $conds['log_type'] = array( 'delete', 'suppress' );
267 $conds['log_action'] = $this->getList()->getLogAction();
268 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
269 $conds['ls_value'] = $this->ids
;
274 * Show a deleted file version requested by the visitor.
275 * TODO Mostly copied from Special:Undelete. Refactor.
277 protected function tryShowFile( $archiveName ) {
278 $repo = RepoGroup
::singleton()->getLocalRepo();
279 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
281 // Check if user is allowed to see this file
282 if ( !$oimage->exists() ) {
283 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
286 $user = $this->getUser();
287 if( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
288 if( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
289 $this->getOutput()->permissionRequired( 'suppressrevision' );
291 $this->getOutput()->permissionRequired( 'deletedtext' );
295 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
296 $lang = $this->getLanguage();
297 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
298 $this->targetObj
->getText(),
299 $lang->userDate( $oimage->getTimestamp(), $user ),
300 $lang->userTime( $oimage->getTimestamp(), $user ) );
301 $this->getOutput()->addHTML(
302 Xml
::openElement( 'form', array(
304 'action' => $this->getTitle()->getLocalUrl(
305 'target=' . urlencode( $this->targetObj
->getPrefixedDBkey() ) .
306 '&file=' . urlencode( $archiveName ) .
307 '&token=' . urlencode( $user->getEditToken( $archiveName ) ) )
310 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
315 $this->getOutput()->disable();
316 # We mustn't allow the output to be Squid cached, otherwise
317 # if an admin previews a deleted image, and it's cached, then
318 # a user without appropriate permissions can toddle off and
319 # nab the image, and Squid will serve it
320 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
321 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
322 $this->getRequest()->response()->header( 'Pragma: no-cache' );
324 $key = $oimage->getStorageKey();
325 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
326 $repo->streamFile( $path );
330 * Get the list object for this request
332 protected function getList() {
333 if ( is_null( $this->list ) ) {
334 $class = $this->typeInfo
['list-class'];
335 $this->list = new $class( $this->getContext(), $this->targetObj
, $this->ids
);
341 * Show a list of items that we will operate on, and show a form with checkboxes
342 * which will allow the user to choose new visibility settings.
344 protected function showForm() {
347 if ( $this->typeName
== 'logging' ) {
348 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLanguage()->formatNum( count($this->ids
) ) );
350 $this->getOutput()->addWikiMsg( 'revdelete-selected',
351 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
354 $this->getOutput()->addHTML( "<ul>" );
358 $list = $this->getList();
359 for ( $list->reset(); $list->current(); $list->next() ) {
360 $item = $list->current();
361 if ( !$item->canView() ) {
362 if( !$this->submitClicked
) {
363 $this->getOutput()->permissionRequired( 'suppressrevision' );
366 $UserAllowed = false;
369 $this->getOutput()->addHTML( $item->getHTML() );
372 if( !$numRevisions ) {
373 $this->getOutput()->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
377 $this->getOutput()->addHTML( "</ul>" );
379 $this->addUsageText();
381 // Normal sysops can always see what they did, but can't always change it
382 if( !$UserAllowed ) return;
384 // Show form if the user can submit
385 if( $this->mIsAllowed
) {
386 $out = Xml
::openElement( 'form', array( 'method' => 'post',
387 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
388 'id' => 'mw-revdel-form-revisions' ) ) .
389 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
390 $this->buildCheckBoxes() .
391 Xml
::openElement( 'table' ) .
393 '<td class="mw-label">' .
394 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
396 '<td class="mw-input">' .
397 Xml
::listDropDown( 'wpRevDeleteReasonList',
398 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
399 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
400 '', 'wpReasonDropDown', 1
404 '<td class="mw-label">' .
405 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
407 '<td class="mw-input">' .
408 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
412 '<td class="mw-submit">' .
413 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
414 array( 'name' => 'wpSubmit' ) ) .
417 Xml
::closeElement( 'table' ) .
418 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
419 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
420 Html
::hidden( 'type', $this->typeName
) .
421 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
422 Xml
::closeElement( 'fieldset' ) . "\n";
426 if( $this->mIsAllowed
) {
427 $out .= Xml
::closeElement( 'form' ) . "\n";
428 // Show link to edit the dropdown reasons
429 if( $this->getUser()->isAllowed( 'editinterface' ) ) {
430 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
431 $link = Linker
::link(
433 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
435 array( 'action' => 'edit' )
437 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
440 $this->getOutput()->addHTML( $out );
444 * Show some introductory text
445 * @todo FIXME: Wikimedia-specific policy text
447 protected function addUsageText() {
448 $this->getOutput()->addWikiMsg( 'revdelete-text' );
449 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
450 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
452 if( $this->mIsAllowed
) {
453 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
458 * @return String: HTML
460 protected function buildCheckBoxes() {
462 // If there is just one item, use checkboxes
463 $list = $this->getList();
464 if( $list->length() == 1 ) {
466 $bitfield = $list->current()->getBits(); // existing field
467 if( $this->submitClicked
) {
468 $bitfield = $this->extractBitfield( $this->extractBitParams(), $bitfield );
470 foreach( $this->checks
as $item ) {
471 list( $message, $name, $field ) = $item;
472 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
473 if( $field == Revision
::DELETED_RESTRICTED
)
474 $innerHTML = "<b>$innerHTML</b>";
475 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
476 $html .= "<tr>$line</tr>\n";
478 // Otherwise, use tri-state radios
481 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
482 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
483 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
484 $html .= "<th></th></tr>\n";
485 foreach( $this->checks
as $item ) {
486 list( $message, $name, $field ) = $item;
487 // If there are several items, use third state by default...
488 if( $this->submitClicked
) {
489 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
491 $selected = -1; // use existing field
493 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
494 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
495 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
496 $label = $this->msg( $message )->escaped();
497 if( $field == Revision
::DELETED_RESTRICTED
) {
498 $label = "<b>$label</b>";
500 $line .= "<td>$label</td>";
501 $html .= "<tr>$line</tr>\n";
510 * UI entry point for form submission.
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' );
520 $bitParams = $this->extractBitParams();
521 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
522 $comment = $listReason;
523 if( $comment != 'other' && $this->otherReason
!= '' ) {
524 // Entry from drop down menu + additional comment
525 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
526 } elseif( $comment == 'other' ) {
527 $comment = $this->otherReason
;
529 # Can the user set this field?
530 if( $bitParams[Revision
::DELETED_RESTRICTED
]==1 && !$this->getUser()->isAllowed('suppressrevision') ) {
531 $this->getOutput()->permissionRequired( 'suppressrevision' );
534 # If the save went through, go to success message...
535 $status = $this->save( $bitParams, $comment, $this->targetObj
);
536 if ( $status->isGood() ) {
539 # ...otherwise, bounce back to form...
541 $this->failure( $status );
547 * Report that the submit operation succeeded
549 protected function success() {
550 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
551 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo
['success'] );
552 $this->list->reloadFromMaster();
557 * Report that the submit operation failed
559 protected function failure( $status ) {
560 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
561 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
566 * Put together an array that contains -1, 0, or the *_deleted const for each bit
570 protected function extractBitParams() {
572 foreach( $this->checks
as $item ) {
573 list( /* message */ , $name, $field ) = $item;
574 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
575 if( $val < -1 ||
$val > 1) {
576 $val = -1; // -1 for existing value
578 $bitfield[$field] = $val;
580 if( !isset($bitfield[Revision
::DELETED_RESTRICTED
]) ) {
581 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
587 * Put together a rev_deleted bitfield
588 * @param $bitPars array extractBitParams() params
589 * @param $oldfield int current bitfield
592 public static function extractBitfield( $bitPars, $oldfield ) {
593 // Build the actual new rev_deleted bitfield
595 foreach( $bitPars as $const => $val ) {
597 $newBits |
= $const; // $const is the *_deleted const
598 } elseif( $val == -1 ) {
599 $newBits |
= ($oldfield & $const); // use existing
606 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
612 protected function save( $bitfield, $reason, $title ) {
613 return $this->getList()->setVisibility(
614 array( 'value' => $bitfield, 'comment' => $reason )