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