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