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