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