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