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