* Added User paremeter to Revision::userCan(), Revision::userCanBitfield(), LogEvents...
[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 # Load latest data for the current page (bug 31179)
403 $article->loadPageData( 'fromdbmaster' );
404 $oldcountable = $article->isCountable();
405
406 $options = 'FOR UPDATE'; // lock page
407 $page = $dbw->selectRow( 'page',
408 array( 'page_id', 'page_latest' ),
409 array( 'page_namespace' => $this->title->getNamespace(),
410 'page_title' => $this->title->getDBkey() ),
411 __METHOD__,
412 $options
413 );
414 if( $page ) {
415 $makepage = false;
416 # Page already exists. Import the history, and if necessary
417 # we'll update the latest revision field in the record.
418 $newid = 0;
419 $pageId = $page->page_id;
420 $previousRevId = $page->page_latest;
421 # Get the time span of this page
422 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
423 array( 'rev_id' => $previousRevId ),
424 __METHOD__ );
425 if( $previousTimestamp === false ) {
426 wfDebug( __METHOD__.": existing page refers to a page_latest that does not exist\n" );
427 return 0;
428 }
429 } else {
430 # Have to create a new article...
431 $makepage = true;
432 $previousRevId = 0;
433 $previousTimestamp = 0;
434 }
435
436 if( $restoreAll ) {
437 $oldones = '1 = 1'; # All revisions...
438 } else {
439 $oldts = implode( ',',
440 array_map( array( &$dbw, 'addQuotes' ),
441 array_map( array( &$dbw, 'timestamp' ),
442 $timestamps ) ) );
443
444 $oldones = "ar_timestamp IN ( {$oldts} )";
445 }
446
447 /**
448 * Select each archived revision...
449 */
450 $result = $dbw->select( 'archive',
451 /* fields */ array(
452 'ar_rev_id',
453 'ar_text',
454 'ar_comment',
455 'ar_user',
456 'ar_user_text',
457 'ar_timestamp',
458 'ar_minor_edit',
459 'ar_flags',
460 'ar_text_id',
461 'ar_deleted',
462 'ar_page_id',
463 'ar_len' ),
464 /* WHERE */ array(
465 'ar_namespace' => $this->title->getNamespace(),
466 'ar_title' => $this->title->getDBkey(),
467 $oldones ),
468 __METHOD__,
469 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
470 );
471 $ret = $dbw->resultObject( $result );
472 $rev_count = $dbw->numRows( $result );
473 if( !$rev_count ) {
474 wfDebug( __METHOD__ . ": no revisions to restore\n" );
475 return false; // ???
476 }
477
478 $ret->seek( $rev_count - 1 ); // move to last
479 $row = $ret->fetchObject(); // get newest archived rev
480 $ret->seek( 0 ); // move back
481
482 if( $makepage ) {
483 // Check the state of the newest to-be version...
484 if( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
485 return false; // we can't leave the current revision like this!
486 }
487 // Safe to insert now...
488 $newid = $article->insertOn( $dbw );
489 $pageId = $newid;
490 } else {
491 // Check if a deleted revision will become the current revision...
492 if( $row->ar_timestamp > $previousTimestamp ) {
493 // Check the state of the newest to-be version...
494 if( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
495 return false; // we can't leave the current revision like this!
496 }
497 }
498 }
499
500 $revision = null;
501 $restored = 0;
502
503 foreach ( $ret as $row ) {
504 // Check for key dupes due to shitty archive integrity.
505 if( $row->ar_rev_id ) {
506 $exists = $dbw->selectField( 'revision', '1',
507 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
508 if( $exists ) {
509 continue; // don't throw DB errors
510 }
511 }
512 // Insert one revision at a time...maintaining deletion status
513 // unless we are specifically removing all restrictions...
514 $revision = Revision::newFromArchiveRow( $row,
515 array(
516 'page' => $pageId,
517 'deleted' => $unsuppress ? 0 : $row->ar_deleted
518 ) );
519
520 $revision->insertOn( $dbw );
521 $restored++;
522
523 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
524 }
525 # Now that it's safely stored, take it out of the archive
526 $dbw->delete( 'archive',
527 /* WHERE */ array(
528 'ar_namespace' => $this->title->getNamespace(),
529 'ar_title' => $this->title->getDBkey(),
530 $oldones ),
531 __METHOD__ );
532
533 // Was anything restored at all?
534 if ( $restored == 0 ) {
535 return 0;
536 }
537
538 $created = (bool)$newid;
539
540 // Attach the latest revision to the page...
541 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
542 if ( $created || $wasnew ) {
543 // Update site stats, link tables, etc
544 $user = User::newFromName( $revision->getRawUserText(), false );
545 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
546 }
547
548 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment ) );
549
550 if( $this->title->getNamespace() == NS_FILE ) {
551 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
552 $update->doUpdate();
553 }
554
555 return $restored;
556 }
557
558 /**
559 * @return Status
560 */
561 function getFileStatus() { return $this->fileStatus; }
562 }
563
564 /**
565 * Special page allowing users with the appropriate permissions to view
566 * and restore deleted content.
567 *
568 * @ingroup SpecialPage
569 */
570 class SpecialUndelete extends SpecialPage {
571 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
572 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
573
574 /**
575 * @var Title
576 */
577 var $mTargetObj;
578
579 function __construct() {
580 parent::__construct( 'Undelete', 'deletedhistory' );
581 }
582
583 function loadRequest() {
584 $request = $this->getRequest();
585 $user = $this->getUser();
586
587 $this->mAction = $request->getVal( 'action' );
588 $this->mTarget = $request->getVal( 'target' );
589 $this->mSearchPrefix = $request->getText( 'prefix' );
590 $time = $request->getVal( 'timestamp' );
591 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
592 $this->mFilename = $request->getVal( 'file' );
593
594 $posted = $request->wasPosted() &&
595 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
596 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
597 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
598 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
599 $this->mDiff = $request->getCheck( 'diff' );
600 $this->mComment = $request->getText( 'wpComment' );
601 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
602 $this->mToken = $request->getVal( 'token' );
603
604 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
605 $this->mAllowed = true; // user can restore
606 $this->mCanView = true; // user can view content
607 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
608 $this->mAllowed = false; // user cannot restore
609 $this->mCanView = true; // user can view content
610 } else { // user can only view the list of revisions
611 $this->mAllowed = false;
612 $this->mCanView = false;
613 $this->mTimestamp = '';
614 $this->mRestore = false;
615 }
616
617 if( $this->mRestore || $this->mInvert ) {
618 $timestamps = array();
619 $this->mFileVersions = array();
620 foreach( $request->getValues() as $key => $val ) {
621 $matches = array();
622 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
623 array_push( $timestamps, $matches[1] );
624 }
625
626 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
627 $this->mFileVersions[] = intval( $matches[1] );
628 }
629 }
630 rsort( $timestamps );
631 $this->mTargetTimestamp = $timestamps;
632 }
633 }
634
635 function execute( $par ) {
636 $this->setHeaders();
637
638 $user = $this->getUser();
639 if ( !$this->userCanExecute( $user ) ) {
640 $this->displayRestrictionError();
641 return;
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( $user->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, $user ) ) {
691 if( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
692 $out->permissionRequired( 'suppressrevision' );
693 } else {
694 $out->permissionRequired( 'deletedtext' );
695 }
696 return false;
697 } elseif ( !$user->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 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
778 return 0;
779 }
780
781 $archive = new PageArchive( $this->mTargetObj );
782 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) );
783 $rev = $archive->getRevision( $timestamp );
784
785 $out = $this->getOutput();
786 $user = $this->getUser();
787
788 if( !$rev ) {
789 $out->addWikiMsg( 'undeleterevision-missing' );
790 return;
791 }
792
793 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
794 if( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
795 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
796 return;
797 } else {
798 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
799 $out->addHTML( '<br />' );
800 // and we are allowed to see...
801 }
802 }
803
804 $out->setPageTitle( wfMsg( 'undeletepage' ) );
805
806 if( $this->mDiff ) {
807 $previousRev = $archive->getPreviousRevision( $timestamp );
808 if( $previousRev ) {
809 $this->showDiff( $previousRev, $rev );
810 if( $user->getOption( 'diffonly' ) ) {
811 return;
812 } else {
813 $out->addHTML( '<hr />' );
814 }
815 } else {
816 $out->addWikiMsg( 'undelete-nodiff' );
817 }
818 }
819
820 $link = Linker::linkKnown(
821 $this->getTitle( $this->mTargetObj->getPrefixedDBkey() ),
822 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
823 );
824
825 // date and time are separate parameters to facilitate localisation.
826 // $time is kept for backward compat reasons.
827 $time = $this->getLang()->timeAndDate( $timestamp, true );
828 $d = $this->getLang()->date( $timestamp, true );
829 $t = $this->getLang()->time( $timestamp, true );
830 $userLink = Linker::revUserTools( $rev );
831
832 if( $this->mPreview ) {
833 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
834 } else {
835 $openDiv = '<div id="mw-undelete-revision">';
836 }
837 $out->addHTML( $openDiv );
838
839 // Revision delete links
840 if ( !$this->mDiff ) {
841 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
842 if ( $revdel ) {
843 $out->addHTML( "$revdel " );
844 }
845 }
846
847 $out->addHTML( wfMessage( 'undelete-revision' )->rawParams( $link )->params(
848 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
849 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
850
851 if( $this->mPreview ) {
852 // Hide [edit]s
853 $popts = $out->parserOptions();
854 $popts->setEditSection( false );
855 $out->parserOptions( $popts );
856 $out->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER, $user ), $this->mTargetObj, true );
857 }
858
859 $out->addHTML(
860 Xml::element( 'textarea', array(
861 'readonly' => 'readonly',
862 'cols' => intval( $user->getOption( 'cols' ) ),
863 'rows' => intval( $user->getOption( 'rows' ) ) ),
864 $rev->getText( Revision::FOR_THIS_USER, $user ) . "\n" ) .
865 Xml::openElement( 'div' ) .
866 Xml::openElement( 'form', array(
867 'method' => 'post',
868 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
869 Xml::element( 'input', array(
870 'type' => 'hidden',
871 'name' => 'target',
872 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
873 Xml::element( 'input', array(
874 'type' => 'hidden',
875 'name' => 'timestamp',
876 'value' => $timestamp ) ) .
877 Xml::element( 'input', array(
878 'type' => 'hidden',
879 'name' => 'wpEditToken',
880 'value' => $user->editToken() ) ) .
881 Xml::element( 'input', array(
882 'type' => 'submit',
883 'name' => 'preview',
884 'value' => wfMsg( 'showpreview' ) ) ) .
885 Xml::element( 'input', array(
886 'name' => 'diff',
887 'type' => 'submit',
888 'value' => wfMsg( 'showdiff' ) ) ) .
889 Xml::closeElement( 'form' ) .
890 Xml::closeElement( 'div' ) );
891 }
892
893 /**
894 * Build a diff display between this and the previous either deleted
895 * or non-deleted edit.
896 *
897 * @param $previousRev Revision
898 * @param $currentRev Revision
899 * @return String: HTML
900 */
901 function showDiff( $previousRev, $currentRev ) {
902 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
903 $diffEngine->showDiffStyle();
904 $this->getOutput()->addHTML(
905 "<div>" .
906 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
907 "<col class='diff-marker' />" .
908 "<col class='diff-content' />" .
909 "<col class='diff-marker' />" .
910 "<col class='diff-content' />" .
911 "<tr>" .
912 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
913 $this->diffHeader( $previousRev, 'o' ) .
914 "</td>\n" .
915 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
916 $this->diffHeader( $currentRev, 'n' ) .
917 "</td>\n" .
918 "</tr>" .
919 $diffEngine->generateDiffBody(
920 $previousRev->getText(), $currentRev->getText() ) .
921 "</table>" .
922 "</div>\n"
923 );
924 }
925
926 /**
927 * @param $rev Revision
928 * @param $prefix
929 * @return string
930 */
931 private function diffHeader( $rev, $prefix ) {
932 $isDeleted = !( $rev->getId() && $rev->getTitle() );
933 if( $isDeleted ) {
934 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
935 $targetPage = $this->getTitle();
936 $targetQuery = array(
937 'target' => $this->mTargetObj->getPrefixedText(),
938 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
939 );
940 } else {
941 /// @todo FIXME: getId() may return non-zero for deleted revs...
942 $targetPage = $rev->getTitle();
943 $targetQuery = array( 'oldid' => $rev->getId() );
944 }
945 // Add show/hide deletion links if available
946 $rdel = Linker::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj );
947 if ( $rdel ) $rdel = " $rdel";
948 return
949 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
950 Linker::link(
951 $targetPage,
952 wfMsgExt(
953 'revisionasof',
954 array( 'escape' ),
955 $this->getLang()->timeanddate( $rev->getTimestamp(), true ),
956 $this->getLang()->date( $rev->getTimestamp(), true ),
957 $this->getLang()->time( $rev->getTimestamp(), true )
958 ),
959 array(),
960 $targetQuery
961 ) .
962 '</strong></div>' .
963 '<div id="mw-diff-'.$prefix.'title2">' .
964 Linker::revUserTools( $rev ) . '<br />' .
965 '</div>' .
966 '<div id="mw-diff-'.$prefix.'title3">' .
967 Linker::revComment( $rev ) . $rdel . '<br />' .
968 '</div>';
969 }
970
971 /**
972 * Show a form confirming whether a tokenless user really wants to see a file
973 */
974 private function showFileConfirmationForm( $key ) {
975 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
976 $this->getOutput()->addWikiMsg( 'undelete-show-file-confirm',
977 $this->mTargetObj->getText(),
978 $this->getLang()->date( $file->getTimestamp() ),
979 $this->getLang()->time( $file->getTimestamp() ) );
980 $this->getOutput()->addHTML(
981 Xml::openElement( 'form', array(
982 'method' => 'POST',
983 'action' => $this->getTitle()->getLocalURL(
984 'target=' . urlencode( $this->mTarget ) .
985 '&file=' . urlencode( $key ) .
986 '&token=' . urlencode( $this->getUser()->editToken( $key ) ) )
987 )
988 ) .
989 Xml::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
990 '</form>'
991 );
992 }
993
994 /**
995 * Show a deleted file version requested by the visitor.
996 */
997 private function showFile( $key ) {
998 $this->getOutput()->disable();
999
1000 # We mustn't allow the output to be Squid cached, otherwise
1001 # if an admin previews a deleted image, and it's cached, then
1002 # a user without appropriate permissions can toddle off and
1003 # nab the image, and Squid will serve it
1004 $response = $this->getRequest()->response();
1005 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1006 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1007 $response->header( 'Pragma: no-cache' );
1008
1009 $repo = RepoGroup::singleton()->getLocalRepo();
1010 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1011 StreamFile::stream( $path );
1012 }
1013
1014 private function showHistory() {
1015 $out = $this->getOutput();
1016 if( $this->mAllowed ) {
1017 $out->addModules( 'mediawiki.special.undelete' );
1018 $out->setPageTitle( wfMsg( 'undeletepage' ) );
1019 } else {
1020 $out->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1021 }
1022 $out->wrapWikiMsg(
1023 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1024 array( 'undeletepagetitle', $this->mTargetObj->getPrefixedText() )
1025 );
1026
1027 $archive = new PageArchive( $this->mTargetObj );
1028 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1029 /*
1030 $text = $archive->getLastRevisionText();
1031 if( is_null( $text ) ) {
1032 $out->addWikiMsg( 'nohistory' );
1033 return;
1034 }
1035 */
1036 $out->addHTML( '<div class="mw-undelete-history">' );
1037 if ( $this->mAllowed ) {
1038 $out->addWikiMsg( 'undeletehistory' );
1039 $out->addWikiMsg( 'undeleterevdel' );
1040 } else {
1041 $out->addWikiMsg( 'undeletehistorynoadmin' );
1042 }
1043 $out->addHTML( '</div>' );
1044
1045 # List all stored revisions
1046 $revisions = $archive->listRevisions();
1047 $files = $archive->listFiles();
1048
1049 $haveRevisions = $revisions && $revisions->numRows() > 0;
1050 $haveFiles = $files && $files->numRows() > 0;
1051
1052 # Batch existence check on user and talk pages
1053 if( $haveRevisions ) {
1054 $batch = new LinkBatch();
1055 foreach ( $revisions as $row ) {
1056 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1057 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1058 }
1059 $batch->execute();
1060 $revisions->seek( 0 );
1061 }
1062 if( $haveFiles ) {
1063 $batch = new LinkBatch();
1064 foreach ( $files as $row ) {
1065 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1066 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1067 }
1068 $batch->execute();
1069 $files->seek( 0 );
1070 }
1071
1072 if ( $this->mAllowed ) {
1073 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1074 # Start the form here
1075 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1076 $out->addHTML( $top );
1077 }
1078
1079 # Show relevant lines from the deletion log:
1080 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) . "\n" );
1081 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1082 # Show relevant lines from the suppression log:
1083 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1084 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'suppress' ) ) . "\n" );
1085 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1086 }
1087
1088 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1089 # Format the user-visible controls (comment field, submission button)
1090 # in a nice little table
1091 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1092 $unsuppressBox =
1093 "<tr>
1094 <td>&#160;</td>
1095 <td class='mw-input'>" .
1096 Xml::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1097 'mw-undelete-unsuppress', $this->mUnsuppress ).
1098 "</td>
1099 </tr>";
1100 } else {
1101 $unsuppressBox = '';
1102 }
1103 $table =
1104 Xml::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1105 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1106 "<tr>
1107 <td colspan='2' class='mw-undelete-extrahelp'>" .
1108 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1109 "</td>
1110 </tr>
1111 <tr>
1112 <td class='mw-label'>" .
1113 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1114 "</td>
1115 <td class='mw-input'>" .
1116 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
1117 "</td>
1118 </tr>
1119 <tr>
1120 <td>&#160;</td>
1121 <td class='mw-submit'>" .
1122 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1123 Xml::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1124 "</td>
1125 </tr>" .
1126 $unsuppressBox .
1127 Xml::closeElement( 'table' ) .
1128 Xml::closeElement( 'fieldset' );
1129
1130 $out->addHTML( $table );
1131 }
1132
1133 $out->addHTML( Xml::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1134
1135 if( $haveRevisions ) {
1136 # The page's stored (deleted) history:
1137 $out->addHTML( '<ul>' );
1138 $remaining = $revisions->numRows();
1139 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1140
1141 foreach ( $revisions as $row ) {
1142 $remaining--;
1143 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1144 }
1145 $revisions->free();
1146 $out->addHTML( '</ul>' );
1147 } else {
1148 $out->addWikiMsg( 'nohistory' );
1149 }
1150
1151 if( $haveFiles ) {
1152 $out->addHTML( Xml::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1153 $out->addHTML( '<ul>' );
1154 foreach ( $files as $row ) {
1155 $out->addHTML( $this->formatFileRow( $row ) );
1156 }
1157 $files->free();
1158 $out->addHTML( '</ul>' );
1159 }
1160
1161 if ( $this->mAllowed ) {
1162 # Slip in the hidden controls here
1163 $misc = Html::hidden( 'target', $this->mTarget );
1164 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->editToken() );
1165 $misc .= Xml::closeElement( 'form' );
1166 $out->addHTML( $misc );
1167 }
1168
1169 return true;
1170 }
1171
1172 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1173 $rev = Revision::newFromArchiveRow( $row,
1174 array( 'page' => $this->mTargetObj->getArticleId() ) );
1175 $stxt = '';
1176 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1177 // Build checkboxen...
1178 if( $this->mAllowed ) {
1179 if( $this->mInvert ) {
1180 if( in_array( $ts, $this->mTargetTimestamp ) ) {
1181 $checkBox = Xml::check( "ts$ts" );
1182 } else {
1183 $checkBox = Xml::check( "ts$ts", true );
1184 }
1185 } else {
1186 $checkBox = Xml::check( "ts$ts" );
1187 }
1188 } else {
1189 $checkBox = '';
1190 }
1191 // Build page & diff links...
1192 if( $this->mCanView ) {
1193 $titleObj = $this->getTitle();
1194 # Last link
1195 if( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1196 $pageLink = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1197 $last = wfMsgHtml( 'diff' );
1198 } elseif( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1199 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1200 $last = Linker::linkKnown(
1201 $titleObj,
1202 wfMsgHtml( 'diff' ),
1203 array(),
1204 array(
1205 'target' => $this->mTargetObj->getPrefixedText(),
1206 'timestamp' => $ts,
1207 'diff' => 'prev'
1208 )
1209 );
1210 } else {
1211 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1212 $last = wfMsgHtml( 'diff' );
1213 }
1214 } else {
1215 $pageLink = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1216 $last = wfMsgHtml( 'diff' );
1217 }
1218 // User links
1219 $userLink = Linker::revUserTools( $rev );
1220 // Revision text size
1221 $size = $row->ar_len;
1222 if( !is_null( $size ) ) {
1223 $stxt = Linker::formatRevisionSize( $size );
1224 }
1225 // Edit summary
1226 $comment = Linker::revComment( $rev );
1227 // Revision delete links
1228 $revdlink = Linker::getRevDeleteLink( $this->getUser(), $rev, $this->mTargetObj );
1229 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1230 }
1231
1232 private function formatFileRow( $row ) {
1233 $file = ArchivedFile::newFromRow( $row );
1234
1235 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1236 if( $this->mAllowed && $row->fa_storage_key ) {
1237 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1238 $key = urlencode( $row->fa_storage_key );
1239 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1240 } else {
1241 $checkBox = '';
1242 $pageLink = $this->getLang()->timeanddate( $ts, true );
1243 }
1244 $userLink = $this->getFileUser( $file );
1245 $data =
1246 wfMsg( 'widthheight',
1247 $this->getLang()->formatNum( $row->fa_width ),
1248 $this->getLang()->formatNum( $row->fa_height ) ) .
1249 ' (' .
1250 wfMsg( 'nbytes', $this->getLang()->formatNum( $row->fa_size ) ) .
1251 ')';
1252 $data = htmlspecialchars( $data );
1253 $comment = $this->getFileComment( $file );
1254
1255 // Add show/hide deletion links if available
1256 $user = $this->getUser();
1257 $canHide = $user->isAllowed( 'deleterevision' );
1258 if( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1259 if( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1260 $revdlink = Linker::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1261 } else {
1262 $query = array(
1263 'type' => 'filearchive',
1264 'target' => $this->mTargetObj->getPrefixedDBkey(),
1265 'ids' => $row->fa_id
1266 );
1267 $revdlink = Linker::revDeleteLink( $query,
1268 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1269 }
1270 } else {
1271 $revdlink = '';
1272 }
1273
1274 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1275 }
1276
1277 /**
1278 * Fetch revision text link if it's available to all users
1279 *
1280 * @param $rev Revision
1281 * @return string
1282 */
1283 function getPageLink( $rev, $titleObj, $ts ) {
1284 $time = htmlspecialchars( $this->getLang()->timeanddate( $ts, true ) );
1285
1286 if( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1287 return '<span class="history-deleted">' . $time . '</span>';
1288 } else {
1289 $link = Linker::linkKnown(
1290 $titleObj,
1291 $time,
1292 array(),
1293 array(
1294 'target' => $this->mTargetObj->getPrefixedText(),
1295 'timestamp' => $ts
1296 )
1297 );
1298 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1299 $link = '<span class="history-deleted">' . $link . '</span>';
1300 }
1301 return $link;
1302 }
1303 }
1304
1305 /**
1306 * Fetch image view link if it's available to all users
1307 *
1308 * @param $file File
1309 * @return String: HTML fragment
1310 */
1311 function getFileLink( $file, $titleObj, $ts, $key ) {
1312 if( !$file->userCan( File::DELETED_FILE, $this->getUser() ) ) {
1313 return '<span class="history-deleted">' . $this->getLang()->timeanddate( $ts, true ) . '</span>';
1314 } else {
1315 $link = Linker::linkKnown(
1316 $titleObj,
1317 $this->getLang()->timeanddate( $ts, true ),
1318 array(),
1319 array(
1320 'target' => $this->mTargetObj->getPrefixedText(),
1321 'file' => $key,
1322 'token' => $this->getUser()->editToken( $key )
1323 )
1324 );
1325 if( $file->isDeleted( File::DELETED_FILE ) ) {
1326 $link = '<span class="history-deleted">' . $link . '</span>';
1327 }
1328 return $link;
1329 }
1330 }
1331
1332 /**
1333 * Fetch file's user id if it's available to this user
1334 *
1335 * @param $file File
1336 * @return String: HTML fragment
1337 */
1338 function getFileUser( $file ) {
1339 if( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1340 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1341 } else {
1342 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1343 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1344 if( $file->isDeleted( File::DELETED_USER ) ) {
1345 $link = '<span class="history-deleted">' . $link . '</span>';
1346 }
1347 return $link;
1348 }
1349 }
1350
1351 /**
1352 * Fetch file upload comment if it's available to this user
1353 *
1354 * @param $file File
1355 * @return String: HTML fragment
1356 */
1357 function getFileComment( $file ) {
1358 if( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1359 return '<span class="history-deleted"><span class="comment">' .
1360 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1361 } else {
1362 $link = Linker::commentBlock( $file->getRawDescription() );
1363 if( $file->isDeleted( File::DELETED_COMMENT ) ) {
1364 $link = '<span class="history-deleted">' . $link . '</span>';
1365 }
1366 return $link;
1367 }
1368 }
1369
1370 function undelete() {
1371 if ( wfReadOnly() ) {
1372 throw new ReadOnlyError;
1373 }
1374
1375 if( !is_null( $this->mTargetObj ) ) {
1376 $archive = new PageArchive( $this->mTargetObj );
1377 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1378 $ok = $archive->undelete(
1379 $this->mTargetTimestamp,
1380 $this->mComment,
1381 $this->mFileVersions,
1382 $this->mUnsuppress );
1383
1384 if( is_array( $ok ) ) {
1385 if ( $ok[1] ) { // Undeleted file count
1386 wfRunHooks( 'FileUndeleteComplete', array(
1387 $this->mTargetObj, $this->mFileVersions,
1388 $this->getUser(), $this->mComment ) );
1389 }
1390
1391 $link = Linker::linkKnown( $this->mTargetObj );
1392 $this->getOutput()->addHTML( wfMessage( 'undeletedpage' )->rawParams( $link )->parse() );
1393 } else {
1394 $this->getOutput()->showFatalError( wfMsg( 'cannotundelete' ) );
1395 $this->getOutput()->addWikiMsg( 'undeleterevdel' );
1396 }
1397
1398 // Show file deletion warnings and errors
1399 $status = $archive->getFileStatus();
1400 if( $status && !$status->isGood() ) {
1401 $this->getOutput()->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1402 }
1403 } else {
1404 $this->getOutput()->showFatalError( wfMsg( 'cannotundelete' ) );
1405 }
1406 return false;
1407 }
1408 }