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