Revert to arbitrarily old point before initial remote branch creation to help clean up
[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' => array( 'ar_namespace', 'ar_title' ),
101 'ORDER BY' => array( '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 $page = $dbw->selectRow( 'page',
409 array( 'page_id', 'page_latest' ),
410 array( 'page_namespace' => $this->title->getNamespace(),
411 'page_title' => $this->title->getDBkey() ),
412 __METHOD__,
413 array( 'FOR UPDATE' ) // lock page
414 );
415 if( $page ) {
416 $makepage = false;
417 # Page already exists. Import the history, and if necessary
418 # we'll update the latest revision field in the record.
419 $newid = 0;
420 $pageId = $page->page_id;
421 $previousRevId = $page->page_latest;
422 # Get the time span of this page
423 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
424 array( 'rev_id' => $previousRevId ),
425 __METHOD__ );
426 if( $previousTimestamp === false ) {
427 wfDebug( __METHOD__.": existing page refers to a page_latest that does not exist\n" );
428 return 0;
429 }
430 } else {
431 # Have to create a new article...
432 $makepage = true;
433 $previousRevId = 0;
434 $previousTimestamp = 0;
435 }
436
437 if( $restoreAll ) {
438 $oldones = '1 = 1'; # All revisions...
439 } else {
440 $oldts = implode( ',',
441 array_map( array( &$dbw, 'addQuotes' ),
442 array_map( array( &$dbw, 'timestamp' ),
443 $timestamps ) ) );
444
445 $oldones = "ar_timestamp IN ( {$oldts} )";
446 }
447
448 /**
449 * Select each archived revision...
450 */
451 $result = $dbw->select( 'archive',
452 /* fields */ array(
453 'ar_rev_id',
454 'ar_text',
455 'ar_comment',
456 'ar_user',
457 'ar_user_text',
458 'ar_timestamp',
459 'ar_minor_edit',
460 'ar_flags',
461 'ar_text_id',
462 'ar_deleted',
463 'ar_page_id',
464 'ar_len',
465 'ar_sha1' ),
466 /* WHERE */ array(
467 'ar_namespace' => $this->title->getNamespace(),
468 'ar_title' => $this->title->getDBkey(),
469 $oldones ),
470 __METHOD__,
471 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
472 );
473 $ret = $dbw->resultObject( $result );
474 $rev_count = $dbw->numRows( $result );
475 if( !$rev_count ) {
476 wfDebug( __METHOD__ . ": no revisions to restore\n" );
477 return false; // ???
478 }
479
480 $ret->seek( $rev_count - 1 ); // move to last
481 $row = $ret->fetchObject(); // get newest archived rev
482 $ret->seek( 0 ); // move back
483
484 if( $makepage ) {
485 // Check the state of the newest to-be version...
486 if( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
487 return false; // we can't leave the current revision like this!
488 }
489 // Safe to insert now...
490 $newid = $article->insertOn( $dbw );
491 $pageId = $newid;
492 } else {
493 // Check if a deleted revision will become the current revision...
494 if( $row->ar_timestamp > $previousTimestamp ) {
495 // Check the state of the newest to-be version...
496 if( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
497 return false; // we can't leave the current revision like this!
498 }
499 }
500 }
501
502 $revision = null;
503 $restored = 0;
504
505 foreach ( $ret as $row ) {
506 // Check for key dupes due to shitty archive integrity.
507 if( $row->ar_rev_id ) {
508 $exists = $dbw->selectField( 'revision', '1',
509 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
510 if( $exists ) {
511 continue; // don't throw DB errors
512 }
513 }
514 // Insert one revision at a time...maintaining deletion status
515 // unless we are specifically removing all restrictions...
516 $revision = Revision::newFromArchiveRow( $row,
517 array(
518 'page' => $pageId,
519 'deleted' => $unsuppress ? 0 : $row->ar_deleted
520 ) );
521
522 $revision->insertOn( $dbw );
523 $restored++;
524
525 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
526 }
527 # Now that it's safely stored, take it out of the archive
528 $dbw->delete( 'archive',
529 /* WHERE */ array(
530 'ar_namespace' => $this->title->getNamespace(),
531 'ar_title' => $this->title->getDBkey(),
532 $oldones ),
533 __METHOD__ );
534
535 // Was anything restored at all?
536 if ( $restored == 0 ) {
537 return 0;
538 }
539
540 $created = (bool)$newid;
541
542 // Attach the latest revision to the page...
543 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
544 if ( $created || $wasnew ) {
545 // Update site stats, link tables, etc
546 $user = User::newFromName( $revision->getRawUserText(), false );
547 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
548 }
549
550 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment ) );
551
552 if( $this->title->getNamespace() == NS_FILE ) {
553 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
554 $update->doUpdate();
555 }
556
557 return $restored;
558 }
559
560 /**
561 * @return Status
562 */
563 function getFileStatus() { return $this->fileStatus; }
564 }
565
566 /**
567 * Special page allowing users with the appropriate permissions to view
568 * and restore deleted content.
569 *
570 * @ingroup SpecialPage
571 */
572 class SpecialUndelete extends SpecialPage {
573 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
574 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
575
576 /**
577 * @var Title
578 */
579 var $mTargetObj;
580
581 function __construct() {
582 parent::__construct( 'Undelete', 'deletedhistory' );
583 }
584
585 function loadRequest( $par ) {
586 $request = $this->getRequest();
587 $user = $this->getUser();
588
589 $this->mAction = $request->getVal( 'action' );
590 if ( $par !== null && $par !== '' ) {
591 $this->mTarget = $par;
592 } else {
593 $this->mTarget = $request->getVal( 'target' );
594 }
595 $this->mTargetObj = null;
596 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
597 $this->mTargetObj = Title::newFromURL( $this->mTarget );
598 }
599 $this->mSearchPrefix = $request->getText( 'prefix' );
600 $time = $request->getVal( 'timestamp' );
601 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
602 $this->mFilename = $request->getVal( 'file' );
603
604 $posted = $request->wasPosted() &&
605 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
606 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
607 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
608 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
609 $this->mDiff = $request->getCheck( 'diff' );
610 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
611 $this->mComment = $request->getText( 'wpComment' );
612 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
613 $this->mToken = $request->getVal( 'token' );
614
615 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
616 $this->mAllowed = true; // user can restore
617 $this->mCanView = true; // user can view content
618 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
619 $this->mAllowed = false; // user cannot restore
620 $this->mCanView = true; // user can view content
621 $this->mRestore = false;
622 } else { // user can only view the list of revisions
623 $this->mAllowed = false;
624 $this->mCanView = false;
625 $this->mTimestamp = '';
626 $this->mRestore = false;
627 }
628
629 if( $this->mRestore || $this->mInvert ) {
630 $timestamps = array();
631 $this->mFileVersions = array();
632 foreach( $request->getValues() as $key => $val ) {
633 $matches = array();
634 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
635 array_push( $timestamps, $matches[1] );
636 }
637
638 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
639 $this->mFileVersions[] = intval( $matches[1] );
640 }
641 }
642 rsort( $timestamps );
643 $this->mTargetTimestamp = $timestamps;
644 }
645 }
646
647 function execute( $par ) {
648 $this->checkPermissions();
649 $user = $this->getUser();
650
651 $this->setHeaders();
652 $this->outputHeader();
653
654 $this->loadRequest( $par );
655
656 $out = $this->getOutput();
657
658 if ( is_null( $this->mTargetObj ) ) {
659 $out->addWikiMsg( 'undelete-header' );
660
661 # Not all users can just browse every deleted page from the list
662 if ( $user->isAllowed( 'browsearchive' ) ) {
663 $this->showSearchForm();
664 }
665 return;
666 }
667
668 if ( $this->mAllowed ) {
669 $out->setPageTitle( $this->msg( 'undeletepage' ) );
670 } else {
671 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
672 }
673
674 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
675
676 if ( $this->mTimestamp !== '' ) {
677 $this->showRevision( $this->mTimestamp );
678 } elseif ( $this->mFilename !== null ) {
679 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
680 // Check if user is allowed to see this file
681 if ( !$file->exists() ) {
682 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
683 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
684 if( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
685 throw new PermissionsError( 'suppressrevision' );
686 } else {
687 throw new PermissionsError( 'deletedtext' );
688 }
689 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
690 $this->showFileConfirmationForm( $this->mFilename );
691 } else {
692 $this->showFile( $this->mFilename );
693 }
694 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
695 $this->undelete();
696 } else {
697 $this->showHistory();
698 }
699 }
700
701 function showSearchForm() {
702 global $wgScript;
703
704 $out = $this->getOutput();
705 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
706 $out->addHTML(
707 Xml::openElement( 'form', array(
708 'method' => 'get',
709 'action' => $wgScript ) ) .
710 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
711 Html::hidden( 'title',
712 $this->getTitle()->getPrefixedDbKey() ) .
713 Xml::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
714 'prefix', 'prefix', 20,
715 $this->mSearchPrefix ) . ' ' .
716 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
717 Xml::closeElement( 'fieldset' ) .
718 Xml::closeElement( 'form' )
719 );
720
721 # List undeletable articles
722 if( $this->mSearchPrefix ) {
723 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
724 $this->showList( $result );
725 }
726 }
727
728 /**
729 * Generic list of deleted pages
730 *
731 * @param $result ResultWrapper
732 * @return bool
733 */
734 private function showList( $result ) {
735 $out = $this->getOutput();
736
737 if( $result->numRows() == 0 ) {
738 $out->addWikiMsg( 'undelete-no-results' );
739 return;
740 }
741
742 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
743
744 $undelete = $this->getTitle();
745 $out->addHTML( "<ul>\n" );
746 foreach ( $result as $row ) {
747 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
748 if ( $title !== null ) {
749 $item = Linker::linkKnown(
750 $undelete,
751 htmlspecialchars( $title->getPrefixedText() ),
752 array(),
753 array( 'target' => $title->getPrefixedText() )
754 );
755 } else {
756 // The title is no longer valid, show as text
757 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
758 $item = htmlspecialchars( $title->getPrefixedText() );
759 }
760 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
761 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
762 }
763 $result->free();
764 $out->addHTML( "</ul>\n" );
765
766 return true;
767 }
768
769 private function showRevision( $timestamp ) {
770 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
771 return 0;
772 }
773
774 $archive = new PageArchive( $this->mTargetObj );
775 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) );
776 $rev = $archive->getRevision( $timestamp );
777
778 $out = $this->getOutput();
779 $user = $this->getUser();
780
781 if( !$rev ) {
782 $out->addWikiMsg( 'undeleterevision-missing' );
783 return;
784 }
785
786 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
787 if( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
788 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
789 return;
790 } else {
791 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
792 $out->addHTML( '<br />' );
793 // and we are allowed to see...
794 }
795 }
796
797 if( $this->mDiff ) {
798 $previousRev = $archive->getPreviousRevision( $timestamp );
799 if( $previousRev ) {
800 $this->showDiff( $previousRev, $rev );
801 if( $this->mDiffOnly ) {
802 return;
803 } else {
804 $out->addHTML( '<hr />' );
805 }
806 } else {
807 $out->addWikiMsg( 'undelete-nodiff' );
808 }
809 }
810
811 $link = Linker::linkKnown(
812 $this->getTitle( $this->mTargetObj->getPrefixedDBkey() ),
813 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
814 );
815
816 $lang = $this->getLanguage();
817
818 // date and time are separate parameters to facilitate localisation.
819 // $time is kept for backward compat reasons.
820 $time = $lang->userTimeAndDate( $timestamp, $user );
821 $d = $lang->userDate( $timestamp, $user );
822 $t = $lang->userTime( $timestamp, $user );
823 $userLink = Linker::revUserTools( $rev );
824
825 if( $this->mPreview ) {
826 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
827 } else {
828 $openDiv = '<div id="mw-undelete-revision">';
829 }
830 $out->addHTML( $openDiv );
831
832 // Revision delete links
833 if ( !$this->mDiff ) {
834 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
835 if ( $revdel ) {
836 $out->addHTML( "$revdel " );
837 }
838 }
839
840 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
841 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
842 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
843
844 if( $this->mPreview ) {
845 // Hide [edit]s
846 $popts = $out->parserOptions();
847 $popts->setEditSection( false );
848 $out->parserOptions( $popts );
849 $out->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER, $user ), $this->mTargetObj, true );
850 }
851
852 $out->addHTML(
853 Xml::element( 'textarea', array(
854 'readonly' => 'readonly',
855 'cols' => intval( $user->getOption( 'cols' ) ),
856 'rows' => intval( $user->getOption( 'rows' ) ) ),
857 $rev->getText( Revision::FOR_THIS_USER, $user ) . "\n" ) .
858 Xml::openElement( 'div' ) .
859 Xml::openElement( 'form', array(
860 'method' => 'post',
861 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
862 Xml::element( 'input', array(
863 'type' => 'hidden',
864 'name' => 'target',
865 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
866 Xml::element( 'input', array(
867 'type' => 'hidden',
868 'name' => 'timestamp',
869 'value' => $timestamp ) ) .
870 Xml::element( 'input', array(
871 'type' => 'hidden',
872 'name' => 'wpEditToken',
873 'value' => $user->getEditToken() ) ) .
874 Xml::element( 'input', array(
875 'type' => 'submit',
876 'name' => 'preview',
877 'value' => $this->msg( 'showpreview' )->text() ) ) .
878 Xml::element( 'input', array(
879 'name' => 'diff',
880 'type' => 'submit',
881 'value' => $this->msg( 'showdiff' )->text() ) ) .
882 Xml::closeElement( 'form' ) .
883 Xml::closeElement( 'div' ) );
884 }
885
886 /**
887 * Build a diff display between this and the previous either deleted
888 * or non-deleted edit.
889 *
890 * @param $previousRev Revision
891 * @param $currentRev Revision
892 * @return String: HTML
893 */
894 function showDiff( $previousRev, $currentRev ) {
895 $diffEngine = new DifferenceEngine( $this->getContext() );
896 $diffEngine->showDiffStyle();
897 $this->getOutput()->addHTML(
898 "<div>" .
899 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
900 "<col class='diff-marker' />" .
901 "<col class='diff-content' />" .
902 "<col class='diff-marker' />" .
903 "<col class='diff-content' />" .
904 "<tr>" .
905 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
906 $this->diffHeader( $previousRev, 'o' ) .
907 "</td>\n" .
908 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
909 $this->diffHeader( $currentRev, 'n' ) .
910 "</td>\n" .
911 "</tr>" .
912 $diffEngine->generateDiffBody(
913 $previousRev->getText(), $currentRev->getText() ) .
914 "</table>" .
915 "</div>\n"
916 );
917 }
918
919 /**
920 * @param $rev Revision
921 * @param $prefix
922 * @return string
923 */
924 private function diffHeader( $rev, $prefix ) {
925 $isDeleted = !( $rev->getId() && $rev->getTitle() );
926 if( $isDeleted ) {
927 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
928 $targetPage = $this->getTitle();
929 $targetQuery = array(
930 'target' => $this->mTargetObj->getPrefixedText(),
931 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
932 );
933 } else {
934 /// @todo FIXME: getId() may return non-zero for deleted revs...
935 $targetPage = $rev->getTitle();
936 $targetQuery = array( 'oldid' => $rev->getId() );
937 }
938 // Add show/hide deletion links if available
939 $user = $this->getUser();
940 $lang = $this->getLanguage();
941 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
942 if ( $rdel ) $rdel = " $rdel";
943 return
944 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
945 Linker::link(
946 $targetPage,
947 $this->msg(
948 'revisionasof',
949 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
950 $lang->userDate( $rev->getTimestamp(), $user ),
951 $lang->userTime( $rev->getTimestamp(), $user )
952 )->escaped(),
953 array(),
954 $targetQuery
955 ) .
956 '</strong></div>' .
957 '<div id="mw-diff-'.$prefix.'title2">' .
958 Linker::revUserTools( $rev ) . '<br />' .
959 '</div>' .
960 '<div id="mw-diff-'.$prefix.'title3">' .
961 Linker::revComment( $rev ) . $rdel . '<br />' .
962 '</div>';
963 }
964
965 /**
966 * Show a form confirming whether a tokenless user really wants to see a file
967 */
968 private function showFileConfirmationForm( $key ) {
969 $out = $this->getOutput();
970 $lang = $this->getLanguage();
971 $user = $this->getUser();
972 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
973 $out->addWikiMsg( 'undelete-show-file-confirm',
974 $this->mTargetObj->getText(),
975 $lang->userDate( $file->getTimestamp(), $user ),
976 $lang->userTime( $file->getTimestamp(), $user ) );
977 $out->addHTML(
978 Xml::openElement( 'form', array(
979 'method' => 'POST',
980 'action' => $this->getTitle()->getLocalURL(
981 'target=' . urlencode( $this->mTarget ) .
982 '&file=' . urlencode( $key ) .
983 '&token=' . urlencode( $user->getEditToken( $key ) ) )
984 )
985 ) .
986 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
987 '</form>'
988 );
989 }
990
991 /**
992 * Show a deleted file version requested by the visitor.
993 */
994 private function showFile( $key ) {
995 $this->getOutput()->disable();
996
997 # We mustn't allow the output to be Squid cached, otherwise
998 # if an admin previews a deleted image, and it's cached, then
999 # a user without appropriate permissions can toddle off and
1000 # nab the image, and Squid will serve it
1001 $response = $this->getRequest()->response();
1002 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1003 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1004 $response->header( 'Pragma: no-cache' );
1005
1006 $repo = RepoGroup::singleton()->getLocalRepo();
1007 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1008 $repo->streamFile( $path );
1009 }
1010
1011 private function showHistory() {
1012 $out = $this->getOutput();
1013 if( $this->mAllowed ) {
1014 $out->addModules( 'mediawiki.special.undelete' );
1015 }
1016 $out->wrapWikiMsg(
1017 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1018 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1019 );
1020
1021 $archive = new PageArchive( $this->mTargetObj );
1022 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1023 /*
1024 $text = $archive->getLastRevisionText();
1025 if( is_null( $text ) ) {
1026 $out->addWikiMsg( 'nohistory' );
1027 return;
1028 }
1029 */
1030 $out->addHTML( '<div class="mw-undelete-history">' );
1031 if ( $this->mAllowed ) {
1032 $out->addWikiMsg( 'undeletehistory' );
1033 $out->addWikiMsg( 'undeleterevdel' );
1034 } else {
1035 $out->addWikiMsg( 'undeletehistorynoadmin' );
1036 }
1037 $out->addHTML( '</div>' );
1038
1039 # List all stored revisions
1040 $revisions = $archive->listRevisions();
1041 $files = $archive->listFiles();
1042
1043 $haveRevisions = $revisions && $revisions->numRows() > 0;
1044 $haveFiles = $files && $files->numRows() > 0;
1045
1046 # Batch existence check on user and talk pages
1047 if( $haveRevisions ) {
1048 $batch = new LinkBatch();
1049 foreach ( $revisions as $row ) {
1050 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1051 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1052 }
1053 $batch->execute();
1054 $revisions->seek( 0 );
1055 }
1056 if( $haveFiles ) {
1057 $batch = new LinkBatch();
1058 foreach ( $files as $row ) {
1059 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1060 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1061 }
1062 $batch->execute();
1063 $files->seek( 0 );
1064 }
1065
1066 if ( $this->mAllowed ) {
1067 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1068 # Start the form here
1069 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1070 $out->addHTML( $top );
1071 }
1072
1073 # Show relevant lines from the deletion log:
1074 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) . "\n" );
1075 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1076 # Show relevant lines from the suppression log:
1077 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1078 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'suppress' ) ) . "\n" );
1079 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1080 }
1081
1082 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1083 # Format the user-visible controls (comment field, submission button)
1084 # in a nice little table
1085 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1086 $unsuppressBox =
1087 "<tr>
1088 <td>&#160;</td>
1089 <td class='mw-input'>" .
1090 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1091 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ).
1092 "</td>
1093 </tr>";
1094 } else {
1095 $unsuppressBox = '';
1096 }
1097 $table =
1098 Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1099 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1100 "<tr>
1101 <td colspan='2' class='mw-undelete-extrahelp'>" .
1102 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1103 "</td>
1104 </tr>
1105 <tr>
1106 <td class='mw-label'>" .
1107 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1108 "</td>
1109 <td class='mw-input'>" .
1110 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
1111 "</td>
1112 </tr>
1113 <tr>
1114 <td>&#160;</td>
1115 <td class='mw-submit'>" .
1116 Xml::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1117 Xml::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1118 "</td>
1119 </tr>" .
1120 $unsuppressBox .
1121 Xml::closeElement( 'table' ) .
1122 Xml::closeElement( 'fieldset' );
1123
1124 $out->addHTML( $table );
1125 }
1126
1127 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1128
1129 if( $haveRevisions ) {
1130 # The page's stored (deleted) history:
1131 $out->addHTML( '<ul>' );
1132 $remaining = $revisions->numRows();
1133 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1134
1135 foreach ( $revisions as $row ) {
1136 $remaining--;
1137 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1138 }
1139 $revisions->free();
1140 $out->addHTML( '</ul>' );
1141 } else {
1142 $out->addWikiMsg( 'nohistory' );
1143 }
1144
1145 if( $haveFiles ) {
1146 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1147 $out->addHTML( '<ul>' );
1148 foreach ( $files as $row ) {
1149 $out->addHTML( $this->formatFileRow( $row ) );
1150 }
1151 $files->free();
1152 $out->addHTML( '</ul>' );
1153 }
1154
1155 if ( $this->mAllowed ) {
1156 # Slip in the hidden controls here
1157 $misc = Html::hidden( 'target', $this->mTarget );
1158 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1159 $misc .= Xml::closeElement( 'form' );
1160 $out->addHTML( $misc );
1161 }
1162
1163 return true;
1164 }
1165
1166 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1167 $rev = Revision::newFromArchiveRow( $row,
1168 array( 'page' => $this->mTargetObj->getArticleID() ) );
1169 $stxt = '';
1170 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1171 // Build checkboxen...
1172 if( $this->mAllowed ) {
1173 if( $this->mInvert ) {
1174 if( in_array( $ts, $this->mTargetTimestamp ) ) {
1175 $checkBox = Xml::check( "ts$ts" );
1176 } else {
1177 $checkBox = Xml::check( "ts$ts", true );
1178 }
1179 } else {
1180 $checkBox = Xml::check( "ts$ts" );
1181 }
1182 } else {
1183 $checkBox = '';
1184 }
1185 $user = $this->getUser();
1186 // Build page & diff links...
1187 if( $this->mCanView ) {
1188 $titleObj = $this->getTitle();
1189 # Last link
1190 if( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1191 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1192 $last = $this->msg( 'diff' )->escaped();
1193 } elseif( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1194 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1195 $last = Linker::linkKnown(
1196 $titleObj,
1197 $this->msg( 'diff' )->escaped(),
1198 array(),
1199 array(
1200 'target' => $this->mTargetObj->getPrefixedText(),
1201 'timestamp' => $ts,
1202 'diff' => 'prev'
1203 )
1204 );
1205 } else {
1206 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1207 $last = $this->msg( 'diff' )->escaped();
1208 }
1209 } else {
1210 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1211 $last = $this->msg( 'diff' )->escaped();
1212 }
1213 // User links
1214 $userLink = Linker::revUserTools( $rev );
1215 // Revision text size
1216 $size = $row->ar_len;
1217 if( !is_null( $size ) ) {
1218 $stxt = Linker::formatRevisionSize( $size );
1219 }
1220 // Edit summary
1221 $comment = Linker::revComment( $rev );
1222 // Revision delete links
1223 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1224 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1225 }
1226
1227 private function formatFileRow( $row ) {
1228 $file = ArchivedFile::newFromRow( $row );
1229
1230 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1231 $user = $this->getUser();
1232 if( $this->mAllowed && $row->fa_storage_key ) {
1233 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1234 $key = urlencode( $row->fa_storage_key );
1235 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1236 } else {
1237 $checkBox = '';
1238 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1239 }
1240 $userLink = $this->getFileUser( $file );
1241 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1242 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )->plain();
1243 $data = htmlspecialchars( $data . ' ' . $bytes );
1244 $comment = $this->getFileComment( $file );
1245
1246 // Add show/hide deletion links if available
1247 $canHide = $user->isAllowed( 'deleterevision' );
1248 if( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1249 if( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1250 $revdlink = Linker::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1251 } else {
1252 $query = array(
1253 'type' => 'filearchive',
1254 'target' => $this->mTargetObj->getPrefixedDBkey(),
1255 'ids' => $row->fa_id
1256 );
1257 $revdlink = Linker::revDeleteLink( $query,
1258 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1259 }
1260 } else {
1261 $revdlink = '';
1262 }
1263
1264 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1265 }
1266
1267 /**
1268 * Fetch revision text link if it's available to all users
1269 *
1270 * @param $rev Revision
1271 * @param $titleObj Title
1272 * @param $ts string Timestamp
1273 * @return string
1274 */
1275 function getPageLink( $rev, $titleObj, $ts ) {
1276 $user = $this->getUser();
1277 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1278
1279 if( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1280 return '<span class="history-deleted">' . $time . '</span>';
1281 } else {
1282 $link = Linker::linkKnown(
1283 $titleObj,
1284 htmlspecialchars( $time ),
1285 array(),
1286 array(
1287 'target' => $this->mTargetObj->getPrefixedText(),
1288 'timestamp' => $ts
1289 )
1290 );
1291 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1292 $link = '<span class="history-deleted">' . $link . '</span>';
1293 }
1294 return $link;
1295 }
1296 }
1297
1298 /**
1299 * Fetch image view link if it's available to all users
1300 *
1301 * @param $file File
1302 * @param $titleObj Title
1303 * @param $ts string A timestamp
1304 * @param $key String: a storage key
1305 *
1306 * @return String: HTML fragment
1307 */
1308 function getFileLink( $file, $titleObj, $ts, $key ) {
1309 $user = $this->getUser();
1310 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1311
1312 if( !$file->userCan( File::DELETED_FILE, $user ) ) {
1313 return '<span class="history-deleted">' . $time . '</span>';
1314 } else {
1315 $link = Linker::linkKnown(
1316 $titleObj,
1317 htmlspecialchars( $time ),
1318 array(),
1319 array(
1320 'target' => $this->mTargetObj->getPrefixedText(),
1321 'file' => $key,
1322 'token' => $user->getEditToken( $key )
1323 )
1324 );
1325 if( $file->isDeleted( File::DELETED_FILE ) ) {
1326 $link = '<span class="history-deleted">' . $link . '</span>';
1327 }
1328 return $link;
1329 }
1330 }
1331
1332 /**
1333 * Fetch file's user id if it's available to this user
1334 *
1335 * @param $file File
1336 * @return String: HTML fragment
1337 */
1338 function getFileUser( $file ) {
1339 if( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1340 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1341 } else {
1342 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1343 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1344 if( $file->isDeleted( File::DELETED_USER ) ) {
1345 $link = '<span class="history-deleted">' . $link . '</span>';
1346 }
1347 return $link;
1348 }
1349 }
1350
1351 /**
1352 * Fetch file upload comment if it's available to this user
1353 *
1354 * @param $file File
1355 * @return String: HTML fragment
1356 */
1357 function getFileComment( $file ) {
1358 if( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1359 return '<span class="history-deleted"><span class="comment">' .
1360 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1361 } else {
1362 $link = Linker::commentBlock( $file->getRawDescription() );
1363 if( $file->isDeleted( File::DELETED_COMMENT ) ) {
1364 $link = '<span class="history-deleted">' . $link . '</span>';
1365 }
1366 return $link;
1367 }
1368 }
1369
1370 function undelete() {
1371 global $wgUploadMaintenance;
1372
1373 if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
1374 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1375 }
1376
1377 if ( wfReadOnly() ) {
1378 throw new ReadOnlyError;
1379 }
1380
1381 $out = $this->getOutput();
1382 $archive = new PageArchive( $this->mTargetObj );
1383 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1384 $ok = $archive->undelete(
1385 $this->mTargetTimestamp,
1386 $this->mComment,
1387 $this->mFileVersions,
1388 $this->mUnsuppress );
1389
1390 if( is_array( $ok ) ) {
1391 if ( $ok[1] ) { // Undeleted file count
1392 wfRunHooks( 'FileUndeleteComplete', array(
1393 $this->mTargetObj, $this->mFileVersions,
1394 $this->getUser(), $this->mComment ) );
1395 }
1396
1397 $link = Linker::linkKnown( $this->mTargetObj );
1398 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1399 } else {
1400 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1401 $out->addWikiMsg( 'cannotundelete' );
1402 $out->addWikiMsg( 'undeleterevdel' );
1403 }
1404
1405 // Show file deletion warnings and errors
1406 $status = $archive->getFileStatus();
1407 if( $status && !$status->isGood() ) {
1408 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );
1409 }
1410 }
1411 }