4 * Special page allowing users with the appropriate permissions to view
5 * and restore deleted content
8 * @subpackage Special pages
14 function wfSpecialUndelete( $par ) {
17 $form = new UndeleteForm( $wgRequest, $par );
24 * @subpackage SpecialPage
29 function PageArchive( &$title ) {
30 if( is_null( $title ) ) {
31 throw new MWException( 'Archiver() given a null title.');
33 $this->title
=& $title;
37 * List all deleted pages recorded in the archive table. Returns result
38 * wrapper with (ar_namespace, ar_title, count) fields, ordered by page
39 * namespace/title. Can be called staticaly.
41 * @return ResultWrapper
43 /* static */ function listAllPages() {
44 $dbr =& wfGetDB( DB_SLAVE
);
45 $archive = $dbr->tableName( 'archive' );
47 $sql = "SELECT ar_namespace,ar_title, COUNT(*) AS count FROM $archive " .
48 "GROUP BY ar_namespace,ar_title ORDER BY ar_namespace,ar_title";
50 return $dbr->resultObject( $dbr->query( $sql, 'PageArchive::listAllPages' ) );
54 * List the revisions of the given page. Returns result wrapper with
55 * (ar_minor_edit, ar_timestamp, ar_user, ar_user_text, ar_comment) fields.
57 * @return ResultWrapper
59 function listRevisions() {
60 $dbr =& wfGetDB( DB_SLAVE
);
61 $res = $dbr->select( 'archive',
62 array( 'ar_minor_edit', 'ar_timestamp', 'ar_user', 'ar_user_text', 'ar_comment' ),
63 array( 'ar_namespace' => $this->title
->getNamespace(),
64 'ar_title' => $this->title
->getDBkey() ),
65 'PageArchive::listRevisions',
66 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
67 $ret = $dbr->resultObject( $res );
72 * List the deleted file revisions for this page, if it's a file page.
73 * Returns a result wrapper with various filearchive fields, or null
76 * @return ResultWrapper
77 * @fixme Does this belong in Image for fuller encapsulation?
79 function listFiles() {
80 if( $this->title
->getNamespace() == NS_IMAGE
) {
81 $dbr =& wfGetDB( DB_SLAVE
);
82 $res = $dbr->select( 'filearchive',
94 array( 'fa_name' => $this->title
->getDbKey() ),
96 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
97 $ret = $dbr->resultObject( $res );
104 * Fetch (and decompress if necessary) the stored text for the deleted
105 * revision of the page with the given timestamp.
108 * @deprecated Use getRevision() for more flexible information
110 function getRevisionText( $timestamp ) {
111 $rev = $this->getRevision( $timestamp );
112 return $rev ?
$rev->getText() : null;
116 * Return a Revision object containing data for the deleted revision.
117 * Note that the result *may* or *may not* have a null page ID.
118 * @param string $timestamp
121 function getRevision( $timestamp ) {
122 $dbr =& wfGetDB( DB_SLAVE
);
123 $row = $dbr->selectRow( 'archive',
134 array( 'ar_namespace' => $this->title
->getNamespace(),
135 'ar_title' => $this->title
->getDbkey(),
136 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
139 return new Revision( array(
140 'page' => $this->title
->getArticleId(),
141 'id' => $row->ar_rev_id
,
142 'text' => ($row->ar_text_id
144 : Revision
::getRevisionText( $row, 'ar_' ) ),
145 'comment' => $row->ar_comment
,
146 'user' => $row->ar_user
,
147 'user_text' => $row->ar_user_text
,
148 'timestamp' => $row->ar_timestamp
,
149 'minor_edit' => $row->ar_minor_edit
,
150 'text_id' => $row->ar_text_id
) );
157 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
159 function getTextFromRow( $row ) {
160 if( is_null( $row->ar_text_id
) ) {
161 // An old row from MediaWiki 1.4 or previous.
162 // Text is embedded in this row in classic compression format.
163 return Revision
::getRevisionText( $row, "ar_" );
165 // New-style: keyed to the text storage backend.
166 $dbr =& wfGetDB( DB_SLAVE
);
167 $text = $dbr->selectRow( 'text',
168 array( 'old_text', 'old_flags' ),
169 array( 'old_id' => $row->ar_text_id
),
171 return Revision
::getRevisionText( $text );
177 * Fetch (and decompress if necessary) the stored text of the most
178 * recently edited deleted revision of the page.
180 * If there are no archived revisions for the page, returns NULL.
184 function getLastRevisionText() {
185 $dbr =& wfGetDB( DB_SLAVE
);
186 $row = $dbr->selectRow( 'archive',
187 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
188 array( 'ar_namespace' => $this->title
->getNamespace(),
189 'ar_title' => $this->title
->getDBkey() ),
190 'PageArchive::getLastRevisionText',
191 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
193 return $this->getTextFromRow( $row );
200 * Quick check if any archived revisions are present for the page.
203 function isDeleted() {
204 $dbr =& wfGetDB( DB_SLAVE
);
205 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
206 array( 'ar_namespace' => $this->title
->getNamespace(),
207 'ar_title' => $this->title
->getDBkey() ) );
212 * Restore the given (or all) text and file revisions for the page.
213 * Once restored, the items will be removed from the archive tables.
214 * The deletion log will be updated with an undeletion notice.
216 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
217 * @param string $comment
218 * @param array $fileVersions
220 * @return true on success.
222 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
223 // If both the set of text revisions and file revisions are empty,
224 // restore everything. Otherwise, just restore the requested items.
225 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
227 $restoreText = $restoreAll ||
!empty( $timestamps );
228 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
230 if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
231 $img = new Image( $this->title
);
232 $filesRestored = $img->restore( $fileVersions );
238 $textRestored = $this->undeleteRevisions( $timestamps );
245 $log = new LogPage( 'delete' );
247 if( $textRestored && $filesRestored ) {
248 $reason = wfMsgForContent( 'undeletedrevisions-files',
249 $wgContLang->formatNum( $textRestored ),
250 $wgContLang->formatNum( $filesRestored ) );
251 } elseif( $textRestored ) {
252 $reason = wfMsgForContent( 'undeletedrevisions',
253 $wgContLang->formatNum( $textRestored ) );
254 } elseif( $filesRestored ) {
255 $reason = wfMsgForContent( 'undeletedfiles',
256 $wgContLang->formatNum( $filesRestored ) );
258 wfDebug( "Undelete: nothing undeleted...\n" );
262 if( trim( $comment ) != '' )
263 $reason .= ": {$comment}";
264 $log->addEntry( 'restore', $this->title
, $reason );
270 * This is the meaty bit -- restores archived revisions of the given page
271 * to the cur/old tables. If the page currently exists, all revisions will
272 * be stuffed into old, otherwise the most recent will go into cur.
274 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
275 * @param string $comment
276 * @param array $fileVersions
278 * @return int number of revisions restored
280 private function undeleteRevisions( $timestamps ) {
281 global $wgParser, $wgDBtype;
283 $restoreAll = empty( $timestamps );
285 $dbw =& wfGetDB( DB_MASTER
);
286 extract( $dbw->tableNames( 'page', 'archive' ) );
288 # Does this page already exist? We'll have to update it...
289 $article = new Article( $this->title
);
290 $options = ( $wgDBtype == 'postgres' )
291 ?
'' // pg doesn't support this?
293 $page = $dbw->selectRow( 'page',
294 array( 'page_id', 'page_latest' ),
295 array( 'page_namespace' => $this->title
->getNamespace(),
296 'page_title' => $this->title
->getDBkey() ),
300 # Page already exists. Import the history, and if necessary
301 # we'll update the latest revision field in the record.
303 $pageId = $page->page_id
;
304 $previousRevId = $page->page_latest
;
306 # Have to create a new article...
307 $newid = $article->insertOn( $dbw );
313 $oldones = '1 = 1'; # All revisions...
315 $oldts = implode( ',',
316 array_map( array( &$dbw, 'addQuotes' ),
317 array_map( array( &$dbw, 'timestamp' ),
320 $oldones = "ar_timestamp IN ( {$oldts} )";
324 * Restore each revision...
326 $result = $dbw->select( 'archive',
338 'ar_namespace' => $this->title
->getNamespace(),
339 'ar_title' => $this->title
->getDBkey(),
343 'ORDER BY' => 'ar_timestamp' )
345 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
346 wfDebug( __METHOD__
.": couldn't find all requested rows\n" );
351 $newRevId = $previousRevId;
354 while( $row = $dbw->fetchObject( $result ) ) {
355 if( $row->ar_text_id
) {
356 // Revision was deleted in 1.5+; text is in
357 // the regular text table, use the reference.
358 // Specify null here so the so the text is
359 // dereferenced for page length info if needed.
362 // Revision was deleted in 1.4 or earlier.
363 // Text is squashed into the archive row, and
364 // a new text table entry will be created for it.
365 $revText = Revision
::getRevisionText( $row, 'ar_' );
367 $revision = new Revision( array(
369 'id' => $row->ar_rev_id
,
371 'comment' => $row->ar_comment
,
372 'user' => $row->ar_user
,
373 'user_text' => $row->ar_user_text
,
374 'timestamp' => $row->ar_timestamp
,
375 'minor_edit' => $row->ar_minor_edit
,
376 'text_id' => $row->ar_text_id
,
378 $newRevId = $revision->insertOn( $dbw );
383 # FIXME: Update latest if newer as well...
385 // Attach the latest revision to the page...
386 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
388 // Update site stats, link tables, etc
389 $article->createUpdates( $revision );
393 Article
::onArticleCreate( $this->title
);
395 Article
::onArticleEdit( $this->title
);
398 # Something went terribly wrong!
401 # Now that it's safely stored, take it out of the archive
402 $dbw->delete( 'archive',
404 'ar_namespace' => $this->title
->getNamespace(),
405 'ar_title' => $this->title
->getDBkey(),
417 * @subpackage SpecialPage
420 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
421 var $mTargetTimestamp, $mAllowed, $mComment;
423 function UndeleteForm( &$request, $par = "" ) {
425 $this->mAction
= $request->getVal( 'action' );
426 $this->mTarget
= $request->getVal( 'target' );
427 $time = $request->getVal( 'timestamp' );
428 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
429 $this->mFile
= $request->getVal( 'file' );
431 $posted = $request->wasPosted() &&
432 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
433 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
434 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
435 $this->mComment
= $request->getText( 'wpComment' );
438 $this->mTarget
= $par;
440 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
441 $this->mAllowed
= true;
443 $this->mAllowed
= false;
444 $this->mTimestamp
= '';
445 $this->mRestore
= false;
447 if ( $this->mTarget
!== "" ) {
448 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
450 $this->mTargetObj
= NULL;
452 if( $this->mRestore
) {
453 $timestamps = array();
454 $this->mFileVersions
= array();
455 foreach( $_REQUEST as $key => $val ) {
456 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
457 array_push( $timestamps, $matches[1] );
460 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
461 $this->mFileVersions
[] = intval( $matches[1] );
464 rsort( $timestamps );
465 $this->mTargetTimestamp
= $timestamps;
471 if( is_null( $this->mTargetObj
) ) {
472 return $this->showList();
474 if( $this->mTimestamp
!== '' ) {
475 return $this->showRevision( $this->mTimestamp
);
477 if( $this->mFile
!== null ) {
478 return $this->showFile( $this->mFile
);
480 if( $this->mRestore
&& $this->mAction
== "submit" ) {
481 return $this->undelete();
483 return $this->showHistory();
486 /* private */ function showList() {
487 global $wgLang, $wgContLang, $wgUser, $wgOut;
489 # List undeletable articles
490 $result = PageArchive
::listAllPages();
492 if ( $this->mAllowed
) {
493 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
495 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
497 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
499 $sk = $wgUser->getSkin();
500 $undelete =& SpecialPage
::getTitleFor( 'Undelete' );
501 $wgOut->addHTML( "<ul>\n" );
502 while( $row = $result->fetchObject() ) {
503 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
504 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
505 $revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count
) );
506 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
509 $wgOut->addHTML( "</ul>\n" );
514 /* private */ function showRevision( $timestamp ) {
515 global $wgLang, $wgUser, $wgOut;
517 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
519 $archive = new PageArchive( $this->mTargetObj
);
520 $rev = $archive->getRevision( $timestamp );
522 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
523 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
524 $wgLang->date( $timestamp ) ) . ")\n" );
527 $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
531 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
533 if( $this->mPreview
) {
534 $wgOut->addHtml( "<hr />\n" );
535 $wgOut->addWikiText( $rev->getText() );
538 $self = SpecialPage
::getTitleFor( "Undelete" );
541 wfElement( 'textarea', array(
543 'cols' => intval( $wgUser->getOption( 'cols' ) ),
544 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
545 $rev->getText() . "\n" ) .
546 wfOpenElement( 'div' ) .
547 wfOpenElement( 'form', array(
549 'action' => $self->getLocalURL( "action=submit" ) ) ) .
550 wfElement( 'input', array(
553 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
554 wfElement( 'input', array(
556 'name' => 'timestamp',
557 'value' => $timestamp ) ) .
558 wfElement( 'input', array(
560 'name' => 'wpEditToken',
561 'value' => $wgUser->editToken() ) ) .
562 wfElement( 'input', array(
566 wfElement( 'input', array(
568 'value' => wfMsg( 'showpreview' ) ) ) .
569 wfCloseElement( 'form' ) .
570 wfCloseElement( 'div' ) );
574 * Show a deleted file version requested by the visitor.
576 function showFile( $key ) {
580 $store = FileStore
::get( 'deleted' );
581 $store->stream( $key );
584 /* private */ function showHistory() {
585 global $wgLang, $wgUser, $wgOut;
587 $sk = $wgUser->getSkin();
588 if ( $this->mAllowed
) {
589 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
591 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
594 $archive = new PageArchive( $this->mTargetObj
);
595 $text = $archive->getLastRevisionText();
597 if( is_null( $text ) ) {
598 $wgOut->addWikiText( wfMsg( "nohistory" ) );
602 if ( $this->mAllowed
) {
603 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
605 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
608 # List all stored revisions
609 $revisions = $archive->listRevisions();
610 $files = $archive->listFiles();
612 $haveRevisions = $revisions && $revisions->numRows() > 0;
613 $haveFiles = $files && $files->numRows() > 0;
615 # Batch existence check on user and talk pages
616 if( $haveRevisions ) {
617 $batch = new LinkBatch();
618 while( $row = $revisions->fetchObject() ) {
619 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
620 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
623 $revisions->seek( 0 );
626 $batch = new LinkBatch();
627 while( $row = $files->fetchObject() ) {
628 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
629 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
635 if ( $this->mAllowed
) {
636 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
637 $action = $titleObj->getLocalURL( "action=submit" );
638 # Start the form here
639 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
640 $wgOut->addHtml( $top );
643 # Show relevant lines from the deletion log:
644 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
645 $logViewer = new LogViewer(
648 array( 'page' => $this->mTargetObj
->getPrefixedText(),
649 'type' => 'delete' ) ) ) );
650 $logViewer->showList( $wgOut );
652 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
653 # Format the user-visible controls (comment field, submission button)
654 # in a nice little table
655 $table = '<fieldset><table><tr>';
656 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
657 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
658 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment
) . '</td>';
659 $table .= '</tr><tr><td> </td><td>';
660 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
661 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
662 $table .= '</td></tr></table></fieldset>';
663 $wgOut->addHtml( $table );
666 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
668 if( $haveRevisions ) {
669 # The page's stored (deleted) history:
670 $wgOut->addHTML("<ul>");
671 $target = urlencode( $this->mTarget
);
672 while( $row = $revisions->fetchObject() ) {
673 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
674 if ( $this->mAllowed
) {
675 $checkBox = wfCheck( "ts$ts" );
676 $pageLink = $sk->makeKnownLinkObj( $titleObj,
677 $wgLang->timeanddate( $ts, true ),
678 "target=$target×tamp=$ts" );
681 $pageLink = $wgLang->timeanddate( $ts, true );
683 $userLink = $sk->userLink( $row->ar_user
, $row->ar_user_text
) . $sk->userToolLinks( $row->ar_user
, $row->ar_user_text
);
684 $comment = $sk->commentBlock( $row->ar_comment
);
685 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
689 $wgOut->addHTML("</ul>");
691 $wgOut->addWikiText( wfMsg( "nohistory" ) );
696 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
697 $wgOut->addHtml( "<ul>" );
698 while( $row = $files->fetchObject() ) {
699 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
700 if ( $this->mAllowed
&& $row->fa_storage_key
) {
701 $checkBox = wfCheck( "fileid" . $row->fa_id
);
702 $key = urlencode( $row->fa_storage_key
);
703 $target = urlencode( $this->mTarget
);
704 $pageLink = $sk->makeKnownLinkObj( $titleObj,
705 $wgLang->timeanddate( $ts, true ),
706 "target=$target&file=$key" );
709 $pageLink = $wgLang->timeanddate( $ts, true );
711 $userLink = $sk->userLink( $row->fa_user
, $row->fa_user_text
) . $sk->userToolLinks( $row->fa_user
, $row->fa_user_text
);
713 wfMsgHtml( 'widthheight',
714 $wgLang->formatNum( $row->fa_width
),
715 $wgLang->formatNum( $row->fa_height
) ) .
717 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
719 $comment = $sk->commentBlock( $row->fa_description
);
720 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
723 $wgOut->addHTML( "</ul>" );
726 if ( $this->mAllowed
) {
727 # Slip in the hidden controls here
728 $misc = wfHidden( 'target', $this->mTarget
);
729 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
730 $wgOut->addHtml( $misc . '</form>' );
736 function undelete() {
737 global $wgOut, $wgUser;
738 if( !is_null( $this->mTargetObj
) ) {
739 $archive = new PageArchive( $this->mTargetObj
);
742 $ok = $archive->undelete(
743 $this->mTargetTimestamp
,
745 $this->mFileVersions
);
748 $skin =& $wgUser->getSkin();
749 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
750 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
754 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );