Merge branch 'master' of ssh://gerrit.wikimedia.org:29418/mediawiki/core 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' => 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 //FIXME: ContentHandler will have to provide some specialized magic to do this
876 $popts = $out->parserOptions();
877 $popts->setEditSection( false );
878 $out->parserOptions( $popts );
879 $out->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER, $user ), $this->mTargetObj, true );
880 }
881
882 $out->addHTML(
883 Xml::element( 'textarea', array(
884 'readonly' => 'readonly',
885 'cols' => intval( $user->getOption( 'cols' ) ),
886 'rows' => intval( $user->getOption( 'rows' ) ) ),
887 $rev->getText( Revision::FOR_THIS_USER, $user ) . "\n" ) . //FIXME: ContentHandler will have to provide some specialized magic to do this
888 Xml::openElement( 'div' ) .
889 Xml::openElement( 'form', array(
890 'method' => 'post',
891 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
892 Xml::element( 'input', array(
893 'type' => 'hidden',
894 'name' => 'target',
895 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
896 Xml::element( 'input', array(
897 'type' => 'hidden',
898 'name' => 'timestamp',
899 'value' => $timestamp ) ) .
900 Xml::element( 'input', array(
901 'type' => 'hidden',
902 'name' => 'wpEditToken',
903 'value' => $user->getEditToken() ) ) .
904 Xml::element( 'input', array(
905 'type' => 'submit',
906 'name' => 'preview',
907 'value' => $this->msg( 'showpreview' )->text() ) ) .
908 Xml::element( 'input', array(
909 'name' => 'diff',
910 'type' => 'submit',
911 'value' => $this->msg( 'showdiff' )->text() ) ) .
912 Xml::closeElement( 'form' ) .
913 Xml::closeElement( 'div' ) );
914 }
915
916 /**
917 * Build a diff display between this and the previous either deleted
918 * or non-deleted edit.
919 *
920 * @param $previousRev Revision
921 * @param $currentRev Revision
922 * @return String: HTML
923 */
924 function showDiff( $previousRev, $currentRev ) {
925 $contentHandler = ContentHandler::getForTitle( $this->getTitle() );
926 $diffEngine = $contentHandler->createDifferenceEngine( $this->getContext() );
927 $diffEngine->showDiffStyle();
928 $this->getOutput()->addHTML(
929 "<div>" .
930 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
931 "<col class='diff-marker' />" .
932 "<col class='diff-content' />" .
933 "<col class='diff-marker' />" .
934 "<col class='diff-content' />" .
935 "<tr>" .
936 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
937 $this->diffHeader( $previousRev, 'o' ) .
938 "</td>\n" .
939 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
940 $this->diffHeader( $currentRev, 'n' ) .
941 "</td>\n" .
942 "</tr>" .
943 $diffEngine->generateContentDiffBody(
944 $previousRev->getContent(), $currentRev->getContent() ) .
945 "</table>" .
946 "</div>\n"
947 );
948 }
949
950 /**
951 * @param $rev Revision
952 * @param $prefix
953 * @return string
954 */
955 private function diffHeader( $rev, $prefix ) {
956 $isDeleted = !( $rev->getId() && $rev->getTitle() );
957 if( $isDeleted ) {
958 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
959 $targetPage = $this->getTitle();
960 $targetQuery = array(
961 'target' => $this->mTargetObj->getPrefixedText(),
962 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
963 );
964 } else {
965 /// @todo FIXME: getId() may return non-zero for deleted revs...
966 $targetPage = $rev->getTitle();
967 $targetQuery = array( 'oldid' => $rev->getId() );
968 }
969 // Add show/hide deletion links if available
970 $user = $this->getUser();
971 $lang = $this->getLanguage();
972 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
973 if ( $rdel ) $rdel = " $rdel";
974 return
975 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
976 Linker::link(
977 $targetPage,
978 $this->msg(
979 'revisionasof',
980 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
981 $lang->userDate( $rev->getTimestamp(), $user ),
982 $lang->userTime( $rev->getTimestamp(), $user )
983 )->escaped(),
984 array(),
985 $targetQuery
986 ) .
987 '</strong></div>' .
988 '<div id="mw-diff-'.$prefix.'title2">' .
989 Linker::revUserTools( $rev ) . '<br />' .
990 '</div>' .
991 '<div id="mw-diff-'.$prefix.'title3">' .
992 Linker::revComment( $rev ) . $rdel . '<br />' .
993 '</div>';
994 }
995
996 /**
997 * Show a form confirming whether a tokenless user really wants to see a file
998 */
999 private function showFileConfirmationForm( $key ) {
1000 $out = $this->getOutput();
1001 $lang = $this->getLanguage();
1002 $user = $this->getUser();
1003 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1004 $out->addWikiMsg( 'undelete-show-file-confirm',
1005 $this->mTargetObj->getText(),
1006 $lang->userDate( $file->getTimestamp(), $user ),
1007 $lang->userTime( $file->getTimestamp(), $user ) );
1008 $out->addHTML(
1009 Xml::openElement( 'form', array(
1010 'method' => 'POST',
1011 'action' => $this->getTitle()->getLocalURL(
1012 'target=' . urlencode( $this->mTarget ) .
1013 '&file=' . urlencode( $key ) .
1014 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1015 )
1016 ) .
1017 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1018 '</form>'
1019 );
1020 }
1021
1022 /**
1023 * Show a deleted file version requested by the visitor.
1024 */
1025 private function showFile( $key ) {
1026 $this->getOutput()->disable();
1027
1028 # We mustn't allow the output to be Squid cached, otherwise
1029 # if an admin previews a deleted image, and it's cached, then
1030 # a user without appropriate permissions can toddle off and
1031 # nab the image, and Squid will serve it
1032 $response = $this->getRequest()->response();
1033 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1034 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1035 $response->header( 'Pragma: no-cache' );
1036
1037 $repo = RepoGroup::singleton()->getLocalRepo();
1038 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1039 $repo->streamFile( $path );
1040 }
1041
1042 private function showHistory() {
1043 $out = $this->getOutput();
1044 if( $this->mAllowed ) {
1045 $out->addModules( 'mediawiki.special.undelete' );
1046 }
1047 $out->wrapWikiMsg(
1048 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1049 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1050 );
1051
1052 $archive = new PageArchive( $this->mTargetObj );
1053 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1054 /*
1055 $text = $archive->getLastRevisionText();
1056 if( is_null( $text ) ) {
1057 $out->addWikiMsg( 'nohistory' );
1058 return;
1059 }
1060 */
1061 $out->addHTML( '<div class="mw-undelete-history">' );
1062 if ( $this->mAllowed ) {
1063 $out->addWikiMsg( 'undeletehistory' );
1064 $out->addWikiMsg( 'undeleterevdel' );
1065 } else {
1066 $out->addWikiMsg( 'undeletehistorynoadmin' );
1067 }
1068 $out->addHTML( '</div>' );
1069
1070 # List all stored revisions
1071 $revisions = $archive->listRevisions();
1072 $files = $archive->listFiles();
1073
1074 $haveRevisions = $revisions && $revisions->numRows() > 0;
1075 $haveFiles = $files && $files->numRows() > 0;
1076
1077 # Batch existence check on user and talk pages
1078 if( $haveRevisions ) {
1079 $batch = new LinkBatch();
1080 foreach ( $revisions as $row ) {
1081 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1082 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1083 }
1084 $batch->execute();
1085 $revisions->seek( 0 );
1086 }
1087 if( $haveFiles ) {
1088 $batch = new LinkBatch();
1089 foreach ( $files as $row ) {
1090 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1091 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1092 }
1093 $batch->execute();
1094 $files->seek( 0 );
1095 }
1096
1097 if ( $this->mAllowed ) {
1098 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1099 # Start the form here
1100 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1101 $out->addHTML( $top );
1102 }
1103
1104 # Show relevant lines from the deletion log:
1105 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) . "\n" );
1106 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1107 # Show relevant lines from the suppression log:
1108 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1109 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'suppress' ) ) . "\n" );
1110 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1111 }
1112
1113 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1114 # Format the user-visible controls (comment field, submission button)
1115 # in a nice little table
1116 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1117 $unsuppressBox =
1118 "<tr>
1119 <td>&#160;</td>
1120 <td class='mw-input'>" .
1121 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1122 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ).
1123 "</td>
1124 </tr>";
1125 } else {
1126 $unsuppressBox = '';
1127 }
1128 $table =
1129 Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1130 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1131 "<tr>
1132 <td colspan='2' class='mw-undelete-extrahelp'>" .
1133 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1134 "</td>
1135 </tr>
1136 <tr>
1137 <td class='mw-label'>" .
1138 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1139 "</td>
1140 <td class='mw-input'>" .
1141 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
1142 "</td>
1143 </tr>
1144 <tr>
1145 <td>&#160;</td>
1146 <td class='mw-submit'>" .
1147 Xml::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1148 Xml::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1149 "</td>
1150 </tr>" .
1151 $unsuppressBox .
1152 Xml::closeElement( 'table' ) .
1153 Xml::closeElement( 'fieldset' );
1154
1155 $out->addHTML( $table );
1156 }
1157
1158 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1159
1160 if( $haveRevisions ) {
1161 # The page's stored (deleted) history:
1162 $out->addHTML( '<ul>' );
1163 $remaining = $revisions->numRows();
1164 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1165
1166 foreach ( $revisions as $row ) {
1167 $remaining--;
1168 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1169 }
1170 $revisions->free();
1171 $out->addHTML( '</ul>' );
1172 } else {
1173 $out->addWikiMsg( 'nohistory' );
1174 }
1175
1176 if( $haveFiles ) {
1177 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1178 $out->addHTML( '<ul>' );
1179 foreach ( $files as $row ) {
1180 $out->addHTML( $this->formatFileRow( $row ) );
1181 }
1182 $files->free();
1183 $out->addHTML( '</ul>' );
1184 }
1185
1186 if ( $this->mAllowed ) {
1187 # Slip in the hidden controls here
1188 $misc = Html::hidden( 'target', $this->mTarget );
1189 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1190 $misc .= Xml::closeElement( 'form' );
1191 $out->addHTML( $misc );
1192 }
1193
1194 return true;
1195 }
1196
1197 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1198 $rev = Revision::newFromArchiveRow( $row,
1199 array( 'page' => $this->mTargetObj->getArticleID() ) );
1200 $revTextSize = '';
1201 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1202 // Build checkboxen...
1203 if( $this->mAllowed ) {
1204 if( $this->mInvert ) {
1205 if( in_array( $ts, $this->mTargetTimestamp ) ) {
1206 $checkBox = Xml::check( "ts$ts" );
1207 } else {
1208 $checkBox = Xml::check( "ts$ts", true );
1209 }
1210 } else {
1211 $checkBox = Xml::check( "ts$ts" );
1212 }
1213 } else {
1214 $checkBox = '';
1215 }
1216 $user = $this->getUser();
1217 // Build page & diff links...
1218 if( $this->mCanView ) {
1219 $titleObj = $this->getTitle();
1220 # Last link
1221 if( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1222 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1223 $last = $this->msg( 'diff' )->escaped();
1224 } elseif( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1225 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1226 $last = Linker::linkKnown(
1227 $titleObj,
1228 $this->msg( 'diff' )->escaped(),
1229 array(),
1230 array(
1231 'target' => $this->mTargetObj->getPrefixedText(),
1232 'timestamp' => $ts,
1233 'diff' => 'prev'
1234 )
1235 );
1236 } else {
1237 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1238 $last = $this->msg( 'diff' )->escaped();
1239 }
1240 } else {
1241 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1242 $last = $this->msg( 'diff' )->escaped();
1243 }
1244 // User links
1245 $userLink = Linker::revUserTools( $rev );
1246 // Revision text size
1247 $size = $row->ar_len;
1248 if( !is_null( $size ) ) {
1249 $revTextSize = Linker::formatRevisionSize( $size );
1250 }
1251 // Edit summary
1252 $comment = Linker::revComment( $rev );
1253 // Revision delete links
1254 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1255
1256 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1257 return "<li>$revisionRow</li>";
1258 }
1259
1260 private function formatFileRow( $row ) {
1261 $file = ArchivedFile::newFromRow( $row );
1262
1263 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1264 $user = $this->getUser();
1265 if( $this->mAllowed && $row->fa_storage_key ) {
1266 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1267 $key = urlencode( $row->fa_storage_key );
1268 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1269 } else {
1270 $checkBox = '';
1271 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1272 }
1273 $userLink = $this->getFileUser( $file );
1274 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1275 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )->plain();
1276 $data = htmlspecialchars( $data . ' ' . $bytes );
1277 $comment = $this->getFileComment( $file );
1278
1279 // Add show/hide deletion links if available
1280 $canHide = $user->isAllowed( 'deleterevision' );
1281 if( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1282 if( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1283 $revdlink = Linker::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1284 } else {
1285 $query = array(
1286 'type' => 'filearchive',
1287 'target' => $this->mTargetObj->getPrefixedDBkey(),
1288 'ids' => $row->fa_id
1289 );
1290 $revdlink = Linker::revDeleteLink( $query,
1291 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1292 }
1293 } else {
1294 $revdlink = '';
1295 }
1296
1297 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1298 }
1299
1300 /**
1301 * Fetch revision text link if it's available to all users
1302 *
1303 * @param $rev Revision
1304 * @param $titleObj Title
1305 * @param $ts string Timestamp
1306 * @return string
1307 */
1308 function getPageLink( $rev, $titleObj, $ts ) {
1309 $user = $this->getUser();
1310 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1311
1312 if( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1313 return '<span class="history-deleted">' . $time . '</span>';
1314 } else {
1315 $link = Linker::linkKnown(
1316 $titleObj,
1317 htmlspecialchars( $time ),
1318 array(),
1319 array(
1320 'target' => $this->mTargetObj->getPrefixedText(),
1321 'timestamp' => $ts
1322 )
1323 );
1324 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1325 $link = '<span class="history-deleted">' . $link . '</span>';
1326 }
1327 return $link;
1328 }
1329 }
1330
1331 /**
1332 * Fetch image view link if it's available to all users
1333 *
1334 * @param $file File
1335 * @param $titleObj Title
1336 * @param $ts string A timestamp
1337 * @param $key String: a storage key
1338 *
1339 * @return String: HTML fragment
1340 */
1341 function getFileLink( $file, $titleObj, $ts, $key ) {
1342 $user = $this->getUser();
1343 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1344
1345 if( !$file->userCan( File::DELETED_FILE, $user ) ) {
1346 return '<span class="history-deleted">' . $time . '</span>';
1347 } else {
1348 $link = Linker::linkKnown(
1349 $titleObj,
1350 htmlspecialchars( $time ),
1351 array(),
1352 array(
1353 'target' => $this->mTargetObj->getPrefixedText(),
1354 'file' => $key,
1355 'token' => $user->getEditToken( $key )
1356 )
1357 );
1358 if( $file->isDeleted( File::DELETED_FILE ) ) {
1359 $link = '<span class="history-deleted">' . $link . '</span>';
1360 }
1361 return $link;
1362 }
1363 }
1364
1365 /**
1366 * Fetch file's user id if it's available to this user
1367 *
1368 * @param $file File
1369 * @return String: HTML fragment
1370 */
1371 function getFileUser( $file ) {
1372 if( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1373 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1374 } else {
1375 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1376 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1377 if( $file->isDeleted( File::DELETED_USER ) ) {
1378 $link = '<span class="history-deleted">' . $link . '</span>';
1379 }
1380 return $link;
1381 }
1382 }
1383
1384 /**
1385 * Fetch file upload comment if it's available to this user
1386 *
1387 * @param $file File
1388 * @return String: HTML fragment
1389 */
1390 function getFileComment( $file ) {
1391 if( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1392 return '<span class="history-deleted"><span class="comment">' .
1393 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1394 } else {
1395 $link = Linker::commentBlock( $file->getRawDescription() );
1396 if( $file->isDeleted( File::DELETED_COMMENT ) ) {
1397 $link = '<span class="history-deleted">' . $link . '</span>';
1398 }
1399 return $link;
1400 }
1401 }
1402
1403 function undelete() {
1404 global $wgUploadMaintenance;
1405
1406 if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
1407 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1408 }
1409
1410 if ( wfReadOnly() ) {
1411 throw new ReadOnlyError;
1412 }
1413
1414 $out = $this->getOutput();
1415 $archive = new PageArchive( $this->mTargetObj );
1416 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1417 $ok = $archive->undelete(
1418 $this->mTargetTimestamp,
1419 $this->mComment,
1420 $this->mFileVersions,
1421 $this->mUnsuppress );
1422
1423 if( is_array( $ok ) ) {
1424 if ( $ok[1] ) { // Undeleted file count
1425 wfRunHooks( 'FileUndeleteComplete', array(
1426 $this->mTargetObj, $this->mFileVersions,
1427 $this->getUser(), $this->mComment ) );
1428 }
1429
1430 $link = Linker::linkKnown( $this->mTargetObj );
1431 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1432 } else {
1433 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1434 $out->addWikiMsg( 'cannotundelete' );
1435 $out->addWikiMsg( 'undeleterevdel' );
1436 }
1437
1438 // Show file deletion warnings and errors
1439 $status = $archive->getFileStatus();
1440 if( $status && !$status->isGood() ) {
1441 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );
1442 }
1443 }
1444 }