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