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