Merge "Refactor Watchlist code so mobile can be more consistent"
[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 $oldWhere = array(
496 'ar_namespace' => $this->title->getNamespace(),
497 'ar_title' => $this->title->getDBkey(),
498 );
499 if ( !$restoreAll ) {
500 $oldWhere['ar_timestamp'] = array_map( array( &$dbw, 'timestamp' ), $timestamps );
501 }
502
503 $fields = array(
504 'ar_rev_id',
505 'ar_text',
506 'ar_comment',
507 'ar_user',
508 'ar_user_text',
509 'ar_timestamp',
510 'ar_minor_edit',
511 'ar_flags',
512 'ar_text_id',
513 'ar_deleted',
514 'ar_page_id',
515 'ar_len',
516 'ar_sha1'
517 );
518
519 if ( $wgContentHandlerUseDB ) {
520 $fields[] = 'ar_content_format';
521 $fields[] = 'ar_content_model';
522 }
523
524 /**
525 * Select each archived revision...
526 */
527 $result = $dbw->select( 'archive',
528 $fields,
529 $oldWhere,
530 __METHOD__,
531 /* options */ array( 'ORDER BY' => 'ar_timestamp' )
532 );
533 $ret = $dbw->resultObject( $result );
534 $rev_count = $dbw->numRows( $result );
535
536 if ( !$rev_count ) {
537 wfDebug( __METHOD__ . ": no revisions to restore\n" );
538
539 $status = Status::newGood( 0 );
540 $status->warning( "undelete-no-results" );
541
542 return $status;
543 }
544
545 $ret->seek( $rev_count - 1 ); // move to last
546 $row = $ret->fetchObject(); // get newest archived rev
547 $oldPageId = (int)$row->ar_page_id; // pass this to ArticleUndelete hook
548 $ret->seek( 0 ); // move back
549
550 // grab the content to check consistency with global state before restoring the page.
551 $revision = Revision::newFromArchiveRow( $row,
552 array(
553 'title' => $article->getTitle(), // used to derive default content model
554 )
555 );
556 $user = User::newFromName( $revision->getRawUserText(), false );
557 $content = $revision->getContent( Revision::RAW );
558
559 //NOTE: article ID may not be known yet. prepareSave() should not modify the database.
560 $status = $content->prepareSave( $article, 0, -1, $user );
561
562 if ( !$status->isOK() ) {
563 return $status;
564 }
565
566 if ( $makepage ) {
567 // Check the state of the newest to-be version...
568 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
569 return Status::newFatal( "undeleterevdel" );
570 }
571 // Safe to insert now...
572 $newid = $article->insertOn( $dbw );
573 $pageId = $newid;
574 } else {
575 // Check if a deleted revision will become the current revision...
576 if ( $row->ar_timestamp > $previousTimestamp ) {
577 // Check the state of the newest to-be version...
578 if ( !$unsuppress && ( $row->ar_deleted & Revision::DELETED_TEXT ) ) {
579 return Status::newFatal( "undeleterevdel" );
580 }
581 }
582
583 $newid = false;
584 $pageId = $article->getId();
585 }
586
587 $revision = null;
588 $restored = 0;
589
590 foreach ( $ret as $row ) {
591 // Check for key dupes due to shitty archive integrity.
592 if ( $row->ar_rev_id ) {
593 $exists = $dbw->selectField( 'revision', '1',
594 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
595 if ( $exists ) {
596 continue; // don't throw DB errors
597 }
598 }
599 // Insert one revision at a time...maintaining deletion status
600 // unless we are specifically removing all restrictions...
601 $revision = Revision::newFromArchiveRow( $row,
602 array(
603 'page' => $pageId,
604 'title' => $this->title,
605 'deleted' => $unsuppress ? 0 : $row->ar_deleted
606 ) );
607
608 $revision->insertOn( $dbw );
609 $restored++;
610
611 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
612 }
613 # Now that it's safely stored, take it out of the archive
614 $dbw->delete( 'archive',
615 $oldWhere,
616 __METHOD__ );
617
618 // Was anything restored at all?
619 if ( $restored == 0 ) {
620 return Status::newGood( 0 );
621 }
622
623 $created = (bool)$newid;
624
625 // Attach the latest revision to the page...
626 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
627 if ( $created || $wasnew ) {
628 // Update site stats, link tables, etc
629 $user = User::newFromName( $revision->getRawUserText(), false );
630 $article->doEditUpdates(
631 $revision,
632 $user,
633 array( 'created' => $created, 'oldcountable' => $oldcountable )
634 );
635 }
636
637 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment, $oldPageId ) );
638
639 if ( $this->title->getNamespace() == NS_FILE ) {
640 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
641 $update->doUpdate();
642 }
643
644 return Status::newGood( $restored );
645 }
646
647 /**
648 * @return Status
649 */
650 function getFileStatus() {
651 return $this->fileStatus;
652 }
653
654 /**
655 * @return Status
656 */
657 function getRevisionStatus() {
658 return $this->revisionStatus;
659 }
660 }
661
662 /**
663 * Special page allowing users with the appropriate permissions to view
664 * and restore deleted content.
665 *
666 * @ingroup SpecialPage
667 */
668 class SpecialUndelete extends SpecialPage {
669 private $mAction;
670 private $mTarget;
671 private $mTimestamp;
672 private $mRestore;
673 private $mInvert;
674 private $mFilename;
675 private $mTargetTimestamp;
676 private $mAllowed;
677 private $mCanView;
678 private $mComment;
679 private $mToken;
680
681 /** @var Title */
682 private $mTargetObj;
683
684 function __construct() {
685 parent::__construct( 'Undelete', 'deletedhistory' );
686 }
687
688 function loadRequest( $par ) {
689 $request = $this->getRequest();
690 $user = $this->getUser();
691
692 $this->mAction = $request->getVal( 'action' );
693 if ( $par !== null && $par !== '' ) {
694 $this->mTarget = $par;
695 } else {
696 $this->mTarget = $request->getVal( 'target' );
697 }
698
699 $this->mTargetObj = null;
700
701 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
702 $this->mTargetObj = Title::newFromURL( $this->mTarget );
703 }
704
705 $this->mSearchPrefix = $request->getText( 'prefix' );
706 $time = $request->getVal( 'timestamp' );
707 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
708 $this->mFilename = $request->getVal( 'file' );
709
710 $posted = $request->wasPosted() &&
711 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
712 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
713 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
714 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
715 $this->mDiff = $request->getCheck( 'diff' );
716 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
717 $this->mComment = $request->getText( 'wpComment' );
718 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
719 $this->mToken = $request->getVal( 'token' );
720
721 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
722 $this->mAllowed = true; // user can restore
723 $this->mCanView = true; // user can view content
724 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
725 $this->mAllowed = false; // user cannot restore
726 $this->mCanView = true; // user can view content
727 $this->mRestore = false;
728 } else { // user can only view the list of revisions
729 $this->mAllowed = false;
730 $this->mCanView = false;
731 $this->mTimestamp = '';
732 $this->mRestore = false;
733 }
734
735 if ( $this->mRestore || $this->mInvert ) {
736 $timestamps = array();
737 $this->mFileVersions = array();
738 foreach ( $request->getValues() as $key => $val ) {
739 $matches = array();
740 if ( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
741 array_push( $timestamps, $matches[1] );
742 }
743
744 if ( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
745 $this->mFileVersions[] = intval( $matches[1] );
746 }
747 }
748 rsort( $timestamps );
749 $this->mTargetTimestamp = $timestamps;
750 }
751 }
752
753 function execute( $par ) {
754 $this->checkPermissions();
755 $user = $this->getUser();
756
757 $this->setHeaders();
758 $this->outputHeader();
759
760 $this->loadRequest( $par );
761
762 $out = $this->getOutput();
763
764 if ( is_null( $this->mTargetObj ) ) {
765 $out->addWikiMsg( 'undelete-header' );
766
767 # Not all users can just browse every deleted page from the list
768 if ( $user->isAllowed( 'browsearchive' ) ) {
769 $this->showSearchForm();
770 }
771
772 return;
773 }
774
775 if ( $this->mAllowed ) {
776 $out->setPageTitle( $this->msg( 'undeletepage' ) );
777 } else {
778 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
779 }
780
781 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
782
783 if ( $this->mTimestamp !== '' ) {
784 $this->showRevision( $this->mTimestamp );
785 } elseif ( $this->mFilename !== null && $this->mTargetObj->inNamespace( NS_FILE ) ) {
786 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
787 // Check if user is allowed to see this file
788 if ( !$file->exists() ) {
789 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
790 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
791 if ( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
792 throw new PermissionsError( 'suppressrevision' );
793 } else {
794 throw new PermissionsError( 'deletedtext' );
795 }
796 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
797 $this->showFileConfirmationForm( $this->mFilename );
798 } else {
799 $this->showFile( $this->mFilename );
800 }
801 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
802 $this->undelete();
803 } else {
804 $this->showHistory();
805 }
806 }
807
808 function showSearchForm() {
809 global $wgScript;
810
811 $out = $this->getOutput();
812 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
813 $out->addHTML(
814 Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
815 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
816 Html::hidden( 'title', $this->getPageTitle()->getPrefixedDBkey() ) .
817 Html::rawElement(
818 'label',
819 array( 'for' => 'prefix' ),
820 $this->msg( 'undelete-search-prefix' )->parse()
821 ) .
822 Xml::input(
823 'prefix',
824 20,
825 $this->mSearchPrefix,
826 array( 'id' => 'prefix', 'autofocus' => true )
827 ) . ' ' .
828 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
829 Xml::closeElement( 'fieldset' ) .
830 Xml::closeElement( 'form' )
831 );
832
833 # List undeletable articles
834 if ( $this->mSearchPrefix ) {
835 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
836 $this->showList( $result );
837 }
838 }
839
840 /**
841 * Generic list of deleted pages
842 *
843 * @param ResultWrapper $result
844 * @return bool
845 */
846 private function showList( $result ) {
847 $out = $this->getOutput();
848
849 if ( $result->numRows() == 0 ) {
850 $out->addWikiMsg( 'undelete-no-results' );
851
852 return false;
853 }
854
855 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
856
857 $undelete = $this->getPageTitle();
858 $out->addHTML( "<ul>\n" );
859 foreach ( $result as $row ) {
860 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
861 if ( $title !== null ) {
862 $item = Linker::linkKnown(
863 $undelete,
864 htmlspecialchars( $title->getPrefixedText() ),
865 array(),
866 array( 'target' => $title->getPrefixedText() )
867 );
868 } else {
869 // The title is no longer valid, show as text
870 $item = Html::element(
871 'span',
872 array( 'class' => 'mw-invalidtitle' ),
873 Linker::getInvalidTitleDescription(
874 $this->getContext(),
875 $row->ar_namespace,
876 $row->ar_title
877 )
878 );
879 }
880 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
881 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
882 }
883 $result->free();
884 $out->addHTML( "</ul>\n" );
885
886 return true;
887 }
888
889 private function showRevision( $timestamp ) {
890 if ( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
891 return;
892 }
893
894 $archive = new PageArchive( $this->mTargetObj );
895 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) ) ) {
896 return;
897 }
898 $rev = $archive->getRevision( $timestamp );
899
900 $out = $this->getOutput();
901 $user = $this->getUser();
902
903 if ( !$rev ) {
904 $out->addWikiMsg( 'undeleterevision-missing' );
905
906 return;
907 }
908
909 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
910 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
911 $out->wrapWikiMsg(
912 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
913 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ?
914 'rev-suppressed-text-permission' : 'rev-deleted-text-permission'
915 );
916
917 return;
918 }
919
920 $out->wrapWikiMsg(
921 "<div class='mw-warning plainlinks'>\n$1\n</div>\n",
922 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ?
923 'rev-suppressed-text-view' : 'rev-deleted-text-view'
924 );
925 $out->addHTML( '<br />' );
926 // and we are allowed to see...
927 }
928
929 if ( $this->mDiff ) {
930 $previousRev = $archive->getPreviousRevision( $timestamp );
931 if ( $previousRev ) {
932 $this->showDiff( $previousRev, $rev );
933 if ( $this->mDiffOnly ) {
934 return;
935 }
936
937 $out->addHTML( '<hr />' );
938 } else {
939 $out->addWikiMsg( 'undelete-nodiff' );
940 }
941 }
942
943 $link = Linker::linkKnown(
944 $this->getPageTitle( $this->mTargetObj->getPrefixedDBkey() ),
945 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
946 );
947
948 $lang = $this->getLanguage();
949
950 // date and time are separate parameters to facilitate localisation.
951 // $time is kept for backward compat reasons.
952 $time = $lang->userTimeAndDate( $timestamp, $user );
953 $d = $lang->userDate( $timestamp, $user );
954 $t = $lang->userTime( $timestamp, $user );
955 $userLink = Linker::revUserTools( $rev );
956
957 $content = $rev->getContent( Revision::FOR_THIS_USER, $user );
958
959 $isText = ( $content instanceof TextContent );
960
961 if ( $this->mPreview || $isText ) {
962 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
963 } else {
964 $openDiv = '<div id="mw-undelete-revision">';
965 }
966 $out->addHTML( $openDiv );
967
968 // Revision delete links
969 if ( !$this->mDiff ) {
970 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
971 if ( $revdel ) {
972 $out->addHTML( "$revdel " );
973 }
974 }
975
976 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
977 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
978
979 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) ) ) {
980 return;
981 }
982
983 if ( $this->mPreview || !$isText ) {
984 // NOTE: non-text content has no source view, so always use rendered preview
985
986 // Hide [edit]s
987 $popts = $out->parserOptions();
988 $popts->setEditSection( false );
989
990 $pout = $content->getParserOutput( $this->mTargetObj, $rev->getId(), $popts, true );
991 $out->addParserOutput( $pout );
992 }
993
994 if ( $isText ) {
995 // source view for textual content
996 $sourceView = Xml::element(
997 'textarea',
998 array(
999 'readonly' => 'readonly',
1000 'cols' => $user->getIntOption( 'cols' ),
1001 'rows' => $user->getIntOption( 'rows' )
1002 ),
1003 $content->getNativeData() . "\n"
1004 );
1005
1006 $previewButton = Xml::element( 'input', array(
1007 'type' => 'submit',
1008 'name' => 'preview',
1009 'value' => $this->msg( 'showpreview' )->text()
1010 ) );
1011 } else {
1012 $sourceView = '';
1013 $previewButton = '';
1014 }
1015
1016 $diffButton = Xml::element( 'input', array(
1017 'name' => 'diff',
1018 'type' => 'submit',
1019 'value' => $this->msg( 'showdiff' )->text() ) );
1020
1021 $out->addHTML(
1022 $sourceView .
1023 Xml::openElement( 'div', array(
1024 'style' => 'clear: both' ) ) .
1025 Xml::openElement( 'form', array(
1026 'method' => 'post',
1027 'action' => $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
1028 Xml::element( 'input', array(
1029 'type' => 'hidden',
1030 'name' => 'target',
1031 'value' => $this->mTargetObj->getPrefixedDBkey() ) ) .
1032 Xml::element( 'input', array(
1033 'type' => 'hidden',
1034 'name' => 'timestamp',
1035 'value' => $timestamp ) ) .
1036 Xml::element( 'input', array(
1037 'type' => 'hidden',
1038 'name' => 'wpEditToken',
1039 'value' => $user->getEditToken() ) ) .
1040 $previewButton .
1041 $diffButton .
1042 Xml::closeElement( 'form' ) .
1043 Xml::closeElement( 'div' )
1044 );
1045 }
1046
1047 /**
1048 * Build a diff display between this and the previous either deleted
1049 * or non-deleted edit.
1050 *
1051 * @param Revision $previousRev
1052 * @param Revision $currentRev
1053 * @return string HTML
1054 */
1055 function showDiff( $previousRev, $currentRev ) {
1056 $diffContext = clone $this->getContext();
1057 $diffContext->setTitle( $currentRev->getTitle() );
1058 $diffContext->setWikiPage( WikiPage::factory( $currentRev->getTitle() ) );
1059
1060 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1061 $diffEngine->showDiffStyle();
1062
1063 $formattedDiff = $diffEngine->generateContentDiffBody(
1064 $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
1065 $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() )
1066 );
1067
1068 $formattedDiff = $diffEngine->addHeader(
1069 $formattedDiff,
1070 $this->diffHeader( $previousRev, 'o' ),
1071 $this->diffHeader( $currentRev, 'n' )
1072 );
1073
1074 $this->getOutput()->addHTML( "<div>$formattedDiff</div>\n" );
1075 }
1076
1077 /**
1078 * @param Revision $rev
1079 * @param string $prefix
1080 * @return string
1081 */
1082 private function diffHeader( $rev, $prefix ) {
1083 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1084 if ( $isDeleted ) {
1085 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1086 $targetPage = $this->getPageTitle();
1087 $targetQuery = array(
1088 'target' => $this->mTargetObj->getPrefixedText(),
1089 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
1090 );
1091 } else {
1092 /// @todo FIXME: getId() may return non-zero for deleted revs...
1093 $targetPage = $rev->getTitle();
1094 $targetQuery = array( 'oldid' => $rev->getId() );
1095 }
1096
1097 // Add show/hide deletion links if available
1098 $user = $this->getUser();
1099 $lang = $this->getLanguage();
1100 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1101
1102 if ( $rdel ) {
1103 $rdel = " $rdel";
1104 }
1105
1106 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1107
1108 $tags = wfGetDB( DB_SLAVE )->selectField(
1109 'tag_summary',
1110 'ts_tags',
1111 array( 'ts_rev_id' => $rev->getId() ),
1112 __METHOD__
1113 );
1114 $tagSummary = ChangeTags::formatSummaryRow( $tags, 'deleteddiff' );
1115
1116 // FIXME This is reimplementing DifferenceEngine#getRevisionHeader
1117 // and partially #showDiffPage, but worse
1118 return '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1119 Linker::link(
1120 $targetPage,
1121 $this->msg(
1122 'revisionasof',
1123 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1124 $lang->userDate( $rev->getTimestamp(), $user ),
1125 $lang->userTime( $rev->getTimestamp(), $user )
1126 )->escaped(),
1127 array(),
1128 $targetQuery
1129 ) .
1130 '</strong></div>' .
1131 '<div id="mw-diff-' . $prefix . 'title2">' .
1132 Linker::revUserTools( $rev ) . '<br />' .
1133 '</div>' .
1134 '<div id="mw-diff-' . $prefix . 'title3">' .
1135 $minor . Linker::revComment( $rev ) . $rdel . '<br />' .
1136 '</div>' .
1137 '<div id="mw-diff-' . $prefix . 'title5">' .
1138 $tagSummary[0] . '<br />' .
1139 '</div>';
1140 }
1141
1142 /**
1143 * Show a form confirming whether a tokenless user really wants to see a file
1144 * @param string $key
1145 */
1146 private function showFileConfirmationForm( $key ) {
1147 $out = $this->getOutput();
1148 $lang = $this->getLanguage();
1149 $user = $this->getUser();
1150 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1151 $out->addWikiMsg( 'undelete-show-file-confirm',
1152 $this->mTargetObj->getText(),
1153 $lang->userDate( $file->getTimestamp(), $user ),
1154 $lang->userTime( $file->getTimestamp(), $user ) );
1155 $out->addHTML(
1156 Xml::openElement( 'form', array(
1157 'method' => 'POST',
1158 'action' => $this->getPageTitle()->getLocalURL( array(
1159 'target' => $this->mTarget,
1160 'file' => $key,
1161 'token' => $user->getEditToken( $key ),
1162 ) ),
1163 )
1164 ) .
1165 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1166 '</form>'
1167 );
1168 }
1169
1170 /**
1171 * Show a deleted file version requested by the visitor.
1172 * @param string $key
1173 */
1174 private function showFile( $key ) {
1175 $this->getOutput()->disable();
1176
1177 # We mustn't allow the output to be Squid cached, otherwise
1178 # if an admin previews a deleted image, and it's cached, then
1179 # a user without appropriate permissions can toddle off and
1180 # nab the image, and Squid will serve it
1181 $response = $this->getRequest()->response();
1182 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1183 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1184 $response->header( 'Pragma: no-cache' );
1185
1186 $repo = RepoGroup::singleton()->getLocalRepo();
1187 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1188 $repo->streamFile( $path );
1189 }
1190
1191 private function showHistory() {
1192 $out = $this->getOutput();
1193 if ( $this->mAllowed ) {
1194 $out->addModules( 'mediawiki.special.undelete' );
1195 }
1196 $out->wrapWikiMsg(
1197 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1198 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1199 );
1200
1201 $archive = new PageArchive( $this->mTargetObj );
1202 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1203 /*
1204 $text = $archive->getLastRevisionText();
1205 if( is_null( $text ) ) {
1206 $out->addWikiMsg( 'nohistory' );
1207 return;
1208 }
1209 */
1210 $out->addHTML( '<div class="mw-undelete-history">' );
1211 if ( $this->mAllowed ) {
1212 $out->addWikiMsg( 'undeletehistory' );
1213 $out->addWikiMsg( 'undeleterevdel' );
1214 } else {
1215 $out->addWikiMsg( 'undeletehistorynoadmin' );
1216 }
1217 $out->addHTML( '</div>' );
1218
1219 # List all stored revisions
1220 $revisions = $archive->listRevisions();
1221 $files = $archive->listFiles();
1222
1223 $haveRevisions = $revisions && $revisions->numRows() > 0;
1224 $haveFiles = $files && $files->numRows() > 0;
1225
1226 # Batch existence check on user and talk pages
1227 if ( $haveRevisions ) {
1228 $batch = new LinkBatch();
1229 foreach ( $revisions as $row ) {
1230 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1231 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1232 }
1233 $batch->execute();
1234 $revisions->seek( 0 );
1235 }
1236 if ( $haveFiles ) {
1237 $batch = new LinkBatch();
1238 foreach ( $files as $row ) {
1239 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1240 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1241 }
1242 $batch->execute();
1243 $files->seek( 0 );
1244 }
1245
1246 if ( $this->mAllowed ) {
1247 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
1248 # Start the form here
1249 $top = Xml::openElement(
1250 'form',
1251 array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' )
1252 );
1253 $out->addHTML( $top );
1254 }
1255
1256 # Show relevant lines from the deletion log:
1257 $deleteLogPage = new LogPage( 'delete' );
1258 $out->addHTML( Xml::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1259 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1260 # Show relevant lines from the suppression log:
1261 $suppressLogPage = new LogPage( 'suppress' );
1262 if ( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1263 $out->addHTML( Xml::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1264 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1265 }
1266
1267 if ( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1268 # Format the user-visible controls (comment field, submission button)
1269 # in a nice little table
1270 if ( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1271 $unsuppressBox =
1272 "<tr>
1273 <td>&#160;</td>
1274 <td class='mw-input'>" .
1275 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1276 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ) .
1277 "</td>
1278 </tr>";
1279 } else {
1280 $unsuppressBox = '';
1281 }
1282
1283 $table = Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1284 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1285 "<tr>
1286 <td colspan='2' class='mw-undelete-extrahelp'>" .
1287 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1288 "</td>
1289 </tr>
1290 <tr>
1291 <td class='mw-label'>" .
1292 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1293 "</td>
1294 <td class='mw-input'>" .
1295 Xml::input(
1296 'wpComment',
1297 50,
1298 $this->mComment,
1299 array( 'id' => 'wpComment', 'autofocus' => true )
1300 ) .
1301 "</td>
1302 </tr>
1303 <tr>
1304 <td>&#160;</td>
1305 <td class='mw-submit'>" .
1306 Xml::submitButton(
1307 $this->msg( 'undeletebtn' )->text(),
1308 array( 'name' => 'restore', 'id' => 'mw-undelete-submit' )
1309 ) . ' ' .
1310 Xml::submitButton(
1311 $this->msg( 'undeleteinvert' )->text(),
1312 array( 'name' => 'invert', 'id' => 'mw-undelete-invert' )
1313 ) .
1314 "</td>
1315 </tr>" .
1316 $unsuppressBox .
1317 Xml::closeElement( 'table' ) .
1318 Xml::closeElement( 'fieldset' );
1319
1320 $out->addHTML( $table );
1321 }
1322
1323 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1324
1325 if ( $haveRevisions ) {
1326 # The page's stored (deleted) history:
1327 $out->addHTML( '<ul>' );
1328 $remaining = $revisions->numRows();
1329 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1330
1331 foreach ( $revisions as $row ) {
1332 $remaining--;
1333 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1334 }
1335 $revisions->free();
1336 $out->addHTML( '</ul>' );
1337 } else {
1338 $out->addWikiMsg( 'nohistory' );
1339 }
1340
1341 if ( $haveFiles ) {
1342 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1343 $out->addHTML( '<ul>' );
1344 foreach ( $files as $row ) {
1345 $out->addHTML( $this->formatFileRow( $row ) );
1346 }
1347 $files->free();
1348 $out->addHTML( '</ul>' );
1349 }
1350
1351 if ( $this->mAllowed ) {
1352 # Slip in the hidden controls here
1353 $misc = Html::hidden( 'target', $this->mTarget );
1354 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1355 $misc .= Xml::closeElement( 'form' );
1356 $out->addHTML( $misc );
1357 }
1358
1359 return true;
1360 }
1361
1362 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1363 $rev = Revision::newFromArchiveRow( $row,
1364 array(
1365 'title' => $this->mTargetObj
1366 ) );
1367
1368 $revTextSize = '';
1369 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1370 // Build checkboxen...
1371 if ( $this->mAllowed ) {
1372 if ( $this->mInvert ) {
1373 if ( in_array( $ts, $this->mTargetTimestamp ) ) {
1374 $checkBox = Xml::check( "ts$ts" );
1375 } else {
1376 $checkBox = Xml::check( "ts$ts", true );
1377 }
1378 } else {
1379 $checkBox = Xml::check( "ts$ts" );
1380 }
1381 } else {
1382 $checkBox = '';
1383 }
1384
1385 // Build page & diff links...
1386 $user = $this->getUser();
1387 if ( $this->mCanView ) {
1388 $titleObj = $this->getPageTitle();
1389 # Last link
1390 if ( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1391 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1392 $last = $this->msg( 'diff' )->escaped();
1393 } elseif ( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1394 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1395 $last = Linker::linkKnown(
1396 $titleObj,
1397 $this->msg( 'diff' )->escaped(),
1398 array(),
1399 array(
1400 'target' => $this->mTargetObj->getPrefixedText(),
1401 'timestamp' => $ts,
1402 'diff' => 'prev'
1403 )
1404 );
1405 } else {
1406 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1407 $last = $this->msg( 'diff' )->escaped();
1408 }
1409 } else {
1410 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1411 $last = $this->msg( 'diff' )->escaped();
1412 }
1413
1414 // User links
1415 $userLink = Linker::revUserTools( $rev );
1416
1417 // Minor edit
1418 $minor = $rev->isMinor() ? ChangesList::flag( 'minor' ) : '';
1419
1420 // Revision text size
1421 $size = $row->ar_len;
1422 if ( !is_null( $size ) ) {
1423 $revTextSize = Linker::formatRevisionSize( $size );
1424 }
1425
1426 // Edit summary
1427 $comment = Linker::revComment( $rev );
1428
1429 // Tags
1430 $attribs = array();
1431 list( $tagSummary, $classes ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'deletedhistory' );
1432 if ( $classes ) {
1433 $attribs['class'] = implode( ' ', $classes );
1434 }
1435
1436 // Revision delete links
1437 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1438
1439 $revisionRow = $this->msg( 'undelete-revision-row' )
1440 ->rawParams(
1441 $checkBox,
1442 $revdlink,
1443 $last,
1444 $pageLink,
1445 $userLink,
1446 $minor,
1447 $revTextSize,
1448 $comment,
1449 $tagSummary
1450 )
1451 ->escaped();
1452
1453 return Xml::tags( 'li', $attribs, $revisionRow ) . "\n";
1454 }
1455
1456 private function formatFileRow( $row ) {
1457 $file = ArchivedFile::newFromRow( $row );
1458 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1459 $user = $this->getUser();
1460
1461 if ( $this->mAllowed && $row->fa_storage_key ) {
1462 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1463 $key = urlencode( $row->fa_storage_key );
1464 $pageLink = $this->getFileLink( $file, $this->getPageTitle(), $ts, $key );
1465 } else {
1466 $checkBox = '';
1467 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1468 }
1469 $userLink = $this->getFileUser( $file );
1470 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1471 $bytes = $this->msg( 'parentheses' )
1472 ->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )
1473 ->plain();
1474 $data = htmlspecialchars( $data . ' ' . $bytes );
1475 $comment = $this->getFileComment( $file );
1476
1477 // Add show/hide deletion links if available
1478 $canHide = $user->isAllowed( 'deleterevision' );
1479 if ( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1480 if ( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1481 // Revision was hidden from sysops
1482 $revdlink = Linker::revDeleteLinkDisabled( $canHide );
1483 } else {
1484 $query = array(
1485 'type' => 'filearchive',
1486 'target' => $this->mTargetObj->getPrefixedDBkey(),
1487 'ids' => $row->fa_id
1488 );
1489 $revdlink = Linker::revDeleteLink( $query,
1490 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1491 }
1492 } else {
1493 $revdlink = '';
1494 }
1495
1496 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1497 }
1498
1499 /**
1500 * Fetch revision text link if it's available to all users
1501 *
1502 * @param Revision $rev
1503 * @param Title $titleObj
1504 * @param string $ts Timestamp
1505 * @return string
1506 */
1507 function getPageLink( $rev, $titleObj, $ts ) {
1508 $user = $this->getUser();
1509 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1510
1511 if ( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
1512 return '<span class="history-deleted">' . $time . '</span>';
1513 }
1514
1515 $link = Linker::linkKnown(
1516 $titleObj,
1517 htmlspecialchars( $time ),
1518 array(),
1519 array(
1520 'target' => $this->mTargetObj->getPrefixedText(),
1521 'timestamp' => $ts
1522 )
1523 );
1524
1525 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1526 $link = '<span class="history-deleted">' . $link . '</span>';
1527 }
1528
1529 return $link;
1530 }
1531
1532 /**
1533 * Fetch image view link if it's available to all users
1534 *
1535 * @param File|ArchivedFile $file
1536 * @param Title $titleObj
1537 * @param string $ts A timestamp
1538 * @param string $key A storage key
1539 *
1540 * @return string HTML fragment
1541 */
1542 function getFileLink( $file, $titleObj, $ts, $key ) {
1543 $user = $this->getUser();
1544 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1545
1546 if ( !$file->userCan( File::DELETED_FILE, $user ) ) {
1547 return '<span class="history-deleted">' . $time . '</span>';
1548 }
1549
1550 $link = Linker::linkKnown(
1551 $titleObj,
1552 htmlspecialchars( $time ),
1553 array(),
1554 array(
1555 'target' => $this->mTargetObj->getPrefixedText(),
1556 'file' => $key,
1557 'token' => $user->getEditToken( $key )
1558 )
1559 );
1560
1561 if ( $file->isDeleted( File::DELETED_FILE ) ) {
1562 $link = '<span class="history-deleted">' . $link . '</span>';
1563 }
1564
1565 return $link;
1566 }
1567
1568 /**
1569 * Fetch file's user id if it's available to this user
1570 *
1571 * @param File|ArchivedFile $file
1572 * @return string HTML fragment
1573 */
1574 function getFileUser( $file ) {
1575 if ( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1576 return '<span class="history-deleted">' .
1577 $this->msg( 'rev-deleted-user' )->escaped() .
1578 '</span>';
1579 }
1580
1581 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1582 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1583
1584 if ( $file->isDeleted( File::DELETED_USER ) ) {
1585 $link = '<span class="history-deleted">' . $link . '</span>';
1586 }
1587
1588 return $link;
1589 }
1590
1591 /**
1592 * Fetch file upload comment if it's available to this user
1593 *
1594 * @param File|ArchivedFile $file
1595 * @return string HTML fragment
1596 */
1597 function getFileComment( $file ) {
1598 if ( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1599 return '<span class="history-deleted"><span class="comment">' .
1600 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1601 }
1602
1603 $link = Linker::commentBlock( $file->getRawDescription() );
1604
1605 if ( $file->isDeleted( File::DELETED_COMMENT ) ) {
1606 $link = '<span class="history-deleted">' . $link . '</span>';
1607 }
1608
1609 return $link;
1610 }
1611
1612 function undelete() {
1613 global $wgUploadMaintenance;
1614
1615 if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
1616 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1617 }
1618
1619 if ( wfReadOnly() ) {
1620 throw new ReadOnlyError;
1621 }
1622
1623 $out = $this->getOutput();
1624 $archive = new PageArchive( $this->mTargetObj );
1625 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1626 $ok = $archive->undelete(
1627 $this->mTargetTimestamp,
1628 $this->mComment,
1629 $this->mFileVersions,
1630 $this->mUnsuppress,
1631 $this->getUser()
1632 );
1633
1634 if ( is_array( $ok ) ) {
1635 if ( $ok[1] ) { // Undeleted file count
1636 wfRunHooks( 'FileUndeleteComplete', array(
1637 $this->mTargetObj, $this->mFileVersions,
1638 $this->getUser(), $this->mComment ) );
1639 }
1640
1641 $link = Linker::linkKnown( $this->mTargetObj );
1642 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1643 } else {
1644 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1645 }
1646
1647 // Show revision undeletion warnings and errors
1648 $status = $archive->getRevisionStatus();
1649 if ( $status && !$status->isGood() ) {
1650 $out->addWikiText( '<div class="error">' .
1651 $status->getWikiText(
1652 'cannotundelete',
1653 'cannotundelete'
1654 ) . '</div>'
1655 );
1656 }
1657
1658 // Show file undeletion warnings and errors
1659 $status = $archive->getFileStatus();
1660 if ( $status && !$status->isGood() ) {
1661 $out->addWikiText( '<div class="error">' .
1662 $status->getWikiText(
1663 'undelete-error-short',
1664 'undelete-error-long'
1665 ) . '</div>'
1666 );
1667 }
1668 }
1669
1670 protected function getGroupName() {
1671 return 'pagetools';
1672 }
1673 }