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