Merge "Add a 'revdelete-selected-file' message on Special:RevisionDelete"
[lhc/web/wiklou.git] / includes / specials / SpecialRevisiondelete.php
1 <?php
2 /**
3 * Implements Special:Revisiondelete
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * Special page allowing users with the appropriate permissions to view
26 * and hide revisions. Log items can also be hidden.
27 *
28 * @ingroup SpecialPage
29 */
30 class SpecialRevisionDelete extends UnlistedSpecialPage {
31 /** True if the submit button was clicked, and the form was posted */
32 var $submitClicked;
33
34 /** Target ID list */
35 var $ids;
36
37 /** Archive name, for reviewing deleted files */
38 var $archiveName;
39
40 /** Edit token for securing image views against XSS */
41 var $token;
42
43 /** Title object for target parameter */
44 var $targetObj;
45
46 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
47 var $typeName;
48
49 /** Array of checkbox specs (message, name, deletion bits) */
50 var $checks;
51
52 /** UI Labels about the current type */
53 var $typeLabels;
54
55 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
56 var $list;
57
58 /** Was the DB modified in this request */
59 protected $wasSaved = false;
60
61 /**
62 * UI labels for each type.
63 */
64 static $UILabels = array(
65 'revision' => array(
66 'check-label' => 'revdelete-hide-text',
67 'success' => 'revdelete-success',
68 'failure' => 'revdelete-failure',
69 'text' => 'revdelete-text-text',
70 'selected'=> 'revdelete-selected-text',
71 ),
72 'archive' => array(
73 'check-label' => 'revdelete-hide-text',
74 'success' => 'revdelete-success',
75 'failure' => 'revdelete-failure',
76 'text' => 'revdelete-text-text',
77 'selected'=> 'revdelete-selected-text',
78 ),
79 'oldimage' => array(
80 'check-label' => 'revdelete-hide-image',
81 'success' => 'revdelete-success',
82 'failure' => 'revdelete-failure',
83 'text' => 'revdelete-text-file',
84 'selected'=> 'revdelete-selected-file',
85 ),
86 'filearchive' => array(
87 'check-label' => 'revdelete-hide-image',
88 'success' => 'revdelete-success',
89 'failure' => 'revdelete-failure',
90 'text' => 'revdelete-text-file',
91 'selected'=> 'revdelete-selected-file',
92 ),
93 'logging' => array(
94 'check-label' => 'revdelete-hide-name',
95 'success' => 'logdelete-success',
96 'failure' => 'logdelete-failure',
97 'text' => 'logdelete-text',
98 'selected' => 'logdelete-selected',
99 ),
100 );
101
102 public function __construct() {
103 parent::__construct( 'Revisiondelete', 'deletedhistory' );
104 }
105
106 public function execute( $par ) {
107 $this->checkPermissions();
108 $this->checkReadOnly();
109
110 $output = $this->getOutput();
111 $user = $this->getUser();
112
113 $this->setHeaders();
114 $this->outputHeader();
115 $request = $this->getRequest();
116 $this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' );
117 # Handle our many different possible input types.
118 $ids = $request->getVal( 'ids' );
119 if ( !is_null( $ids ) ) {
120 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
121 $this->ids = explode( ',', $ids );
122 } else {
123 # Array input
124 $this->ids = array_keys( $request->getArray( 'ids', array() ) );
125 }
126 // $this->ids = array_map( 'intval', $this->ids );
127 $this->ids = array_unique( array_filter( $this->ids ) );
128
129 if ( $request->getVal( 'action' ) == 'historysubmit' || $request->getVal( 'action' ) == 'revisiondelete' ) {
130 // For show/hide form submission from history page
131 // Since we are access through index.php?title=XXX&action=historysubmit
132 // getFullTitle() will contain the target title and not our title
133 $this->targetObj = $this->getFullTitle();
134 $this->typeName = 'revision';
135 } else {
136 $this->typeName = $request->getVal( 'type' );
137 $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
138 }
139
140 # For reviewing deleted files...
141 $this->archiveName = $request->getVal( 'file' );
142 $this->token = $request->getVal( 'token' );
143 if ( $this->archiveName && $this->targetObj ) {
144 $this->tryShowFile( $this->archiveName );
145
146 return;
147 }
148
149 $this->typeName = RevisionDeleter::getCanonicalTypeName( $this->typeName );
150
151 # No targets?
152 if ( !$this->typeName || count( $this->ids ) == 0 ) {
153 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
154 }
155 $this->typeLabels = self::$UILabels[$this->typeName];
156 $this->mIsAllowed = $user->isAllowed( RevisionDeleter::getRestriction( $this->typeName ) );
157
158 # Allow the list type to adjust the passed target
159 $this->targetObj = RevisionDeleter::suggestTarget( $this->typeName, $this->targetObj, $this->ids );
160
161 $this->otherReason = $request->getVal( 'wpReason' );
162 # We need a target page!
163 if ( is_null( $this->targetObj ) ) {
164 $output->addWikiMsg( 'undelete-header' );
165
166 return;
167 }
168 # Give a link to the logs/hist for this page
169 $this->showConvenienceLinks();
170
171 # Initialise checkboxes
172 $this->checks = array(
173 array( $this->typeLabels['check-label'], 'wpHidePrimary',
174 RevisionDeleter::getRevdelConstant( $this->typeName )
175 ),
176 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
177 array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER )
178 );
179 if ( $user->isAllowed( 'suppressrevision' ) ) {
180 $this->checks[] = array( 'revdelete-hide-restricted',
181 'wpHideRestricted', Revision::DELETED_RESTRICTED );
182 }
183
184 # Either submit or create our form
185 if ( $this->mIsAllowed && $this->submitClicked ) {
186 $this->submit( $request );
187 } else {
188 $this->showForm();
189 }
190
191 $qc = $this->getLogQueryCond();
192 # Show relevant lines from the deletion log
193 $deleteLogPage = new LogPage( 'delete' );
194 $output->addHTML( "<h2>" . $deleteLogPage->getName()->escaped() . "</h2>\n" );
195 LogEventsList::showLogExtract(
196 $output,
197 'delete',
198 $this->targetObj,
199 '', /* user */
200 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved )
201 );
202 # Show relevant lines from the suppression log
203 if ( $user->isAllowed( 'suppressionlog' ) ) {
204 $suppressLogPage = new LogPage( 'suppress' );
205 $output->addHTML( "<h2>" . $suppressLogPage->getName()->escaped() . "</h2>\n" );
206 LogEventsList::showLogExtract(
207 $output,
208 'suppress',
209 $this->targetObj,
210 '',
211 array( 'lim' => 25, 'conds' => $qc, 'useMaster' => $this->wasSaved )
212 );
213 }
214 }
215
216 /**
217 * Show some useful links in the subtitle
218 */
219 protected function showConvenienceLinks() {
220 # Give a link to the logs/hist for this page
221 if ( $this->targetObj ) {
222 // Also set header tabs to be for the target.
223 $this->getSkin()->setRelevantTitle( $this->targetObj );
224
225 $links = array();
226 $links[] = Linker::linkKnown(
227 SpecialPage::getTitleFor( 'Log' ),
228 $this->msg( 'viewpagelogs' )->escaped(),
229 array(),
230 array( 'page' => $this->targetObj->getPrefixedText() )
231 );
232 if ( !$this->targetObj->isSpecialPage() ) {
233 # Give a link to the page history
234 $links[] = Linker::linkKnown(
235 $this->targetObj,
236 $this->msg( 'pagehist' )->escaped(),
237 array(),
238 array( 'action' => 'history' )
239 );
240 # Link to deleted edits
241 if ( $this->getUser()->isAllowed( 'undelete' ) ) {
242 $undelete = SpecialPage::getTitleFor( 'Undelete' );
243 $links[] = Linker::linkKnown(
244 $undelete,
245 $this->msg( 'deletedhist' )->escaped(),
246 array(),
247 array( 'target' => $this->targetObj->getPrefixedDBkey() )
248 );
249 }
250 }
251 # Logs themselves don't have histories or archived revisions
252 $this->getOutput()->addSubtitle( $this->getLanguage()->pipeList( $links ) );
253 }
254 }
255
256 /**
257 * Get the condition used for fetching log snippets
258 * @return array
259 */
260 protected function getLogQueryCond() {
261 $conds = array();
262 // Revision delete logs for these item
263 $conds['log_type'] = array( 'delete', 'suppress' );
264 $conds['log_action'] = $this->getList()->getLogAction();
265 $conds['ls_field'] = RevisionDeleter::getRelationType( $this->typeName );
266 $conds['ls_value'] = $this->ids;
267
268 return $conds;
269 }
270
271 /**
272 * Show a deleted file version requested by the visitor.
273 * TODO Mostly copied from Special:Undelete. Refactor.
274 */
275 protected function tryShowFile( $archiveName ) {
276 $repo = RepoGroup::singleton()->getLocalRepo();
277 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
278 $oimage->load();
279 // Check if user is allowed to see this file
280 if ( !$oimage->exists() ) {
281 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
282
283 return;
284 }
285 $user = $this->getUser();
286 if ( !$oimage->userCan( File::DELETED_FILE, $user ) ) {
287 if ( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
288 throw new PermissionsError( 'suppressrevision' );
289 } else {
290 throw new PermissionsError( 'deletedtext' );
291 }
292 }
293 if ( !$user->matchEditToken( $this->token, $archiveName ) ) {
294 $lang = $this->getLanguage();
295 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
296 $this->targetObj->getText(),
297 $lang->userDate( $oimage->getTimestamp(), $user ),
298 $lang->userTime( $oimage->getTimestamp(), $user ) );
299 $this->getOutput()->addHTML(
300 Xml::openElement( 'form', array(
301 'method' => 'POST',
302 'action' => $this->getPageTitle()->getLocalURL( array(
303 'target' => $this->targetObj->getPrefixedDBkey(),
304 'file' => $archiveName,
305 'token' => $user->getEditToken( $archiveName ),
306 ) )
307 )
308 ) .
309 Xml::submitButton( $this->msg( 'revdelete-show-file-submit' )->text() ) .
310 '</form>'
311 );
312
313 return;
314 }
315 $this->getOutput()->disable();
316 # We mustn't allow the output to be Squid cached, otherwise
317 # if an admin previews a deleted image, and it's cached, then
318 # a user without appropriate permissions can toddle off and
319 # nab the image, and Squid will serve it
320 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
321 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
322 $this->getRequest()->response()->header( 'Pragma: no-cache' );
323
324 $key = $oimage->getStorageKey();
325 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
326 $repo->streamFile( $path );
327 }
328
329 /**
330 * Get the list object for this request
331 */
332 protected function getList() {
333 if ( is_null( $this->list ) ) {
334 $this->list = RevisionDeleter::createList(
335 $this->typeName, $this->getContext(), $this->targetObj, $this->ids
336 );
337 }
338
339 return $this->list;
340 }
341
342 /**
343 * Show a list of items that we will operate on, and show a form with checkboxes
344 * which will allow the user to choose new visibility settings.
345 */
346 protected function showForm() {
347 $userAllowed = true;
348
349 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>", array( $this->typeLabels['selected'],
350 $this->getLanguage()->formatNum( count( $this->ids ) ), $this->targetObj->getPrefixedText() ) );
351
352 $this->getOutput()->addHTML( "<ul>" );
353
354 $numRevisions = 0;
355 // Live revisions...
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 throw new PermissionsError( 'suppressrevision' );
362 }
363 $userAllowed = false;
364 }
365 $numRevisions++;
366 $this->getOutput()->addHTML( $item->getHTML() );
367 }
368
369 if ( !$numRevisions ) {
370 throw new ErrorPageError( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
371 }
372
373 $this->getOutput()->addHTML( "</ul>" );
374 // Explanation text
375 $this->addUsageText();
376
377 // Normal sysops can always see what they did, but can't always change it
378 if ( !$userAllowed ) {
379 return;
380 }
381
382 // Show form if the user can submit
383 if ( $this->mIsAllowed ) {
384 $out = Xml::openElement( 'form', array( 'method' => 'post',
385 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ),
386 'id' => 'mw-revdel-form-revisions' ) ) .
387 Xml::fieldset( $this->msg( 'revdelete-legend' )->text() ) .
388 $this->buildCheckBoxes() .
389 Xml::openElement( 'table' ) .
390 "<tr>\n" .
391 '<td class="mw-label">' .
392 Xml::label( $this->msg( 'revdelete-log' )->text(), 'wpRevDeleteReasonList' ) .
393 '</td>' .
394 '<td class="mw-input">' .
395 Xml::listDropDown( 'wpRevDeleteReasonList',
396 $this->msg( 'revdelete-reason-dropdown' )->inContentLanguage()->text(),
397 $this->msg( 'revdelete-reasonotherlist' )->inContentLanguage()->text(),
398 $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ), 'wpReasonDropDown', 1
399 ) .
400 '</td>' .
401 "</tr><tr>\n" .
402 '<td class="mw-label">' .
403 Xml::label( $this->msg( 'revdelete-otherreason' )->text(), 'wpReason' ) .
404 '</td>' .
405 '<td class="mw-input">' .
406 Xml::input( 'wpReason', 60, $this->otherReason, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
407 '</td>' .
408 "</tr><tr>\n" .
409 '<td></td>' .
410 '<td class="mw-submit">' .
411 Xml::submitButton( $this->msg( 'revdelete-submit', $numRevisions )->text(),
412 array( 'name' => 'wpSubmit' ) ) .
413 '</td>' .
414 "</tr>\n" .
415 Xml::closeElement( 'table' ) .
416 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
417 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
418 Html::hidden( 'type', $this->typeName ) .
419 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
420 Xml::closeElement( 'fieldset' ) . "\n";
421 } else {
422 $out = '';
423 }
424 if ( $this->mIsAllowed ) {
425 $out .= Xml::closeElement( 'form' ) . "\n";
426 // Show link to edit the dropdown reasons
427 if ( $this->getUser()->isAllowed( 'editinterface' ) ) {
428 $title = Title::makeTitle( NS_MEDIAWIKI, 'Revdelete-reason-dropdown' );
429 $link = Linker::link(
430 $title,
431 $this->msg( 'revdelete-edit-reasonlist' )->escaped(),
432 array(),
433 array( 'action' => 'edit' )
434 );
435 $out .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
436 }
437 }
438 $this->getOutput()->addHTML( $out );
439 }
440
441 /**
442 * Show some introductory text
443 * @todo FIXME: Wikimedia-specific policy text
444 */
445 protected function addUsageText() {
446 $this->getOutput()->wrapWikiMsg( "<strong>$1</strong>\n$2", $this->typeLabels['text'], 'revdelete-text-others' );
447 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
448 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
449 }
450 if ( $this->mIsAllowed ) {
451 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
452 }
453 }
454
455 /**
456 * @return String: HTML
457 */
458 protected function buildCheckBoxes() {
459 $html = '<table>';
460 // If there is just one item, use checkboxes
461 $list = $this->getList();
462 if ( $list->length() == 1 ) {
463 $list->reset();
464 $bitfield = $list->current()->getBits(); // existing field
465 if ( $this->submitClicked ) {
466 $bitfield = RevisionDeleter::extractBitfield( $this->extractBitParams(), $bitfield );
467 }
468 foreach ( $this->checks as $item ) {
469 list( $message, $name, $field ) = $item;
470 $innerHTML = Xml::checkLabel( $this->msg( $message )->text(), $name, $name, $bitfield & $field );
471 if ( $field == Revision::DELETED_RESTRICTED ) {
472 $innerHTML = "<b>$innerHTML</b>";
473 }
474 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
475 $html .= "<tr>$line</tr>\n";
476 }
477 } else {
478 // Otherwise, use tri-state radios
479 $html .= '<tr>';
480 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-same' )->escaped() . '</th>';
481 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-unset' )->escaped() . '</th>';
482 $html .= '<th class="mw-revdel-checkbox">' . $this->msg( 'revdelete-radio-set' )->escaped() . '</th>';
483 $html .= "<th></th></tr>\n";
484 foreach ( $this->checks as $item ) {
485 list( $message, $name, $field ) = $item;
486 // If there are several items, use third state by default...
487 if ( $this->submitClicked ) {
488 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
489 } else {
490 $selected = -1; // use existing field
491 }
492 $line = '<td class="mw-revdel-checkbox">' . Xml::radio( $name, -1, $selected == -1 ) . '</td>';
493 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 0, $selected == 0 ) . '</td>';
494 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 1, $selected == 1 ) . '</td>';
495 $label = $this->msg( $message )->escaped();
496 if ( $field == Revision::DELETED_RESTRICTED ) {
497 $label = "<b>$label</b>";
498 }
499 $line .= "<td>$label</td>";
500 $html .= "<tr>$line</tr>\n";
501 }
502 }
503
504 $html .= '</table>';
505
506 return $html;
507 }
508
509 /**
510 * UI entry point for form submission.
511 * @throws PermissionsError
512 * @return bool
513 */
514 protected function submit() {
515 # Check edit token on submission
516 $token = $this->getRequest()->getVal( 'wpEditToken' );
517 if ( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
518 $this->getOutput()->addWikiMsg( 'sessionfailure' );
519
520 return false;
521 }
522 $bitParams = $this->extractBitParams();
523 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
524 $comment = $listReason;
525 if ( $comment != 'other' && $this->otherReason != '' ) {
526 // Entry from drop down menu + additional comment
527 $comment .= $this->msg( 'colon-separator' )->inContentLanguage()->text() . $this->otherReason;
528 } elseif ( $comment == 'other' ) {
529 $comment = $this->otherReason;
530 }
531 # Can the user set this field?
532 if ( $bitParams[Revision::DELETED_RESTRICTED] == 1 && !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
533 throw new PermissionsError( 'suppressrevision' );
534 }
535 # If the save went through, go to success message...
536 $status = $this->save( $bitParams, $comment, $this->targetObj );
537 if ( $status->isGood() ) {
538 $this->success();
539
540 return true;
541 } else {
542 # ...otherwise, bounce back to form...
543 $this->failure( $status );
544 }
545
546 return false;
547 }
548
549 /**
550 * Report that the submit operation succeeded
551 */
552 protected function success() {
553 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
554 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeLabels['success'] );
555 $this->wasSaved = true;
556 $this->list->reloadFromMaster();
557 $this->showForm();
558 }
559
560 /**
561 * Report that the submit operation failed
562 */
563 protected function failure( $status ) {
564 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
565 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeLabels['failure'] ) );
566 $this->showForm();
567 }
568
569 /**
570 * Put together an array that contains -1, 0, or the *_deleted const for each bit
571 *
572 * @return array
573 */
574 protected function extractBitParams() {
575 $bitfield = array();
576 foreach ( $this->checks as $item ) {
577 list( /* message */, $name, $field ) = $item;
578 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
579 if ( $val < -1 || $val > 1 ) {
580 $val = -1; // -1 for existing value
581 }
582 $bitfield[$field] = $val;
583 }
584 if ( !isset( $bitfield[Revision::DELETED_RESTRICTED] ) ) {
585 $bitfield[Revision::DELETED_RESTRICTED] = 0;
586 }
587
588 return $bitfield;
589 }
590
591 /**
592 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
593 * @param $bitfield
594 * @param $reason
595 * @param $title
596 * @return
597 */
598 protected function save( $bitfield, $reason, $title ) {
599 return $this->getList()->setVisibility(
600 array( 'value' => $bitfield, 'comment' => $reason )
601 );
602 }
603
604 protected function getGroupName() {
605 return 'pagetools';
606 }
607 }