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