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