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