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