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