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