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.
109 function getRevisionText( $timestamp ) {
110 $dbr =& wfGetDB( DB_SLAVE
);
111 $row = $dbr->selectRow( 'archive',
112 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
113 array( 'ar_namespace' => $this->title
->getNamespace(),
114 'ar_title' => $this->title
->getDbkey(),
115 'ar_timestamp' => $dbr->timestamp( $timestamp ) ),
118 return $this->getTextFromRow( $row );
125 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
127 function getTextFromRow( $row ) {
128 if( is_null( $row->ar_text_id
) ) {
129 // An old row from MediaWiki 1.4 or previous.
130 // Text is embedded in this row in classic compression format.
131 return Revision
::getRevisionText( $row, "ar_" );
133 // New-style: keyed to the text storage backend.
134 $dbr =& wfGetDB( DB_SLAVE
);
135 $text = $dbr->selectRow( 'text',
136 array( 'old_text', 'old_flags' ),
137 array( 'old_id' => $row->ar_text_id
),
139 return Revision
::getRevisionText( $text );
145 * Fetch (and decompress if necessary) the stored text of the most
146 * recently edited deleted revision of the page.
148 * If there are no archived revisions for the page, returns NULL.
152 function getLastRevisionText() {
153 $dbr =& wfGetDB( DB_SLAVE
);
154 $row = $dbr->selectRow( 'archive',
155 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
156 array( 'ar_namespace' => $this->title
->getNamespace(),
157 'ar_title' => $this->title
->getDBkey() ),
158 'PageArchive::getLastRevisionText',
159 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
161 return $this->getTextFromRow( $row );
168 * Quick check if any archived revisions are present for the page.
171 function isDeleted() {
172 $dbr =& wfGetDB( DB_SLAVE
);
173 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
174 array( 'ar_namespace' => $this->title
->getNamespace(),
175 'ar_title' => $this->title
->getDBkey() ) );
180 * Restore the given (or all) text and file revisions for the page.
181 * Once restored, the items will be removed from the archive tables.
182 * The deletion log will be updated with an undeletion notice.
184 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
185 * @param string $comment
186 * @param array $fileVersions
188 * @return true on success.
190 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
191 // If both the set of text revisions and file revisions are empty,
192 // restore everything. Otherwise, just restore the requested items.
193 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
195 $restoreText = $restoreAll ||
!empty( $timestamps );
196 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
198 if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
199 $img = new Image( $this->title
);
200 $filesRestored = $img->restore( $fileVersions );
206 $textRestored = $this->undeleteRevisions( $timestamps );
213 $log = new LogPage( 'delete' );
215 if( $textRestored && $filesRestored ) {
216 $reason = wfMsgForContent( 'undeletedrevisions-files',
217 $wgContLang->formatNum( $textRestored ),
218 $wgContLang->formatNum( $filesRestored ) );
219 } elseif( $textRestored ) {
220 $reason = wfMsgForContent( 'undeletedrevisions',
221 $wgContLang->formatNum( $textRestored ) );
222 } elseif( $filesRestored ) {
223 $reason = wfMsgForContent( 'undeletedfiles',
224 $wgContLang->formatNum( $filesRestored ) );
226 wfDebug( "Undelete: nothing undeleted...\n" );
230 if( trim( $comment ) != '' )
231 $reason .= ": {$comment}";
232 $log->addEntry( 'restore', $this->title
, $reason );
238 * This is the meaty bit -- restores archived revisions of the given page
239 * to the cur/old tables. If the page currently exists, all revisions will
240 * be stuffed into old, otherwise the most recent will go into cur.
242 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
243 * @param string $comment
244 * @param array $fileVersions
246 * @return int number of revisions restored
248 private function undeleteRevisions( $timestamps ) {
249 global $wgParser, $wgDBtype;
251 $restoreAll = empty( $timestamps );
253 $dbw =& wfGetDB( DB_MASTER
);
254 extract( $dbw->tableNames( 'page', 'archive' ) );
256 # Does this page already exist? We'll have to update it...
257 $article = new Article( $this->title
);
258 $options = ( $wgDBtype == 'postgres' )
259 ?
'' // pg doesn't support this?
261 $page = $dbw->selectRow( 'page',
262 array( 'page_id', 'page_latest' ),
263 array( 'page_namespace' => $this->title
->getNamespace(),
264 'page_title' => $this->title
->getDBkey() ),
268 # Page already exists. Import the history, and if necessary
269 # we'll update the latest revision field in the record.
271 $pageId = $page->page_id
;
272 $previousRevId = $page->page_latest
;
274 # Have to create a new article...
275 $newid = $article->insertOn( $dbw );
281 $oldones = '1 = 1'; # All revisions...
283 $oldts = implode( ',',
284 array_map( array( &$dbw, 'addQuotes' ),
285 array_map( array( &$dbw, 'timestamp' ),
288 $oldones = "ar_timestamp IN ( {$oldts} )";
292 * Restore each revision...
294 $result = $dbw->select( 'archive',
306 'ar_namespace' => $this->title
->getNamespace(),
307 'ar_title' => $this->title
->getDBkey(),
311 'ORDER BY' => 'ar_timestamp' )
313 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
314 wfDebug( __METHOD__
.": couldn't find all requested rows\n" );
319 $newRevId = $previousRevId;
322 while( $row = $dbw->fetchObject( $result ) ) {
323 if( $row->ar_text_id
) {
324 // Revision was deleted in 1.5+; text is in
325 // the regular text table, use the reference.
326 // Specify null here so the so the text is
327 // dereferenced for page length info if needed.
330 // Revision was deleted in 1.4 or earlier.
331 // Text is squashed into the archive row, and
332 // a new text table entry will be created for it.
333 $revText = Revision
::getRevisionText( $row, 'ar_' );
335 $revision = new Revision( array(
337 'id' => $row->ar_rev_id
,
339 'comment' => $row->ar_comment
,
340 'user' => $row->ar_user
,
341 'user_text' => $row->ar_user_text
,
342 'timestamp' => $row->ar_timestamp
,
343 'minor_edit' => $row->ar_minor_edit
,
344 'text_id' => $row->ar_text_id
,
346 $newRevId = $revision->insertOn( $dbw );
351 # FIXME: Update latest if newer as well...
353 // Attach the latest revision to the page...
354 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
356 // Update site stats, link tables, etc
357 $article->createUpdates( $revision );
361 Article
::onArticleCreate( $this->title
);
363 Article
::onArticleEdit( $this->title
);
366 # Something went terribly wrong!
369 # Now that it's safely stored, take it out of the archive
370 $dbw->delete( 'archive',
372 'ar_namespace' => $this->title
->getNamespace(),
373 'ar_title' => $this->title
->getDBkey(),
385 * @subpackage SpecialPage
388 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
389 var $mTargetTimestamp, $mAllowed, $mComment;
391 function UndeleteForm( &$request, $par = "" ) {
393 $this->mAction
= $request->getVal( 'action' );
394 $this->mTarget
= $request->getVal( 'target' );
395 $time = $request->getVal( 'timestamp' );
396 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
397 $this->mFile
= $request->getVal( 'file' );
399 $posted = $request->wasPosted() &&
400 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
401 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
402 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
403 $this->mComment
= $request->getText( 'wpComment' );
406 $this->mTarget
= $par;
408 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
409 $this->mAllowed
= true;
411 $this->mAllowed
= false;
412 $this->mTimestamp
= '';
413 $this->mRestore
= false;
415 if ( $this->mTarget
!== "" ) {
416 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
418 $this->mTargetObj
= NULL;
420 if( $this->mRestore
) {
421 $timestamps = array();
422 $this->mFileVersions
= array();
423 foreach( $_REQUEST as $key => $val ) {
424 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
425 array_push( $timestamps, $matches[1] );
428 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
429 $this->mFileVersions
[] = intval( $matches[1] );
432 rsort( $timestamps );
433 $this->mTargetTimestamp
= $timestamps;
439 if( is_null( $this->mTargetObj
) ) {
440 return $this->showList();
442 if( $this->mTimestamp
!== '' ) {
443 return $this->showRevision( $this->mTimestamp
);
445 if( $this->mFile
!== null ) {
446 return $this->showFile( $this->mFile
);
448 if( $this->mRestore
&& $this->mAction
== "submit" ) {
449 return $this->undelete();
451 return $this->showHistory();
454 /* private */ function showList() {
455 global $wgLang, $wgContLang, $wgUser, $wgOut;
457 # List undeletable articles
458 $result = PageArchive
::listAllPages();
460 if ( $this->mAllowed
) {
461 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
463 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
465 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
467 $sk = $wgUser->getSkin();
468 $undelete =& Title
::makeTitle( NS_SPECIAL
, 'Undelete' );
469 $wgOut->addHTML( "<ul>\n" );
470 while( $row = $result->fetchObject() ) {
471 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
472 $link = $sk->makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
473 $revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count
) );
474 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
477 $wgOut->addHTML( "</ul>\n" );
482 /* private */ function showRevision( $timestamp ) {
483 global $wgLang, $wgUser, $wgOut;
485 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
487 $archive = new PageArchive( $this->mTargetObj
);
488 $text = $archive->getRevisionText( $timestamp );
490 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
491 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
492 $wgLang->date( $timestamp ) ) . ")\n" );
494 if( $this->mPreview
) {
495 $wgOut->addHtml( "<hr />\n" );
496 $wgOut->addWikiText( $text );
499 $self = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
502 wfElement( 'textarea', array(
504 'cols' => intval( $wgUser->getOption( 'cols' ) ),
505 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
507 wfOpenElement( 'div' ) .
508 wfOpenElement( 'form', array(
510 'action' => $self->getLocalURL( "action=submit" ) ) ) .
511 wfElement( 'input', array(
514 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
515 wfElement( 'input', array(
517 'name' => 'timestamp',
518 'value' => $timestamp ) ) .
519 wfElement( 'input', array(
521 'name' => 'wpEditToken',
522 'value' => $wgUser->editToken() ) ) .
523 wfElement( 'input', array(
527 wfElement( 'input', array(
529 'value' => wfMsg( 'showpreview' ) ) ) .
530 wfCloseElement( 'form' ) .
531 wfCloseElement( 'div' ) );
535 * Show a deleted file version requested by the visitor.
537 function showFile( $key ) {
541 $store = FileStore
::get( 'deleted' );
542 $store->stream( $key );
545 /* private */ function showHistory() {
546 global $wgLang, $wgUser, $wgOut;
548 $sk = $wgUser->getSkin();
549 if ( $this->mAllowed
) {
550 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
552 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
555 $archive = new PageArchive( $this->mTargetObj
);
556 $text = $archive->getLastRevisionText();
558 if( is_null( $text ) ) {
559 $wgOut->addWikiText( wfMsg( "nohistory" ) );
563 if ( $this->mAllowed
) {
564 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
566 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
569 # List all stored revisions
570 $revisions = $archive->listRevisions();
571 $files = $archive->listFiles();
573 $haveRevisions = $revisions && $revisions->numRows() > 0;
574 $haveFiles = $files && $files->numRows() > 0;
576 # Batch existence check on user and talk pages
577 if( $haveRevisions ) {
578 $batch = new LinkBatch();
579 while( $row = $revisions->fetchObject() ) {
580 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
581 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
584 $revisions->seek( 0 );
587 $batch = new LinkBatch();
588 while( $row = $files->fetchObject() ) {
589 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
590 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
596 if ( $this->mAllowed
) {
597 $titleObj = Title
::makeTitle( NS_SPECIAL
, "Undelete" );
598 $action = $titleObj->getLocalURL( "action=submit" );
599 # Start the form here
600 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
601 $wgOut->addHtml( $top );
604 # Show relevant lines from the deletion log:
605 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
606 $logViewer = new LogViewer(
609 array( 'page' => $this->mTargetObj
->getPrefixedText(),
610 'type' => 'delete' ) ) ) );
611 $logViewer->showList( $wgOut );
613 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
614 # Format the user-visible controls (comment field, submission button)
615 # in a nice little table
616 $table = '<fieldset><table><tr>';
617 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
618 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
619 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment
) . '</td>';
620 $table .= '</tr><tr><td> </td><td>';
621 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
622 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
623 $table .= '</td></tr></table></fieldset>';
624 $wgOut->addHtml( $table );
627 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
629 if( $haveRevisions ) {
630 # The page's stored (deleted) history:
631 $wgOut->addHTML("<ul>");
632 $target = urlencode( $this->mTarget
);
633 while( $row = $revisions->fetchObject() ) {
634 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
635 if ( $this->mAllowed
) {
636 $checkBox = wfCheck( "ts$ts" );
637 $pageLink = $sk->makeKnownLinkObj( $titleObj,
638 $wgLang->timeanddate( $ts, true ),
639 "target=$target×tamp=$ts" );
642 $pageLink = $wgLang->timeanddate( $ts, true );
644 $userLink = $sk->userLink( $row->ar_user
, $row->ar_user_text
) . $sk->userToolLinks( $row->ar_user
, $row->ar_user_text
);
645 $comment = $sk->commentBlock( $row->ar_comment
);
646 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
650 $wgOut->addHTML("</ul>");
652 $wgOut->addWikiText( wfMsg( "nohistory" ) );
657 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
658 $wgOut->addHtml( "<ul>" );
659 while( $row = $files->fetchObject() ) {
660 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
661 if ( $this->mAllowed
&& $row->fa_storage_key
) {
662 $checkBox = wfCheck( "fileid" . $row->fa_id
);
663 $key = urlencode( $row->fa_storage_key
);
664 $target = urlencode( $this->mTarget
);
665 $pageLink = $sk->makeKnownLinkObj( $titleObj,
666 $wgLang->timeanddate( $ts, true ),
667 "target=$target&file=$key" );
670 $pageLink = $wgLang->timeanddate( $ts, true );
672 $userLink = $sk->userLink( $row->fa_user
, $row->fa_user_text
) . $sk->userToolLinks( $row->fa_user
, $row->fa_user_text
);
674 wfMsgHtml( 'widthheight',
675 $wgLang->formatNum( $row->fa_width
),
676 $wgLang->formatNum( $row->fa_height
) ) .
678 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
680 $comment = $sk->commentBlock( $row->fa_description
);
681 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
684 $wgOut->addHTML( "</ul>" );
687 if ( $this->mAllowed
) {
688 # Slip in the hidden controls here
689 $misc = wfHidden( 'target', $this->mTarget
);
690 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
691 $wgOut->addHtml( $misc . '</form>' );
697 function undelete() {
698 global $wgOut, $wgUser;
699 if( !is_null( $this->mTargetObj
) ) {
700 $archive = new PageArchive( $this->mTargetObj
);
703 $ok = $archive->undelete(
704 $this->mTargetTimestamp
,
706 $this->mFileVersions
);
709 $skin =& $wgUser->getSkin();
710 $link = $skin->makeKnownLinkObj( $this->mTargetObj
);
711 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
715 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );