Merge "Fix "UTPage" creation in tests"
[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 if ( $restoreAll ) {
496 $oldones = '1 = 1'; # All revisions...
497 } else {
498 $oldts = implode( ',',
499 array_map( array( &$dbw, 'addQuotes' ),
500 array_map( array( &$dbw, 'timestamp' ),
501 $timestamps ) ) );
502
503 $oldones = "ar_timestamp IN ( {$oldts} )";
504 }
505
506 $fields = array(
507 'ar_rev_id',
508 'ar_text',
509 'ar_comment',
510 'ar_user',
511 'ar_user_text',
512 'ar_timestamp',
513 'ar_minor_edit',
514 'ar_flags',
515 'ar_text_id',
516 'ar_deleted',
517 'ar_page_id',
518 'ar_len',
519 'ar_sha1'
520 );
521
522 if ( $wgContentHandlerUseDB ) {
523 $fields[] = 'ar_content_format';
524 $fields[] = 'ar_content_model';
525 }
526
527 /**
528 * Select each archived revision...
529 */
530 $result = $dbw->select( 'archive',
531 $fields,
532 /* WHERE */ array(
533 'ar_namespace' => $this->title->getNamespace(),
534 'ar_title' => $this->title->getDBkey(),
535 $oldones ),
536 __METHOD__,
537 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
538 );
539 $ret = $dbw->resultObject( $result );
540 $rev_count = $dbw->numRows( $result );
541
542 if ( !$rev_count ) {
543 wfDebug( __METHOD__ . ": no revisions to restore\n" );
544
545 $status = Status::newGood( 0 );
546 $status->warning( "undelete-no-results" );
547
548 return $status;
549 }
550
551 $ret->seek( $rev_count - 1 ); // move to last
552 $row = $ret->fetchObject(); // get newest archived rev
553 $ret->seek( 0 ); // move back
554
555 // grab the content to check consistency with global state before restoring the page.
556 $revision = Revision::newFromArchiveRow( $row,
557 array(
558 'title' => $article->getTitle(), // used to derive default content model
559 )
560 );
561 $user = User::newFromName( $revision->getRawUserText(), false );
562 $content = $revision->getContent( Revision::RAW );
563
564 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
565 $status = $content->prepareSave( $article, 0, -1, $user );
566
567 if ( !$status->isOK() ) {
568 return $status;
569 }
570
571 if ( $makepage ) {
572 // Check the state of the newest to-be version...
573 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
574 return Status::newFatal( "undeleterevdel" );
575 }
576 // Safe to insert now...
577 $newid = $article->insertOn( $dbw );
578 $pageId = $newid;
579 } else {
580 // Check if a deleted revision will become the current revision...
581 if ( $row->ar_timestamp > $previousTimestamp ) {
582 // Check the state of the newest to-be version...
583 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
584 return Status::newFatal( "undeleterevdel" );
585 }
586 }
587
588 $newid = false;
589 $pageId = $article->getId();
590 }
591
592 $revision = null;
593 $restored = 0;
594
595 foreach ( $ret as $row ) {
596 // Check for key dupes due to shitty archive integrity.
597 if ( $row->ar_rev_id ) {
598 $exists = $dbw->selectField( 'revision', '1',
599 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
600 if ( $exists ) {
601 continue; // don't throw DB errors
602 }
603 }
604 // Insert one revision at a time...maintaining deletion status
605 // unless we are specifically removing all restrictions...
606 $revision = Revision::newFromArchiveRow( $row,
607 array(
608 'page' => $pageId,
609 'title' => $this->title,
610 'deleted' => $unsuppress ? 0 : $row->ar_deleted
611 ) );
612
613 $revision->insertOn( $dbw );
614 $restored++;
615
616 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
617 }
618 # Now that it's safely stored, take it out of the archive
619 $dbw->delete( 'archive',
620 /* WHERE */ array(
621 'ar_namespace' => $this->title->getNamespace(),
622 'ar_title' => $this->title->getDBkey(),
623 $oldones ),
624 __METHOD__ );
625
626 // Was anything restored at all?
627 if ( $restored == 0 ) {
628 return Status::newGood( 0 );
629 }
630
631 $created = (bool)$newid;
632
633 // Attach the latest revision to the page...
634 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
635 if ( $created || $wasnew ) {
636 // Update site stats, link tables, etc
637 $user = User::newFromName( $revision->getRawUserText(), false );
638 $article->doEditUpdates(
639 $revision,
640 $user,
641 array( 'created' => $created, 'oldcountable' => $oldcountable )
642 );
643 }
644
645 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment ) );
646
647 if ( $this->title->getNamespace() == NS_FILE ) {
648 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
649 $update->doUpdate();
650 }
651
652 return Status::newGood( $restored );
653 }
654
655 /**
656 * @return Status
657 */
658 function getFileStatus() {
659 return $this->fileStatus;
660 }
661
662 /**
663 * @return Status
664 */
665 function getRevisionStatus() {
666 return $this->revisionStatus;
667 }
668 }
669
670 /**
671 * Special page allowing users with the appropriate permissions to view
672 * and restore deleted content.
673 *
674 * @ingroup SpecialPage
675 */
676 class SpecialUndelete extends SpecialPage {
677 private $mAction;
678 private $mTarget;
679 private $mTimestamp;
680 private $mRestore;
681 private $mInvert;
682 private $mFilename;
683 private $mTargetTimestamp;
684 private $mAllowed;
685 private $mCanView;
686 private $mComment;
687 private $mToken;
688
689 /** @var Title */
690 private $mTargetObj;
691
692 function __construct() {
693 parent::__construct( 'Undelete', 'deletedhistory' );
694 }
695
696 function loadRequest( $par ) {
697 $request = $this->getRequest();
698 $user = $this->getUser();
699
700 $this->mAction = $request->getVal( 'action' );
701 if ( $par !== null && $par !== '' ) {
702 $this->mTarget = $par;
703 } else {
704 $this->mTarget = $request->getVal( 'target' );
705 }
706
707 $this->mTargetObj = null;
708
709 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
710 $this->mTargetObj = Title::newFromURL( $this->mTarget );
711 }
712
713 $this->mSearchPrefix = $request->getText( 'prefix' );
714 $time = $request->getVal( 'timestamp' );
715 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
716 $this->mFilename = $request->getVal( 'file' );
717
718 $posted = $request->wasPosted() &&
719 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
720 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
721 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
722 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
723 $this->mDiff = $request->getCheck( 'diff' );
724 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
725 $this->mComment = $request->getText( 'wpComment' );
726 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
727 $this->mToken = $request->getVal( 'token' );
728
729 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
730 $this->mAllowed = true; // user can restore
731 $this->mCanView = true; // user can view content
732 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
733 $this->mAllowed = false; // user cannot restore
734 $this->mCanView = true; // user can view content
735 $this->mRestore = false;
736 } else { // user can only view the list of revisions
737 $this->mAllowed = false;
738 $this->mCanView = false;
739 $this->mTimestamp = '';
740 $this->mRestore = false;
741 }
742
743 if ( $this->mRestore || $this->mInvert ) {
744 $timestamps = array();
745 $this->mFileVersions = array();
746 foreach ( $request->getValues() as $key => $val ) {
747 $matches = array();
748 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
749 array_push( $timestamps, $matches[1] );
750 }
751
752 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
753 $this->mFileVersions[] = intval( $matches[1] );
754 }
755 }
756 rsort( $timestamps );
757 $this->mTargetTimestamp = $timestamps;
758 }
759 }
760
761 function execute( $par ) {
762 $this->checkPermissions();
763 $user = $this->getUser();
764
765 $this->setHeaders();
766 $this->outputHeader();
767
768 $this->loadRequest( $par );
769
770 $out = $this->getOutput();
771
772 if ( is_null( $this->mTargetObj ) ) {
773 $out->addWikiMsg( 'undelete-header' );
774
775 # Not all users can just browse every deleted page from the list
776 if ( $user->isAllowed( 'browsearchive' ) ) {
777 $this->showSearchForm();
778 }
779
780 return;
781 }
782
783 if ( $this->mAllowed ) {
784 $out->setPageTitle( $this->msg( 'undeletepage' ) );
785 } else {
786 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
787 }
788
789 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
790
791 if ( $this->mTimestamp !== '' ) {
792 $this->showRevision( $this->mTimestamp );
793 } elseif ( $this->mFilename !== null && $this->mTargetObj->inNamespace( NS_FILE ) ) {
794 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
795 // Check if user is allowed to see this file
796 if ( !$file->exists() ) {
797 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
798 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
799 if ( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
800 throw new PermissionsError( 'suppressrevision' );
801 } else {
802 throw new PermissionsError( 'deletedtext' );
803 }
804 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
805 $this->showFileConfirmationForm( $this->mFilename );
806 } else {
807 $this->showFile( $this->mFilename );
808 }
809 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
810 $this->undelete();
811 } else {
812 $this->showHistory();
813 }
814 }
815
816 function showSearchForm() {
817 global $wgScript;
818
819 $out = $this->getOutput();
820 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
821 $out->addHTML(
822 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
823 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
824 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
825 Html::rawElement(
826 'label',
827 array( 'for' => 'prefix' ),
828 $this->msg( 'undelete-search-prefix' )->parse()
829 ) .
830 Xml::input(
831 'prefix',
832 20,
833 $this->mSearchPrefix,
834 array( 'id' => 'prefix', 'autofocus' => true )
835 ) . ' ' .
836 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
837 Xml::closeElement( 'fieldset' ) .
838 Xml::closeElement( 'form' )
839 );
840
841 # List undeletable articles
842 if ( $this->mSearchPrefix ) {
843 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
844 $this->showList( $result );
845 }
846 }
847
848 /**
849 * Generic list of deleted pages
850 *
851 * @param ResultWrapper $result
852 * @return bool
853 */
854 private function showList( $result ) {
855 $out = $this->getOutput();
856
857 if ( $result->numRows() == 0 ) {
858 $out->addWikiMsg( 'undelete-no-results' );
859
860 return false;
861 }
862
863 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
864
865 $undelete = $this->getPageTitle();
866 $out->addHTML( "<ul>\n" );
867 foreach ( $result as $row ) {
868 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
869 if ( $title !== null ) {
870 $item = Linker::linkKnown(
871 $undelete,
872 htmlspecialchars( $title->getPrefixedText() ),
873 array(),
874 array( 'target' => $title->getPrefixedText() )
875 );
876 } else {
877 // The title is no longer valid, show as text
878 $item = Html::element(
879 'span',
880 array( 'class' => 'mw-invalidtitle' ),
881 Linker::getInvalidTitleDescription(
882 $this->getContext(),
883 $row->ar_namespace,
884 $row->ar_title
885 )
886 );
887 }
888 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
889 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
890 }
891 $result->free();
892 $out->addHTML( "</ul>\n" );
893
894 return true;
895 }
896
897 private function showRevision( $timestamp ) {
898 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
899 return;
900 }
901
902 $archive = new PageArchive( $this->mTargetObj );
903 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) ) ) {
904 return;
905 }
906 $rev = $archive->getRevision( $timestamp );
907
908 $out = $this->getOutput();
909 $user = $this->getUser();
910
911 if ( !$rev ) {
912 $out->addWikiMsg( 'undeleterevision-missing' );
913
914 return;
915 }
916
917 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
918 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
919 $out->wrapWikiMsg(
920 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
921 'rev-deleted-text-permission'
922 );
923
924 return;
925 }
926
927 $out->wrapWikiMsg(
928 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
929 'rev-deleted-text-view'
930 );
931 $out->addHTML( '<br />' );
932 // and we are allowed to see...
933 }
934
935 if ( $this->mDiff ) {
936 $previousRev = $archive->getPreviousRevision( $timestamp );
937 if ( $previousRev ) {
938 $this->showDiff( $previousRev, $rev );
939 if ( $this->mDiffOnly ) {
940 return;
941 }
942
943 $out->addHTML( '<hr />' );
944 } else {
945 $out->addWikiMsg( 'undelete-nodiff' );
946 }
947 }
948
949 $link = Linker::linkKnown(
950 $this->getPageTitle( $this->mTargetObj->getPrefixedDBkey() ),
951 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
952 );
953
954 $lang = $this->getLanguage();
955
956 // date and time are separate parameters to facilitate localisation.
957 // $time is kept for backward compat reasons.
958 $time = $lang->userTimeAndDate( $timestamp, $user );
959 $d = $lang->userDate( $timestamp, $user );
960 $t = $lang->userTime( $timestamp, $user );
961 $userLink = Linker::revUserTools( $rev );
962
963 $content = $rev->getContent( Revision::FOR_THIS_USER, $user );
964
965 $isText = ( $content instanceof TextContent );
966
967 if ( $this->mPreview || $isText ) {
968 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
969 } else {
970 $openDiv = '<div id="mw-undelete-revision">';
971 }
972 $out->addHTML( $openDiv );
973
974 // Revision delete links
975 if ( !$this->mDiff ) {
976 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
977 if ( $revdel ) {
978 $out->addHTML( "$revdel " );
979 }
980 }
981
982 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
983 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
984
985 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) ) ) {
986 return;
987 }
988
989 if ( $this->mPreview || !$isText ) {
990 // NOTE: non-text content has no source view, so always use rendered preview
991
992 // Hide [edit]s
993 $popts = $out->parserOptions();
994 $popts->setEditSection( false );
995
996 $pout = $content->getParserOutput( $this->mTargetObj, $rev->getId(), $popts, true );
997 $out->addParserOutput( $pout );
998 }
999
1000 if ( $isText ) {
1001 // source view for textual content
1002 $sourceView = Xml::element(
1003 'textarea',
1004 array(
1005 'readonly' => 'readonly',
1006 'cols' => $user->getIntOption( 'cols' ),
1007 'rows' => $user->getIntOption( 'rows' )
1008 ),
1009 $content->getNativeData() . "\n"
1010 );
1011
1012 $previewButton = Xml::element( 'input', array(
1013 'type' => 'submit',
1014 'name' => 'preview',
1015 'value' => $this->msg( 'showpreview' )->text()
1016 ) );
1017 } else {
1018 $sourceView = '';
1019 $previewButton = '';
1020 }
1021
1022 $diffButton = Xml::element( 'input', array(
1023 'name' => 'diff',
1024 'type' => 'submit',
1025 'value' => $this->msg( 'showdiff' )->text() ) );
1026
1027 $out->addHTML(
1028 $sourceView .
1029 Xml::openElement( 'div', array(
1030 'style' => 'clear: both' ) ) .
1031 Xml::openElement( 'form', array(
1032 'method' => 'post',
1033 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
1034 Xml::element( 'input', array(
1035 'type' => 'hidden',
1036 'name' => 'target',
1037 'value' => $this->mTargetObj->getPrefixedDBkey() ) ) .
1038 Xml::element( 'input', array(
1039 'type' => 'hidden',
1040 'name' => 'timestamp',
1041 'value' => $timestamp ) ) .
1042 Xml::element( 'input', array(
1043 'type' => 'hidden',
1044 'name' => 'wpEditToken',
1045 'value' => $user->getEditToken() ) ) .
1046 $previewButton .
1047 $diffButton .
1048 Xml::closeElement( 'form' ) .
1049 Xml::closeElement( 'div' )
1050 );
1051 }
1052
1053 /**
1054 * Build a diff display between this and the previous either deleted
1055 * or non-deleted edit.
1056 *
1057 * @param Revision $previousRev
1058 * @param Revision $currentRev
1059 * @return string HTML
1060 */
1061 function showDiff( $previousRev, $currentRev ) {
1062 $diffContext = clone $this->getContext();
1063 $diffContext->setTitle( $currentRev->getTitle() );
1064 $diffContext->setWikiPage( WikiPage::factory( $currentRev->getTitle() ) );
1065
1066 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1067 $diffEngine->showDiffStyle();
1068
1069 $formattedDiff = $diffEngine->generateContentDiffBody(
1070 $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
1071 $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
1072 );
1073
1074 $formattedDiff = $diffEngine->addHeader(
1075 $formattedDiff,
1076 $this->diffHeader( $previousRev, 'o' ),
1077 $this->diffHeader( $currentRev, 'n' )
1078 );
1079
1080 $this->getOutput()->addHTML( "<div>$formattedDiff</div>\n" );
1081 }
1082
1083 /**
1084 * @param Revision $rev
1085 * @param string $prefix
1086 * @return string
1087 */
1088 private function diffHeader( $rev, $prefix ) {
1089 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1090 if ( $isDeleted ) {
1091 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1092 $targetPage = $this->getPageTitle();
1093 $targetQuery = array(
1094 'target' => $this->mTargetObj->getPrefixedText(),
1095 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
1096 );
1097 } else {
1098 /// @todo FIXME: getId() may return non-zero for deleted revs...
1099 $targetPage = $rev->getTitle();
1100 $targetQuery = array( 'oldid' => $rev->getId() );
1101 }
1102
1103 // Add show/hide deletion links if available
1104 $user = $this->getUser();
1105 $lang = $this->getLanguage();
1106 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1107
1108 if ( $rdel ) {
1109 $rdel = " $rdel";
1110 }
1111
1112 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1113
1114 $tags = wfGetDB( DB_SLAVE )->selectField(
1115 'tag_summary',
1116 'ts_tags',
1117 array( 'ts_rev_id' => $rev->getId() ),
1118 __METHOD__
1119 );
1120 $tagSummary = ChangeTags::formatSummaryRow( $tags, 'deleteddiff' );
1121
1122 // FIXME This is reimplementing DifferenceEngine#getRevisionHeader
1123 // and partially #showDiffPage, but worse
1124 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1125 Linker::link(
1126 $targetPage,
1127 $this->msg(
1128 'revisionasof',
1129 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1130 $lang->userDate( $rev->getTimestamp(), $user ),
1131 $lang->userTime( $rev->getTimestamp(), $user )
1132 )->escaped(),
1133 array(),
1134 $targetQuery
1135 ) .
1136 '</strong></div>' .
1137 '<div id="mw-diff-' . $prefix . 'title2">' .
1138 Linker::revUserTools( $rev ) . '<br />' .
1139 '</div>' .
1140 '<div id="mw-diff-' . $prefix . 'title3">' .
1141 $minor . Linker::revComment( $rev ) . $rdel . '<br />' .
1142 '</div>' .
1143 '<div id="mw-diff-' . $prefix . 'title5">' .
1144 $tagSummary[0] . '<br />' .
1145 '</div>';
1146 }
1147
1148 /**
1149 * Show a form confirming whether a tokenless user really wants to see a file
1150 * @param string $key
1151 */
1152 private function showFileConfirmationForm( $key ) {
1153 $out = $this->getOutput();
1154 $lang = $this->getLanguage();
1155 $user = $this->getUser();
1156 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1157 $out->addWikiMsg( 'undelete-show-file-confirm',
1158 $this->mTargetObj->getText(),
1159 $lang->userDate( $file->getTimestamp(), $user ),
1160 $lang->userTime( $file->getTimestamp(), $user ) );
1161 $out->addHTML(
1162 Xml::openElement( 'form', array(
1163 'method' => 'POST',
1164 'action' => $this->getPageTitle()->getLocalURL( array(
1165 'target' => $this->mTarget,
1166 'file' => $key,
1167 'token' => $user->getEditToken( $key ),
1168 ) ),
1169 )
1170 ) .
1171 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1172 '</form>'
1173 );
1174 }
1175
1176 /**
1177 * Show a deleted file version requested by the visitor.
1178 * @param string $key
1179 */
1180 private function showFile( $key ) {
1181 $this->getOutput()->disable();
1182
1183 # We mustn't allow the output to be Squid cached, otherwise
1184 # if an admin previews a deleted image, and it's cached, then
1185 # a user without appropriate permissions can toddle off and
1186 # nab the image, and Squid will serve it
1187 $response = $this->getRequest()->response();
1188 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1189 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1190 $response->header( 'Pragma: no-cache' );
1191
1192 $repo = RepoGroup::singleton()->getLocalRepo();
1193 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1194 $repo->streamFile( $path );
1195 }
1196
1197 private function showHistory() {
1198 $out = $this->getOutput();
1199 if ( $this->mAllowed ) {
1200 $out->addModules( 'mediawiki.special.undelete' );
1201 }
1202 $out->wrapWikiMsg(
1203 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1204 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1205 );
1206
1207 $archive = new PageArchive( $this->mTargetObj );
1208 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1209 /*
1210 $text = $archive->getLastRevisionText();
1211 if( is_null( $text ) ) {
1212 $out->addWikiMsg( 'nohistory' );
1213 return;
1214 }
1215 */
1216 $out->addHTML( '<div class="mw-undelete-history">' );
1217 if ( $this->mAllowed ) {
1218 $out->addWikiMsg( 'undeletehistory' );
1219 $out->addWikiMsg( 'undeleterevdel' );
1220 } else {
1221 $out->addWikiMsg( 'undeletehistorynoadmin' );
1222 }
1223 $out->addHTML( '</div>' );
1224
1225 # List all stored revisions
1226 $revisions = $archive->listRevisions();
1227 $files = $archive->listFiles();
1228
1229 $haveRevisions = $revisions && $revisions->numRows() > 0;
1230 $haveFiles = $files && $files->numRows() > 0;
1231
1232 # Batch existence check on user and talk pages
1233 if ( $haveRevisions ) {
1234 $batch = new LinkBatch();
1235 foreach ( $revisions as $row ) {
1236 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1237 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1238 }
1239 $batch->execute();
1240 $revisions->seek( 0 );
1241 }
1242 if ( $haveFiles ) {
1243 $batch = new LinkBatch();
1244 foreach ( $files as $row ) {
1245 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1246 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1247 }
1248 $batch->execute();
1249 $files->seek( 0 );
1250 }
1251
1252 if ( $this->mAllowed ) {
1253 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
1254 # Start the form here
1255 $top = Xml::openElement(
1256 'form',
1257 array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' )
1258 );
1259 $out->addHTML( $top );
1260 }
1261
1262 # Show relevant lines from the deletion log:
1263 $deleteLogPage = new LogPage( 'delete' );
1264 $out->addHTML( Xml::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1265 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1266 # Show relevant lines from the suppression log:
1267 $suppressLogPage = new LogPage( 'suppress' );
1268 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1269 $out->addHTML( Xml::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1270 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1271 }
1272
1273 if ( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1274 # Format the user-visible controls (comment field, submission button)
1275 # in a nice little table
1276 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1277 $unsuppressBox =
1278 "<tr>
1279 <td>&#160;</td>
1280 <td class='mw-input'>" .
1281 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1282 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ) .
1283 "</td>
1284 </tr>";
1285 } else {
1286 $unsuppressBox = '';
1287 }
1288
1289 $table = Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1290 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1291 "<tr>
1292 <td colspan='2' class='mw-undelete-extrahelp'>" .
1293 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1294 "</td>
1295 </tr>
1296 <tr>
1297 <td class='mw-label'>" .
1298 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1299 "</td>
1300 <td class='mw-input'>" .
1301 Xml::input(
1302 'wpComment',
1303 50,
1304 $this->mComment,
1305 array( 'id' => 'wpComment', 'autofocus' => true )
1306 ) .
1307 "</td>
1308 </tr>
1309 <tr>
1310 <td>&#160;</td>
1311 <td class='mw-submit'>" .
1312 Xml::submitButton(
1313 $this->msg( 'undeletebtn' )->text(),
1314 array( 'name' => 'restore', 'id' => 'mw-undelete-submit' )
1315 ) . ' ' .
1316 Xml::submitButton(
1317 $this->msg( 'undeleteinvert' )->text(),
1318 array( 'name' => 'invert', 'id' => 'mw-undelete-invert' )
1319 ) .
1320 "</td>
1321 </tr>" .
1322 $unsuppressBox .
1323 Xml::closeElement( 'table' ) .
1324 Xml::closeElement( 'fieldset' );
1325
1326 $out->addHTML( $table );
1327 }
1328
1329 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1330
1331 if ( $haveRevisions ) {
1332 # The page's stored (deleted) history:
1333 $out->addHTML( '<ul>' );
1334 $remaining = $revisions->numRows();
1335 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1336
1337 foreach ( $revisions as $row ) {
1338 $remaining--;
1339 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1340 }
1341 $revisions->free();
1342 $out->addHTML( '</ul>' );
1343 } else {
1344 $out->addWikiMsg( 'nohistory' );
1345 }
1346
1347 if ( $haveFiles ) {
1348 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1349 $out->addHTML( '<ul>' );
1350 foreach ( $files as $row ) {
1351 $out->addHTML( $this->formatFileRow( $row ) );
1352 }
1353 $files->free();
1354 $out->addHTML( '</ul>' );
1355 }
1356
1357 if ( $this->mAllowed ) {
1358 # Slip in the hidden controls here
1359 $misc = Html::hidden( 'target', $this->mTarget );
1360 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1361 $misc .= Xml::closeElement( 'form' );
1362 $out->addHTML( $misc );
1363 }
1364
1365 return true;
1366 }
1367
1368 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1369 $rev = Revision::newFromArchiveRow( $row,
1370 array(
1371 'title' => $this->mTargetObj
1372 ) );
1373
1374 $revTextSize = '';
1375 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1376 // Build checkboxen...
1377 if ( $this->mAllowed ) {
1378 if ( $this->mInvert ) {
1379 if ( in_array( $ts, $this->mTargetTimestamp ) ) {
1380 $checkBox = Xml::check( "ts$ts" );
1381 } else {
1382 $checkBox = Xml::check( "ts$ts", true );
1383 }
1384 } else {
1385 $checkBox = Xml::check( "ts$ts" );
1386 }
1387 } else {
1388 $checkBox = '';
1389 }
1390
1391 // Build page & diff links...
1392 $user = $this->getUser();
1393 if ( $this->mCanView ) {
1394 $titleObj = $this->getPageTitle();
1395 # Last link
1396 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1397 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1398 $last = $this->msg( 'diff' )->escaped();
1399 } elseif ( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1400 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1401 $last = Linker::linkKnown(
1402 $titleObj,
1403 $this->msg( 'diff' )->escaped(),
1404 array(),
1405 array(
1406 'target' => $this->mTargetObj->getPrefixedText(),
1407 'timestamp' => $ts,
1408 'diff' => 'prev'
1409 )
1410 );
1411 } else {
1412 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1413 $last = $this->msg( 'diff' )->escaped();
1414 }
1415 } else {
1416 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1417 $last = $this->msg( 'diff' )->escaped();
1418 }
1419
1420 // User links
1421 $userLink = Linker::revUserTools( $rev );
1422
1423 // Minor edit
1424 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1425
1426 // Revision text size
1427 $size = $row->ar_len;
1428 if ( !is_null( $size ) ) {
1429 $revTextSize = Linker::formatRevisionSize( $size );
1430 }
1431
1432 // Edit summary
1433 $comment = Linker::revComment( $rev );
1434
1435 // Tags
1436 $attribs = array();
1437 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'deletedhistory' );
1438 if ( $classes ) {
1439 $attribs['class'] = implode( ' ', $classes );
1440 }
1441
1442 // Revision delete links
1443 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1444
1445 $revisionRow = $this->msg( 'undelete-revision-row' )
1446 ->rawParams(
1447 $checkBox,
1448 $revdlink,
1449 $last,
1450 $pageLink,
1451 $userLink,
1452 $minor,
1453 $revTextSize,
1454 $comment,
1455 $tagSummary
1456 )
1457 ->escaped();
1458
1459 return Xml::tags( 'li', $attribs, $revisionRow ) . "\n";
1460 }
1461
1462 private function formatFileRow( $row ) {
1463 $file = ArchivedFile::newFromRow( $row );
1464 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1465 $user = $this->getUser();
1466
1467 if ( $this->mAllowed && $row->fa_storage_key ) {
1468 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1469 $key = urlencode( $row->fa_storage_key );
1470 $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
1471 } else {
1472 $checkBox = '';
1473 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1474 }
1475 $userLink = $this->getFileUser( $file );
1476 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1477 $bytes = $this->msg( 'parentheses' )
1478 ->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )
1479 ->plain();
1480 $data = htmlspecialchars( $data . ' ' . $bytes );
1481 $comment = $this->getFileComment( $file );
1482
1483 // Add show/hide deletion links if available
1484 $canHide = $user->isAllowed( 'deleterevision' );
1485 if ( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1486 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1487 // Revision was hidden from sysops
1488 $revdlink = Linker::revDeleteLinkDisabled( $canHide );
1489 } else {
1490 $query = array(
1491 'type' => 'filearchive',
1492 'target' => $this->mTargetObj->getPrefixedDBkey(),
1493 'ids' => $row->fa_id
1494 );
1495 $revdlink = Linker::revDeleteLink( $query,
1496 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1497 }
1498 } else {
1499 $revdlink = '';
1500 }
1501
1502 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1503 }
1504
1505 /**
1506 * Fetch revision text link if it's available to all users
1507 *
1508 * @param Revision $rev
1509 * @param Title $titleObj
1510 * @param string $ts Timestamp
1511 * @return string
1512 */
1513 function getPageLink( $rev, $titleObj, $ts ) {
1514 $user = $this->getUser();
1515 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1516
1517 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1518 return '<span class="history-deleted">' . $time . '</span>';
1519 }
1520
1521 $link = Linker::linkKnown(
1522 $titleObj,
1523 htmlspecialchars( $time ),
1524 array(),
1525 array(
1526 'target' => $this->mTargetObj->getPrefixedText(),
1527 'timestamp' => $ts
1528 )
1529 );
1530
1531 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1532 $link = '<span class="history-deleted">' . $link . '</span>';
1533 }
1534
1535 return $link;
1536 }
1537
1538 /**
1539 * Fetch image view link if it's available to all users
1540 *
1541 * @param File|ArchivedFile $file
1542 * @param Title $titleObj
1543 * @param string $ts A timestamp
1544 * @param string $key A storage key
1545 *
1546 * @return string HTML fragment
1547 */
1548 function getFileLink( $file, $titleObj, $ts, $key ) {
1549 $user = $this->getUser();
1550 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1551
1552 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
1553 return '<span class="history-deleted">' . $time . '</span>';
1554 }
1555
1556 $link = Linker::linkKnown(
1557 $titleObj,
1558 htmlspecialchars( $time ),
1559 array(),
1560 array(
1561 'target' => $this->mTargetObj->getPrefixedText(),
1562 'file' => $key,
1563 'token' => $user->getEditToken( $key )
1564 )
1565 );
1566
1567 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1568 $link = '<span class="history-deleted">' . $link . '</span>';
1569 }
1570
1571 return $link;
1572 }
1573
1574 /**
1575 * Fetch file's user id if it's available to this user
1576 *
1577 * @param File|ArchivedFile $file
1578 * @return string HTML fragment
1579 */
1580 function getFileUser( $file ) {
1581 if ( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1582 return '<span class="history-deleted">' .
1583 $this->msg( 'rev-deleted-user' )->escaped() .
1584 '</span>';
1585 }
1586
1587 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1588 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1589
1590 if ( $file->isDeleted( File::DELETED_USER ) ) {
1591 $link = '<span class="history-deleted">' . $link . '</span>';
1592 }
1593
1594 return $link;
1595 }
1596
1597 /**
1598 * Fetch file upload comment if it's available to this user
1599 *
1600 * @param File|ArchivedFile $file
1601 * @return string HTML fragment
1602 */
1603 function getFileComment( $file ) {
1604 if ( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1605 return '<span class="history-deleted"><span class="comment">' .
1606 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1607 }
1608
1609 $link = Linker::commentBlock( $file->getRawDescription() );
1610
1611 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1612 $link = '<span class="history-deleted">' . $link . '</span>';
1613 }
1614
1615 return $link;
1616 }
1617
1618 function undelete() {
1619 global $wgUploadMaintenance;
1620
1621 if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
1622 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1623 }
1624
1625 if ( wfReadOnly() ) {
1626 throw new ReadOnlyError;
1627 }
1628
1629 $out = $this->getOutput();
1630 $archive = new PageArchive( $this->mTargetObj );
1631 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1632 $ok = $archive->undelete(
1633 $this->mTargetTimestamp,
1634 $this->mComment,
1635 $this->mFileVersions,
1636 $this->mUnsuppress,
1637 $this->getUser()
1638 );
1639
1640 if ( is_array( $ok ) ) {
1641 if ( $ok[1] ) { // Undeleted file count
1642 wfRunHooks( 'FileUndeleteComplete', array(
1643 $this->mTargetObj, $this->mFileVersions,
1644 $this->getUser(), $this->mComment ) );
1645 }
1646
1647 $link = Linker::linkKnown( $this->mTargetObj );
1648 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1649 } else {
1650 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1651 }
1652
1653 // Show revision undeletion warnings and errors
1654 $status = $archive->getRevisionStatus();
1655 if ( $status && !$status->isGood() ) {
1656 $out->addWikiText( '<div class="error">' .
1657 $status->getWikiText(
1658 'cannotundelete',
1659 'cannotundelete'
1660 ) . '</div>'
1661 );
1662 }
1663
1664 // Show file undeletion warnings and errors
1665 $status = $archive->getFileStatus();
1666 if ( $status && !$status->isGood() ) {
1667 $out->addWikiText( '<div class="error">' .
1668 $status->getWikiText(
1669 'undelete-error-short',
1670 'undelete-error-long'
1671 ) . '</div>'
1672 );
1673 }
1674 }
1675
1676 protected function getGroupName() {
1677 return 'pagetools';
1678 }
1679 }