3 * Implements Special:Undelete
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.
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.
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
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
25 use MediaWiki\Revision\RevisionRecord
;
26 use Wikimedia\Rdbms\IResultWrapper
;
29 * Special page allowing users with the appropriate permissions to view
30 * and restore deleted content.
32 * @ingroup SpecialPage
34 class SpecialUndelete
extends SpecialPage
{
42 private $mTargetTimestamp;
51 * @var string Search prefix
53 private $mSearchPrefix;
55 function __construct() {
56 parent
::__construct( 'Undelete', 'deletedhistory' );
59 public function doesWrites() {
63 function loadRequest( $par ) {
64 $request = $this->getRequest();
65 $user = $this->getUser();
67 $this->mAction
= $request->getVal( 'action' );
68 if ( $par !== null && $par !== '' ) {
69 $this->mTarget
= $par;
71 $this->mTarget
= $request->getVal( 'target' );
74 $this->mTargetObj
= null;
76 if ( $this->mTarget
!== null && $this->mTarget
!== '' ) {
77 $this->mTargetObj
= Title
::newFromText( $this->mTarget
);
80 $this->mSearchPrefix
= $request->getText( 'prefix' );
81 $time = $request->getVal( 'timestamp' );
82 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
83 $this->mFilename
= $request->getVal( 'file' );
85 $posted = $request->wasPosted() &&
86 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
87 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
88 $this->mRevdel
= $request->getCheck( 'revdel' ) && $posted;
89 $this->mInvert
= $request->getCheck( 'invert' ) && $posted;
90 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
91 $this->mDiff
= $request->getCheck( 'diff' );
92 $this->mDiffOnly
= $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
93 $this->mComment
= $request->getText( 'wpComment' );
94 $this->mUnsuppress
= $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
95 $this->mToken
= $request->getVal( 'token' );
97 if ( $this->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
98 $this->mAllowed
= true; // user can restore
99 $this->mCanView
= true; // user can view content
100 } elseif ( $this->isAllowed( 'deletedtext' ) ) {
101 $this->mAllowed
= false; // user cannot restore
102 $this->mCanView
= true; // user can view content
103 $this->mRestore
= false;
104 } else { // user can only view the list of revisions
105 $this->mAllowed
= false;
106 $this->mCanView
= false;
107 $this->mTimestamp
= '';
108 $this->mRestore
= false;
111 if ( $this->mRestore ||
$this->mInvert
) {
113 $this->mFileVersions
= [];
114 foreach ( $request->getValues() as $key => $val ) {
116 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
117 array_push( $timestamps, $matches[1] );
120 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
121 $this->mFileVersions
[] = intval( $matches[1] );
124 rsort( $timestamps );
125 $this->mTargetTimestamp
= $timestamps;
130 * Checks whether a user is allowed the permission for the
131 * specific title if one is set.
133 * @param string $permission
134 * @param User|null $user
137 protected function isAllowed( $permission, User
$user = null ) {
138 $user = $user ?
: $this->getUser();
139 if ( $this->mTargetObj
!== null ) {
140 return $this->mTargetObj
->userCan( $permission, $user );
142 return $user->isAllowed( $permission );
146 function userCanExecute( User
$user ) {
147 return $this->isAllowed( $this->mRestriction
, $user );
150 function execute( $par ) {
151 $this->useTransactionalTimeLimit();
153 $user = $this->getUser();
156 $this->outputHeader();
158 $this->loadRequest( $par );
159 $this->checkPermissions(); // Needs to be after mTargetObj is set
161 $out = $this->getOutput();
163 if ( is_null( $this->mTargetObj
) ) {
164 $out->addWikiMsg( 'undelete-header' );
166 # Not all users can just browse every deleted page from the list
167 if ( $user->isAllowed( 'browsearchive' ) ) {
168 $this->showSearchForm();
174 $this->addHelpLink( 'Help:Undelete' );
175 if ( $this->mAllowed
) {
176 $out->setPageTitle( $this->msg( 'undeletepage' ) );
178 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
181 $this->getSkin()->setRelevantTitle( $this->mTargetObj
);
183 if ( $this->mTimestamp
!== '' ) {
184 $this->showRevision( $this->mTimestamp
);
185 } elseif ( $this->mFilename
!== null && $this->mTargetObj
->inNamespace( NS_FILE
) ) {
186 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
187 // Check if user is allowed to see this file
188 if ( !$file->exists() ) {
189 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename
);
190 } elseif ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
191 if ( $file->isDeleted( File
::DELETED_RESTRICTED
) ) {
192 throw new PermissionsError( 'suppressrevision' );
194 throw new PermissionsError( 'deletedtext' );
196 } elseif ( !$user->matchEditToken( $this->mToken
, $this->mFilename
) ) {
197 $this->showFileConfirmationForm( $this->mFilename
);
199 $this->showFile( $this->mFilename
);
201 } elseif ( $this->mAction
=== "submit" ) {
202 if ( $this->mRestore
) {
204 } elseif ( $this->mRevdel
) {
205 $this->redirectToRevDel();
209 $this->showHistory();
214 * Convert submitted form data to format expected by RevisionDelete and
215 * redirect the request
217 private function redirectToRevDel() {
218 $archive = new PageArchive( $this->mTargetObj
);
222 foreach ( $this->getRequest()->getValues() as $key => $val ) {
224 if ( preg_match( "/^ts(\d{14})$/", $key, $matches ) ) {
225 $revisions[ $archive->getRevision( $matches[1] )->getId() ] = 1;
229 "type" => "revision",
231 "target" => $this->mTargetObj
->getPrefixedText()
233 $url = SpecialPage
::getTitleFor( 'Revisiondelete' )->getFullURL( $query );
234 $this->getOutput()->redirect( $url );
237 function showSearchForm() {
238 $out = $this->getOutput();
239 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
240 $fuzzySearch = $this->getRequest()->getVal( 'fuzzy', true );
244 $fields[] = new OOUI\
ActionFieldLayout(
245 new OOUI\
TextInputWidget( [
247 'inputId' => 'prefix',
249 'value' => $this->mSearchPrefix
,
252 new OOUI\
ButtonInputWidget( [
253 'label' => $this->msg( 'undelete-search-submit' )->text(),
254 'flags' => [ 'primary', 'progressive' ],
255 'inputId' => 'searchUndelete',
259 'label' => new OOUI\
HtmlSnippet(
261 $fuzzySearch ?
'undelete-search-full' : 'undelete-search-prefix'
268 $fieldset = new OOUI\
FieldsetLayout( [
269 'label' => $this->msg( 'undelete-search-box' )->text(),
273 $form = new OOUI\
FormLayout( [
275 'action' => wfScript(),
278 $form->appendContent(
280 new OOUI\
HtmlSnippet(
281 Html
::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
282 Html
::hidden( 'fuzzy', $fuzzySearch )
287 new OOUI\
PanelLayout( [
295 # List undeletable articles
296 if ( $this->mSearchPrefix
) {
297 // For now, we enable search engine match only when specifically asked to
298 // by using fuzzy=1 parameter.
299 if ( $fuzzySearch ) {
300 $result = PageArchive
::listPagesBySearch( $this->mSearchPrefix
);
302 $result = PageArchive
::listPagesByPrefix( $this->mSearchPrefix
);
304 $this->showList( $result );
309 * Generic list of deleted pages
311 * @param IResultWrapper $result
314 private function showList( $result ) {
315 $out = $this->getOutput();
317 if ( $result->numRows() == 0 ) {
318 $out->addWikiMsg( 'undelete-no-results' );
323 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
325 $linkRenderer = $this->getLinkRenderer();
326 $undelete = $this->getPageTitle();
327 $out->addHTML( "<ul id='undeleteResultsList'>\n" );
328 foreach ( $result as $row ) {
329 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
330 if ( $title !== null ) {
331 $item = $linkRenderer->makeKnownLink(
333 $title->getPrefixedText(),
335 [ 'target' => $title->getPrefixedText() ]
338 // The title is no longer valid, show as text
339 $item = Html
::element(
341 [ 'class' => 'mw-invalidtitle' ],
342 Linker
::getInvalidTitleDescription(
349 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count
)->parse();
353 [ 'class' => 'undeleteResult' ],
359 $out->addHTML( "</ul>\n" );
364 private function showRevision( $timestamp ) {
365 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
369 $archive = new PageArchive( $this->mTargetObj
, $this->getConfig() );
370 if ( !Hooks
::run( 'UndeleteForm::showRevision', [ &$archive, $this->mTargetObj
] ) ) {
373 $rev = $archive->getRevision( $timestamp );
375 $out = $this->getOutput();
376 $user = $this->getUser();
379 $out->addWikiMsg( 'undeleterevision-missing' );
384 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
385 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
387 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
388 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) ?
389 'rev-suppressed-text-permission' : 'rev-deleted-text-permission'
396 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
397 $rev->isDeleted( Revision
::DELETED_RESTRICTED
) ?
398 'rev-suppressed-text-view' : 'rev-deleted-text-view'
400 $out->addHTML( '<br />' );
401 // and we are allowed to see...
404 if ( $this->mDiff
) {
405 $previousRev = $archive->getPreviousRevision( $timestamp );
406 if ( $previousRev ) {
407 $this->showDiff( $previousRev, $rev );
408 if ( $this->mDiffOnly
) {
412 $out->addHTML( '<hr />' );
414 $out->addWikiMsg( 'undelete-nodiff' );
418 $link = $this->getLinkRenderer()->makeKnownLink(
419 $this->getPageTitle( $this->mTargetObj
->getPrefixedDBkey() ),
420 $this->mTargetObj
->getPrefixedText()
423 $lang = $this->getLanguage();
425 // date and time are separate parameters to facilitate localisation.
426 // $time is kept for backward compat reasons.
427 $time = $lang->userTimeAndDate( $timestamp, $user );
428 $d = $lang->userDate( $timestamp, $user );
429 $t = $lang->userTime( $timestamp, $user );
430 $userLink = Linker
::revUserTools( $rev );
432 $content = $rev->getContent( RevisionRecord
::FOR_THIS_USER
, $user );
434 // TODO: MCR: this will have to become something like $hasTextSlots and $hasNonTextSlots
435 $isText = ( $content instanceof TextContent
);
437 if ( $this->mPreview ||
$isText ) {
438 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
440 $openDiv = '<div id="mw-undelete-revision">';
442 $out->addHTML( $openDiv );
444 // Revision delete links
445 if ( !$this->mDiff
) {
446 $revdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
448 $out->addHTML( "$revdel " );
454 Message
::rawParam( $link ), $time,
455 Message
::rawParam( $userLink ), $d, $t
457 $out->addHtml( '</div>' );
459 if ( !Hooks
::run( 'UndeleteShowRevision', [ $this->mTargetObj
, $rev ] ) ) {
463 if ( $this->mPreview ||
!$isText ) {
464 // NOTE: non-text content has no source view, so always use rendered preview
466 $popts = $out->parserOptions();
467 $renderer = MediaWikiServices
::getInstance()->getRevisionRenderer();
469 $rendered = $renderer->getRenderedRevision(
470 $rev->getRevisionRecord(),
473 [ 'audience' => RevisionRecord
::FOR_THIS_USER
]
476 // Fail hard if the audience check fails, since we already checked
477 // at the beginning of this method.
478 $pout = $rendered->getRevisionParserOutput();
480 $out->addParserOutput( $pout, [
481 'enableSectionEditLinks' => false,
489 // TODO: MCR: make this work for multiple slots
490 // source view for textual content
491 $sourceView = Xml
::element( 'textarea', [
492 'readonly' => 'readonly',
495 ], $content->getNativeData() . "\n" );
497 $buttonFields[] = new OOUI\
ButtonInputWidget( [
500 'label' => $this->msg( 'showpreview' )->text()
507 $buttonFields[] = new OOUI\
ButtonInputWidget( [
510 'label' => $this->msg( 'showdiff' )->text()
515 Xml
::openElement( 'div', [
516 'style' => 'clear: both' ] ) .
517 Xml
::openElement( 'form', [
519 'action' => $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] ) ] ) .
520 Xml
::element( 'input', [
523 'value' => $this->mTargetObj
->getPrefixedDBkey() ] ) .
524 Xml
::element( 'input', [
526 'name' => 'timestamp',
527 'value' => $timestamp ] ) .
528 Xml
::element( 'input', [
530 'name' => 'wpEditToken',
531 'value' => $user->getEditToken() ] ) .
532 new OOUI\
FieldLayout(
534 'content' => new OOUI\
HorizontalLayout( [
535 'items' => $buttonFields
539 Xml
::closeElement( 'form' ) .
540 Xml
::closeElement( 'div' )
545 * Build a diff display between this and the previous either deleted
546 * or non-deleted edit.
548 * @param Revision $previousRev
549 * @param Revision $currentRev
550 * @return string HTML
552 function showDiff( $previousRev, $currentRev ) {
553 $diffContext = clone $this->getContext();
554 $diffContext->setTitle( $currentRev->getTitle() );
555 $diffContext->setWikiPage( WikiPage
::factory( $currentRev->getTitle() ) );
557 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
558 $diffEngine->setRevisions( $previousRev->getRevisionRecord(), $currentRev->getRevisionRecord() );
559 $diffEngine->showDiffStyle();
560 $formattedDiff = $diffEngine->getDiff(
561 $this->diffHeader( $previousRev, 'o' ),
562 $this->diffHeader( $currentRev, 'n' )
565 $this->getOutput()->addHTML( "<div>$formattedDiff</div>\n" );
569 * @param Revision $rev
570 * @param string $prefix
573 private function diffHeader( $rev, $prefix ) {
574 $isDeleted = !( $rev->getId() && $rev->getTitle() );
576 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
577 $targetPage = $this->getPageTitle();
579 'target' => $this->mTargetObj
->getPrefixedText(),
580 'timestamp' => wfTimestamp( TS_MW
, $rev->getTimestamp() )
583 /// @todo FIXME: getId() may return non-zero for deleted revs...
584 $targetPage = $rev->getTitle();
585 $targetQuery = [ 'oldid' => $rev->getId() ];
588 // Add show/hide deletion links if available
589 $user = $this->getUser();
590 $lang = $this->getLanguage();
591 $rdel = Linker
::getRevDeleteLink( $user, $rev, $this->mTargetObj
);
597 $minor = $rev->isMinor() ? ChangesList
::flag( 'minor' ) : '';
599 $tags = wfGetDB( DB_REPLICA
)->selectField(
602 [ 'ts_rev_id' => $rev->getId() ],
605 $tagSummary = ChangeTags
::formatSummaryRow( $tags, 'deleteddiff', $this->getContext() );
607 // FIXME This is reimplementing DifferenceEngine#getRevisionHeader
608 // and partially #showDiffPage, but worse
609 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
610 $this->getLinkRenderer()->makeLink(
614 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
615 $lang->userDate( $rev->getTimestamp(), $user ),
616 $lang->userTime( $rev->getTimestamp(), $user )
622 '<div id="mw-diff-' . $prefix . 'title2">' .
623 Linker
::revUserTools( $rev ) . '<br />' .
625 '<div id="mw-diff-' . $prefix . 'title3">' .
626 $minor . Linker
::revComment( $rev ) . $rdel . '<br />' .
628 '<div id="mw-diff-' . $prefix . 'title5">' .
629 $tagSummary[0] . '<br />' .
634 * Show a form confirming whether a tokenless user really wants to see a file
637 private function showFileConfirmationForm( $key ) {
638 $out = $this->getOutput();
639 $lang = $this->getLanguage();
640 $user = $this->getUser();
641 $file = new ArchivedFile( $this->mTargetObj
, '', $this->mFilename
);
642 $out->addWikiMsg( 'undelete-show-file-confirm',
643 $this->mTargetObj
->getText(),
644 $lang->userDate( $file->getTimestamp(), $user ),
645 $lang->userTime( $file->getTimestamp(), $user ) );
647 Xml
::openElement( 'form', [
649 'action' => $this->getPageTitle()->getLocalURL( [
650 'target' => $this->mTarget
,
652 'token' => $user->getEditToken( $key ),
656 Xml
::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
662 * Show a deleted file version requested by the visitor.
665 private function showFile( $key ) {
666 $this->getOutput()->disable();
668 # We mustn't allow the output to be CDN cached, otherwise
669 # if an admin previews a deleted image, and it's cached, then
670 # a user without appropriate permissions can toddle off and
671 # nab the image, and CDN will serve it
672 $response = $this->getRequest()->response();
673 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
674 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
675 $response->header( 'Pragma: no-cache' );
677 $repo = RepoGroup
::singleton()->getLocalRepo();
678 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
679 $repo->streamFile( $path );
682 protected function showHistory() {
683 $this->checkReadOnly();
685 $out = $this->getOutput();
686 if ( $this->mAllowed
) {
687 $out->addModules( 'mediawiki.special.undelete' );
690 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
691 [ 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj
->getPrefixedText() ) ]
694 $archive = new PageArchive( $this->mTargetObj
, $this->getConfig() );
695 Hooks
::run( 'UndeleteForm::showHistory', [ &$archive, $this->mTargetObj
] );
697 $out->addHTML( '<div class="mw-undelete-history">' );
698 if ( $this->mAllowed
) {
699 $out->addWikiMsg( 'undeletehistory' );
700 $out->addWikiMsg( 'undeleterevdel' );
702 $out->addWikiMsg( 'undeletehistorynoadmin' );
704 $out->addHTML( '</div>' );
706 # List all stored revisions
707 $revisions = $archive->listRevisions();
708 $files = $archive->listFiles();
710 $haveRevisions = $revisions && $revisions->numRows() > 0;
711 $haveFiles = $files && $files->numRows() > 0;
713 # Batch existence check on user and talk pages
714 if ( $haveRevisions ) {
715 $batch = new LinkBatch();
716 foreach ( $revisions as $row ) {
717 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
718 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
721 $revisions->seek( 0 );
724 $batch = new LinkBatch();
725 foreach ( $files as $row ) {
726 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
727 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
733 if ( $this->mAllowed
) {
736 $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
737 # Start the form here
738 $form = new OOUI\
FormLayout( [
745 # Show relevant lines from the deletion log:
746 $deleteLogPage = new LogPage( 'delete' );
747 $out->addHTML( Xml
::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
748 LogEventsList
::showLogExtract( $out, 'delete', $this->mTargetObj
);
749 # Show relevant lines from the suppression log:
750 $suppressLogPage = new LogPage( 'suppress' );
751 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
752 $out->addHTML( Xml
::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
753 LogEventsList
::showLogExtract( $out, 'suppress', $this->mTargetObj
);
756 if ( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
757 $fields[] = new OOUI\
Layout( [
758 'content' => new OOUI\
HtmlSnippet( $this->msg( 'undeleteextrahelp' )->parseAsBlock() )
761 $conf = $this->getConfig();
762 $oldCommentSchema = $conf->get( 'CommentTableSchemaMigrationStage' ) === MIGRATION_OLD
;
764 $fields[] = new OOUI\
FieldLayout(
765 new OOUI\
TextInputWidget( [
766 'name' => 'wpComment',
767 'inputId' => 'wpComment',
769 'value' => $this->mComment
,
771 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
772 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
773 // Unicode codepoints (or 255 UTF-8 bytes for old schema).
774 'maxLength' => $oldCommentSchema ?
255 : CommentStore
::COMMENT_CHARACTER_LIMIT
,
777 'label' => $this->msg( 'undeletecomment' )->text(),
782 $fields[] = new OOUI\
FieldLayout(
784 'content' => new OOUI\
HorizontalLayout( [
786 new OOUI\
ButtonInputWidget( [
788 'inputId' => 'mw-undelete-submit',
790 'label' => $this->msg( 'undeletebtn' )->text(),
791 'flags' => [ 'primary', 'progressive' ],
794 new OOUI\
ButtonInputWidget( [
796 'inputId' => 'mw-undelete-invert',
798 'label' => $this->msg( 'undeleteinvert' )->text()
805 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
806 $fields[] = new OOUI\
FieldLayout(
807 new OOUI\
CheckboxInputWidget( [
808 'name' => 'wpUnsuppress',
809 'inputId' => 'mw-undelete-unsuppress',
813 'label' => $this->msg( 'revdelete-unsuppress' )->text(),
819 $fieldset = new OOUI\
FieldsetLayout( [
820 'label' => $this->msg( 'undelete-fieldset-title' )->text(),
821 'id' => 'mw-undelete-table',
825 $form->appendContent(
826 new OOUI\
PanelLayout( [
830 'content' => $fieldset,
832 new OOUI\
HtmlSnippet(
833 Html
::hidden( 'target', $this->mTarget
) .
834 Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() )
840 $history .= Xml
::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n";
842 if ( $haveRevisions ) {
843 # Show the page's stored (deleted) history
845 if ( $this->getUser()->isAllowed( 'deleterevision' ) ) {
846 $history .= Html
::element(
851 'class' => 'deleterevision-log-submit mw-log-deleterevision-button'
853 $this->msg( 'showhideselectedversions' )->text()
857 $history .= '<ul class="mw-undelete-revlist">';
858 $remaining = $revisions->numRows();
859 $earliestLiveTime = $this->mTargetObj
->getEarliestRevTime();
861 foreach ( $revisions as $row ) {
863 $history .= $this->formatRevisionRow( $row, $earliestLiveTime, $remaining );
868 $out->addWikiMsg( 'nohistory' );
872 $history .= Xml
::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n";
873 $history .= '<ul class="mw-undelete-revlist">';
874 foreach ( $files as $row ) {
875 $history .= $this->formatFileRow( $row );
881 if ( $this->mAllowed
) {
882 # Slip in the hidden controls here
883 $misc = Html
::hidden( 'target', $this->mTarget
);
884 $misc .= Html
::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
887 $form->appendContent( new OOUI\
HtmlSnippet( $history ) );
888 $out->addHTML( $form );
890 $out->addHTML( $history );
896 protected function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
897 $rev = Revision
::newFromArchiveRow( $row,
899 'title' => $this->mTargetObj
903 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
904 // Build checkboxen...
905 if ( $this->mAllowed
) {
906 if ( $this->mInvert
) {
907 if ( in_array( $ts, $this->mTargetTimestamp
) ) {
908 $checkBox = Xml
::check( "ts$ts" );
910 $checkBox = Xml
::check( "ts$ts", true );
913 $checkBox = Xml
::check( "ts$ts" );
919 // Build page & diff links...
920 $user = $this->getUser();
921 if ( $this->mCanView
) {
922 $titleObj = $this->getPageTitle();
924 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $this->getUser() ) ) {
925 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
926 $last = $this->msg( 'diff' )->escaped();
927 } elseif ( $remaining > 0 ||
( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
928 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
929 $last = $this->getLinkRenderer()->makeKnownLink(
931 $this->msg( 'diff' )->text(),
934 'target' => $this->mTargetObj
->getPrefixedText(),
940 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
941 $last = $this->msg( 'diff' )->escaped();
944 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
945 $last = $this->msg( 'diff' )->escaped();
949 $userLink = Linker
::revUserTools( $rev );
952 $minor = $rev->isMinor() ? ChangesList
::flag( 'minor' ) : '';
954 // Revision text size
955 $size = $row->ar_len
;
956 if ( !is_null( $size ) ) {
957 $revTextSize = Linker
::formatRevisionSize( $size );
961 $comment = Linker
::revComment( $rev );
965 list( $tagSummary, $classes ) = ChangeTags
::formatSummaryRow(
971 $attribs['class'] = implode( ' ', $classes );
974 $revisionRow = $this->msg( 'undelete-revision-row2' )
987 return Xml
::tags( 'li', $attribs, $revisionRow ) . "\n";
990 private function formatFileRow( $row ) {
991 $file = ArchivedFile
::newFromRow( $row );
992 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
993 $user = $this->getUser();
996 if ( $this->mCanView
&& $row->fa_storage_key
) {
997 if ( $this->mAllowed
) {
998 $checkBox = Xml
::check( 'fileid' . $row->fa_id
);
1000 $key = urlencode( $row->fa_storage_key
);
1001 $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
1003 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1005 $userLink = $this->getFileUser( $file );
1006 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width
, $row->fa_height
)->text();
1007 $bytes = $this->msg( 'parentheses' )
1008 ->plaintextParams( $this->msg( 'nbytes' )->numParams( $row->fa_size
)->text() )
1010 $data = htmlspecialchars( $data . ' ' . $bytes );
1011 $comment = $this->getFileComment( $file );
1013 // Add show/hide deletion links if available
1014 $canHide = $this->isAllowed( 'deleterevision' );
1015 if ( $canHide ||
( $file->getVisibility() && $this->isAllowed( 'deletedhistory' ) ) ) {
1016 if ( !$file->userCan( File
::DELETED_RESTRICTED
, $user ) ) {
1017 // Revision was hidden from sysops
1018 $revdlink = Linker
::revDeleteLinkDisabled( $canHide );
1021 'type' => 'filearchive',
1022 'target' => $this->mTargetObj
->getPrefixedDBkey(),
1023 'ids' => $row->fa_id
1025 $revdlink = Linker
::revDeleteLink( $query,
1026 $file->isDeleted( File
::DELETED_RESTRICTED
), $canHide );
1032 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1036 * Fetch revision text link if it's available to all users
1038 * @param Revision $rev
1039 * @param Title $titleObj
1040 * @param string $ts Timestamp
1043 function getPageLink( $rev, $titleObj, $ts ) {
1044 $user = $this->getUser();
1045 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1047 if ( !$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
1048 return '<span class="history-deleted">' . $time . '</span>';
1051 $link = $this->getLinkRenderer()->makeKnownLink(
1056 'target' => $this->mTargetObj
->getPrefixedText(),
1061 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
1062 $link = '<span class="history-deleted">' . $link . '</span>';
1069 * Fetch image view link if it's available to all users
1071 * @param File|ArchivedFile $file
1072 * @param Title $titleObj
1073 * @param string $ts A timestamp
1074 * @param string $key A storage key
1076 * @return string HTML fragment
1078 function getFileLink( $file, $titleObj, $ts, $key ) {
1079 $user = $this->getUser();
1080 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1082 if ( !$file->userCan( File
::DELETED_FILE
, $user ) ) {
1083 return '<span class="history-deleted">' . htmlspecialchars( $time ) . '</span>';
1086 $link = $this->getLinkRenderer()->makeKnownLink(
1091 'target' => $this->mTargetObj
->getPrefixedText(),
1093 'token' => $user->getEditToken( $key )
1097 if ( $file->isDeleted( File
::DELETED_FILE
) ) {
1098 $link = '<span class="history-deleted">' . $link . '</span>';
1105 * Fetch file's user id if it's available to this user
1107 * @param File|ArchivedFile $file
1108 * @return string HTML fragment
1110 function getFileUser( $file ) {
1111 if ( !$file->userCan( File
::DELETED_USER
, $this->getUser() ) ) {
1112 return '<span class="history-deleted">' .
1113 $this->msg( 'rev-deleted-user' )->escaped() .
1117 $link = Linker
::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1118 Linker
::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1120 if ( $file->isDeleted( File
::DELETED_USER
) ) {
1121 $link = '<span class="history-deleted">' . $link . '</span>';
1128 * Fetch file upload comment if it's available to this user
1130 * @param File|ArchivedFile $file
1131 * @return string HTML fragment
1133 function getFileComment( $file ) {
1134 if ( !$file->userCan( File
::DELETED_COMMENT
, $this->getUser() ) ) {
1135 return '<span class="history-deleted"><span class="comment">' .
1136 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1139 $link = Linker
::commentBlock( $file->getRawDescription() );
1141 if ( $file->isDeleted( File
::DELETED_COMMENT
) ) {
1142 $link = '<span class="history-deleted">' . $link . '</span>';
1148 function undelete() {
1149 if ( $this->getConfig()->get( 'UploadMaintenance' )
1150 && $this->mTargetObj
->getNamespace() == NS_FILE
1152 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1155 $this->checkReadOnly();
1157 $out = $this->getOutput();
1158 $archive = new PageArchive( $this->mTargetObj
, $this->getConfig() );
1159 Hooks
::run( 'UndeleteForm::undelete', [ &$archive, $this->mTargetObj
] );
1160 $ok = $archive->undelete(
1161 $this->mTargetTimestamp
,
1163 $this->mFileVersions
,
1168 if ( is_array( $ok ) ) {
1169 if ( $ok[1] ) { // Undeleted file count
1170 Hooks
::run( 'FileUndeleteComplete', [
1171 $this->mTargetObj
, $this->mFileVersions
,
1172 $this->getUser(), $this->mComment
] );
1175 $link = $this->getLinkRenderer()->makeKnownLink( $this->mTargetObj
);
1176 $out->addWikiMsg( 'undeletedpage', Message
::rawParam( $link ) );
1178 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1181 // Show revision undeletion warnings and errors
1182 $status = $archive->getRevisionStatus();
1183 if ( $status && !$status->isGood() ) {
1184 $out->wrapWikiTextAsInterface(
1186 '<div id="mw-error-cannotundelete">' .
1187 $status->getWikiText(
1194 // Show file undeletion warnings and errors
1195 $status = $archive->getFileStatus();
1196 if ( $status && !$status->isGood() ) {
1197 $out->wrapWikiTextAsInterface(
1199 $status->getWikiText(
1200 'undelete-error-short',
1201 'undelete-error-long'
1208 * Return an array of subpages beginning with $search that this special page will accept.
1210 * @param string $search Prefix to search for
1211 * @param int $limit Maximum number of results to return (usually 10)
1212 * @param int $offset Number of results to skip (usually 0)
1213 * @return string[] Matching subpages
1215 public function prefixSearchSubpages( $search, $limit, $offset ) {
1216 return $this->prefixSearchString( $search, $limit, $offset );
1219 protected function getGroupName() {