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