More unused variables, whitespace
[lhc/web/wiklou.git] / includes / specials / SpecialRevisiondelete.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * Special page allowing users with the appropriate permissions to view
22 * and hide revisions. Log items can also be hidden.
23 *
24 * @file
25 * @ingroup SpecialPage
26 */
27
28 class SpecialRevisionDelete extends UnlistedSpecialPage {
29 /** Skin object */
30 var $skin;
31
32 /** True if the submit button was clicked, and the form was posted */
33 var $submitClicked;
34
35 /** Target ID list */
36 var $ids;
37
38 /** Archive name, for reviewing deleted files */
39 var $archiveName;
40
41 /** Edit token for securing image views against XSS */
42 var $token;
43
44 /** Title object for target parameter */
45 var $targetObj;
46
47 /** Deletion type, may be revision, archive, oldimage, filearchive, logging. */
48 var $typeName;
49
50 /** Array of checkbox specs (message, name, deletion bits) */
51 var $checks;
52
53 /** Information about the current type */
54 var $typeInfo;
55
56 /** The RevDel_List object, storing the list of items to be deleted/undeleted */
57 var $list;
58
59 /**
60 * Assorted information about each type, needed by the special page.
61 * TODO Move some of this to the list class
62 */
63 static $allowedTypes = array(
64 'revision' => array(
65 'check-label' => 'revdelete-hide-text',
66 'deletion-bits' => Revision::DELETED_TEXT,
67 'success' => 'revdelete-success',
68 'failure' => 'revdelete-failure',
69 'list-class' => 'RevDel_RevisionList',
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 ),
78 'oldimage'=> array(
79 'check-label' => 'revdelete-hide-image',
80 'deletion-bits' => File::DELETED_FILE,
81 'success' => 'revdelete-success',
82 'failure' => 'revdelete-failure',
83 'list-class' => 'RevDel_FileList',
84 ),
85 'filearchive' => array(
86 'check-label' => 'revdelete-hide-image',
87 'deletion-bits' => File::DELETED_FILE,
88 'success' => 'revdelete-success',
89 'failure' => 'revdelete-failure',
90 'list-class' => 'RevDel_ArchivedFileList',
91 ),
92 'logging' => array(
93 'check-label' => 'revdelete-hide-name',
94 'deletion-bits' => LogPage::DELETED_ACTION,
95 'success' => 'logdelete-success',
96 'failure' => 'logdelete-failure',
97 'list-class' => 'RevDel_LogList',
98 ),
99 );
100
101 /** Type map to support old log entries */
102 static $deprecatedTypeMap = array(
103 'oldid' => 'revision',
104 'artimestamp' => 'archive',
105 'oldimage' => 'oldimage',
106 'fileid' => 'filearchive',
107 'logid' => 'logging',
108 );
109
110 public function __construct() {
111 parent::__construct( 'Revisiondelete', 'deletedhistory' );
112 }
113
114 public function execute( $par ) {
115 global $wgOut, $wgUser, $wgRequest;
116 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
117 $wgOut->permissionRequired( 'deletedhistory' );
118 return;
119 } else if( wfReadOnly() ) {
120 $wgOut->readOnlyPage();
121 return;
122 }
123 $this->mIsAllowed = $wgUser->isAllowed('deleterevision'); // for changes
124 $this->skin = $wgUser->getSkin();
125 $this->setHeaders();
126 $this->outputHeader();
127 $this->submitClicked = $wgRequest->wasPosted() && $wgRequest->getBool( 'wpSubmit' );
128 # Handle our many different possible input types.
129 $ids = $wgRequest->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( $wgRequest->getArray('ids',array()) );
136 }
137 // $this->ids = array_map( 'intval', $this->ids );
138 $this->ids = array_unique( array_filter( $this->ids ) );
139
140 if ( $wgRequest->getVal( 'action' ) == 'historysubmit' ) {
141 # For show/hide form submission from history page
142 $this->targetObj = $GLOBALS['wgTitle'];
143 $this->typeName = 'revision';
144 } else {
145 $this->typeName = $wgRequest->getVal( 'type' );
146 $this->targetObj = Title::newFromText( $wgRequest->getText( 'target' ) );
147 }
148
149 # For reviewing deleted files...
150 $this->archiveName = $wgRequest->getVal( 'file' );
151 $this->token = $wgRequest->getVal( 'token' );
152 if ( $this->archiveName && $this->targetObj ) {
153 $this->tryShowFile( $this->archiveName );
154 return;
155 }
156
157 if ( isset( self::$deprecatedTypeMap[$this->typeName] ) ) {
158 $this->typeName = self::$deprecatedTypeMap[$this->typeName];
159 }
160
161 # No targets?
162 if( !isset( self::$allowedTypes[$this->typeName] ) || count( $this->ids ) == 0 ) {
163 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
164 return;
165 }
166 $this->typeInfo = self::$allowedTypes[$this->typeName];
167
168 # If we have revisions, get the title from the first one
169 # since they should all be from the same page. This allows
170 # for more flexibility with page moves...
171 if( $this->typeName == 'revision' ) {
172 $rev = Revision::newFromId( $this->ids[0] );
173 $this->targetObj = $rev ? $rev->getTitle() : $this->targetObj;
174 }
175
176 $this->otherReason = $wgRequest->getVal( 'wpReason' );
177 # We need a target page!
178 if( is_null($this->targetObj) ) {
179 $wgOut->addWikiMsg( 'undelete-header' );
180 return;
181 }
182 # Give a link to the logs/hist for this page
183 $this->showConvenienceLinks();
184
185 # Initialise checkboxes
186 $this->checks = array(
187 array( $this->typeInfo['check-label'], 'wpHidePrimary', $this->typeInfo['deletion-bits'] ),
188 array( 'revdelete-hide-comment', 'wpHideComment', Revision::DELETED_COMMENT ),
189 array( 'revdelete-hide-user', 'wpHideUser', Revision::DELETED_USER )
190 );
191 if( $wgUser->isAllowed('suppressrevision') ) {
192 $this->checks[] = array( 'revdelete-hide-restricted',
193 'wpHideRestricted', Revision::DELETED_RESTRICTED );
194 }
195
196 # Either submit or create our form
197 if( $this->mIsAllowed && $this->submitClicked ) {
198 $this->submit( $wgRequest );
199 } else {
200 $this->showForm();
201 }
202
203 $qc = $this->getLogQueryCond();
204 # Show relevant lines from the deletion log
205 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
206 LogEventsList::showLogExtract( $wgOut, 'delete',
207 $this->targetObj->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
208 # Show relevant lines from the suppression log
209 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
210 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'suppress' ) ) . "</h2>\n" );
211 LogEventsList::showLogExtract( $wgOut, 'suppress',
212 $this->targetObj->getPrefixedText(), '', array( 'lim' => 25, 'conds' => $qc ) );
213 }
214 }
215
216 /**
217 * Show some useful links in the subtitle
218 */
219 protected function showConvenienceLinks() {
220 global $wgOut, $wgUser, $wgLang;
221 # Give a link to the logs/hist for this page
222 if( $this->targetObj ) {
223 $links = array();
224 $links[] = $this->skin->linkKnown(
225 SpecialPage::getTitleFor( 'Log' ),
226 wfMsgHtml( 'viewpagelogs' ),
227 array(),
228 array( 'page' => $this->targetObj->getPrefixedText() )
229 );
230 if ( $this->targetObj->getNamespace() != NS_SPECIAL ) {
231 # Give a link to the page history
232 $links[] = $this->skin->linkKnown(
233 $this->targetObj,
234 wfMsgHtml( 'pagehist' ),
235 array(),
236 array( 'action' => 'history' )
237 );
238 # Link to deleted edits
239 if( $wgUser->isAllowed('undelete') ) {
240 $undelete = SpecialPage::getTitleFor( 'Undelete' );
241 $links[] = $this->skin->linkKnown(
242 $undelete,
243 wfMsgHtml( 'deletedhist' ),
244 array(),
245 array( 'target' => $this->targetObj->getPrefixedDBkey() )
246 );
247 }
248 }
249 # Logs themselves don't have histories or archived revisions
250 $wgOut->setSubtitle( '<p>' . $wgLang->pipeList( $links ) . '</p>' );
251 }
252 }
253
254 /**
255 * Get the condition used for fetching log snippets
256 */
257 protected function getLogQueryCond() {
258 $conds = array();
259 // Revision delete logs for these item
260 $conds['log_type'] = array('delete','suppress');
261 $conds['log_action'] = $this->getList()->getLogAction();
262 $conds['ls_field'] = RevisionDeleter::getRelationType( $this->typeName );
263 $conds['ls_value'] = $this->ids;
264 return $conds;
265 }
266
267 /**
268 * Show a deleted file version requested by the visitor.
269 * TODO Mostly copied from Special:Undelete. Refactor.
270 */
271 protected function tryShowFile( $archiveName ) {
272 global $wgOut, $wgRequest, $wgUser, $wgLang;
273
274 $repo = RepoGroup::singleton()->getLocalRepo();
275 $oimage = $repo->newFromArchiveName( $this->targetObj, $archiveName );
276 $oimage->load();
277 // Check if user is allowed to see this file
278 if ( !$oimage->exists() ) {
279 $wgOut->addWikiMsg( 'revdelete-no-file' );
280 return;
281 }
282 if( !$oimage->userCan(File::DELETED_FILE) ) {
283 if( $oimage->isDeleted( File::DELETED_RESTRICTED ) ) {
284 $wgOut->permissionRequired( 'suppressrevision' );
285 } else {
286 $wgOut->permissionRequired( 'deletedtext' );
287 }
288 return;
289 }
290 if ( !$wgUser->matchEditToken( $this->token, $archiveName ) ) {
291 $wgOut->addWikiMsg( 'revdelete-show-file-confirm',
292 $this->targetObj->getText(),
293 $wgLang->date( $oimage->getTimestamp() ),
294 $wgLang->time( $oimage->getTimestamp() ) );
295 $wgOut->addHTML(
296 Xml::openElement( 'form', array(
297 'method' => 'POST',
298 'action' => $this->getTitle()->getLocalUrl(
299 'target=' . urlencode( $oimage->getName() ) .
300 '&file=' . urlencode( $archiveName ) .
301 '&token=' . urlencode( $wgUser->editToken( $archiveName ) ) )
302 )
303 ) .
304 Xml::submitButton( wfMsg( 'revdelete-show-file-submit' ) ) .
305 '</form>'
306 );
307 return;
308 }
309 $wgOut->disable();
310 # We mustn't allow the output to be Squid cached, otherwise
311 # if an admin previews a deleted image, and it's cached, then
312 # a user without appropriate permissions can toddle off and
313 # nab the image, and Squid will serve it
314 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
315 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
316 $wgRequest->response()->header( 'Pragma: no-cache' );
317
318 # Stream the file to the client
319 global $IP;
320 require_once( "$IP/includes/StreamFile.php" );
321 $key = $oimage->getStorageKey();
322 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
323 wfStreamFile( $path );
324 }
325
326 /**
327 * Get the list object for this request
328 */
329 protected function getList() {
330 if ( is_null( $this->list ) ) {
331 $class = $this->typeInfo['list-class'];
332 $this->list = new $class( $this, $this->targetObj, $this->ids );
333 }
334 return $this->list;
335 }
336
337 /**
338 * Show a list of items that we will operate on, and show a form with checkboxes
339 * which will allow the user to choose new visibility settings.
340 */
341 protected function showForm() {
342 global $wgOut, $wgUser, $wgLang;
343 $UserAllowed = true;
344
345 if ( $this->typeName == 'logging' ) {
346 $wgOut->addWikiMsg( 'logdelete-selected', $wgLang->formatNum( count($this->ids) ) );
347 } else {
348 $wgOut->addWikiMsg( 'revdelete-selected',
349 $this->targetObj->getPrefixedText(), count( $this->ids ) );
350 }
351
352 $wgOut->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 $wgOut->permissionRequired( 'suppressrevision' );
362 return;
363 }
364 $UserAllowed = false;
365 }
366 $numRevisions++;
367 $wgOut->addHTML( $item->getHTML() );
368 }
369
370 if( !$numRevisions ) {
371 $wgOut->showErrorPage( 'revdelete-nooldid-title', 'revdelete-nooldid-text' );
372 return;
373 }
374
375 $wgOut->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' ) ) .
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 Xml::hidden( 'wpEditToken', $wgUser->editToken() ) .
416 Xml::hidden( 'target', $this->targetObj->getPrefixedText() ) .
417 Xml::hidden( 'type', $this->typeName ) .
418 Xml::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( $wgUser->isAllowed( 'editinterface' ) ) {
427 $title = Title::makeTitle( NS_MEDIAWIKI, 'revdelete-reason-dropdown' );
428 $link = $wgUser->getSkin()->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 $wgOut->addHTML( $out );
438 }
439
440 /**
441 * Show some introductory text
442 * FIXME Wikimedia-specific policy text
443 */
444 protected function addUsageText() {
445 global $wgOut, $wgUser;
446 $wgOut->addWikiMsg( 'revdelete-text' );
447 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
448 $wgOut->addWikiMsg( 'revdelete-suppress-text' );
449 }
450 if( $this->mIsAllowed ) {
451 $wgOut->addWikiMsg( 'revdelete-confirm' );
452 }
453 }
454
455 /**
456 * @return String: HTML
457 */
458 protected function buildCheckBoxes() {
459 global $wgRequest;
460
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($wgRequest), $bitfield );
469 }
470 foreach( $this->checks as $item ) {
471 list( $message, $name, $field ) = $item;
472 $innerHTML = Xml::checkLabel( wfMsg($message), $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">'.wfMsgHtml('revdelete-radio-same').'</th>';
482 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-unset').'</th>';
483 $html .= '<th class="mw-revdel-checkbox">'.wfMsgHtml('revdelete-radio-set').'</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 = $wgRequest->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 = wfMsgHtml($message);
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 * @param $request WebRequest
512 */
513 protected function submit( $request ) {
514 global $wgUser, $wgOut;
515 # Check edit token on submission
516 if( $this->submitClicked && !$wgUser->matchEditToken( $request->getVal('wpEditToken') ) ) {
517 $wgOut->addWikiMsg( 'sessionfailure' );
518 return false;
519 }
520 $bitParams = $this->extractBitParams( $request );
521 $listReason = $request->getText( 'wpRevDeleteReasonList', 'other' ); // from dropdown
522 $comment = $listReason;
523 if( $comment != 'other' && $this->otherReason != '' ) {
524 // Entry from drop down menu + additional comment
525 $comment .= wfMsgForContent( 'colon-separator' ) . $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 && !$wgUser->isAllowed('suppressrevision') ) {
531 $wgOut->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 global $wgOut;
551 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
552 $wgOut->wrapWikiMsg( "<span class=\"success\">\n$1\n</span>", $this->typeInfo['success'] );
553 $this->list->reloadFromMaster();
554 $this->showForm();
555 }
556
557 /**
558 * Report that the submit operation failed
559 */
560 protected function failure( $status ) {
561 global $wgOut;
562 $wgOut->setPagetitle( wfMsg( 'actionfailed' ) );
563 $wgOut->addWikiText( $status->getWikiText( $this->typeInfo['failure'] ) );
564 $this->showForm();
565 }
566
567 /**
568 * Put together an array that contains -1, 0, or the *_deleted const for each bit
569 * @param $request WebRequest
570 * @return array
571 */
572 protected function extractBitParams( $request ) {
573 $bitfield = array();
574 foreach( $this->checks as $item ) {
575 list( /* message */ , $name, $field ) = $item;
576 $val = $request->getInt( $name, 0 /* unchecked */ );
577 if( $val < -1 || $val > 1) {
578 $val = -1; // -1 for existing value
579 }
580 $bitfield[$field] = $val;
581 }
582 if( !isset($bitfield[Revision::DELETED_RESTRICTED]) ) {
583 $bitfield[Revision::DELETED_RESTRICTED] = 0;
584 }
585 return $bitfield;
586 }
587
588 /**
589 * Put together a rev_deleted bitfield
590 * @param $bitPars array extractBitParams() params
591 * @param $oldfield int current bitfield
592 * @return array
593 */
594 public static function extractBitfield( $bitPars, $oldfield ) {
595 // Build the actual new rev_deleted bitfield
596 $newBits = 0;
597 foreach( $bitPars as $const => $val ) {
598 if( $val == 1 ) {
599 $newBits |= $const; // $const is the *_deleted const
600 } else if( $val == -1 ) {
601 $newBits |= ($oldfield & $const); // use existing
602 }
603 }
604 return $newBits;
605 }
606
607 /**
608 * Do the write operations. Simple wrapper for RevDel_*List::setVisibility().
609 */
610 protected function save( $bitfield, $reason, $title ) {
611 return $this->getList()->setVisibility(
612 array( 'value' => $bitfield, 'comment' => $reason )
613 );
614 }
615 }
616