Support offsets in prefix searching
[mediawiki.git] / includes / specials / SpecialRevisiondelete.php
blob7eea71da5161fc5888fc5b4b5ce08a82e555e983
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
297 protected function tryShowFile( $archiveName ) {
298 $repo = RepoGroup::singleton()->getLocalRepo();
299 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
300 $oimage->load();
301 // Check if user is allowed to see this file
302 if ( !$oimage->exists() ) {
303 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
305 return;
307 $user = $this->getUser();
308 if ( !$oimage->userCan( File::DELETED_FILE, $user ) ) {
309 if ( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
310 throw new PermissionsError( 'suppressrevision' );
311 } else {
312 throw new PermissionsError( 'deletedtext' );
315 if ( !$user->matchEditToken( $this->token, $archiveName ) ) {
316 $lang = $this->getLanguage();
317 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
318 $this->targetObj->getText(),
319 $lang->userDate( $oimage->getTimestamp(), $user ),
320 $lang->userTime( $oimage->getTimestamp(), $user ) );
321 $this->getOutput()->addHTML(
322 Xml::openElement( 'form', array(
323 'method' => 'POST',
324 'action' => $this->getPageTitle()->getLocalURL( array(
325 'target' => $this->targetObj->getPrefixedDBkey(),
326 'file' => $archiveName,
327 'token' => $user->getEditToken( $archiveName ),
331 Xml::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
332 '</form>'
335 return;
337 $this->getOutput()->disable();
338 # We mustn't allow the output to be Squid cached, otherwise
339 # if an admin previews a deleted image, and it's cached, then
340 # a user without appropriate permissions can toddle off and
341 # nab the image, and Squid will serve it
342 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
343 $this->getRequest()->response()->header(
344 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate'
346 $this->getRequest()->response()->header( 'Pragma: no-cache' );
348 $key = $oimage->getStorageKey();
349 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
350 $repo->streamFile( $path );
354 * Get the list object for this request
355 * @return RevDelList
357 protected function getList() {
358 if ( is_null( $this->revDelList ) ) {
359 $this->revDelList = RevisionDeleter::createList(
360 $this->typeName, $this->getContext(), $this->targetObj, $this->ids
364 return $this->revDelList;
368 * Show a list of items that we will operate on, and show a form with checkboxes
369 * which will allow the user to choose new visibility settings.
371 protected function showForm() {
372 $userAllowed = true;
374 // Messages: revdelete-selected-text, revdelete-selected-file, logdelete-selected
375 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels['selected'],
376 $this->getLanguage()->formatNum( count( $this->ids ) ), $this->targetObj->getPrefixedText() ) );
378 $this->getOutput()->addHTML( "<ul>" );
380 $numRevisions = 0;
381 // Live revisions...
382 $list = $this->getList();
383 // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed
384 for ( $list->reset(); $list->current(); $list->next() ) {
385 // @codingStandardsIgnoreEnd
386 $item = $list->current();
388 if ( !$item->canView() ) {
389 if ( !$this->submitClicked ) {
390 throw new PermissionsError( 'suppressrevision' );
392 $userAllowed = false;
395 $numRevisions++;
396 $this->getOutput()->addHTML( $item->getHTML() );
399 if ( !$numRevisions ) {
400 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
403 $this->getOutput()->addHTML( "</ul>" );
404 // Explanation text
405 $this->addUsageText();
407 // Normal sysops can always see what they did, but can't always change it
408 if ( !$userAllowed ) {
409 return;
412 // Show form if the user can submit
413 if ( $this->mIsAllowed ) {
414 $out = Xml::openElement( 'form', array( 'method' => 'post',
415 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
416 'id' => 'mw-revdel-form-revisions' ) ) .
417 Xml::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
418 $this->buildCheckBoxes() .
419 Xml::openElement( 'table' ) .
420 "<tr>\n" .
421 '<td class="mw-label">' .
422 Xml::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
423 '</td>' .
424 '<td class="mw-input">' .
425 Xml::listDropDown( 'wpRevDeleteReasonList',
426 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
427 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
428 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown'
430 '</td>' .
431 "</tr><tr>\n" .
432 '<td class="mw-label">' .
433 Xml::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
434 '</td>' .
435 '<td class="mw-input">' .
436 Xml::input(
437 'wpReason',
439 $this->otherReason,
440 array( 'id' => 'wpReason', 'maxlength' => 100 )
442 '</td>' .
443 "</tr><tr>\n" .
444 '<td></td>' .
445 '<td class="mw-submit">' .
446 Xml::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
447 array( 'name' => 'wpSubmit' ) ) .
448 '</td>' .
449 "</tr>\n" .
450 Xml::closeElement( 'table' ) .
451 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
452 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
453 Html::hidden( 'type', $this->typeName ) .
454 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
455 Xml::closeElement( 'fieldset' ) . "\n" .
456 Xml::closeElement( 'form' ) . "\n";
457 // Show link to edit the dropdown reasons
458 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
459 $title = Title::makeTitle( NS_MEDIAWIKI, 'Revdelete-reason-dropdown' );
460 $link = Linker::link(
461 $title,
462 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
463 array(),
464 array( 'action' => 'edit' )
466 $out .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
468 } else {
469 $out = '';
471 $this->getOutput()->addHTML( $out );
475 * Show some introductory text
476 * @todo FIXME: Wikimedia-specific policy text
478 protected function addUsageText() {
479 // Messages: revdelete-text-text, revdelete-text-file, logdelete-text
480 $this->getOutput()->wrapWikiMsg(
481 "<strong>$1</strong>\n$2", $this->typeLabels['text'],
482 'revdelete-text-others'
485 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
486 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
489 if ( $this->mIsAllowed ) {
490 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
495 * @return string HTML
497 protected function buildCheckBoxes() {
498 $html = '<table>';
499 // If there is just one item, use checkboxes
500 $list = $this->getList();
501 if ( $list->length() == 1 ) {
502 $list->reset();
503 $bitfield = $list->current()->getBits(); // existing field
505 if ( $this->submitClicked ) {
506 $bitfield = RevisionDeleter::extractBitfield( $this->extractBitParams(), $bitfield );
509 foreach ( $this->checks as $item ) {
510 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
511 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
512 list( $message, $name, $field ) = $item;
513 $innerHTML = Xml::checkLabel(
514 $this->msg( $message )->text(),
515 $name,
516 $name,
517 $bitfield & $field
520 if ( $field == Revision::DELETED_RESTRICTED ) {
521 $innerHTML = "<b>$innerHTML</b>";
524 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
525 $html .= "<tr>$line</tr>\n";
527 } else {
528 // Otherwise, use tri-state radios
529 $html .= '<tr>';
530 $html .= '<th class="mw-revdel-checkbox">'
531 . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
532 $html .= '<th class="mw-revdel-checkbox">'
533 . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
534 $html .= '<th class="mw-revdel-checkbox">'
535 . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
536 $html .= "<th></th></tr>\n";
537 foreach ( $this->checks as $item ) {
538 // Messages: revdelete-hide-text, revdelete-hide-image, revdelete-hide-name,
539 // revdelete-hide-comment, revdelete-hide-user, revdelete-hide-restricted
540 list( $message, $name, $field ) = $item;
541 // If there are several items, use third state by default...
542 if ( $this->submitClicked ) {
543 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
544 } else {
545 $selected = -1; // use existing field
547 $line = '<td class="mw-revdel-checkbox">' . Xml::radio( $name, -1, $selected == -1 ) . '</td>';
548 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 0, $selected == 0 ) . '</td>';
549 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 1, $selected == 1 ) . '</td>';
550 $label = $this->msg( $message )->escaped();
551 if ( $field == Revision::DELETED_RESTRICTED ) {
552 $label = "<b>$label</b>";
554 $line .= "<td>$label</td>";
555 $html .= "<tr>$line</tr>\n";
559 $html .= '</table>';
561 return $html;
565 * UI entry point for form submission.
566 * @throws PermissionsError
567 * @return bool
569 protected function submit() {
570 # Check edit token on submission
571 $token = $this->getRequest()->getVal( 'wpEditToken' );
572 if ( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
573 $this->getOutput()->addWikiMsg( 'sessionfailure' );
575 return false;
577 $bitParams = $this->extractBitParams();
578 // from dropdown
579 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' );
580 $comment = $listReason;
581 if ( $comment === 'other' ) {
582 $comment = $this->otherReason;
583 } elseif ( $this->otherReason !== '' ) {
584 // Entry from drop down menu + additional comment
585 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
586 . $this->otherReason;
588 # Can the user set this field?
589 if ( $bitParams[Revision::DELETED_RESTRICTED] == 1
590 && !$this->getUser()->isAllowed( 'suppressrevision' )
592 throw new PermissionsError( 'suppressrevision' );
594 # If the save went through, go to success message...
595 $status = $this->save( $bitParams, $comment, $this->targetObj );
596 if ( $status->isGood() ) {
597 $this->success();
599 return true;
600 } else {
601 # ...otherwise, bounce back to form...
602 $this->failure( $status );
605 return false;
609 * Report that the submit operation succeeded
611 protected function success() {
612 // Messages: revdelete-success, logdelete-success
613 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
614 $this->getOutput()->wrapWikiMsg(
615 "<span class=\"success\">\n$1\n</span>",
616 $this->typeLabels['success']
618 $this->wasSaved = true;
619 $this->revDelList->reloadFromMaster();
620 $this->showForm();
624 * Report that the submit operation failed
625 * @param Status $status
627 protected function failure( $status ) {
628 // Messages: revdelete-failure, logdelete-failure
629 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
630 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels['failure'] ) );
631 $this->showForm();
635 * Put together an array that contains -1, 0, or the *_deleted const for each bit
637 * @return array
639 protected function extractBitParams() {
640 $bitfield = array();
641 foreach ( $this->checks as $item ) {
642 list( /* message */, $name, $field ) = $item;
643 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
644 if ( $val < -1 || $val > 1 ) {
645 $val = -1; // -1 for existing value
647 $bitfield[$field] = $val;
649 if ( !isset( $bitfield[Revision::DELETED_RESTRICTED] ) ) {
650 $bitfield[Revision::DELETED_RESTRICTED] = 0;
653 return $bitfield;
657 * Do the write operations. Simple wrapper for RevDel*List::setVisibility().
658 * @param int $bitfield
659 * @param string $reason
660 * @param Title $title
661 * @return Status
663 protected function save( $bitfield, $reason, $title ) {
664 return $this->getList()->setVisibility(
665 array( 'value' => $bitfield, 'comment' => $reason )
669 protected function getGroupName() {
670 return 'pagetools';