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
{
34 /** True if the submit button was clicked, and the form was posted */
40 /** Archive name, for reviewing deleted files */
43 /** Edit token for securing image views against XSS */
46 /** Title object for target parameter */
49 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
52 /** Array of checkbox specs (message, name, deletion bits) */
55 /** Information about the current type */
58 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
62 * Assorted information about each type, needed by the special page.
63 * TODO Move some of this to the list class
65 static $allowedTypes = array(
67 'check-label' => 'revdelete-hide-text',
68 'deletion-bits' => Revision
::DELETED_TEXT
,
69 'success' => 'revdelete-success',
70 'failure' => 'revdelete-failure',
71 'list-class' => 'RevDel_RevisionList',
74 'check-label' => 'revdelete-hide-text',
75 'deletion-bits' => Revision
::DELETED_TEXT
,
76 'success' => 'revdelete-success',
77 'failure' => 'revdelete-failure',
78 'list-class' => 'RevDel_ArchiveList',
81 'check-label' => 'revdelete-hide-image',
82 'deletion-bits' => File
::DELETED_FILE
,
83 'success' => 'revdelete-success',
84 'failure' => 'revdelete-failure',
85 'list-class' => 'RevDel_FileList',
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',
95 'check-label' => 'revdelete-hide-name',
96 'deletion-bits' => LogPage
::DELETED_ACTION
,
97 'success' => 'logdelete-success',
98 'failure' => 'logdelete-failure',
99 'list-class' => 'RevDel_LogList',
103 /** Type map to support old log entries */
104 static $deprecatedTypeMap = array(
105 'oldid' => 'revision',
106 'artimestamp' => 'archive',
107 'oldimage' => 'oldimage',
108 'fileid' => 'filearchive',
109 'logid' => 'logging',
112 public function __construct() {
113 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
116 public function execute( $par ) {
117 global $wgOut, $wgUser, $wgRequest;
118 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
119 $wgOut->permissionRequired( 'deletedhistory' );
121 } else if( wfReadOnly() ) {
122 $wgOut->readOnlyPage();
125 $this->mIsAllowed
= $wgUser->isAllowed('deleterevision'); // for changes
126 $this->skin
= $wgUser->getSkin();
128 $this->outputHeader();
129 $this->submitClicked
= $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
130 # Handle our many different possible input types.
131 $ids = $wgRequest->getVal( 'ids' );
132 if ( !is_null( $ids ) ) {
133 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
134 $this->ids
= explode( ',', $ids );
137 $this->ids
= array_keys( $wgRequest->getArray('ids',array()) );
139 // $this->ids = array_map( 'intval', $this->ids );
140 $this->ids
= array_unique( array_filter( $this->ids
) );
142 if ( $wgRequest->getVal( 'action' ) == 'historysubmit' ) {
143 # For show/hide form submission from history page
144 $this->targetObj
= $GLOBALS['wgTitle'];
145 $this->typeName
= 'revision';
147 $this->typeName
= $wgRequest->getVal( 'type' );
148 $this->targetObj
= Title
::newFromText( $wgRequest->getText( 'target' ) );
151 # For reviewing deleted files...
152 $this->archiveName
= $wgRequest->getVal( 'file' );
153 $this->token
= $wgRequest->getVal( 'token' );
154 if ( $this->archiveName
&& $this->targetObj
) {
155 $this->tryShowFile( $this->archiveName
);
159 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
160 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
164 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
165 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
168 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
170 # If we have revisions, get the title from the first one
171 # since they should all be from the same page. This allows
172 # for more flexibility with page moves...
173 if( $this->typeName
== 'revision' ) {
174 $rev = Revision
::newFromId( $this->ids
[0] );
175 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
178 $this->otherReason
= $wgRequest->getVal( 'wpReason' );
179 # We need a target page!
180 if( is_null($this->targetObj
) ) {
181 $wgOut->addWikiMsg( 'undelete-header' );
184 # Give a link to the logs/hist for this page
185 $this->showConvenienceLinks();
187 # Initialise checkboxes
188 $this->checks
= array(
189 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
190 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
191 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
193 if( $wgUser->isAllowed('suppressrevision') ) {
194 $this->checks
[] = array( 'revdelete-hide-restricted',
195 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
198 # Either submit or create our form
199 if( $this->mIsAllowed
&& $this->submitClicked
) {
200 $this->submit( $wgRequest );
205 $qc = $this->getLogQueryCond();
206 # Show relevant lines from the deletion log
207 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
208 LogEventsList
::showLogExtract( $wgOut, 'delete',
209 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
210 # Show relevant lines from the suppression log
211 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
212 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
213 LogEventsList
::showLogExtract( $wgOut, 'suppress',
214 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
219 * Show some useful links in the subtitle
221 protected function showConvenienceLinks() {
222 global $wgOut, $wgUser, $wgLang;
223 # Give a link to the logs/hist for this page
224 if( $this->targetObj
) {
226 $links[] = $this->skin
->linkKnown(
227 SpecialPage
::getTitleFor( 'Log' ),
228 wfMsgHtml( 'viewpagelogs' ),
230 array( 'page' => $this->targetObj
->getPrefixedText() )
232 if ( $this->targetObj
->getNamespace() != NS_SPECIAL
) {
233 # Give a link to the page history
234 $links[] = $this->skin
->linkKnown(
236 wfMsgHtml( 'pagehist' ),
238 array( 'action' => 'history' )
240 # Link to deleted edits
241 if( $wgUser->isAllowed('undelete') ) {
242 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
243 $links[] = $this->skin
->linkKnown(
245 wfMsgHtml( 'deletedhist' ),
247 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
251 # Logs themselves don't have histories or archived revisions
252 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
257 * Get the condition used for fetching log snippets
259 protected function getLogQueryCond() {
261 // Revision delete logs for these item
262 $conds['log_type'] = array('delete','suppress');
263 $conds['log_action'] = $this->getList()->getLogAction();
264 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
265 $conds['ls_value'] = $this->ids
;
270 * Show a deleted file version requested by the visitor.
271 * TODO Mostly copied from Special:Undelete. Refactor.
273 protected function tryShowFile( $archiveName ) {
274 global $wgOut, $wgRequest, $wgUser, $wgLang;
276 $repo = RepoGroup
::singleton()->getLocalRepo();
277 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
279 // Check if user is allowed to see this file
280 if ( !$oimage->exists() ) {
281 $wgOut->addWikiMsg( 'revdelete-no-file' );
284 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
285 if( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
286 $wgOut->permissionRequired( 'suppressrevision' );
288 $wgOut->permissionRequired( 'deletedtext' );
292 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
293 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
294 $this->targetObj
->getText(),
295 $wgLang->date( $oimage->getTimestamp() ),
296 $wgLang->time( $oimage->getTimestamp() ) );
298 Xml
::openElement( 'form', array(
300 'action' => $this->getTitle()->getLocalUrl(
301 'target=' . urlencode( $oimage->getName() ) .
302 '&file=' . urlencode( $archiveName ) .
303 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
306 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
312 # We mustn't allow the output to be Squid cached, otherwise
313 # if an admin previews a deleted image, and it's cached, then
314 # a user without appropriate permissions can toddle off and
315 # nab the image, and Squid will serve it
316 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
317 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
318 $wgRequest->response()->header( 'Pragma: no-cache' );
320 # Stream the file to the client
322 require_once( "$IP/includes/StreamFile.php" );
323 $key = $oimage->getStorageKey();
324 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
325 wfStreamFile( $path );
329 * Get the list object for this request
331 protected function getList() {
332 if ( is_null( $this->list ) ) {
333 $class = $this->typeInfo
['list-class'];
334 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
340 * Show a list of items that we will operate on, and show a form with checkboxes
341 * which will allow the user to choose new visibility settings.
343 protected function showForm() {
344 global $wgOut, $wgUser, $wgLang;
347 if ( $this->typeName
== 'logging' ) {
348 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
350 $wgOut->addWikiMsg( 'revdelete-selected',
351 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
354 $wgOut->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 $wgOut->permissionRequired( 'suppressrevision' );
366 $UserAllowed = false;
369 $wgOut->addHTML( $item->getHTML() );
372 if( !$numRevisions ) {
373 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
377 $wgOut->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( wfMsg( 'revdelete-legend' ) ) .
390 $this->buildCheckBoxes() .
391 Xml
::openElement( 'table' ) .
393 '<td class="mw-label">' .
394 Xml
::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
396 '<td class="mw-input">' .
397 Xml
::listDropDown( 'wpRevDeleteReasonList',
398 wfMsgForContent( 'revdelete-reason-dropdown' ),
399 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
403 '<td class="mw-label">' .
404 Xml
::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
406 '<td class="mw-input">' .
407 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
411 '<td class="mw-submit">' .
412 Xml
::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
413 array( 'name' => 'wpSubmit' ) ) .
416 Xml
::closeElement( 'table' ) .
417 Html
::hidden( 'wpEditToken', $wgUser->editToken() ) .
418 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
419 Html
::hidden( 'type', $this->typeName
) .
420 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
421 Xml
::closeElement( 'fieldset' ) . "\n";
425 if( $this->mIsAllowed
) {
426 $out .= Xml
::closeElement( 'form' ) . "\n";
427 // Show link to edit the dropdown reasons
428 if( $wgUser->isAllowed( 'editinterface' ) ) {
429 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'revdelete-reason-dropdown' );
430 $link = $wgUser->getSkin()->link(
432 wfMsgHtml( 'revdelete-edit-reasonlist' ),
434 array( 'action' => 'edit' )
436 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
439 $wgOut->addHTML( $out );
443 * Show some introductory text
444 * @todo FIXME: Wikimedia-specific policy text
446 protected function addUsageText() {
447 global $wgOut, $wgUser;
448 $wgOut->addWikiMsg( 'revdelete-text' );
449 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
450 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
452 if( $this->mIsAllowed
) {
453 $wgOut->addWikiMsg( 'revdelete-confirm' );
458 * @return String: HTML
460 protected function buildCheckBoxes() {
464 // If there is just one item, use checkboxes
465 $list = $this->getList();
466 if( $list->length() == 1 ) {
468 $bitfield = $list->current()->getBits(); // existing field
469 if( $this->submitClicked
) {
470 $bitfield = $this->extractBitfield( $this->extractBitParams($wgRequest), $bitfield );
472 foreach( $this->checks
as $item ) {
473 list( $message, $name, $field ) = $item;
474 $innerHTML = Xml
::checkLabel( wfMsg($message), $name, $name, $bitfield & $field );
475 if( $field == Revision
::DELETED_RESTRICTED
)
476 $innerHTML = "<b>$innerHTML</b>";
477 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
478 $html .= "<tr>$line</tr>\n";
480 // Otherwise, use tri-state radios
483 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-same').'</th>';
484 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
485 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</th>';
486 $html .= "<th></th></tr>\n";
487 foreach( $this->checks
as $item ) {
488 list( $message, $name, $field ) = $item;
489 // If there are several items, use third state by default...
490 if( $this->submitClicked
) {
491 $selected = $wgRequest->getInt( $name, 0 /* unchecked */ );
493 $selected = -1; // use existing field
495 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
496 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
497 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
498 $label = wfMsgHtml($message);
499 if( $field == Revision
::DELETED_RESTRICTED
) {
500 $label = "<b>$label</b>";
502 $line .= "<td>$label</td>";
503 $html .= "<tr>$line</tr>\n";
512 * UI entry point for form submission.
513 * @param $request WebRequest
515 protected function submit( $request ) {
516 global $wgUser, $wgOut;
517 # Check edit token on submission
518 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
519 $wgOut->addWikiMsg( 'sessionfailure' );
522 $bitParams = $this->extractBitParams( $request );
523 $listReason = $request->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
524 $comment = $listReason;
525 if( $comment != 'other' && $this->otherReason
!= '' ) {
526 // Entry from drop down menu + additional comment
527 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason
;
528 } elseif( $comment == 'other' ) {
529 $comment = $this->otherReason
;
531 # Can the user set this field?
532 if( $bitParams[Revision
::DELETED_RESTRICTED
]==1 && !$wgUser->isAllowed('suppressrevision') ) {
533 $wgOut->permissionRequired( 'suppressrevision' );
536 # If the save went through, go to success message...
537 $status = $this->save( $bitParams, $comment, $this->targetObj
);
538 if ( $status->isGood() ) {
541 # ...otherwise, bounce back to form...
543 $this->failure( $status );
549 * Report that the submit operation succeeded
551 protected function success() {
553 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
554 $wgOut->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo
['success'] );
555 $this->list->reloadFromMaster();
560 * Report that the submit operation failed
562 protected function failure( $status ) {
564 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
565 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
570 * Put together an array that contains -1, 0, or the *_deleted const for each bit
571 * @param $request WebRequest
574 protected function extractBitParams( $request ) {
576 foreach( $this->checks
as $item ) {
577 list( /* message */ , $name, $field ) = $item;
578 $val = $request->getInt( $name, 0 /* unchecked */ );
579 if( $val < -1 ||
$val > 1) {
580 $val = -1; // -1 for existing value
582 $bitfield[$field] = $val;
584 if( !isset($bitfield[Revision
::DELETED_RESTRICTED
]) ) {
585 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
591 * Put together a rev_deleted bitfield
592 * @param $bitPars array extractBitParams() params
593 * @param $oldfield int current bitfield
596 public static function extractBitfield( $bitPars, $oldfield ) {
597 // Build the actual new rev_deleted bitfield
599 foreach( $bitPars as $const => $val ) {
601 $newBits |
= $const; // $const is the *_deleted const
602 } else if( $val == -1 ) {
603 $newBits |
= ($oldfield & $const); // use existing
610 * 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 )