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