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