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