Removal of unused globals
[lhc/web/wiklou.git] / includes / specials / SpecialUndelete.php
1 <?php
2 /**
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 */
19
20 /**
21 * Used to show archived pages and eventually restore them.
22 * @ingroup SpecialPage
23 */
24 class PageArchive {
25 protected $title;
26 var $fileStatus;
27
28 function __construct( $title ) {
29 if( is_null( $title ) ) {
30 throw new MWException( __METHOD__ . ' given a null title.' );
31 }
32 $this->title = $title;
33 }
34
35 /**
36 * List all deleted pages recorded in the archive table. Returns result
37 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
38 * namespace/title.
39 *
40 * @return ResultWrapper
41 */
42 public static function listAllPages() {
43 $dbr = wfGetDB( DB_SLAVE );
44 return self::listPages( $dbr, '' );
45 }
46
47 /**
48 * List deleted pages recorded in the archive table matching the
49 * given title prefix.
50 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
51 *
52 * @param $prefix String: title prefix
53 * @return ResultWrapper
54 */
55 public static function listPagesByPrefix( $prefix ) {
56 $dbr = wfGetDB( DB_SLAVE );
57
58 $title = Title::newFromText( $prefix );
59 if( $title ) {
60 $ns = $title->getNamespace();
61 $prefix = $title->getDBkey();
62 } else {
63 // Prolly won't work too good
64 // @todo handle bare namespace names cleanly?
65 $ns = 0;
66 }
67 $conds = array(
68 'ar_namespace' => $ns,
69 'ar_title' . $dbr->buildLike( $prefix, $dbr->anyString() ),
70 );
71 return self::listPages( $dbr, $conds );
72 }
73
74 protected static function listPages( $dbr, $condition ) {
75 return $dbr->resultObject(
76 $dbr->select(
77 array( 'archive' ),
78 array(
79 'ar_namespace',
80 'ar_title',
81 'COUNT(*) AS count'
82 ),
83 $condition,
84 __METHOD__,
85 array(
86 'GROUP BY' => 'ar_namespace,ar_title',
87 'ORDER BY' => 'ar_namespace,ar_title',
88 'LIMIT' => 100,
89 )
90 )
91 );
92 }
93
94 /**
95 * List the revisions of the given page. Returns result wrapper with
96 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
97 *
98 * @return ResultWrapper
99 */
100 function listRevisions() {
101 $dbr = wfGetDB( DB_SLAVE );
102 $res = $dbr->select( 'archive',
103 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len', 'ar_deleted' ),
104 array( 'ar_namespace' => $this->title->getNamespace(),
105 'ar_title' => $this->title->getDBkey() ),
106 'PageArchive::listRevisions',
107 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
108 $ret = $dbr->resultObject( $res );
109 return $ret;
110 }
111
112 /**
113 * List the deleted file revisions for this page, if it's a file page.
114 * Returns a result wrapper with various filearchive fields, or null
115 * if not a file page.
116 *
117 * @return ResultWrapper
118 * @todo Does this belong in Image for fuller encapsulation?
119 */
120 function listFiles() {
121 if( $this->title->getNamespace() == NS_FILE ) {
122 $dbr = wfGetDB( DB_SLAVE );
123 $res = $dbr->select( 'filearchive',
124 array(
125 'fa_id',
126 'fa_name',
127 'fa_archive_name',
128 'fa_storage_key',
129 'fa_storage_group',
130 'fa_size',
131 'fa_width',
132 'fa_height',
133 'fa_bits',
134 'fa_metadata',
135 'fa_media_type',
136 'fa_major_mime',
137 'fa_minor_mime',
138 'fa_description',
139 'fa_user',
140 'fa_user_text',
141 'fa_timestamp',
142 'fa_deleted' ),
143 array( 'fa_name' => $this->title->getDBkey() ),
144 __METHOD__,
145 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
146 $ret = $dbr->resultObject( $res );
147 return $ret;
148 }
149 return null;
150 }
151
152 /**
153 * Fetch (and decompress if necessary) the stored text for the deleted
154 * revision of the page with the given timestamp.
155 *
156 * @param $timestamp String
157 * @return String
158 * @deprecated Use getRevision() for more flexible information
159 */
160 function getRevisionText( $timestamp ) {
161 $rev = $this->getRevision( $timestamp );
162 return $rev ? $rev->getText() : null;
163 }
164
165 /**
166 * Return a Revision object containing data for the deleted revision.
167 * Note that the result *may* or *may not* have a null page ID.
168 *
169 * @param $timestamp String
170 * @return Revision
171 */
172 function getRevision( $timestamp ) {
173 $dbr = wfGetDB( DB_SLAVE );
174 $row = $dbr->selectRow( 'archive',
175 array(
176 'ar_rev_id',
177 'ar_text',
178 'ar_comment',
179 'ar_user',
180 'ar_user_text',
181 'ar_timestamp',
182 'ar_minor_edit',
183 'ar_flags',
184 'ar_text_id',
185 'ar_deleted',
186 'ar_len' ),
187 array( 'ar_namespace' => $this->title->getNamespace(),
188 'ar_title' => $this->title->getDBkey(),
189 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
190 __METHOD__ );
191 if( $row ) {
192 return Revision::newFromArchiveRow( $row, array( 'page' => $this->title->getArticleId() ) );
193 } else {
194 return null;
195 }
196 }
197
198 /**
199 * Return the most-previous revision, either live or deleted, against
200 * the deleted revision given by timestamp.
201 *
202 * May produce unexpected results in case of history merges or other
203 * unusual time issues.
204 *
205 * @param $timestamp String
206 * @return Revision or null
207 */
208 function getPreviousRevision( $timestamp ) {
209 $dbr = wfGetDB( DB_SLAVE );
210
211 // Check the previous deleted revision...
212 $row = $dbr->selectRow( 'archive',
213 'ar_timestamp',
214 array( 'ar_namespace' => $this->title->getNamespace(),
215 'ar_title' => $this->title->getDBkey(),
216 'ar_timestamp < ' .
217 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
218 __METHOD__,
219 array(
220 'ORDER BY' => 'ar_timestamp DESC',
221 'LIMIT' => 1 ) );
222 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
223
224 $row = $dbr->selectRow( array( 'page', 'revision' ),
225 array( 'rev_id', 'rev_timestamp' ),
226 array(
227 'page_namespace' => $this->title->getNamespace(),
228 'page_title' => $this->title->getDBkey(),
229 'page_id = rev_page',
230 'rev_timestamp < ' .
231 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
232 __METHOD__,
233 array(
234 'ORDER BY' => 'rev_timestamp DESC',
235 'LIMIT' => 1 ) );
236 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
237 $prevLiveId = $row ? intval( $row->rev_id ) : null;
238
239 if( $prevLive && $prevLive > $prevDeleted ) {
240 // Most prior revision was live
241 return Revision::newFromId( $prevLiveId );
242 } elseif( $prevDeleted ) {
243 // Most prior revision was deleted
244 return $this->getRevision( $prevDeleted );
245 } else {
246 // No prior revision on this page.
247 return null;
248 }
249 }
250
251 /**
252 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
253 *
254 * @param $row Object: database row
255 * @return Revision
256 */
257 function getTextFromRow( $row ) {
258 if( is_null( $row->ar_text_id ) ) {
259 // An old row from MediaWiki 1.4 or previous.
260 // Text is embedded in this row in classic compression format.
261 return Revision::getRevisionText( $row, "ar_" );
262 } else {
263 // New-style: keyed to the text storage backend.
264 $dbr = wfGetDB( DB_SLAVE );
265 $text = $dbr->selectRow( 'text',
266 array( 'old_text', 'old_flags' ),
267 array( 'old_id' => $row->ar_text_id ),
268 __METHOD__ );
269 return Revision::getRevisionText( $text );
270 }
271 }
272
273
274 /**
275 * Fetch (and decompress if necessary) the stored text of the most
276 * recently edited deleted revision of the page.
277 *
278 * If there are no archived revisions for the page, returns NULL.
279 *
280 * @return String
281 */
282 function getLastRevisionText() {
283 $dbr = wfGetDB( DB_SLAVE );
284 $row = $dbr->selectRow( 'archive',
285 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
286 array( 'ar_namespace' => $this->title->getNamespace(),
287 'ar_title' => $this->title->getDBkey() ),
288 __METHOD__,
289 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
290 if( $row ) {
291 return $this->getTextFromRow( $row );
292 } else {
293 return null;
294 }
295 }
296
297 /**
298 * Quick check if any archived revisions are present for the page.
299 *
300 * @return Boolean
301 */
302 function isDeleted() {
303 $dbr = wfGetDB( DB_SLAVE );
304 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
305 array( 'ar_namespace' => $this->title->getNamespace(),
306 'ar_title' => $this->title->getDBkey() ) );
307 return ($n > 0);
308 }
309
310 /**
311 * Restore the given (or all) text and file revisions for the page.
312 * Once restored, the items will be removed from the archive tables.
313 * The deletion log will be updated with an undeletion notice.
314 *
315 * @param $timestamps Array: pass an empty array to restore all revisions, otherwise list the ones to undelete.
316 * @param $comment String
317 * @param $fileVersions Array
318 * @param $unsuppress Boolean
319 *
320 * @return array(number of file revisions restored, number of image revisions restored, log message)
321 * on success, false on failure
322 */
323 function undelete( $timestamps, $comment = '', $fileVersions = array(), $unsuppress = false ) {
324 // If both the set of text revisions and file revisions are empty,
325 // restore everything. Otherwise, just restore the requested items.
326 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
327
328 $restoreText = $restoreAll || !empty( $timestamps );
329 $restoreFiles = $restoreAll || !empty( $fileVersions );
330
331 if( $restoreFiles && $this->title->getNamespace() == NS_FILE ) {
332 $img = wfLocalFile( $this->title );
333 $this->fileStatus = $img->restore( $fileVersions, $unsuppress );
334 $filesRestored = $this->fileStatus->successCount;
335 } else {
336 $filesRestored = 0;
337 }
338
339 if( $restoreText ) {
340 $textRestored = $this->undeleteRevisions( $timestamps, $unsuppress, $comment );
341 if($textRestored === false) // It must be one of UNDELETE_*
342 return false;
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 while( $row = $ret->fetchObject() ) {
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 if( $revision ) {
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 } else {
542 // Revision couldn't be created. This is very weird
543 return self::UNDELETE_UNKNOWNERR;
544 }
545
546 return $restored;
547 }
548
549 function getFileStatus() { return $this->fileStatus; }
550 }
551
552 /**
553 * Special page allowing users with the appropriate permissions to view
554 * and restore deleted content.
555 *
556 * @ingroup SpecialPage
557 */
558 class UndeleteForm extends SpecialPage {
559 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mTargetObj;
560 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
561
562 function __construct( $request = null ) {
563 parent::__construct( 'Undelete', 'deletedhistory' );
564
565 if ( $request === null ) {
566 global $wgRequest;
567 $this->mRequest = $wgRequest;
568 } else {
569 $this->mRequest = $request;
570 }
571 }
572
573 function loadRequest() {
574 global $wgUser;
575 $this->mAction = $this->mRequest->getVal( 'action' );
576 $this->mTarget = $this->mRequest->getVal( 'target' );
577 $this->mSearchPrefix = $this->mRequest->getText( 'prefix' );
578 $time = $this->mRequest->getVal( 'timestamp' );
579 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
580 $this->mFile = $this->mRequest->getVal( 'file' );
581
582 $posted = $this->mRequest->wasPosted() &&
583 $wgUser->matchEditToken( $this->mRequest->getVal( 'wpEditToken' ) );
584 $this->mRestore = $this->mRequest->getCheck( 'restore' ) && $posted;
585 $this->mInvert = $this->mRequest->getCheck( 'invert' ) && $posted;
586 $this->mPreview = $this->mRequest->getCheck( 'preview' ) && $posted;
587 $this->mDiff = $this->mRequest->getCheck( 'diff' );
588 $this->mComment = $this->mRequest->getText( 'wpComment' );
589 $this->mUnsuppress = $this->mRequest->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
590 $this->mToken = $this->mRequest->getVal( 'token' );
591
592 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
593 $this->mAllowed = true; // user can restore
594 $this->mCanView = true; // user can view content
595 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
596 $this->mAllowed = false; // user cannot restore
597 $this->mCanView = true; // user can view content
598 } else { // user can only view the list of revisions
599 $this->mAllowed = false;
600 $this->mCanView = false;
601 $this->mTimestamp = '';
602 $this->mRestore = false;
603 }
604
605 if( $this->mRestore || $this->mInvert ) {
606 $timestamps = array();
607 $this->mFileVersions = array();
608 foreach( $_REQUEST as $key => $val ) {
609 $matches = array();
610 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
611 array_push( $timestamps, $matches[1] );
612 }
613
614 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
615 $this->mFileVersions[] = intval( $matches[1] );
616 }
617 }
618 rsort( $timestamps );
619 $this->mTargetTimestamp = $timestamps;
620 }
621 }
622
623 function execute( $par ) {
624 global $wgOut, $wgUser;
625
626 $this->setHeaders();
627 if ( !$this->userCanExecute( $wgUser ) ) {
628 $this->displayRestrictionError();
629 return;
630 }
631 $this->outputHeader();
632
633 $this->loadRequest();
634
635 if ( $this->mAllowed ) {
636 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
637 } else {
638 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
639 }
640
641 if( $par != '' ) {
642 $this->mTarget = $par;
643 }
644 if ( $this->mTarget !== '' ) {
645 $this->mTargetObj = Title::newFromURL( $this->mTarget );
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 Xml::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 while( $row = $result->fetchObject() ) {
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
815 // Revision delete links
816 $canHide = $wgUser->isAllowed( 'deleterevision' );
817 if( $this->mDiff ) {
818 $revdlink = ''; // diffs already have revision delete links
819 } else if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
820 if( !$rev->userCan(Revision::DELETED_RESTRICTED ) ) {
821 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
822 } else {
823 $query = array(
824 'type' => 'archive',
825 'target' => $this->mTargetObj->getPrefixedDBkey(),
826 'ids' => $rev->getTimestamp()
827 );
828 $revdlink = $skin->revDeleteLink( $query,
829 $rev->isDeleted( File::DELETED_RESTRICTED ), $canHide );
830 }
831 } else {
832 $revdlink = '';
833 }
834
835 $wgOut->addHTML( $openDiv . $revdlink . wfMsgWikiHtml( 'undelete-revision', $link, $time, $user, $d, $t ) . '</div>' );
836 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
837
838 if( $this->mPreview ) {
839 //Hide [edit]s
840 $popts = $wgOut->parserOptions();
841 $popts->setEditSection( false );
842 $wgOut->parserOptions( $popts );
843 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER ), $this->mTargetObj, true );
844 }
845
846 $wgOut->addHTML(
847 Xml::element( 'textarea', array(
848 'readonly' => 'readonly',
849 'cols' => intval( $wgUser->getOption( 'cols' ) ),
850 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
851 $rev->getText( Revision::FOR_THIS_USER ) . "\n" ) .
852 Xml::openElement( 'div' ) .
853 Xml::openElement( 'form', array(
854 'method' => 'post',
855 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
856 Xml::element( 'input', array(
857 'type' => 'hidden',
858 'name' => 'target',
859 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
860 Xml::element( 'input', array(
861 'type' => 'hidden',
862 'name' => 'timestamp',
863 'value' => $timestamp ) ) .
864 Xml::element( 'input', array(
865 'type' => 'hidden',
866 'name' => 'wpEditToken',
867 'value' => $wgUser->editToken() ) ) .
868 Xml::element( 'input', array(
869 'type' => 'submit',
870 'name' => 'preview',
871 'value' => wfMsg( 'showpreview' ) ) ) .
872 Xml::element( 'input', array(
873 'name' => 'diff',
874 'type' => 'submit',
875 'value' => wfMsg( 'showdiff' ) ) ) .
876 Xml::closeElement( 'form' ) .
877 Xml::closeElement( 'div' ) );
878 }
879
880 /**
881 * Build a diff display between this and the previous either deleted
882 * or non-deleted edit.
883 *
884 * @param $previousRev Revision
885 * @param $currentRev Revision
886 * @return String: HTML
887 */
888 function showDiff( $previousRev, $currentRev ) {
889 global $wgOut;
890
891 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
892 $diffEngine->showDiffStyle();
893 $wgOut->addHTML(
894 "<div>" .
895 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
896 "<col class='diff-marker' />" .
897 "<col class='diff-content' />" .
898 "<col class='diff-marker' />" .
899 "<col class='diff-content' />" .
900 "<tr>" .
901 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
902 $this->diffHeader( $previousRev, 'o' ) .
903 "</td>\n" .
904 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
905 $this->diffHeader( $currentRev, 'n' ) .
906 "</td>\n" .
907 "</tr>" .
908 $diffEngine->generateDiffBody(
909 $previousRev->getText(), $currentRev->getText() ) .
910 "</table>" .
911 "</div>\n"
912 );
913 }
914
915 private function diffHeader( $rev, $prefix ) {
916 global $wgUser, $wgLang;
917 $sk = $wgUser->getSkin();
918 $isDeleted = !( $rev->getId() && $rev->getTitle() );
919 if( $isDeleted ) {
920 /// @todo Fixme: $rev->getTitle() is null for deleted revs...?
921 $targetPage = $this->getTitle();
922 $targetQuery = array(
923 'target' => $this->mTargetObj->getPrefixedText(),
924 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
925 );
926 } else {
927 /// @todo Fixme getId() may return non-zero for deleted revs...
928 $targetPage = $rev->getTitle();
929 $targetQuery = array( 'oldid' => $rev->getId() );
930 }
931 // Add show/hide deletion links if available
932 $canHide = $wgUser->isAllowed( 'deleterevision' );
933 if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
934 $del = ' ';
935 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
936 $del .= $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
937 } else {
938 $query = array(
939 'type' => 'archive',
940 'target' => $this->mTargetObj->getPrefixedDbkey(),
941 'ids' => $rev->getTimestamp()
942 );
943 $del .= $sk->revDeleteLink( $query,
944 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
945 }
946 } else {
947 $del = '';
948 }
949 return
950 '<div id="mw-diff-'.$prefix.'title1"><strong>' .
951 $sk->link(
952 $targetPage,
953 wfMsgHtml(
954 'revisionasof',
955 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
956 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
957 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
958 ),
959 array(),
960 $targetQuery
961 ) .
962 '</strong></div>' .
963 '<div id="mw-diff-'.$prefix.'title2">' .
964 $sk->revUserTools( $rev ) . '<br />' .
965 '</div>' .
966 '<div id="mw-diff-'.$prefix.'title3">' .
967 $sk->revComment( $rev ) . $del . '<br />' .
968 '</div>';
969 }
970
971 /**
972 * Show a form confirming whether a tokenless user really wants to see a file
973 */
974 private function showFileConfirmationForm( $key ) {
975 global $wgOut, $wgUser, $wgLang;
976 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFile );
977 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
978 $this->mTargetObj->getText(),
979 $wgLang->date( $file->getTimestamp() ),
980 $wgLang->time( $file->getTimestamp() ) );
981 $wgOut->addHTML(
982 Xml::openElement( 'form', array(
983 'method' => 'POST',
984 'action' => $this->getTitle()->getLocalUrl(
985 'target=' . urlencode( $this->mTarget ) .
986 '&file=' . urlencode( $key ) .
987 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
988 )
989 ) .
990 Xml::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
991 '</form>'
992 );
993 }
994
995 /**
996 * Show a deleted file version requested by the visitor.
997 */
998 private function showFile( $key ) {
999 global $wgOut, $wgRequest;
1000 $wgOut->disable();
1001
1002 # We mustn't allow the output to be Squid cached, otherwise
1003 # if an admin previews a deleted image, and it's cached, then
1004 # a user without appropriate permissions can toddle off and
1005 # nab the image, and Squid will serve it
1006 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1007 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1008 $wgRequest->response()->header( 'Pragma: no-cache' );
1009
1010 global $IP;
1011 require_once( "$IP/includes/StreamFile.php" );
1012 $repo = RepoGroup::singleton()->getLocalRepo();
1013 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1014 wfStreamFile( $path );
1015 }
1016
1017 private function showHistory( ) {
1018 global $wgUser, $wgOut;
1019
1020 $sk = $wgUser->getSkin();
1021 if( $this->mAllowed ) {
1022 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
1023 } else {
1024 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
1025 }
1026
1027 $wgOut->wrapWikiMsg( "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n", array ( 'undeletepagetitle', $this->mTargetObj->getPrefixedText() ) );
1028
1029 $archive = new PageArchive( $this->mTargetObj );
1030 /*
1031 $text = $archive->getLastRevisionText();
1032 if( is_null( $text ) ) {
1033 $wgOut->addWikiMsg( "nohistory" );
1034 return;
1035 }
1036 */
1037 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1038 if ( $this->mAllowed ) {
1039 $wgOut->addWikiMsg( "undeletehistory" );
1040 $wgOut->addWikiMsg( "undeleterevdel" );
1041 } else {
1042 $wgOut->addWikiMsg( "undeletehistorynoadmin" );
1043 }
1044 $wgOut->addHTML( '</div>' );
1045
1046 # List all stored revisions
1047 $revisions = $archive->listRevisions();
1048 $files = $archive->listFiles();
1049
1050 $haveRevisions = $revisions && $revisions->numRows() > 0;
1051 $haveFiles = $files && $files->numRows() > 0;
1052
1053 # Batch existence check on user and talk pages
1054 if( $haveRevisions ) {
1055 $batch = new LinkBatch();
1056 while( $row = $revisions->fetchObject() ) {
1057 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1058 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1059 }
1060 $batch->execute();
1061 $revisions->seek( 0 );
1062 }
1063 if( $haveFiles ) {
1064 $batch = new LinkBatch();
1065 while( $row = $files->fetchObject() ) {
1066 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1067 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1068 }
1069 $batch->execute();
1070 $files->seek( 0 );
1071 }
1072
1073 if ( $this->mAllowed ) {
1074 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1075 # Start the form here
1076 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1077 $wgOut->addHTML( $top );
1078 }
1079
1080 # Show relevant lines from the deletion log:
1081 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) . "\n" );
1082 LogEventsList::showLogExtract( $wgOut, 'delete', $this->mTargetObj->getPrefixedText() );
1083 # Show relevant lines from the suppression log:
1084 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1085 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'suppress' ) ) . "\n" );
1086 LogEventsList::showLogExtract( $wgOut, 'suppress', $this->mTargetObj->getPrefixedText() );
1087 }
1088
1089 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1090 # Format the user-visible controls (comment field, submission button)
1091 # in a nice little table
1092 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1093 $unsuppressBox =
1094 "<tr>
1095 <td>&#160;</td>
1096 <td class='mw-input'>" .
1097 Xml::checkLabel( wfMsg('revdelete-unsuppress'), 'wpUnsuppress',
1098 'mw-undelete-unsuppress', $this->mUnsuppress ).
1099 "</td>
1100 </tr>";
1101 } else {
1102 $unsuppressBox = "";
1103 }
1104 $table =
1105 Xml::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1106 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1107 "<tr>
1108 <td colspan='2' class='mw-undelete-extrahelp'>" .
1109 wfMsgWikiHtml( 'undeleteextrahelp' ) .
1110 "</td>
1111 </tr>
1112 <tr>
1113 <td class='mw-label'>" .
1114 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1115 "</td>
1116 <td class='mw-input'>" .
1117 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
1118 "</td>
1119 </tr>
1120 <tr>
1121 <td>&#160;</td>
1122 <td class='mw-submit'>" .
1123 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1124 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) . ' ' .
1125 Xml::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1126 "</td>
1127 </tr>" .
1128 $unsuppressBox .
1129 Xml::closeElement( 'table' ) .
1130 Xml::closeElement( 'fieldset' );
1131
1132 $wgOut->addHTML( $table );
1133 }
1134
1135 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1136
1137 if( $haveRevisions ) {
1138 # The page's stored (deleted) history:
1139 $wgOut->addHTML("<ul>");
1140 $target = urlencode( $this->mTarget );
1141 $remaining = $revisions->numRows();
1142 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1143
1144 while( $row = $revisions->fetchObject() ) {
1145 $remaining--;
1146 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1147 }
1148 $revisions->free();
1149 $wgOut->addHTML("</ul>");
1150 } else {
1151 $wgOut->addWikiMsg( "nohistory" );
1152 }
1153
1154 if( $haveFiles ) {
1155 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1156 $wgOut->addHTML( "<ul>" );
1157 while( $row = $files->fetchObject() ) {
1158 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1159 }
1160 $files->free();
1161 $wgOut->addHTML( "</ul>" );
1162 }
1163
1164 if ( $this->mAllowed ) {
1165 # Slip in the hidden controls here
1166 $misc = Xml::hidden( 'target', $this->mTarget );
1167 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
1168 $misc .= Xml::closeElement( 'form' );
1169 $wgOut->addHTML( $misc );
1170 }
1171
1172 return true;
1173 }
1174
1175 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1176 global $wgUser, $wgLang;
1177
1178 $rev = Revision::newFromArchiveRow( $row,
1179 array( 'page' => $this->mTargetObj->getArticleId() ) );
1180 $stxt = '';
1181 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1182 // Build checkboxen...
1183 if( $this->mAllowed ) {
1184 if( $this->mInvert ) {
1185 if( in_array( $ts, $this->mTargetTimestamp ) ) {
1186 $checkBox = Xml::check( "ts$ts");
1187 } else {
1188 $checkBox = Xml::check( "ts$ts", true );
1189 }
1190 } else {
1191 $checkBox = Xml::check( "ts$ts" );
1192 }
1193 } else {
1194 $checkBox = '';
1195 }
1196 // Build page & diff links...
1197 if( $this->mCanView ) {
1198 $titleObj = $this->getTitle();
1199 # Last link
1200 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
1201 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1202 $last = wfMsgHtml('diff');
1203 } else if( $remaining > 0 || ($earliestLiveTime && $ts > $earliestLiveTime) ) {
1204 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1205 $last = $sk->linkKnown(
1206 $titleObj,
1207 wfMsgHtml('diff'),
1208 array(),
1209 array(
1210 'target' => $this->mTargetObj->getPrefixedText(),
1211 'timestamp' => $ts,
1212 'diff' => 'prev'
1213 )
1214 );
1215 } else {
1216 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1217 $last = wfMsgHtml('diff');
1218 }
1219 } else {
1220 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1221 $last = wfMsgHtml('diff');
1222 }
1223 // User links
1224 $userLink = $sk->revUserTools( $rev );
1225 // Revision text size
1226 if( !is_null($size = $row->ar_len) ) {
1227 $stxt = $sk->formatRevisionSize( $size );
1228 }
1229 // Edit summary
1230 $comment = $sk->revComment( $rev );
1231 // Revision delete links
1232 $canHide = $wgUser->isAllowed( 'deleterevision' );
1233 if( $canHide || ($rev->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1234 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
1235 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1236 } else {
1237 $query = array(
1238 'type' => 'archive',
1239 'target' => $this->mTargetObj->getPrefixedDBkey(),
1240 'ids' => $ts
1241 );
1242 $revdlink = $sk->revDeleteLink( $query,
1243 $rev->isDeleted( Revision::DELETED_RESTRICTED ), $canHide );
1244 }
1245 } else {
1246 $revdlink = '';
1247 }
1248 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1249 }
1250
1251 private function formatFileRow( $row, $sk ) {
1252 global $wgUser, $wgLang;
1253
1254 $file = ArchivedFile::newFromRow( $row );
1255
1256 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1257 if( $this->mAllowed && $row->fa_storage_key ) {
1258 $checkBox = Xml::check( "fileid" . $row->fa_id );
1259 $key = urlencode( $row->fa_storage_key );
1260 $target = urlencode( $this->mTarget );
1261 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1262 } else {
1263 $checkBox = '';
1264 $pageLink = $wgLang->timeanddate( $ts, true );
1265 }
1266 $userLink = $this->getFileUser( $file, $sk );
1267 $data =
1268 wfMsg( 'widthheight',
1269 $wgLang->formatNum( $row->fa_width ),
1270 $wgLang->formatNum( $row->fa_height ) ) .
1271 ' (' .
1272 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
1273 ')';
1274 $data = htmlspecialchars( $data );
1275 $comment = $this->getFileComment( $file, $sk );
1276 // Add show/hide deletion links if available
1277 $canHide = $wgUser->isAllowed( 'deleterevision' );
1278 if( $canHide || ($file->getVisibility() && $wgUser->isAllowed('deletedhistory')) ) {
1279 if( !$file->userCan(File::DELETED_RESTRICTED ) ) {
1280 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1281 } else {
1282 $query = array(
1283 'type' => 'filearchive',
1284 'target' => $this->mTargetObj->getPrefixedDBkey(),
1285 'ids' => $row->fa_id
1286 );
1287 $revdlink = $sk->revDeleteLink( $query,
1288 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1289 }
1290 } else {
1291 $revdlink = '';
1292 }
1293 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1294 }
1295
1296 /**
1297 * Fetch revision text link if it's available to all users
1298 * @return string
1299 */
1300 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1301 global $wgLang;
1302
1303 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1304
1305 if( !$rev->userCan(Revision::DELETED_TEXT) ) {
1306 return '<span class="history-deleted">' . $time . '</span>';
1307 } else {
1308 $link = $sk->linkKnown(
1309 $titleObj,
1310 $time,
1311 array(),
1312 array(
1313 'target' => $this->mTargetObj->getPrefixedText(),
1314 'timestamp' => $ts
1315 )
1316 );
1317 if( $rev->isDeleted(Revision::DELETED_TEXT) )
1318 $link = '<span class="history-deleted">' . $link . '</span>';
1319 return $link;
1320 }
1321 }
1322
1323 /**
1324 * Fetch image view link if it's available to all users
1325 *
1326 * @return String: HTML fragment
1327 */
1328 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1329 global $wgLang, $wgUser;
1330
1331 if( !$file->userCan(File::DELETED_FILE) ) {
1332 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1333 } else {
1334 $link = $sk->linkKnown(
1335 $titleObj,
1336 $wgLang->timeanddate( $ts, true ),
1337 array(),
1338 array(
1339 'target' => $this->mTargetObj->getPrefixedText(),
1340 'file' => $key,
1341 'token' => $wgUser->editToken( $key )
1342 )
1343 );
1344 if( $file->isDeleted(File::DELETED_FILE) )
1345 $link = '<span class="history-deleted">' . $link . '</span>';
1346 return $link;
1347 }
1348 }
1349
1350 /**
1351 * Fetch file's user id if it's available to this user
1352 *
1353 * @return String: HTML fragment
1354 */
1355 function getFileUser( $file, $sk ) {
1356 if( !$file->userCan(File::DELETED_USER) ) {
1357 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1358 } else {
1359 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1360 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1361 if( $file->isDeleted(File::DELETED_USER) )
1362 $link = '<span class="history-deleted">' . $link . '</span>';
1363 return $link;
1364 }
1365 }
1366
1367 /**
1368 * Fetch file upload comment if it's available to this user
1369 *
1370 * @return String: HTML fragment
1371 */
1372 function getFileComment( $file, $sk ) {
1373 if( !$file->userCan(File::DELETED_COMMENT) ) {
1374 return '<span class="history-deleted"><span class="comment">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1375 } else {
1376 $link = $sk->commentBlock( $file->getRawDescription() );
1377 if( $file->isDeleted(File::DELETED_COMMENT) )
1378 $link = '<span class="history-deleted">' . $link . '</span>';
1379 return $link;
1380 }
1381 }
1382
1383 function undelete() {
1384 global $wgOut, $wgUser;
1385 if ( wfReadOnly() ) {
1386 $wgOut->readOnlyPage();
1387 return;
1388 }
1389 if( !is_null( $this->mTargetObj ) ) {
1390 $archive = new PageArchive( $this->mTargetObj );
1391 $ok = $archive->undelete(
1392 $this->mTargetTimestamp,
1393 $this->mComment,
1394 $this->mFileVersions,
1395 $this->mUnsuppress );
1396
1397 if( is_array($ok) ) {
1398 if ( $ok[1] ) // Undeleted file count
1399 wfRunHooks( 'FileUndeleteComplete', array(
1400 $this->mTargetObj, $this->mFileVersions,
1401 $wgUser, $this->mComment) );
1402
1403 $skin = $wgUser->getSkin();
1404 $link = $skin->linkKnown( $this->mTargetObj );
1405 $wgOut->addHTML( wfMsgWikiHtml( 'undeletedpage', $link ) );
1406 } else {
1407 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1408 $wgOut->addHTML( '<p>' . wfMsgHtml( "undeleterevdel" ) . '</p>' );
1409 }
1410
1411 // Show file deletion warnings and errors
1412 $status = $archive->getFileStatus();
1413 if( $status && !$status->isGood() ) {
1414 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1415 }
1416 } else {
1417 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1418 }
1419 return false;
1420 }
1421 }