Merge "Make label on Special:Undelete search reflect whether search is full-text...
[lhc/web/wiklou.git] / includes / specials / SpecialUndelete.php
1 <?php
2 /**
3 * Implements Special:Undelete
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 use Wikimedia\Rdbms\ResultWrapper;
25
26 /**
27 * Special page allowing users with the appropriate permissions to view
28 * and restore deleted content.
29 *
30 * @ingroup SpecialPage
31 */
32 class SpecialUndelete extends SpecialPage {
33 private $mAction;
34 private $mTarget;
35 private $mTimestamp;
36 private $mRestore;
37 private $mRevdel;
38 private $mInvert;
39 private $mFilename;
40 private $mTargetTimestamp;
41 private $mAllowed;
42 private $mCanView;
43 private $mComment;
44 private $mToken;
45
46 /** @var Title */
47 private $mTargetObj;
48 /**
49 * @var string Search prefix
50 */
51 private $mSearchPrefix;
52
53 function __construct() {
54 parent::__construct( 'Undelete', 'deletedhistory' );
55 }
56
57 public function doesWrites() {
58 return true;
59 }
60
61 function loadRequest( $par ) {
62 $request = $this->getRequest();
63 $user = $this->getUser();
64
65 $this->mAction = $request->getVal( 'action' );
66 if ( $par !== null && $par !== '' ) {
67 $this->mTarget = $par;
68 } else {
69 $this->mTarget = $request->getVal( 'target' );
70 }
71
72 $this->mTargetObj = null;
73
74 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
75 $this->mTargetObj = Title::newFromText( $this->mTarget );
76 }
77
78 $this->mSearchPrefix = $request->getText( 'prefix' );
79 $time = $request->getVal( 'timestamp' );
80 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
81 $this->mFilename = $request->getVal( 'file' );
82
83 $posted = $request->wasPosted() &&
84 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
85 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
86 $this->mRevdel = $request->getCheck( 'revdel' ) && $posted;
87 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
88 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
89 $this->mDiff = $request->getCheck( 'diff' );
90 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
91 $this->mComment = $request->getText( 'wpComment' );
92 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
93 $this->mToken = $request->getVal( 'token' );
94
95 if ( $this->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
96 $this->mAllowed = true; // user can restore
97 $this->mCanView = true; // user can view content
98 } elseif ( $this->isAllowed( 'deletedtext' ) ) {
99 $this->mAllowed = false; // user cannot restore
100 $this->mCanView = true; // user can view content
101 $this->mRestore = false;
102 } else { // user can only view the list of revisions
103 $this->mAllowed = false;
104 $this->mCanView = false;
105 $this->mTimestamp = '';
106 $this->mRestore = false;
107 }
108
109 if ( $this->mRestore || $this->mInvert ) {
110 $timestamps = [];
111 $this->mFileVersions = [];
112 foreach ( $request->getValues() as $key => $val ) {
113 $matches = [];
114 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
115 array_push( $timestamps, $matches[1] );
116 }
117
118 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
119 $this->mFileVersions[] = intval( $matches[1] );
120 }
121 }
122 rsort( $timestamps );
123 $this->mTargetTimestamp = $timestamps;
124 }
125 }
126
127 /**
128 * Checks whether a user is allowed the permission for the
129 * specific title if one is set.
130 *
131 * @param string $permission
132 * @param User $user
133 * @return bool
134 */
135 protected function isAllowed( $permission, User $user = null ) {
136 $user = $user ?: $this->getUser();
137 if ( $this->mTargetObj !== null ) {
138 return $this->mTargetObj->userCan( $permission, $user );
139 } else {
140 return $user->isAllowed( $permission );
141 }
142 }
143
144 function userCanExecute( User $user ) {
145 return $this->isAllowed( $this->mRestriction, $user );
146 }
147
148 function execute( $par ) {
149 $this->useTransactionalTimeLimit();
150
151 $user = $this->getUser();
152
153 $this->setHeaders();
154 $this->outputHeader();
155
156 $this->loadRequest( $par );
157 $this->checkPermissions(); // Needs to be after mTargetObj is set
158
159 $out = $this->getOutput();
160
161 if ( is_null( $this->mTargetObj ) ) {
162 $out->addWikiMsg( 'undelete-header' );
163
164 # Not all users can just browse every deleted page from the list
165 if ( $user->isAllowed( 'browsearchive' ) ) {
166 $this->showSearchForm();
167 }
168
169 return;
170 }
171
172 $this->addHelpLink( 'Help:Undelete' );
173 if ( $this->mAllowed ) {
174 $out->setPageTitle( $this->msg( 'undeletepage' ) );
175 } else {
176 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
177 }
178
179 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
180
181 if ( $this->mTimestamp !== '' ) {
182 $this->showRevision( $this->mTimestamp );
183 } elseif ( $this->mFilename !== null && $this->mTargetObj->inNamespace( NS_FILE ) ) {
184 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
185 // Check if user is allowed to see this file
186 if ( !$file->exists() ) {
187 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
188 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
189 if ( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
190 throw new PermissionsError( 'suppressrevision' );
191 } else {
192 throw new PermissionsError( 'deletedtext' );
193 }
194 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
195 $this->showFileConfirmationForm( $this->mFilename );
196 } else {
197 $this->showFile( $this->mFilename );
198 }
199 } elseif ( $this->mAction === "submit" ) {
200 if ( $this->mRestore ) {
201 $this->undelete();
202 } elseif ( $this->mRevdel ) {
203 $this->redirectToRevDel();
204 }
205
206 } else {
207 $this->showHistory();
208 }
209 }
210
211 /**
212 * Convert submitted form data to format expected by RevisionDelete and
213 * redirect the request
214 */
215 private function redirectToRevDel() {
216 $archive = new PageArchive( $this->mTargetObj );
217
218 $revisions = [];
219
220 foreach ( $this->getRequest()->getValues() as $key => $val ) {
221 $matches = [];
222 if ( preg_match( "/^ts(\d{14})$/", $key, $matches ) ) {
223 $revisions[ $archive->getRevision( $matches[1] )->getId() ] = 1;
224 }
225 }
226 $query = [
227 "type" => "revision",
228 "ids" => $revisions,
229 "target" => $this->mTargetObj->getPrefixedText()
230 ];
231 $url = SpecialPage::getTitleFor( 'Revisiondelete' )->getFullURL( $query );
232 $this->getOutput()->redirect( $url );
233 }
234
235 function showSearchForm() {
236 $out = $this->getOutput();
237 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
238 $fuzzySearch = $this->getRequest()->getVal( "fuzzy", false );
239 $out->addHTML(
240 Xml::openElement( 'form', [ 'method' => 'get', 'action' => wfScript() ] ) .
241 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
242 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
243 Html::hidden( 'fuzzy', $this->getRequest()->getVal( 'fuzzy' ) ) .
244 Html::rawElement(
245 'label',
246 [ 'for' => 'prefix' ],
247 $this->msg( $fuzzySearch ? 'undelete-search-full' : 'undelete-search-prefix' )
248 ->parse()
249 ) .
250 Xml::input(
251 'prefix',
252 20,
253 $this->mSearchPrefix,
254 [ 'id' => 'prefix', 'autofocus' => '' ]
255 ) .
256 ' ' .
257 Xml::submitButton(
258 $this->msg( 'undelete-search-submit' )->text(),
259 [ 'id' => 'searchUndelete' ]
260 ) .
261 Xml::closeElement( 'fieldset' ) .
262 Xml::closeElement( 'form' )
263 );
264
265 # List undeletable articles
266 if ( $this->mSearchPrefix ) {
267 // For now, we enable search engine match only when specifically asked to
268 // by using fuzzy=1 parameter.
269 if ( $fuzzySearch ) {
270 $result = PageArchive::listPagesBySearch( $this->mSearchPrefix );
271 } else {
272 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
273 }
274 $this->showList( $result );
275 }
276 }
277
278 /**
279 * Generic list of deleted pages
280 *
281 * @param ResultWrapper $result
282 * @return bool
283 */
284 private function showList( $result ) {
285 $out = $this->getOutput();
286
287 if ( $result->numRows() == 0 ) {
288 $out->addWikiMsg( 'undelete-no-results' );
289
290 return false;
291 }
292
293 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
294
295 $linkRenderer = $this->getLinkRenderer();
296 $undelete = $this->getPageTitle();
297 $out->addHTML( "<ul id='undeleteResultsList'>\n" );
298 foreach ( $result as $row ) {
299 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
300 if ( $title !== null ) {
301 $item = $linkRenderer->makeKnownLink(
302 $undelete,
303 $title->getPrefixedText(),
304 [],
305 [ 'target' => $title->getPrefixedText() ]
306 );
307 } else {
308 // The title is no longer valid, show as text
309 $item = Html::element(
310 'span',
311 [ 'class' => 'mw-invalidtitle' ],
312 Linker::getInvalidTitleDescription(
313 $this->getContext(),
314 $row->ar_namespace,
315 $row->ar_title
316 )
317 );
318 }
319 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
320 $out->addHTML( "<li class='undeleteResult'>{$item} ({$revs})</li>\n" );
321 }
322 $result->free();
323 $out->addHTML( "</ul>\n" );
324
325 return true;
326 }
327
328 private function showRevision( $timestamp ) {
329 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
330 return;
331 }
332
333 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
334 if ( !Hooks::run( 'UndeleteForm::showRevision', [ &$archive, $this->mTargetObj ] ) ) {
335 return;
336 }
337 $rev = $archive->getRevision( $timestamp );
338
339 $out = $this->getOutput();
340 $user = $this->getUser();
341
342 if ( !$rev ) {
343 $out->addWikiMsg( 'undeleterevision-missing' );
344
345 return;
346 }
347
348 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
349 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
350 $out->wrapWikiMsg(
351 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
352 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ?
353 'rev-suppressed-text-permission' : 'rev-deleted-text-permission'
354 );
355
356 return;
357 }
358
359 $out->wrapWikiMsg(
360 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
361 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ?
362 'rev-suppressed-text-view' : 'rev-deleted-text-view'
363 );
364 $out->addHTML( '<br />' );
365 // and we are allowed to see...
366 }
367
368 if ( $this->mDiff ) {
369 $previousRev = $archive->getPreviousRevision( $timestamp );
370 if ( $previousRev ) {
371 $this->showDiff( $previousRev, $rev );
372 if ( $this->mDiffOnly ) {
373 return;
374 }
375
376 $out->addHTML( '<hr />' );
377 } else {
378 $out->addWikiMsg( 'undelete-nodiff' );
379 }
380 }
381
382 $link = $this->getLinkRenderer()->makeKnownLink(
383 $this->getPageTitle( $this->mTargetObj->getPrefixedDBkey() ),
384 $this->mTargetObj->getPrefixedText()
385 );
386
387 $lang = $this->getLanguage();
388
389 // date and time are separate parameters to facilitate localisation.
390 // $time is kept for backward compat reasons.
391 $time = $lang->userTimeAndDate( $timestamp, $user );
392 $d = $lang->userDate( $timestamp, $user );
393 $t = $lang->userTime( $timestamp, $user );
394 $userLink = Linker::revUserTools( $rev );
395
396 $content = $rev->getContent( Revision::FOR_THIS_USER, $user );
397
398 $isText = ( $content instanceof TextContent );
399
400 if ( $this->mPreview || $isText ) {
401 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
402 } else {
403 $openDiv = '<div id="mw-undelete-revision">';
404 }
405 $out->addHTML( $openDiv );
406
407 // Revision delete links
408 if ( !$this->mDiff ) {
409 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
410 if ( $revdel ) {
411 $out->addHTML( "$revdel " );
412 }
413 }
414
415 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
416 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
417
418 if ( !Hooks::run( 'UndeleteShowRevision', [ $this->mTargetObj, $rev ] ) ) {
419 return;
420 }
421
422 if ( ( $this->mPreview || !$isText ) && $content ) {
423 // NOTE: non-text content has no source view, so always use rendered preview
424
425 // Hide [edit]s
426 $popts = $out->parserOptions();
427 $popts->setEditSection( false );
428
429 $pout = $content->getParserOutput( $this->mTargetObj, $rev->getId(), $popts, true );
430 $out->addParserOutput( $pout );
431 }
432
433 if ( $isText ) {
434 // source view for textual content
435 $sourceView = Xml::element(
436 'textarea',
437 [
438 'readonly' => 'readonly',
439 'cols' => 80,
440 'rows' => 25
441 ],
442 $content->getNativeData() . "\n"
443 );
444
445 $previewButton = Xml::element( 'input', [
446 'type' => 'submit',
447 'name' => 'preview',
448 'value' => $this->msg( 'showpreview' )->text()
449 ] );
450 } else {
451 $sourceView = '';
452 $previewButton = '';
453 }
454
455 $diffButton = Xml::element( 'input', [
456 'name' => 'diff',
457 'type' => 'submit',
458 'value' => $this->msg( 'showdiff' )->text() ] );
459
460 $out->addHTML(
461 $sourceView .
462 Xml::openElement( 'div', [
463 'style' => 'clear: both' ] ) .
464 Xml::openElement( 'form', [
465 'method' => 'post',
466 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ) ] ) .
467 Xml::element( 'input', [
468 'type' => 'hidden',
469 'name' => 'target',
470 'value' => $this->mTargetObj->getPrefixedDBkey() ] ) .
471 Xml::element( 'input', [
472 'type' => 'hidden',
473 'name' => 'timestamp',
474 'value' => $timestamp ] ) .
475 Xml::element( 'input', [
476 'type' => 'hidden',
477 'name' => 'wpEditToken',
478 'value' => $user->getEditToken() ] ) .
479 $previewButton .
480 $diffButton .
481 Xml::closeElement( 'form' ) .
482 Xml::closeElement( 'div' )
483 );
484 }
485
486 /**
487 * Build a diff display between this and the previous either deleted
488 * or non-deleted edit.
489 *
490 * @param Revision $previousRev
491 * @param Revision $currentRev
492 * @return string HTML
493 */
494 function showDiff( $previousRev, $currentRev ) {
495 $diffContext = clone $this->getContext();
496 $diffContext->setTitle( $currentRev->getTitle() );
497 $diffContext->setWikiPage( WikiPage::factory( $currentRev->getTitle() ) );
498
499 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
500 $diffEngine->showDiffStyle();
501
502 $formattedDiff = $diffEngine->generateContentDiffBody(
503 $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
504 $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
505 );
506
507 $formattedDiff = $diffEngine->addHeader(
508 $formattedDiff,
509 $this->diffHeader( $previousRev, 'o' ),
510 $this->diffHeader( $currentRev, 'n' )
511 );
512
513 $this->getOutput()->addHTML( "<div>$formattedDiff</div>\n" );
514 }
515
516 /**
517 * @param Revision $rev
518 * @param string $prefix
519 * @return string
520 */
521 private function diffHeader( $rev, $prefix ) {
522 $isDeleted = !( $rev->getId() && $rev->getTitle() );
523 if ( $isDeleted ) {
524 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
525 $targetPage = $this->getPageTitle();
526 $targetQuery = [
527 'target' => $this->mTargetObj->getPrefixedText(),
528 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
529 ];
530 } else {
531 /// @todo FIXME: getId() may return non-zero for deleted revs...
532 $targetPage = $rev->getTitle();
533 $targetQuery = [ 'oldid' => $rev->getId() ];
534 }
535
536 // Add show/hide deletion links if available
537 $user = $this->getUser();
538 $lang = $this->getLanguage();
539 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
540
541 if ( $rdel ) {
542 $rdel = " $rdel";
543 }
544
545 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
546
547 $tags = wfGetDB( DB_REPLICA )->selectField(
548 'tag_summary',
549 'ts_tags',
550 [ 'ts_rev_id' => $rev->getId() ],
551 __METHOD__
552 );
553 $tagSummary = ChangeTags::formatSummaryRow( $tags, 'deleteddiff', $this->getContext() );
554
555 // FIXME This is reimplementing DifferenceEngine#getRevisionHeader
556 // and partially #showDiffPage, but worse
557 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
558 $this->getLinkRenderer()->makeLink(
559 $targetPage,
560 $this->msg(
561 'revisionasof',
562 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
563 $lang->userDate( $rev->getTimestamp(), $user ),
564 $lang->userTime( $rev->getTimestamp(), $user )
565 )->text(),
566 [],
567 $targetQuery
568 ) .
569 '</strong></div>' .
570 '<div id="mw-diff-' . $prefix . 'title2">' .
571 Linker::revUserTools( $rev ) . '<br />' .
572 '</div>' .
573 '<div id="mw-diff-' . $prefix . 'title3">' .
574 $minor . Linker::revComment( $rev ) . $rdel . '<br />' .
575 '</div>' .
576 '<div id="mw-diff-' . $prefix . 'title5">' .
577 $tagSummary[0] . '<br />' .
578 '</div>';
579 }
580
581 /**
582 * Show a form confirming whether a tokenless user really wants to see a file
583 * @param string $key
584 */
585 private function showFileConfirmationForm( $key ) {
586 $out = $this->getOutput();
587 $lang = $this->getLanguage();
588 $user = $this->getUser();
589 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
590 $out->addWikiMsg( 'undelete-show-file-confirm',
591 $this->mTargetObj->getText(),
592 $lang->userDate( $file->getTimestamp(), $user ),
593 $lang->userTime( $file->getTimestamp(), $user ) );
594 $out->addHTML(
595 Xml::openElement( 'form', [
596 'method' => 'POST',
597 'action' => $this->getPageTitle()->getLocalURL( [
598 'target' => $this->mTarget,
599 'file' => $key,
600 'token' => $user->getEditToken( $key ),
601 ] ),
602 ]
603 ) .
604 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
605 '</form>'
606 );
607 }
608
609 /**
610 * Show a deleted file version requested by the visitor.
611 * @param string $key
612 */
613 private function showFile( $key ) {
614 $this->getOutput()->disable();
615
616 # We mustn't allow the output to be CDN cached, otherwise
617 # if an admin previews a deleted image, and it's cached, then
618 # a user without appropriate permissions can toddle off and
619 # nab the image, and CDN will serve it
620 $response = $this->getRequest()->response();
621 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
622 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
623 $response->header( 'Pragma: no-cache' );
624
625 $repo = RepoGroup::singleton()->getLocalRepo();
626 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
627 $repo->streamFile( $path );
628 }
629
630 protected function showHistory() {
631 $this->checkReadOnly();
632
633 $out = $this->getOutput();
634 if ( $this->mAllowed ) {
635 $out->addModules( 'mediawiki.special.undelete' );
636 }
637 $out->wrapWikiMsg(
638 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
639 [ 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) ]
640 );
641
642 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
643 Hooks::run( 'UndeleteForm::showHistory', [ &$archive, $this->mTargetObj ] );
644 /*
645 $text = $archive->getLastRevisionText();
646 if( is_null( $text ) ) {
647 $out->addWikiMsg( 'nohistory' );
648 return;
649 }
650 */
651 $out->addHTML( '<div class="mw-undelete-history">' );
652 if ( $this->mAllowed ) {
653 $out->addWikiMsg( 'undeletehistory' );
654 $out->addWikiMsg( 'undeleterevdel' );
655 } else {
656 $out->addWikiMsg( 'undeletehistorynoadmin' );
657 }
658 $out->addHTML( '</div>' );
659
660 # List all stored revisions
661 $revisions = $archive->listRevisions();
662 $files = $archive->listFiles();
663
664 $haveRevisions = $revisions && $revisions->numRows() > 0;
665 $haveFiles = $files && $files->numRows() > 0;
666
667 # Batch existence check on user and talk pages
668 if ( $haveRevisions ) {
669 $batch = new LinkBatch();
670 foreach ( $revisions as $row ) {
671 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
672 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
673 }
674 $batch->execute();
675 $revisions->seek( 0 );
676 }
677 if ( $haveFiles ) {
678 $batch = new LinkBatch();
679 foreach ( $files as $row ) {
680 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
681 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
682 }
683 $batch->execute();
684 $files->seek( 0 );
685 }
686
687 if ( $this->mAllowed ) {
688 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
689 # Start the form here
690 $top = Xml::openElement(
691 'form',
692 [ 'method' => 'post', 'action' => $action, 'id' => 'undelete' ]
693 );
694 $out->addHTML( $top );
695 }
696
697 # Show relevant lines from the deletion log:
698 $deleteLogPage = new LogPage( 'delete' );
699 $out->addHTML( Xml::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
700 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
701 # Show relevant lines from the suppression log:
702 $suppressLogPage = new LogPage( 'suppress' );
703 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
704 $out->addHTML( Xml::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
705 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
706 }
707
708 if ( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
709 # Format the user-visible controls (comment field, submission button)
710 # in a nice little table
711 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
712 $unsuppressBox =
713 "<tr>
714 <td>&#160;</td>
715 <td class='mw-input'>" .
716 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
717 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ) .
718 "</td>
719 </tr>";
720 } else {
721 $unsuppressBox = '';
722 }
723
724 $table = Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
725 Xml::openElement( 'table', [ 'id' => 'mw-undelete-table' ] ) .
726 "<tr>
727 <td colspan='2' class='mw-undelete-extrahelp'>" .
728 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
729 "</td>
730 </tr>
731 <tr>
732 <td class='mw-label'>" .
733 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
734 "</td>
735 <td class='mw-input'>" .
736 Xml::input(
737 'wpComment',
738 50,
739 $this->mComment,
740 [ 'id' => 'wpComment', 'autofocus' => '' ]
741 ) .
742 "</td>
743 </tr>
744 <tr>
745 <td>&#160;</td>
746 <td class='mw-submit'>" .
747 Xml::submitButton(
748 $this->msg( 'undeletebtn' )->text(),
749 [ 'name' => 'restore', 'id' => 'mw-undelete-submit' ]
750 ) . ' ' .
751 Xml::submitButton(
752 $this->msg( 'undeleteinvert' )->text(),
753 [ 'name' => 'invert', 'id' => 'mw-undelete-invert' ]
754 ) .
755 "</td>
756 </tr>" .
757 $unsuppressBox .
758 Xml::closeElement( 'table' ) .
759 Xml::closeElement( 'fieldset' );
760
761 $out->addHTML( $table );
762 }
763
764 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
765
766 if ( $haveRevisions ) {
767 # Show the page's stored (deleted) history
768
769 if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
770 $out->addHTML( Html::element(
771 'button',
772 [
773 'name' => 'revdel',
774 'type' => 'submit',
775 'class' => 'deleterevision-log-submit mw-log-deleterevision-button'
776 ],
777 $this->msg( 'showhideselectedversions' )->text()
778 ) . "\n" );
779 }
780
781 $out->addHTML( '<ul>' );
782 $remaining = $revisions->numRows();
783 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
784
785 foreach ( $revisions as $row ) {
786 $remaining--;
787 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
788 }
789 $revisions->free();
790 $out->addHTML( '</ul>' );
791 } else {
792 $out->addWikiMsg( 'nohistory' );
793 }
794
795 if ( $haveFiles ) {
796 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
797 $out->addHTML( '<ul>' );
798 foreach ( $files as $row ) {
799 $out->addHTML( $this->formatFileRow( $row ) );
800 }
801 $files->free();
802 $out->addHTML( '</ul>' );
803 }
804
805 if ( $this->mAllowed ) {
806 # Slip in the hidden controls here
807 $misc = Html::hidden( 'target', $this->mTarget );
808 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
809 $misc .= Xml::closeElement( 'form' );
810 $out->addHTML( $misc );
811 }
812
813 return true;
814 }
815
816 protected function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
817 $rev = Revision::newFromArchiveRow( $row,
818 [
819 'title' => $this->mTargetObj
820 ] );
821
822 $revTextSize = '';
823 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
824 // Build checkboxen...
825 if ( $this->mAllowed ) {
826 if ( $this->mInvert ) {
827 if ( in_array( $ts, $this->mTargetTimestamp ) ) {
828 $checkBox = Xml::check( "ts$ts" );
829 } else {
830 $checkBox = Xml::check( "ts$ts", true );
831 }
832 } else {
833 $checkBox = Xml::check( "ts$ts" );
834 }
835 } else {
836 $checkBox = '';
837 }
838
839 // Build page & diff links...
840 $user = $this->getUser();
841 if ( $this->mCanView ) {
842 $titleObj = $this->getPageTitle();
843 # Last link
844 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
845 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
846 $last = $this->msg( 'diff' )->escaped();
847 } elseif ( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
848 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
849 $last = $this->getLinkRenderer()->makeKnownLink(
850 $titleObj,
851 $this->msg( 'diff' )->text(),
852 [],
853 [
854 'target' => $this->mTargetObj->getPrefixedText(),
855 'timestamp' => $ts,
856 'diff' => 'prev'
857 ]
858 );
859 } else {
860 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
861 $last = $this->msg( 'diff' )->escaped();
862 }
863 } else {
864 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
865 $last = $this->msg( 'diff' )->escaped();
866 }
867
868 // User links
869 $userLink = Linker::revUserTools( $rev );
870
871 // Minor edit
872 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
873
874 // Revision text size
875 $size = $row->ar_len;
876 if ( !is_null( $size ) ) {
877 $revTextSize = Linker::formatRevisionSize( $size );
878 }
879
880 // Edit summary
881 $comment = Linker::revComment( $rev );
882
883 // Tags
884 $attribs = [];
885 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow(
886 $row->ts_tags,
887 'deletedhistory',
888 $this->getContext()
889 );
890 if ( $classes ) {
891 $attribs['class'] = implode( ' ', $classes );
892 }
893
894 $revisionRow = $this->msg( 'undelete-revision-row2' )
895 ->rawParams(
896 $checkBox,
897 $last,
898 $pageLink,
899 $userLink,
900 $minor,
901 $revTextSize,
902 $comment,
903 $tagSummary
904 )
905 ->escaped();
906
907 return Xml::tags( 'li', $attribs, $revisionRow ) . "\n";
908 }
909
910 private function formatFileRow( $row ) {
911 $file = ArchivedFile::newFromRow( $row );
912 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
913 $user = $this->getUser();
914
915 $checkBox = '';
916 if ( $this->mCanView && $row->fa_storage_key ) {
917 if ( $this->mAllowed ) {
918 $checkBox = Xml::check( 'fileid' . $row->fa_id );
919 }
920 $key = urlencode( $row->fa_storage_key );
921 $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
922 } else {
923 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
924 }
925 $userLink = $this->getFileUser( $file );
926 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
927 $bytes = $this->msg( 'parentheses' )
928 ->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )
929 ->plain();
930 $data = htmlspecialchars( $data . ' ' . $bytes );
931 $comment = $this->getFileComment( $file );
932
933 // Add show/hide deletion links if available
934 $canHide = $this->isAllowed( 'deleterevision' );
935 if ( $canHide || ( $file->getVisibility() && $this->isAllowed( 'deletedhistory' ) ) ) {
936 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
937 // Revision was hidden from sysops
938 $revdlink = Linker::revDeleteLinkDisabled( $canHide );
939 } else {
940 $query = [
941 'type' => 'filearchive',
942 'target' => $this->mTargetObj->getPrefixedDBkey(),
943 'ids' => $row->fa_id
944 ];
945 $revdlink = Linker::revDeleteLink( $query,
946 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
947 }
948 } else {
949 $revdlink = '';
950 }
951
952 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
953 }
954
955 /**
956 * Fetch revision text link if it's available to all users
957 *
958 * @param Revision $rev
959 * @param Title $titleObj
960 * @param string $ts Timestamp
961 * @return string
962 */
963 function getPageLink( $rev, $titleObj, $ts ) {
964 $user = $this->getUser();
965 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
966
967 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
968 return '<span class="history-deleted">' . $time . '</span>';
969 }
970
971 $link = $this->getLinkRenderer()->makeKnownLink(
972 $titleObj,
973 $time,
974 [],
975 [
976 'target' => $this->mTargetObj->getPrefixedText(),
977 'timestamp' => $ts
978 ]
979 );
980
981 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
982 $link = '<span class="history-deleted">' . $link . '</span>';
983 }
984
985 return $link;
986 }
987
988 /**
989 * Fetch image view link if it's available to all users
990 *
991 * @param File|ArchivedFile $file
992 * @param Title $titleObj
993 * @param string $ts A timestamp
994 * @param string $key A storage key
995 *
996 * @return string HTML fragment
997 */
998 function getFileLink( $file, $titleObj, $ts, $key ) {
999 $user = $this->getUser();
1000 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1001
1002 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
1003 return '<span class="history-deleted">' . $time . '</span>';
1004 }
1005
1006 $link = $this->getLinkRenderer()->makeKnownLink(
1007 $titleObj,
1008 $time,
1009 [],
1010 [
1011 'target' => $this->mTargetObj->getPrefixedText(),
1012 'file' => $key,
1013 'token' => $user->getEditToken( $key )
1014 ]
1015 );
1016
1017 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1018 $link = '<span class="history-deleted">' . $link . '</span>';
1019 }
1020
1021 return $link;
1022 }
1023
1024 /**
1025 * Fetch file's user id if it's available to this user
1026 *
1027 * @param File|ArchivedFile $file
1028 * @return string HTML fragment
1029 */
1030 function getFileUser( $file ) {
1031 if ( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1032 return '<span class="history-deleted">' .
1033 $this->msg( 'rev-deleted-user' )->escaped() .
1034 '</span>';
1035 }
1036
1037 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1038 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1039
1040 if ( $file->isDeleted( File::DELETED_USER ) ) {
1041 $link = '<span class="history-deleted">' . $link . '</span>';
1042 }
1043
1044 return $link;
1045 }
1046
1047 /**
1048 * Fetch file upload comment if it's available to this user
1049 *
1050 * @param File|ArchivedFile $file
1051 * @return string HTML fragment
1052 */
1053 function getFileComment( $file ) {
1054 if ( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1055 return '<span class="history-deleted"><span class="comment">' .
1056 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1057 }
1058
1059 $link = Linker::commentBlock( $file->getRawDescription() );
1060
1061 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1062 $link = '<span class="history-deleted">' . $link . '</span>';
1063 }
1064
1065 return $link;
1066 }
1067
1068 function undelete() {
1069 if ( $this->getConfig()->get( 'UploadMaintenance' )
1070 && $this->mTargetObj->getNamespace() == NS_FILE
1071 ) {
1072 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1073 }
1074
1075 $this->checkReadOnly();
1076
1077 $out = $this->getOutput();
1078 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
1079 Hooks::run( 'UndeleteForm::undelete', [ &$archive, $this->mTargetObj ] );
1080 $ok = $archive->undelete(
1081 $this->mTargetTimestamp,
1082 $this->mComment,
1083 $this->mFileVersions,
1084 $this->mUnsuppress,
1085 $this->getUser()
1086 );
1087
1088 if ( is_array( $ok ) ) {
1089 if ( $ok[1] ) { // Undeleted file count
1090 Hooks::run( 'FileUndeleteComplete', [
1091 $this->mTargetObj, $this->mFileVersions,
1092 $this->getUser(), $this->mComment ] );
1093 }
1094
1095 $link = $this->getLinkRenderer()->makeKnownLink( $this->mTargetObj );
1096 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1097 } else {
1098 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1099 }
1100
1101 // Show revision undeletion warnings and errors
1102 $status = $archive->getRevisionStatus();
1103 if ( $status && !$status->isGood() ) {
1104 $out->addWikiText( '<div class="error" id="mw-error-cannotundelete">' .
1105 $status->getWikiText(
1106 'cannotundelete',
1107 'cannotundelete'
1108 ) . '</div>'
1109 );
1110 }
1111
1112 // Show file undeletion warnings and errors
1113 $status = $archive->getFileStatus();
1114 if ( $status && !$status->isGood() ) {
1115 $out->addWikiText( '<div class="error">' .
1116 $status->getWikiText(
1117 'undelete-error-short',
1118 'undelete-error-long'
1119 ) . '</div>'
1120 );
1121 }
1122 }
1123
1124 /**
1125 * Return an array of subpages beginning with $search that this special page will accept.
1126 *
1127 * @param string $search Prefix to search for
1128 * @param int $limit Maximum number of results to return (usually 10)
1129 * @param int $offset Number of results to skip (usually 0)
1130 * @return string[] Matching subpages
1131 */
1132 public function prefixSearchSubpages( $search, $limit, $offset ) {
1133 return $this->prefixSearchString( $search, $limit, $offset );
1134 }
1135
1136 protected function getGroupName() {
1137 return 'pagetools';
1138 }
1139 }