(bug 29443) Special:Undelete should invert checkboxes without reloading
[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
536 // Attach the latest revision to the page...
537 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
538 if ( $created || $wasnew ) {
539 // Update site stats, link tables, etc
540 $article->editUpdates( $revision->getText(), $revision->getComment(),
541 $revision->isMinor(), wfTimestamp(), $revision->getId(), true, null, $created );
542 }
543
544 wfRunHooks( 'ArticleUndelete', array( &$this->title, $created, $comment ) );
545
546 if( $created ) {
547 Article::onArticleCreate( $this->title );
548 } else {
549 Article::onArticleEdit( $this->title );
550 }
551
552 if( $this->title->getNamespace() == NS_FILE ) {
553 $update = new HTMLCacheUpdate( $this->title, 'imagelinks' );
554 $update->doUpdate();
555 }
556
557 return $restored;
558 }
559
560 /**
561 * @return Status
562 */
563 function getFileStatus() { return $this->fileStatus; }
564 }
565
566 /**
567 * Special page allowing users with the appropriate permissions to view
568 * and restore deleted content.
569 *
570 * @ingroup SpecialPage
571 */
572 class SpecialUndelete extends SpecialPage {
573 var $mAction, $mTarget, $mTimestamp, $mRestore, $mInvert, $mFilename;
574 var $mTargetTimestamp, $mAllowed, $mCanView, $mComment, $mToken, $mRequest;
575
576 /**
577 * @var Title
578 */
579 var $mTargetObj;
580
581 function __construct( $request = null ) {
582 parent::__construct( 'Undelete', 'deletedhistory' );
583
584 if ( $request === null ) {
585 global $wgRequest;
586 $this->mRequest = $wgRequest;
587 } else {
588 $this->mRequest = $request;
589 }
590 }
591
592 function loadRequest() {
593 global $wgUser;
594 $this->mAction = $this->mRequest->getVal( 'action' );
595 $this->mTarget = $this->mRequest->getVal( 'target' );
596 $this->mSearchPrefix = $this->mRequest->getText( 'prefix' );
597 $time = $this->mRequest->getVal( 'timestamp' );
598 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
599 $this->mFilename = $this->mRequest->getVal( 'file' );
600
601 $posted = $this->mRequest->wasPosted() &&
602 $wgUser->matchEditToken( $this->mRequest->getVal( 'wpEditToken' ) );
603 $this->mRestore = $this->mRequest->getCheck( 'restore' ) && $posted;
604 $this->mInvert = $this->mRequest->getCheck( 'invert' ) && $posted;
605 $this->mPreview = $this->mRequest->getCheck( 'preview' ) && $posted;
606 $this->mDiff = $this->mRequest->getCheck( 'diff' );
607 $this->mComment = $this->mRequest->getText( 'wpComment' );
608 $this->mUnsuppress = $this->mRequest->getVal( 'wpUnsuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
609 $this->mToken = $this->mRequest->getVal( 'token' );
610
611 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
612 $this->mAllowed = true; // user can restore
613 $this->mCanView = true; // user can view content
614 } elseif ( $wgUser->isAllowed( 'deletedtext' ) ) {
615 $this->mAllowed = false; // user cannot restore
616 $this->mCanView = true; // user can view content
617 } else { // user can only view the list of revisions
618 $this->mAllowed = false;
619 $this->mCanView = false;
620 $this->mTimestamp = '';
621 $this->mRestore = false;
622 }
623
624 if( $this->mRestore || $this->mInvert ) {
625 $timestamps = array();
626 $this->mFileVersions = array();
627 foreach( $_REQUEST as $key => $val ) {
628 $matches = array();
629 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
630 array_push( $timestamps, $matches[1] );
631 }
632
633 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
634 $this->mFileVersions[] = intval( $matches[1] );
635 }
636 }
637 rsort( $timestamps );
638 $this->mTargetTimestamp = $timestamps;
639 }
640 }
641
642 function execute( $par ) {
643 global $wgOut, $wgUser;
644
645 $this->setHeaders();
646 if ( !$this->userCanExecute( $wgUser ) ) {
647 $this->displayRestrictionError();
648 return;
649 }
650 $this->outputHeader();
651
652 $this->loadRequest();
653
654 if ( $this->mAllowed ) {
655 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
656 } else {
657 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
658 }
659
660 if( $par != '' ) {
661 $this->mTarget = $par;
662 }
663 if ( $this->mTarget !== '' ) {
664 $this->mTargetObj = Title::newFromURL( $this->mTarget );
665 $wgUser->getSkin()->setRelevantTitle( $this->mTargetObj );
666 } else {
667 $this->mTargetObj = null;
668 }
669
670 if( is_null( $this->mTargetObj ) ) {
671 # Not all users can just browse every deleted page from the list
672 if( $wgUser->isAllowed( 'browsearchive' ) ) {
673 $this->showSearchForm();
674
675 # List undeletable articles
676 if( $this->mSearchPrefix ) {
677 $result = PageArchive::listPagesByPrefix( $this->mSearchPrefix );
678 $this->showList( $result );
679 }
680 } else {
681 $wgOut->addWikiMsg( 'undelete-header' );
682 }
683 return;
684 }
685 if( $this->mTimestamp !== '' ) {
686 return $this->showRevision( $this->mTimestamp );
687 }
688 if( $this->mFilename !== null ) {
689 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
690 // Check if user is allowed to see this file
691 if ( !$file->exists() ) {
692 $wgOut->addWikiMsg( 'filedelete-nofile', $this->mFilename );
693 return;
694 } elseif( !$file->userCan( File::DELETED_FILE ) ) {
695 if( $file->isDeleted( File::DELETED_RESTRICTED ) ) {
696 $wgOut->permissionRequired( 'suppressrevision' );
697 } else {
698 $wgOut->permissionRequired( 'deletedtext' );
699 }
700 return false;
701 } elseif ( !$wgUser->matchEditToken( $this->mToken, $this->mFilename ) ) {
702 $this->showFileConfirmationForm( $this->mFilename );
703 return false;
704 } else {
705 return $this->showFile( $this->mFilename );
706 }
707 }
708 if( $this->mRestore && $this->mAction == 'submit' ) {
709 global $wgUploadMaintenance;
710 if( $wgUploadMaintenance && $this->mTargetObj && $this->mTargetObj->getNamespace() == NS_FILE ) {
711 $wgOut->wrapWikiMsg( "<div class='error'>\n$1\n</div>\n", array( 'filedelete-maintenance' ) );
712 return;
713 }
714 return $this->undelete();
715 }
716 if( $this->mInvert && $this->mAction == 'submit' ) {
717 return $this->showHistory();
718 }
719 return $this->showHistory();
720 }
721
722 function showSearchForm() {
723 global $wgOut, $wgScript;
724 $wgOut->addWikiMsg( 'undelete-header' );
725
726 $wgOut->addHTML(
727 Xml::openElement( 'form', array(
728 'method' => 'get',
729 'action' => $wgScript ) ) .
730 Xml::fieldset( wfMsg( 'undelete-search-box' ) ) .
731 Html::hidden( 'title',
732 $this->getTitle()->getPrefixedDbKey() ) .
733 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
734 'prefix', 'prefix', 20,
735 $this->mSearchPrefix ) . ' ' .
736 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
737 Xml::closeElement( 'fieldset' ) .
738 Xml::closeElement( 'form' )
739 );
740 }
741
742 /**
743 * Generic list of deleted pages
744 *
745 * @param $result ResultWrapper
746 * @return bool
747 */
748 private function showList( $result ) {
749 global $wgLang, $wgUser, $wgOut;
750
751 if( $result->numRows() == 0 ) {
752 $wgOut->addWikiMsg( 'undelete-no-results' );
753 return;
754 }
755
756 $wgOut->addWikiMsg( 'undeletepagetext', $wgLang->formatNum( $result->numRows() ) );
757
758 $sk = $wgUser->getSkin();
759 $undelete = $this->getTitle();
760 $wgOut->addHTML( "<ul>\n" );
761 foreach ( $result as $row ) {
762 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
763 $link = $sk->linkKnown(
764 $undelete,
765 htmlspecialchars( $title->getPrefixedText() ),
766 array(),
767 array( 'target' => $title->getPrefixedText() )
768 );
769 $revs = wfMsgExt( 'undeleterevisions',
770 array( 'parseinline' ),
771 $wgLang->formatNum( $row->count ) );
772 $wgOut->addHTML( "<li>{$link} ({$revs})</li>\n" );
773 }
774 $result->free();
775 $wgOut->addHTML( "</ul>\n" );
776
777 return true;
778 }
779
780 private function showRevision( $timestamp ) {
781 global $wgLang, $wgUser, $wgOut;
782
783 $skin = $wgUser->getSkin();
784
785 if( !preg_match( '/[0-9]{14}/', $timestamp ) ) {
786 return 0;
787 }
788
789 $archive = new PageArchive( $this->mTargetObj );
790 wfRunHooks( 'UndeleteForm::showRevision', array( &$archive, $this->mTargetObj ) );
791 $rev = $archive->getRevision( $timestamp );
792
793 if( !$rev ) {
794 $wgOut->addWikiMsg( 'undeleterevision-missing' );
795 return;
796 }
797
798 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
799 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
800 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-permission' );
801 return;
802 } else {
803 $wgOut->wrapWikiMsg( "<div class='mw-warning plainlinks'>\n$1\n</div>\n", 'rev-deleted-text-view' );
804 $wgOut->addHTML( '<br />' );
805 // and we are allowed to see...
806 }
807 }
808
809 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
810
811 $link = $skin->linkKnown(
812 $this->getTitle( $this->mTargetObj->getPrefixedDBkey() ),
813 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
814 );
815
816 if( $this->mDiff ) {
817 $previousRev = $archive->getPreviousRevision( $timestamp );
818 if( $previousRev ) {
819 $this->showDiff( $previousRev, $rev );
820 if( $wgUser->getOption( 'diffonly' ) ) {
821 return;
822 } else {
823 $wgOut->addHTML( '<hr />' );
824 }
825 } else {
826 $wgOut->addWikiMsg( 'undelete-nodiff' );
827 }
828 }
829
830 // date and time are separate parameters to facilitate localisation.
831 // $time is kept for backward compat reasons.
832 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
833 $d = htmlspecialchars( $wgLang->date( $timestamp, true ) );
834 $t = htmlspecialchars( $wgLang->time( $timestamp, true ) );
835 $user = $skin->revUserTools( $rev );
836
837 if( $this->mPreview ) {
838 $openDiv = '<div id="mw-undelete-revision" class="mw-warning">';
839 } else {
840 $openDiv = '<div id="mw-undelete-revision">';
841 }
842 $wgOut->addHTML( $openDiv );
843
844 // Revision delete links
845 if ( !$this->mDiff ) {
846 $revdel = $this->revDeleteLink( $rev );
847 if ( $revdel ) {
848 $wgOut->addHTML( $revdel );
849 }
850 }
851
852 $wgOut->addWikiMsgArray( 'undelete-revision', array( $link, $time, $user, $d, $t ), array( 'replaceafter' ) );
853 $wgOut->addHTML( '</div>' );
854 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
855
856 if( $this->mPreview ) {
857 // Hide [edit]s
858 $popts = $wgOut->parserOptions();
859 $popts->setEditSection( false );
860 $wgOut->parserOptions( $popts );
861 $wgOut->addWikiTextTitleTidy( $rev->getText( Revision::FOR_THIS_USER ), $this->mTargetObj, true );
862 }
863
864 $wgOut->addHTML(
865 Xml::element( 'textarea', array(
866 'readonly' => 'readonly',
867 'cols' => intval( $wgUser->getOption( 'cols' ) ),
868 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
869 $rev->getText( Revision::FOR_THIS_USER ) . "\n" ) .
870 Xml::openElement( 'div' ) .
871 Xml::openElement( 'form', array(
872 'method' => 'post',
873 'action' => $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) ) ) ) .
874 Xml::element( 'input', array(
875 'type' => 'hidden',
876 'name' => 'target',
877 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
878 Xml::element( 'input', array(
879 'type' => 'hidden',
880 'name' => 'timestamp',
881 'value' => $timestamp ) ) .
882 Xml::element( 'input', array(
883 'type' => 'hidden',
884 'name' => 'wpEditToken',
885 'value' => $wgUser->editToken() ) ) .
886 Xml::element( 'input', array(
887 'type' => 'submit',
888 'name' => 'preview',
889 'value' => wfMsg( 'showpreview' ) ) ) .
890 Xml::element( 'input', array(
891 'name' => 'diff',
892 'type' => 'submit',
893 'value' => wfMsg( 'showdiff' ) ) ) .
894 Xml::closeElement( 'form' ) .
895 Xml::closeElement( 'div' ) );
896 }
897
898 /**
899 * Get a revision-deletion link, or disabled link, or nothing, depending
900 * on user permissions & the settings on the revision.
901 *
902 * Will use forward-compatible revision ID in the Special:RevDelete link
903 * if possible, otherwise the timestamp-based ID which may break after
904 * undeletion.
905 *
906 * @param Revision $rev
907 * @return string HTML fragment
908 */
909 function revDeleteLink( $rev ) {
910 global $wgUser;
911 $canHide = $wgUser->isAllowed( 'deleterevision' );
912 if( $canHide || ( $rev->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
913 $skin = $wgUser->getSkin();
914 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
915 $revdlink = $skin->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
916 } else {
917 if ( $rev->getId() ) {
918 // RevDelete links using revision ID are stable across
919 // page deletion and undeletion; use when possible.
920 $query = array(
921 'type' => 'revision',
922 'target' => $this->mTargetObj->getPrefixedDBkey(),
923 'ids' => $rev->getId()
924 );
925 } else {
926 // Older deleted entries didn't save a revision ID.
927 // We have to refer to these by timestamp, ick!
928 $query = array(
929 'type' => 'archive',
930 'target' => $this->mTargetObj->getPrefixedDBkey(),
931 'ids' => $rev->getTimestamp()
932 );
933 }
934 return $skin->revDeleteLink( $query,
935 $rev->isDeleted( File::DELETED_RESTRICTED ), $canHide );
936 }
937 } else {
938 return '';
939 }
940 }
941
942 /**
943 * Build a diff display between this and the previous either deleted
944 * or non-deleted edit.
945 *
946 * @param $previousRev Revision
947 * @param $currentRev Revision
948 * @return String: HTML
949 */
950 function showDiff( $previousRev, $currentRev ) {
951 global $wgOut;
952
953 $diffEngine = new DifferenceEngine( $previousRev->getTitle() );
954 $diffEngine->showDiffStyle();
955 $wgOut->addHTML(
956 "<div>" .
957 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
958 "<col class='diff-marker' />" .
959 "<col class='diff-content' />" .
960 "<col class='diff-marker' />" .
961 "<col class='diff-content' />" .
962 "<tr>" .
963 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
964 $this->diffHeader( $previousRev, 'o' ) .
965 "</td>\n" .
966 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
967 $this->diffHeader( $currentRev, 'n' ) .
968 "</td>\n" .
969 "</tr>" .
970 $diffEngine->generateDiffBody(
971 $previousRev->getText(), $currentRev->getText() ) .
972 "</table>" .
973 "</div>\n"
974 );
975 }
976
977 /**
978 * @param $rev Revision
979 * @param $prefix
980 * @return string
981 */
982 private function diffHeader( $rev, $prefix ) {
983 global $wgUser, $wgLang;
984 $sk = $wgUser->getSkin();
985 $isDeleted = !( $rev->getId() && $rev->getTitle() );
986 if( $isDeleted ) {
987 /// @todo FIXME: $rev->getTitle() is null for deleted revs...?
988 $targetPage = $this->getTitle();
989 $targetQuery = array(
990 'target' => $this->mTargetObj->getPrefixedText(),
991 'timestamp' => wfTimestamp( TS_MW, $rev->getTimestamp() )
992 );
993 } else {
994 /// @todo FIXME: getId() may return non-zero for deleted revs...
995 $targetPage = $rev->getTitle();
996 $targetQuery = array( 'oldid' => $rev->getId() );
997 }
998 // Add show/hide deletion links if available
999 $del = $this->revDeleteLink( $rev );
1000 return
1001 '<div id="mw-diff-' . $prefix . 'title1"><strong>' .
1002 $sk->link(
1003 $targetPage,
1004 wfMsgHtml(
1005 'revisionasof',
1006 htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
1007 htmlspecialchars( $wgLang->date( $rev->getTimestamp(), true ) ),
1008 htmlspecialchars( $wgLang->time( $rev->getTimestamp(), true ) )
1009 ),
1010 array(),
1011 $targetQuery
1012 ) .
1013 '</strong></div>' .
1014 '<div id="mw-diff-'.$prefix.'title2">' .
1015 $sk->revUserTools( $rev ) . '<br />' .
1016 '</div>' .
1017 '<div id="mw-diff-'.$prefix.'title3">' .
1018 $sk->revComment( $rev ) . $del . '<br />' .
1019 '</div>';
1020 }
1021
1022 /**
1023 * Show a form confirming whether a tokenless user really wants to see a file
1024 */
1025 private function showFileConfirmationForm( $key ) {
1026 global $wgOut, $wgUser, $wgLang;
1027 $file = new ArchivedFile( $this->mTargetObj, '', $this->mFilename );
1028 $wgOut->addWikiMsg( 'undelete-show-file-confirm',
1029 $this->mTargetObj->getText(),
1030 $wgLang->date( $file->getTimestamp() ),
1031 $wgLang->time( $file->getTimestamp() ) );
1032 $wgOut->addHTML(
1033 Xml::openElement( 'form', array(
1034 'method' => 'POST',
1035 'action' => $this->getTitle()->getLocalURL(
1036 'target=' . urlencode( $this->mTarget ) .
1037 '&file=' . urlencode( $key ) .
1038 '&token=' . urlencode( $wgUser->editToken( $key ) ) )
1039 )
1040 ) .
1041 Xml::submitButton( wfMsg( 'undelete-show-file-submit' ) ) .
1042 '</form>'
1043 );
1044 }
1045
1046 /**
1047 * Show a deleted file version requested by the visitor.
1048 */
1049 private function showFile( $key ) {
1050 global $wgOut, $wgRequest;
1051 $wgOut->disable();
1052
1053 # We mustn't allow the output to be Squid cached, otherwise
1054 # if an admin previews a deleted image, and it's cached, then
1055 # a user without appropriate permissions can toddle off and
1056 # nab the image, and Squid will serve it
1057 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
1058 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
1059 $wgRequest->response()->header( 'Pragma: no-cache' );
1060
1061 global $IP;
1062 require_once( "$IP/includes/StreamFile.php" );
1063 $repo = RepoGroup::singleton()->getLocalRepo();
1064 $path = $repo->getZonePath( 'deleted' ) . '/' . $repo->getDeletedHashPath( $key ) . $key;
1065 wfStreamFile( $path );
1066 }
1067
1068 private function showHistory() {
1069 global $wgUser, $wgOut;
1070
1071 $sk = $wgUser->getSkin();
1072 if( $this->mAllowed ) {
1073 $wgOut->addModules( 'mediawiki.special.undelete' );
1074 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
1075 } else {
1076 $wgOut->setPageTitle( wfMsg( 'viewdeletedpage' ) );
1077 }
1078 $wgOut->wrapWikiMsg(
1079 "<div class='mw-undelete-pagetitle'>\n$1\n</div>\n",
1080 array( 'undeletepagetitle', $this->mTargetObj->getPrefixedText() )
1081 );
1082
1083 $archive = new PageArchive( $this->mTargetObj );
1084 wfRunHooks( 'UndeleteForm::showHistory', array( &$archive, $this->mTargetObj ) );
1085 /*
1086 $text = $archive->getLastRevisionText();
1087 if( is_null( $text ) ) {
1088 $wgOut->addWikiMsg( 'nohistory' );
1089 return;
1090 }
1091 */
1092 $wgOut->addHTML( '<div class="mw-undelete-history">' );
1093 if ( $this->mAllowed ) {
1094 $wgOut->addWikiMsg( 'undeletehistory' );
1095 $wgOut->addWikiMsg( 'undeleterevdel' );
1096 } else {
1097 $wgOut->addWikiMsg( 'undeletehistorynoadmin' );
1098 }
1099 $wgOut->addHTML( '</div>' );
1100
1101 # List all stored revisions
1102 $revisions = $archive->listRevisions();
1103 $files = $archive->listFiles();
1104
1105 $haveRevisions = $revisions && $revisions->numRows() > 0;
1106 $haveFiles = $files && $files->numRows() > 0;
1107
1108 # Batch existence check on user and talk pages
1109 if( $haveRevisions ) {
1110 $batch = new LinkBatch();
1111 foreach ( $revisions as $row ) {
1112 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
1113 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
1114 }
1115 $batch->execute();
1116 $revisions->seek( 0 );
1117 }
1118 if( $haveFiles ) {
1119 $batch = new LinkBatch();
1120 foreach ( $files as $row ) {
1121 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
1122 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
1123 }
1124 $batch->execute();
1125 $files->seek( 0 );
1126 }
1127
1128 if ( $this->mAllowed ) {
1129 $action = $this->getTitle()->getLocalURL( array( 'action' => 'submit' ) );
1130 # Start the form here
1131 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
1132 $wgOut->addHTML( $top );
1133 }
1134
1135 # Show relevant lines from the deletion log:
1136 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'delete' ) ) . "\n" );
1137 LogEventsList::showLogExtract( $wgOut, 'delete', $this->mTargetObj->getPrefixedText() );
1138 # Show relevant lines from the suppression log:
1139 if( $wgUser->isAllowed( 'suppressionlog' ) ) {
1140 $wgOut->addHTML( Xml::element( 'h2', null, LogPage::logName( 'suppress' ) ) . "\n" );
1141 LogEventsList::showLogExtract( $wgOut, 'suppress', $this->mTargetObj->getPrefixedText() );
1142 }
1143
1144 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
1145 # Format the user-visible controls (comment field, submission button)
1146 # in a nice little table
1147 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
1148 $unsuppressBox =
1149 "<tr>
1150 <td>&#160;</td>
1151 <td class='mw-input'>" .
1152 Xml::checkLabel( wfMsg( 'revdelete-unsuppress' ), 'wpUnsuppress',
1153 'mw-undelete-unsuppress', $this->mUnsuppress ).
1154 "</td>
1155 </tr>";
1156 } else {
1157 $unsuppressBox = '';
1158 }
1159 $table =
1160 Xml::fieldset( wfMsg( 'undelete-fieldset-title' ) ) .
1161 Xml::openElement( 'table', array( 'id' => 'mw-undelete-table' ) ) .
1162 "<tr>
1163 <td colspan='2' class='mw-undelete-extrahelp'>" .
1164 wfMsgExt( 'undeleteextrahelp', 'parse' ) .
1165 "</td>
1166 </tr>
1167 <tr>
1168 <td class='mw-label'>" .
1169 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
1170 "</td>
1171 <td class='mw-input'>" .
1172 Xml::input( 'wpComment', 50, $this->mComment, array( 'id' => 'wpComment' ) ) .
1173 "</td>
1174 </tr>
1175 <tr>
1176 <td>&#160;</td>
1177 <td class='mw-submit'>" .
1178 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) . ' ' .
1179 Xml::submitButton( wfMsg( 'undeleteinvert' ), array( 'name' => 'invert', 'id' => 'mw-undelete-invert' ) ) .
1180 "</td>
1181 </tr>" .
1182 $unsuppressBox .
1183 Xml::closeElement( 'table' ) .
1184 Xml::closeElement( 'fieldset' );
1185
1186 $wgOut->addHTML( $table );
1187 }
1188
1189 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'history' ) ) . "\n" );
1190
1191 if( $haveRevisions ) {
1192 # The page's stored (deleted) history:
1193 $wgOut->addHTML( '<ul>' );
1194 $remaining = $revisions->numRows();
1195 $earliestLiveTime = $this->mTargetObj->getEarliestRevTime();
1196
1197 foreach ( $revisions as $row ) {
1198 $remaining--;
1199 $wgOut->addHTML( $this->formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) );
1200 }
1201 $revisions->free();
1202 $wgOut->addHTML( '</ul>' );
1203 } else {
1204 $wgOut->addWikiMsg( 'nohistory' );
1205 }
1206
1207 if( $haveFiles ) {
1208 $wgOut->addHTML( Xml::element( 'h2', null, wfMsg( 'filehist' ) ) . "\n" );
1209 $wgOut->addHTML( '<ul>' );
1210 foreach ( $files as $row ) {
1211 $wgOut->addHTML( $this->formatFileRow( $row, $sk ) );
1212 }
1213 $files->free();
1214 $wgOut->addHTML( '</ul>' );
1215 }
1216
1217 if ( $this->mAllowed ) {
1218 # Slip in the hidden controls here
1219 $misc = Html::hidden( 'target', $this->mTarget );
1220 $misc .= Html::hidden( 'wpEditToken', $wgUser->editToken() );
1221 $misc .= Xml::closeElement( 'form' );
1222 $wgOut->addHTML( $misc );
1223 }
1224
1225 return true;
1226 }
1227
1228 private function formatRevisionRow( $row, $earliestLiveTime, $remaining, $sk ) {
1229 global $wgLang;
1230
1231 $rev = Revision::newFromArchiveRow( $row,
1232 array( 'page' => $this->mTargetObj->getArticleId() ) );
1233 $stxt = '';
1234 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
1235 // Build checkboxen...
1236 if( $this->mAllowed ) {
1237 if( $this->mInvert ) {
1238 if( in_array( $ts, $this->mTargetTimestamp ) ) {
1239 $checkBox = Xml::check( "ts$ts" );
1240 } else {
1241 $checkBox = Xml::check( "ts$ts", true );
1242 }
1243 } else {
1244 $checkBox = Xml::check( "ts$ts" );
1245 }
1246 } else {
1247 $checkBox = '';
1248 }
1249 // Build page & diff links...
1250 if( $this->mCanView ) {
1251 $titleObj = $this->getTitle();
1252 # Last link
1253 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
1254 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1255 $last = wfMsgHtml( 'diff' );
1256 } elseif( $remaining > 0 || ( $earliestLiveTime && $ts > $earliestLiveTime ) ) {
1257 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1258 $last = $sk->linkKnown(
1259 $titleObj,
1260 wfMsgHtml( 'diff' ),
1261 array(),
1262 array(
1263 'target' => $this->mTargetObj->getPrefixedText(),
1264 'timestamp' => $ts,
1265 'diff' => 'prev'
1266 )
1267 );
1268 } else {
1269 $pageLink = $this->getPageLink( $rev, $titleObj, $ts, $sk );
1270 $last = wfMsgHtml( 'diff' );
1271 }
1272 } else {
1273 $pageLink = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1274 $last = wfMsgHtml( 'diff' );
1275 }
1276 // User links
1277 $userLink = $sk->revUserTools( $rev );
1278 // Revision text size
1279 $size = $row->ar_len;
1280 if( !is_null( $size ) ) {
1281 $stxt = $sk->formatRevisionSize( $size );
1282 }
1283 // Edit summary
1284 $comment = $sk->revComment( $rev );
1285 // Revision delete links
1286 $revdlink = $this->revDeleteLink( $rev );
1287 return "<li>$checkBox $revdlink ($last) $pageLink . . $userLink $stxt $comment</li>";
1288 }
1289
1290 private function formatFileRow( $row, $sk ) {
1291 global $wgUser, $wgLang;
1292
1293 $file = ArchivedFile::newFromRow( $row );
1294
1295 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1296 if( $this->mAllowed && $row->fa_storage_key ) {
1297 $checkBox = Xml::check( 'fileid' . $row->fa_id );
1298 $key = urlencode( $row->fa_storage_key );
1299 $pageLink = $this->getFileLink( $file, $this->getTitle(), $ts, $key, $sk );
1300 } else {
1301 $checkBox = '';
1302 $pageLink = $wgLang->timeanddate( $ts, true );
1303 }
1304 $userLink = $this->getFileUser( $file, $sk );
1305 $data =
1306 wfMsg( 'widthheight',
1307 $wgLang->formatNum( $row->fa_width ),
1308 $wgLang->formatNum( $row->fa_height ) ) .
1309 ' (' .
1310 wfMsg( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
1311 ')';
1312 $data = htmlspecialchars( $data );
1313 $comment = $this->getFileComment( $file, $sk );
1314 // Add show/hide deletion links if available
1315 $canHide = $wgUser->isAllowed( 'deleterevision' );
1316 if( $canHide || ( $file->getVisibility() && $wgUser->isAllowed( 'deletedhistory' ) ) ) {
1317 if( !$file->userCan( File::DELETED_RESTRICTED ) ) {
1318 $revdlink = $sk->revDeleteLinkDisabled( $canHide ); // revision was hidden from sysops
1319 } else {
1320 $query = array(
1321 'type' => 'filearchive',
1322 'target' => $this->mTargetObj->getPrefixedDBkey(),
1323 'ids' => $row->fa_id
1324 );
1325 $revdlink = $sk->revDeleteLink( $query,
1326 $file->isDeleted( File::DELETED_RESTRICTED ), $canHide );
1327 }
1328 } else {
1329 $revdlink = '';
1330 }
1331 return "<li>$checkBox $revdlink $pageLink . . $userLink $data $comment</li>\n";
1332 }
1333
1334 /**
1335 * Fetch revision text link if it's available to all users
1336 *
1337 * @param $rev Revision
1338 * @param $sk Skin
1339 * @return string
1340 */
1341 function getPageLink( $rev, $titleObj, $ts, $sk ) {
1342 global $wgLang;
1343
1344 $time = htmlspecialchars( $wgLang->timeanddate( $ts, true ) );
1345
1346 if( !$rev->userCan( Revision::DELETED_TEXT ) ) {
1347 return '<span class="history-deleted">' . $time . '</span>';
1348 } else {
1349 $link = $sk->linkKnown(
1350 $titleObj,
1351 $time,
1352 array(),
1353 array(
1354 'target' => $this->mTargetObj->getPrefixedText(),
1355 'timestamp' => $ts
1356 )
1357 );
1358 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
1359 $link = '<span class="history-deleted">' . $link . '</span>';
1360 }
1361 return $link;
1362 }
1363 }
1364
1365 /**
1366 * Fetch image view link if it's available to all users
1367 *
1368 * @param $file File
1369 * @param $sk Skin
1370 * @return String: HTML fragment
1371 */
1372 function getFileLink( $file, $titleObj, $ts, $key, $sk ) {
1373 global $wgLang, $wgUser;
1374
1375 if( !$file->userCan( File::DELETED_FILE ) ) {
1376 return '<span class="history-deleted">' . $wgLang->timeanddate( $ts, true ) . '</span>';
1377 } else {
1378 $link = $sk->linkKnown(
1379 $titleObj,
1380 $wgLang->timeanddate( $ts, true ),
1381 array(),
1382 array(
1383 'target' => $this->mTargetObj->getPrefixedText(),
1384 'file' => $key,
1385 'token' => $wgUser->editToken( $key )
1386 )
1387 );
1388 if( $file->isDeleted( File::DELETED_FILE ) ) {
1389 $link = '<span class="history-deleted">' . $link . '</span>';
1390 }
1391 return $link;
1392 }
1393 }
1394
1395 /**
1396 * Fetch file's user id if it's available to this user
1397 *
1398 * @param $file File
1399 * @param $sk Skin
1400 * @return String: HTML fragment
1401 */
1402 function getFileUser( $file, $sk ) {
1403 if( !$file->userCan( File::DELETED_USER ) ) {
1404 return '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
1405 } else {
1406 $link = $sk->userLink( $file->getRawUser(), $file->getRawUserText() ) .
1407 $sk->userToolLinks( $file->getRawUser(), $file->getRawUserText() );
1408 if( $file->isDeleted( File::DELETED_USER ) ) {
1409 $link = '<span class="history-deleted">' . $link . '</span>';
1410 }
1411 return $link;
1412 }
1413 }
1414
1415 /**
1416 * Fetch file upload comment if it's available to this user
1417 *
1418 * @param $file File
1419 * @param $sk Skin
1420 * @return String: HTML fragment
1421 */
1422 function getFileComment( $file, $sk ) {
1423 if( !$file->userCan( File::DELETED_COMMENT ) ) {
1424 return '<span class="history-deleted"><span class="comment">' .
1425 wfMsgHtml( 'rev-deleted-comment' ) . '</span></span>';
1426 } else {
1427 $link = $sk->commentBlock( $file->getRawDescription() );
1428 if( $file->isDeleted( File::DELETED_COMMENT ) ) {
1429 $link = '<span class="history-deleted">' . $link . '</span>';
1430 }
1431 return $link;
1432 }
1433 }
1434
1435 function undelete() {
1436 global $wgOut, $wgUser;
1437 if ( wfReadOnly() ) {
1438 $wgOut->readOnlyPage();
1439 return;
1440 }
1441 if( !is_null( $this->mTargetObj ) ) {
1442 $archive = new PageArchive( $this->mTargetObj );
1443 wfRunHooks( 'UndeleteForm::undelete', array( &$archive, $this->mTargetObj ) );
1444 $ok = $archive->undelete(
1445 $this->mTargetTimestamp,
1446 $this->mComment,
1447 $this->mFileVersions,
1448 $this->mUnsuppress );
1449
1450 if( is_array( $ok ) ) {
1451 if ( $ok[1] ) { // Undeleted file count
1452 wfRunHooks( 'FileUndeleteComplete', array(
1453 $this->mTargetObj, $this->mFileVersions,
1454 $wgUser, $this->mComment ) );
1455 }
1456
1457 $skin = $wgUser->getSkin();
1458 $link = $skin->linkKnown( $this->mTargetObj );
1459 $wgOut->addWikiMsgArray( 'undeletedpage', array( $link ), array( 'replaceafter' ) );
1460 } else {
1461 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );
1462 $wgOut->addWikiMsg( 'undeleterevdel' );
1463 }
1464
1465 // Show file deletion warnings and errors
1466 $status = $archive->getFileStatus();
1467 if( $status && !$status->isGood() ) {
1468 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1469 }
1470 } else {
1471 $wgOut->showFatalError( wfMsg( 'cannotundelete' ) );
1472 }
1473 return false;
1474 }
1475 }