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