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