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