* Only show most recent 5000 revisions on Special:Undelete by default. Accept
[lhc/web/wiklou.git] / includes / SpecialUndelete.php
1 <?php
2
3 /**
4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
6 *
7 * @addtogroup SpecialPage
8 */
9
10 /**
11 * Constructor
12 */
13 function wfSpecialUndelete( $par ) {
14 global $wgRequest;
15
16 $form = new UndeleteForm( $wgRequest, $par );
17 $form->execute();
18 }
19
20 /**
21 * Used to show archived pages and eventually restore them.
22 * @addtogroup SpecialPage
23 */
24 class PageArchive {
25 protected $title;
26 var $fileStatus;
27
28 function __construct( $title ) {
29 if( is_null( $title ) ) {
30 throw new MWException( 'Archiver() given a null title.');
31 }
32 $this->title = $title;
33 }
34
35 /**
36 * List all deleted pages recorded in the archive table. Returns result
37 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
38 * namespace/title.
39 *
40 * @return ResultWrapper
41 */
42 public static function listAllPages() {
43 $dbr = wfGetDB( DB_SLAVE );
44 return self::listPages( $dbr, '' );
45 }
46
47 /**
48 * List deleted pages recorded in the archive table matching the
49 * given title prefix.
50 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
51 *
52 * @return ResultWrapper
53 */
54 public static function listPagesByPrefix( $prefix ) {
55 $dbr = wfGetDB( DB_SLAVE );
56
57 $title = Title::newFromText( $prefix );
58 if( $title ) {
59 $ns = $title->getNamespace();
60 $encPrefix = $dbr->escapeLike( $title->getDbKey() );
61 } else {
62 // Prolly won't work too good
63 // @todo handle bare namespace names cleanly?
64 $ns = 0;
65 $encPrefix = $dbr->escapeLike( $prefix );
66 }
67 $conds = array(
68 'ar_namespace' => $ns,
69 "ar_title LIKE '$encPrefix%'",
70 );
71 return self::listPages( $dbr, $conds );
72 }
73
74 protected static function listPages( $dbr, $condition ) {
75 return $dbr->resultObject(
76 $dbr->select(
77 array( 'archive' ),
78 array(
79 'ar_namespace',
80 'ar_title',
81 'COUNT(*) AS count',
82 ),
83 $condition,
84 __METHOD__,
85 array(
86 'GROUP BY' => 'ar_namespace,ar_title',
87 'ORDER BY' => 'ar_namespace,ar_title',
88 'LIMIT' => 100,
89 )
90 )
91 );
92 }
93
94 /**
95 * List the revisions of the given page. Returns result wrapper with
96 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
97 *
98 * @return ResultWrapper
99 */
100 function listRevisions( $startTime, $limit ) {
101 $whereClause = array( 'ar_namespace' => $this->title->getNamespace(),
102 'ar_title' => $this->title->getDBkey() );
103 if ( $startTime )
104 $whereClause[] = "ar_timestamp < $startTime";
105
106 $dbr = wfGetDB( DB_SLAVE );
107 $res = $dbr->select( 'archive',
108 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len' ),
109 $whereClause,
110 'PageArchive::listRevisions',
111 array( 'ORDER BY' => 'ar_timestamp DESC',
112 'LIMIT' => intval($limit) ) );
113 $ret = $dbr->resultObject( $res );
114 return $ret;
115 }
116
117 /**
118 * List the deleted file revisions for this page, if it's a file page.
119 * Returns a result wrapper with various filearchive fields, or null
120 * if not a file page.
121 *
122 * @return ResultWrapper
123 * @todo Does this belong in Image for fuller encapsulation?
124 */
125 function listFiles() {
126 if( $this->title->getNamespace() == NS_IMAGE ) {
127 $dbr = wfGetDB( DB_SLAVE );
128 $res = $dbr->select( 'filearchive',
129 array(
130 'fa_id',
131 'fa_name',
132 'fa_storage_key',
133 'fa_size',
134 'fa_width',
135 'fa_height',
136 'fa_description',
137 'fa_user',
138 'fa_user_text',
139 'fa_timestamp' ),
140 array( 'fa_name' => $this->title->getDbKey() ),
141 __METHOD__,
142 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
143 $ret = $dbr->resultObject( $res );
144 return $ret;
145 }
146 return null;
147 }
148
149 /**
150 * Fetch (and decompress if necessary) the stored text for the deleted
151 * revision of the page with the given timestamp.
152 *
153 * @return string
154 * @deprecated Use getRevision() for more flexible information
155 */
156 function getRevisionText( $timestamp ) {
157 $rev = $this->getRevision( $timestamp );
158 return $rev ? $rev->getText() : null;
159 }
160
161 /**
162 * Return a Revision object containing data for the deleted revision.
163 * Note that the result *may* or *may not* have a null page ID.
164 * @param string $timestamp
165 * @return Revision
166 */
167 function getRevision( $timestamp ) {
168 $dbr = wfGetDB( DB_SLAVE );
169 $row = $dbr->selectRow( 'archive',
170 array(
171 'ar_rev_id',
172 'ar_text',
173 'ar_comment',
174 'ar_user',
175 'ar_user_text',
176 'ar_timestamp',
177 'ar_minor_edit',
178 'ar_flags',
179 'ar_text_id',
180 'ar_len' ),
181 array( 'ar_namespace' => $this->title->getNamespace(),
182 'ar_title' => $this->title->getDbkey(),
183 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
184 __METHOD__ );
185 if( $row ) {
186 return new Revision( array(
187 'page' => $this->title->getArticleId(),
188 'id' => $row->ar_rev_id,
189 'text' => ($row->ar_text_id
190 ? null
191 : Revision::getRevisionText( $row, 'ar_' ) ),
192 'comment' => $row->ar_comment,
193 'user' => $row->ar_user,
194 'user_text' => $row->ar_user_text,
195 'timestamp' => $row->ar_timestamp,
196 'minor_edit' => $row->ar_minor_edit,
197 'text_id' => $row->ar_text_id ) );
198 } else {
199 return null;
200 }
201 }
202
203 /**
204 * Return the most-previous revision, either live or deleted, against
205 * the deleted revision given by timestamp.
206 *
207 * May produce unexpected results in case of history merges or other
208 * unusual time issues.
209 *
210 * @param string $timestamp
211 * @return Revision or null
212 */
213 function getPreviousRevision( $timestamp ) {
214 $dbr = wfGetDB( DB_SLAVE );
215
216 // Check the previous deleted revision...
217 $row = $dbr->selectRow( 'archive',
218 'ar_timestamp',
219 array( 'ar_namespace' => $this->title->getNamespace(),
220 'ar_title' => $this->title->getDbkey(),
221 'ar_timestamp < ' .
222 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
223 __METHOD__,
224 array(
225 'ORDER BY' => 'ar_timestamp DESC',
226 'LIMIT' => 1 ) );
227 $prevDeleted = $row ? wfTimestamp( TS_MW, $row->ar_timestamp ) : false;
228
229 $row = $dbr->selectRow( array( 'page', 'revision' ),
230 array( 'rev_id', 'rev_timestamp' ),
231 array(
232 'page_namespace' => $this->title->getNamespace(),
233 'page_title' => $this->title->getDbkey(),
234 'page_id = rev_page',
235 'rev_timestamp < ' .
236 $dbr->addQuotes( $dbr->timestamp( $timestamp ) ) ),
237 __METHOD__,
238 array(
239 'ORDER BY' => 'rev_timestamp DESC',
240 'LIMIT' => 1 ) );
241 $prevLive = $row ? wfTimestamp( TS_MW, $row->rev_timestamp ) : false;
242 $prevLiveId = $row ? intval( $row->rev_id ) : null;
243
244 if( $prevLive && $prevLive > $prevDeleted ) {
245 // Most prior revision was live
246 return Revision::newFromId( $prevLiveId );
247 } elseif( $prevDeleted ) {
248 // Most prior revision was deleted
249 return $this->getRevision( $prevDeleted );
250 } else {
251 // No prior revision on this page.
252 return null;
253 }
254 }
255
256 /**
257 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
258 */
259 function getTextFromRow( $row ) {
260 if( is_null( $row->ar_text_id ) ) {
261 // An old row from MediaWiki 1.4 or previous.
262 // Text is embedded in this row in classic compression format.
263 return Revision::getRevisionText( $row, "ar_" );
264 } else {
265 // New-style: keyed to the text storage backend.
266 $dbr = wfGetDB( DB_SLAVE );
267 $text = $dbr->selectRow( 'text',
268 array( 'old_text', 'old_flags' ),
269 array( 'old_id' => $row->ar_text_id ),
270 __METHOD__ );
271 return Revision::getRevisionText( $text );
272 }
273 }
274
275
276 /**
277 * Fetch (and decompress if necessary) the stored text of the most
278 * recently edited deleted revision of the page.
279 *
280 * If there are no archived revisions for the page, returns NULL.
281 *
282 * @return string
283 */
284 function getLastRevisionText() {
285 $dbr = wfGetDB( DB_SLAVE );
286 $row = $dbr->selectRow( 'archive',
287 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
288 array( 'ar_namespace' => $this->title->getNamespace(),
289 'ar_title' => $this->title->getDBkey() ),
290 'PageArchive::getLastRevisionText',
291 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
292 if( $row ) {
293 return $this->getTextFromRow( $row );
294 } else {
295 return NULL;
296 }
297 }
298
299 /**
300 * Quick check if any archived revisions are present for the page.
301 * @return bool
302 */
303 function isDeleted() {
304 $dbr = wfGetDB( DB_SLAVE );
305 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
306 array( 'ar_namespace' => $this->title->getNamespace(),
307 'ar_title' => $this->title->getDBkey() ) );
308 return ($n > 0);
309 }
310
311 const UNDELETE_NOTHINGRESTORED = 0; // No revisions could be restored
312 const UNDELETE_NOTAVAIL = -1; // Not all requested revisions are available
313 const UNDELETE_UNKNOWNERR = -2; // Unknown error
314 /**
315 * Restore the given (or all) text and file revisions for the page.
316 * Once restored, the items will be removed from the archive tables.
317 * The deletion log will be updated with an undeletion notice.
318 *
319 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
320 * @param string $comment
321 * @param array $fileVersions
322 *
323 * @return array(number of revisions restored, number of file versions restored, log reason) on success or UNDELETE_* on failure
324 */
325 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
326 // If both the set of text revisions and file revisions are empty,
327 // restore everything. Otherwise, just restore the requested items.
328 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
329
330 $restoreText = $restoreAll || !empty( $timestamps );
331 $restoreFiles = $restoreAll || !empty( $fileVersions );
332
333 if( $restoreFiles && $this->title->getNamespace() == NS_IMAGE ) {
334 $img = wfLocalFile( $this->title );
335 $this->fileStatus = $img->restore( $fileVersions );
336 $filesRestored = $this->fileStatus->successCount;
337 } else {
338 $filesRestored = 0;
339 }
340
341 if( $restoreText ) {
342 $textRestored = $this->undeleteRevisions( $timestamps );
343 if($textRestored < 0) // It must be one of UNDELETE_*
344 return $textRestored;
345 } else {
346 $textRestored = 0;
347 }
348
349 // Touch the log!
350 global $wgContLang;
351 $log = new LogPage( 'delete' );
352
353 if( $textRestored && $filesRestored ) {
354 $reason = wfMsgExt( 'undeletedrevisions-files', array( 'content', 'parsemag' ),
355 $wgContLang->formatNum( $textRestored ),
356 $wgContLang->formatNum( $filesRestored ) );
357 } elseif( $textRestored ) {
358 $reason = wfMsgExt( 'undeletedrevisions', array( 'content', 'parsemag' ),
359 $wgContLang->formatNum( $textRestored ) );
360 } elseif( $filesRestored ) {
361 $reason = wfMsgExt( 'undeletedfiles', array( 'content', 'parsemag' ),
362 $wgContLang->formatNum( $filesRestored ) );
363 } else {
364 wfDebug( "Undelete: nothing undeleted...\n" );
365 return self::UNDELETE_NOTHINGRESTORED;
366 }
367
368 if( trim( $comment ) != '' )
369 $reason .= ": {$comment}";
370 $log->addEntry( 'restore', $this->title, $reason );
371
372 return array($textRestored, $filesRestored, $reason);
373 }
374
375 /**
376 * This is the meaty bit -- restores archived revisions of the given page
377 * to the cur/old tables. If the page currently exists, all revisions will
378 * be stuffed into old, otherwise the most recent will go into cur.
379 *
380 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
381 * @param string $comment
382 * @param array $fileVersions
383 *
384 * @return int number of revisions restored on success or UNDELETE_* on failure
385 */
386 private function undeleteRevisions( $timestamps ) {
387 if ( wfReadOnly() ) return 0;
388
389 $restoreAll = empty( $timestamps );
390
391 $dbw = wfGetDB( DB_MASTER );
392
393 # Does this page already exist? We'll have to update it...
394 $article = new Article( $this->title );
395 $options = 'FOR UPDATE';
396 $page = $dbw->selectRow( 'page',
397 array( 'page_id', 'page_latest' ),
398 array( 'page_namespace' => $this->title->getNamespace(),
399 'page_title' => $this->title->getDBkey() ),
400 __METHOD__,
401 $options );
402 if( $page ) {
403 # Page already exists. Import the history, and if necessary
404 # we'll update the latest revision field in the record.
405 $newid = 0;
406 $pageId = $page->page_id;
407 $previousRevId = $page->page_latest;
408 } else {
409 # Have to create a new article...
410 $newid = $article->insertOn( $dbw );
411 $pageId = $newid;
412 $previousRevId = 0;
413 }
414
415 if( $restoreAll ) {
416 $oldones = '1 = 1'; # All revisions...
417 } else {
418 $oldts = implode( ',',
419 array_map( array( &$dbw, 'addQuotes' ),
420 array_map( array( &$dbw, 'timestamp' ),
421 $timestamps ) ) );
422
423 $oldones = "ar_timestamp IN ( {$oldts} )";
424 }
425
426 /**
427 * Restore each revision...
428 */
429 $result = $dbw->select( 'archive',
430 /* fields */ array(
431 'ar_rev_id',
432 'ar_text',
433 'ar_comment',
434 'ar_user',
435 'ar_user_text',
436 'ar_timestamp',
437 'ar_minor_edit',
438 'ar_flags',
439 'ar_text_id',
440 'ar_len' ),
441 /* WHERE */ array(
442 'ar_namespace' => $this->title->getNamespace(),
443 'ar_title' => $this->title->getDBkey(),
444 $oldones ),
445 __METHOD__,
446 /* options */ array(
447 'ORDER BY' => 'ar_timestamp' )
448 );
449 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
450 wfDebug( __METHOD__.": couldn't find all requested rows\n" );
451 return self::UNDELETE_NOTAVAIL;
452 }
453
454 $revision = null;
455 $restored = 0;
456
457 while( $row = $dbw->fetchObject( $result ) ) {
458 if( $row->ar_text_id ) {
459 // Revision was deleted in 1.5+; text is in
460 // the regular text table, use the reference.
461 // Specify null here so the so the text is
462 // dereferenced for page length info if needed.
463 $revText = null;
464 } else {
465 // Revision was deleted in 1.4 or earlier.
466 // Text is squashed into the archive row, and
467 // a new text table entry will be created for it.
468 $revText = Revision::getRevisionText( $row, 'ar_' );
469 }
470 $revision = new Revision( array(
471 'page' => $pageId,
472 'id' => $row->ar_rev_id,
473 'text' => $revText,
474 'comment' => $row->ar_comment,
475 'user' => $row->ar_user,
476 'user_text' => $row->ar_user_text,
477 'timestamp' => $row->ar_timestamp,
478 'minor_edit' => $row->ar_minor_edit,
479 'text_id' => $row->ar_text_id,
480 'len' => $row->ar_len
481 ) );
482 $revision->insertOn( $dbw );
483 $restored++;
484 }
485 // Was anything restored at all?
486 if($restored == 0)
487 return 0;
488
489 if( $revision ) {
490 // Attach the latest revision to the page...
491 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
492
493 if( $newid || $wasnew ) {
494 // Update site stats, link tables, etc
495 $article->createUpdates( $revision );
496 }
497
498 if( $newid ) {
499 wfRunHooks( 'ArticleUndelete', array( &$this->title, true ) );
500 Article::onArticleCreate( $this->title );
501 } else {
502 wfRunHooks( 'ArticleUndelete', array( &$this->title, false ) );
503 Article::onArticleEdit( $this->title );
504 }
505 } else {
506 // Revision couldn't be created. This is very weird
507 return self::UNDELETE_UNKNOWNERR;
508 }
509
510 # Now that it's safely stored, take it out of the archive
511 $dbw->delete( 'archive',
512 /* WHERE */ array(
513 'ar_namespace' => $this->title->getNamespace(),
514 'ar_title' => $this->title->getDBkey(),
515 $oldones ),
516 __METHOD__ );
517
518 return $restored;
519 }
520
521 function getFileStatus() { return $this->fileStatus; }
522 }
523
524 /**
525 * The HTML form for Special:Undelete, which allows users with the appropriate
526 * permissions to view and restore deleted content.
527 * @addtogroup SpecialPage
528 */
529 class UndeleteForm {
530 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
531 var $mTargetTimestamp, $mAllowed, $mComment;
532
533 function UndeleteForm( $request, $par = "" ) {
534 global $wgUser;
535 $this->mAction = $request->getVal( 'action' );
536 $this->mTarget = $request->getVal( 'target' );
537 $this->mSearchPrefix = $request->getText( 'prefix' );
538 $time = $request->getVal( 'timestamp' );
539 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
540 $this->mFile = $request->getVal( 'file' );
541
542 $posted = $request->wasPosted() &&
543 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
544 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
545 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
546 $this->mDiff = $request->getCheck( 'diff' );
547 $this->mComment = $request->getText( 'wpComment' );
548
549 if( $par != "" ) {
550 $this->mTarget = $par;
551 }
552 if ( $wgUser->isAllowed( 'undelete' ) && !$wgUser->isBlocked() ) {
553 $this->mAllowed = true;
554 } else {
555 $this->mAllowed = false;
556 $this->mTimestamp = '';
557 $this->mRestore = false;
558 }
559 if ( $this->mTarget !== "" ) {
560 $this->mTargetObj = Title::newFromURL( $this->mTarget );
561 } else {
562 $this->mTargetObj = NULL;
563 }
564 if( $this->mRestore ) {
565 $timestamps = array();
566 $this->mFileVersions = array();
567 foreach( $_REQUEST as $key => $val ) {
568 $matches = array();
569 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
570 array_push( $timestamps, $matches[1] );
571 }
572
573 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
574 $this->mFileVersions[] = intval( $matches[1] );
575 }
576 }
577 rsort( $timestamps );
578 $this->mTargetTimestamp = $timestamps;
579 }
580 }
581
582 function execute() {
583 global $wgOut;
584 if ( $this->mAllowed ) {
585 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
586 } else {
587 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
588 }
589
590 if( is_null( $this->mTargetObj ) ) {
591 $this->showSearchForm();
592
593 # List undeletable articles
594 if( $this->mSearchPrefix ) {
595 $result = PageArchive::listPagesByPrefix(
596 $this->mSearchPrefix );
597 $this->showList( $result );
598 }
599 return;
600 }
601 if( $this->mTimestamp !== '' ) {
602 return $this->showRevision( $this->mTimestamp );
603 }
604 if( $this->mFile !== null ) {
605 return $this->showFile( $this->mFile );
606 }
607 if( $this->mRestore && $this->mAction == "submit" ) {
608 return $this->undelete();
609 }
610 return $this->showHistory();
611 }
612
613 function showSearchForm() {
614 global $wgOut, $wgScript;
615 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
616
617 $wgOut->addHtml(
618 Xml::openElement( 'form', array(
619 'method' => 'get',
620 'action' => $wgScript ) ) .
621 '<fieldset>' .
622 Xml::element( 'legend', array(),
623 wfMsg( 'undelete-search-box' ) ) .
624 Xml::hidden( 'title',
625 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
626 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
627 'prefix', 'prefix', 20,
628 $this->mSearchPrefix ) .
629 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
630 '</fieldset>' .
631 '</form>' );
632 }
633
634 /* private */ function showList( $result ) {
635 global $wgLang, $wgContLang, $wgUser, $wgOut;
636
637 if( $result->numRows() == 0 ) {
638 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
639 return;
640 }
641
642 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
643
644 $sk = $wgUser->getSkin();
645 $undelete = SpecialPage::getTitleFor( 'Undelete' );
646 $wgOut->addHTML( "<ul>\n" );
647 while( $row = $result->fetchObject() ) {
648 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
649 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
650 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
651 $revs = wfMsgExt( 'undeleterevisions',
652 array( 'parseinline' ),
653 $wgLang->formatNum( $row->count ) );
654 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
655 }
656 $result->free();
657 $wgOut->addHTML( "</ul>\n" );
658
659 return true;
660 }
661
662 /* private */ function showRevision( $timestamp ) {
663 global $wgLang, $wgUser, $wgOut;
664 $self = SpecialPage::getTitleFor( 'Undelete' );
665 $skin = $wgUser->getSkin();
666
667 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
668
669 $archive = new PageArchive( $this->mTargetObj );
670 $rev = $archive->getRevision( $timestamp );
671
672 if( !$rev ) {
673 $wgOut->addWikiTexT( wfMsg( 'undeleterevision-missing' ) );
674 return;
675 }
676
677 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
678
679 $link = $skin->makeKnownLinkObj(
680 SpecialPage::getTitleFor( 'Undelete', $this->mTargetObj->getPrefixedDBkey() ),
681 htmlspecialchars( $this->mTargetObj->getPrefixedText() )
682 );
683 $time = htmlspecialchars( $wgLang->timeAndDate( $timestamp, true ) );
684 $user = $skin->userLink( $rev->getUser(), $rev->getUserText() )
685 . $skin->userToolLinks( $rev->getUser(), $rev->getUserText() );
686
687 if( $this->mDiff ) {
688 $previousRev = $archive->getPreviousRevision( $timestamp );
689 if( $previousRev ) {
690 $this->showDiff( $previousRev, $rev );
691 if( $wgUser->getOption( 'diffonly' ) ) {
692 return;
693 } else {
694 $wgOut->addHtml( '<hr />' );
695 }
696 } else {
697 $wgOut->addHtml( wfMsgHtml( 'undelete-nodiff' ) );
698 }
699 }
700
701 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link, $time, $user ) . '</p>' );
702
703 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
704
705 if( $this->mPreview ) {
706 $wgOut->addHtml( "<hr />\n" );
707 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj, false );
708 }
709
710 $wgOut->addHtml(
711 wfElement( 'textarea', array(
712 'readonly' => 'readonly',
713 'cols' => intval( $wgUser->getOption( 'cols' ) ),
714 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
715 $rev->getText() . "\n" ) .
716 wfOpenElement( 'div' ) .
717 wfOpenElement( 'form', array(
718 'method' => 'post',
719 'action' => $self->getLocalURL( "action=submit" ) ) ) .
720 wfElement( 'input', array(
721 'type' => 'hidden',
722 'name' => 'target',
723 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
724 wfElement( 'input', array(
725 'type' => 'hidden',
726 'name' => 'timestamp',
727 'value' => $timestamp ) ) .
728 wfElement( 'input', array(
729 'type' => 'hidden',
730 'name' => 'wpEditToken',
731 'value' => $wgUser->editToken() ) ) .
732 wfElement( 'input', array(
733 'type' => 'submit',
734 'name' => 'preview',
735 'value' => wfMsg( 'showpreview' ) ) ) .
736 wfElement( 'input', array(
737 'name' => 'diff',
738 'type' => 'submit',
739 'value' => wfMsg( 'showdiff' ) ) ) .
740 wfCloseElement( 'form' ) .
741 wfCloseElement( 'div' ) );
742 }
743
744 /**
745 * Build a diff display between this and the previous either deleted
746 * or non-deleted edit.
747 * @param Revision $previousRev
748 * @param Revision $currentRev
749 * @return string HTML
750 */
751 function showDiff( $previousRev, $currentRev ) {
752 global $wgOut, $wgUser;
753
754 $diffEngine = new DifferenceEngine();
755 $diffEngine->showDiffStyle();
756 $wgOut->addHtml(
757 "<div>" .
758 "<table border='0' width='98%' cellpadding='0' cellspacing='4' class='diff'>" .
759 "<col class='diff-marker' />" .
760 "<col class='diff-content' />" .
761 "<col class='diff-marker' />" .
762 "<col class='diff-content' />" .
763 "<tr>" .
764 "<td colspan='2' width='50%' align='center' class='diff-otitle'>" .
765 $this->diffHeader( $previousRev ) .
766 "</td>" .
767 "<td colspan='2' width='50%' align='center' class='diff-ntitle'>" .
768 $this->diffHeader( $currentRev ) .
769 "</td>" .
770 "</tr>" .
771 $diffEngine->generateDiffBody(
772 $previousRev->getText(), $currentRev->getText() ) .
773 "</table>" .
774 "</div>\n" );
775
776 }
777
778 private function diffHeader( $rev ) {
779 global $wgUser, $wgLang, $wgLang;
780 $sk = $wgUser->getSkin();
781 $isDeleted = !( $rev->getId() && $rev->getTitle() );
782 if( $isDeleted ) {
783 /// @fixme $rev->getTitle() is null for deleted revs...?
784 $targetPage = SpecialPage::getTitleFor( 'Undelete' );
785 $targetQuery = 'target=' .
786 $this->mTargetObj->getPrefixedUrl() .
787 '&timestamp=' .
788 wfTimestamp( TS_MW, $rev->getTimestamp() );
789 } else {
790 /// @fixme getId() may return non-zero for deleted revs...
791 $targetPage = $rev->getTitle();
792 $targetQuery = 'oldid=' . $rev->getId();
793 }
794 return
795 '<div id="mw-diff-otitle1"><strong>' .
796 $sk->makeLinkObj( $targetPage,
797 wfMsgHtml( 'revisionasof',
798 $wgLang->timeanddate( $rev->getTimestamp(), true ) ),
799 $targetQuery ) .
800 ( $isDeleted ? ' ' . wfMsgHtml( 'deletedrev' ) : '' ) .
801 '</strong></div>' .
802 '<div id="mw-diff-otitle2">' .
803 $sk->revUserTools( $rev ) . '<br/>' .
804 '</div>' .
805 '<div id="mw-diff-otitle3">' .
806 $sk->revComment( $rev ) . '<br/>' .
807 '</div>';
808 }
809
810 /**
811 * Show a deleted file version requested by the visitor.
812 */
813 function showFile( $key ) {
814 global $wgOut, $wgRequest;
815 $wgOut->disable();
816
817 # We mustn't allow the output to be Squid cached, otherwise
818 # if an admin previews a deleted image, and it's cached, then
819 # a user without appropriate permissions can toddle off and
820 # nab the image, and Squid will serve it
821 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
822 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
823 $wgRequest->response()->header( 'Pragma: no-cache' );
824
825 $store = FileStore::get( 'deleted' );
826 $store->stream( $key );
827 }
828
829 /* private */ function showHistory() {
830 global $wgLang, $wgContLang, $wgUser, $wgOut, $wgRequest;
831
832 $sk = $wgUser->getSkin();
833 if ( $this->mAllowed ) {
834 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
835 } else {
836 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
837 }
838
839 $archive = new PageArchive( $this->mTargetObj );
840 /*
841 $text = $archive->getLastRevisionText();
842 if( is_null( $text ) ) {
843 $wgOut->addWikiText( wfMsg( "nohistory" ) );
844 return;
845 }
846 */
847 if ( $this->mAllowed ) {
848 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
849 } else {
850 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
851 }
852
853 # List all stored revisions
854 $tmpLimit = $wgRequest->getIntOrNull ( 'limit' );
855 $tmpLimit = (is_null($tmpLimit))? 5001 : $tmpLimit + 1;
856 $revisions = $archive->listRevisions( $wgRequest->getVal ( 'offset' ),
857 $tmpLimit );
858 $files = $archive->listFiles();
859
860 $haveRevisions = $revisions && $revisions->numRows() > 0;
861 $haveFiles = $files && $files->numRows() > 0;
862
863 $hasMore = false;
864 if ( $revisions && $revisions->numRows() >= $tmpLimit ) {
865 $revisions->seek ( $revisions->numRows() - 2 );
866 $tmp = $revisions->fetchObject();
867 $revisions->rewind ( );
868
869 $titleObj = SpecialPage::getTitleFor ( 'Undelete' );
870 $tmplink = $sk->makeKnownLinkObj ( $titleObj, wfMsg( 'undelete-next-revs', 5000 ),
871 "target={$this->mTarget}&limit=5000&offset={$tmp->ar_timestamp}" );
872
873 $wgOut->addHTML ( wfMsg ( 'undelete-more-revs', $tmpLimit - 1, $tmplink ) );
874 $hasMore = true;
875 }
876 # Batch existence check on user and talk pages
877 if( $haveRevisions ) {
878 $batch = new LinkBatch();
879 while( $row = $revisions->fetchObject() ) {
880 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
881 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
882 }
883 $batch->execute();
884 $revisions->seek( 0 );
885 }
886 if( $haveFiles ) {
887 $batch = new LinkBatch();
888 while( $row = $files->fetchObject() ) {
889 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
890 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
891 }
892 $batch->execute();
893 $files->seek( 0 );
894 }
895
896 if ( $this->mAllowed ) {
897 $titleObj = SpecialPage::getTitleFor( "Undelete" );
898 $action = $titleObj->getLocalURL( "action=submit" );
899 # Start the form here
900 $top = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
901 $wgOut->addHtml( $top );
902 }
903
904 # Show relevant lines from the deletion log:
905 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
906 $logViewer = new LogViewer(
907 new LogReader(
908 new FauxRequest(
909 array(
910 'page' => $this->mTargetObj->getPrefixedText(),
911 'type' => 'delete'
912 )
913 )
914 ), LogViewer::NO_ACTION_LINK
915 );
916 $logViewer->showList( $wgOut );
917
918 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
919 # Format the user-visible controls (comment field, submission button)
920 # in a nice little table
921 $align = $wgContLang->isRtl() ? 'left' : 'right';
922 $table =
923 Xml::openElement( 'fieldset' ) .
924 Xml::openElement( 'table' ) .
925 "<tr>
926 <td colspan='2'>" .
927 wfMsgWikiHtml( 'undeleteextrahelp' ) .
928 "</td>
929 </tr>
930 <tr>
931 <td align='$align'>" .
932 Xml::label( wfMsg( 'undeletecomment' ), 'wpComment' ) .
933 "</td>
934 <td>" .
935 Xml::input( 'wpComment', 50, $this->mComment ) .
936 "</td>
937 </tr>
938 <tr>
939 <td>&nbsp;</td>
940 <td>" .
941 Xml::submitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore', 'id' => 'mw-undelete-submit' ) ) .
942 Xml::element( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ), 'id' => 'mw-undelete-reset' ) ) .
943 "</td>
944 </tr>" .
945 Xml::closeElement( 'table' ) .
946 Xml::closeElement( 'fieldset' );
947
948 $wgOut->addHtml( $table );
949 }
950
951 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
952
953 if( $haveRevisions ) {
954 # The page's stored (deleted) history:
955 $wgOut->addHTML("<ul>");
956 $target = urlencode( $this->mTarget );
957 $remaining = $revisions->numRows();
958 $earliestLiveTime = $this->getEarliestTime( $this->mTargetObj );
959
960 if ( $hasMore ) $remaining --;
961
962 while( ( $row = $revisions->fetchObject() ) && $remaining-- ) {
963 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
964 if ( $this->mAllowed ) {
965 $checkBox = Xml::check( "ts$ts" );
966 $pageLink = $sk->makeKnownLinkObj( $titleObj,
967 $wgLang->timeanddate( $ts, true ),
968 "target=$target&timestamp=$ts" );
969 if( ($remaining > 0 || $hasMore ) ||
970 ($earliestLiveTime && $ts > $earliestLiveTime ) ) {
971 $diffLink = '(' .
972 $sk->makeKnownLinkObj( $titleObj,
973 wfMsgHtml( 'diff' ),
974 "target=$target&timestamp=$ts&diff=prev" ) .
975 ')';
976 } else {
977 // No older revision to diff against
978 $diffLink = '';
979 }
980 } else {
981 $checkBox = '';
982 $pageLink = $wgLang->timeanddate( $ts, true );
983 $diffLink = '';
984 }
985 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
986 $stxt = '';
987 if (!is_null($size = $row->ar_len)) {
988 if ($size == 0) {
989 $stxt = wfMsgHtml('historyempty');
990 } else {
991 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
992 }
993 }
994 $comment = $sk->commentBlock( $row->ar_comment );
995 $wgOut->addHTML( "<li>$checkBox $pageLink $diffLink . . $userLink $stxt $comment</li>\n" );
996
997 }
998 $revisions->free();
999 $wgOut->addHTML("</ul>");
1000 } else {
1001 $wgOut->addWikiText( wfMsg( "nohistory" ) );
1002 }
1003
1004 if( $haveFiles ) {
1005 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'filehist' ) . "</h2>\n" );
1006 $wgOut->addHtml( "<ul>" );
1007 while( $row = $files->fetchObject() ) {
1008 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
1009 if ( $this->mAllowed && $row->fa_storage_key ) {
1010 $checkBox = Xml::check( "fileid" . $row->fa_id );
1011 $key = urlencode( $row->fa_storage_key );
1012 $target = urlencode( $this->mTarget );
1013 $pageLink = $sk->makeKnownLinkObj( $titleObj,
1014 $wgLang->timeanddate( $ts, true ),
1015 "target=$target&file=$key" );
1016 } else {
1017 $checkBox = '';
1018 $pageLink = $wgLang->timeanddate( $ts, true );
1019 }
1020 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
1021 $data =
1022 wfMsgHtml( 'widthheight',
1023 $wgLang->formatNum( $row->fa_width ),
1024 $wgLang->formatNum( $row->fa_height ) ) .
1025 ' (' .
1026 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
1027 ')';
1028 $comment = $sk->commentBlock( $row->fa_description );
1029 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
1030 }
1031 $files->free();
1032 $wgOut->addHTML( "</ul>" );
1033 }
1034
1035 if ( $this->mAllowed ) {
1036 # Slip in the hidden controls here
1037 $misc = Xml::hidden( 'target', $this->mTarget );
1038 $misc .= Xml::hidden( 'wpEditToken', $wgUser->editToken() );
1039 $misc .= Xml::closeElement( 'form' );
1040 $wgOut->addHtml( $misc );
1041 }
1042
1043 return true;
1044 }
1045
1046 private function getEarliestTime( $title ) {
1047 $dbr = wfGetDB( DB_SLAVE );
1048 if( $title->exists() ) {
1049 $min = $dbr->selectField( 'revision',
1050 'MIN(rev_timestamp)',
1051 array( 'rev_page' => $title->getArticleId() ),
1052 __METHOD__ );
1053 return wfTimestampOrNull( TS_MW, $min );
1054 }
1055 return null;
1056 }
1057
1058 function undelete() {
1059 global $wgOut, $wgUser;
1060 if( !is_null( $this->mTargetObj ) ) {
1061 $archive = new PageArchive( $this->mTargetObj );
1062
1063 $ok = $archive->undelete(
1064 $this->mTargetTimestamp,
1065 $this->mComment,
1066 $this->mFileVersions );
1067
1068 if( is_array($ok) ) {
1069 $skin = $wgUser->getSkin();
1070 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
1071 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
1072 } else {
1073 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1074 }
1075
1076 // Show file deletion warnings and errors
1077 $status = $archive->getFileStatus();
1078 if ( $status && !$status->isGood() ) {
1079 $wgOut->addWikiText( $status->getWikiText( 'undelete-error-short', 'undelete-error-long' ) );
1080 }
1081 } else {
1082 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
1083 }
1084 return false;
1085 }
1086 }