Use local context instead of global variables
[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 } elseif( $user->isBlocked() ) {
122 throw new UserBlockedError( $user->getBlock() );
123 }
124
125 $this->mIsAllowed = $user->isAllowed('deleterevision'); // for changes
126 $this->setHeaders();
127 $this->outputHeader();
128 $request = $this->getRequest();
129 $this->submitClicked = $request->wasPosted() && $request->getBool( 'wpSubmit' );
130 # Handle our many different possible input types.
131 $ids = $request->getVal( 'ids' );
132 if ( !is_null( $ids ) ) {
133 # Allow CSV, for backwards compatibility, or a single ID for show/hide links
134 $this->ids = explode( ',', $ids );
135 } else {
136 # Array input
137 $this->ids = array_keys( $request->getArray('ids',array()) );
138 }
139 // $this->ids = array_map( 'intval', $this->ids );
140 $this->ids = array_unique( array_filter( $this->ids ) );
141
142 if ( $request->getVal( 'action' ) == 'historysubmit' ) {
143 // For show/hide form submission from history page
144 // Since we are access through index.php?title=XXX&action=historysubmit
145 // getFullTitle() will contain the target title and not our title
146 $this->targetObj = $this->getFullTitle();
147 $this->typeName = 'revision';
148 } else {
149 $this->typeName = $request->getVal( 'type' );
150 $this->targetObj = Title::newFromText( $request->getText( 'target' ) );
151 }
152
153 # For reviewing deleted files...
154 $this->archiveName = $request->getVal( 'file' );
155 $this->token = $request->getVal( 'token' );
156 if ( $this->archiveName && $this->targetObj ) {
157 $this->tryShowFile( $this->archiveName );
158 return;
159 }
160
161 if ( isset( self::$deprecatedTypeMap[$this->typeName] ) ) {
162 $this->typeName = self::$deprecatedTypeMap[$this->typeName];
163 }
164
165 # No targets?
166 if( !isset( self::$allowedTypes[$this->typeName] ) || count( $this->ids ) == 0 ) {
167 $output->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
168 return;
169 }
170 $this->typeInfo = self::$allowedTypes[$this->typeName];
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 $output->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
210 LogEventsList::showLogExtract( $output, 'delete',
211 $this->targetObj->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
212 # Show relevant lines from the suppression log
213 if( $user->isAllowed( 'suppressionlog' ) ) {
214 $output->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'suppress' ) ) . "</h2>\n" );
215 LogEventsList::showLogExtract( $output, 'suppress',
216 $this->targetObj->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
217 }
218 }
219
220 /**
221 * Show some useful links in the subtitle
222 */
223 protected function showConvenienceLinks() {
224 # Give a link to the logs/hist for this page
225 if( $this->targetObj ) {
226 $links = array();
227 $links[] = Linker::linkKnown(
228 SpecialPage::getTitleFor( 'Log' ),
229 wfMsgHtml( 'viewpagelogs' ),
230 array(),
231 array( 'page' => $this->targetObj->getPrefixedText() )
232 );
233 if ( $this->targetObj->getNamespace() != NS_SPECIAL ) {
234 # Give a link to the page history
235 $links[] = Linker::linkKnown(
236 $this->targetObj,
237 wfMsgHtml( 'pagehist' ),
238 array(),
239 array( 'action' => 'history' )
240 );
241 # Link to deleted edits
242 if( $this->getUser()->isAllowed('undelete') ) {
243 $undelete = SpecialPage::getTitleFor( 'Undelete' );
244 $links[] = Linker::linkKnown(
245 $undelete,
246 wfMsgHtml( 'deletedhist' ),
247 array(),
248 array( 'target' => $this->targetObj->getPrefixedDBkey() )
249 );
250 }
251 }
252 # Logs themselves don't have histories or archived revisions
253 $this->getOutput()->setSubtitle( '<p>' . $this->getLang()->pipeList( $links ) . '</p>' );
254 }
255 }
256
257 /**
258 * Get the condition used for fetching log snippets
259 */
260 protected function getLogQueryCond() {
261 $conds = array();
262 // Revision delete logs for these item
263 $conds['log_type'] = array( 'delete', 'suppress' );
264 $conds['log_action'] = $this->getList()->getLogAction();
265 $conds['ls_field'] = RevisionDeleter::getRelationType( $this->typeName );
266 $conds['ls_value'] = $this->ids;
267 return $conds;
268 }
269
270 /**
271 * Show a deleted file version requested by the visitor.
272 * TODO Mostly copied from Special:Undelete. Refactor.
273 */
274 protected function tryShowFile( $archiveName ) {
275 $repo = RepoGroup::singleton()->getLocalRepo();
276 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
277 $oimage->load();
278 // Check if user is allowed to see this file
279 if ( !$oimage->exists() ) {
280 $this->getOutput()->addWikiMsg( 'revdelete-no-file' );
281 return;
282 }
283 if( !$oimage->userCan(File::DELETED_FILE) ) {
284 if( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
285 $this->getOutput()->permissionRequired( 'suppressrevision' );
286 } else {
287 $this->getOutput()->permissionRequired( 'deletedtext' );
288 }
289 return;
290 }
291 if ( !$this->getUser()->matchEditToken( $this->token, $archiveName ) ) {
292 $this->getOutput()->addWikiMsg( 'revdelete-show-file-confirm',
293 $this->targetObj->getText(),
294 $this->getLang()->date( $oimage->getTimestamp() ),
295 $this->getLang()->time( $oimage->getTimestamp() ) );
296 $this->getOutput()->addHTML(
297 Xml::openElement( 'form', array(
298 'method' => 'POST',
299 'action' => $this->getTitle()->getLocalUrl(
300 'target=' . urlencode( $oimage->getName() ) .
301 '&file=' . urlencode( $archiveName ) .
302 '&token=' . urlencode( $this->getUser()->editToken( $archiveName ) ) )
303 )
304 ) .
305 Xml::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
306 '</form>'
307 );
308 return;
309 }
310 $this->getOutput()->disable();
311 # We mustn't allow the output to be Squid cached, otherwise
312 # if an admin previews a deleted image, and it's cached, then
313 # a user without appropriate permissions can toddle off and
314 # nab the image, and Squid will serve it
315 $this->getRequest()->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
316 $this->getRequest()->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
317 $this->getRequest()->response()->header( 'Pragma: no-cache' );
318
319 # Stream the file to the client
320 global $IP;
321 require_once( "$IP/includes/StreamFile.php" );
322 $key = $oimage->getStorageKey();
323 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
324 wfStreamFile( $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->getLang()->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 $this->getOutput()->permissionRequired( 'suppressrevision' );
362 return;
363 }
364 $UserAllowed = false;
365 }
366 $numRevisions++;
367 $this->getOutput()->addHTML( $item->getHTML() );
368 }
369
370 if( !$numRevisions ) {
371 $this->getOutput()->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
372 return;
373 }
374
375 $this->getOutput()->addHTML( "</ul>" );
376 // Explanation text
377 $this->addUsageText();
378
379 // Normal sysops can always see what they did, but can't always change it
380 if( !$UserAllowed ) return;
381
382 // Show form if the user can submit
383 if( $this->mIsAllowed ) {
384 $out = Xml::openElement( 'form', array( 'method' => 'post',
385 'action' => $this->getTitle()->getLocalUrl( array( 'action' => 'submit' ) ),
386 'id' => 'mw-revdel-form-revisions' ) ) .
387 Xml::fieldset( wfMsg( 'revdelete-legend' ) ) .
388 $this->buildCheckBoxes() .
389 Xml::openElement( 'table' ) .
390 "<tr>\n" .
391 '<td class="mw-label">' .
392 Xml::label( wfMsg( 'revdelete-log' ), 'wpRevDeleteReasonList' ) .
393 '</td>' .
394 '<td class="mw-input">' .
395 Xml::listDropDown( 'wpRevDeleteReasonList',
396 wfMsgForContent( 'revdelete-reason-dropdown' ),
397 wfMsgForContent( 'revdelete-reasonotherlist' ), '', 'wpReasonDropDown', 1
398 ) .
399 '</td>' .
400 "</tr><tr>\n" .
401 '<td class="mw-label">' .
402 Xml::label( wfMsg( 'revdelete-otherreason' ), 'wpReason' ) .
403 '</td>' .
404 '<td class="mw-input">' .
405 Xml::input( 'wpReason', 60, $this->otherReason, array( 'id' => 'wpReason', 'maxlength' => 100 ) ) .
406 '</td>' .
407 "</tr><tr>\n" .
408 '<td></td>' .
409 '<td class="mw-submit">' .
410 Xml::submitButton( wfMsgExt('revdelete-submit','parsemag',$numRevisions),
411 array( 'name' => 'wpSubmit' ) ) .
412 '</td>' .
413 "</tr>\n" .
414 Xml::closeElement( 'table' ) .
415 Html::hidden( 'wpEditToken', $this->getUser()->editToken() ) .
416 Html::hidden( 'target', $this->targetObj->getPrefixedText() ) .
417 Html::hidden( 'type', $this->typeName ) .
418 Html::hidden( 'ids', implode( ',', $this->ids ) ) .
419 Xml::closeElement( 'fieldset' ) . "\n";
420 } else {
421 $out = '';
422 }
423 if( $this->mIsAllowed ) {
424 $out .= Xml::closeElement( 'form' ) . "\n";
425 // Show link to edit the dropdown reasons
426 if( $this->getUser()->isAllowed( 'editinterface' ) ) {
427 $title = Title::makeTitle( NS_MEDIAWIKI, 'revdelete-reason-dropdown' );
428 $link = Linker::link(
429 $title,
430 wfMsgHtml( 'revdelete-edit-reasonlist' ),
431 array(),
432 array( 'action' => 'edit' )
433 );
434 $out .= Xml::tags( 'p', array( 'class' => 'mw-revdel-editreasons' ), $link ) . "\n";
435 }
436 }
437 $this->getOutput()->addHTML( $out );
438 }
439
440 /**
441 * Show some introductory text
442 * @todo FIXME: Wikimedia-specific policy text
443 */
444 protected function addUsageText() {
445 $this->getOutput()->addWikiMsg( 'revdelete-text' );
446 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
447 $this->getOutput()->addWikiMsg( 'revdelete-suppress-text' );
448 }
449 if( $this->mIsAllowed ) {
450 $this->getOutput()->addWikiMsg( 'revdelete-confirm' );
451 }
452 }
453
454 /**
455 * @return String: HTML
456 */
457 protected function buildCheckBoxes() {
458 $html = '<table>';
459 // If there is just one item, use checkboxes
460 $list = $this->getList();
461 if( $list->length() == 1 ) {
462 $list->reset();
463 $bitfield = $list->current()->getBits(); // existing field
464 if( $this->submitClicked ) {
465 $bitfield = $this->extractBitfield( $this->extractBitParams(), $bitfield );
466 }
467 foreach( $this->checks as $item ) {
468 list( $message, $name, $field ) = $item;
469 $innerHTML = Xml::checkLabel( wfMsg($message), $name, $name, $bitfield & $field );
470 if( $field == Revision::DELETED_RESTRICTED )
471 $innerHTML = "<b>$innerHTML</b>";
472 $line = Xml::tags( 'td', array( 'class' => 'mw-input' ), $innerHTML );
473 $html .= "<tr>$line</tr>\n";
474 }
475 // Otherwise, use tri-state radios
476 } else {
477 $html .= '<tr>';
478 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-same').'</th>';
479 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
480 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</th>';
481 $html .= "<th></th></tr>\n";
482 foreach( $this->checks as $item ) {
483 list( $message, $name, $field ) = $item;
484 // If there are several items, use third state by default...
485 if( $this->submitClicked ) {
486 $selected = $this->getRequest()->getInt( $name, 0 /* unchecked */ );
487 } else {
488 $selected = -1; // use existing field
489 }
490 $line = '<td class="mw-revdel-checkbox">' . Xml::radio( $name, -1, $selected == -1 ) . '</td>';
491 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 0, $selected == 0 ) . '</td>';
492 $line .= '<td class="mw-revdel-checkbox">' . Xml::radio( $name, 1, $selected == 1 ) . '</td>';
493 $label = wfMsgHtml($message);
494 if( $field == Revision::DELETED_RESTRICTED ) {
495 $label = "<b>$label</b>";
496 }
497 $line .= "<td>$label</td>";
498 $html .= "<tr>$line</tr>\n";
499 }
500 }
501
502 $html .= '</table>';
503 return $html;
504 }
505
506 /**
507 * UI entry point for form submission.
508 */
509 protected function submit() {
510 # Check edit token on submission
511 $token = $this->getRequest()->getVal('wpEditToken');
512 if( $this->submitClicked && !$this->getUser()->matchEditToken( $token ) ) {
513 $this->getOutput()->addWikiMsg( 'sessionfailure' );
514 return false;
515 }
516 $bitParams = $this->extractBitParams();
517 $listReason = $this->getRequest()->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
518 $comment = $listReason;
519 if( $comment != 'other' && $this->otherReason != '' ) {
520 // Entry from drop down menu + additional comment
521 $comment .= wfMsgForContent( 'colon-separator' ) . $this->otherReason;
522 } elseif( $comment == 'other' ) {
523 $comment = $this->otherReason;
524 }
525 # Can the user set this field?
526 if( $bitParams[Revision::DELETED_RESTRICTED]==1 && !$this->getUser()->isAllowed('suppressrevision') ) {
527 $this->getOutput()->permissionRequired( 'suppressrevision' );
528 return false;
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( wfMsg( '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( wfMsg( '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 */
604 protected function save( $bitfield, $reason, $title ) {
605 return $this->getList()->setVisibility(
606 array( 'value' => $bitfield, 'comment' => $reason )
607 );
608 }
609 }
610