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