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