Implement extension registration from an extension.json file
[mediawiki.git] / includes / specials / SpecialRevisiondelete.php
blob7a350c701c5dd2c87637727d094449ca422e1dd3
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
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 */
38 private $ids;
40 /** @var string Archive name, for reviewing deleted files */
41 private $archiveName;
43 /** @var string Edit token for securing image views against XSS */
44 private $token;
46 /** @var Title Title object for target parameter */
47 private $targetObj;
49 /** @var string Deletion type, may be revision, archive, oldimage, filearchive, logging. */
50 private $typeName;
52 /** @var array Array of checkbox specs (message, name, deletion bits) */
53 private $checks;
55 /** @var array UI Labels about the current type */
56 private $typeLabels;
58 /** @var RevDelList RevDelList object, storing the list of items to be deleted/undeleted */
59 private $revDelList;
61 /** @var bool Whether user is allowed to perform the action */
62 private $mIsAllowed;
64 /** @var string */
65 private $otherReason;
67 /**
68 * UI labels for each type.
70 private static $UILabels = array(
71 'revision' => 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',
78 'archive' => array(
79 'check-label' => 'revdelete-hide-text',
80 'success' => 'revdelete-success',
81 'failure' => 'revdelete-failure',
82 'text' => 'revdelete-text-text',
83 'selected'=> 'revdelete-selected-text',
85 'oldimage' => array(
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',
99 'logging' => array(
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();
119 $this->setHeaders();
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 );
128 } else {
129 # Array input
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'
136 || $request->getVal( 'action' ) == 'revisiondelete'
138 // For show/hide form submission from history page
139 // Since we are access through index.php?title=XXX&action=historysubmit
140 // getFullTitle() will contain the target title and not our title
141 $this->targetObj = $this->getFullTitle();
142 $this->typeName = 'revision';
143 } else {
144 $this->typeName = $request->getVal( 'type' );
145 $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
148 # For reviewing deleted files...
149 $this->archiveName = $request->getVal( 'file' );
150 $this->token = $request->getVal( 'token' );
151 if ( $this->archiveName && $this->targetObj ) {
152 $this->tryShowFile( $this->archiveName );
154 return;
157 $this->typeName = RevisionDeleter::getCanonicalTypeName( $this->typeName );
159 # No targets?
160 if ( !$this->typeName || count( $this->ids ) == 0 ) {
161 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
164 # Allow the list type to adjust the passed target
165 $this->targetObj = RevisionDeleter::suggestTarget(
166 $this->typeName,
167 $this->targetObj,
168 $this->ids
171 $this->typeLabels = self::$UILabels[$this->typeName];
172 $list = $this->getList();
173 $list->reset();
174 $bitfield = $list->current()->getBits();
175 $this->mIsAllowed = $user->isAllowed( RevisionDeleter::getRestriction( $this->typeName ) );
176 $canViewSuppressedOnly = $this->getUser()->isAllowed( 'viewsuppressed' ) &&
177 !$this->getUser()->isAllowed( 'suppressrevision' );
178 $pageIsSuppressed = $bitfield & Revision::DELETED_RESTRICTED;
179 $this->mIsAllowed = $this->mIsAllowed && !( $canViewSuppressedOnly && $pageIsSuppressed );
181 $this->otherReason = $request->getVal( 'wpReason' );
182 # We need a target page!
183 if ( is_null( $this->targetObj ) ) {
184 $output->addWikiMsg( 'undelete-header' );
186 return;
188 # Give a link to the logs/hist for this page
189 $this->showConvenienceLinks();
191 # Initialise checkboxes
192 $this->checks = array(
193 # Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name
194 array( $this->typeLabels['check-label'], 'wpHidePrimary',
195 RevisionDeleter::getRevdelConstant( $this->typeName )
197 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
198 array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER )
200 if ( $user->isAllowed( 'suppressrevision' ) ) {
201 $this->checks[] = array( 'revdelete-hide-restricted',
202 'wpHideRestricted', Revision::DELETED_RESTRICTED );
205 # Either submit or create our form
206 if ( $this->mIsAllowed && $this->submitClicked ) {
207 $this->submit( $request );
208 } else {
209 $this->showForm();
212 $qc = $this->getLogQueryCond();
213 # Show relevant lines from the deletion log
214 $deleteLogPage = new LogPage( 'delete' );
215 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
216 LogEventsList::showLogExtract(
217 $output,
218 'delete',
219 $this->targetObj,
220 '', /* user */
221 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved )
223 # Show relevant lines from the suppression log
224 if ( $user->isAllowed( 'suppressionlog' ) ) {
225 $suppressLogPage = new LogPage( 'suppress' );
226 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
227 LogEventsList::showLogExtract(
228 $output,
229 'suppress',
230 $this->targetObj,
232 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved )
238 * Show some useful links in the subtitle
240 protected function showConvenienceLinks() {
241 # Give a link to the logs/hist for this page
242 if ( $this->targetObj ) {
243 // Also set header tabs to be for the target.
244 $this->getSkin()->setRelevantTitle( $this->targetObj );
246 $links = array();
247 $links[] = Linker::linkKnown(
248 SpecialPage::getTitleFor( 'Log' ),
249 $this->msg( 'viewpagelogs' )->escaped(),
250 array(),
251 array( 'page' => $this->targetObj->getPrefixedText() )
253 if ( !$this->targetObj->isSpecialPage() ) {
254 # Give a link to the page history
255 $links[] = Linker::linkKnown(
256 $this->targetObj,
257 $this->msg( 'pagehist' )->escaped(),
258 array(),
259 array( 'action' => 'history' )
261 # Link to deleted edits
262 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
263 $undelete = SpecialPage::getTitleFor( 'Undelete' );
264 $links[] = Linker::linkKnown(
265 $undelete,
266 $this->msg( 'deletedhist' )->escaped(),
267 array(),
268 array( 'target' => $this->targetObj->getPrefixedDBkey() )
272 # Logs themselves don't have histories or archived revisions
273 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
278 * Get the condition used for fetching log snippets
279 * @return array
281 protected function getLogQueryCond() {
282 $conds = array();
283 // Revision delete logs for these item
284 $conds['log_type'] = array( 'delete', 'suppress' );
285 $conds['log_action'] = $this->getList()->getLogAction();
286 $conds['ls_field'] = RevisionDeleter::getRelationType( $this->typeName );
287 $conds['ls_value'] = $this->ids;
289 return $conds;
293 * Show a deleted file version requested by the visitor.
294 * @todo Mostly copied from Special:Undelete. Refactor.
295 * @param string $archiveName
296 * @throws MWException
297 * @throws PermissionsError
299 protected function tryShowFile( $archiveName ) {
300 $repo = RepoGroup::singleton()->getLocalRepo();
301 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
302 $oimage->load();
303 // Check if user is allowed to see this file
304 if ( !$oimage->exists() ) {
305 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
307 return;
309 $user = $this->getUser();
310 if ( !$oimage->userCan( File::DELETED_FILE, $user ) ) {
311 if ( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
312 throw new PermissionsError( 'suppressrevision' );
313 } else {
314 throw new PermissionsError( 'deletedtext' );
317 if ( !$user->matchEditToken( $this->token, $archiveName ) ) {
318 $lang = $this->getLanguage();
319 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
320 $this->targetObj->getText(),
321 $lang->userDate( $oimage->getTimestamp(), $user ),
322 $lang->userTime( $oimage->getTimestamp(), $user ) );
323 $this->getOutput()->addHTML(
324 Xml::openElement( 'form', array(
325 'method' => 'POST',
326 'action' => $this->getPageTitle()->getLocalURL( array(
327 'target' => $this->targetObj->getPrefixedDBkey(),
328 'file' => $archiveName,
329 'token' => $user->getEditToken( $archiveName ),
333 Xml::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
334 '</form>'
337 return;
339 $this->getOutput()->disable();
340 # We mustn't allow the output to be Squid cached, otherwise
341 # if an admin previews a deleted image, and it's cached, then
342 # a user without appropriate permissions can toddle off and
343 # nab the image, and Squid will serve it
344 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
345 $this->getRequest()->response()->header(
346 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate'
348 $this->getRequest()->response()->header( 'Pragma: no-cache' );
350 $key = $oimage->getStorageKey();
351 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
352 $repo->streamFile( $path );
356 * Get the list object for this request
357 * @return RevDelList
359 protected function getList() {
360 if ( is_null( $this->revDelList ) ) {
361 $this->revDelList = RevisionDeleter::createList(
362 $this->typeName, $this->getContext(), $this->targetObj, $this->ids
366 return $this->revDelList;
370 * Show a list of items that we will operate on, and show a form with checkboxes
371 * which will allow the user to choose new visibility settings.
373 protected function showForm() {
374 $userAllowed = true;
376 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
377 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels['selected'],
378 $this->getLanguage()->formatNum( count( $this->ids ) ), $this->targetObj->getPrefixedText() ) );
380 $this->getOutput()->addHTML( "<ul>" );
382 $numRevisions = 0;
383 // Live revisions...
384 $list = $this->getList();
385 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
386 for ( $list->reset(); $list->current(); $list->next() ) {
387 // @codingStandardsIgnoreEnd
388 $item = $list->current();
390 if ( !$item->canView() ) {
391 if ( !$this->submitClicked ) {
392 throw new PermissionsError( 'suppressrevision' );
394 $userAllowed = false;
397 $numRevisions++;
398 $this->getOutput()->addHTML( $item->getHTML() );
401 if ( !$numRevisions ) {
402 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
405 $this->getOutput()->addHTML( "</ul>" );
406 // Explanation text
407 $this->addUsageText();
409 // Normal sysops can always see what they did, but can't always change it
410 if ( !$userAllowed ) {
411 return;
414 // Show form if the user can submit
415 if ( $this->mIsAllowed ) {
416 $out = Xml::openElement( 'form', array( 'method' => 'post',
417 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
418 'id' => 'mw-revdel-form-revisions' ) ) .
419 Xml::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
420 $this->buildCheckBoxes() .
421 Xml::openElement( 'table' ) .
422 "<tr>\n" .
423 '<td class="mw-label">' .
424 Xml::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
425 '</td>' .
426 '<td class="mw-input">' .
427 Xml::listDropDown( 'wpRevDeleteReasonList',
428 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
429 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
430 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown'
432 '</td>' .
433 "</tr><tr>\n" .
434 '<td class="mw-label">' .
435 Xml::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
436 '</td>' .
437 '<td class="mw-input">' .
438 Xml::input(
439 'wpReason',
441 $this->otherReason,
442 array( 'id' => 'wpReason', 'maxlength' => 100 )
444 '</td>' .
445 "</tr><tr>\n" .
446 '<td></td>' .
447 '<td class="mw-submit">' .
448 Xml::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
449 array( 'name' => 'wpSubmit' ) ) .
450 '</td>' .
451 "</tr>\n" .
452 Xml::closeElement( 'table' ) .
453 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
454 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
455 Html::hidden( 'type', $this->typeName ) .
456 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
457 Xml::closeElement( 'fieldset' ) . "\n" .
458 Xml::closeElement( 'form' ) . "\n";
459 // Show link to edit the dropdown reasons
460 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
461 $title = Title::makeTitle( NS_MEDIAWIKI, 'Revdelete-reason-dropdown' );
462 $link = Linker::link(
463 $title,
464 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
465 array(),
466 array( 'action' => 'edit' )
468 $out .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
470 } else {
471 $out = '';
473 $this->getOutput()->addHTML( $out );
477 * Show some introductory text
478 * @todo FIXME: Wikimedia-specific policy text
480 protected function addUsageText() {
481 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
482 $this->getOutput()->wrapWikiMsg(
483 "<strong>$1</strong>\n$2", $this->typeLabels['text'],
484 'revdelete-text-others'
487 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
488 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
491 if ( $this->mIsAllowed ) {
492 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
497 * @return string HTML
499 protected function buildCheckBoxes() {
500 $html = '<table>';
501 // If there is just one item, use checkboxes
502 $list = $this->getList();
503 if ( $list->length() == 1 ) {
504 $list->reset();
505 $bitfield = $list->current()->getBits(); // existing field
507 if ( $this->submitClicked ) {
508 $bitfield = RevisionDeleter::extractBitfield( $this->extractBitParams(), $bitfield );
511 foreach ( $this->checks as $item ) {
512 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
513 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
514 list( $message, $name, $field ) = $item;
515 $innerHTML = Xml::checkLabel(
516 $this->msg( $message )->text(),
517 $name,
518 $name,
519 $bitfield & $field
522 if ( $field == Revision::DELETED_RESTRICTED ) {
523 $innerHTML = "<b>$innerHTML</b>";
526 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
527 $html .= "<tr>$line</tr>\n";
529 } else {
530 // Otherwise, use tri-state radios
531 $html .= '<tr>';
532 $html .= '<th class="mw-revdel-checkbox">'
533 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
534 $html .= '<th class="mw-revdel-checkbox">'
535 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
536 $html .= '<th class="mw-revdel-checkbox">'
537 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
538 $html .= "<th></th></tr>\n";
539 foreach ( $this->checks as $item ) {
540 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
541 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
542 list( $message, $name, $field ) = $item;
543 // If there are several items, use third state by default...
544 if ( $this->submitClicked ) {
545 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
546 } else {
547 $selected = -1; // use existing field
549 $line = '<td class="mw-revdel-checkbox">' . Xml::radio( $name, -1, $selected == -1 ) . '</td>';
550 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 0, $selected == 0 ) . '</td>';
551 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 1, $selected == 1 ) . '</td>';
552 $label = $this->msg( $message )->escaped();
553 if ( $field == Revision::DELETED_RESTRICTED ) {
554 $label = "<b>$label</b>";
556 $line .= "<td>$label</td>";
557 $html .= "<tr>$line</tr>\n";
561 $html .= '</table>';
563 return $html;
567 * UI entry point for form submission.
568 * @throws PermissionsError
569 * @return bool
571 protected function submit() {
572 # Check edit token on submission
573 $token = $this->getRequest()->getVal( 'wpEditToken' );
574 if ( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
575 $this->getOutput()->addWikiMsg( 'sessionfailure' );
577 return false;
579 $bitParams = $this->extractBitParams();
580 // from dropdown
581 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
582 $comment = $listReason;
583 if ( $comment === 'other' ) {
584 $comment = $this->otherReason;
585 } elseif ( $this->otherReason !== '' ) {
586 // Entry from drop down menu + additional comment
587 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
588 . $this->otherReason;
590 # Can the user set this field?
591 if ( $bitParams[Revision::DELETED_RESTRICTED] == 1
592 && !$this->getUser()->isAllowed( 'suppressrevision' )
594 throw new PermissionsError( 'suppressrevision' );
596 # If the save went through, go to success message...
597 $status = $this->save( $bitParams, $comment, $this->targetObj );
598 if ( $status->isGood() ) {
599 $this->success();
601 return true;
602 } else {
603 # ...otherwise, bounce back to form...
604 $this->failure( $status );
607 return false;
611 * Report that the submit operation succeeded
613 protected function success() {
614 // Messages: revdelete-success, logdelete-success
615 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
616 $this->getOutput()->wrapWikiMsg(
617 "<span class=\"success\">\n$1\n</span>",
618 $this->typeLabels['success']
620 $this->wasSaved = true;
621 $this->revDelList->reloadFromMaster();
622 $this->showForm();
626 * Report that the submit operation failed
627 * @param Status $status
629 protected function failure( $status ) {
630 // Messages: revdelete-failure, logdelete-failure
631 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
632 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels['failure'] ) );
633 $this->showForm();
637 * Put together an array that contains -1, 0, or the *_deleted const for each bit
639 * @return array
641 protected function extractBitParams() {
642 $bitfield = array();
643 foreach ( $this->checks as $item ) {
644 list( /* message */, $name, $field ) = $item;
645 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
646 if ( $val < -1 || $val > 1 ) {
647 $val = -1; // -1 for existing value
649 $bitfield[$field] = $val;
651 if ( !isset( $bitfield[Revision::DELETED_RESTRICTED] ) ) {
652 $bitfield[Revision::DELETED_RESTRICTED] = 0;
655 return $bitfield;
659 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
660 * @param int $bitfield
661 * @param string $reason
662 * @param Title $title
663 * @return Status
665 protected function save( $bitfield, $reason, $title ) {
666 return $this->getList()->setVisibility(
667 array( 'value' => $bitfield, 'comment' => $reason )
671 protected function getGroupName() {
672 return 'pagetools';