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