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