Per Nikerabbit's comment on r100621:
[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 $output = $this->getOutput();
115 $user = $this->getUser();
116
117 if( !$user->isAllowed( 'deletedhistory' ) ) {
118 throw new PermissionsError( 'deletedhistory' );
119 } elseif( wfReadOnly() ) {
120 throw new ReadOnlyError;
121 }
122
123 $this->mIsAllowed = $user->isAllowed('deleterevision'); // for changes
124 $this->setHeaders();
125 $this->outputHeader();
126 $request = $this->getRequest();
127 $this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' );
128 # Handle our many different possible input types.
129 $ids = $request->getVal( 'ids' );
130 if ( !is_null( $ids ) ) {
131 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
132 $this->ids = explode( ',', $ids );
133 } else {
134 # Array input
135 $this->ids = array_keys( $request->getArray('ids',array()) );
136 }
137 // $this->ids = array_map( 'intval', $this->ids );
138 $this->ids = array_unique( array_filter( $this->ids ) );
139
140 if ( $request->getVal( 'action' ) == 'historysubmit' ) {
141 // For show/hide form submission from history page
142 // Since we are access through index.php?title=XXX&action=historysubmit
143 // getFullTitle() will contain the target title and not our title
144 $this->targetObj = $this->getFullTitle();
145 $this->typeName = 'revision';
146 } else {
147 $this->typeName = $request->getVal( 'type' );
148 $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
149 }
150
151 # For reviewing deleted files...
152 $this->archiveName = $request->getVal( 'file' );
153 $this->token = $request->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 $output->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 = $request->getVal( 'wpReason' );
179 # We need a target page!
180 if( is_null($this->targetObj) ) {
181 $output->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( $user->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( $request );
201 } else {
202 $this->showForm();
203 }
204
205 $qc = $this->getLogQueryCond();
206 # Show relevant lines from the deletion log
207 $output->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
208 LogEventsList::showLogExtract( $output, 'delete',
209 $this->targetObj, '', array( 'lim' => 25, 'conds' => $qc ) );
210 # Show relevant lines from the suppression log
211 if( $user->isAllowed( 'suppressionlog' ) ) {
212 $output->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'suppress' ) ) . "</h2>\n" );
213 LogEventsList::showLogExtract( $output, 'suppress',
214 $this->targetObj, '', array( 'lim' => 25, 'conds' => $qc ) );
215 }
216 }
217
218 /**
219 * Show some useful links in the subtitle
220 */
221 protected function showConvenienceLinks() {
222 # Give a link to the logs/hist for this page
223 if( $this->targetObj ) {
224 $links = array();
225 $links[] = Linker::linkKnown(
226 SpecialPage::getTitleFor( 'Log' ),
227 wfMsgHtml( 'viewpagelogs' ),
228 array(),
229 array( 'page' => $this->targetObj->getPrefixedText() )
230 );
231 if ( $this->targetObj->getNamespace() != NS_SPECIAL ) {
232 # Give a link to the page history
233 $links[] = Linker::linkKnown(
234 $this->targetObj,
235 wfMsgHtml( 'pagehist' ),
236 array(),
237 array( 'action' => 'history' )
238 );
239 # Link to deleted edits
240 if( $this->getUser()->isAllowed('undelete') ) {
241 $undelete = SpecialPage::getTitleFor( 'Undelete' );
242 $links[] = Linker::linkKnown(
243 $undelete,
244 wfMsgHtml( 'deletedhist' ),
245 array(),
246 array( 'target' => $this->targetObj->getPrefixedDBkey() )
247 );
248 }
249 }
250 # Logs themselves don't have histories or archived revisions
251 $this->getOutput()->setSubtitle( '<p>' . $this->getLang()->pipeList( $links ) . '</p>' );
252 }
253 }
254
255 /**
256 * Get the condition used for fetching log snippets
257 */
258 protected function getLogQueryCond() {
259 $conds = array();
260 // Revision delete logs for these item
261 $conds['log_type'] = array( 'delete', 'suppress' );
262 $conds['log_action'] = $this->getList()->getLogAction();
263 $conds['ls_field'] = RevisionDeleter::getRelationType( $this->typeName );
264 $conds['ls_value'] = $this->ids;
265 return $conds;
266 }
267
268 /**
269 * Show a deleted file version requested by the visitor.
270 * TODO Mostly copied from Special:Undelete. Refactor.
271 */
272 protected function tryShowFile( $archiveName ) {
273 $repo = RepoGroup::singleton()->getLocalRepo();
274 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
275 $oimage->load();
276 // Check if user is allowed to see this file
277 if ( !$oimage->exists() ) {
278 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
279 return;
280 }
281 if( !$oimage->userCan( File::DELETED_FILE, $this->getUser() ) ) {
282 if( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
283 $this->getOutput()->permissionRequired( 'suppressrevision' );
284 } else {
285 $this->getOutput()->permissionRequired( 'deletedtext' );
286 }
287 return;
288 }
289 if ( !$this->getUser()->matchEditToken( $this->token, $archiveName ) ) {
290 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
291 $this->targetObj->getText(),
292 $this->getLang()->date( $oimage->getTimestamp() ),
293 $this->getLang()->time( $oimage->getTimestamp() ) );
294 $this->getOutput()->addHTML(
295 Xml::openElement( 'form', array(
296 'method' => 'POST',
297 'action' => $this->getTitle()->getLocalUrl(
298 'target=' . urlencode( $oimage->getName() ) .
299 '&file=' . urlencode( $archiveName ) .
300 '&token=' . urlencode( $this->getUser()->editToken( $archiveName ) ) )
301 )
302 ) .
303 Xml::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
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 StreamFile::stream( $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->getLang()->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( wfMsg( 'revdelete-legend' ) ) .
383 $this->buildCheckBoxes() .
384 Xml::openElement( 'table' ) .
385 "<tr>\n" .
386 '<td class="mw-label">' .
387 Xml::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
388 '</td>' .
389 '<td class="mw-input">' .
390 Xml::listDropDown( 'wpRevDeleteReasonList',
391 wfMsgForContent( 'revdelete-reason-dropdown' ),
392 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
393 ) .
394 '</td>' .
395 "</tr><tr>\n" .
396 '<td class="mw-label">' .
397 Xml::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
398 '</td>' .
399 '<td class="mw-input">' .
400 Xml::input( 'wpReason', 60, $this->otherReason, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
401 '</td>' .
402 "</tr><tr>\n" .
403 '<td></td>' .
404 '<td class="mw-submit">' .
405 Xml::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
406 array( 'name' => 'wpSubmit' ) ) .
407 '</td>' .
408 "</tr>\n" .
409 Xml::closeElement( 'table' ) .
410 Html::hidden( 'wpEditToken', $this->getUser()->editToken() ) .
411 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
412 Html::hidden( 'type', $this->typeName ) .
413 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
414 Xml::closeElement( 'fieldset' ) . "\n";
415 } else {
416 $out = '';
417 }
418 if( $this->mIsAllowed ) {
419 $out .= Xml::closeElement( 'form' ) . "\n";
420 // Show link to edit the dropdown reasons
421 if( $this->getUser()->isAllowed( 'editinterface' ) ) {
422 $title = Title::makeTitle( NS_MEDIAWIKI, 'revdelete-reason-dropdown' );
423 $link = Linker::link(
424 $title,
425 wfMsgHtml( 'revdelete-edit-reasonlist' ),
426 array(),
427 array( 'action' => 'edit' )
428 );
429 $out .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
430 }
431 }
432 $this->getOutput()->addHTML( $out );
433 }
434
435 /**
436 * Show some introductory text
437 * @todo FIXME: Wikimedia-specific policy text
438 */
439 protected function addUsageText() {
440 $this->getOutput()->addWikiMsg( 'revdelete-text' );
441 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
442 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
443 }
444 if( $this->mIsAllowed ) {
445 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
446 }
447 }
448
449 /**
450 * @return String: HTML
451 */
452 protected function buildCheckBoxes() {
453 $html = '<table>';
454 // If there is just one item, use checkboxes
455 $list = $this->getList();
456 if( $list->length() == 1 ) {
457 $list->reset();
458 $bitfield = $list->current()->getBits(); // existing field
459 if( $this->submitClicked ) {
460 $bitfield = $this->extractBitfield( $this->extractBitParams(), $bitfield );
461 }
462 foreach( $this->checks as $item ) {
463 list( $message, $name, $field ) = $item;
464 $innerHTML = Xml::checkLabel( wfMsg($message), $name, $name, $bitfield & $field );
465 if( $field == Revision::DELETED_RESTRICTED )
466 $innerHTML = "<b>$innerHTML</b>";
467 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
468 $html .= "<tr>$line</tr>\n";
469 }
470 // Otherwise, use tri-state radios
471 } else {
472 $html .= '<tr>';
473 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-same').'</th>';
474 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
475 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</th>';
476 $html .= "<th></th></tr>\n";
477 foreach( $this->checks as $item ) {
478 list( $message, $name, $field ) = $item;
479 // If there are several items, use third state by default...
480 if( $this->submitClicked ) {
481 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
482 } else {
483 $selected = -1; // use existing field
484 }
485 $line = '<td class="mw-revdel-checkbox">' . Xml::radio( $name, -1, $selected == -1 ) . '</td>';
486 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 0, $selected == 0 ) . '</td>';
487 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 1, $selected == 1 ) . '</td>';
488 $label = wfMsgHtml($message);
489 if( $field == Revision::DELETED_RESTRICTED ) {
490 $label = "<b>$label</b>";
491 }
492 $line .= "<td>$label</td>";
493 $html .= "<tr>$line</tr>\n";
494 }
495 }
496
497 $html .= '</table>';
498 return $html;
499 }
500
501 /**
502 * UI entry point for form submission.
503 */
504 protected function submit() {
505 # Check edit token on submission
506 $token = $this->getRequest()->getVal('wpEditToken');
507 if( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
508 $this->getOutput()->addWikiMsg( 'sessionfailure' );
509 return false;
510 }
511 $bitParams = $this->extractBitParams();
512 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
513 $comment = $listReason;
514 if( $comment != 'other' && $this->otherReason != '' ) {
515 // Entry from drop down menu + additional comment
516 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason;
517 } elseif( $comment == 'other' ) {
518 $comment = $this->otherReason;
519 }
520 # Can the user set this field?
521 if( $bitParams[Revision::DELETED_RESTRICTED]==1 && !$this->getUser()->isAllowed('suppressrevision') ) {
522 $this->getOutput()->permissionRequired( 'suppressrevision' );
523 return false;
524 }
525 # If the save went through, go to success message...
526 $status = $this->save( $bitParams, $comment, $this->targetObj );
527 if ( $status->isGood() ) {
528 $this->success();
529 return true;
530 # ...otherwise, bounce back to form...
531 } else {
532 $this->failure( $status );
533 }
534 return false;
535 }
536
537 /**
538 * Report that the submit operation succeeded
539 */
540 protected function success() {
541 $this->getOutput()->setPageTitle( $this->msg( 'actioncomplete' ) );
542 $this->getOutput()->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo['success'] );
543 $this->list->reloadFromMaster();
544 $this->showForm();
545 }
546
547 /**
548 * Report that the submit operation failed
549 */
550 protected function failure( $status ) {
551 $this->getOutput()->setPageTitle( $this->msg( 'actionfailed' ) );
552 $this->getOutput()->addWikiText( $status->getWikiText( $this->typeInfo['failure'] ) );
553 $this->showForm();
554 }
555
556 /**
557 * Put together an array that contains -1, 0, or the *_deleted const for each bit
558 *
559 * @return array
560 */
561 protected function extractBitParams() {
562 $bitfield = array();
563 foreach( $this->checks as $item ) {
564 list( /* message */ , $name, $field ) = $item;
565 $val = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
566 if( $val < -1 || $val > 1) {
567 $val = -1; // -1 for existing value
568 }
569 $bitfield[$field] = $val;
570 }
571 if( !isset($bitfield[Revision::DELETED_RESTRICTED]) ) {
572 $bitfield[Revision::DELETED_RESTRICTED] = 0;
573 }
574 return $bitfield;
575 }
576
577 /**
578 * Put together a rev_deleted bitfield
579 * @param $bitPars array extractBitParams() params
580 * @param $oldfield int current bitfield
581 * @return array
582 */
583 public static function extractBitfield( $bitPars, $oldfield ) {
584 // Build the actual new rev_deleted bitfield
585 $newBits = 0;
586 foreach( $bitPars as $const => $val ) {
587 if( $val == 1 ) {
588 $newBits |= $const; // $const is the *_deleted const
589 } elseif( $val == -1 ) {
590 $newBits |= ($oldfield & $const); // use existing
591 }
592 }
593 return $newBits;
594 }
595
596 /**
597 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
598 */
599 protected function save( $bitfield, $reason, $title ) {
600 return $this->getList()->setVisibility(
601 array( 'value' => $bitfield, 'comment' => $reason )
602 );
603 }
604 }
605