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