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