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