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