Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / specials / SpecialUndelete.php
blobaada064febfb03d9a9a3dbdda55f379fe9e979de
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * Used to show archived pages and eventually restore them.
27 * @ingroup SpecialPage
29 class PageArchive {
30 /** @var Title */
31 protected $title;
33 /** @var Status */
34 protected $fileStatus;
36 /** @var Status */
37 protected $revisionStatus;
39 /** @var Config */
40 protected $config;
42 function __construct( $title, Config $config = null ) {
43 if ( is_null( $title ) ) {
44 throw new MWException( __METHOD__ . ' given a null title.' );
46 $this->title = $title;
47 if ( $config === null ) {
48 wfDebug( __METHOD__ . ' did not have a Config object passed to it' );
49 $config = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
51 $this->config = $config;
54 /**
55 * List all deleted pages recorded in the archive table. Returns result
56 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
57 * namespace/title.
59 * @return ResultWrapper
61 public static function listAllPages() {
62 $dbr = wfGetDB( DB_SLAVE );
64 return self::listPages( $dbr, '' );
67 /**
68 * List deleted pages recorded in the archive table matching the
69 * given title prefix.
70 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
72 * @param string $prefix Title prefix
73 * @return ResultWrapper
75 public static function listPagesByPrefix( $prefix ) {
76 $dbr = wfGetDB( DB_SLAVE );
78 $title = Title::newFromText( $prefix );
79 if ( $title ) {
80 $ns = $title->getNamespace();
81 $prefix = $title->getDBkey();
82 } else {
83 // Prolly won't work too good
84 // @todo handle bare namespace names cleanly?
85 $ns = 0;
88 $conds = array(
89 'ar_namespace' => $ns,
90 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
93 return self::listPages( $dbr, $conds );
96 /**
97 * @param IDatabase $dbr
98 * @param string|array $condition
99 * @return bool|ResultWrapper
101 protected static function listPages( $dbr, $condition ) {
102 return $dbr->select(
103 array( 'archive' ),
104 array(
105 'ar_namespace',
106 'ar_title',
107 'count' => 'COUNT(*)'
109 $condition,
110 __METHOD__,
111 array(
112 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
113 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
114 'LIMIT' => 100,
120 * List the revisions of the given page. Returns result wrapper with
121 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
123 * @return ResultWrapper
125 function listRevisions() {
126 $dbr = wfGetDB( DB_SLAVE );
128 $tables = array( 'archive' );
130 $fields = array(
131 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
132 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
135 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
136 $fields[] = 'ar_content_format';
137 $fields[] = 'ar_content_model';
140 $conds = array( 'ar_namespace' => $this->title->getNamespace(),
141 'ar_title' => $this->title->getDBkey() );
143 $options = array( 'ORDER BY' => 'ar_timestamp DESC' );
145 $join_conds = array();
147 ChangeTags::modifyDisplayQuery(
148 $tables,
149 $fields,
150 $conds,
151 $join_conds,
152 $options,
156 return $dbr->select( $tables,
157 $fields,
158 $conds,
159 __METHOD__,
160 $options,
161 $join_conds
166 * List the deleted file revisions for this page, if it's a file page.
167 * Returns a result wrapper with various filearchive fields, or null
168 * if not a file page.
170 * @return ResultWrapper
171 * @todo Does this belong in Image for fuller encapsulation?
173 function listFiles() {
174 if ( $this->title->getNamespace() != NS_FILE ) {
175 return null;
178 $dbr = wfGetDB( DB_SLAVE );
179 return $dbr->select(
180 'filearchive',
181 ArchivedFile::selectFields(),
182 array( 'fa_name' => $this->title->getDBkey() ),
183 __METHOD__,
184 array( 'ORDER BY' => 'fa_timestamp DESC' )
189 * Return a Revision object containing data for the deleted revision.
190 * Note that the result *may* or *may not* have a null page ID.
192 * @param string $timestamp
193 * @return Revision|null
195 function getRevision( $timestamp ) {
196 $dbr = wfGetDB( DB_SLAVE );
198 $fields = array(
199 'ar_rev_id',
200 'ar_text',
201 'ar_comment',
202 'ar_user',
203 'ar_user_text',
204 'ar_timestamp',
205 'ar_minor_edit',
206 'ar_flags',
207 'ar_text_id',
208 'ar_deleted',
209 'ar_len',
210 'ar_sha1',
213 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
214 $fields[] = 'ar_content_format';
215 $fields[] = 'ar_content_model';
218 $row = $dbr->selectRow( 'archive',
219 $fields,
220 array( 'ar_namespace' => $this->title->getNamespace(),
221 'ar_title' => $this->title->getDBkey(),
222 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
223 __METHOD__ );
225 if ( $row ) {
226 return Revision::newFromArchiveRow( $row, array( 'title' => $this->title ) );
229 return null;
233 * Return the most-previous revision, either live or deleted, against
234 * the deleted revision given by timestamp.
236 * May produce unexpected results in case of history merges or other
237 * unusual time issues.
239 * @param string $timestamp
240 * @return Revision|null Null when there is no previous revision
242 function getPreviousRevision( $timestamp ) {
243 $dbr = wfGetDB( DB_SLAVE );
245 // Check the previous deleted revision...
246 $row = $dbr->selectRow( 'archive',
247 'ar_timestamp',
248 array( 'ar_namespace' => $this->title->getNamespace(),
249 'ar_title' => $this->title->getDBkey(),
250 'ar_timestamp < ' .
251 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
252 __METHOD__,
253 array(
254 'ORDER BY' => 'ar_timestamp DESC',
255 'LIMIT' => 1 ) );
256 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
258 $row = $dbr->selectRow( array( 'page', 'revision' ),
259 array( 'rev_id', 'rev_timestamp' ),
260 array(
261 'page_namespace' => $this->title->getNamespace(),
262 'page_title' => $this->title->getDBkey(),
263 'page_id = rev_page',
264 'rev_timestamp < ' .
265 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
266 __METHOD__,
267 array(
268 'ORDER BY' => 'rev_timestamp DESC',
269 'LIMIT' => 1 ) );
270 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
271 $prevLiveId = $row ? intval( $row->rev_id ) : null;
273 if ( $prevLive && $prevLive > $prevDeleted ) {
274 // Most prior revision was live
275 return Revision::newFromId( $prevLiveId );
276 } elseif ( $prevDeleted ) {
277 // Most prior revision was deleted
278 return $this->getRevision( $prevDeleted );
281 // No prior revision on this page.
282 return null;
286 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
288 * @param object $row Database row
289 * @return string
291 function getTextFromRow( $row ) {
292 if ( is_null( $row->ar_text_id ) ) {
293 // An old row from MediaWiki 1.4 or previous.
294 // Text is embedded in this row in classic compression format.
295 return Revision::getRevisionText( $row, 'ar_' );
298 // New-style: keyed to the text storage backend.
299 $dbr = wfGetDB( DB_SLAVE );
300 $text = $dbr->selectRow( 'text',
301 array( 'old_text', 'old_flags' ),
302 array( 'old_id' => $row->ar_text_id ),
303 __METHOD__ );
305 return Revision::getRevisionText( $text );
309 * Fetch (and decompress if necessary) the stored text of the most
310 * recently edited deleted revision of the page.
312 * If there are no archived revisions for the page, returns NULL.
314 * @return string|null
316 function getLastRevisionText() {
317 $dbr = wfGetDB( DB_SLAVE );
318 $row = $dbr->selectRow( 'archive',
319 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
320 array( 'ar_namespace' => $this->title->getNamespace(),
321 'ar_title' => $this->title->getDBkey() ),
322 __METHOD__,
323 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
325 if ( $row ) {
326 return $this->getTextFromRow( $row );
329 return null;
333 * Quick check if any archived revisions are present for the page.
335 * @return bool
337 function isDeleted() {
338 $dbr = wfGetDB( DB_SLAVE );
339 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
340 array( 'ar_namespace' => $this->title->getNamespace(),
341 'ar_title' => $this->title->getDBkey() ),
342 __METHOD__
345 return ( $n > 0 );
349 * Restore the given (or all) text and file revisions for the page.
350 * Once restored, the items will be removed from the archive tables.
351 * The deletion log will be updated with an undeletion notice.
353 * @param array $timestamps Pass an empty array to restore all revisions,
354 * otherwise list the ones to undelete.
355 * @param string $comment
356 * @param array $fileVersions
357 * @param bool $unsuppress
358 * @param User $user User performing the action, or null to use $wgUser
359 * @return array(number of file revisions restored, number of image revisions
360 * restored, log message) on success, false on failure.
362 function undelete( $timestamps, $comment = '', $fileVersions = array(),
363 $unsuppress = false, User $user = null
365 // If both the set of text revisions and file revisions are empty,
366 // restore everything. Otherwise, just restore the requested items.
367 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
369 $restoreText = $restoreAll || !empty( $timestamps );
370 $restoreFiles = $restoreAll || !empty( $fileVersions );
372 if ( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
373 $img = wfLocalFile( $this->title );
374 $img->load( File::READ_LATEST );
375 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
376 if ( !$this->fileStatus->isOK() ) {
377 return false;
379 $filesRestored = $this->fileStatus->successCount;
380 } else {
381 $filesRestored = 0;
384 if ( $restoreText ) {
385 $this->revisionStatus = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
386 if ( !$this->revisionStatus->isOK() ) {
387 return false;
390 $textRestored = $this->revisionStatus->getValue();
391 } else {
392 $textRestored = 0;
395 // Touch the log!
397 if ( $textRestored && $filesRestored ) {
398 $reason = wfMessage( 'undeletedrevisions-files' )
399 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
400 } elseif ( $textRestored ) {
401 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
402 ->inContentLanguage()->text();
403 } elseif ( $filesRestored ) {
404 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
405 ->inContentLanguage()->text();
406 } else {
407 wfDebug( "Undelete: nothing undeleted...\n" );
409 return false;
412 if ( trim( $comment ) != '' ) {
413 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
416 if ( $user === null ) {
417 global $wgUser;
418 $user = $wgUser;
421 $logEntry = new ManualLogEntry( 'delete', 'restore' );
422 $logEntry->setPerformer( $user );
423 $logEntry->setTarget( $this->title );
424 $logEntry->setComment( $reason );
426 Hooks::run( 'ArticleUndeleteLogEntry', array( $this, &$logEntry, $user ) );
428 $logid = $logEntry->insert();
429 $logEntry->publish( $logid );
431 return array( $textRestored, $filesRestored, $reason );
435 * This is the meaty bit -- restores archived revisions of the given page
436 * to the cur/old tables. If the page currently exists, all revisions will
437 * be stuffed into old, otherwise the most recent will go into cur.
439 * @param array $timestamps Pass an empty array to restore all revisions,
440 * otherwise list the ones to undelete.
441 * @param bool $unsuppress Remove all ar_deleted/fa_deleted restrictions of seletected revs
442 * @param string $comment
443 * @throws ReadOnlyError
444 * @return Status Status object containing the number of revisions restored on success
446 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
447 if ( wfReadOnly() ) {
448 throw new ReadOnlyError();
451 $restoreAll = empty( $timestamps );
452 $dbw = wfGetDB( DB_MASTER );
454 # Does this page already exist? We'll have to update it...
455 $article = WikiPage::factory( $this->title );
456 # Load latest data for the current page (bug 31179)
457 $article->loadPageData( 'fromdbmaster' );
458 $oldcountable = $article->isCountable();
460 $page = $dbw->selectRow( 'page',
461 array( 'page_id', 'page_latest' ),
462 array( 'page_namespace' => $this->title->getNamespace(),
463 'page_title' => $this->title->getDBkey() ),
464 __METHOD__,
465 array( 'FOR UPDATE' ) // lock page
468 if ( $page ) {
469 $makepage = false;
470 # Page already exists. Import the history, and if necessary
471 # we'll update the latest revision field in the record.
473 $previousRevId = $page->page_latest;
475 # Get the time span of this page
476 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
477 array( 'rev_id' => $previousRevId ),
478 __METHOD__ );
480 if ( $previousTimestamp === false ) {
481 wfDebug( __METHOD__ . ": existing page refers to a page_latest that does not exist\n" );
483 $status = Status::newGood( 0 );
484 $status->warning( 'undeleterevision-missing' );
486 return $status;
488 } else {
489 # Have to create a new article...
490 $makepage = true;
491 $previousRevId = 0;
492 $previousTimestamp = 0;
495 $oldWhere = array(
496 'ar_namespace' => $this->title->getNamespace(),
497 'ar_title' => $this->title->getDBkey(),
499 if ( !$restoreAll ) {
500 $oldWhere['ar_timestamp'] = array_map( array( &$dbw, 'timestamp' ), $timestamps );
503 $fields = array(
504 'ar_rev_id',
505 'ar_text',
506 'ar_comment',
507 'ar_user',
508 'ar_user_text',
509 'ar_timestamp',
510 'ar_minor_edit',
511 'ar_flags',
512 'ar_text_id',
513 'ar_deleted',
514 'ar_page_id',
515 'ar_len',
516 'ar_sha1'
519 if ( $this->config->get( 'ContentHandlerUseDB' ) ) {
520 $fields[] = 'ar_content_format';
521 $fields[] = 'ar_content_model';
525 * Select each archived revision...
527 $result = $dbw->select( 'archive',
528 $fields,
529 $oldWhere,
530 __METHOD__,
531 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
534 $rev_count = $result->numRows();
535 if ( !$rev_count ) {
536 wfDebug( __METHOD__ . ": no revisions to restore\n" );
538 $status = Status::newGood( 0 );
539 $status->warning( "undelete-no-results" );
541 return $status;
544 $result->seek( $rev_count - 1 ); // move to last
545 $row = $result->fetchObject(); // get newest archived rev
546 $oldPageId = (int)$row->ar_page_id; // pass this to ArticleUndelete hook
547 $result->seek( 0 ); // move back
549 // grab the content to check consistency with global state before restoring the page.
550 $revision = Revision::newFromArchiveRow( $row,
551 array(
552 'title' => $article->getTitle(), // used to derive default content model
555 $user = User::newFromName( $revision->getUserText( Revision::RAW ), false );
556 $content = $revision->getContent( Revision::RAW );
558 // NOTE: article ID may not be known yet. prepareSave() should not modify the database.
559 $status = $content->prepareSave( $article, 0, -1, $user );
561 if ( !$status->isOK() ) {
562 return $status;
565 if ( $makepage ) {
566 // Check the state of the newest to-be version...
567 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
568 return Status::newFatal( "undeleterevdel" );
570 // Safe to insert now...
571 $newid = $article->insertOn( $dbw );
572 $pageId = $newid;
573 } else {
574 // Check if a deleted revision will become the current revision...
575 if ( $row->ar_timestamp > $previousTimestamp ) {
576 // Check the state of the newest to-be version...
577 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
578 return Status::newFatal( "undeleterevdel" );
582 $newid = false;
583 $pageId = $article->getId();
586 $revision = null;
587 $restored = 0;
589 foreach ( $result as $row ) {
590 // Check for key dupes due to needed archive integrity.
591 if ( $row->ar_rev_id ) {
592 $exists = $dbw->selectField( 'revision', '1',
593 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
594 if ( $exists ) {
595 continue; // don't throw DB errors
598 // Insert one revision at a time...maintaining deletion status
599 // unless we are specifically removing all restrictions...
600 $revision = Revision::newFromArchiveRow( $row,
601 array(
602 'page' => $pageId,
603 'title' => $this->title,
604 'deleted' => $unsuppress ? 0 : $row->ar_deleted
605 ) );
607 $revision->insertOn( $dbw );
608 $restored++;
610 Hooks::run( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
612 # Now that it's safely stored, take it out of the archive
613 $dbw->delete( 'archive',
614 $oldWhere,
615 __METHOD__ );
617 // Was anything restored at all?
618 if ( $restored == 0 ) {
619 return Status::newGood( 0 );
622 $created = (bool)$newid;
624 // Attach the latest revision to the page...
625 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
626 if ( $created || $wasnew ) {
627 // Update site stats, link tables, etc
628 $article->doEditUpdates(
629 $revision,
630 User::newFromName( $revision->getUserText( Revision::RAW ), false ),
631 array(
632 'created' => $created,
633 'oldcountable' => $oldcountable,
634 'restored' => true
639 Hooks::run( 'ArticleUndelete', array( &$this->title, $created, $comment, $oldPageId ) );
641 if ( $this->title->getNamespace() == NS_FILE ) {
642 DeferredUpdates::addUpdate( new HTMLCacheUpdate( $this->title, 'imagelinks' ) );
645 return Status::newGood( $restored );
649 * @return Status
651 function getFileStatus() {
652 return $this->fileStatus;
656 * @return Status
658 function getRevisionStatus() {
659 return $this->revisionStatus;
664 * Special page allowing users with the appropriate permissions to view
665 * and restore deleted content.
667 * @ingroup SpecialPage
669 class SpecialUndelete extends SpecialPage {
670 private $mAction;
671 private $mTarget;
672 private $mTimestamp;
673 private $mRestore;
674 private $mInvert;
675 private $mFilename;
676 private $mTargetTimestamp;
677 private $mAllowed;
678 private $mCanView;
679 private $mComment;
680 private $mToken;
682 /** @var Title */
683 private $mTargetObj;
685 function __construct() {
686 parent::__construct( 'Undelete', 'deletedhistory' );
689 function loadRequest( $par ) {
690 $request = $this->getRequest();
691 $user = $this->getUser();
693 $this->mAction = $request->getVal( 'action' );
694 if ( $par !== null && $par !== '' ) {
695 $this->mTarget = $par;
696 } else {
697 $this->mTarget = $request->getVal( 'target' );
700 $this->mTargetObj = null;
702 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
703 $this->mTargetObj = Title::newFromText( $this->mTarget );
706 $this->mSearchPrefix = $request->getText( 'prefix' );
707 $time = $request->getVal( 'timestamp' );
708 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
709 $this->mFilename = $request->getVal( 'file' );
711 $posted = $request->wasPosted() &&
712 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
713 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
714 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
715 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
716 $this->mDiff = $request->getCheck( 'diff' );
717 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
718 $this->mComment = $request->getText( 'wpComment' );
719 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
720 $this->mToken = $request->getVal( 'token' );
722 if ( $this->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
723 $this->mAllowed = true; // user can restore
724 $this->mCanView = true; // user can view content
725 } elseif ( $this->isAllowed( 'deletedtext' ) ) {
726 $this->mAllowed = false; // user cannot restore
727 $this->mCanView = true; // user can view content
728 $this->mRestore = false;
729 } else { // user can only view the list of revisions
730 $this->mAllowed = false;
731 $this->mCanView = false;
732 $this->mTimestamp = '';
733 $this->mRestore = false;
736 if ( $this->mRestore || $this->mInvert ) {
737 $timestamps = array();
738 $this->mFileVersions = array();
739 foreach ( $request->getValues() as $key => $val ) {
740 $matches = array();
741 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
742 array_push( $timestamps, $matches[1] );
745 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
746 $this->mFileVersions[] = intval( $matches[1] );
749 rsort( $timestamps );
750 $this->mTargetTimestamp = $timestamps;
755 * Checks whether a user is allowed the permission for the
756 * specific title if one is set.
758 * @param string $permission
759 * @param User $user
760 * @return bool
762 protected function isAllowed( $permission, User $user = null ) {
763 $user = $user ?: $this->getUser();
764 if ( $this->mTargetObj !== null ) {
765 return $this->mTargetObj->userCan( $permission, $user );
766 } else {
767 return $user->isAllowed( $permission );
771 function userCanExecute( User $user ) {
772 return $this->isAllowed( $this->mRestriction, $user );
775 function execute( $par ) {
776 $this->useTransactionalTimeLimit();
778 $user = $this->getUser();
780 $this->setHeaders();
781 $this->outputHeader();
783 $this->loadRequest( $par );
784 $this->checkPermissions(); // Needs to be after mTargetObj is set
786 $out = $this->getOutput();
788 if ( is_null( $this->mTargetObj ) ) {
789 $out->addWikiMsg( 'undelete-header' );
791 # Not all users can just browse every deleted page from the list
792 if ( $user->isAllowed( 'browsearchive' ) ) {
793 $this->showSearchForm();
796 return;
799 $this->addHelpLink( 'Help:Undelete' );
800 if ( $this->mAllowed ) {
801 $out->setPageTitle( $this->msg( 'undeletepage' ) );
802 } else {
803 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
806 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
808 if ( $this->mTimestamp !== '' ) {
809 $this->showRevision( $this->mTimestamp );
810 } elseif ( $this->mFilename !== null && $this->mTargetObj->inNamespace( NS_FILE ) ) {
811 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
812 // Check if user is allowed to see this file
813 if ( !$file->exists() ) {
814 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
815 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
816 if ( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
817 throw new PermissionsError( 'suppressrevision' );
818 } else {
819 throw new PermissionsError( 'deletedtext' );
821 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
822 $this->showFileConfirmationForm( $this->mFilename );
823 } else {
824 $this->showFile( $this->mFilename );
826 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
827 $this->undelete();
828 } else {
829 $this->showHistory();
833 function showSearchForm() {
834 $out = $this->getOutput();
835 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
836 $out->addHTML(
837 Xml::openElement( 'form', array( 'method' => 'get', 'action' => wfScript() ) ) .
838 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
839 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
840 Html::rawElement(
841 'label',
842 array( 'for' => 'prefix' ),
843 $this->msg( 'undelete-search-prefix' )->parse()
845 Xml::input(
846 'prefix',
848 $this->mSearchPrefix,
849 array( 'id' => 'prefix', 'autofocus' => '' )
850 ) . ' ' .
851 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
852 Xml::closeElement( 'fieldset' ) .
853 Xml::closeElement( 'form' )
856 # List undeletable articles
857 if ( $this->mSearchPrefix ) {
858 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
859 $this->showList( $result );
864 * Generic list of deleted pages
866 * @param ResultWrapper $result
867 * @return bool
869 private function showList( $result ) {
870 $out = $this->getOutput();
872 if ( $result->numRows() == 0 ) {
873 $out->addWikiMsg( 'undelete-no-results' );
875 return false;
878 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
880 $undelete = $this->getPageTitle();
881 $out->addHTML( "<ul>\n" );
882 foreach ( $result as $row ) {
883 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
884 if ( $title !== null ) {
885 $item = Linker::linkKnown(
886 $undelete,
887 htmlspecialchars( $title->getPrefixedText() ),
888 array(),
889 array( 'target' => $title->getPrefixedText() )
891 } else {
892 // The title is no longer valid, show as text
893 $item = Html::element(
894 'span',
895 array( 'class' => 'mw-invalidtitle' ),
896 Linker::getInvalidTitleDescription(
897 $this->getContext(),
898 $row->ar_namespace,
899 $row->ar_title
903 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
904 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
906 $result->free();
907 $out->addHTML( "</ul>\n" );
909 return true;
912 private function showRevision( $timestamp ) {
913 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
914 return;
917 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
918 if ( !Hooks::run( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) ) ) {
919 return;
921 $rev = $archive->getRevision( $timestamp );
923 $out = $this->getOutput();
924 $user = $this->getUser();
926 if ( !$rev ) {
927 $out->addWikiMsg( 'undeleterevision-missing' );
929 return;
932 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
933 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
934 $out->wrapWikiMsg(
935 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
936 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ?
937 'rev-suppressed-text-permission' : 'rev-deleted-text-permission'
940 return;
943 $out->wrapWikiMsg(
944 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
945 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ?
946 'rev-suppressed-text-view' : 'rev-deleted-text-view'
948 $out->addHTML( '<br />' );
949 // and we are allowed to see...
952 if ( $this->mDiff ) {
953 $previousRev = $archive->getPreviousRevision( $timestamp );
954 if ( $previousRev ) {
955 $this->showDiff( $previousRev, $rev );
956 if ( $this->mDiffOnly ) {
957 return;
960 $out->addHTML( '<hr />' );
961 } else {
962 $out->addWikiMsg( 'undelete-nodiff' );
966 $link = Linker::linkKnown(
967 $this->getPageTitle( $this->mTargetObj->getPrefixedDBkey() ),
968 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
971 $lang = $this->getLanguage();
973 // date and time are separate parameters to facilitate localisation.
974 // $time is kept for backward compat reasons.
975 $time = $lang->userTimeAndDate( $timestamp, $user );
976 $d = $lang->userDate( $timestamp, $user );
977 $t = $lang->userTime( $timestamp, $user );
978 $userLink = Linker::revUserTools( $rev );
980 $content = $rev->getContent( Revision::FOR_THIS_USER, $user );
982 $isText = ( $content instanceof TextContent );
984 if ( $this->mPreview || $isText ) {
985 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
986 } else {
987 $openDiv = '<div id="mw-undelete-revision">';
989 $out->addHTML( $openDiv );
991 // Revision delete links
992 if ( !$this->mDiff ) {
993 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
994 if ( $revdel ) {
995 $out->addHTML( "$revdel " );
999 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
1000 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
1002 if ( !Hooks::run( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) ) ) {
1003 return;
1006 if ( ( $this->mPreview || !$isText ) && $content ) {
1007 // NOTE: non-text content has no source view, so always use rendered preview
1009 // Hide [edit]s
1010 $popts = $out->parserOptions();
1011 $popts->setEditSection( false );
1013 $pout = $content->getParserOutput( $this->mTargetObj, $rev->getId(), $popts, true );
1014 $out->addParserOutput( $pout );
1017 if ( $isText ) {
1018 // source view for textual content
1019 $sourceView = Xml::element(
1020 'textarea',
1021 array(
1022 'readonly' => 'readonly',
1023 'cols' => $user->getIntOption( 'cols' ),
1024 'rows' => $user->getIntOption( 'rows' )
1026 $content->getNativeData() . "\n"
1029 $previewButton = Xml::element( 'input', array(
1030 'type' => 'submit',
1031 'name' => 'preview',
1032 'value' => $this->msg( 'showpreview' )->text()
1033 ) );
1034 } else {
1035 $sourceView = '';
1036 $previewButton = '';
1039 $diffButton = Xml::element( 'input', array(
1040 'name' => 'diff',
1041 'type' => 'submit',
1042 'value' => $this->msg( 'showdiff' )->text() ) );
1044 $out->addHTML(
1045 $sourceView .
1046 Xml::openElement( 'div', array(
1047 'style' => 'clear: both' ) ) .
1048 Xml::openElement( 'form', array(
1049 'method' => 'post',
1050 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
1051 Xml::element( 'input', array(
1052 'type' => 'hidden',
1053 'name' => 'target',
1054 'value' => $this->mTargetObj->getPrefixedDBkey() ) ) .
1055 Xml::element( 'input', array(
1056 'type' => 'hidden',
1057 'name' => 'timestamp',
1058 'value' => $timestamp ) ) .
1059 Xml::element( 'input', array(
1060 'type' => 'hidden',
1061 'name' => 'wpEditToken',
1062 'value' => $user->getEditToken() ) ) .
1063 $previewButton .
1064 $diffButton .
1065 Xml::closeElement( 'form' ) .
1066 Xml::closeElement( 'div' )
1071 * Build a diff display between this and the previous either deleted
1072 * or non-deleted edit.
1074 * @param Revision $previousRev
1075 * @param Revision $currentRev
1076 * @return string HTML
1078 function showDiff( $previousRev, $currentRev ) {
1079 $diffContext = clone $this->getContext();
1080 $diffContext->setTitle( $currentRev->getTitle() );
1081 $diffContext->setWikiPage( WikiPage::factory( $currentRev->getTitle() ) );
1083 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1084 $diffEngine->showDiffStyle();
1086 $formattedDiff = $diffEngine->generateContentDiffBody(
1087 $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
1088 $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
1091 $formattedDiff = $diffEngine->addHeader(
1092 $formattedDiff,
1093 $this->diffHeader( $previousRev, 'o' ),
1094 $this->diffHeader( $currentRev, 'n' )
1097 $this->getOutput()->addHTML( "<div>$formattedDiff</div>\n" );
1101 * @param Revision $rev
1102 * @param string $prefix
1103 * @return string
1105 private function diffHeader( $rev, $prefix ) {
1106 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1107 if ( $isDeleted ) {
1108 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1109 $targetPage = $this->getPageTitle();
1110 $targetQuery = array(
1111 'target' => $this->mTargetObj->getPrefixedText(),
1112 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
1114 } else {
1115 /// @todo FIXME: getId() may return non-zero for deleted revs...
1116 $targetPage = $rev->getTitle();
1117 $targetQuery = array( 'oldid' => $rev->getId() );
1120 // Add show/hide deletion links if available
1121 $user = $this->getUser();
1122 $lang = $this->getLanguage();
1123 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1125 if ( $rdel ) {
1126 $rdel = " $rdel";
1129 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1131 $tags = wfGetDB( DB_SLAVE )->selectField(
1132 'tag_summary',
1133 'ts_tags',
1134 array( 'ts_rev_id' => $rev->getId() ),
1135 __METHOD__
1137 $tagSummary = ChangeTags::formatSummaryRow( $tags, 'deleteddiff' );
1139 // FIXME This is reimplementing DifferenceEngine#getRevisionHeader
1140 // and partially #showDiffPage, but worse
1141 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1142 Linker::link(
1143 $targetPage,
1144 $this->msg(
1145 'revisionasof',
1146 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1147 $lang->userDate( $rev->getTimestamp(), $user ),
1148 $lang->userTime( $rev->getTimestamp(), $user )
1149 )->escaped(),
1150 array(),
1151 $targetQuery
1153 '</strong></div>' .
1154 '<div id="mw-diff-' . $prefix . 'title2">' .
1155 Linker::revUserTools( $rev ) . '<br />' .
1156 '</div>' .
1157 '<div id="mw-diff-' . $prefix . 'title3">' .
1158 $minor . Linker::revComment( $rev ) . $rdel . '<br />' .
1159 '</div>' .
1160 '<div id="mw-diff-' . $prefix . 'title5">' .
1161 $tagSummary[0] . '<br />' .
1162 '</div>';
1166 * Show a form confirming whether a tokenless user really wants to see a file
1167 * @param string $key
1169 private function showFileConfirmationForm( $key ) {
1170 $out = $this->getOutput();
1171 $lang = $this->getLanguage();
1172 $user = $this->getUser();
1173 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1174 $out->addWikiMsg( 'undelete-show-file-confirm',
1175 $this->mTargetObj->getText(),
1176 $lang->userDate( $file->getTimestamp(), $user ),
1177 $lang->userTime( $file->getTimestamp(), $user ) );
1178 $out->addHTML(
1179 Xml::openElement( 'form', array(
1180 'method' => 'POST',
1181 'action' => $this->getPageTitle()->getLocalURL( array(
1182 'target' => $this->mTarget,
1183 'file' => $key,
1184 'token' => $user->getEditToken( $key ),
1185 ) ),
1188 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1189 '</form>'
1194 * Show a deleted file version requested by the visitor.
1195 * @param string $key
1197 private function showFile( $key ) {
1198 $this->getOutput()->disable();
1200 # We mustn't allow the output to be CDN cached, otherwise
1201 # if an admin previews a deleted image, and it's cached, then
1202 # a user without appropriate permissions can toddle off and
1203 # nab the image, and CDN will serve it
1204 $response = $this->getRequest()->response();
1205 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1206 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1207 $response->header( 'Pragma: no-cache' );
1209 $repo = RepoGroup::singleton()->getLocalRepo();
1210 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1211 $repo->streamFile( $path );
1214 protected function showHistory() {
1215 $this->checkReadOnly();
1217 $out = $this->getOutput();
1218 if ( $this->mAllowed ) {
1219 $out->addModules( 'mediawiki.special.undelete' );
1221 $out->wrapWikiMsg(
1222 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1223 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1226 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
1227 Hooks::run( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1229 $text = $archive->getLastRevisionText();
1230 if( is_null( $text ) ) {
1231 $out->addWikiMsg( 'nohistory' );
1232 return;
1235 $out->addHTML( '<div class="mw-undelete-history">' );
1236 if ( $this->mAllowed ) {
1237 $out->addWikiMsg( 'undeletehistory' );
1238 $out->addWikiMsg( 'undeleterevdel' );
1239 } else {
1240 $out->addWikiMsg( 'undeletehistorynoadmin' );
1242 $out->addHTML( '</div>' );
1244 # List all stored revisions
1245 $revisions = $archive->listRevisions();
1246 $files = $archive->listFiles();
1248 $haveRevisions = $revisions && $revisions->numRows() > 0;
1249 $haveFiles = $files && $files->numRows() > 0;
1251 # Batch existence check on user and talk pages
1252 if ( $haveRevisions ) {
1253 $batch = new LinkBatch();
1254 foreach ( $revisions as $row ) {
1255 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1256 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1258 $batch->execute();
1259 $revisions->seek( 0 );
1261 if ( $haveFiles ) {
1262 $batch = new LinkBatch();
1263 foreach ( $files as $row ) {
1264 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1265 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1267 $batch->execute();
1268 $files->seek( 0 );
1271 if ( $this->mAllowed ) {
1272 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
1273 # Start the form here
1274 $top = Xml::openElement(
1275 'form',
1276 array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' )
1278 $out->addHTML( $top );
1281 # Show relevant lines from the deletion log:
1282 $deleteLogPage = new LogPage( 'delete' );
1283 $out->addHTML( Xml::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1284 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1285 # Show relevant lines from the suppression log:
1286 $suppressLogPage = new LogPage( 'suppress' );
1287 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1288 $out->addHTML( Xml::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1289 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1292 if ( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1293 # Format the user-visible controls (comment field, submission button)
1294 # in a nice little table
1295 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1296 $unsuppressBox =
1297 "<tr>
1298 <td>&#160;</td>
1299 <td class='mw-input'>" .
1300 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1301 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ) .
1302 "</td>
1303 </tr>";
1304 } else {
1305 $unsuppressBox = '';
1308 $table = Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1309 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1310 "<tr>
1311 <td colspan='2' class='mw-undelete-extrahelp'>" .
1312 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1313 "</td>
1314 </tr>
1315 <tr>
1316 <td class='mw-label'>" .
1317 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1318 "</td>
1319 <td class='mw-input'>" .
1320 Xml::input(
1321 'wpComment',
1323 $this->mComment,
1324 array( 'id' => 'wpComment', 'autofocus' => '' )
1326 "</td>
1327 </tr>
1328 <tr>
1329 <td>&#160;</td>
1330 <td class='mw-submit'>" .
1331 Xml::submitButton(
1332 $this->msg( 'undeletebtn' )->text(),
1333 array( 'name' => 'restore', 'id' => 'mw-undelete-submit' )
1334 ) . ' ' .
1335 Xml::submitButton(
1336 $this->msg( 'undeleteinvert' )->text(),
1337 array( 'name' => 'invert', 'id' => 'mw-undelete-invert' )
1339 "</td>
1340 </tr>" .
1341 $unsuppressBox .
1342 Xml::closeElement( 'table' ) .
1343 Xml::closeElement( 'fieldset' );
1345 $out->addHTML( $table );
1348 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1350 if ( $haveRevisions ) {
1351 # The page's stored (deleted) history:
1352 $out->addHTML( '<ul>' );
1353 $remaining = $revisions->numRows();
1354 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1356 foreach ( $revisions as $row ) {
1357 $remaining--;
1358 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1360 $revisions->free();
1361 $out->addHTML( '</ul>' );
1362 } else {
1363 $out->addWikiMsg( 'nohistory' );
1366 if ( $haveFiles ) {
1367 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1368 $out->addHTML( '<ul>' );
1369 foreach ( $files as $row ) {
1370 $out->addHTML( $this->formatFileRow( $row ) );
1372 $files->free();
1373 $out->addHTML( '</ul>' );
1376 if ( $this->mAllowed ) {
1377 # Slip in the hidden controls here
1378 $misc = Html::hidden( 'target', $this->mTarget );
1379 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1380 $misc .= Xml::closeElement( 'form' );
1381 $out->addHTML( $misc );
1384 return true;
1387 protected function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1388 $rev = Revision::newFromArchiveRow( $row,
1389 array(
1390 'title' => $this->mTargetObj
1391 ) );
1393 $revTextSize = '';
1394 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1395 // Build checkboxen...
1396 if ( $this->mAllowed ) {
1397 if ( $this->mInvert ) {
1398 if ( in_array( $ts, $this->mTargetTimestamp ) ) {
1399 $checkBox = Xml::check( "ts$ts" );
1400 } else {
1401 $checkBox = Xml::check( "ts$ts", true );
1403 } else {
1404 $checkBox = Xml::check( "ts$ts" );
1406 } else {
1407 $checkBox = '';
1410 // Build page & diff links...
1411 $user = $this->getUser();
1412 if ( $this->mCanView ) {
1413 $titleObj = $this->getPageTitle();
1414 # Last link
1415 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1416 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1417 $last = $this->msg( 'diff' )->escaped();
1418 } elseif ( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1419 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1420 $last = Linker::linkKnown(
1421 $titleObj,
1422 $this->msg( 'diff' )->escaped(),
1423 array(),
1424 array(
1425 'target' => $this->mTargetObj->getPrefixedText(),
1426 'timestamp' => $ts,
1427 'diff' => 'prev'
1430 } else {
1431 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1432 $last = $this->msg( 'diff' )->escaped();
1434 } else {
1435 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1436 $last = $this->msg( 'diff' )->escaped();
1439 // User links
1440 $userLink = Linker::revUserTools( $rev );
1442 // Minor edit
1443 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1445 // Revision text size
1446 $size = $row->ar_len;
1447 if ( !is_null( $size ) ) {
1448 $revTextSize = Linker::formatRevisionSize( $size );
1451 // Edit summary
1452 $comment = Linker::revComment( $rev );
1454 // Tags
1455 $attribs = array();
1456 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'deletedhistory' );
1457 if ( $classes ) {
1458 $attribs['class'] = implode( ' ', $classes );
1461 // Revision delete links
1462 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1464 $revisionRow = $this->msg( 'undelete-revision-row' )
1465 ->rawParams(
1466 $checkBox,
1467 $revdlink,
1468 $last,
1469 $pageLink,
1470 $userLink,
1471 $minor,
1472 $revTextSize,
1473 $comment,
1474 $tagSummary
1476 ->escaped();
1478 return Xml::tags( 'li', $attribs, $revisionRow ) . "\n";
1481 private function formatFileRow( $row ) {
1482 $file = ArchivedFile::newFromRow( $row );
1483 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1484 $user = $this->getUser();
1486 $checkBox = '';
1487 if ( $this->mCanView && $row->fa_storage_key ) {
1488 if ( $this->mAllowed ) {
1489 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1491 $key = urlencode( $row->fa_storage_key );
1492 $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
1493 } else {
1494 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1496 $userLink = $this->getFileUser( $file );
1497 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1498 $bytes = $this->msg( 'parentheses' )
1499 ->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )
1500 ->plain();
1501 $data = htmlspecialchars( $data . ' ' . $bytes );
1502 $comment = $this->getFileComment( $file );
1504 // Add show/hide deletion links if available
1505 $canHide = $this->isAllowed( 'deleterevision' );
1506 if ( $canHide || ( $file->getVisibility() && $this->isAllowed( 'deletedhistory' ) ) ) {
1507 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1508 // Revision was hidden from sysops
1509 $revdlink = Linker::revDeleteLinkDisabled( $canHide );
1510 } else {
1511 $query = array(
1512 'type' => 'filearchive',
1513 'target' => $this->mTargetObj->getPrefixedDBkey(),
1514 'ids' => $row->fa_id
1516 $revdlink = Linker::revDeleteLink( $query,
1517 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1519 } else {
1520 $revdlink = '';
1523 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1527 * Fetch revision text link if it's available to all users
1529 * @param Revision $rev
1530 * @param Title $titleObj
1531 * @param string $ts Timestamp
1532 * @return string
1534 function getPageLink( $rev, $titleObj, $ts ) {
1535 $user = $this->getUser();
1536 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1538 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1539 return '<span class="history-deleted">' . $time . '</span>';
1542 $link = Linker::linkKnown(
1543 $titleObj,
1544 htmlspecialchars( $time ),
1545 array(),
1546 array(
1547 'target' => $this->mTargetObj->getPrefixedText(),
1548 'timestamp' => $ts
1552 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1553 $link = '<span class="history-deleted">' . $link . '</span>';
1556 return $link;
1560 * Fetch image view link if it's available to all users
1562 * @param File|ArchivedFile $file
1563 * @param Title $titleObj
1564 * @param string $ts A timestamp
1565 * @param string $key A storage key
1567 * @return string HTML fragment
1569 function getFileLink( $file, $titleObj, $ts, $key ) {
1570 $user = $this->getUser();
1571 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1573 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
1574 return '<span class="history-deleted">' . $time . '</span>';
1577 $link = Linker::linkKnown(
1578 $titleObj,
1579 htmlspecialchars( $time ),
1580 array(),
1581 array(
1582 'target' => $this->mTargetObj->getPrefixedText(),
1583 'file' => $key,
1584 'token' => $user->getEditToken( $key )
1588 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1589 $link = '<span class="history-deleted">' . $link . '</span>';
1592 return $link;
1596 * Fetch file's user id if it's available to this user
1598 * @param File|ArchivedFile $file
1599 * @return string HTML fragment
1601 function getFileUser( $file ) {
1602 if ( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1603 return '<span class="history-deleted">' .
1604 $this->msg( 'rev-deleted-user' )->escaped() .
1605 '</span>';
1608 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1609 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1611 if ( $file->isDeleted( File::DELETED_USER ) ) {
1612 $link = '<span class="history-deleted">' . $link . '</span>';
1615 return $link;
1619 * Fetch file upload comment if it's available to this user
1621 * @param File|ArchivedFile $file
1622 * @return string HTML fragment
1624 function getFileComment( $file ) {
1625 if ( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1626 return '<span class="history-deleted"><span class="comment">' .
1627 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1630 $link = Linker::commentBlock( $file->getRawDescription() );
1632 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1633 $link = '<span class="history-deleted">' . $link . '</span>';
1636 return $link;
1639 function undelete() {
1640 if ( $this->getConfig()->get( 'UploadMaintenance' )
1641 && $this->mTargetObj->getNamespace() == NS_FILE
1643 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1646 $this->checkReadOnly();
1648 $out = $this->getOutput();
1649 $archive = new PageArchive( $this->mTargetObj, $this->getConfig() );
1650 Hooks::run( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1651 $ok = $archive->undelete(
1652 $this->mTargetTimestamp,
1653 $this->mComment,
1654 $this->mFileVersions,
1655 $this->mUnsuppress,
1656 $this->getUser()
1659 if ( is_array( $ok ) ) {
1660 if ( $ok[1] ) { // Undeleted file count
1661 Hooks::run( 'FileUndeleteComplete', array(
1662 $this->mTargetObj, $this->mFileVersions,
1663 $this->getUser(), $this->mComment ) );
1666 $link = Linker::linkKnown( $this->mTargetObj );
1667 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1668 } else {
1669 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1672 // Show revision undeletion warnings and errors
1673 $status = $archive->getRevisionStatus();
1674 if ( $status && !$status->isGood() ) {
1675 $out->addWikiText( '<div class="error">' .
1676 $status->getWikiText(
1677 'cannotundelete',
1678 'cannotundelete'
1679 ) . '</div>'
1683 // Show file undeletion warnings and errors
1684 $status = $archive->getFileStatus();
1685 if ( $status && !$status->isGood() ) {
1686 $out->addWikiText( '<div class="error">' .
1687 $status->getWikiText(
1688 'undelete-error-short',
1689 'undelete-error-long'
1690 ) . '</div>'
1696 * Return an array of subpages beginning with $search that this special page will accept.
1698 * @param string $search Prefix to search for
1699 * @param int $limit Maximum number of results to return (usually 10)
1700 * @param int $offset Number of results to skip (usually 0)
1701 * @return string[] Matching subpages
1703 public function prefixSearchSubpages( $search, $limit, $offset ) {
1704 $title = Title::newFromText( $search );
1705 if ( !$title || !$title->canExist() ) {
1706 // No prefix suggestion in special and media namespace
1707 return array();
1709 // Autocomplete subpage the same as a normal search
1710 $prefixSearcher = new StringPrefixSearch;
1711 $result = $prefixSearcher->search( $search, $limit, array(), $offset );
1712 return $result;
1715 protected function getGroupName() {
1716 return 'pagetools';