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