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