* API: added categories property
[mediawiki.git] / includes / SpecialUndelete.php
blob7a322dbd40ebd50c0443418772581c2d794da814
1 <?php
3 /**
4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
7 * @addtogroup SpecialPage
8 */
10 /**
11 * Constructor
13 function wfSpecialUndelete( $par ) {
14 global $wgRequest;
16 $form = new UndeleteForm( $wgRequest, $par );
17 $form->execute();
20 /**
21 * Used to show archived pages and eventually restore them.
22 * @addtogroup SpecialPage
24 class PageArchive {
25 protected $title;
27 function __construct( $title ) {
28 if( is_null( $title ) ) {
29 throw new MWException( 'Archiver() given a null title.');
31 $this->title = $title;
34 /**
35 * List all deleted pages recorded in the archive table. Returns result
36 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
37 * namespace/title.
39 * @return ResultWrapper
41 public static function listAllPages() {
42 $dbr = wfGetDB( DB_SLAVE );
43 return self::listPages( $dbr, '' );
46 /**
47 * List deleted pages recorded in the archive table matching the
48 * given title prefix.
49 * Returns result wrapper with (ar_namespace, ar_title, count) fields.
51 * @return ResultWrapper
53 public static function listPagesByPrefix( $prefix ) {
54 $dbr = wfGetDB( DB_SLAVE );
56 $title = Title::newFromText( $prefix );
57 if( $title ) {
58 $ns = $title->getNamespace();
59 $encPrefix = $dbr->escapeLike( $title->getDbKey() );
60 } else {
61 // Prolly won't work too good
62 // @todo handle bare namespace names cleanly?
63 $ns = 0;
64 $encPrefix = $dbr->escapeLike( $prefix );
66 $conds = array(
67 'ar_namespace' => $ns,
68 "ar_title LIKE '$encPrefix%'",
70 return self::listPages( $dbr, $conds );
73 protected static function listPages( $dbr, $condition ) {
74 return $dbr->resultObject(
75 $dbr->select(
76 array( 'archive' ),
77 array(
78 'ar_namespace',
79 'ar_title',
80 'COUNT(*) AS count',
82 $condition,
83 __METHOD__,
84 array(
85 'GROUP BY' => 'ar_namespace,ar_title',
86 'ORDER BY' => 'ar_namespace,ar_title',
87 'LIMIT' => 100,
93 /**
94 * List the revisions of the given page. Returns result wrapper with
95 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
97 * @return ResultWrapper
99 function listRevisions() {
100 $dbr = wfGetDB( DB_SLAVE );
101 $res = $dbr->select( 'archive',
102 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment', 'ar_len' ),
103 array( 'ar_namespace' => $this->title->getNamespace(),
104 'ar_title' => $this->title->getDBkey() ),
105 'PageArchive::listRevisions',
106 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
107 $ret = $dbr->resultObject( $res );
108 return $ret;
112 * List the deleted file revisions for this page, if it's a file page.
113 * Returns a result wrapper with various filearchive fields, or null
114 * if not a file page.
116 * @return ResultWrapper
117 * @todo Does this belong in Image for fuller encapsulation?
119 function listFiles() {
120 if( $this->title->getNamespace() == NS_IMAGE ) {
121 $dbr = wfGetDB( DB_SLAVE );
122 $res = $dbr->select( 'filearchive',
123 array(
124 'fa_id',
125 'fa_name',
126 'fa_storage_key',
127 'fa_size',
128 'fa_width',
129 'fa_height',
130 'fa_description',
131 'fa_user',
132 'fa_user_text',
133 'fa_timestamp' ),
134 array( 'fa_name' => $this->title->getDbKey() ),
135 __METHOD__,
136 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
137 $ret = $dbr->resultObject( $res );
138 return $ret;
140 return null;
144 * Fetch (and decompress if necessary) the stored text for the deleted
145 * revision of the page with the given timestamp.
147 * @return string
148 * @deprecated Use getRevision() for more flexible information
150 function getRevisionText( $timestamp ) {
151 $rev = $this->getRevision( $timestamp );
152 return $rev ? $rev->getText() : null;
156 * Return a Revision object containing data for the deleted revision.
157 * Note that the result *may* or *may not* have a null page ID.
158 * @param string $timestamp
159 * @return Revision
161 function getRevision( $timestamp ) {
162 $dbr = wfGetDB( DB_SLAVE );
163 $row = $dbr->selectRow( 'archive',
164 array(
165 'ar_rev_id',
166 'ar_text',
167 'ar_comment',
168 'ar_user',
169 'ar_user_text',
170 'ar_timestamp',
171 'ar_minor_edit',
172 'ar_flags',
173 'ar_text_id',
174 'ar_len' ),
175 array( 'ar_namespace' => $this->title->getNamespace(),
176 'ar_title' => $this->title->getDbkey(),
177 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
178 __METHOD__ );
179 if( $row ) {
180 return new Revision( array(
181 'page' => $this->title->getArticleId(),
182 'id' => $row->ar_rev_id,
183 'text' => ($row->ar_text_id
184 ? null
185 : Revision::getRevisionText( $row, 'ar_' ) ),
186 'comment' => $row->ar_comment,
187 'user' => $row->ar_user,
188 'user_text' => $row->ar_user_text,
189 'timestamp' => $row->ar_timestamp,
190 'minor_edit' => $row->ar_minor_edit,
191 'text_id' => $row->ar_text_id ) );
192 } else {
193 return null;
198 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
200 function getTextFromRow( $row ) {
201 if( is_null( $row->ar_text_id ) ) {
202 // An old row from MediaWiki 1.4 or previous.
203 // Text is embedded in this row in classic compression format.
204 return Revision::getRevisionText( $row, "ar_" );
205 } else {
206 // New-style: keyed to the text storage backend.
207 $dbr = wfGetDB( DB_SLAVE );
208 $text = $dbr->selectRow( 'text',
209 array( 'old_text', 'old_flags' ),
210 array( 'old_id' => $row->ar_text_id ),
211 __METHOD__ );
212 return Revision::getRevisionText( $text );
218 * Fetch (and decompress if necessary) the stored text of the most
219 * recently edited deleted revision of the page.
221 * If there are no archived revisions for the page, returns NULL.
223 * @return string
225 function getLastRevisionText() {
226 $dbr = wfGetDB( DB_SLAVE );
227 $row = $dbr->selectRow( 'archive',
228 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
229 array( 'ar_namespace' => $this->title->getNamespace(),
230 'ar_title' => $this->title->getDBkey() ),
231 'PageArchive::getLastRevisionText',
232 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
233 if( $row ) {
234 return $this->getTextFromRow( $row );
235 } else {
236 return NULL;
241 * Quick check if any archived revisions are present for the page.
242 * @return bool
244 function isDeleted() {
245 $dbr = wfGetDB( DB_SLAVE );
246 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
247 array( 'ar_namespace' => $this->title->getNamespace(),
248 'ar_title' => $this->title->getDBkey() ) );
249 return ($n > 0);
253 * Restore the given (or all) text and file revisions for the page.
254 * Once restored, the items will be removed from the archive tables.
255 * The deletion log will be updated with an undeletion notice.
257 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
258 * @param string $comment
259 * @param array $fileVersions
261 * @return true on success.
263 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
264 // If both the set of text revisions and file revisions are empty,
265 // restore everything. Otherwise, just restore the requested items.
266 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
268 $restoreText = $restoreAll || !empty( $timestamps );
269 $restoreFiles = $restoreAll || !empty( $fileVersions );
271 if( $restoreFiles && $this->title->getNamespace() == NS_IMAGE ) {
272 $img = new Image( $this->title );
273 $filesRestored = $img->restore( $fileVersions );
274 } else {
275 $filesRestored = 0;
278 if( $restoreText ) {
279 $textRestored = $this->undeleteRevisions( $timestamps );
280 } else {
281 $textRestored = 0;
284 // Touch the log!
285 global $wgContLang;
286 $log = new LogPage( 'delete' );
288 if( $textRestored && $filesRestored ) {
289 $reason = wfMsgForContent( 'undeletedrevisions-files',
290 $wgContLang->formatNum( $textRestored ),
291 $wgContLang->formatNum( $filesRestored ) );
292 } elseif( $textRestored ) {
293 $reason = wfMsgForContent( 'undeletedrevisions',
294 $wgContLang->formatNum( $textRestored ) );
295 } elseif( $filesRestored ) {
296 $reason = wfMsgForContent( 'undeletedfiles',
297 $wgContLang->formatNum( $filesRestored ) );
298 } else {
299 wfDebug( "Undelete: nothing undeleted...\n" );
300 return false;
303 if( trim( $comment ) != '' )
304 $reason .= ": {$comment}";
305 $log->addEntry( 'restore', $this->title, $reason );
307 return true;
311 * This is the meaty bit -- restores archived revisions of the given page
312 * to the cur/old tables. If the page currently exists, all revisions will
313 * be stuffed into old, otherwise the most recent will go into cur.
315 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
316 * @param string $comment
317 * @param array $fileVersions
319 * @return int number of revisions restored
321 private function undeleteRevisions( $timestamps ) {
322 $restoreAll = empty( $timestamps );
324 $dbw = wfGetDB( DB_MASTER );
325 $page = $dbw->tableName( 'archive' );
327 # Does this page already exist? We'll have to update it...
328 $article = new Article( $this->title );
329 $options = 'FOR UPDATE';
330 $page = $dbw->selectRow( 'page',
331 array( 'page_id', 'page_latest' ),
332 array( 'page_namespace' => $this->title->getNamespace(),
333 'page_title' => $this->title->getDBkey() ),
334 __METHOD__,
335 $options );
336 if( $page ) {
337 # Page already exists. Import the history, and if necessary
338 # we'll update the latest revision field in the record.
339 $newid = 0;
340 $pageId = $page->page_id;
341 $previousRevId = $page->page_latest;
342 } else {
343 # Have to create a new article...
344 $newid = $article->insertOn( $dbw );
345 $pageId = $newid;
346 $previousRevId = 0;
349 if( $restoreAll ) {
350 $oldones = '1 = 1'; # All revisions...
351 } else {
352 $oldts = implode( ',',
353 array_map( array( &$dbw, 'addQuotes' ),
354 array_map( array( &$dbw, 'timestamp' ),
355 $timestamps ) ) );
357 $oldones = "ar_timestamp IN ( {$oldts} )";
361 * Restore each revision...
363 $result = $dbw->select( 'archive',
364 /* fields */ array(
365 'ar_rev_id',
366 'ar_text',
367 'ar_comment',
368 'ar_user',
369 'ar_user_text',
370 'ar_timestamp',
371 'ar_minor_edit',
372 'ar_flags',
373 'ar_text_id',
374 'ar_len' ),
375 /* WHERE */ array(
376 'ar_namespace' => $this->title->getNamespace(),
377 'ar_title' => $this->title->getDBkey(),
378 $oldones ),
379 __METHOD__,
380 /* options */ array(
381 'ORDER BY' => 'ar_timestamp' )
383 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
384 wfDebug( __METHOD__.": couldn't find all requested rows\n" );
385 return false;
388 $revision = null;
389 $restored = 0;
391 while( $row = $dbw->fetchObject( $result ) ) {
392 if( $row->ar_text_id ) {
393 // Revision was deleted in 1.5+; text is in
394 // the regular text table, use the reference.
395 // Specify null here so the so the text is
396 // dereferenced for page length info if needed.
397 $revText = null;
398 } else {
399 // Revision was deleted in 1.4 or earlier.
400 // Text is squashed into the archive row, and
401 // a new text table entry will be created for it.
402 $revText = Revision::getRevisionText( $row, 'ar_' );
404 $revision = new Revision( array(
405 'page' => $pageId,
406 'id' => $row->ar_rev_id,
407 'text' => $revText,
408 'comment' => $row->ar_comment,
409 'user' => $row->ar_user,
410 'user_text' => $row->ar_user_text,
411 'timestamp' => $row->ar_timestamp,
412 'minor_edit' => $row->ar_minor_edit,
413 'text_id' => $row->ar_text_id,
414 'len' => $row->ar_len
415 ) );
416 $revision->insertOn( $dbw );
417 $restored++;
420 if( $revision ) {
421 // Attach the latest revision to the page...
422 $wasnew = $article->updateIfNewerOn( $dbw, $revision, $previousRevId );
424 if( $newid || $wasnew ) {
425 // Update site stats, link tables, etc
426 $article->createUpdates( $revision );
429 if( $newid ) {
430 wfRunHooks( 'ArticleUndelete', array( &$this->title, true ) );
431 Article::onArticleCreate( $this->title );
432 } else {
433 wfRunHooks( 'ArticleUndelete', array( &$this->title, false ) );
434 Article::onArticleEdit( $this->title );
436 } else {
437 # Something went terribly wrong!
440 # Now that it's safely stored, take it out of the archive
441 $dbw->delete( 'archive',
442 /* WHERE */ array(
443 'ar_namespace' => $this->title->getNamespace(),
444 'ar_title' => $this->title->getDBkey(),
445 $oldones ),
446 __METHOD__ );
448 return $restored;
454 * The HTML form for Special:Undelete, which allows users with the appropriate
455 * permissions to view and restore deleted content.
456 * @addtogroup SpecialPage
458 class UndeleteForm {
459 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
460 var $mTargetTimestamp, $mAllowed, $mComment;
462 function UndeleteForm( $request, $par = "" ) {
463 global $wgUser;
464 $this->mAction = $request->getVal( 'action' );
465 $this->mTarget = $request->getVal( 'target' );
466 $this->mSearchPrefix = $request->getText( 'prefix' );
467 $time = $request->getVal( 'timestamp' );
468 $this->mTimestamp = $time ? wfTimestamp( TS_MW, $time ) : '';
469 $this->mFile = $request->getVal( 'file' );
471 $posted = $request->wasPosted() &&
472 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
473 $this->mRestore = $request->getCheck( 'restore' ) && $posted;
474 $this->mPreview = $request->getCheck( 'preview' ) && $posted;
475 $this->mComment = $request->getText( 'wpComment' );
477 if( $par != "" ) {
478 $this->mTarget = $par;
480 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
481 $this->mAllowed = true;
482 } else {
483 $this->mAllowed = false;
484 $this->mTimestamp = '';
485 $this->mRestore = false;
487 if ( $this->mTarget !== "" ) {
488 $this->mTargetObj = Title::newFromURL( $this->mTarget );
489 } else {
490 $this->mTargetObj = NULL;
492 if( $this->mRestore ) {
493 $timestamps = array();
494 $this->mFileVersions = array();
495 foreach( $_REQUEST as $key => $val ) {
496 $matches = array();
497 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
498 array_push( $timestamps, $matches[1] );
501 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
502 $this->mFileVersions[] = intval( $matches[1] );
505 rsort( $timestamps );
506 $this->mTargetTimestamp = $timestamps;
510 function execute() {
511 global $wgOut;
512 if ( $this->mAllowed ) {
513 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
514 } else {
515 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
518 if( is_null( $this->mTargetObj ) ) {
519 $this->showSearchForm();
521 # List undeletable articles
522 if( $this->mSearchPrefix ) {
523 $result = PageArchive::listPagesByPrefix(
524 $this->mSearchPrefix );
525 $this->showList( $result );
527 return;
529 if( $this->mTimestamp !== '' ) {
530 return $this->showRevision( $this->mTimestamp );
532 if( $this->mFile !== null ) {
533 return $this->showFile( $this->mFile );
535 if( $this->mRestore && $this->mAction == "submit" ) {
536 return $this->undelete();
538 return $this->showHistory();
541 function showSearchForm() {
542 global $wgOut, $wgScript;
543 $wgOut->addWikiText( wfMsg( 'undelete-header' ) );
545 $wgOut->addHtml(
546 Xml::openElement( 'form', array(
547 'method' => 'get',
548 'action' => $wgScript ) ) .
549 '<fieldset>' .
550 Xml::element( 'legend', array(),
551 wfMsg( 'undelete-search-box' ) ) .
552 Xml::hidden( 'title',
553 SpecialPage::getTitleFor( 'Undelete' )->getPrefixedDbKey() ) .
554 Xml::inputLabel( wfMsg( 'undelete-search-prefix' ),
555 'prefix', 'prefix', 20,
556 $this->mSearchPrefix ) .
557 Xml::submitButton( wfMsg( 'undelete-search-submit' ) ) .
558 '</fieldset>' .
559 '</form>' );
562 /* private */ function showList( $result ) {
563 global $wgLang, $wgContLang, $wgUser, $wgOut;
565 if( $result->numRows() == 0 ) {
566 $wgOut->addWikiText( wfMsg( 'undelete-no-results' ) );
567 return;
570 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
572 $sk = $wgUser->getSkin();
573 $undelete = SpecialPage::getTitleFor( 'Undelete' );
574 $wgOut->addHTML( "<ul>\n" );
575 while( $row = $result->fetchObject() ) {
576 $title = Title::makeTitleSafe( $row->ar_namespace, $row->ar_title );
577 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
578 #$revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count ) );
579 $revs = wfMsgExt( 'undeleterevisions',
580 array( 'parseinline' ),
581 $wgLang->formatNum( $row->count ) );
582 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
584 $result->free();
585 $wgOut->addHTML( "</ul>\n" );
587 return true;
590 /* private */ function showRevision( $timestamp ) {
591 global $wgLang, $wgUser, $wgOut;
592 $self = SpecialPage::getTitleFor( 'Undelete' );
593 $skin = $wgUser->getSkin();
595 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
597 $archive = new PageArchive( $this->mTargetObj );
598 $rev = $archive->getRevision( $timestamp );
600 $wgOut->setPageTitle( wfMsg( 'undeletepage' ) );
601 $link = $skin->makeKnownLinkObj( $self, htmlspecialchars( $this->mTargetObj->getPrefixedText() ),
602 'target=' . $this->mTargetObj->getPrefixedUrl() );
603 $wgOut->addHtml( '<p>' . wfMsgHtml( 'undelete-revision', $link,
604 htmlspecialchars( $wgLang->timeAndDate( $timestamp ) ) ) . '</p>' );
606 if( !$rev ) {
607 $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
608 return;
611 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj, $rev ) );
613 if( $this->mPreview ) {
614 $wgOut->addHtml( "<hr />\n" );
615 $wgOut->addWikiTextTitleTidy( $rev->getText(), $this->mTargetObj, false );
618 $wgOut->addHtml(
619 wfElement( 'textarea', array(
620 'readonly' => true,
621 'cols' => intval( $wgUser->getOption( 'cols' ) ),
622 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
623 $rev->getText() . "\n" ) .
624 wfOpenElement( 'div' ) .
625 wfOpenElement( 'form', array(
626 'method' => 'post',
627 'action' => $self->getLocalURL( "action=submit" ) ) ) .
628 wfElement( 'input', array(
629 'type' => 'hidden',
630 'name' => 'target',
631 'value' => $this->mTargetObj->getPrefixedDbKey() ) ) .
632 wfElement( 'input', array(
633 'type' => 'hidden',
634 'name' => 'timestamp',
635 'value' => $timestamp ) ) .
636 wfElement( 'input', array(
637 'type' => 'hidden',
638 'name' => 'wpEditToken',
639 'value' => $wgUser->editToken() ) ) .
640 wfElement( 'input', array(
641 'type' => 'hidden',
642 'name' => 'preview',
643 'value' => '1' ) ) .
644 wfElement( 'input', array(
645 'type' => 'submit',
646 'value' => wfMsg( 'showpreview' ) ) ) .
647 wfCloseElement( 'form' ) .
648 wfCloseElement( 'div' ) );
652 * Show a deleted file version requested by the visitor.
654 function showFile( $key ) {
655 global $wgOut, $wgRequest;
656 $wgOut->disable();
658 # We mustn't allow the output to be Squid cached, otherwise
659 # if an admin previews a deleted image, and it's cached, then
660 # a user without appropriate permissions can toddle off and
661 # nab the image, and Squid will serve it
662 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', 0 ) . ' GMT' );
663 $wgRequest->response()->header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
664 $wgRequest->response()->header( 'Pragma: no-cache' );
666 $store = FileStore::get( 'deleted' );
667 $store->stream( $key );
670 /* private */ function showHistory() {
671 global $wgLang, $wgUser, $wgOut;
673 $sk = $wgUser->getSkin();
674 if ( $this->mAllowed ) {
675 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
676 } else {
677 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
680 $archive = new PageArchive( $this->mTargetObj );
682 $text = $archive->getLastRevisionText();
683 if( is_null( $text ) ) {
684 $wgOut->addWikiText( wfMsg( "nohistory" ) );
685 return;
688 if ( $this->mAllowed ) {
689 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
690 } else {
691 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
694 # List all stored revisions
695 $revisions = $archive->listRevisions();
696 $files = $archive->listFiles();
698 $haveRevisions = $revisions && $revisions->numRows() > 0;
699 $haveFiles = $files && $files->numRows() > 0;
701 # Batch existence check on user and talk pages
702 if( $haveRevisions ) {
703 $batch = new LinkBatch();
704 while( $row = $revisions->fetchObject() ) {
705 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->ar_user_text ) );
706 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->ar_user_text ) );
708 $batch->execute();
709 $revisions->seek( 0 );
711 if( $haveFiles ) {
712 $batch = new LinkBatch();
713 while( $row = $files->fetchObject() ) {
714 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->fa_user_text ) );
715 $batch->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->fa_user_text ) );
717 $batch->execute();
718 $files->seek( 0 );
721 if ( $this->mAllowed ) {
722 $titleObj = SpecialPage::getTitleFor( "Undelete" );
723 $action = $titleObj->getLocalURL( "action=submit" );
724 # Start the form here
725 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
726 $wgOut->addHtml( $top );
729 # Show relevant lines from the deletion log:
730 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
731 $logViewer = new LogViewer(
732 new LogReader(
733 new FauxRequest(
734 array( 'page' => $this->mTargetObj->getPrefixedText(),
735 'type' => 'delete' ) ) ) );
736 $logViewer->showList( $wgOut );
738 if( $this->mAllowed && ( $haveRevisions || $haveFiles ) ) {
739 # Format the user-visible controls (comment field, submission button)
740 # in a nice little table
741 $table = '<fieldset><table><tr>';
742 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
743 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
744 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment ) . '</td>';
745 $table .= '</tr><tr><td>&nbsp;</td><td>';
746 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
747 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
748 $table .= '</td></tr></table></fieldset>';
749 $wgOut->addHtml( $table );
752 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
754 if( $haveRevisions ) {
755 # The page's stored (deleted) history:
756 $wgOut->addHTML("<ul>");
757 $target = urlencode( $this->mTarget );
758 while( $row = $revisions->fetchObject() ) {
759 $ts = wfTimestamp( TS_MW, $row->ar_timestamp );
760 if ( $this->mAllowed ) {
761 $checkBox = wfCheck( "ts$ts" );
762 $pageLink = $sk->makeKnownLinkObj( $titleObj,
763 $wgLang->timeanddate( $ts, true ),
764 "target=$target&timestamp=$ts" );
765 } else {
766 $checkBox = '';
767 $pageLink = $wgLang->timeanddate( $ts, true );
769 $userLink = $sk->userLink( $row->ar_user, $row->ar_user_text ) . $sk->userToolLinks( $row->ar_user, $row->ar_user_text );
770 $stxt = '';
771 if (!is_null($size = $row->ar_len)) {
772 if ($size == 0) {
773 $stxt = wfMsgHtml('historyempty');
774 } else {
775 $stxt = wfMsgHtml('historysize', $wgLang->formatNum( $size ) );
778 $comment = $sk->commentBlock( $row->ar_comment );
779 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $stxt $comment</li>\n" );
782 $revisions->free();
783 $wgOut->addHTML("</ul>");
784 } else {
785 $wgOut->addWikiText( wfMsg( "nohistory" ) );
789 if( $haveFiles ) {
790 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
791 $wgOut->addHtml( "<ul>" );
792 while( $row = $files->fetchObject() ) {
793 $ts = wfTimestamp( TS_MW, $row->fa_timestamp );
794 if ( $this->mAllowed && $row->fa_storage_key ) {
795 $checkBox = wfCheck( "fileid" . $row->fa_id );
796 $key = urlencode( $row->fa_storage_key );
797 $target = urlencode( $this->mTarget );
798 $pageLink = $sk->makeKnownLinkObj( $titleObj,
799 $wgLang->timeanddate( $ts, true ),
800 "target=$target&file=$key" );
801 } else {
802 $checkBox = '';
803 $pageLink = $wgLang->timeanddate( $ts, true );
805 $userLink = $sk->userLink( $row->fa_user, $row->fa_user_text ) . $sk->userToolLinks( $row->fa_user, $row->fa_user_text );
806 $data =
807 wfMsgHtml( 'widthheight',
808 $wgLang->formatNum( $row->fa_width ),
809 $wgLang->formatNum( $row->fa_height ) ) .
810 ' (' .
811 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size ) ) .
812 ')';
813 $comment = $sk->commentBlock( $row->fa_description );
814 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
816 $files->free();
817 $wgOut->addHTML( "</ul>" );
820 if ( $this->mAllowed ) {
821 # Slip in the hidden controls here
822 $misc = wfHidden( 'target', $this->mTarget );
823 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
824 $wgOut->addHtml( $misc . '</form>' );
827 return true;
830 function undelete() {
831 global $wgOut, $wgUser;
832 if( !is_null( $this->mTargetObj ) ) {
833 $archive = new PageArchive( $this->mTargetObj );
835 $ok = $archive->undelete(
836 $this->mTargetTimestamp,
837 $this->mComment,
838 $this->mFileVersions );
840 if( $ok ) {
841 $skin = $wgUser->getSkin();
842 $link = $skin->makeKnownLinkObj( $this->mTargetObj );
843 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
844 return true;
847 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );
848 return false;