Merge "Allow wildcard searching in wiki IDs for interwiki user rights logs"
[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 /**
25 * Used to show archived pages and eventually restore them.
26 *
27 * @ingroup SpecialPage
28 */
29 class PageArchive {
30 /** @var Title */
31 protected $title;
32
33 /** @var Status */
34 protected $fileStatus;
35
36 /** @var Status */
37 protected $revisionStatus;
38
39 function __construct( $title ) {
40 if ( is_null( $title ) ) {
41 throw new MWException( __METHOD__ . ' given a null title.' );
42 }
43 $this->title = $title;
44 }
45
46 /**
47 * List all deleted pages recorded in the archive table. Returns result
48 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
49 * namespace/title.
50 *
51 * @return ResultWrapper
52 */
53 public static function listAllPages() {
54 $dbr = wfGetDB( DB_SLAVE );
55
56 return self::listPages( $dbr, '' );
57 }
58
59 /**
60 * List deleted pages recorded in the archive table matching the
61 * given title prefix.
62 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
63 *
64 * @param string $prefix Title prefix
65 * @return ResultWrapper
66 */
67 public static function listPagesByPrefix( $prefix ) {
68 $dbr = wfGetDB( DB_SLAVE );
69
70 $title = Title::newFromText( $prefix );
71 if ( $title ) {
72 $ns = $title->getNamespace();
73 $prefix = $title->getDBkey();
74 } else {
75 // Prolly won't work too good
76 // @todo handle bare namespace names cleanly?
77 $ns = 0;
78 }
79
80 $conds = array(
81 'ar_namespace' => $ns,
82 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
83 );
84
85 return self::listPages( $dbr, $conds );
86 }
87
88 /**
89 * @param DatabaseBase $dbr
90 * @param string|array $condition
91 * @return bool|ResultWrapper
92 */
93 protected static function listPages( $dbr, $condition ) {
94 return $dbr->resultObject( $dbr->select(
95 array( 'archive' ),
96 array(
97 'ar_namespace',
98 'ar_title',
99 'count' => 'COUNT(*)'
100 ),
101 $condition,
102 __METHOD__,
103 array(
104 'GROUP BY' => array( 'ar_namespace', 'ar_title' ),
105 'ORDER BY' => array( 'ar_namespace', 'ar_title' ),
106 'LIMIT' => 100,
107 )
108 ) );
109 }
110
111 /**
112 * List the revisions of the given page. Returns result wrapper with
113 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
114 *
115 * @return ResultWrapper
116 */
117 function listRevisions() {
118 global $wgContentHandlerUseDB;
119
120 $dbr = wfGetDB( DB_SLAVE );
121
122 $tables = array( 'archive' );
123
124 $fields = array(
125 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text',
126 'ar_comment', 'ar_len', 'ar_deleted', 'ar_rev_id', 'ar_sha1',
127 );
128
129 if ( $wgContentHandlerUseDB ) {
130 $fields[] = 'ar_content_format';
131 $fields[] = 'ar_content_model';
132 }
133
134 $conds = array( 'ar_namespace' => $this->title->getNamespace(),
135 'ar_title' => $this->title->getDBkey() );
136
137 $options = array( 'ORDER BY' => 'ar_timestamp DESC' );
138
139 $join_conds = array();
140
141 ChangeTags::modifyDisplayQuery(
142 $tables,
143 $fields,
144 $conds,
145 $join_conds,
146 $options
147 );
148
149 $res = $dbr->select( $tables,
150 $fields,
151 $conds,
152 __METHOD__,
153 $options,
154 $join_conds
155 );
156
157 return $dbr->resultObject( $res );
158 }
159
160 /**
161 * List the deleted file revisions for this page, if it's a file page.
162 * Returns a result wrapper with various filearchive fields, or null
163 * if not a file page.
164 *
165 * @return ResultWrapper
166 * @todo Does this belong in Image for fuller encapsulation?
167 */
168 function listFiles() {
169 if ( $this->title->getNamespace() != NS_FILE ) {
170 return null;
171 }
172
173 $dbr = wfGetDB( DB_SLAVE );
174 $res = $dbr->select(
175 'filearchive',
176 ArchivedFile::selectFields(),
177 array( 'fa_name' => $this->title->getDBkey() ),
178 __METHOD__,
179 array( 'ORDER BY' => 'fa_timestamp DESC' )
180 );
181
182 return $dbr->resultObject( $res );
183 }
184
185 /**
186 * Return a Revision object containing data for the deleted revision.
187 * Note that the result *may* or *may not* have a null page ID.
188 *
189 * @param string $timestamp
190 * @return Revision|null
191 */
192 function getRevision( $timestamp ) {
193 global $wgContentHandlerUseDB;
194
195 $dbr = wfGetDB( DB_SLAVE );
196
197 $fields = array(
198 'ar_rev_id',
199 'ar_text',
200 'ar_comment',
201 'ar_user',
202 'ar_user_text',
203 'ar_timestamp',
204 'ar_minor_edit',
205 'ar_flags',
206 'ar_text_id',
207 'ar_deleted',
208 'ar_len',
209 'ar_sha1',
210 );
211
212 if ( $wgContentHandlerUseDB ) {
213 $fields[] = 'ar_content_format';
214 $fields[] = 'ar_content_model';
215 }
216
217 $row = $dbr->selectRow( 'archive',
218 $fields,
219 array( 'ar_namespace' => $this->title->getNamespace(),
220 'ar_title' => $this->title->getDBkey(),
221 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
222 __METHOD__ );
223
224 if ( $row ) {
225 return Revision::newFromArchiveRow( $row, array( 'title' => $this->title ) );
226 }
227
228 return null;
229 }
230
231 /**
232 * Return the most-previous revision, either live or deleted, against
233 * the deleted revision given by timestamp.
234 *
235 * May produce unexpected results in case of history merges or other
236 * unusual time issues.
237 *
238 * @param string $timestamp
239 * @return Revision|null Null when there is no previous revision
240 */
241 function getPreviousRevision( $timestamp ) {
242 $dbr = wfGetDB( DB_SLAVE );
243
244 // Check the previous deleted revision...
245 $row = $dbr->selectRow( 'archive',
246 'ar_timestamp',
247 array( 'ar_namespace' => $this->title->getNamespace(),
248 'ar_title' => $this->title->getDBkey(),
249 'ar_timestamp < ' .
250 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
251 __METHOD__,
252 array(
253 'ORDER BY' => 'ar_timestamp DESC',
254 'LIMIT' => 1 ) );
255 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
256
257 $row = $dbr->selectRow( array( 'page', 'revision' ),
258 array( 'rev_id', 'rev_timestamp' ),
259 array(
260 'page_namespace' => $this->title->getNamespace(),
261 'page_title' => $this->title->getDBkey(),
262 'page_id = rev_page',
263 'rev_timestamp < ' .
264 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
265 __METHOD__,
266 array(
267 'ORDER BY' => 'rev_timestamp DESC',
268 'LIMIT' => 1 ) );
269 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
270 $prevLiveId = $row ? intval( $row->rev_id ) : null;
271
272 if ( $prevLive && $prevLive > $prevDeleted ) {
273 // Most prior revision was live
274 return Revision::newFromId( $prevLiveId );
275 } elseif ( $prevDeleted ) {
276 // Most prior revision was deleted
277 return $this->getRevision( $prevDeleted );
278 }
279
280 // No prior revision on this page.
281 return null;
282 }
283
284 /**
285 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
286 *
287 * @param object $row Database row
288 * @return string
289 */
290 function getTextFromRow( $row ) {
291 if ( is_null( $row->ar_text_id ) ) {
292 // An old row from MediaWiki 1.4 or previous.
293 // Text is embedded in this row in classic compression format.
294 return Revision::getRevisionText( $row, 'ar_' );
295 }
296
297 // New-style: keyed to the text storage backend.
298 $dbr = wfGetDB( DB_SLAVE );
299 $text = $dbr->selectRow( 'text',
300 array( 'old_text', 'old_flags' ),
301 array( 'old_id' => $row->ar_text_id ),
302 __METHOD__ );
303
304 return Revision::getRevisionText( $text );
305 }
306
307 /**
308 * Fetch (and decompress if necessary) the stored text of the most
309 * recently edited deleted revision of the page.
310 *
311 * If there are no archived revisions for the page, returns NULL.
312 *
313 * @return string|null
314 */
315 function getLastRevisionText() {
316 $dbr = wfGetDB( DB_SLAVE );
317 $row = $dbr->selectRow( 'archive',
318 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
319 array( 'ar_namespace' => $this->title->getNamespace(),
320 'ar_title' => $this->title->getDBkey() ),
321 __METHOD__,
322 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
323
324 if ( $row ) {
325 return $this->getTextFromRow( $row );
326 }
327
328 return null;
329 }
330
331 /**
332 * Quick check if any archived revisions are present for the page.
333 *
334 * @return bool
335 */
336 function isDeleted() {
337 $dbr = wfGetDB( DB_SLAVE );
338 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
339 array( 'ar_namespace' => $this->title->getNamespace(),
340 'ar_title' => $this->title->getDBkey() ),
341 __METHOD__
342 );
343
344 return ( $n > 0 );
345 }
346
347 /**
348 * Restore the given (or all) text and file revisions for the page.
349 * Once restored, the items will be removed from the archive tables.
350 * The deletion log will be updated with an undeletion notice.
351 *
352 * @param array $timestamps Pass an empty array to restore all revisions,
353 * otherwise list the ones to undelete.
354 * @param string $comment
355 * @param array $fileVersions
356 * @param bool $unsuppress
357 * @param User $user User performing the action, or null to use $wgUser
358 * @return array(number of file revisions restored, number of image revisions
359 * restored, log message) on success, false on failure.
360 */
361 function undelete( $timestamps, $comment = '', $fileVersions = array(),
362 $unsuppress = false, User $user = null
363 ) {
364 // If both the set of text revisions and file revisions are empty,
365 // restore everything. Otherwise, just restore the requested items.
366 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
367
368 $restoreText = $restoreAll || !empty( $timestamps );
369 $restoreFiles = $restoreAll || !empty( $fileVersions );
370
371 if ( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
372 $img = wfLocalFile( $this->title );
373 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
374 if ( !$this->fileStatus->isOK() ) {
375 return false;
376 }
377 $filesRestored = $this->fileStatus->successCount;
378 } else {
379 $filesRestored = 0;
380 }
381
382 if ( $restoreText ) {
383 $this->revisionStatus = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
384 if ( !$this->revisionStatus->isOK() ) {
385 return false;
386 }
387
388 $textRestored = $this->revisionStatus->getValue();
389 } else {
390 $textRestored = 0;
391 }
392
393 // Touch the log!
394
395 if ( $textRestored && $filesRestored ) {
396 $reason = wfMessage( 'undeletedrevisions-files' )
397 ->numParams( $textRestored, $filesRestored )->inContentLanguage()->text();
398 } elseif ( $textRestored ) {
399 $reason = wfMessage( 'undeletedrevisions' )->numParams( $textRestored )
400 ->inContentLanguage()->text();
401 } elseif ( $filesRestored ) {
402 $reason = wfMessage( 'undeletedfiles' )->numParams( $filesRestored )
403 ->inContentLanguage()->text();
404 } else {
405 wfDebug( "Undelete: nothing undeleted...\n" );
406
407 return false;
408 }
409
410 if ( trim( $comment ) != '' ) {
411 $reason .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $comment;
412 }
413
414 if ( $user === null ) {
415 global $wgUser;
416 $user = $wgUser;
417 }
418
419 $logEntry = new ManualLogEntry( 'delete', 'restore' );
420 $logEntry->setPerformer( $user );
421 $logEntry->setTarget( $this->title );
422 $logEntry->setComment( $reason );
423
424 wfRunHooks( 'ArticleUndeleteLogEntry', array( $this, &$logEntry, $user ) );
425
426 $logid = $logEntry->insert();
427 $logEntry->publish( $logid );
428
429 return array( $textRestored, $filesRestored, $reason );
430 }
431
432 /**
433 * This is the meaty bit -- restores archived revisions of the given page
434 * to the cur/old tables. If the page currently exists, all revisions will
435 * be stuffed into old, otherwise the most recent will go into cur.
436 *
437 * @param array $timestamps Pass an empty array to restore all revisions,
438 * otherwise list the ones to undelete.
439 * @param bool $unsuppress Remove all ar_deleted/fa_deleted restrictions of seletected revs
440 * @param string $comment
441 * @throws ReadOnlyError
442 * @return Status Status object containing the number of revisions restored on success
443 */
444 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
445 global $wgContentHandlerUseDB;
446
447 if ( wfReadOnly() ) {
448 throw new ReadOnlyError();
449 }
450
451 $restoreAll = empty( $timestamps );
452 $dbw = wfGetDB( DB_MASTER );
453
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();
459
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
466 );
467
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.
472
473 $previousRevId = $page->page_latest;
474
475 # Get the time span of this page
476 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
477 array( 'rev_id' => $previousRevId ),
478 __METHOD__ );
479
480 if ( $previousTimestamp === false ) {
481 wfDebug( __METHOD__ . ": existing page refers to a page_latest that does not exist\n" );
482
483 $status = Status::newGood( 0 );
484 $status->warning( 'undeleterevision-missing' );
485
486 return $status;
487 }
488 } else {
489 # Have to create a new article...
490 $makepage = true;
491 $previousRevId = 0;
492 $previousTimestamp = 0;
493 }
494
495 $oldWhere = array(
496 'ar_namespace' => $this->title->getNamespace(),
497 'ar_title' => $this->title->getDBkey(),
498 );
499 if ( !$restoreAll ) {
500 $oldWhere['ar_timestamp'] = array_map( array( &$dbw, 'timestamp' ), $timestamps );
501 }
502
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'
517 );
518
519 if ( $wgContentHandlerUseDB ) {
520 $fields[] = 'ar_content_format';
521 $fields[] = 'ar_content_model';
522 }
523
524 /**
525 * Select each archived revision...
526 */
527 $result = $dbw->select( 'archive',
528 $fields,
529 $oldWhere,
530 __METHOD__,
531 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
532 );
533 $ret = $dbw->resultObject( $result );
534 $rev_count = $dbw->numRows( $result );
535
536 if ( !$rev_count ) {
537 wfDebug( __METHOD__ . ": no revisions to restore\n" );
538
539 $status = Status::newGood( 0 );
540 $status->warning( "undelete-no-results" );
541
542 return $status;
543 }
544
545 $ret->seek( $rev_count - 1 ); // move to last
546 $row = $ret->fetchObject(); // get newest archived rev
547 $oldPageId = (int)$row->ar_page_id; // pass this to ArticleUndelete hook
548 $ret->seek( 0 ); // move back
549
550 // grab the content to check consistency with global state before restoring the page.
551 $revision = Revision::newFromArchiveRow( $row,
552 array(
553 'title' => $article->getTitle(), // used to derive default content model
554 )
555 );
556 $user = User::newFromName( $revision->getRawUserText(), false );
557 $content = $revision->getContent( Revision::RAW );
558
559 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
560 $status = $content->prepareSave( $article, 0, -1, $user );
561
562 if ( !$status->isOK() ) {
563 return $status;
564 }
565
566 if ( $makepage ) {
567 // Check the state of the newest to-be version...
568 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
569 return Status::newFatal( "undeleterevdel" );
570 }
571 // Safe to insert now...
572 $newid = $article->insertOn( $dbw );
573 $pageId = $newid;
574 } else {
575 // Check if a deleted revision will become the current revision...
576 if ( $row->ar_timestamp > $previousTimestamp ) {
577 // Check the state of the newest to-be version...
578 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
579 return Status::newFatal( "undeleterevdel" );
580 }
581 }
582
583 $newid = false;
584 $pageId = $article->getId();
585 }
586
587 $revision = null;
588 $restored = 0;
589
590 foreach ( $ret as $row ) {
591 // Check for key dupes due to shitty archive integrity.
592 if ( $row->ar_rev_id ) {
593 $exists = $dbw->selectField( 'revision', '1',
594 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
595 if ( $exists ) {
596 continue; // don't throw DB errors
597 }
598 }
599 // Insert one revision at a time...maintaining deletion status
600 // unless we are specifically removing all restrictions...
601 $revision = Revision::newFromArchiveRow( $row,
602 array(
603 'page' => $pageId,
604 'title' => $this->title,
605 'deleted' => $unsuppress ? 0 : $row->ar_deleted
606 ) );
607
608 $revision->insertOn( $dbw );
609 $restored++;
610
611 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
612 }
613 # Now that it's safely stored, take it out of the archive
614 $dbw->delete( 'archive',
615 $oldWhere,
616 __METHOD__ );
617
618 // Was anything restored at all?
619 if ( $restored == 0 ) {
620 return Status::newGood( 0 );
621 }
622
623 $created = (bool)$newid;
624
625 // Attach the latest revision to the page...
626 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
627 if ( $created || $wasnew ) {
628 // Update site stats, link tables, etc
629 $user = User::newFromName( $revision->getRawUserText(), false );
630 $article->doEditUpdates(
631 $revision,
632 $user,
633 array( 'created' => $created, 'oldcountable' => $oldcountable )
634 );
635 }
636
637 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment, $oldPageId ) );
638
639 if ( $this->title->getNamespace() == NS_FILE ) {
640 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
641 $update->doUpdate();
642 }
643
644 return Status::newGood( $restored );
645 }
646
647 /**
648 * @return Status
649 */
650 function getFileStatus() {
651 return $this->fileStatus;
652 }
653
654 /**
655 * @return Status
656 */
657 function getRevisionStatus() {
658 return $this->revisionStatus;
659 }
660 }
661
662 /**
663 * Special page allowing users with the appropriate permissions to view
664 * and restore deleted content.
665 *
666 * @ingroup SpecialPage
667 */
668 class SpecialUndelete extends SpecialPage {
669 private $mAction;
670 private $mTarget;
671 private $mTimestamp;
672 private $mRestore;
673 private $mInvert;
674 private $mFilename;
675 private $mTargetTimestamp;
676 private $mAllowed;
677 private $mCanView;
678 private $mComment;
679 private $mToken;
680
681 /** @var Title */
682 private $mTargetObj;
683
684 function __construct() {
685 parent::__construct( 'Undelete', 'deletedhistory' );
686 }
687
688 function loadRequest( $par ) {
689 $request = $this->getRequest();
690 $user = $this->getUser();
691
692 $this->mAction = $request->getVal( 'action' );
693 if ( $par !== null && $par !== '' ) {
694 $this->mTarget = $par;
695 } else {
696 $this->mTarget = $request->getVal( 'target' );
697 }
698
699 $this->mTargetObj = null;
700
701 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
702 $this->mTargetObj = Title::newFromURL( $this->mTarget );
703 }
704
705 $this->mSearchPrefix = $request->getText( 'prefix' );
706 $time = $request->getVal( 'timestamp' );
707 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
708 $this->mFilename = $request->getVal( 'file' );
709
710 $posted = $request->wasPosted() &&
711 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
712 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
713 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
714 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
715 $this->mDiff = $request->getCheck( 'diff' );
716 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
717 $this->mComment = $request->getText( 'wpComment' );
718 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
719 $this->mToken = $request->getVal( 'token' );
720
721 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
722 $this->mAllowed = true; // user can restore
723 $this->mCanView = true; // user can view content
724 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
725 $this->mAllowed = false; // user cannot restore
726 $this->mCanView = true; // user can view content
727 $this->mRestore = false;
728 } else { // user can only view the list of revisions
729 $this->mAllowed = false;
730 $this->mCanView = false;
731 $this->mTimestamp = '';
732 $this->mRestore = false;
733 }
734
735 if ( $this->mRestore || $this->mInvert ) {
736 $timestamps = array();
737 $this->mFileVersions = array();
738 foreach ( $request->getValues() as $key => $val ) {
739 $matches = array();
740 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
741 array_push( $timestamps, $matches[1] );
742 }
743
744 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
745 $this->mFileVersions[] = intval( $matches[1] );
746 }
747 }
748 rsort( $timestamps );
749 $this->mTargetTimestamp = $timestamps;
750 }
751 }
752
753 function execute( $par ) {
754 $this->checkPermissions();
755 $user = $this->getUser();
756
757 $this->setHeaders();
758 $this->outputHeader();
759
760 $this->loadRequest( $par );
761
762 $out = $this->getOutput();
763
764 if ( is_null( $this->mTargetObj ) ) {
765 $out->addWikiMsg( 'undelete-header' );
766
767 # Not all users can just browse every deleted page from the list
768 if ( $user->isAllowed( 'browsearchive' ) ) {
769 $this->showSearchForm();
770 }
771
772 return;
773 }
774
775 if ( $this->mAllowed ) {
776 $out->setPageTitle( $this->msg( 'undeletepage' ) );
777 } else {
778 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
779 }
780
781 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
782
783 if ( $this->mTimestamp !== '' ) {
784 $this->showRevision( $this->mTimestamp );
785 } elseif ( $this->mFilename !== null && $this->mTargetObj->inNamespace( NS_FILE ) ) {
786 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
787 // Check if user is allowed to see this file
788 if ( !$file->exists() ) {
789 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
790 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
791 if ( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
792 throw new PermissionsError( 'suppressrevision' );
793 } else {
794 throw new PermissionsError( 'deletedtext' );
795 }
796 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
797 $this->showFileConfirmationForm( $this->mFilename );
798 } else {
799 $this->showFile( $this->mFilename );
800 }
801 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
802 $this->undelete();
803 } else {
804 $this->showHistory();
805 }
806 }
807
808 function showSearchForm() {
809 global $wgScript;
810
811 $out = $this->getOutput();
812 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
813 $out->addHTML(
814 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
815 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
816 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
817 Html::rawElement(
818 'label',
819 array( 'for' => 'prefix' ),
820 $this->msg( 'undelete-search-prefix' )->parse()
821 ) .
822 Xml::input(
823 'prefix',
824 20,
825 $this->mSearchPrefix,
826 array( 'id' => 'prefix', 'autofocus' => true )
827 ) . ' ' .
828 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
829 Xml::closeElement( 'fieldset' ) .
830 Xml::closeElement( 'form' )
831 );
832
833 # List undeletable articles
834 if ( $this->mSearchPrefix ) {
835 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
836 $this->showList( $result );
837 }
838 }
839
840 /**
841 * Generic list of deleted pages
842 *
843 * @param ResultWrapper $result
844 * @return bool
845 */
846 private function showList( $result ) {
847 $out = $this->getOutput();
848
849 if ( $result->numRows() == 0 ) {
850 $out->addWikiMsg( 'undelete-no-results' );
851
852 return false;
853 }
854
855 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
856
857 $undelete = $this->getPageTitle();
858 $out->addHTML( "<ul>\n" );
859 foreach ( $result as $row ) {
860 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
861 if ( $title !== null ) {
862 $item = Linker::linkKnown(
863 $undelete,
864 htmlspecialchars( $title->getPrefixedText() ),
865 array(),
866 array( 'target' => $title->getPrefixedText() )
867 );
868 } else {
869 // The title is no longer valid, show as text
870 $item = Html::element(
871 'span',
872 array( 'class' => 'mw-invalidtitle' ),
873 Linker::getInvalidTitleDescription(
874 $this->getContext(),
875 $row->ar_namespace,
876 $row->ar_title
877 )
878 );
879 }
880 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
881 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
882 }
883 $result->free();
884 $out->addHTML( "</ul>\n" );
885
886 return true;
887 }
888
889 private function showRevision( $timestamp ) {
890 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
891 return;
892 }
893
894 $archive = new PageArchive( $this->mTargetObj );
895 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) ) ) {
896 return;
897 }
898 $rev = $archive->getRevision( $timestamp );
899
900 $out = $this->getOutput();
901 $user = $this->getUser();
902
903 if ( !$rev ) {
904 $out->addWikiMsg( 'undeleterevision-missing' );
905
906 return;
907 }
908
909 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
910 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
911 $out->wrapWikiMsg(
912 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
913 'rev-deleted-text-permission'
914 );
915
916 return;
917 }
918
919 $out->wrapWikiMsg(
920 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
921 'rev-deleted-text-view'
922 );
923 $out->addHTML( '<br />' );
924 // and we are allowed to see...
925 }
926
927 if ( $this->mDiff ) {
928 $previousRev = $archive->getPreviousRevision( $timestamp );
929 if ( $previousRev ) {
930 $this->showDiff( $previousRev, $rev );
931 if ( $this->mDiffOnly ) {
932 return;
933 }
934
935 $out->addHTML( '<hr />' );
936 } else {
937 $out->addWikiMsg( 'undelete-nodiff' );
938 }
939 }
940
941 $link = Linker::linkKnown(
942 $this->getPageTitle( $this->mTargetObj->getPrefixedDBkey() ),
943 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
944 );
945
946 $lang = $this->getLanguage();
947
948 // date and time are separate parameters to facilitate localisation.
949 // $time is kept for backward compat reasons.
950 $time = $lang->userTimeAndDate( $timestamp, $user );
951 $d = $lang->userDate( $timestamp, $user );
952 $t = $lang->userTime( $timestamp, $user );
953 $userLink = Linker::revUserTools( $rev );
954
955 $content = $rev->getContent( Revision::FOR_THIS_USER, $user );
956
957 $isText = ( $content instanceof TextContent );
958
959 if ( $this->mPreview || $isText ) {
960 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
961 } else {
962 $openDiv = '<div id="mw-undelete-revision">';
963 }
964 $out->addHTML( $openDiv );
965
966 // Revision delete links
967 if ( !$this->mDiff ) {
968 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
969 if ( $revdel ) {
970 $out->addHTML( "$revdel " );
971 }
972 }
973
974 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
975 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
976
977 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) ) ) {
978 return;
979 }
980
981 if ( $this->mPreview || !$isText ) {
982 // NOTE: non-text content has no source view, so always use rendered preview
983
984 // Hide [edit]s
985 $popts = $out->parserOptions();
986 $popts->setEditSection( false );
987
988 $pout = $content->getParserOutput( $this->mTargetObj, $rev->getId(), $popts, true );
989 $out->addParserOutput( $pout );
990 }
991
992 if ( $isText ) {
993 // source view for textual content
994 $sourceView = Xml::element(
995 'textarea',
996 array(
997 'readonly' => 'readonly',
998 'cols' => $user->getIntOption( 'cols' ),
999 'rows' => $user->getIntOption( 'rows' )
1000 ),
1001 $content->getNativeData() . "\n"
1002 );
1003
1004 $previewButton = Xml::element( 'input', array(
1005 'type' => 'submit',
1006 'name' => 'preview',
1007 'value' => $this->msg( 'showpreview' )->text()
1008 ) );
1009 } else {
1010 $sourceView = '';
1011 $previewButton = '';
1012 }
1013
1014 $diffButton = Xml::element( 'input', array(
1015 'name' => 'diff',
1016 'type' => 'submit',
1017 'value' => $this->msg( 'showdiff' )->text() ) );
1018
1019 $out->addHTML(
1020 $sourceView .
1021 Xml::openElement( 'div', array(
1022 'style' => 'clear: both' ) ) .
1023 Xml::openElement( 'form', array(
1024 'method' => 'post',
1025 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
1026 Xml::element( 'input', array(
1027 'type' => 'hidden',
1028 'name' => 'target',
1029 'value' => $this->mTargetObj->getPrefixedDBkey() ) ) .
1030 Xml::element( 'input', array(
1031 'type' => 'hidden',
1032 'name' => 'timestamp',
1033 'value' => $timestamp ) ) .
1034 Xml::element( 'input', array(
1035 'type' => 'hidden',
1036 'name' => 'wpEditToken',
1037 'value' => $user->getEditToken() ) ) .
1038 $previewButton .
1039 $diffButton .
1040 Xml::closeElement( 'form' ) .
1041 Xml::closeElement( 'div' )
1042 );
1043 }
1044
1045 /**
1046 * Build a diff display between this and the previous either deleted
1047 * or non-deleted edit.
1048 *
1049 * @param Revision $previousRev
1050 * @param Revision $currentRev
1051 * @return string HTML
1052 */
1053 function showDiff( $previousRev, $currentRev ) {
1054 $diffContext = clone $this->getContext();
1055 $diffContext->setTitle( $currentRev->getTitle() );
1056 $diffContext->setWikiPage( WikiPage::factory( $currentRev->getTitle() ) );
1057
1058 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1059 $diffEngine->showDiffStyle();
1060
1061 $formattedDiff = $diffEngine->generateContentDiffBody(
1062 $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
1063 $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
1064 );
1065
1066 $formattedDiff = $diffEngine->addHeader(
1067 $formattedDiff,
1068 $this->diffHeader( $previousRev, 'o' ),
1069 $this->diffHeader( $currentRev, 'n' )
1070 );
1071
1072 $this->getOutput()->addHTML( "<div>$formattedDiff</div>\n" );
1073 }
1074
1075 /**
1076 * @param Revision $rev
1077 * @param string $prefix
1078 * @return string
1079 */
1080 private function diffHeader( $rev, $prefix ) {
1081 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1082 if ( $isDeleted ) {
1083 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1084 $targetPage = $this->getPageTitle();
1085 $targetQuery = array(
1086 'target' => $this->mTargetObj->getPrefixedText(),
1087 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
1088 );
1089 } else {
1090 /// @todo FIXME: getId() may return non-zero for deleted revs...
1091 $targetPage = $rev->getTitle();
1092 $targetQuery = array( 'oldid' => $rev->getId() );
1093 }
1094
1095 // Add show/hide deletion links if available
1096 $user = $this->getUser();
1097 $lang = $this->getLanguage();
1098 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1099
1100 if ( $rdel ) {
1101 $rdel = " $rdel";
1102 }
1103
1104 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1105
1106 $tags = wfGetDB( DB_SLAVE )->selectField(
1107 'tag_summary',
1108 'ts_tags',
1109 array( 'ts_rev_id' => $rev->getId() ),
1110 __METHOD__
1111 );
1112 $tagSummary = ChangeTags::formatSummaryRow( $tags, 'deleteddiff' );
1113
1114 // FIXME This is reimplementing DifferenceEngine#getRevisionHeader
1115 // and partially #showDiffPage, but worse
1116 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1117 Linker::link(
1118 $targetPage,
1119 $this->msg(
1120 'revisionasof',
1121 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1122 $lang->userDate( $rev->getTimestamp(), $user ),
1123 $lang->userTime( $rev->getTimestamp(), $user )
1124 )->escaped(),
1125 array(),
1126 $targetQuery
1127 ) .
1128 '</strong></div>' .
1129 '<div id="mw-diff-' . $prefix . 'title2">' .
1130 Linker::revUserTools( $rev ) . '<br />' .
1131 '</div>' .
1132 '<div id="mw-diff-' . $prefix . 'title3">' .
1133 $minor . Linker::revComment( $rev ) . $rdel . '<br />' .
1134 '</div>' .
1135 '<div id="mw-diff-' . $prefix . 'title5">' .
1136 $tagSummary[0] . '<br />' .
1137 '</div>';
1138 }
1139
1140 /**
1141 * Show a form confirming whether a tokenless user really wants to see a file
1142 * @param string $key
1143 */
1144 private function showFileConfirmationForm( $key ) {
1145 $out = $this->getOutput();
1146 $lang = $this->getLanguage();
1147 $user = $this->getUser();
1148 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1149 $out->addWikiMsg( 'undelete-show-file-confirm',
1150 $this->mTargetObj->getText(),
1151 $lang->userDate( $file->getTimestamp(), $user ),
1152 $lang->userTime( $file->getTimestamp(), $user ) );
1153 $out->addHTML(
1154 Xml::openElement( 'form', array(
1155 'method' => 'POST',
1156 'action' => $this->getPageTitle()->getLocalURL( array(
1157 'target' => $this->mTarget,
1158 'file' => $key,
1159 'token' => $user->getEditToken( $key ),
1160 ) ),
1161 )
1162 ) .
1163 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1164 '</form>'
1165 );
1166 }
1167
1168 /**
1169 * Show a deleted file version requested by the visitor.
1170 * @param string $key
1171 */
1172 private function showFile( $key ) {
1173 $this->getOutput()->disable();
1174
1175 # We mustn't allow the output to be Squid cached, otherwise
1176 # if an admin previews a deleted image, and it's cached, then
1177 # a user without appropriate permissions can toddle off and
1178 # nab the image, and Squid will serve it
1179 $response = $this->getRequest()->response();
1180 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1181 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1182 $response->header( 'Pragma: no-cache' );
1183
1184 $repo = RepoGroup::singleton()->getLocalRepo();
1185 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1186 $repo->streamFile( $path );
1187 }
1188
1189 private function showHistory() {
1190 $out = $this->getOutput();
1191 if ( $this->mAllowed ) {
1192 $out->addModules( 'mediawiki.special.undelete' );
1193 }
1194 $out->wrapWikiMsg(
1195 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1196 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1197 );
1198
1199 $archive = new PageArchive( $this->mTargetObj );
1200 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1201 /*
1202 $text = $archive->getLastRevisionText();
1203 if( is_null( $text ) ) {
1204 $out->addWikiMsg( 'nohistory' );
1205 return;
1206 }
1207 */
1208 $out->addHTML( '<div class="mw-undelete-history">' );
1209 if ( $this->mAllowed ) {
1210 $out->addWikiMsg( 'undeletehistory' );
1211 $out->addWikiMsg( 'undeleterevdel' );
1212 } else {
1213 $out->addWikiMsg( 'undeletehistorynoadmin' );
1214 }
1215 $out->addHTML( '</div>' );
1216
1217 # List all stored revisions
1218 $revisions = $archive->listRevisions();
1219 $files = $archive->listFiles();
1220
1221 $haveRevisions = $revisions && $revisions->numRows() > 0;
1222 $haveFiles = $files && $files->numRows() > 0;
1223
1224 # Batch existence check on user and talk pages
1225 if ( $haveRevisions ) {
1226 $batch = new LinkBatch();
1227 foreach ( $revisions as $row ) {
1228 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1229 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1230 }
1231 $batch->execute();
1232 $revisions->seek( 0 );
1233 }
1234 if ( $haveFiles ) {
1235 $batch = new LinkBatch();
1236 foreach ( $files as $row ) {
1237 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1238 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1239 }
1240 $batch->execute();
1241 $files->seek( 0 );
1242 }
1243
1244 if ( $this->mAllowed ) {
1245 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
1246 # Start the form here
1247 $top = Xml::openElement(
1248 'form',
1249 array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' )
1250 );
1251 $out->addHTML( $top );
1252 }
1253
1254 # Show relevant lines from the deletion log:
1255 $deleteLogPage = new LogPage( 'delete' );
1256 $out->addHTML( Xml::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1257 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1258 # Show relevant lines from the suppression log:
1259 $suppressLogPage = new LogPage( 'suppress' );
1260 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1261 $out->addHTML( Xml::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1262 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1263 }
1264
1265 if ( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1266 # Format the user-visible controls (comment field, submission button)
1267 # in a nice little table
1268 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1269 $unsuppressBox =
1270 "<tr>
1271 <td>&#160;</td>
1272 <td class='mw-input'>" .
1273 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1274 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ) .
1275 "</td>
1276 </tr>";
1277 } else {
1278 $unsuppressBox = '';
1279 }
1280
1281 $table = Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1282 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1283 "<tr>
1284 <td colspan='2' class='mw-undelete-extrahelp'>" .
1285 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1286 "</td>
1287 </tr>
1288 <tr>
1289 <td class='mw-label'>" .
1290 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1291 "</td>
1292 <td class='mw-input'>" .
1293 Xml::input(
1294 'wpComment',
1295 50,
1296 $this->mComment,
1297 array( 'id' => 'wpComment', 'autofocus' => true )
1298 ) .
1299 "</td>
1300 </tr>
1301 <tr>
1302 <td>&#160;</td>
1303 <td class='mw-submit'>" .
1304 Xml::submitButton(
1305 $this->msg( 'undeletebtn' )->text(),
1306 array( 'name' => 'restore', 'id' => 'mw-undelete-submit' )
1307 ) . ' ' .
1308 Xml::submitButton(
1309 $this->msg( 'undeleteinvert' )->text(),
1310 array( 'name' => 'invert', 'id' => 'mw-undelete-invert' )
1311 ) .
1312 "</td>
1313 </tr>" .
1314 $unsuppressBox .
1315 Xml::closeElement( 'table' ) .
1316 Xml::closeElement( 'fieldset' );
1317
1318 $out->addHTML( $table );
1319 }
1320
1321 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1322
1323 if ( $haveRevisions ) {
1324 # The page's stored (deleted) history:
1325 $out->addHTML( '<ul>' );
1326 $remaining = $revisions->numRows();
1327 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1328
1329 foreach ( $revisions as $row ) {
1330 $remaining--;
1331 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1332 }
1333 $revisions->free();
1334 $out->addHTML( '</ul>' );
1335 } else {
1336 $out->addWikiMsg( 'nohistory' );
1337 }
1338
1339 if ( $haveFiles ) {
1340 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1341 $out->addHTML( '<ul>' );
1342 foreach ( $files as $row ) {
1343 $out->addHTML( $this->formatFileRow( $row ) );
1344 }
1345 $files->free();
1346 $out->addHTML( '</ul>' );
1347 }
1348
1349 if ( $this->mAllowed ) {
1350 # Slip in the hidden controls here
1351 $misc = Html::hidden( 'target', $this->mTarget );
1352 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1353 $misc .= Xml::closeElement( 'form' );
1354 $out->addHTML( $misc );
1355 }
1356
1357 return true;
1358 }
1359
1360 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1361 $rev = Revision::newFromArchiveRow( $row,
1362 array(
1363 'title' => $this->mTargetObj
1364 ) );
1365
1366 $revTextSize = '';
1367 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1368 // Build checkboxen...
1369 if ( $this->mAllowed ) {
1370 if ( $this->mInvert ) {
1371 if ( in_array( $ts, $this->mTargetTimestamp ) ) {
1372 $checkBox = Xml::check( "ts$ts" );
1373 } else {
1374 $checkBox = Xml::check( "ts$ts", true );
1375 }
1376 } else {
1377 $checkBox = Xml::check( "ts$ts" );
1378 }
1379 } else {
1380 $checkBox = '';
1381 }
1382
1383 // Build page & diff links...
1384 $user = $this->getUser();
1385 if ( $this->mCanView ) {
1386 $titleObj = $this->getPageTitle();
1387 # Last link
1388 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1389 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1390 $last = $this->msg( 'diff' )->escaped();
1391 } elseif ( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1392 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1393 $last = Linker::linkKnown(
1394 $titleObj,
1395 $this->msg( 'diff' )->escaped(),
1396 array(),
1397 array(
1398 'target' => $this->mTargetObj->getPrefixedText(),
1399 'timestamp' => $ts,
1400 'diff' => 'prev'
1401 )
1402 );
1403 } else {
1404 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1405 $last = $this->msg( 'diff' )->escaped();
1406 }
1407 } else {
1408 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1409 $last = $this->msg( 'diff' )->escaped();
1410 }
1411
1412 // User links
1413 $userLink = Linker::revUserTools( $rev );
1414
1415 // Minor edit
1416 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1417
1418 // Revision text size
1419 $size = $row->ar_len;
1420 if ( !is_null( $size ) ) {
1421 $revTextSize = Linker::formatRevisionSize( $size );
1422 }
1423
1424 // Edit summary
1425 $comment = Linker::revComment( $rev );
1426
1427 // Tags
1428 $attribs = array();
1429 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'deletedhistory' );
1430 if ( $classes ) {
1431 $attribs['class'] = implode( ' ', $classes );
1432 }
1433
1434 // Revision delete links
1435 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1436
1437 $revisionRow = $this->msg( 'undelete-revision-row' )
1438 ->rawParams(
1439 $checkBox,
1440 $revdlink,
1441 $last,
1442 $pageLink,
1443 $userLink,
1444 $minor,
1445 $revTextSize,
1446 $comment,
1447 $tagSummary
1448 )
1449 ->escaped();
1450
1451 return Xml::tags( 'li', $attribs, $revisionRow ) . "\n";
1452 }
1453
1454 private function formatFileRow( $row ) {
1455 $file = ArchivedFile::newFromRow( $row );
1456 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1457 $user = $this->getUser();
1458
1459 if ( $this->mAllowed && $row->fa_storage_key ) {
1460 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1461 $key = urlencode( $row->fa_storage_key );
1462 $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
1463 } else {
1464 $checkBox = '';
1465 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1466 }
1467 $userLink = $this->getFileUser( $file );
1468 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1469 $bytes = $this->msg( 'parentheses' )
1470 ->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )
1471 ->plain();
1472 $data = htmlspecialchars( $data . ' ' . $bytes );
1473 $comment = $this->getFileComment( $file );
1474
1475 // Add show/hide deletion links if available
1476 $canHide = $user->isAllowed( 'deleterevision' );
1477 if ( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1478 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1479 // Revision was hidden from sysops
1480 $revdlink = Linker::revDeleteLinkDisabled( $canHide );
1481 } else {
1482 $query = array(
1483 'type' => 'filearchive',
1484 'target' => $this->mTargetObj->getPrefixedDBkey(),
1485 'ids' => $row->fa_id
1486 );
1487 $revdlink = Linker::revDeleteLink( $query,
1488 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1489 }
1490 } else {
1491 $revdlink = '';
1492 }
1493
1494 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1495 }
1496
1497 /**
1498 * Fetch revision text link if it's available to all users
1499 *
1500 * @param Revision $rev
1501 * @param Title $titleObj
1502 * @param string $ts Timestamp
1503 * @return string
1504 */
1505 function getPageLink( $rev, $titleObj, $ts ) {
1506 $user = $this->getUser();
1507 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1508
1509 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1510 return '<span class="history-deleted">' . $time . '</span>';
1511 }
1512
1513 $link = Linker::linkKnown(
1514 $titleObj,
1515 htmlspecialchars( $time ),
1516 array(),
1517 array(
1518 'target' => $this->mTargetObj->getPrefixedText(),
1519 'timestamp' => $ts
1520 )
1521 );
1522
1523 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1524 $link = '<span class="history-deleted">' . $link . '</span>';
1525 }
1526
1527 return $link;
1528 }
1529
1530 /**
1531 * Fetch image view link if it's available to all users
1532 *
1533 * @param File|ArchivedFile $file
1534 * @param Title $titleObj
1535 * @param string $ts A timestamp
1536 * @param string $key A storage key
1537 *
1538 * @return string HTML fragment
1539 */
1540 function getFileLink( $file, $titleObj, $ts, $key ) {
1541 $user = $this->getUser();
1542 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1543
1544 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
1545 return '<span class="history-deleted">' . $time . '</span>';
1546 }
1547
1548 $link = Linker::linkKnown(
1549 $titleObj,
1550 htmlspecialchars( $time ),
1551 array(),
1552 array(
1553 'target' => $this->mTargetObj->getPrefixedText(),
1554 'file' => $key,
1555 'token' => $user->getEditToken( $key )
1556 )
1557 );
1558
1559 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1560 $link = '<span class="history-deleted">' . $link . '</span>';
1561 }
1562
1563 return $link;
1564 }
1565
1566 /**
1567 * Fetch file's user id if it's available to this user
1568 *
1569 * @param File|ArchivedFile $file
1570 * @return string HTML fragment
1571 */
1572 function getFileUser( $file ) {
1573 if ( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1574 return '<span class="history-deleted">' .
1575 $this->msg( 'rev-deleted-user' )->escaped() .
1576 '</span>';
1577 }
1578
1579 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1580 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1581
1582 if ( $file->isDeleted( File::DELETED_USER ) ) {
1583 $link = '<span class="history-deleted">' . $link . '</span>';
1584 }
1585
1586 return $link;
1587 }
1588
1589 /**
1590 * Fetch file upload comment if it's available to this user
1591 *
1592 * @param File|ArchivedFile $file
1593 * @return string HTML fragment
1594 */
1595 function getFileComment( $file ) {
1596 if ( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1597 return '<span class="history-deleted"><span class="comment">' .
1598 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1599 }
1600
1601 $link = Linker::commentBlock( $file->getRawDescription() );
1602
1603 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1604 $link = '<span class="history-deleted">' . $link . '</span>';
1605 }
1606
1607 return $link;
1608 }
1609
1610 function undelete() {
1611 global $wgUploadMaintenance;
1612
1613 if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
1614 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1615 }
1616
1617 if ( wfReadOnly() ) {
1618 throw new ReadOnlyError;
1619 }
1620
1621 $out = $this->getOutput();
1622 $archive = new PageArchive( $this->mTargetObj );
1623 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1624 $ok = $archive->undelete(
1625 $this->mTargetTimestamp,
1626 $this->mComment,
1627 $this->mFileVersions,
1628 $this->mUnsuppress,
1629 $this->getUser()
1630 );
1631
1632 if ( is_array( $ok ) ) {
1633 if ( $ok[1] ) { // Undeleted file count
1634 wfRunHooks( 'FileUndeleteComplete', array(
1635 $this->mTargetObj, $this->mFileVersions,
1636 $this->getUser(), $this->mComment ) );
1637 }
1638
1639 $link = Linker::linkKnown( $this->mTargetObj );
1640 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1641 } else {
1642 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1643 }
1644
1645 // Show revision undeletion warnings and errors
1646 $status = $archive->getRevisionStatus();
1647 if ( $status && !$status->isGood() ) {
1648 $out->addWikiText( '<div class="error">' .
1649 $status->getWikiText(
1650 'cannotundelete',
1651 'cannotundelete'
1652 ) . '</div>'
1653 );
1654 }
1655
1656 // Show file undeletion warnings and errors
1657 $status = $archive->getFileStatus();
1658 if ( $status && !$status->isGood() ) {
1659 $out->addWikiText( '<div class="error">' .
1660 $status->getWikiText(
1661 'undelete-error-short',
1662 'undelete-error-long'
1663 ) . '</div>'
1664 );
1665 }
1666 }
1667
1668 protected function getGroupName() {
1669 return 'pagetools';
1670 }
1671 }