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