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