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