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