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 */
59 * UI labels for each type.
61 static $UILabels = array(
63 'check-label' => 'revdelete-hide-text',
64 'success' => 'revdelete-success',
65 'failure' => 'revdelete-failure',
68 'check-label' => 'revdelete-hide-text',
69 'success' => 'revdelete-success',
70 'failure' => 'revdelete-failure',
73 'check-label' => 'revdelete-hide-image',
74 'success' => 'revdelete-success',
75 'failure' => 'revdelete-failure',
77 'filearchive' => array(
78 'check-label' => 'revdelete-hide-image',
79 'success' => 'revdelete-success',
80 'failure' => 'revdelete-failure',
83 'check-label' => 'revdelete-hide-name',
84 'success' => 'logdelete-success',
85 'failure' => 'logdelete-failure',
89 public function __construct() {
90 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
93 public function execute( $par ) {
94 $this->checkPermissions();
95 $this->checkReadOnly();
97 $output = $this->getOutput();
98 $user = $this->getUser();
101 $this->outputHeader();
102 $request = $this->getRequest();
103 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
104 # Handle our many different possible input types.
105 $ids = $request->getVal( 'ids' );
106 if ( !is_null( $ids ) ) {
107 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
108 $this->ids
= explode( ',', $ids );
111 $this->ids
= array_keys( $request->getArray( 'ids', array() ) );
113 // $this->ids = array_map( 'intval', $this->ids );
114 $this->ids
= array_unique( array_filter( $this->ids
) );
116 if ( $request->getVal( 'action' ) == 'historysubmit' ||
$request->getVal( 'action' ) == 'revisiondelete' ) {
117 // For show/hide form submission from history page
118 // Since we are access through index.php?title=XXX&action=historysubmit
119 // getFullTitle() will contain the target title and not our title
120 $this->targetObj
= $this->getFullTitle();
121 $this->typeName
= 'revision';
123 $this->typeName
= $request->getVal( 'type' );
124 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
127 # For reviewing deleted files...
128 $this->archiveName
= $request->getVal( 'file' );
129 $this->token
= $request->getVal( 'token' );
130 if ( $this->archiveName
&& $this->targetObj
) {
131 $this->tryShowFile( $this->archiveName
);
135 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
138 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
139 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
141 $this->typeLabels
= self
::$UILabels[$this->typeName
];
142 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
144 # Allow the list type to adjust the passed target
145 $this->targetObj
= RevisionDeleter
::suggestTarget( $this->typeName
, $this->targetObj
, $this->ids
);
147 $this->otherReason
= $request->getVal( 'wpReason' );
148 # We need a target page!
149 if ( is_null( $this->targetObj
) ) {
150 $output->addWikiMsg( 'undelete-header' );
153 # Give a link to the logs/hist for this page
154 $this->showConvenienceLinks();
156 # Initialise checkboxes
157 $this->checks
= array(
158 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
159 RevisionDeleter
::getRevdelConstant( $this->typeName
)
161 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
162 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
164 if ( $user->isAllowed( 'suppressrevision' ) ) {
165 $this->checks
[] = array( 'revdelete-hide-restricted',
166 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
169 # Either submit or create our form
170 if ( $this->mIsAllowed
&& $this->submitClicked
) {
171 $this->submit( $request );
176 $qc = $this->getLogQueryCond();
177 # Show relevant lines from the deletion log
178 $deleteLogPage = new LogPage( 'delete' );
179 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
180 LogEventsList
::showLogExtract( $output, 'delete',
181 $this->targetObj
, '', array( 'lim' => 25, 'conds' => $qc ) );
182 # Show relevant lines from the suppression log
183 if ( $user->isAllowed( 'suppressionlog' ) ) {
184 $suppressLogPage = new LogPage( 'suppress' );
185 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
186 LogEventsList
::showLogExtract( $output, 'suppress',
187 $this->targetObj
, '', array( 'lim' => 25, 'conds' => $qc ) );
192 * Show some useful links in the subtitle
194 protected function showConvenienceLinks() {
195 # Give a link to the logs/hist for this page
196 if ( $this->targetObj
) {
197 // Also set header tabs to be for the target.
198 $this->getSkin()->setRelevantTitle( $this->targetObj
);
201 $links[] = Linker
::linkKnown(
202 SpecialPage
::getTitleFor( 'Log' ),
203 $this->msg( 'viewpagelogs' )->escaped(),
205 array( 'page' => $this->targetObj
->getPrefixedText() )
207 if ( !$this->targetObj
->isSpecialPage() ) {
208 # Give a link to the page history
209 $links[] = Linker
::linkKnown(
211 $this->msg( 'pagehist' )->escaped(),
213 array( 'action' => 'history' )
215 # Link to deleted edits
216 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
217 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
218 $links[] = Linker
::linkKnown(
220 $this->msg( 'deletedhist' )->escaped(),
222 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
226 # Logs themselves don't have histories or archived revisions
227 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
232 * Get the condition used for fetching log snippets
235 protected function getLogQueryCond() {
237 // Revision delete logs for these item
238 $conds['log_type'] = array( 'delete', 'suppress' );
239 $conds['log_action'] = $this->getList()->getLogAction();
240 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
241 $conds['ls_value'] = $this->ids
;
246 * Show a deleted file version requested by the visitor.
247 * TODO Mostly copied from Special:Undelete. Refactor.
249 protected function tryShowFile( $archiveName ) {
250 $repo = RepoGroup
::singleton()->getLocalRepo();
251 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
253 // Check if user is allowed to see this file
254 if ( !$oimage->exists() ) {
255 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
258 $user = $this->getUser();
259 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
260 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
261 throw new PermissionsError( 'suppressrevision' );
263 throw new PermissionsError( 'deletedtext' );
266 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
267 $lang = $this->getLanguage();
268 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
269 $this->targetObj
->getText(),
270 $lang->userDate( $oimage->getTimestamp(), $user ),
271 $lang->userTime( $oimage->getTimestamp(), $user ) );
272 $this->getOutput()->addHTML(
273 Xml
::openElement( 'form', array(
275 'action' => $this->getTitle()->getLocalURL( array(
276 'target' => $this->targetObj
->getPrefixedDBkey(),
277 'file' => $archiveName,
278 'token' => $user->getEditToken( $archiveName ),
282 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
287 $this->getOutput()->disable();
288 # We mustn't allow the output to be Squid cached, otherwise
289 # if an admin previews a deleted image, and it's cached, then
290 # a user without appropriate permissions can toddle off and
291 # nab the image, and Squid will serve it
292 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
293 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
294 $this->getRequest()->response()->header( 'Pragma: no-cache' );
296 $key = $oimage->getStorageKey();
297 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
298 $repo->streamFile( $path );
302 * Get the list object for this request
304 protected function getList() {
305 if ( is_null( $this->list ) ) {
306 $this->list = RevisionDeleter
::createList(
307 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
314 * Show a list of items that we will operate on, and show a form with checkboxes
315 * which will allow the user to choose new visibility settings.
317 protected function showForm() {
320 if ( $this->typeName
== 'logging' ) {
321 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLanguage()->formatNum( count( $this->ids
) ) );
323 $this->getOutput()->addWikiMsg( 'revdelete-selected',
324 $this->targetObj
->getPrefixedText(), count( $this->ids
) );
327 $this->getOutput()->addHTML( "<ul>" );
331 $list = $this->getList();
332 for ( $list->reset(); $list->current(); $list->next() ) {
333 $item = $list->current();
334 if ( !$item->canView() ) {
335 if ( !$this->submitClicked
) {
336 throw new PermissionsError( 'suppressrevision' );
338 $UserAllowed = false;
341 $this->getOutput()->addHTML( $item->getHTML() );
344 if ( !$numRevisions ) {
345 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
348 $this->getOutput()->addHTML( "</ul>" );
350 $this->addUsageText();
352 // Normal sysops can always see what they did, but can't always change it
353 if ( !$UserAllowed ) {
357 // Show form if the user can submit
358 if ( $this->mIsAllowed
) {
359 $out = Xml
::openElement( 'form', array( 'method' => 'post',
360 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ),
361 'id' => 'mw-revdel-form-revisions' ) ) .
362 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
363 $this->buildCheckBoxes() .
364 Xml
::openElement( 'table' ) .
366 '<td class="mw-label">' .
367 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
369 '<td class="mw-input">' .
370 Xml
::listDropDown( 'wpRevDeleteReasonList',
371 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
372 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
373 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
377 '<td class="mw-label">' .
378 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
380 '<td class="mw-input">' .
381 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
385 '<td class="mw-submit">' .
386 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
387 array( 'name' => 'wpSubmit' ) ) .
390 Xml
::closeElement( 'table' ) .
391 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
392 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
393 Html
::hidden( 'type', $this->typeName
) .
394 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
395 Xml
::closeElement( 'fieldset' ) . "\n";
399 if ( $this->mIsAllowed
) {
400 $out .= Xml
::closeElement( 'form' ) . "\n";
401 // Show link to edit the dropdown reasons
402 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
403 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
404 $link = Linker
::link(
406 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
408 array( 'action' => 'edit' )
410 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
413 $this->getOutput()->addHTML( $out );
417 * Show some introductory text
418 * @todo FIXME: Wikimedia-specific policy text
420 protected function addUsageText() {
421 $this->getOutput()->addWikiMsg( 'revdelete-text' );
422 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
423 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
425 if ( $this->mIsAllowed
) {
426 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
431 * @return String: HTML
433 protected function buildCheckBoxes() {
435 // If there is just one item, use checkboxes
436 $list = $this->getList();
437 if ( $list->length() == 1 ) {
439 $bitfield = $list->current()->getBits(); // existing field
440 if ( $this->submitClicked
) {
441 $bitfield = $this->extractBitfield( $this->extractBitParams(), $bitfield );
443 foreach ( $this->checks
as $item ) {
444 list( $message, $name, $field ) = $item;
445 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
446 if ( $field == Revision
::DELETED_RESTRICTED
) {
447 $innerHTML = "<b>$innerHTML</b>";
449 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
450 $html .= "<tr>$line</tr>\n";
452 // Otherwise, use tri-state radios
455 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
456 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
457 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
458 $html .= "<th></th></tr>\n";
459 foreach ( $this->checks
as $item ) {
460 list( $message, $name, $field ) = $item;
461 // If there are several items, use third state by default...
462 if ( $this->submitClicked
) {
463 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
465 $selected = -1; // use existing field
467 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
468 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
469 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
470 $label = $this->msg( $message )->escaped();
471 if ( $field == Revision
::DELETED_RESTRICTED
) {
472 $label = "<b>$label</b>";
474 $line .= "<td>$label</td>";
475 $html .= "<tr>$line</tr>\n";
484 * UI entry point for form submission.
485 * @throws PermissionsError
488 protected function submit() {
489 # Check edit token on submission
490 $token = $this->getRequest()->getVal( 'wpEditToken' );
491 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
492 $this->getOutput()->addWikiMsg( 'sessionfailure' );
495 $bitParams = $this->extractBitParams();
496 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
497 $comment = $listReason;
498 if ( $comment != 'other' && $this->otherReason
!= '' ) {
499 // Entry from drop down menu + additional comment
500 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
501 } elseif ( $comment == 'other' ) {
502 $comment = $this->otherReason
;
504 # Can the user set this field?
505 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1 && !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
506 throw new PermissionsError( 'suppressrevision' );
508 # If the save went through, go to success message...
509 $status = $this->save( $bitParams, $comment, $this->targetObj
);
510 if ( $status->isGood() ) {
513 # ...otherwise, bounce back to form...
515 $this->failure( $status );
521 * Report that the submit operation succeeded
523 protected function success() {
524 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
525 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeLabels
['success'] );
526 $this->list->reloadFromMaster();
531 * Report that the submit operation failed
533 protected function failure( $status ) {
534 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
535 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
540 * Put together an array that contains -1, 0, or the *_deleted const for each bit
544 protected function extractBitParams() {
546 foreach ( $this->checks
as $item ) {
547 list( /* message */, $name, $field ) = $item;
548 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
549 if ( $val < -1 ||
$val > 1 ) {
550 $val = -1; // -1 for existing value
552 $bitfield[$field] = $val;
554 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
555 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
561 * Put together a rev_deleted bitfield
562 * @deprecated since 1.22, use RevisionDeleter::extractBitfield instead
563 * @param array $bitPars extractBitParams() params
564 * @param int $oldfield current bitfield
567 public static function extractBitfield( $bitPars, $oldfield ) {
568 return RevisionDeleter
::extractBitfield( $bitPars, $oldfield );
572 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
578 protected function save( $bitfield, $reason, $title ) {
579 return $this->getList()->setVisibility(
580 array( 'value' => $bitfield, 'comment' => $reason )
584 protected function getGroupName() {