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