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