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