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