Merge branch 'master' into Wikidata
[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', 'ar_sha1',
120 'ar_content_format', 'ar_content_model'
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 'ar_sha1',
193 'ar_content_format',
194 'ar_content_model',
195 ),
196 array( 'ar_namespace' => $this->title->getNamespace(),
197 'ar_title' => $this->title->getDBkey(),
198 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
199 __METHOD__ );
200 if( $row ) {
201 return Revision::newFromArchiveRow( $row, array( 'page' => $this->title->getArticleID() ) );
202 } else {
203 return null;
204 }
205 }
206
207 /**
208 * Return the most-previous revision, either live or deleted, against
209 * the deleted revision given by timestamp.
210 *
211 * May produce unexpected results in case of history merges or other
212 * unusual time issues.
213 *
214 * @param $timestamp String
215 * @return Revision or null
216 */
217 function getPreviousRevision( $timestamp ) {
218 $dbr = wfGetDB( DB_SLAVE );
219
220 // Check the previous deleted revision...
221 $row = $dbr->selectRow( 'archive',
222 'ar_timestamp',
223 array( 'ar_namespace' => $this->title->getNamespace(),
224 'ar_title' => $this->title->getDBkey(),
225 'ar_timestamp < ' .
226 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
227 __METHOD__,
228 array(
229 'ORDER BY' => 'ar_timestamp DESC',
230 'LIMIT' => 1 ) );
231 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
232
233 $row = $dbr->selectRow( array( 'page', 'revision' ),
234 array( 'rev_id', 'rev_timestamp' ),
235 array(
236 'page_namespace' => $this->title->getNamespace(),
237 'page_title' => $this->title->getDBkey(),
238 'page_id = rev_page',
239 'rev_timestamp < ' .
240 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
241 __METHOD__,
242 array(
243 'ORDER BY' => 'rev_timestamp DESC',
244 'LIMIT' => 1 ) );
245 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
246 $prevLiveId = $row ? intval( $row->rev_id ) : null;
247
248 if( $prevLive && $prevLive > $prevDeleted ) {
249 // Most prior revision was live
250 return Revision::newFromId( $prevLiveId );
251 } elseif( $prevDeleted ) {
252 // Most prior revision was deleted
253 return $this->getRevision( $prevDeleted );
254 } else {
255 // No prior revision on this page.
256 return null;
257 }
258 }
259
260 /**
261 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
262 *
263 * @param $row Object: database row
264 * @return Revision
265 */
266 function getTextFromRow( $row ) {
267 if( is_null( $row->ar_text_id ) ) {
268 // An old row from MediaWiki 1.4 or previous.
269 // Text is embedded in this row in classic compression format.
270 return Revision::getRevisionText( $row, 'ar_' );
271 } else {
272 // New-style: keyed to the text storage backend.
273 $dbr = wfGetDB( DB_SLAVE );
274 $text = $dbr->selectRow( 'text',
275 array( 'old_text', 'old_flags' ),
276 array( 'old_id' => $row->ar_text_id ),
277 __METHOD__ );
278 return Revision::getRevisionText( $text );
279 }
280 }
281
282 /**
283 * Fetch (and decompress if necessary) the stored text of the most
284 * recently edited deleted revision of the page.
285 *
286 * If there are no archived revisions for the page, returns NULL.
287 *
288 * @return String
289 */
290 function getLastRevisionText() {
291 $dbr = wfGetDB( DB_SLAVE );
292 $row = $dbr->selectRow( 'archive',
293 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
294 array( 'ar_namespace' => $this->title->getNamespace(),
295 'ar_title' => $this->title->getDBkey() ),
296 __METHOD__,
297 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
298 if( $row ) {
299 return $this->getTextFromRow( $row );
300 } else {
301 return null;
302 }
303 }
304
305 /**
306 * Quick check if any archived revisions are present for the page.
307 *
308 * @return Boolean
309 */
310 function isDeleted() {
311 $dbr = wfGetDB( DB_SLAVE );
312 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
313 array( 'ar_namespace' => $this->title->getNamespace(),
314 'ar_title' => $this->title->getDBkey() ) );
315 return ( $n > 0 );
316 }
317
318 /**
319 * Restore the given (or all) text and file revisions for the page.
320 * Once restored, the items will be removed from the archive tables.
321 * The deletion log will be updated with an undeletion notice.
322 *
323 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
324 * @param $comment String
325 * @param $fileVersions Array
326 * @param $unsuppress Boolean
327 *
328 * @return array(number of file revisions restored, number of image revisions restored, log message)
329 * on success, false on failure
330 */
331 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
332 // If both the set of text revisions and file revisions are empty,
333 // restore everything. Otherwise, just restore the requested items.
334 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
335
336 $restoreText = $restoreAll || !empty( $timestamps );
337 $restoreFiles = $restoreAll || !empty( $fileVersions );
338
339 if( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
340 $img = wfLocalFile( $this->title );
341 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
342 if ( !$this->fileStatus->isOk() ) {
343 return false;
344 }
345 $filesRestored = $this->fileStatus->successCount;
346 } else {
347 $filesRestored = 0;
348 }
349
350 if( $restoreText ) {
351 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
352 if( $textRestored === false ) { // It must be one of UNDELETE_*
353 return false;
354 }
355 } else {
356 $textRestored = 0;
357 }
358
359 // Touch the log!
360 global $wgContLang;
361 $log = new LogPage( 'delete' );
362
363 if( $textRestored && $filesRestored ) {
364 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
365 $wgContLang->formatNum( $textRestored ),
366 $wgContLang->formatNum( $filesRestored ) );
367 } elseif( $textRestored ) {
368 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
369 $wgContLang->formatNum( $textRestored ) );
370 } elseif( $filesRestored ) {
371 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
372 $wgContLang->formatNum( $filesRestored ) );
373 } else {
374 wfDebug( "Undelete: nothing undeleted...\n" );
375 return false;
376 }
377
378 if( trim( $comment ) != '' ) {
379 $reason .= wfMsgForContent( 'colon-separator' ) . $comment;
380 }
381 $log->addEntry( 'restore', $this->title, $reason );
382
383 return array( $textRestored, $filesRestored, $reason );
384 }
385
386 /**
387 * This is the meaty bit -- restores archived revisions of the given page
388 * to the cur/old tables. If the page currently exists, all revisions will
389 * be stuffed into old, otherwise the most recent will go into cur.
390 *
391 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
392 * @param $comment String
393 * @param $unsuppress Boolean: remove all ar_deleted/fa_deleted restrictions of seletected revs
394 *
395 * @return Mixed: number of revisions restored or false on failure
396 */
397 private function undeleteRevisions( $timestamps, $unsuppress = false, $comment = '' ) {
398 if ( wfReadOnly() ) {
399 return false;
400 }
401 $restoreAll = empty( $timestamps );
402
403 $dbw = wfGetDB( DB_MASTER );
404
405 # Does this page already exist? We'll have to update it...
406 $article = WikiPage::factory( $this->title );
407 # Load latest data for the current page (bug 31179)
408 $article->loadPageData( 'fromdbmaster' );
409 $oldcountable = $article->isCountable();
410
411 $page = $dbw->selectRow( 'page',
412 array( 'page_id', 'page_latest' ),
413 array( 'page_namespace' => $this->title->getNamespace(),
414 'page_title' => $this->title->getDBkey() ),
415 __METHOD__,
416 array( 'FOR UPDATE' ) // lock page
417 );
418 if( $page ) {
419 $makepage = false;
420 # Page already exists. Import the history, and if necessary
421 # we'll update the latest revision field in the record.
422 $newid = 0;
423 $pageId = $page->page_id;
424 $previousRevId = $page->page_latest;
425 # Get the time span of this page
426 $previousTimestamp = $dbw->selectField( 'revision', 'rev_timestamp',
427 array( 'rev_id' => $previousRevId ),
428 __METHOD__ );
429 if( $previousTimestamp === false ) {
430 wfDebug( __METHOD__.": existing page refers to a page_latest that does not exist\n" );
431 return 0;
432 }
433 } else {
434 # Have to create a new article...
435 $makepage = true;
436 $previousRevId = 0;
437 $previousTimestamp = 0;
438 }
439
440 if( $restoreAll ) {
441 $oldones = '1 = 1'; # All revisions...
442 } else {
443 $oldts = implode( ',',
444 array_map( array( &$dbw, 'addQuotes' ),
445 array_map( array( &$dbw, 'timestamp' ),
446 $timestamps ) ) );
447
448 $oldones = "ar_timestamp IN ( {$oldts} )";
449 }
450
451 /**
452 * Select each archived revision...
453 */
454 $result = $dbw->select( 'archive',
455 /* fields */ array(
456 'ar_rev_id',
457 'ar_text',
458 'ar_comment',
459 'ar_user',
460 'ar_user_text',
461 'ar_timestamp',
462 'ar_minor_edit',
463 'ar_flags',
464 'ar_text_id',
465 'ar_deleted',
466 'ar_page_id',
467 'ar_len',
468 'ar_sha1',
469 'ar_content_format',
470 'ar_content_model' ),
471 /* WHERE */ array(
472 'ar_namespace' => $this->title->getNamespace(),
473 'ar_title' => $this->title->getDBkey(),
474 $oldones ),
475 __METHOD__,
476 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
477 );
478 $ret = $dbw->resultObject( $result );
479 $rev_count = $dbw->numRows( $result );
480 if( !$rev_count ) {
481 wfDebug( __METHOD__ . ": no revisions to restore\n" );
482 return false; // ???
483 }
484
485 $ret->seek( $rev_count - 1 ); // move to last
486 $row = $ret->fetchObject(); // get newest archived rev
487 $ret->seek( 0 ); // move back
488
489 if( $makepage ) {
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 // Safe to insert now...
495 $newid = $article->insertOn( $dbw );
496 $pageId = $newid;
497 } else {
498 // Check if a deleted revision will become the current revision...
499 if( $row->ar_timestamp > $previousTimestamp ) {
500 // Check the state of the newest to-be version...
501 if( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
502 return false; // we can't leave the current revision like this!
503 }
504 }
505 }
506
507 $revision = null;
508 $restored = 0;
509
510 foreach ( $ret as $row ) {
511 // Check for key dupes due to shitty archive integrity.
512 if( $row->ar_rev_id ) {
513 $exists = $dbw->selectField( 'revision', '1',
514 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
515 if( $exists ) {
516 continue; // don't throw DB errors
517 }
518 }
519 // Insert one revision at a time...maintaining deletion status
520 // unless we are specifically removing all restrictions...
521 $revision = Revision::newFromArchiveRow( $row,
522 array(
523 'page' => $pageId,
524 'deleted' => $unsuppress ? 0 : $row->ar_deleted
525 ) );
526
527 $revision->insertOn( $dbw );
528 $restored++;
529
530 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
531 }
532 # Now that it's safely stored, take it out of the archive
533 $dbw->delete( 'archive',
534 /* WHERE */ array(
535 'ar_namespace' => $this->title->getNamespace(),
536 'ar_title' => $this->title->getDBkey(),
537 $oldones ),
538 __METHOD__ );
539
540 // Was anything restored at all?
541 if ( $restored == 0 ) {
542 return 0;
543 }
544
545 $created = (bool)$newid;
546
547 // Attach the latest revision to the page...
548 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
549 if ( $created || $wasnew ) {
550 // Update site stats, link tables, etc
551 $user = User::newFromName( $revision->getRawUserText(), false );
552 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
553 }
554
555 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment ) );
556
557 if( $this->title->getNamespace() == NS_FILE ) {
558 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
559 $update->doUpdate();
560 }
561
562 return $restored;
563 }
564
565 /**
566 * @return Status
567 */
568 function getFileStatus() { return $this->fileStatus; }
569 }
570
571 /**
572 * Special page allowing users with the appropriate permissions to view
573 * and restore deleted content.
574 *
575 * @ingroup SpecialPage
576 */
577 class SpecialUndelete extends SpecialPage {
578 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
579 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
580
581 /**
582 * @var Title
583 */
584 var $mTargetObj;
585
586 function __construct() {
587 parent::__construct( 'Undelete', 'deletedhistory' );
588 }
589
590 function loadRequest( $par ) {
591 $request = $this->getRequest();
592 $user = $this->getUser();
593
594 $this->mAction = $request->getVal( 'action' );
595 if ( $par !== null && $par !== '' ) {
596 $this->mTarget = $par;
597 } else {
598 $this->mTarget = $request->getVal( 'target' );
599 }
600 $this->mTargetObj = null;
601 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
602 $this->mTargetObj = Title::newFromURL( $this->mTarget );
603 }
604 $this->mSearchPrefix = $request->getText( 'prefix' );
605 $time = $request->getVal( 'timestamp' );
606 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
607 $this->mFilename = $request->getVal( 'file' );
608
609 $posted = $request->wasPosted() &&
610 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
611 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
612 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
613 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
614 $this->mDiff = $request->getCheck( 'diff' );
615 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
616 $this->mComment = $request->getText( 'wpComment' );
617 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
618 $this->mToken = $request->getVal( 'token' );
619
620 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
621 $this->mAllowed = true; // user can restore
622 $this->mCanView = true; // user can view content
623 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
624 $this->mAllowed = false; // user cannot restore
625 $this->mCanView = true; // user can view content
626 $this->mRestore = false;
627 } else { // user can only view the list of revisions
628 $this->mAllowed = false;
629 $this->mCanView = false;
630 $this->mTimestamp = '';
631 $this->mRestore = false;
632 }
633
634 if( $this->mRestore || $this->mInvert ) {
635 $timestamps = array();
636 $this->mFileVersions = array();
637 foreach( $request->getValues() as $key => $val ) {
638 $matches = array();
639 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
640 array_push( $timestamps, $matches[1] );
641 }
642
643 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
644 $this->mFileVersions[] = intval( $matches[1] );
645 }
646 }
647 rsort( $timestamps );
648 $this->mTargetTimestamp = $timestamps;
649 }
650 }
651
652 function execute( $par ) {
653 $this->checkPermissions();
654 $user = $this->getUser();
655
656 $this->setHeaders();
657 $this->outputHeader();
658
659 $this->loadRequest( $par );
660
661 $out = $this->getOutput();
662
663 if ( is_null( $this->mTargetObj ) ) {
664 $out->addWikiMsg( 'undelete-header' );
665
666 # Not all users can just browse every deleted page from the list
667 if ( $user->isAllowed( 'browsearchive' ) ) {
668 $this->showSearchForm();
669 }
670 return;
671 }
672
673 if ( $this->mAllowed ) {
674 $out->setPageTitle( $this->msg( 'undeletepage' ) );
675 } else {
676 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
677 }
678
679 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
680
681 if ( $this->mTimestamp !== '' ) {
682 $this->showRevision( $this->mTimestamp );
683 } elseif ( $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 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
689 if( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
690 throw new PermissionsError( 'suppressrevision' );
691 } else {
692 throw new PermissionsError( 'deletedtext' );
693 }
694 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
695 $this->showFileConfirmationForm( $this->mFilename );
696 } else {
697 $this->showFile( $this->mFilename );
698 }
699 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
700 $this->undelete();
701 } else {
702 $this->showHistory();
703 }
704 }
705
706 function showSearchForm() {
707 global $wgScript;
708
709 $out = $this->getOutput();
710 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
711 $out->addHTML(
712 Xml::openElement( 'form', array(
713 'method' => 'get',
714 'action' => $wgScript ) ) .
715 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
716 Html::hidden( 'title',
717 $this->getTitle()->getPrefixedDbKey() ) .
718 Xml::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
719 'prefix', 'prefix', 20,
720 $this->mSearchPrefix ) . ' ' .
721 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
722 Xml::closeElement( 'fieldset' ) .
723 Xml::closeElement( 'form' )
724 );
725
726 # List undeletable articles
727 if( $this->mSearchPrefix ) {
728 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
729 $this->showList( $result );
730 }
731 }
732
733 /**
734 * Generic list of deleted pages
735 *
736 * @param $result ResultWrapper
737 * @return bool
738 */
739 private function showList( $result ) {
740 $out = $this->getOutput();
741
742 if( $result->numRows() == 0 ) {
743 $out->addWikiMsg( 'undelete-no-results' );
744 return;
745 }
746
747 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
748
749 $undelete = $this->getTitle();
750 $out->addHTML( "<ul>\n" );
751 foreach ( $result as $row ) {
752 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
753 if ( $title !== null ) {
754 $item = Linker::linkKnown(
755 $undelete,
756 htmlspecialchars( $title->getPrefixedText() ),
757 array(),
758 array( 'target' => $title->getPrefixedText() )
759 );
760 } else {
761 // The title is no longer valid, show as text
762 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
763 $item = htmlspecialchars( $title->getPrefixedText() );
764 }
765 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
766 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
767 }
768 $result->free();
769 $out->addHTML( "</ul>\n" );
770
771 return true;
772 }
773
774 private function showRevision( $timestamp ) {
775 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
776 return 0;
777 }
778
779 $archive = new PageArchive( $this->mTargetObj );
780 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) );
781 $rev = $archive->getRevision( $timestamp );
782
783 $out = $this->getOutput();
784 $user = $this->getUser();
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, $user ) ) {
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 if( $this->mDiff ) {
803 $previousRev = $archive->getPreviousRevision( $timestamp );
804 if( $previousRev ) {
805 $this->showDiff( $previousRev, $rev );
806 if( $this->mDiffOnly ) {
807 return;
808 } else {
809 $out->addHTML( '<hr />' );
810 }
811 } else {
812 $out->addWikiMsg( 'undelete-nodiff' );
813 }
814 }
815
816 $link = Linker::linkKnown(
817 $this->getTitle( $this->mTargetObj->getPrefixedDBkey() ),
818 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
819 );
820
821 $lang = $this->getLanguage();
822
823 // date and time are separate parameters to facilitate localisation.
824 // $time is kept for backward compat reasons.
825 $time = $lang->userTimeAndDate( $timestamp, $user );
826 $d = $lang->userDate( $timestamp, $user );
827 $t = $lang->userTime( $timestamp, $user );
828 $userLink = 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( $user, $rev, $this->mTargetObj );
840 if ( $revdel ) {
841 $out->addHTML( "$revdel " );
842 }
843 }
844
845 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
846 $time )->rawParams( $userLink )->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, $user ), $this->mTargetObj, true );
855 }
856
857 $out->addHTML(
858 Xml::element( 'textarea', array(
859 'readonly' => 'readonly',
860 'cols' => intval( $user->getOption( 'cols' ) ),
861 'rows' => intval( $user->getOption( 'rows' ) ) ),
862 $rev->getText( Revision::FOR_THIS_USER, $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' => $user->getEditToken() ) ) .
879 Xml::element( 'input', array(
880 'type' => 'submit',
881 'name' => 'preview',
882 'value' => $this->msg( 'showpreview' )->text() ) ) .
883 Xml::element( 'input', array(
884 'name' => 'diff',
885 'type' => 'submit',
886 'value' => $this->msg( 'showdiff' )->text() ) ) .
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 $contentHandler = ContentHandler::getForTitle( $this->getTitle() );
901 $diffEngine = $contentHandler->getDifferenceEngine( $this->getContext() );
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->generateContentDiffBody(
919 $previousRev->getContent(), $currentRev->getContent() ) .
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 $user = $this->getUser();
946 $lang = $this->getLanguage();
947 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
948 if ( $rdel ) $rdel = " $rdel";
949 return
950 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
951 Linker::link(
952 $targetPage,
953 $this->msg(
954 'revisionasof',
955 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
956 $lang->userDate( $rev->getTimestamp(), $user ),
957 $lang->userTime( $rev->getTimestamp(), $user )
958 )->escaped(),
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 $out = $this->getOutput();
976 $lang = $this->getLanguage();
977 $user = $this->getUser();
978 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
979 $out->addWikiMsg( 'undelete-show-file-confirm',
980 $this->mTargetObj->getText(),
981 $lang->userDate( $file->getTimestamp(), $user ),
982 $lang->userTime( $file->getTimestamp(), $user ) );
983 $out->addHTML(
984 Xml::openElement( 'form', array(
985 'method' => 'POST',
986 'action' => $this->getTitle()->getLocalURL(
987 'target=' . urlencode( $this->mTarget ) .
988 '&file=' . urlencode( $key ) .
989 '&token=' . urlencode( $user->getEditToken( $key ) ) )
990 )
991 ) .
992 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
993 '</form>'
994 );
995 }
996
997 /**
998 * Show a deleted file version requested by the visitor.
999 */
1000 private function showFile( $key ) {
1001 $this->getOutput()->disable();
1002
1003 # We mustn't allow the output to be Squid cached, otherwise
1004 # if an admin previews a deleted image, and it's cached, then
1005 # a user without appropriate permissions can toddle off and
1006 # nab the image, and Squid will serve it
1007 $response = $this->getRequest()->response();
1008 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1009 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1010 $response->header( 'Pragma: no-cache' );
1011
1012 $repo = RepoGroup::singleton()->getLocalRepo();
1013 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1014 $repo->streamFile( $path );
1015 }
1016
1017 private function showHistory() {
1018 $out = $this->getOutput();
1019 if( $this->mAllowed ) {
1020 $out->addModules( 'mediawiki.special.undelete' );
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( $this->msg( 'revdelete-unsuppress' )->text(),
1097 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ).
1098 "</td>
1099 </tr>";
1100 } else {
1101 $unsuppressBox = '';
1102 }
1103 $table =
1104 Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1105 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1106 "<tr>
1107 <td colspan='2' class='mw-undelete-extrahelp'>" .
1108 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1109 "</td>
1110 </tr>
1111 <tr>
1112 <td class='mw-label'>" .
1113 Xml::label( $this->msg( 'undeletecomment' )->text(), '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( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1123 Xml::submitButton( $this->msg( 'undeleteinvert' )->text(), 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, $this->msg( 'history' )->text() ) . "\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, $this->msg( 'filehist' )->text() ) . "\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()->getEditToken() );
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 $user = $this->getUser();
1192 // Build page & diff links...
1193 if( $this->mCanView ) {
1194 $titleObj = $this->getTitle();
1195 # Last link
1196 if( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1197 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1198 $last = $this->msg( 'diff' )->escaped();
1199 } elseif( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1200 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1201 $last = Linker::linkKnown(
1202 $titleObj,
1203 $this->msg( 'diff' )->escaped(),
1204 array(),
1205 array(
1206 'target' => $this->mTargetObj->getPrefixedText(),
1207 'timestamp' => $ts,
1208 'diff' => 'prev'
1209 )
1210 );
1211 } else {
1212 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1213 $last = $this->msg( 'diff' )->escaped();
1214 }
1215 } else {
1216 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1217 $last = $this->msg( 'diff' )->escaped();
1218 }
1219 // User links
1220 $userLink = Linker::revUserTools( $rev );
1221 // Revision text size
1222 $size = $row->ar_len;
1223 if( !is_null( $size ) ) {
1224 $stxt = Linker::formatRevisionSize( $size );
1225 }
1226 // Edit summary
1227 $comment = Linker::revComment( $rev );
1228 // Revision delete links
1229 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1230 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1231 }
1232
1233 private function formatFileRow( $row ) {
1234 $file = ArchivedFile::newFromRow( $row );
1235
1236 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1237 $user = $this->getUser();
1238 if( $this->mAllowed && $row->fa_storage_key ) {
1239 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1240 $key = urlencode( $row->fa_storage_key );
1241 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1242 } else {
1243 $checkBox = '';
1244 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1245 }
1246 $userLink = $this->getFileUser( $file );
1247 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1248 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )->plain();
1249 $data = htmlspecialchars( $data . ' ' . $bytes );
1250 $comment = $this->getFileComment( $file );
1251
1252 // Add show/hide deletion links if available
1253 $canHide = $user->isAllowed( 'deleterevision' );
1254 if( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1255 if( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1256 $revdlink = Linker::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1257 } else {
1258 $query = array(
1259 'type' => 'filearchive',
1260 'target' => $this->mTargetObj->getPrefixedDBkey(),
1261 'ids' => $row->fa_id
1262 );
1263 $revdlink = Linker::revDeleteLink( $query,
1264 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1265 }
1266 } else {
1267 $revdlink = '';
1268 }
1269
1270 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1271 }
1272
1273 /**
1274 * Fetch revision text link if it's available to all users
1275 *
1276 * @param $rev Revision
1277 * @param $titleObj Title
1278 * @param $ts string Timestamp
1279 * @return string
1280 */
1281 function getPageLink( $rev, $titleObj, $ts ) {
1282 $user = $this->getUser();
1283 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1284
1285 if( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1286 return '<span class="history-deleted">' . $time . '</span>';
1287 } else {
1288 $link = Linker::linkKnown(
1289 $titleObj,
1290 htmlspecialchars( $time ),
1291 array(),
1292 array(
1293 'target' => $this->mTargetObj->getPrefixedText(),
1294 'timestamp' => $ts
1295 )
1296 );
1297 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1298 $link = '<span class="history-deleted">' . $link . '</span>';
1299 }
1300 return $link;
1301 }
1302 }
1303
1304 /**
1305 * Fetch image view link if it's available to all users
1306 *
1307 * @param $file File
1308 * @param $titleObj Title
1309 * @param $ts string A timestamp
1310 * @param $key String: a storage key
1311 *
1312 * @return String: HTML fragment
1313 */
1314 function getFileLink( $file, $titleObj, $ts, $key ) {
1315 $user = $this->getUser();
1316 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1317
1318 if( !$file->userCan( File::DELETED_FILE, $user ) ) {
1319 return '<span class="history-deleted">' . $time . '</span>';
1320 } else {
1321 $link = Linker::linkKnown(
1322 $titleObj,
1323 htmlspecialchars( $time ),
1324 array(),
1325 array(
1326 'target' => $this->mTargetObj->getPrefixedText(),
1327 'file' => $key,
1328 'token' => $user->getEditToken( $key )
1329 )
1330 );
1331 if( $file->isDeleted( File::DELETED_FILE ) ) {
1332 $link = '<span class="history-deleted">' . $link . '</span>';
1333 }
1334 return $link;
1335 }
1336 }
1337
1338 /**
1339 * Fetch file's user id if it's available to this user
1340 *
1341 * @param $file File
1342 * @return String: HTML fragment
1343 */
1344 function getFileUser( $file ) {
1345 if( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1346 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1347 } else {
1348 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1349 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1350 if( $file->isDeleted( File::DELETED_USER ) ) {
1351 $link = '<span class="history-deleted">' . $link . '</span>';
1352 }
1353 return $link;
1354 }
1355 }
1356
1357 /**
1358 * Fetch file upload comment if it's available to this user
1359 *
1360 * @param $file File
1361 * @return String: HTML fragment
1362 */
1363 function getFileComment( $file ) {
1364 if( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1365 return '<span class="history-deleted"><span class="comment">' .
1366 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1367 } else {
1368 $link = Linker::commentBlock( $file->getRawDescription() );
1369 if( $file->isDeleted( File::DELETED_COMMENT ) ) {
1370 $link = '<span class="history-deleted">' . $link . '</span>';
1371 }
1372 return $link;
1373 }
1374 }
1375
1376 function undelete() {
1377 global $wgUploadMaintenance;
1378
1379 if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
1380 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1381 }
1382
1383 if ( wfReadOnly() ) {
1384 throw new ReadOnlyError;
1385 }
1386
1387 $out = $this->getOutput();
1388 $archive = new PageArchive( $this->mTargetObj );
1389 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1390 $ok = $archive->undelete(
1391 $this->mTargetTimestamp,
1392 $this->mComment,
1393 $this->mFileVersions,
1394 $this->mUnsuppress );
1395
1396 if( is_array( $ok ) ) {
1397 if ( $ok[1] ) { // Undeleted file count
1398 wfRunHooks( 'FileUndeleteComplete', array(
1399 $this->mTargetObj, $this->mFileVersions,
1400 $this->getUser(), $this->mComment ) );
1401 }
1402
1403 $link = Linker::linkKnown( $this->mTargetObj );
1404 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1405 } else {
1406 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1407 $out->addWikiMsg( 'cannotundelete' );
1408 $out->addWikiMsg( 'undeleterevdel' );
1409 }
1410
1411 // Show file deletion warnings and errors
1412 $status = $archive->getFileStatus();
1413 if( $status && !$status->isGood() ) {
1414 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );
1415 }
1416 }
1417 }