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