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