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