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