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 ) {
283 $restoreAll = empty( $timestamps );
285 $dbw =& wfGetDB( DB_MASTER
);
286 $page = $dbw->tableName( '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" );
353 while( $row = $dbw->fetchObject( $result ) ) {
354 if( $row->ar_text_id
) {
355 // Revision was deleted in 1.5+; text is in
356 // the regular text table, use the reference.
357 // Specify null here so the so the text is
358 // dereferenced for page length info if needed.
361 // Revision was deleted in 1.4 or earlier.
362 // Text is squashed into the archive row, and
363 // a new text table entry will be created for it.
364 $revText = Revision
::getRevisionText( $row, 'ar_' );
366 $revision = new Revision( array(
368 'id' => $row->ar_rev_id
,
370 'comment' => $row->ar_comment
,
371 'user' => $row->ar_user
,
372 'user_text' => $row->ar_user_text
,
373 'timestamp' => $row->ar_timestamp
,
374 'minor_edit' => $row->ar_minor_edit
,
375 'text_id' => $row->ar_text_id
,
377 $revision->insertOn( $dbw );
382 # FIXME: Update latest if newer as well...
384 // Attach the latest revision to the page...
385 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
387 // Update site stats, link tables, etc
388 $article->createUpdates( $revision );
392 Article
::onArticleCreate( $this->title
);
394 Article
::onArticleEdit( $this->title
);
397 # Something went terribly wrong!
400 # Now that it's safely stored, take it out of the archive
401 $dbw->delete( 'archive',
403 'ar_namespace' => $this->title
->getNamespace(),
404 'ar_title' => $this->title
->getDBkey(),
416 * @subpackage SpecialPage
419 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
420 var $mTargetTimestamp, $mAllowed, $mComment;
422 function UndeleteForm( &$request, $par = "" ) {
424 $this->mAction
= $request->getVal( 'action' );
425 $this->mTarget
= $request->getVal( 'target' );
426 $time = $request->getVal( 'timestamp' );
427 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
428 $this->mFile
= $request->getVal( 'file' );
430 $posted = $request->wasPosted() &&
431 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
432 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
433 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
434 $this->mComment
= $request->getText( 'wpComment' );
437 $this->mTarget
= $par;
439 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
440 $this->mAllowed
= true;
442 $this->mAllowed
= false;
443 $this->mTimestamp
= '';
444 $this->mRestore
= false;
446 if ( $this->mTarget
!== "" ) {
447 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
449 $this->mTargetObj
= NULL;
451 if( $this->mRestore
) {
452 $timestamps = array();
453 $this->mFileVersions
= array();
454 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 $article = new Article ( $archive->title
); # OutputPage wants an Article obj
536 $wgOut->addPrimaryWikiText( $rev->getText(), $article, false );
539 $self = SpecialPage
::getTitleFor( "Undelete" );
542 wfElement( 'textarea', array(
544 'cols' => intval( $wgUser->getOption( 'cols' ) ),
545 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
546 $rev->getText() . "\n" ) .
547 wfOpenElement( 'div' ) .
548 wfOpenElement( 'form', array(
550 'action' => $self->getLocalURL( "action=submit" ) ) ) .
551 wfElement( 'input', array(
554 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
555 wfElement( 'input', array(
557 'name' => 'timestamp',
558 'value' => $timestamp ) ) .
559 wfElement( 'input', array(
561 'name' => 'wpEditToken',
562 'value' => $wgUser->editToken() ) ) .
563 wfElement( 'input', array(
567 wfElement( 'input', array(
569 'value' => wfMsg( 'showpreview' ) ) ) .
570 wfCloseElement( 'form' ) .
571 wfCloseElement( 'div' ) );
575 * Show a deleted file version requested by the visitor.
577 function showFile( $key ) {
581 $store = FileStore
::get( 'deleted' );
582 $store->stream( $key );
585 /* private */ function showHistory() {
586 global $wgLang, $wgUser, $wgOut;
588 $sk = $wgUser->getSkin();
589 if ( $this->mAllowed
) {
590 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
592 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
595 $archive = new PageArchive( $this->mTargetObj
);
597 $text = $archive->getLastRevisionText();
598 if( is_null( $text ) ) {
599 $wgOut->addWikiText( wfMsg( "nohistory" ) );
603 if ( $this->mAllowed
) {
604 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
606 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
609 # List all stored revisions
610 $revisions = $archive->listRevisions();
611 $files = $archive->listFiles();
613 $haveRevisions = $revisions && $revisions->numRows() > 0;
614 $haveFiles = $files && $files->numRows() > 0;
616 # Batch existence check on user and talk pages
617 if( $haveRevisions ) {
618 $batch = new LinkBatch();
619 while( $row = $revisions->fetchObject() ) {
620 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
621 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
624 $revisions->seek( 0 );
627 $batch = new LinkBatch();
628 while( $row = $files->fetchObject() ) {
629 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
630 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
636 if ( $this->mAllowed
) {
637 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
638 $action = $titleObj->getLocalURL( "action=submit" );
639 # Start the form here
640 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
641 $wgOut->addHtml( $top );
644 # Show relevant lines from the deletion log:
645 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
646 $logViewer = new LogViewer(
649 array( 'page' => $this->mTargetObj
->getPrefixedText(),
650 'type' => 'delete' ) ) ) );
651 $logViewer->showList( $wgOut );
653 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
654 # Format the user-visible controls (comment field, submission button)
655 # in a nice little table
656 $table = '<fieldset><table><tr>';
657 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
658 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
659 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment
) . '</td>';
660 $table .= '</tr><tr><td> </td><td>';
661 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
662 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
663 $table .= '</td></tr></table></fieldset>';
664 $wgOut->addHtml( $table );
667 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
669 if( $haveRevisions ) {
670 # The page's stored (deleted) history:
671 $wgOut->addHTML("<ul>");
672 $target = urlencode( $this->mTarget
);
673 while( $row = $revisions->fetchObject() ) {
674 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
675 if ( $this->mAllowed
) {
676 $checkBox = wfCheck( "ts$ts" );
677 $pageLink = $sk->makeKnownLinkObj( $titleObj,
678 $wgLang->timeanddate( $ts, true ),
679 "target=$target×tamp=$ts" );
682 $pageLink = $wgLang->timeanddate( $ts, true );
684 $userLink = $sk->userLink( $row->ar_user
, $row->ar_user_text
) . $sk->userToolLinks( $row->ar_user
, $row->ar_user_text
);
685 $comment = $sk->commentBlock( $row->ar_comment
);
686 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
690 $wgOut->addHTML("</ul>");
692 $wgOut->addWikiText( wfMsg( "nohistory" ) );
697 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
698 $wgOut->addHtml( "<ul>" );
699 while( $row = $files->fetchObject() ) {
700 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
701 if ( $this->mAllowed
&& $row->fa_storage_key
) {
702 $checkBox = wfCheck( "fileid" . $row->fa_id
);
703 $key = urlencode( $row->fa_storage_key
);
704 $target = urlencode( $this->mTarget
);
705 $pageLink = $sk->makeKnownLinkObj( $titleObj,
706 $wgLang->timeanddate( $ts, true ),
707 "target=$target&file=$key" );
710 $pageLink = $wgLang->timeanddate( $ts, true );
712 $userLink = $sk->userLink( $row->fa_user
, $row->fa_user_text
) . $sk->userToolLinks( $row->fa_user
, $row->fa_user_text
);
714 wfMsgHtml( 'widthheight',
715 $wgLang->formatNum( $row->fa_width
),
716 $wgLang->formatNum( $row->fa_height
) ) .
718 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
720 $comment = $sk->commentBlock( $row->fa_description
);
721 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
724 $wgOut->addHTML( "</ul>" );
727 if ( $this->mAllowed
) {
728 # Slip in the hidden controls here
729 $misc = wfHidden( 'target', $this->mTarget
);
730 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
731 $wgOut->addHtml( $misc . '</form>' );
737 function undelete() {
738 global $wgOut, $wgUser;
739 if( !is_null( $this->mTargetObj
) ) {
740 $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" ) );