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