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