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 $fname = __CLASS__
. '::' . __FUNCTION__
;
81 if( $this->title
->getNamespace() == NS_IMAGE
) {
82 $dbr =& wfGetDB( DB_SLAVE
);
83 $res = $dbr->select( 'filearchive',
95 array( 'fa_name' => $this->title
->getDbKey() ),
97 array( 'ORDER BY' => 'fa_timestamp DESC' ) );
98 $ret = $dbr->resultObject( $res );
105 * Fetch (and decompress if necessary) the stored text for the deleted
106 * revision of the page with the given timestamp.
110 function getRevisionText( $timestamp ) {
111 $fname = 'PageArchive::getRevisionText';
112 $dbr =& wfGetDB( DB_SLAVE
);
113 $row = $dbr->selectRow( 'archive',
114 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
115 array( 'ar_namespace' => $this->title
->getNamespace(),
116 'ar_title' => $this->title
->getDbkey(),
117 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
120 return $this->getTextFromRow( $row );
127 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
129 function getTextFromRow( $row ) {
130 $fname = 'PageArchive::getTextFromRow';
132 if( is_null( $row->ar_text_id
) ) {
133 // An old row from MediaWiki 1.4 or previous.
134 // Text is embedded in this row in classic compression format.
135 return Revision
::getRevisionText( $row, "ar_" );
137 // New-style: keyed to the text storage backend.
138 $dbr =& wfGetDB( DB_SLAVE
);
139 $text = $dbr->selectRow( 'text',
140 array( 'old_text', 'old_flags' ),
141 array( 'old_id' => $row->ar_text_id
),
143 return Revision
::getRevisionText( $text );
149 * Fetch (and decompress if necessary) the stored text of the most
150 * recently edited deleted revision of the page.
152 * If there are no archived revisions for the page, returns NULL.
156 function getLastRevisionText() {
157 $dbr =& wfGetDB( DB_SLAVE
);
158 $row = $dbr->selectRow( 'archive',
159 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
160 array( 'ar_namespace' => $this->title
->getNamespace(),
161 'ar_title' => $this->title
->getDBkey() ),
162 'PageArchive::getLastRevisionText',
163 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
165 return $this->getTextFromRow( $row );
172 * Quick check if any archived revisions are present for the page.
175 function isDeleted() {
176 $dbr =& wfGetDB( DB_SLAVE
);
177 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
178 array( 'ar_namespace' => $this->title
->getNamespace(),
179 'ar_title' => $this->title
->getDBkey() ) );
184 * Restore the given (or all) text and file revisions for the page.
185 * Once restored, the items will be removed from the archive tables.
186 * The deletion log will be updated with an undeletion notice.
188 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
189 * @param string $comment
190 * @param array $fileVersions
192 * @return true on success.
194 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
195 // If both the set of text revisions and file revisions are empty,
196 // restore everything. Otherwise, just restore the requested items.
197 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
199 $restoreText = $restoreAll ||
!empty( $timestamps );
200 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
202 if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
203 $img = new Image( $this->title
);
204 $filesRestored = $img->restore( $fileVersions );
210 $textRestored = $this->undeleteRevisions( $timestamps );
217 $log = new LogPage( 'delete' );
219 if( $textRestored && $filesRestored ) {
220 $reason = wfMsgForContent( 'undeletedrevisions-files',
221 $wgContLang->formatNum( $textRestored ),
222 $wgContLang->formatNum( $filesRestored ) );
223 } elseif( $textRestored ) {
224 $reason = wfMsgForContent( 'undeletedrevisions',
225 $wgContLang->formatNum( $textRestored ) );
226 } elseif( $filesRestored ) {
227 $reason = wfMsgForContent( 'undeletedfiles',
228 $wgContLang->formatNum( $filesRestored ) );
230 wfDebug( "Undelete: nothing undeleted...\n" );
234 if( trim( $comment ) != '' )
235 $reason .= ": {$comment}";
236 $log->addEntry( 'restore', $this->title
, $reason );
242 * This is the meaty bit -- restores archived revisions of the given page
243 * to the cur/old tables. If the page currently exists, all revisions will
244 * be stuffed into old, otherwise the most recent will go into cur.
246 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
247 * @param string $comment
248 * @param array $fileVersions
250 * @return int number of revisions restored
252 private function undeleteRevisions( $timestamps ) {
253 global $wgParser, $wgDBtype;
255 $fname = __CLASS__
. '::' . __FUNCTION__
;
256 $restoreAll = empty( $timestamps );
258 $dbw =& wfGetDB( DB_MASTER
);
259 extract( $dbw->tableNames( 'page', 'archive' ) );
261 # Does this page already exist? We'll have to update it...
262 $article = new Article( $this->title
);
263 $options = ( $wgDBtype == 'postgres' )
264 ?
'' // pg doesn't support this?
266 $page = $dbw->selectRow( 'page',
267 array( 'page_id', 'page_latest' ),
268 array( 'page_namespace' => $this->title
->getNamespace(),
269 'page_title' => $this->title
->getDBkey() ),
273 # Page already exists. Import the history, and if necessary
274 # we'll update the latest revision field in the record.
276 $pageId = $page->page_id
;
277 $previousRevId = $page->page_latest
;
279 # Have to create a new article...
280 $newid = $article->insertOn( $dbw );
286 $oldones = '1 = 1'; # All revisions...
288 $oldts = implode( ',',
289 array_map( array( &$dbw, 'addQuotes' ),
290 array_map( array( &$dbw, 'timestamp' ),
293 $oldones = "ar_timestamp IN ( {$oldts} )";
297 * Restore each revision...
299 $result = $dbw->select( 'archive',
311 'ar_namespace' => $this->title
->getNamespace(),
312 'ar_title' => $this->title
->getDBkey(),
316 'ORDER BY' => 'ar_timestamp' )
318 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
319 wfDebug( "$fname: couldn't find all requested rows\n" );
324 $newRevId = $previousRevId;
327 while( $row = $dbw->fetchObject( $result ) ) {
328 if( $row->ar_text_id
) {
329 // Revision was deleted in 1.5+; text is in
330 // the regular text table, use the reference.
331 // Specify null here so the so the text is
332 // dereferenced for page length info if needed.
335 // Revision was deleted in 1.4 or earlier.
336 // Text is squashed into the archive row, and
337 // a new text table entry will be created for it.
338 $revText = Revision
::getRevisionText( $row, 'ar_' );
340 $revision = new Revision( array(
342 'id' => $row->ar_rev_id
,
344 'comment' => $row->ar_comment
,
345 'user' => $row->ar_user
,
346 'user_text' => $row->ar_user_text
,
347 'timestamp' => $row->ar_timestamp
,
348 'minor_edit' => $row->ar_minor_edit
,
349 'text_id' => $row->ar_text_id
,
351 $newRevId = $revision->insertOn( $dbw );
356 # FIXME: Update latest if newer as well...
358 # FIXME: update article count if changed...
359 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
361 # Finally, clean up the link tables
362 $options = new ParserOptions
;
363 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options,
364 true, true, $newRevId );
365 $u = new LinksUpdate( $this->title
, $parserOutput );
368 #TODO: SearchUpdate, etc.
372 Article
::onArticleCreate( $this->title
);
374 Article
::onArticleEdit( $this->title
);
377 # Something went terribly wrong!
380 # Now that it's safely stored, take it out of the archive
381 $dbw->delete( 'archive',
383 'ar_namespace' => $this->title
->getNamespace(),
384 'ar_title' => $this->title
->getDBkey(),
396 * @subpackage SpecialPage
399 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
400 var $mTargetTimestamp, $mAllowed, $mComment;
402 function UndeleteForm( &$request, $par = "" ) {
404 $this->mAction
= $request->getText( 'action' );
405 $this->mTarget
= $request->getText( 'target' );
406 $this->mTimestamp
= $request->getText( 'timestamp' );
407 $this->mFile
= $request->getVal( 'file' );
409 $posted = $request->wasPosted() &&
410 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
411 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
412 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
413 $this->mComment
= $request->getText( 'wpComment' );
416 $this->mTarget
= $par;
418 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
419 $this->mAllowed
= true;
421 $this->mAllowed
= false;
422 $this->mTimestamp
= '';
423 $this->mRestore
= false;
425 if ( $this->mTarget
!== "" ) {
426 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
428 $this->mTargetObj
= NULL;
430 if( $this->mRestore
) {
431 $timestamps = array();
432 $this->mFileVersions
= array();
433 foreach( $_REQUEST as $key => $val ) {
434 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
435 array_push( $timestamps, $matches[1] );
438 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
439 $this->mFileVersions
[] = intval( $matches[1] );
442 rsort( $timestamps );
443 $this->mTargetTimestamp
= $timestamps;
449 if( is_null( $this->mTargetObj
) ) {
450 return $this->showList();
452 if( $this->mTimestamp
!== '' ) {
453 return $this->showRevision( $this->mTimestamp
);
455 if( $this->mFile
!== null ) {
456 return $this->showFile( $this->mFile
);
458 if( $this->mRestore
&& $this->mAction
== "submit" ) {
459 return $this->undelete();
461 return $this->showHistory();
464 /* private */ function showList() {
465 global $wgLang, $wgContLang, $wgUser, $wgOut;
466 $fname = "UndeleteForm::showList";
468 # List undeletable articles
469 $result = PageArchive
::listAllPages();
471 if ( $this->mAllowed
) {
472 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
474 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
476 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
478 $sk = $wgUser->getSkin();
479 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
480 $wgOut->addHTML( "<ul>\n" );
481 while( $row = $result->fetchObject() ) {
482 $n = ($row->ar_namespace ?
483 ($wgContLang->getNsText( $row->ar_namespace
) . ":") : "").
485 $link = $sk->makeKnownLinkObj( $undelete,
486 htmlspecialchars( $n ), "target=" . urlencode( $n ) );
487 $revisions = htmlspecialchars( wfMsg( "undeleterevisions",
488 $wgLang->formatNum( $row->count
) ) );
489 $wgOut->addHTML( "<li>$link ($revisions)</li>\n" );
492 $wgOut->addHTML( "</ul>\n" );
497 /* private */ function showRevision( $timestamp ) {
498 global $wgLang, $wgUser, $wgOut;
499 $fname = "UndeleteForm::showRevision";
501 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
503 $archive = new PageArchive( $this->mTargetObj
);
504 $text = $archive->getRevisionText( $timestamp );
506 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
507 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
508 $wgLang->date( $timestamp ) ) . ")\n" );
510 if( $this->mPreview
) {
511 $wgOut->addHtml( "<hr />\n" );
512 $wgOut->addWikiText( $text );
515 $self = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
518 wfElement( 'textarea', array(
520 'cols' => intval( $wgUser->getOption( 'cols' ) ),
521 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
523 wfOpenElement( 'div' ) .
524 wfOpenElement( 'form', array(
526 'action' => $self->getLocalURL( "action=submit" ) ) ) .
527 wfElement( 'input', array(
530 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
531 wfElement( 'input', array(
533 'name' => 'timestamp',
534 'value' => $timestamp ) ) .
535 wfElement( 'input', array(
537 'name' => 'wpEditToken',
538 'value' => $wgUser->editToken() ) ) .
539 wfElement( 'input', array(
543 wfElement( 'input', array(
545 'value' => wfMsg( 'showpreview' ) ) ) .
546 wfCloseElement( 'form' ) .
547 wfCloseElement( 'div' ) );
551 * Show a deleted file version requested by the visitor.
553 function showFile( $key ) {
557 $store = FileStore
::get( 'deleted' );
558 $store->stream( $key );
561 /* private */ function showHistory() {
562 global $wgLang, $wgUser, $wgOut;
564 $sk = $wgUser->getSkin();
565 if ( $this->mAllowed
) {
566 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
568 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
571 $archive = new PageArchive( $this->mTargetObj
);
572 $text = $archive->getLastRevisionText();
574 if( is_null( $text ) ) {
575 $wgOut->addWikiText( wfMsg( "nohistory" ) );
579 if ( $this->mAllowed
) {
580 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
582 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
585 # List all stored revisions
586 $revisions = $archive->listRevisions();
587 $files = $archive->listFiles();
589 $haveRevisions = $revisions && $revisions->numRows() > 0;
590 $haveFiles = $files && $files->numRows() > 0;
592 # Batch existence check on user and talk pages
593 if( $haveRevisions ) {
594 $batch = new LinkBatch();
595 while( $row = $revisions->fetchObject() ) {
596 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
597 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
600 $revisions->seek( 0 );
603 $batch = new LinkBatch();
604 while( $row = $files->fetchObject() ) {
605 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
606 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
612 if ( $this->mAllowed
) {
613 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
614 $action = $titleObj->getLocalURL( "action=submit" );
615 # Start the form here
616 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
617 $wgOut->addHtml( $top );
620 # Show relevant lines from the deletion log:
621 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
622 require_once( 'SpecialLog.php' );
623 $logViewer = new LogViewer(
626 array( 'page' => $this->mTargetObj
->getPrefixedText(),
627 'type' => 'delete' ) ) ) );
628 $logViewer->showList( $wgOut );
630 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
631 # Format the user-visible controls (comment field, submission button)
632 # in a nice little table
633 $table = '<fieldset><table><tr>';
634 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
635 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
636 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment
) . '</td>';
637 $table .= '</tr><tr><td> </td><td>';
638 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
639 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
640 $table .= '</td></tr></table></fieldset>';
641 $wgOut->addHtml( $table );
644 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
646 if( $haveRevisions ) {
647 # The page's stored (deleted) history:
648 $wgOut->addHTML("<ul>");
649 $target = urlencode( $this->mTarget
);
650 while( $row = $revisions->fetchObject() ) {
651 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
652 if ( $this->mAllowed
) {
653 $checkBox = wfCheck( "ts$ts" );
654 $pageLink = $sk->makeKnownLinkObj( $titleObj,
655 $wgLang->timeanddate( $ts, true ),
656 "target=$target×tamp=$ts" );
659 $pageLink = $wgLang->timeanddate( $ts, true );
661 $userLink = $sk->userLink( $row->ar_user
, $row->ar_user_text
) . $sk->userToolLinks( $row->ar_user
, $row->ar_user_text
);
662 $comment = $sk->commentBlock( $row->ar_comment
);
663 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
667 $wgOut->addHTML("</ul>");
669 $wgOut->addWikiText( wfMsg( "nohistory" ) );
674 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
675 $wgOut->addHtml( "<ul>" );
676 while( $row = $files->fetchObject() ) {
677 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
678 if ( $this->mAllowed
&& $row->fa_storage_key
) {
679 $checkBox = wfCheck( "fileid" . $row->fa_id
);
680 $key = urlencode( $row->fa_storage_key
);
681 $target = urlencode( $this->mTarget
);
682 $pageLink = $sk->makeKnownLinkObj( $titleObj,
683 $wgLang->timeanddate( $ts, true ),
684 "target=$target&file=$key" );
687 $pageLink = $wgLang->timeanddate( $ts, true );
689 $userLink = $sk->userLink( $row->fa_user
, $row->fa_user_text
) . $sk->userToolLinks( $row->fa_user
, $row->fa_user_text
);
691 wfMsgHtml( 'widthheight',
692 $wgLang->formatNum( $row->fa_width
),
693 $wgLang->formatNum( $row->fa_height
) ) .
695 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
697 $comment = $sk->commentBlock( $row->fa_description
);
698 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
701 $wgOut->addHTML( "</ul>" );
704 if ( $this->mAllowed
) {
705 # Slip in the hidden controls here
706 $misc = wfHidden( 'target', $this->mTarget
);
707 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
708 $wgOut->addHtml( $misc . '</form>' );
714 function undelete() {
715 global $wgOut, $wgUser;
716 if( !is_null( $this->mTargetObj
) ) {
717 $archive = new PageArchive( $this->mTargetObj
);
720 $ok = $archive->undelete(
721 $this->mTargetTimestamp
,
723 $this->mFileVersions
);
726 $skin =& $wgUser->getSkin();
727 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
728 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
732 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );