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