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