e0f77679f77aecc92ff85b2ff610a5c78fe2231b
[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 $rev = $archive->getRevision( $timestamp );
773
774 if( !$rev ) {
775 $wgOut->addWikiMsg( 'undeleterevision-missing' );
776 return;
777 }
778
779 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
780 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
781 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
782 return;
783 } else {
784 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
785 $wgOut->addHTML( '<br />' );
786 // and we are allowed to see...
787 }
788 }
789
790 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
791
792 $link = $skin->linkKnown(
793 $this->getTitle( $this->mTargetObj->getPrefixedDBkey() ),
794 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
795 );
796
797 if( $this->mDiff ) {
798 $previousRev = $archive->getPreviousRevision( $timestamp );
799 if( $previousRev ) {
800 $this->showDiff( $previousRev, $rev );
801 if( $wgUser->getOption( 'diffonly' ) ) {
802 return;
803 } else {
804 $wgOut->addHTML( '<hr />' );
805 }
806 } else {
807 $wgOut->addWikiMsg( 'undelete-nodiff' );
808 }
809 }
810
811 // date and time are separate parameters to facilitate localisation.
812 // $time is kept for backward compat reasons.
813 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
814 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
815 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
816 $user = $skin->revUserTools( $rev );
817
818 if( $this->mPreview ) {
819 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
820 } else {
821 $openDiv = '<div id="mw-undelete-revision">';
822 }
823 $wgOut->addHTML( $openDiv );
824
825 // Revision delete links
826 if ( !$this->mDiff ) {
827 $revdel = $this->revDeleteLink( $rev );
828 if ( $revdel ) {
829 $wgOut->addHTML( $revdel );
830 }
831 }
832
833 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
834 $wgOut->addHTML( '</div>' );
835 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
836
837 if( $this->mPreview ) {
838 // Hide [edit]s
839 $popts = $wgOut->parserOptions();
840 $popts->setEditSection( false );
841 $wgOut->parserOptions( $popts );
842 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER ), $this->mTargetObj, true );
843 }
844
845 $wgOut->addHTML(
846 Xml::element( 'textarea', array(
847 'readonly' => 'readonly',
848 'cols' => intval( $wgUser->getOption( 'cols' ) ),
849 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
850 $rev->getText( Revision::FOR_THIS_USER ) . "\n" ) .
851 Xml::openElement( 'div' ) .
852 Xml::openElement( 'form', array(
853 'method' => 'post',
854 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
855 Xml::element( 'input', array(
856 'type' => 'hidden',
857 'name' => 'target',
858 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
859 Xml::element( 'input', array(
860 'type' => 'hidden',
861 'name' => 'timestamp',
862 'value' => $timestamp ) ) .
863 Xml::element( 'input', array(
864 'type' => 'hidden',
865 'name' => 'wpEditToken',
866 'value' => $wgUser->editToken() ) ) .
867 Xml::element( 'input', array(
868 'type' => 'submit',
869 'name' => 'preview',
870 'value' => wfMsg( 'showpreview' ) ) ) .
871 Xml::element( 'input', array(
872 'name' => 'diff',
873 'type' => 'submit',
874 'value' => wfMsg( 'showdiff' ) ) ) .
875 Xml::closeElement( 'form' ) .
876 Xml::closeElement( 'div' ) );
877 }
878
879 /**
880 * Get a revision-deletion link, or disabled link, or nothing, depending
881 * on user permissions & the settings on the revision.
882 *
883 * Will use forward-compatible revision ID in the Special:RevDelete link
884 * if possible, otherwise the timestamp-based ID which may break after
885 * undeletion.
886 *
887 * @param Revision $rev
888 * @return string HTML fragment
889 */
890 function revDeleteLink( $rev ) {
891 global $wgUser;
892 $canHide = $wgUser->isAllowed( 'deleterevision' );
893 if( $canHide || ( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
894 $skin = $wgUser->getSkin();
895 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
896 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
897 } else {
898 if ( $rev->getId() ) {
899 // RevDelete links using revision ID are stable across
900 // page deletion and undeletion; use when possible.
901 $query = array(
902 'type' => 'revision',
903 'target' => $this->mTargetObj->getPrefixedDBkey(),
904 'ids' => $rev->getId()
905 );
906 } else {
907 // Older deleted entries didn't save a revision ID.
908 // We have to refer to these by timestamp, ick!
909 $query = array(
910 'type' => 'archive',
911 'target' => $this->mTargetObj->getPrefixedDBkey(),
912 'ids' => $rev->getTimestamp()
913 );
914 }
915 return $skin->revDeleteLink( $query,
916 $rev->isDeleted( File::DELETED_RESTRICTED ), $canHide );
917 }
918 } else {
919 return '';
920 }
921 }
922
923 /**
924 * Build a diff display between this and the previous either deleted
925 * or non-deleted edit.
926 *
927 * @param $previousRev Revision
928 * @param $currentRev Revision
929 * @return String: HTML
930 */
931 function showDiff( $previousRev, $currentRev ) {
932 global $wgOut;
933
934 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
935 $diffEngine->showDiffStyle();
936 $wgOut->addHTML(
937 "<div>" .
938 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
939 "<col class='diff-marker' />" .
940 "<col class='diff-content' />" .
941 "<col class='diff-marker' />" .
942 "<col class='diff-content' />" .
943 "<tr>" .
944 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
945 $this->diffHeader( $previousRev, 'o' ) .
946 "</td>\n" .
947 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
948 $this->diffHeader( $currentRev, 'n' ) .
949 "</td>\n" .
950 "</tr>" .
951 $diffEngine->generateDiffBody(
952 $previousRev->getText(), $currentRev->getText() ) .
953 "</table>" .
954 "</div>\n"
955 );
956 }
957
958 /**
959 * @param $rev Revision
960 * @param $prefix
961 * @return string
962 */
963 private function diffHeader( $rev, $prefix ) {
964 global $wgUser, $wgLang;
965 $sk = $wgUser->getSkin();
966 $isDeleted = !( $rev->getId() && $rev->getTitle() );
967 if( $isDeleted ) {
968 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
969 $targetPage = $this->getTitle();
970 $targetQuery = array(
971 'target' => $this->mTargetObj->getPrefixedText(),
972 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
973 );
974 } else {
975 /// @todo Fixme getId() may return non-zero for deleted revs...
976 $targetPage = $rev->getTitle();
977 $targetQuery = array( 'oldid' => $rev->getId() );
978 }
979 // Add show/hide deletion links if available
980 $del .= $this->revDeleteLink( $rev );
981 return
982 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
983 $sk->link(
984 $targetPage,
985 wfMsgHtml(
986 'revisionasof',
987 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
988 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
989 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
990 ),
991 array(),
992 $targetQuery
993 ) .
994 '</strong></div>' .
995 '<div id="mw-diff-'.$prefix.'title2">' .
996 $sk->revUserTools( $rev ) . '<br />' .
997 '</div>' .
998 '<div id="mw-diff-'.$prefix.'title3">' .
999 $sk->revComment( $rev ) . $del . '<br />' .
1000 '</div>';
1001 }
1002
1003 /**
1004 * Show a form confirming whether a tokenless user really wants to see a file
1005 */
1006 private function showFileConfirmationForm( $key ) {
1007 global $wgOut, $wgUser, $wgLang;
1008 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFile );
1009 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
1010 $this->mTargetObj->getText(),
1011 $wgLang->date( $file->getTimestamp() ),
1012 $wgLang->time( $file->getTimestamp() ) );
1013 $wgOut->addHTML(
1014 Xml::openElement( 'form', array(
1015 'method' => 'POST',
1016 'action' => $this->getTitle()->getLocalURL(
1017 'target=' . urlencode( $this->mTarget ) .
1018 '&file=' . urlencode( $key ) .
1019 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1020 )
1021 ) .
1022 Xml::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1023 '</form>'
1024 );
1025 }
1026
1027 /**
1028 * Show a deleted file version requested by the visitor.
1029 */
1030 private function showFile( $key ) {
1031 global $wgOut, $wgRequest;
1032 $wgOut->disable();
1033
1034 # We mustn't allow the output to be Squid cached, otherwise
1035 # if an admin previews a deleted image, and it's cached, then
1036 # a user without appropriate permissions can toddle off and
1037 # nab the image, and Squid will serve it
1038 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1039 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1040 $wgRequest->response()->header( 'Pragma: no-cache' );
1041
1042 global $IP;
1043 require_once( "$IP/includes/StreamFile.php" );
1044 $repo = RepoGroup::singleton()->getLocalRepo();
1045 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1046 wfStreamFile( $path );
1047 }
1048
1049 private function showHistory() {
1050 global $wgUser, $wgOut;
1051
1052 $sk = $wgUser->getSkin();
1053 if( $this->mAllowed ) {
1054 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
1055 } else {
1056 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1057 }
1058
1059 $wgOut->wrapWikiMsg(
1060 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1061 array( 'undeletepagetitle', $this->mTargetObj->getPrefixedText() )
1062 );
1063
1064 $archive = new PageArchive( $this->mTargetObj );
1065 /*
1066 $text = $archive->getLastRevisionText();
1067 if( is_null( $text ) ) {
1068 $wgOut->addWikiMsg( 'nohistory' );
1069 return;
1070 }
1071 */
1072 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1073 if ( $this->mAllowed ) {
1074 $wgOut->addWikiMsg( 'undeletehistory' );
1075 $wgOut->addWikiMsg( 'undeleterevdel' );
1076 } else {
1077 $wgOut->addWikiMsg( 'undeletehistorynoadmin' );
1078 }
1079 $wgOut->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 $wgOut->addHTML( $top );
1113 }
1114
1115 # Show relevant lines from the deletion log:
1116 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) . "\n" );
1117 LogEventsList::showLogExtract( $wgOut, 'delete', $this->mTargetObj->getPrefixedText() );
1118 # Show relevant lines from the suppression log:
1119 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1120 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'suppress' ) ) . "\n" );
1121 LogEventsList::showLogExtract( $wgOut, 'suppress', $this->mTargetObj->getPrefixedText() );
1122 }
1123
1124 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1125 # Format the user-visible controls (comment field, submission button)
1126 # in a nice little table
1127 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1128 $unsuppressBox =
1129 "<tr>
1130 <td>&#160;</td>
1131 <td class='mw-input'>" .
1132 Xml::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1133 'mw-undelete-unsuppress', $this->mUnsuppress ).
1134 "</td>
1135 </tr>";
1136 } else {
1137 $unsuppressBox = '';
1138 }
1139 $table =
1140 Xml::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1141 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1142 "<tr>
1143 <td colspan='2' class='mw-undelete-extrahelp'>" .
1144 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1145 "</td>
1146 </tr>
1147 <tr>
1148 <td class='mw-label'>" .
1149 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1150 "</td>
1151 <td class='mw-input'>" .
1152 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
1153 "</td>
1154 </tr>
1155 <tr>
1156 <td>&#160;</td>
1157 <td class='mw-submit'>" .
1158 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1159 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1160 Xml::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1161 "</td>
1162 </tr>" .
1163 $unsuppressBox .
1164 Xml::closeElement( 'table' ) .
1165 Xml::closeElement( 'fieldset' );
1166
1167 $wgOut->addHTML( $table );
1168 }
1169
1170 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1171
1172 if( $haveRevisions ) {
1173 # The page's stored (deleted) history:
1174 $wgOut->addHTML( '<ul>' );
1175 $remaining = $revisions->numRows();
1176 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1177
1178 foreach ( $revisions as $row ) {
1179 $remaining--;
1180 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1181 }
1182 $revisions->free();
1183 $wgOut->addHTML( '</ul>' );
1184 } else {
1185 $wgOut->addWikiMsg( 'nohistory' );
1186 }
1187
1188 if( $haveFiles ) {
1189 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1190 $wgOut->addHTML( '<ul>' );
1191 foreach ( $files as $row ) {
1192 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1193 }
1194 $files->free();
1195 $wgOut->addHTML( '</ul>' );
1196 }
1197
1198 if ( $this->mAllowed ) {
1199 # Slip in the hidden controls here
1200 $misc = Html::hidden( 'target', $this->mTarget );
1201 $misc .= Html::hidden( 'wpEditToken', $wgUser->editToken() );
1202 $misc .= Xml::closeElement( 'form' );
1203 $wgOut->addHTML( $misc );
1204 }
1205
1206 return true;
1207 }
1208
1209 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1210 global $wgLang;
1211
1212 $rev = Revision::newFromArchiveRow( $row,
1213 array( 'page' => $this->mTargetObj->getArticleId() ) );
1214 $stxt = '';
1215 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1216 // Build checkboxen...
1217 if( $this->mAllowed ) {
1218 if( $this->mInvert ) {
1219 if( in_array( $ts, $this->mTargetTimestamp ) ) {
1220 $checkBox = Xml::check( "ts$ts" );
1221 } else {
1222 $checkBox = Xml::check( "ts$ts", true );
1223 }
1224 } else {
1225 $checkBox = Xml::check( "ts$ts" );
1226 }
1227 } else {
1228 $checkBox = '';
1229 }
1230 // Build page & diff links...
1231 if( $this->mCanView ) {
1232 $titleObj = $this->getTitle();
1233 # Last link
1234 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
1235 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1236 $last = wfMsgHtml( 'diff' );
1237 } else if( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1238 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1239 $last = $sk->linkKnown(
1240 $titleObj,
1241 wfMsgHtml( 'diff' ),
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, $sk );
1251 $last = wfMsgHtml( 'diff' );
1252 }
1253 } else {
1254 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1255 $last = wfMsgHtml( 'diff' );
1256 }
1257 // User links
1258 $userLink = $sk->revUserTools( $rev );
1259 // Revision text size
1260 $size = $row->ar_len;
1261 if( !is_null( $size ) ) {
1262 $stxt = $sk->formatRevisionSize( $size );
1263 }
1264 // Edit summary
1265 $comment = $sk->revComment( $rev );
1266 // Revision delete links
1267 $revdlink = $this->revDeleteLink( $rev );
1268 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1269 }
1270
1271 private function formatFileRow( $row, $sk ) {
1272 global $wgUser, $wgLang;
1273
1274 $file = ArchivedFile::newFromRow( $row );
1275
1276 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1277 if( $this->mAllowed && $row->fa_storage_key ) {
1278 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1279 $key = urlencode( $row->fa_storage_key );
1280 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1281 } else {
1282 $checkBox = '';
1283 $pageLink = $wgLang->timeanddate( $ts, true );
1284 }
1285 $userLink = $this->getFileUser( $file, $sk );
1286 $data =
1287 wfMsg( 'widthheight',
1288 $wgLang->formatNum( $row->fa_width ),
1289 $wgLang->formatNum( $row->fa_height ) ) .
1290 ' (' .
1291 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
1292 ')';
1293 $data = htmlspecialchars( $data );
1294 $comment = $this->getFileComment( $file, $sk );
1295 // Add show/hide deletion links if available
1296 $canHide = $wgUser->isAllowed( 'deleterevision' );
1297 if( $canHide || ( $file->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
1298 if( !$file->userCan( File::DELETED_RESTRICTED ) ) {
1299 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1300 } else {
1301 $query = array(
1302 'type' => 'filearchive',
1303 'target' => $this->mTargetObj->getPrefixedDBkey(),
1304 'ids' => $row->fa_id
1305 );
1306 $revdlink = $sk->revDeleteLink( $query,
1307 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1308 }
1309 } else {
1310 $revdlink = '';
1311 }
1312 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1313 }
1314
1315 /**
1316 * Fetch revision text link if it's available to all users
1317 *
1318 * @param $rev Revision
1319 * @param $sk Skin
1320 * @return string
1321 */
1322 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1323 global $wgLang;
1324
1325 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1326
1327 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
1328 return '<span class="history-deleted">' . $time . '</span>';
1329 } else {
1330 $link = $sk->linkKnown(
1331 $titleObj,
1332 $time,
1333 array(),
1334 array(
1335 'target' => $this->mTargetObj->getPrefixedText(),
1336 'timestamp' => $ts
1337 )
1338 );
1339 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1340 $link = '<span class="history-deleted">' . $link . '</span>';
1341 }
1342 return $link;
1343 }
1344 }
1345
1346 /**
1347 * Fetch image view link if it's available to all users
1348 *
1349 * @param $file File
1350 * @param $sk Skin
1351 * @return String: HTML fragment
1352 */
1353 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1354 global $wgLang, $wgUser;
1355
1356 if( !$file->userCan( File::DELETED_FILE ) ) {
1357 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1358 } else {
1359 $link = $sk->linkKnown(
1360 $titleObj,
1361 $wgLang->timeanddate( $ts, true ),
1362 array(),
1363 array(
1364 'target' => $this->mTargetObj->getPrefixedText(),
1365 'file' => $key,
1366 'token' => $wgUser->editToken( $key )
1367 )
1368 );
1369 if( $file->isDeleted( File::DELETED_FILE ) ) {
1370 $link = '<span class="history-deleted">' . $link . '</span>';
1371 }
1372 return $link;
1373 }
1374 }
1375
1376 /**
1377 * Fetch file's user id if it's available to this user
1378 *
1379 * @param $file File
1380 * @param $sk Skin
1381 * @return String: HTML fragment
1382 */
1383 function getFileUser( $file, $sk ) {
1384 if( !$file->userCan( File::DELETED_USER ) ) {
1385 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1386 } else {
1387 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1388 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1389 if( $file->isDeleted( File::DELETED_USER ) ) {
1390 $link = '<span class="history-deleted">' . $link . '</span>';
1391 }
1392 return $link;
1393 }
1394 }
1395
1396 /**
1397 * Fetch file upload comment if it's available to this user
1398 *
1399 * @param $file File
1400 * @param $sk Skin
1401 * @return String: HTML fragment
1402 */
1403 function getFileComment( $file, $sk ) {
1404 if( !$file->userCan( File::DELETED_COMMENT ) ) {
1405 return '<span class="history-deleted"><span class="comment">' .
1406 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1407 } else {
1408 $link = $sk->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 $wgOut, $wgUser;
1418 if ( wfReadOnly() ) {
1419 $wgOut->readOnlyPage();
1420 return;
1421 }
1422 if( !is_null( $this->mTargetObj ) ) {
1423 $archive = new PageArchive( $this->mTargetObj );
1424 $ok = $archive->undelete(
1425 $this->mTargetTimestamp,
1426 $this->mComment,
1427 $this->mFileVersions,
1428 $this->mUnsuppress );
1429
1430 if( is_array( $ok ) ) {
1431 if ( $ok[1] ) { // Undeleted file count
1432 wfRunHooks( 'FileUndeleteComplete', array(
1433 $this->mTargetObj, $this->mFileVersions,
1434 $wgUser, $this->mComment ) );
1435 }
1436
1437 $skin = $wgUser->getSkin();
1438 $link = $skin->linkKnown( $this->mTargetObj );
1439 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1440 } else {
1441 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );
1442 $wgOut->addWikiMsg( 'undeleterevdel' );
1443 }
1444
1445 // Show file deletion warnings and errors
1446 $status = $archive->getFileStatus();
1447 if( $status && !$status->isGood() ) {
1448 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1449 }
1450 } else {
1451 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );
1452 }
1453 return false;
1454 }
1455 }