4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 * Special page allowing users with the appropriate permissions to view
22 * and hide revisions. Log items can also be hidden.
25 * @ingroup SpecialPage
28 class SpecialRevisionDelete
extends UnlistedSpecialPage
{
32 /** True if the submit button was clicked, and the form was posted */
38 /** Archive name, for reviewing deleted files */
41 /** Edit token for securing image views against XSS */
44 /** Title object for target parameter */
47 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
50 /** Array of checkbox specs (message, name, deletion bits) */
53 /** Information about the current type */
56 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
60 * Assorted information about each type, needed by the special page.
61 * TODO Move some of this to the list class
63 static $allowedTypes = array(
65 'check-label' => 'revdelete-hide-text',
66 'deletion-bits' => Revision
::DELETED_TEXT
,
67 'success' => 'revdelete-success',
68 'failure' => 'revdelete-failure',
69 'list-class' => 'RevDel_RevisionList',
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',
79 'check-label' => 'revdelete-hide-image',
80 'deletion-bits' => File
::DELETED_FILE
,
81 'success' => 'revdelete-success',
82 'failure' => 'revdelete-failure',
83 'list-class' => 'RevDel_FileList',
85 'filearchive' => array(
86 'check-label' => 'revdelete-hide-image',
87 'deletion-bits' => File
::DELETED_FILE
,
88 'success' => 'revdelete-success',
89 'failure' => 'revdelete-failure',
90 'list-class' => 'RevDel_ArchivedFileList',
93 'check-label' => 'revdelete-hide-name',
94 'deletion-bits' => LogPage
::DELETED_ACTION
,
95 'success' => 'logdelete-success',
96 'failure' => 'logdelete-failure',
97 'list-class' => 'RevDel_LogList',
101 /** Type map to support old log entries */
102 static $deprecatedTypeMap = array(
103 'oldid' => 'revision',
104 'artimestamp' => 'archive',
105 'oldimage' => 'oldimage',
106 'fileid' => 'filearchive',
107 'logid' => 'logging',
110 public function __construct() {
111 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
114 public function execute( $par ) {
115 global $wgOut, $wgUser, $wgRequest;
116 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
117 $wgOut->permissionRequired( 'deletedhistory' );
119 } else if( wfReadOnly() ) {
120 $wgOut->readOnlyPage();
123 $this->mIsAllowed
= $wgUser->isAllowed('deleterevision'); // for changes
124 $this->skin
= $wgUser->getSkin();
126 $this->outputHeader();
127 $this->submitClicked
= $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
128 # Handle our many different possible input types.
129 $ids = $wgRequest->getVal( 'ids' );
130 if ( !is_null( $ids ) ) {
131 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
132 $this->ids
= explode( ',', $ids );
135 $this->ids
= array_keys( $wgRequest->getArray('ids',array()) );
137 // $this->ids = array_map( 'intval', $this->ids );
138 $this->ids
= array_unique( array_filter( $this->ids
) );
140 if ( $wgRequest->getVal( 'action' ) == 'historysubmit' ) {
141 # For show/hide form submission from history page
142 $this->targetObj
= $GLOBALS['wgTitle'];
143 $this->typeName
= 'revision';
145 $this->typeName
= $wgRequest->getVal( 'type' );
146 $this->targetObj
= Title
::newFromText( $wgRequest->getText( 'target' ) );
149 # For reviewing deleted files...
150 $this->archiveName
= $wgRequest->getVal( 'file' );
151 $this->token
= $wgRequest->getVal( 'token' );
152 if ( $this->archiveName
&& $this->targetObj
) {
153 $this->tryShowFile( $this->archiveName
);
157 if ( isset( self
::$deprecatedTypeMap[$this->typeName
] ) ) {
158 $this->typeName
= self
::$deprecatedTypeMap[$this->typeName
];
162 if( !isset( self
::$allowedTypes[$this->typeName
] ) ||
count( $this->ids
) == 0 ) {
163 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
166 $this->typeInfo
= self
::$allowedTypes[$this->typeName
];
168 # If we have revisions, get the title from the first one
169 # since they should all be from the same page. This allows
170 # for more flexibility with page moves...
171 if( $this->typeName
== 'revision' ) {
172 $rev = Revision
::newFromId( $this->ids
[0] );
173 $this->targetObj
= $rev ?
$rev->getTitle() : $this->targetObj
;
176 $this->otherReason
= $wgRequest->getVal( 'wpReason' );
177 # We need a target page!
178 if( is_null($this->targetObj
) ) {
179 $wgOut->addWikiMsg( 'undelete-header' );
182 # Give a link to the logs/hist for this page
183 $this->showConvenienceLinks();
185 # Initialise checkboxes
186 $this->checks
= array(
187 array( $this->typeInfo
['check-label'], 'wpHidePrimary', $this->typeInfo
['deletion-bits'] ),
188 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
189 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
191 if( $wgUser->isAllowed('suppressrevision') ) {
192 $this->checks
[] = array( 'revdelete-hide-restricted',
193 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
196 # Either submit or create our form
197 if( $this->mIsAllowed
&& $this->submitClicked
) {
198 $this->submit( $wgRequest );
203 $qc = $this->getLogQueryCond();
204 # Show relevant lines from the deletion log
205 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
206 LogEventsList
::showLogExtract( $wgOut, 'delete',
207 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
208 # Show relevant lines from the suppression log
209 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
210 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'suppress' ) ) . "</h2>\n" );
211 LogEventsList
::showLogExtract( $wgOut, 'suppress',
212 $this->targetObj
->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
217 * Show some useful links in the subtitle
219 protected function showConvenienceLinks() {
220 global $wgOut, $wgUser, $wgLang;
221 # Give a link to the logs/hist for this page
222 if( $this->targetObj
) {
224 $links[] = $this->skin
->linkKnown(
225 SpecialPage
::getTitleFor( 'Log' ),
226 wfMsgHtml( 'viewpagelogs' ),
228 array( 'page' => $this->targetObj
->getPrefixedText() )
230 if ( $this->targetObj
->getNamespace() != NS_SPECIAL
) {
231 # Give a link to the page history
232 $links[] = $this->skin
->linkKnown(
234 wfMsgHtml( 'pagehist' ),
236 array( 'action' => 'history' )
238 # Link to deleted edits
239 if( $wgUser->isAllowed('undelete') ) {
240 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
241 $links[] = $this->skin
->linkKnown(
243 wfMsgHtml( 'deletedhist' ),
245 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
249 # Logs themselves don't have histories or archived revisions
250 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
255 * Get the condition used for fetching log snippets
257 protected function getLogQueryCond() {
259 // Revision delete logs for these item
260 $conds['log_type'] = array('delete','suppress');
261 $conds['log_action'] = $this->getList()->getLogAction();
262 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
263 $conds['ls_value'] = $this->ids
;
268 * Show a deleted file version requested by the visitor.
269 * TODO Mostly copied from Special:Undelete. Refactor.
271 protected function tryShowFile( $archiveName ) {
272 global $wgOut, $wgRequest, $wgUser, $wgLang;
274 $repo = RepoGroup
::singleton()->getLocalRepo();
275 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
277 // Check if user is allowed to see this file
278 if ( !$oimage->exists() ) {
279 $wgOut->addWikiMsg( 'revdelete-no-file' );
282 if( !$oimage->userCan(File
::DELETED_FILE
) ) {
283 if( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
284 $wgOut->permissionRequired( 'suppressrevision' );
286 $wgOut->permissionRequired( 'deletedtext' );
290 if ( !$wgUser->matchEditToken( $this->token
, $archiveName ) ) {
291 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
292 $this->targetObj
->getText(),
293 $wgLang->date( $oimage->getTimestamp() ),
294 $wgLang->time( $oimage->getTimestamp() ) );
296 Xml
::openElement( 'form', array(
298 'action' => $this->getTitle()->getLocalUrl(
299 'target=' . urlencode( $oimage->getName() ) .
300 '&file=' . urlencode( $archiveName ) .
301 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
304 Xml
::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
310 # We mustn't allow the output to be Squid cached, otherwise
311 # if an admin previews a deleted image, and it's cached, then
312 # a user without appropriate permissions can toddle off and
313 # nab the image, and Squid will serve it
314 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
315 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
316 $wgRequest->response()->header( 'Pragma: no-cache' );
318 # Stream the file to the client
320 require_once( "$IP/includes/StreamFile.php" );
321 $key = $oimage->getStorageKey();
322 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
323 wfStreamFile( $path );
327 * Get the list object for this request
329 protected function getList() {
330 if ( is_null( $this->list ) ) {
331 $class = $this->typeInfo
['list-class'];
332 $this->list = new $class( $this, $this->targetObj
, $this->ids
);
338 * Show a list of items that we will operate on, and show a form with checkboxes
339 * which will allow the user to choose new visibility settings.
341 protected function showForm() {
342 global $wgOut, $wgUser, $wgLang;
345 if ( $this->typeName
== 'logging' ) {
346 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids
) ) );
348 $wgOut->addWikiMsg( 'revdelete-selected',
349 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
352 $wgOut->addHTML( "<ul>" );
356 $list = $this->getList();
357 for ( $list->reset(); $list->current(); $list->next() ) {
358 $item = $list->current();
359 if ( !$item->canView() ) {
360 if( !$this->submitClicked
) {
361 $wgOut->permissionRequired( 'suppressrevision' );
364 $UserAllowed = false;
367 $wgOut->addHTML( $item->getHTML() );
370 if( !$numRevisions ) {
371 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
375 $wgOut->addHTML( "</ul>" );
377 $this->addUsageText();
379 // Normal sysops can always see what they did, but can't always change it
380 if( !$UserAllowed ) return;
382 // Show form if the user can submit
383 if( $this->mIsAllowed
) {
384 $out = Xml
::openElement( 'form', array( 'method' => 'post',
385 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
386 'id' => 'mw-revdel-form-revisions' ) ) .
387 Xml
::fieldset( wfMsg( 'revdelete-legend' ) ) .
388 $this->buildCheckBoxes() .
389 Xml
::openElement( 'table' ) .
391 '<td class="mw-label">' .
392 Xml
::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
394 '<td class="mw-input">' .
395 Xml
::listDropDown( 'wpRevDeleteReasonList',
396 wfMsgForContent( 'revdelete-reason-dropdown' ),
397 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
401 '<td class="mw-label">' .
402 Xml
::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
404 '<td class="mw-input">' .
405 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason' ) ) .
409 '<td class="mw-submit">' .
410 Xml
::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
411 array( 'name' => 'wpSubmit' ) ) .
414 Xml
::closeElement( 'table' ) .
415 Xml
::hidden( 'wpEditToken', $wgUser->editToken() ) .
416 Xml
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
417 Xml
::hidden( 'type', $this->typeName
) .
418 Xml
::hidden( 'ids', implode( ',', $this->ids
) ) .
419 Xml
::closeElement( 'fieldset' ) . "\n";
423 if( $this->mIsAllowed
) {
424 $out .= Xml
::closeElement( 'form' ) . "\n";
425 // Show link to edit the dropdown reasons
426 if( $wgUser->isAllowed( 'editinterface' ) ) {
427 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'revdelete-reason-dropdown' );
428 $link = $wgUser->getSkin()->link(
430 wfMsgHtml( 'revdelete-edit-reasonlist' ),
432 array( 'action' => 'edit' )
434 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
437 $wgOut->addHTML( $out );
441 * Show some introductory text
442 * FIXME Wikimedia-specific policy text
444 protected function addUsageText() {
445 global $wgOut, $wgUser;
446 $wgOut->addWikiMsg( 'revdelete-text' );
447 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
448 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
450 if( $this->mIsAllowed
) {
451 $wgOut->addWikiMsg( 'revdelete-confirm' );
456 * @return String: HTML
458 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($wgRequest), $bitfield );
470 foreach( $this->checks
as $item ) {
471 list( $message, $name, $field ) = $item;
472 $innerHTML = Xml
::checkLabel( wfMsg($message), $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">'.wfMsgHtml('revdelete-radio-same').'</th>';
482 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
483 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</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 = $wgRequest->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 = wfMsgHtml($message);
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.
511 * @param $request WebRequest
513 protected function submit( $request ) {
514 global $wgUser, $wgOut;
515 # Check edit token on submission
516 if( $this->submitClicked
&& !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
517 $wgOut->addWikiMsg( 'sessionfailure' );
520 $bitParams = $this->extractBitParams( $request );
521 $listReason = $request->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
522 $comment = $listReason;
523 if( $comment != 'other' && $this->otherReason
!= '' ) {
524 // Entry from drop down menu + additional comment
525 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason
;
526 } elseif( $comment == 'other' ) {
527 $comment = $this->otherReason
;
529 # Can the user set this field?
530 if( $bitParams[Revision
::DELETED_RESTRICTED
]==1 && !$wgUser->isAllowed('suppressrevision') ) {
531 $wgOut->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() {
551 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
552 $wgOut->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo
['success'] );
553 $this->list->reloadFromMaster();
558 * Report that the submit operation failed
560 protected function failure( $status ) {
562 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
563 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo
['failure'] ) );
568 * Put together an array that contains -1, 0, or the *_deleted const for each bit
569 * @param $request WebRequest
572 protected function extractBitParams( $request ) {
574 foreach( $this->checks
as $item ) {
575 list( /* message */ , $name, $field ) = $item;
576 $val = $request->getInt( $name, 0 /* unchecked */ );
577 if( $val < -1 ||
$val > 1) {
578 $val = -1; // -1 for existing value
580 $bitfield[$field] = $val;
582 if( !isset($bitfield[Revision
::DELETED_RESTRICTED
]) ) {
583 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
589 * Put together a rev_deleted bitfield
590 * @param $bitPars array extractBitParams() params
591 * @param $oldfield int current bitfield
594 public static function extractBitfield( $bitPars, $oldfield ) {
595 // Build the actual new rev_deleted bitfield
597 foreach( $bitPars as $const => $val ) {
599 $newBits |
= $const; // $const is the *_deleted const
600 } else if( $val == -1 ) {
601 $newBits |
= ($oldfield & $const); // use existing
608 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
610 protected function save( $bitfield, $reason, $title ) {
611 return $this->getList()->setVisibility(
612 array( 'value' => $bitfield, 'comment' => $reason )