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