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