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