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