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 $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 $wgContentHandlerUseDB;
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 ( $wgContentHandlerUseDB ) {
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 $newid = false;
580 $pageId = $article->getId();
581 }
582
583 $revision = null;
584 $restored = 0;
585
586 foreach ( $ret as $row ) {
587 // Check for key dupes due to shitty archive integrity.
588 if( $row->ar_rev_id ) {
589 $exists = $dbw->selectField( 'revision', '1',
590 array( 'rev_id' => $row->ar_rev_id ), __METHOD__ );
591 if( $exists ) {
592 continue; // don't throw DB errors
593 }
594 }
595 // Insert one revision at a time...maintaining deletion status
596 // unless we are specifically removing all restrictions...
597 $revision = Revision::newFromArchiveRow( $row,
598 array(
599 'page' => $pageId,
600 'title' => $this->title,
601 'deleted' => $unsuppress ? 0 : $row->ar_deleted
602 ) );
603
604 $revision->insertOn( $dbw );
605 $restored++;
606
607 wfRunHooks( 'ArticleRevisionUndeleted', array( &$this->title, $revision, $row->ar_page_id ) );
608 }
609 # Now that it's safely stored, take it out of the archive
610 $dbw->delete( 'archive',
611 /* WHERE */ array(
612 'ar_namespace' => $this->title->getNamespace(),
613 'ar_title' => $this->title->getDBkey(),
614 $oldones ),
615 __METHOD__ );
616
617 // Was anything restored at all?
618 if ( $restored == 0 ) {
619 return Status::newGood( 0 );
620 }
621
622 $created = (bool)$newid;
623
624 // Attach the latest revision to the page...
625 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
626 if ( $created || $wasnew ) {
627 // Update site stats, link tables, etc
628 $user = User::newFromName( $revision->getRawUserText(), false );
629 $article->doEditUpdates( $revision, $user, array( 'created' => $created, 'oldcountable' => $oldcountable ) );
630 }
631
632 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment ) );
633
634 if( $this->title->getNamespace() == NS_FILE ) {
635 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
636 $update->doUpdate();
637 }
638
639 return Status::newGood( $restored );
640 }
641
642 /**
643 * @return Status
644 */
645 function getFileStatus() { return $this->fileStatus; }
646
647 /**
648 * @return Status
649 */
650 function getRevisionStatus() { return $this->revisionStatus; }
651 }
652
653 /**
654 * Special page allowing users with the appropriate permissions to view
655 * and restore deleted content.
656 *
657 * @ingroup SpecialPage
658 */
659 class SpecialUndelete extends SpecialPage {
660 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
661 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken;
662
663 /**
664 * @var Title
665 */
666 var $mTargetObj;
667
668 function __construct() {
669 parent::__construct( 'Undelete', 'deletedhistory' );
670 }
671
672 function loadRequest( $par ) {
673 $request = $this->getRequest();
674 $user = $this->getUser();
675
676 $this->mAction = $request->getVal( 'action' );
677 if ( $par !== null && $par !== '' ) {
678 $this->mTarget = $par;
679 } else {
680 $this->mTarget = $request->getVal( 'target' );
681 }
682 $this->mTargetObj = null;
683 if ( $this->mTarget !== null && $this->mTarget !== '' ) {
684 $this->mTargetObj = Title::newFromURL( $this->mTarget );
685 }
686 $this->mSearchPrefix = $request->getText( 'prefix' );
687 $time = $request->getVal( 'timestamp' );
688 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
689 $this->mFilename = $request->getVal( 'file' );
690
691 $posted = $request->wasPosted() &&
692 $user->matchEditToken( $request->getVal( 'wpEditToken' ) );
693 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
694 $this->mInvert = $request->getCheck( 'invert' ) && $posted;
695 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
696 $this->mDiff = $request->getCheck( 'diff' );
697 $this->mDiffOnly = $request->getBool( 'diffonly', $this->getUser()->getOption( 'diffonly' ) );
698 $this->mComment = $request->getText( 'wpComment' );
699 $this->mUnsuppress = $request->getVal( 'wpUnsuppress' ) && $user->isAllowed( 'suppressrevision' );
700 $this->mToken = $request->getVal( 'token' );
701
702 if ( $user->isAllowed( 'undelete' ) && !$user->isBlocked() ) {
703 $this->mAllowed = true; // user can restore
704 $this->mCanView = true; // user can view content
705 } elseif ( $user->isAllowed( 'deletedtext' ) ) {
706 $this->mAllowed = false; // user cannot restore
707 $this->mCanView = true; // user can view content
708 $this->mRestore = false;
709 } else { // user can only view the list of revisions
710 $this->mAllowed = false;
711 $this->mCanView = false;
712 $this->mTimestamp = '';
713 $this->mRestore = false;
714 }
715
716 if( $this->mRestore || $this->mInvert ) {
717 $timestamps = array();
718 $this->mFileVersions = array();
719 foreach( $request->getValues() as $key => $val ) {
720 $matches = array();
721 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
722 array_push( $timestamps, $matches[1] );
723 }
724
725 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
726 $this->mFileVersions[] = intval( $matches[1] );
727 }
728 }
729 rsort( $timestamps );
730 $this->mTargetTimestamp = $timestamps;
731 }
732 }
733
734 function execute( $par ) {
735 $this->checkPermissions();
736 $user = $this->getUser();
737
738 $this->setHeaders();
739 $this->outputHeader();
740
741 $this->loadRequest( $par );
742
743 $out = $this->getOutput();
744
745 if ( is_null( $this->mTargetObj ) ) {
746 $out->addWikiMsg( 'undelete-header' );
747
748 # Not all users can just browse every deleted page from the list
749 if ( $user->isAllowed( 'browsearchive' ) ) {
750 $this->showSearchForm();
751 }
752 return;
753 }
754
755 if ( $this->mAllowed ) {
756 $out->setPageTitle( $this->msg( 'undeletepage' ) );
757 } else {
758 $out->setPageTitle( $this->msg( 'viewdeletedpage' ) );
759 }
760
761 $this->getSkin()->setRelevantTitle( $this->mTargetObj );
762
763 if ( $this->mTimestamp !== '' ) {
764 $this->showRevision( $this->mTimestamp );
765 } elseif ( $this->mFilename !== null ) {
766 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
767 // Check if user is allowed to see this file
768 if ( !$file->exists() ) {
769 $out->addWikiMsg( 'filedelete-nofile', $this->mFilename );
770 } elseif ( !$file->userCan( File::DELETED_FILE, $user ) ) {
771 if( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
772 throw new PermissionsError( 'suppressrevision' );
773 } else {
774 throw new PermissionsError( 'deletedtext' );
775 }
776 } elseif ( !$user->matchEditToken( $this->mToken, $this->mFilename ) ) {
777 $this->showFileConfirmationForm( $this->mFilename );
778 } else {
779 $this->showFile( $this->mFilename );
780 }
781 } elseif ( $this->mRestore && $this->mAction == 'submit' ) {
782 $this->undelete();
783 } else {
784 $this->showHistory();
785 }
786 }
787
788 function showSearchForm() {
789 global $wgScript;
790
791 $out = $this->getOutput();
792 $out->setPageTitle( $this->msg( 'undelete-search-title' ) );
793 $out->addHTML(
794 Xml::openElement( 'form', array(
795 'method' => 'get',
796 'action' => $wgScript ) ) .
797 Xml::fieldset( $this->msg( 'undelete-search-box' )->text() ) .
798 Html::hidden( 'title',
799 $this->getTitle()->getPrefixedDbKey() ) .
800 Xml::inputLabel( $this->msg( 'undelete-search-prefix' )->text(),
801 'prefix', 'prefix', 20,
802 $this->mSearchPrefix ) . ' ' .
803 Xml::submitButton( $this->msg( 'undelete-search-submit' )->text() ) .
804 Xml::closeElement( 'fieldset' ) .
805 Xml::closeElement( 'form' )
806 );
807
808 # List undeletable articles
809 if( $this->mSearchPrefix ) {
810 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
811 $this->showList( $result );
812 }
813 }
814
815 /**
816 * Generic list of deleted pages
817 *
818 * @param $result ResultWrapper
819 * @return bool
820 */
821 private function showList( $result ) {
822 $out = $this->getOutput();
823
824 if( $result->numRows() == 0 ) {
825 $out->addWikiMsg( 'undelete-no-results' );
826 return;
827 }
828
829 $out->addWikiMsg( 'undeletepagetext', $this->getLanguage()->formatNum( $result->numRows() ) );
830
831 $undelete = $this->getTitle();
832 $out->addHTML( "<ul>\n" );
833 foreach ( $result as $row ) {
834 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
835 if ( $title !== null ) {
836 $item = Linker::linkKnown(
837 $undelete,
838 htmlspecialchars( $title->getPrefixedText() ),
839 array(),
840 array( 'target' => $title->getPrefixedText() )
841 );
842 } else {
843 // The title is no longer valid, show as text
844 $item = Html::element( 'span', array( 'class' => 'mw-invalidtitle' ),
845 Linker::getInvalidTitleDescription( $this->getContext(), $row->ar_namespace, $row->ar_title ) );
846 }
847 $revs = $this->msg( 'undeleterevisions' )->numParams( $row->count )->parse();
848 $out->addHTML( "<li>{$item} ({$revs})</li>\n" );
849 }
850 $result->free();
851 $out->addHTML( "</ul>\n" );
852
853 return true;
854 }
855
856 private function showRevision( $timestamp ) {
857 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
858 return;
859 }
860
861 $archive = new PageArchive( $this->mTargetObj );
862 if ( !wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) ) ) {
863 return;
864 }
865 $rev = $archive->getRevision( $timestamp );
866
867 $out = $this->getOutput();
868 $user = $this->getUser();
869
870 if( !$rev ) {
871 $out->addWikiMsg( 'undeleterevision-missing' );
872 return;
873 }
874
875 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
876 if( !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
877 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
878 return;
879 } else {
880 $out->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
881 $out->addHTML( '<br />' );
882 // and we are allowed to see...
883 }
884 }
885
886 if( $this->mDiff ) {
887 $previousRev = $archive->getPreviousRevision( $timestamp );
888 if( $previousRev ) {
889 $this->showDiff( $previousRev, $rev );
890 if( $this->mDiffOnly ) {
891 return;
892 } else {
893 $out->addHTML( '<hr />' );
894 }
895 } else {
896 $out->addWikiMsg( 'undelete-nodiff' );
897 }
898 }
899
900 $link = Linker::linkKnown(
901 $this->getTitle( $this->mTargetObj->getPrefixedDBkey() ),
902 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
903 );
904
905 $lang = $this->getLanguage();
906
907 // date and time are separate parameters to facilitate localisation.
908 // $time is kept for backward compat reasons.
909 $time = $lang->userTimeAndDate( $timestamp, $user );
910 $d = $lang->userDate( $timestamp, $user );
911 $t = $lang->userTime( $timestamp, $user );
912 $userLink = Linker::revUserTools( $rev );
913
914 $content = $rev->getContent( Revision::FOR_THIS_USER, $user );
915
916 $isText = ( $content instanceof TextContent );
917
918 if( $this->mPreview || $isText ) {
919 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
920 } else {
921 $openDiv = '<div id="mw-undelete-revision">';
922 }
923 $out->addHTML( $openDiv );
924
925 // Revision delete links
926 if ( !$this->mDiff ) {
927 $revdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
928 if ( $revdel ) {
929 $out->addHTML( "$revdel " );
930 }
931 }
932
933 $out->addHTML( $this->msg( 'undelete-revision' )->rawParams( $link )->params(
934 $time )->rawParams( $userLink )->params( $d, $t )->parse() . '</div>' );
935
936 if ( !wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) ) ) {
937 return;
938 }
939
940 if( $this->mPreview || !$isText ) {
941 // NOTE: non-text content has no source view, so always use rendered preview
942
943 // Hide [edit]s
944 $popts = $out->parserOptions();
945 $popts->setEditSection( false );
946
947 $pout = $content->getParserOutput( $this->mTargetObj, $rev->getId(), $popts, true );
948 $out->addParserOutput( $pout );
949 }
950
951 if ( $isText ) {
952 // source view for textual content
953 $sourceView = Xml::element( 'textarea', array(
954 'readonly' => 'readonly',
955 'cols' => intval( $user->getOption( 'cols' ) ),
956 'rows' => intval( $user->getOption( 'rows' ) ) ),
957 $content->getNativeData() . "\n" );
958
959 $previewButton = Xml::element( 'input', array(
960 'type' => 'submit',
961 'name' => 'preview',
962 'value' => $this->msg( 'showpreview' )->text() ) );
963 } else {
964 $sourceView = '';
965 $previewButton = '';
966 }
967
968 $diffButton = Xml::element( 'input', array(
969 'name' => 'diff',
970 'type' => 'submit',
971 'value' => $this->msg( 'showdiff' )->text() ) );
972
973 $out->addHTML(
974 $sourceView .
975 Xml::openElement( 'div', array(
976 'style' => 'clear: both' ) ) .
977 Xml::openElement( 'form', array(
978 'method' => 'post',
979 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
980 Xml::element( 'input', array(
981 'type' => 'hidden',
982 'name' => 'target',
983 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
984 Xml::element( 'input', array(
985 'type' => 'hidden',
986 'name' => 'timestamp',
987 'value' => $timestamp ) ) .
988 Xml::element( 'input', array(
989 'type' => 'hidden',
990 'name' => 'wpEditToken',
991 'value' => $user->getEditToken() ) ) .
992 $previewButton .
993 $diffButton .
994 Xml::closeElement( 'form' ) .
995 Xml::closeElement( 'div' ) );
996 }
997
998 /**
999 * Build a diff display between this and the previous either deleted
1000 * or non-deleted edit.
1001 *
1002 * @param $previousRev Revision
1003 * @param $currentRev Revision
1004 * @return String: HTML
1005 */
1006 function showDiff( $previousRev, $currentRev ) {
1007 $diffContext = clone $this->getContext();
1008 $diffContext->setTitle( $currentRev->getTitle() );
1009 $diffContext->setWikiPage( WikiPage::factory( $currentRev->getTitle() ) );
1010
1011 $diffEngine = $currentRev->getContentHandler()->createDifferenceEngine( $diffContext );
1012 $diffEngine->showDiffStyle();
1013 $this->getOutput()->addHTML(
1014 "<div>" .
1015 "<table style='width: 98%;' cellpadding='0' cellspacing='4' class='diff'>" .
1016 "<col class='diff-marker' />" .
1017 "<col class='diff-content' />" .
1018 "<col class='diff-marker' />" .
1019 "<col class='diff-content' />" .
1020 "<tr>" .
1021 "<td colspan='2' style='width: 50%; text-align: center' class='diff-otitle'>" .
1022 $this->diffHeader( $previousRev, 'o' ) .
1023 "</td>\n" .
1024 "<td colspan='2' style='width: 50%; text-align: center' class='diff-ntitle'>" .
1025 $this->diffHeader( $currentRev, 'n' ) .
1026 "</td>\n" .
1027 "</tr>" .
1028 $diffEngine->generateContentDiffBody(
1029 $previousRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ),
1030 $currentRev->getContent( Revision::FOR_THIS_USER, $this->getUser() ) ) .
1031 "</table>" .
1032 "</div>\n"
1033 );
1034 }
1035
1036 /**
1037 * @param $rev Revision
1038 * @param $prefix
1039 * @return string
1040 */
1041 private function diffHeader( $rev, $prefix ) {
1042 $isDeleted = !( $rev->getId() && $rev->getTitle() );
1043 if( $isDeleted ) {
1044 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
1045 $targetPage = $this->getTitle();
1046 $targetQuery = array(
1047 'target' => $this->mTargetObj->getPrefixedText(),
1048 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
1049 );
1050 } else {
1051 /// @todo FIXME: getId() may return non-zero for deleted revs...
1052 $targetPage = $rev->getTitle();
1053 $targetQuery = array( 'oldid' => $rev->getId() );
1054 }
1055 // Add show/hide deletion links if available
1056 $user = $this->getUser();
1057 $lang = $this->getLanguage();
1058 $rdel = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1059 if ( $rdel ) $rdel = " $rdel";
1060 return
1061 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1062 Linker::link(
1063 $targetPage,
1064 $this->msg(
1065 'revisionasof',
1066 $lang->userTimeAndDate( $rev->getTimestamp(), $user ),
1067 $lang->userDate( $rev->getTimestamp(), $user ),
1068 $lang->userTime( $rev->getTimestamp(), $user )
1069 )->escaped(),
1070 array(),
1071 $targetQuery
1072 ) .
1073 '</strong></div>' .
1074 '<div id="mw-diff-'.$prefix.'title2">' .
1075 Linker::revUserTools( $rev ) . '<br />' .
1076 '</div>' .
1077 '<div id="mw-diff-'.$prefix.'title3">' .
1078 Linker::revComment( $rev ) . $rdel . '<br />' .
1079 '</div>';
1080 }
1081
1082 /**
1083 * Show a form confirming whether a tokenless user really wants to see a file
1084 */
1085 private function showFileConfirmationForm( $key ) {
1086 $out = $this->getOutput();
1087 $lang = $this->getLanguage();
1088 $user = $this->getUser();
1089 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1090 $out->addWikiMsg( 'undelete-show-file-confirm',
1091 $this->mTargetObj->getText(),
1092 $lang->userDate( $file->getTimestamp(), $user ),
1093 $lang->userTime( $file->getTimestamp(), $user ) );
1094 $out->addHTML(
1095 Xml::openElement( 'form', array(
1096 'method' => 'POST',
1097 'action' => $this->getTitle()->getLocalURL(
1098 'target=' . urlencode( $this->mTarget ) .
1099 '&file=' . urlencode( $key ) .
1100 '&token=' . urlencode( $user->getEditToken( $key ) ) )
1101 )
1102 ) .
1103 Xml::submitButton( $this->msg( 'undelete-show-file-submit' )->text() ) .
1104 '</form>'
1105 );
1106 }
1107
1108 /**
1109 * Show a deleted file version requested by the visitor.
1110 */
1111 private function showFile( $key ) {
1112 $this->getOutput()->disable();
1113
1114 # We mustn't allow the output to be Squid cached, otherwise
1115 # if an admin previews a deleted image, and it's cached, then
1116 # a user without appropriate permissions can toddle off and
1117 # nab the image, and Squid will serve it
1118 $response = $this->getRequest()->response();
1119 $response->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1120 $response->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1121 $response->header( 'Pragma: no-cache' );
1122
1123 $repo = RepoGroup::singleton()->getLocalRepo();
1124 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1125 $repo->streamFile( $path );
1126 }
1127
1128 private function showHistory() {
1129 $out = $this->getOutput();
1130 if( $this->mAllowed ) {
1131 $out->addModules( 'mediawiki.special.undelete' );
1132 }
1133 $out->wrapWikiMsg(
1134 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1135 array( 'undeletepagetitle', wfEscapeWikiText( $this->mTargetObj->getPrefixedText() ) )
1136 );
1137
1138 $archive = new PageArchive( $this->mTargetObj );
1139 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1140 /*
1141 $text = $archive->getLastRevisionText();
1142 if( is_null( $text ) ) {
1143 $out->addWikiMsg( 'nohistory' );
1144 return;
1145 }
1146 */
1147 $out->addHTML( '<div class="mw-undelete-history">' );
1148 if ( $this->mAllowed ) {
1149 $out->addWikiMsg( 'undeletehistory' );
1150 $out->addWikiMsg( 'undeleterevdel' );
1151 } else {
1152 $out->addWikiMsg( 'undeletehistorynoadmin' );
1153 }
1154 $out->addHTML( '</div>' );
1155
1156 # List all stored revisions
1157 $revisions = $archive->listRevisions();
1158 $files = $archive->listFiles();
1159
1160 $haveRevisions = $revisions && $revisions->numRows() > 0;
1161 $haveFiles = $files && $files->numRows() > 0;
1162
1163 # Batch existence check on user and talk pages
1164 if( $haveRevisions ) {
1165 $batch = new LinkBatch();
1166 foreach ( $revisions as $row ) {
1167 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1168 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1169 }
1170 $batch->execute();
1171 $revisions->seek( 0 );
1172 }
1173 if( $haveFiles ) {
1174 $batch = new LinkBatch();
1175 foreach ( $files as $row ) {
1176 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1177 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1178 }
1179 $batch->execute();
1180 $files->seek( 0 );
1181 }
1182
1183 if ( $this->mAllowed ) {
1184 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1185 # Start the form here
1186 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1187 $out->addHTML( $top );
1188 }
1189
1190 # Show relevant lines from the deletion log:
1191 $deleteLogPage = new LogPage( 'delete' );
1192 $out->addHTML( Xml::element( 'h2', null, $deleteLogPage->getName()->text() ) . "\n" );
1193 LogEventsList::showLogExtract( $out, 'delete', $this->mTargetObj );
1194 # Show relevant lines from the suppression log:
1195 $suppressLogPage = new LogPage( 'suppress' );
1196 if( $this->getUser()->isAllowed( 'suppressionlog' ) ) {
1197 $out->addHTML( Xml::element( 'h2', null, $suppressLogPage->getName()->text() ) . "\n" );
1198 LogEventsList::showLogExtract( $out, 'suppress', $this->mTargetObj );
1199 }
1200
1201 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1202 # Format the user-visible controls (comment field, submission button)
1203 # in a nice little table
1204 if( $this->getUser()->isAllowed( 'suppressrevision' ) ) {
1205 $unsuppressBox =
1206 "<tr>
1207 <td>&#160;</td>
1208 <td class='mw-input'>" .
1209 Xml::checkLabel( $this->msg( 'revdelete-unsuppress' )->text(),
1210 'wpUnsuppress', 'mw-undelete-unsuppress', $this->mUnsuppress ).
1211 "</td>
1212 </tr>";
1213 } else {
1214 $unsuppressBox = '';
1215 }
1216 $table =
1217 Xml::fieldset( $this->msg( 'undelete-fieldset-title' )->text() ) .
1218 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1219 "<tr>
1220 <td colspan='2' class='mw-undelete-extrahelp'>" .
1221 $this->msg( 'undeleteextrahelp' )->parseAsBlock() .
1222 "</td>
1223 </tr>
1224 <tr>
1225 <td class='mw-label'>" .
1226 Xml::label( $this->msg( 'undeletecomment' )->text(), 'wpComment' ) .
1227 "</td>
1228 <td class='mw-input'>" .
1229 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
1230 "</td>
1231 </tr>
1232 <tr>
1233 <td>&#160;</td>
1234 <td class='mw-submit'>" .
1235 Xml::submitButton( $this->msg( 'undeletebtn' )->text(), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1236 Xml::submitButton( $this->msg( 'undeleteinvert' )->text(), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1237 "</td>
1238 </tr>" .
1239 $unsuppressBox .
1240 Xml::closeElement( 'table' ) .
1241 Xml::closeElement( 'fieldset' );
1242
1243 $out->addHTML( $table );
1244 }
1245
1246 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'history' )->text() ) . "\n" );
1247
1248 if( $haveRevisions ) {
1249 # The page's stored (deleted) history:
1250 $out->addHTML( '<ul>' );
1251 $remaining = $revisions->numRows();
1252 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1253
1254 foreach ( $revisions as $row ) {
1255 $remaining--;
1256 $out->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining ) );
1257 }
1258 $revisions->free();
1259 $out->addHTML( '</ul>' );
1260 } else {
1261 $out->addWikiMsg( 'nohistory' );
1262 }
1263
1264 if( $haveFiles ) {
1265 $out->addHTML( Xml::element( 'h2', null, $this->msg( 'filehist' )->text() ) . "\n" );
1266 $out->addHTML( '<ul>' );
1267 foreach ( $files as $row ) {
1268 $out->addHTML( $this->formatFileRow( $row ) );
1269 }
1270 $files->free();
1271 $out->addHTML( '</ul>' );
1272 }
1273
1274 if ( $this->mAllowed ) {
1275 # Slip in the hidden controls here
1276 $misc = Html::hidden( 'target', $this->mTarget );
1277 $misc .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken() );
1278 $misc .= Xml::closeElement( 'form' );
1279 $out->addHTML( $misc );
1280 }
1281
1282 return true;
1283 }
1284
1285 private function formatRevisionRow( $row, $earliestLiveTime, $remaining ) {
1286 $rev = Revision::newFromArchiveRow( $row,
1287 array(
1288 'title' => $this->mTargetObj
1289 ) );
1290
1291 $revTextSize = '';
1292 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1293 // Build checkboxen...
1294 if( $this->mAllowed ) {
1295 if( $this->mInvert ) {
1296 if( in_array( $ts, $this->mTargetTimestamp ) ) {
1297 $checkBox = Xml::check( "ts$ts" );
1298 } else {
1299 $checkBox = Xml::check( "ts$ts", true );
1300 }
1301 } else {
1302 $checkBox = Xml::check( "ts$ts" );
1303 }
1304 } else {
1305 $checkBox = '';
1306 }
1307 $user = $this->getUser();
1308 // Build page & diff links...
1309 if( $this->mCanView ) {
1310 $titleObj = $this->getTitle();
1311 # Last link
1312 if( !$rev->userCan( Revision::DELETED_TEXT, $this->getUser() ) ) {
1313 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1314 $last = $this->msg( 'diff' )->escaped();
1315 } elseif( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1316 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1317 $last = Linker::linkKnown(
1318 $titleObj,
1319 $this->msg( 'diff' )->escaped(),
1320 array(),
1321 array(
1322 'target' => $this->mTargetObj->getPrefixedText(),
1323 'timestamp' => $ts,
1324 'diff' => 'prev'
1325 )
1326 );
1327 } else {
1328 $pageLink = $this->getPageLink( $rev, $titleObj, $ts );
1329 $last = $this->msg( 'diff' )->escaped();
1330 }
1331 } else {
1332 $pageLink = htmlspecialchars( $this->getLanguage()->userTimeAndDate( $ts, $user ) );
1333 $last = $this->msg( 'diff' )->escaped();
1334 }
1335 // User links
1336 $userLink = Linker::revUserTools( $rev );
1337 // Revision text size
1338 $size = $row->ar_len;
1339 if( !is_null( $size ) ) {
1340 $revTextSize = Linker::formatRevisionSize( $size );
1341 }
1342 // Edit summary
1343 $comment = Linker::revComment( $rev );
1344 // Revision delete links
1345 $revdlink = Linker::getRevDeleteLink( $user, $rev, $this->mTargetObj );
1346
1347 $revisionRow = $this->msg( 'undelete-revisionrow' )->rawParams( $checkBox, $revdlink, $last, $pageLink , $userLink, $revTextSize, $comment )->escaped();
1348 return "<li>$revisionRow</li>";
1349 }
1350
1351 private function formatFileRow( $row ) {
1352 $file = ArchivedFile::newFromRow( $row );
1353
1354 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1355 $user = $this->getUser();
1356 if( $this->mAllowed && $row->fa_storage_key ) {
1357 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1358 $key = urlencode( $row->fa_storage_key );
1359 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key );
1360 } else {
1361 $checkBox = '';
1362 $pageLink = $this->getLanguage()->userTimeAndDate( $ts, $user );
1363 }
1364 $userLink = $this->getFileUser( $file );
1365 $data = $this->msg( 'widthheight' )->numParams( $row->fa_width, $row->fa_height )->text();
1366 $bytes = $this->msg( 'parentheses' )->rawParams( $this->msg( 'nbytes' )->numParams( $row->fa_size )->text() )->plain();
1367 $data = htmlspecialchars( $data . ' ' . $bytes );
1368 $comment = $this->getFileComment( $file );
1369
1370 // Add show/hide deletion links if available
1371 $canHide = $user->isAllowed( 'deleterevision' );
1372 if( $canHide || ( $file->getVisibility() && $user->isAllowed( 'deletedhistory' ) ) ) {
1373 if( !$file->userCan( File::DELETED_RESTRICTED, $user ) ) {
1374 $revdlink = Linker::revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1375 } else {
1376 $query = array(
1377 'type' => 'filearchive',
1378 'target' => $this->mTargetObj->getPrefixedDBkey(),
1379 'ids' => $row->fa_id
1380 );
1381 $revdlink = Linker::revDeleteLink( $query,
1382 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1383 }
1384 } else {
1385 $revdlink = '';
1386 }
1387
1388 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1389 }
1390
1391 /**
1392 * Fetch revision text link if it's available to all users
1393 *
1394 * @param $rev Revision
1395 * @param $titleObj Title
1396 * @param $ts string Timestamp
1397 * @return string
1398 */
1399 function getPageLink( $rev, $titleObj, $ts ) {
1400 $user = $this->getUser();
1401 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1402
1403 if( !$rev->userCan( Revision::DELETED_TEXT, $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 'timestamp' => $ts
1413 )
1414 );
1415 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1416 $link = '<span class="history-deleted">' . $link . '</span>';
1417 }
1418 return $link;
1419 }
1420 }
1421
1422 /**
1423 * Fetch image view link if it's available to all users
1424 *
1425 * @param $file File
1426 * @param $titleObj Title
1427 * @param $ts string A timestamp
1428 * @param $key String: a storage key
1429 *
1430 * @return String: HTML fragment
1431 */
1432 function getFileLink( $file, $titleObj, $ts, $key ) {
1433 $user = $this->getUser();
1434 $time = $this->getLanguage()->userTimeAndDate( $ts, $user );
1435
1436 if( !$file->userCan( File::DELETED_FILE, $user ) ) {
1437 return '<span class="history-deleted">' . $time . '</span>';
1438 } else {
1439 $link = Linker::linkKnown(
1440 $titleObj,
1441 htmlspecialchars( $time ),
1442 array(),
1443 array(
1444 'target' => $this->mTargetObj->getPrefixedText(),
1445 'file' => $key,
1446 'token' => $user->getEditToken( $key )
1447 )
1448 );
1449 if( $file->isDeleted( File::DELETED_FILE ) ) {
1450 $link = '<span class="history-deleted">' . $link . '</span>';
1451 }
1452 return $link;
1453 }
1454 }
1455
1456 /**
1457 * Fetch file's user id if it's available to this user
1458 *
1459 * @param $file File
1460 * @return String: HTML fragment
1461 */
1462 function getFileUser( $file ) {
1463 if( !$file->userCan( File::DELETED_USER, $this->getUser() ) ) {
1464 return '<span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
1465 } else {
1466 $link = Linker::userLink( $file->getRawUser(), $file->getRawUserText() ) .
1467 Linker::userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1468 if( $file->isDeleted( File::DELETED_USER ) ) {
1469 $link = '<span class="history-deleted">' . $link . '</span>';
1470 }
1471 return $link;
1472 }
1473 }
1474
1475 /**
1476 * Fetch file upload comment if it's available to this user
1477 *
1478 * @param $file File
1479 * @return String: HTML fragment
1480 */
1481 function getFileComment( $file ) {
1482 if( !$file->userCan( File::DELETED_COMMENT, $this->getUser() ) ) {
1483 return '<span class="history-deleted"><span class="comment">' .
1484 $this->msg( 'rev-deleted-comment' )->escaped() . '</span></span>';
1485 } else {
1486 $link = Linker::commentBlock( $file->getRawDescription() );
1487 if( $file->isDeleted( File::DELETED_COMMENT ) ) {
1488 $link = '<span class="history-deleted">' . $link . '</span>';
1489 }
1490 return $link;
1491 }
1492 }
1493
1494 function undelete() {
1495 global $wgUploadMaintenance;
1496
1497 if ( $wgUploadMaintenance && $this->mTargetObj->getNamespace() == NS_FILE ) {
1498 throw new ErrorPageError( 'undelete-error', 'filedelete-maintenance' );
1499 }
1500
1501 if ( wfReadOnly() ) {
1502 throw new ReadOnlyError;
1503 }
1504
1505 $out = $this->getOutput();
1506 $archive = new PageArchive( $this->mTargetObj );
1507 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1508 $ok = $archive->undelete(
1509 $this->mTargetTimestamp,
1510 $this->mComment,
1511 $this->mFileVersions,
1512 $this->mUnsuppress,
1513 $this->getUser()
1514 );
1515
1516 if( is_array( $ok ) ) {
1517 if ( $ok[1] ) { // Undeleted file count
1518 wfRunHooks( 'FileUndeleteComplete', array(
1519 $this->mTargetObj, $this->mFileVersions,
1520 $this->getUser(), $this->mComment ) );
1521 }
1522
1523 $link = Linker::linkKnown( $this->mTargetObj );
1524 $out->addHTML( $this->msg( 'undeletedpage' )->rawParams( $link )->parse() );
1525 } else {
1526 $out->setPageTitle( $this->msg( 'undelete-error' ) );
1527 }
1528
1529 // Show revision undeletion warnings and errors
1530 $status = $archive->getRevisionStatus();
1531 if( $status && !$status->isGood() ) {
1532 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'cannotundelete', 'cannotundelete' ) . '</div>' );
1533 }
1534
1535 // Show file undeletion warnings and errors
1536 $status = $archive->getFileStatus();
1537 if( $status && !$status->isGood() ) {
1538 $out->addWikiText( '<div class="error">' . $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) . '</div>' );
1539 }
1540 }
1541 }