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