d307e09b2afc3895dfbda7f8017042e6c4a64391
[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' ) {
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::getName( '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::getName( '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 wfMsgHtml( 'viewpagelogs' ),
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 wfMsgHtml( 'pagehist' ),
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 wfMsgHtml( 'deletedhist' ),
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 */
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 return $conds;
263 }
264
265 /**
266 * Show a deleted file version requested by the visitor.
267 * TODO Mostly copied from Special:Undelete. Refactor.
268 */
269 protected function tryShowFile( $archiveName ) {
270 $repo = RepoGroup::singleton()->getLocalRepo();
271 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
272 $oimage->load();
273 // Check if user is allowed to see this file
274 if ( !$oimage->exists() ) {
275 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
276 return;
277 }
278 if( !$oimage->userCan( File::DELETED_FILE, $this->getUser() ) ) {
279 if( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
280 $this->getOutput()->permissionRequired( 'suppressrevision' );
281 } else {
282 $this->getOutput()->permissionRequired( 'deletedtext' );
283 }
284 return;
285 }
286 if ( !$this->getUser()->matchEditToken( $this->token, $archiveName ) ) {
287 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
288 $this->targetObj->getText(),
289 $this->getLanguage()->date( $oimage->getTimestamp() ),
290 $this->getLanguage()->time( $oimage->getTimestamp() ) );
291 $this->getOutput()->addHTML(
292 Xml::openElement( 'form', array(
293 'method' => 'POST',
294 'action' => $this->getTitle()->getLocalUrl(
295 'target=' . urlencode( $oimage->getName() ) .
296 '&file=' . urlencode( $archiveName ) .
297 '&token=' . urlencode( $this->getUser()->getEditToken( $archiveName ) ) )
298 )
299 ) .
300 Xml::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
301 '</form>'
302 );
303 return;
304 }
305 $this->getOutput()->disable();
306 # We mustn't allow the output to be Squid cached, otherwise
307 # if an admin previews a deleted image, and it's cached, then
308 # a user without appropriate permissions can toddle off and
309 # nab the image, and Squid will serve it
310 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
311 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
312 $this->getRequest()->response()->header( 'Pragma: no-cache' );
313
314 $key = $oimage->getStorageKey();
315 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
316 StreamFile::stream( $path );
317 }
318
319 /**
320 * Get the list object for this request
321 */
322 protected function getList() {
323 if ( is_null( $this->list ) ) {
324 $class = $this->typeInfo['list-class'];
325 $this->list = new $class( $this->getContext(), $this->targetObj, $this->ids );
326 }
327 return $this->list;
328 }
329
330 /**
331 * Show a list of items that we will operate on, and show a form with checkboxes
332 * which will allow the user to choose new visibility settings.
333 */
334 protected function showForm() {
335 $UserAllowed = true;
336
337 if ( $this->typeName == 'logging' ) {
338 $this->getOutput()->addWikiMsg( 'logdelete-selected', $this->getLanguage()->formatNum( count($this->ids) ) );
339 } else {
340 $this->getOutput()->addWikiMsg( 'revdelete-selected',
341 $this->targetObj->getPrefixedText(), count( $this->ids ) );
342 }
343
344 $this->getOutput()->addHTML( "<ul>" );
345
346 $numRevisions = 0;
347 // Live revisions...
348 $list = $this->getList();
349 for ( $list->reset(); $list->current(); $list->next() ) {
350 $item = $list->current();
351 if ( !$item->canView() ) {
352 if( !$this->submitClicked ) {
353 $this->getOutput()->permissionRequired( 'suppressrevision' );
354 return;
355 }
356 $UserAllowed = false;
357 }
358 $numRevisions++;
359 $this->getOutput()->addHTML( $item->getHTML() );
360 }
361
362 if( !$numRevisions ) {
363 $this->getOutput()->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
364 return;
365 }
366
367 $this->getOutput()->addHTML( "</ul>" );
368 // Explanation text
369 $this->addUsageText();
370
371 // Normal sysops can always see what they did, but can't always change it
372 if( !$UserAllowed ) return;
373
374 // Show form if the user can submit
375 if( $this->mIsAllowed ) {
376 $out = Xml::openElement( 'form', array( 'method' => 'post',
377 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
378 'id' => 'mw-revdel-form-revisions' ) ) .
379 Xml::fieldset( wfMsg( 'revdelete-legend' ) ) .
380 $this->buildCheckBoxes() .
381 Xml::openElement( 'table' ) .
382 "<tr>\n" .
383 '<td class="mw-label">' .
384 Xml::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
385 '</td>' .
386 '<td class="mw-input">' .
387 Xml::listDropDown( 'wpRevDeleteReasonList',
388 wfMsgForContent( 'revdelete-reason-dropdown' ),
389 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
390 ) .
391 '</td>' .
392 "</tr><tr>\n" .
393 '<td class="mw-label">' .
394 Xml::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
395 '</td>' .
396 '<td class="mw-input">' .
397 Xml::input( 'wpReason', 60, $this->otherReason, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
398 '</td>' .
399 "</tr><tr>\n" .
400 '<td></td>' .
401 '<td class="mw-submit">' .
402 Xml::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
403 array( 'name' => 'wpSubmit' ) ) .
404 '</td>' .
405 "</tr>\n" .
406 Xml::closeElement( 'table' ) .
407 Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() ) .
408 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
409 Html::hidden( 'type', $this->typeName ) .
410 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
411 Xml::closeElement( 'fieldset' ) . "\n";
412 } else {
413 $out = '';
414 }
415 if( $this->mIsAllowed ) {
416 $out .= Xml::closeElement( 'form' ) . "\n";
417 // Show link to edit the dropdown reasons
418 if( $this->getUser()->isAllowed( 'editinterface' ) ) {
419 $title = Title::makeTitle( NS_MEDIAWIKI, 'revdelete-reason-dropdown' );
420 $link = Linker::link(
421 $title,
422 wfMsgHtml( 'revdelete-edit-reasonlist' ),
423 array(),
424 array( 'action' => 'edit' )
425 );
426 $out .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
427 }
428 }
429 $this->getOutput()->addHTML( $out );
430 }
431
432 /**
433 * Show some introductory text
434 * @todo FIXME: Wikimedia-specific policy text
435 */
436 protected function addUsageText() {
437 $this->getOutput()->addWikiMsg( 'revdelete-text' );
438 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
439 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
440 }
441 if( $this->mIsAllowed ) {
442 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
443 }
444 }
445
446 /**
447 * @return String: HTML
448 */
449 protected function buildCheckBoxes() {
450 $html = '<table>';
451 // If there is just one item, use checkboxes
452 $list = $this->getList();
453 if( $list->length() == 1 ) {
454 $list->reset();
455 $bitfield = $list->current()->getBits(); // existing field
456 if( $this->submitClicked ) {
457 $bitfield = $this->extractBitfield( $this->extractBitParams(), $bitfield );
458 }
459 foreach( $this->checks as $item ) {
460 list( $message, $name, $field ) = $item;
461 $innerHTML = Xml::checkLabel( wfMsg($message), $name, $name, $bitfield & $field );
462 if( $field == Revision::DELETED_RESTRICTED )
463 $innerHTML = "<b>$innerHTML</b>";
464 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
465 $html .= "<tr>$line</tr>\n";
466 }
467 // Otherwise, use tri-state radios
468 } else {
469 $html .= '<tr>';
470 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-same').'</th>';
471 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
472 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</th>';
473 $html .= "<th></th></tr>\n";
474 foreach( $this->checks as $item ) {
475 list( $message, $name, $field ) = $item;
476 // If there are several items, use third state by default...
477 if( $this->submitClicked ) {
478 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
479 } else {
480 $selected = -1; // use existing field
481 }
482 $line = '<td class="mw-revdel-checkbox">' . Xml::radio( $name, -1, $selected == -1 ) . '</td>';
483 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 0, $selected == 0 ) . '</td>';
484 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 1, $selected == 1 ) . '</td>';
485 $label = wfMsgHtml($message);
486 if( $field == Revision::DELETED_RESTRICTED ) {
487 $label = "<b>$label</b>";
488 }
489 $line .= "<td>$label</td>";
490 $html .= "<tr>$line</tr>\n";
491 }
492 }
493
494 $html .= '</table>';
495 return $html;
496 }
497
498 /**
499 * UI entry point for form submission.
500 */
501 protected function submit() {
502 # Check edit token on submission
503 $token = $this->getRequest()->getVal('wpEditToken');
504 if( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
505 $this->getOutput()->addWikiMsg( 'sessionfailure' );
506 return false;
507 }
508 $bitParams = $this->extractBitParams();
509 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
510 $comment = $listReason;
511 if( $comment != 'other' && $this->otherReason != '' ) {
512 // Entry from drop down menu + additional comment
513 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason;
514 } elseif( $comment == 'other' ) {
515 $comment = $this->otherReason;
516 }
517 # Can the user set this field?
518 if( $bitParams[Revision::DELETED_RESTRICTED]==1 && !$this->getUser()->isAllowed('suppressrevision') ) {
519 $this->getOutput()->permissionRequired( 'suppressrevision' );
520 return false;
521 }
522 # If the save went through, go to success message...
523 $status = $this->save( $bitParams, $comment, $this->targetObj );
524 if ( $status->isGood() ) {
525 $this->success();
526 return true;
527 # ...otherwise, bounce back to form...
528 } else {
529 $this->failure( $status );
530 }
531 return false;
532 }
533
534 /**
535 * Report that the submit operation succeeded
536 */
537 protected function success() {
538 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
539 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo['success'] );
540 $this->list->reloadFromMaster();
541 $this->showForm();
542 }
543
544 /**
545 * Report that the submit operation failed
546 */
547 protected function failure( $status ) {
548 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
549 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeInfo['failure'] ) );
550 $this->showForm();
551 }
552
553 /**
554 * Put together an array that contains -1, 0, or the *_deleted const for each bit
555 *
556 * @return array
557 */
558 protected function extractBitParams() {
559 $bitfield = array();
560 foreach( $this->checks as $item ) {
561 list( /* message */ , $name, $field ) = $item;
562 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
563 if( $val < -1 || $val > 1) {
564 $val = -1; // -1 for existing value
565 }
566 $bitfield[$field] = $val;
567 }
568 if( !isset($bitfield[Revision::DELETED_RESTRICTED]) ) {
569 $bitfield[Revision::DELETED_RESTRICTED] = 0;
570 }
571 return $bitfield;
572 }
573
574 /**
575 * Put together a rev_deleted bitfield
576 * @param $bitPars array extractBitParams() params
577 * @param $oldfield int current bitfield
578 * @return array
579 */
580 public static function extractBitfield( $bitPars, $oldfield ) {
581 // Build the actual new rev_deleted bitfield
582 $newBits = 0;
583 foreach( $bitPars as $const => $val ) {
584 if( $val == 1 ) {
585 $newBits |= $const; // $const is the *_deleted const
586 } elseif( $val == -1 ) {
587 $newBits |= ($oldfield & $const); // use existing
588 }
589 }
590 return $newBits;
591 }
592
593 /**
594 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
595 */
596 protected function save( $bitfield, $reason, $title ) {
597 return $this->getList()->setVisibility(
598 array( 'value' => $bitfield, 'comment' => $reason )
599 );
600 }
601 }
602