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