4edf33c7482735f55f4393786f9819531d8b823b
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 /** @var bool Was the DB modified in this request */
32 protected $wasSaved = false;
34 /** @var bool True if the submit button was clicked, and the form was posted */
35 private $submitClicked;
37 /** @var array Target ID list */
40 /** @var string Archive name, for reviewing deleted files */
43 /** @var string Edit token for securing image views against XSS */
46 /** @var Title Title object for target parameter */
49 /** @var string Deletion type, may be revision, archive, oldimage, filearchive, logging. */
52 /** @var array Array of checkbox specs (message, name, deletion bits) */
55 /** @var array UI Labels about the current type */
58 /** @var RevDel_List RevDel_List object, storing the list of items to be deleted/undeleted */
61 /** @var bool Whether user is allowed to perform the action */
68 * UI labels for each type.
70 private static $UILabels = array(
72 'check-label' => 'revdelete-hide-text',
73 'success' => 'revdelete-success',
74 'failure' => 'revdelete-failure',
75 'text' => 'revdelete-text-text',
76 'selected'=> 'revdelete-selected-text',
79 'check-label' => 'revdelete-hide-text',
80 'success' => 'revdelete-success',
81 'failure' => 'revdelete-failure',
82 'text' => 'revdelete-text-text',
83 'selected'=> 'revdelete-selected-text',
86 'check-label' => 'revdelete-hide-image',
87 'success' => 'revdelete-success',
88 'failure' => 'revdelete-failure',
89 'text' => 'revdelete-text-file',
90 'selected'=> 'revdelete-selected-file',
92 'filearchive' => array(
93 'check-label' => 'revdelete-hide-image',
94 'success' => 'revdelete-success',
95 'failure' => 'revdelete-failure',
96 'text' => 'revdelete-text-file',
97 'selected'=> 'revdelete-selected-file',
100 'check-label' => 'revdelete-hide-name',
101 'success' => 'logdelete-success',
102 'failure' => 'logdelete-failure',
103 'text' => 'logdelete-text',
104 'selected' => 'logdelete-selected',
108 public function __construct() {
109 parent
::__construct( 'Revisiondelete', 'deletedhistory' );
112 public function execute( $par ) {
113 $this->checkPermissions();
114 $this->checkReadOnly();
116 $output = $this->getOutput();
117 $user = $this->getUser();
120 $this->outputHeader();
121 $request = $this->getRequest();
122 $this->submitClicked
= $request->wasPosted() && $request->getBool( 'wpSubmit' );
123 # Handle our many different possible input types.
124 $ids = $request->getVal( 'ids' );
125 if ( !is_null( $ids ) ) {
126 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
127 $this->ids
= explode( ',', $ids );
130 $this->ids
= array_keys( $request->getArray( 'ids', array() ) );
132 // $this->ids = array_map( 'intval', $this->ids );
133 $this->ids
= array_unique( array_filter( $this->ids
) );
135 if ( $request->getVal( 'action' ) == 'historysubmit' ||
$request->getVal( 'action' ) == 'revisiondelete' ) {
136 // For show/hide form submission from history page
137 // Since we are access through index.php?title=XXX&action=historysubmit
138 // getFullTitle() will contain the target title and not our title
139 $this->targetObj
= $this->getFullTitle();
140 $this->typeName
= 'revision';
142 $this->typeName
= $request->getVal( 'type' );
143 $this->targetObj
= Title
::newFromText( $request->getText( 'target' ) );
146 # For reviewing deleted files...
147 $this->archiveName
= $request->getVal( 'file' );
148 $this->token
= $request->getVal( 'token' );
149 if ( $this->archiveName
&& $this->targetObj
) {
150 $this->tryShowFile( $this->archiveName
);
155 $this->typeName
= RevisionDeleter
::getCanonicalTypeName( $this->typeName
);
158 if ( !$this->typeName ||
count( $this->ids
) == 0 ) {
159 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
161 $this->typeLabels
= self
::$UILabels[$this->typeName
];
162 $this->mIsAllowed
= $user->isAllowed( RevisionDeleter
::getRestriction( $this->typeName
) );
164 # Allow the list type to adjust the passed target
165 $this->targetObj
= RevisionDeleter
::suggestTarget( $this->typeName
, $this->targetObj
, $this->ids
);
167 $this->otherReason
= $request->getVal( 'wpReason' );
168 # We need a target page!
169 if ( is_null( $this->targetObj
) ) {
170 $output->addWikiMsg( 'undelete-header' );
174 # Give a link to the logs/hist for this page
175 $this->showConvenienceLinks();
177 # Initialise checkboxes
178 $this->checks
= array(
179 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
180 array( $this->typeLabels
['check-label'], 'wpHidePrimary',
181 RevisionDeleter
::getRevdelConstant( $this->typeName
)
183 array( 'revdelete-hide-comment', 'wpHideComment', Revision
::DELETED_COMMENT
),
184 array( 'revdelete-hide-user', 'wpHideUser', Revision
::DELETED_USER
)
186 if ( $user->isAllowed( 'suppressrevision' ) ) {
187 $this->checks
[] = array( 'revdelete-hide-restricted',
188 'wpHideRestricted', Revision
::DELETED_RESTRICTED
);
191 # Either submit or create our form
192 if ( $this->mIsAllowed
&& $this->submitClicked
) {
193 $this->submit( $request );
198 $qc = $this->getLogQueryCond();
199 # Show relevant lines from the deletion log
200 $deleteLogPage = new LogPage( 'delete' );
201 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
202 LogEventsList
::showLogExtract(
207 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
209 # Show relevant lines from the suppression log
210 if ( $user->isAllowed( 'suppressionlog' ) ) {
211 $suppressLogPage = new LogPage( 'suppress' );
212 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
213 LogEventsList
::showLogExtract(
218 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved
)
224 * Show some useful links in the subtitle
226 protected function showConvenienceLinks() {
227 # Give a link to the logs/hist for this page
228 if ( $this->targetObj
) {
229 // Also set header tabs to be for the target.
230 $this->getSkin()->setRelevantTitle( $this->targetObj
);
233 $links[] = Linker
::linkKnown(
234 SpecialPage
::getTitleFor( 'Log' ),
235 $this->msg( 'viewpagelogs' )->escaped(),
237 array( 'page' => $this->targetObj
->getPrefixedText() )
239 if ( !$this->targetObj
->isSpecialPage() ) {
240 # Give a link to the page history
241 $links[] = Linker
::linkKnown(
243 $this->msg( 'pagehist' )->escaped(),
245 array( 'action' => 'history' )
247 # Link to deleted edits
248 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
249 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
250 $links[] = Linker
::linkKnown(
252 $this->msg( 'deletedhist' )->escaped(),
254 array( 'target' => $this->targetObj
->getPrefixedDBkey() )
258 # Logs themselves don't have histories or archived revisions
259 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
264 * Get the condition used for fetching log snippets
267 protected function getLogQueryCond() {
269 // Revision delete logs for these item
270 $conds['log_type'] = array( 'delete', 'suppress' );
271 $conds['log_action'] = $this->getList()->getLogAction();
272 $conds['ls_field'] = RevisionDeleter
::getRelationType( $this->typeName
);
273 $conds['ls_value'] = $this->ids
;
279 * Show a deleted file version requested by the visitor.
280 * TODO Mostly copied from Special:Undelete. Refactor.
281 * @param string $archiveName
283 protected function tryShowFile( $archiveName ) {
284 $repo = RepoGroup
::singleton()->getLocalRepo();
285 $oimage = $repo->newFromArchiveName( $this->targetObj
, $archiveName );
287 // Check if user is allowed to see this file
288 if ( !$oimage->exists() ) {
289 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
293 $user = $this->getUser();
294 if ( !$oimage->userCan( File
::DELETED_FILE
, $user ) ) {
295 if ( $oimage->isDeleted( File
::DELETED_RESTRICTED
) ) {
296 throw new PermissionsError( 'suppressrevision' );
298 throw new PermissionsError( 'deletedtext' );
301 if ( !$user->matchEditToken( $this->token
, $archiveName ) ) {
302 $lang = $this->getLanguage();
303 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
304 $this->targetObj
->getText(),
305 $lang->userDate( $oimage->getTimestamp(), $user ),
306 $lang->userTime( $oimage->getTimestamp(), $user ) );
307 $this->getOutput()->addHTML(
308 Xml
::openElement( 'form', array(
310 'action' => $this->getPageTitle()->getLocalURL( array(
311 'target' => $this->targetObj
->getPrefixedDBkey(),
312 'file' => $archiveName,
313 'token' => $user->getEditToken( $archiveName ),
317 Xml
::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
323 $this->getOutput()->disable();
324 # We mustn't allow the output to be Squid cached, otherwise
325 # if an admin previews a deleted image, and it's cached, then
326 # a user without appropriate permissions can toddle off and
327 # nab the image, and Squid will serve it
328 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
329 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
330 $this->getRequest()->response()->header( 'Pragma: no-cache' );
332 $key = $oimage->getStorageKey();
333 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
334 $repo->streamFile( $path );
338 * Get the list object for this request
339 * @return RevDel_List
341 protected function getList() {
342 if ( is_null( $this->revDelList
) ) {
343 $this->revDelList
= RevisionDeleter
::createList(
344 $this->typeName
, $this->getContext(), $this->targetObj
, $this->ids
348 return $this->revDelList
;
352 * Show a list of items that we will operate on, and show a form with checkboxes
353 * which will allow the user to choose new visibility settings.
355 protected function showForm() {
358 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
359 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels
['selected'],
360 $this->getLanguage()->formatNum( count( $this->ids
) ), $this->targetObj
->getPrefixedText() ) );
362 $this->getOutput()->addHTML( "<ul>" );
366 $list = $this->getList();
367 for ( $list->reset(); $list->current(); $list->next() ) {
368 $item = $list->current();
369 if ( !$item->canView() ) {
370 if ( !$this->submitClicked
) {
371 throw new PermissionsError( 'suppressrevision' );
373 $userAllowed = false;
376 $this->getOutput()->addHTML( $item->getHTML() );
379 if ( !$numRevisions ) {
380 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
383 $this->getOutput()->addHTML( "</ul>" );
385 $this->addUsageText();
387 // Normal sysops can always see what they did, but can't always change it
388 if ( !$userAllowed ) {
392 // Show form if the user can submit
393 if ( $this->mIsAllowed
) {
394 $out = Xml
::openElement( 'form', array( 'method' => 'post',
395 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
396 'id' => 'mw-revdel-form-revisions' ) ) .
397 Xml
::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
398 $this->buildCheckBoxes() .
399 Xml
::openElement( 'table' ) .
401 '<td class="mw-label">' .
402 Xml
::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
404 '<td class="mw-input">' .
405 Xml
::listDropDown( 'wpRevDeleteReasonList',
406 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
407 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
408 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
412 '<td class="mw-label">' .
413 Xml
::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
415 '<td class="mw-input">' .
416 Xml
::input( 'wpReason', 60, $this->otherReason
, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
420 '<td class="mw-submit">' .
421 Xml
::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
422 array( 'name' => 'wpSubmit' ) ) .
425 Xml
::closeElement( 'table' ) .
426 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
427 Html
::hidden( 'target', $this->targetObj
->getPrefixedText() ) .
428 Html
::hidden( 'type', $this->typeName
) .
429 Html
::hidden( 'ids', implode( ',', $this->ids
) ) .
430 Xml
::closeElement( 'fieldset' ) . "\n";
434 if ( $this->mIsAllowed
) {
435 $out .= Xml
::closeElement( 'form' ) . "\n";
436 // Show link to edit the dropdown reasons
437 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
438 $title = Title
::makeTitle( NS_MEDIAWIKI
, 'Revdelete-reason-dropdown' );
439 $link = Linker
::link(
441 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
443 array( 'action' => 'edit' )
445 $out .= Xml
::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
448 $this->getOutput()->addHTML( $out );
452 * Show some introductory text
453 * @todo FIXME: Wikimedia-specific policy text
455 protected function addUsageText() {
456 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
457 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>\n$2", $this->typeLabels
['text'], 'revdelete-text-others' );
458 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
459 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
461 if ( $this->mIsAllowed
) {
462 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
467 * @return string HTML
469 protected function buildCheckBoxes() {
471 // If there is just one item, use checkboxes
472 $list = $this->getList();
473 if ( $list->length() == 1 ) {
475 $bitfield = $list->current()->getBits(); // existing field
476 if ( $this->submitClicked
) {
477 $bitfield = RevisionDeleter
::extractBitfield( $this->extractBitParams(), $bitfield );
479 foreach ( $this->checks
as $item ) {
480 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
481 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
482 list( $message, $name, $field ) = $item;
483 $innerHTML = Xml
::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
484 if ( $field == Revision
::DELETED_RESTRICTED
) {
485 $innerHTML = "<b>$innerHTML</b>";
487 $line = Xml
::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
488 $html .= "<tr>$line</tr>\n";
491 // Otherwise, use tri-state radios
493 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
494 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
495 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
496 $html .= "<th></th></tr>\n";
497 foreach ( $this->checks
as $item ) {
498 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
499 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
500 list( $message, $name, $field ) = $item;
501 // If there are several items, use third state by default...
502 if ( $this->submitClicked
) {
503 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
505 $selected = -1; // use existing field
507 $line = '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, -1, $selected == -1 ) . '</td>';
508 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 0, $selected == 0 ) . '</td>';
509 $line .= '<td class="mw-revdel-checkbox">' . Xml
::radio( $name, 1, $selected == 1 ) . '</td>';
510 $label = $this->msg( $message )->escaped();
511 if ( $field == Revision
::DELETED_RESTRICTED
) {
512 $label = "<b>$label</b>";
514 $line .= "<td>$label</td>";
515 $html .= "<tr>$line</tr>\n";
525 * UI entry point for form submission.
526 * @throws PermissionsError
529 protected function submit() {
530 # Check edit token on submission
531 $token = $this->getRequest()->getVal( 'wpEditToken' );
532 if ( $this->submitClicked
&& !$this->getUser()->matchEditToken( $token ) ) {
533 $this->getOutput()->addWikiMsg( 'sessionfailure' );
537 $bitParams = $this->extractBitParams();
538 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
539 $comment = $listReason;
540 if ( $comment != 'other' && $this->otherReason
!= '' ) {
541 // Entry from drop down menu + additional comment
542 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason
;
543 } elseif ( $comment == 'other' ) {
544 $comment = $this->otherReason
;
546 # Can the user set this field?
547 if ( $bitParams[Revision
::DELETED_RESTRICTED
] == 1 && !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
548 throw new PermissionsError( 'suppressrevision' );
550 # If the save went through, go to success message...
551 $status = $this->save( $bitParams, $comment, $this->targetObj
);
552 if ( $status->isGood() ) {
557 # ...otherwise, bounce back to form...
558 $this->failure( $status );
565 * Report that the submit operation succeeded
567 protected function success() {
568 // Messages: revdelete-success, logdelete-success
569 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
570 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeLabels
['success'] );
571 $this->wasSaved
= true;
572 $this->revDelList
->reloadFromMaster();
577 * Report that the submit operation failed
579 protected function failure( $status ) {
580 // Messages: revdelete-failure, logdelete-failure
581 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
582 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels
['failure'] ) );
587 * Put together an array that contains -1, 0, or the *_deleted const for each bit
591 protected function extractBitParams() {
593 foreach ( $this->checks
as $item ) {
594 list( /* message */, $name, $field ) = $item;
595 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
596 if ( $val < -1 ||
$val > 1 ) {
597 $val = -1; // -1 for existing value
599 $bitfield[$field] = $val;
601 if ( !isset( $bitfield[Revision
::DELETED_RESTRICTED
] ) ) {
602 $bitfield[Revision
::DELETED_RESTRICTED
] = 0;
609 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
610 * @param int $bitfield
611 * @param string $reason
612 * @param Title $title
615 protected function save( $bitfield, $reason, $title ) {
616 return $this->getList()->setVisibility(
617 array( 'value' => $bitfield, 'comment' => $reason )
621 protected function getGroupName() {