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