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