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