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