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