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 ?
null : $row->ar_text
),
143 'comment' => $row->ar_comment
,
144 'user' => $row->ar_user
,
145 'user_text' => $row->ar_user_text
,
146 'timestamp' => $row->ar_timestamp
,
147 'minor_edit' => $row->ar_minor_edit
,
148 'text_id' => $row->ar_text_id
) );
155 * Get the text from an archive row containing ar_text, ar_flags and ar_text_id
157 function getTextFromRow( $row ) {
158 if( is_null( $row->ar_text_id
) ) {
159 // An old row from MediaWiki 1.4 or previous.
160 // Text is embedded in this row in classic compression format.
161 return Revision
::getRevisionText( $row, "ar_" );
163 // New-style: keyed to the text storage backend.
164 $dbr =& wfGetDB( DB_SLAVE
);
165 $text = $dbr->selectRow( 'text',
166 array( 'old_text', 'old_flags' ),
167 array( 'old_id' => $row->ar_text_id
),
169 return Revision
::getRevisionText( $text );
175 * Fetch (and decompress if necessary) the stored text of the most
176 * recently edited deleted revision of the page.
178 * If there are no archived revisions for the page, returns NULL.
182 function getLastRevisionText() {
183 $dbr =& wfGetDB( DB_SLAVE
);
184 $row = $dbr->selectRow( 'archive',
185 array( 'ar_text', 'ar_flags', 'ar_text_id' ),
186 array( 'ar_namespace' => $this->title
->getNamespace(),
187 'ar_title' => $this->title
->getDBkey() ),
188 'PageArchive::getLastRevisionText',
189 array( 'ORDER BY' => 'ar_timestamp DESC' ) );
191 return $this->getTextFromRow( $row );
198 * Quick check if any archived revisions are present for the page.
201 function isDeleted() {
202 $dbr =& wfGetDB( DB_SLAVE
);
203 $n = $dbr->selectField( 'archive', 'COUNT(ar_title)',
204 array( 'ar_namespace' => $this->title
->getNamespace(),
205 'ar_title' => $this->title
->getDBkey() ) );
210 * Restore the given (or all) text and file revisions for the page.
211 * Once restored, the items will be removed from the archive tables.
212 * The deletion log will be updated with an undeletion notice.
214 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
215 * @param string $comment
216 * @param array $fileVersions
218 * @return true on success.
220 function undelete( $timestamps, $comment = '', $fileVersions = array() ) {
221 // If both the set of text revisions and file revisions are empty,
222 // restore everything. Otherwise, just restore the requested items.
223 $restoreAll = empty( $timestamps ) && empty( $fileVersions );
225 $restoreText = $restoreAll ||
!empty( $timestamps );
226 $restoreFiles = $restoreAll ||
!empty( $fileVersions );
228 if( $restoreFiles && $this->title
->getNamespace() == NS_IMAGE
) {
229 $img = new Image( $this->title
);
230 $filesRestored = $img->restore( $fileVersions );
236 $textRestored = $this->undeleteRevisions( $timestamps );
243 $log = new LogPage( 'delete' );
245 if( $textRestored && $filesRestored ) {
246 $reason = wfMsgForContent( 'undeletedrevisions-files',
247 $wgContLang->formatNum( $textRestored ),
248 $wgContLang->formatNum( $filesRestored ) );
249 } elseif( $textRestored ) {
250 $reason = wfMsgForContent( 'undeletedrevisions',
251 $wgContLang->formatNum( $textRestored ) );
252 } elseif( $filesRestored ) {
253 $reason = wfMsgForContent( 'undeletedfiles',
254 $wgContLang->formatNum( $filesRestored ) );
256 wfDebug( "Undelete: nothing undeleted...\n" );
260 if( trim( $comment ) != '' )
261 $reason .= ": {$comment}";
262 $log->addEntry( 'restore', $this->title
, $reason );
268 * This is the meaty bit -- restores archived revisions of the given page
269 * to the cur/old tables. If the page currently exists, all revisions will
270 * be stuffed into old, otherwise the most recent will go into cur.
272 * @param array $timestamps Pass an empty array to restore all revisions, otherwise list the ones to undelete.
273 * @param string $comment
274 * @param array $fileVersions
276 * @return int number of revisions restored
278 private function undeleteRevisions( $timestamps ) {
279 global $wgParser, $wgDBtype;
281 $restoreAll = empty( $timestamps );
283 $dbw =& wfGetDB( DB_MASTER
);
284 extract( $dbw->tableNames( 'page', 'archive' ) );
286 # Does this page already exist? We'll have to update it...
287 $article = new Article( $this->title
);
288 $options = ( $wgDBtype == 'postgres' )
289 ?
'' // pg doesn't support this?
291 $page = $dbw->selectRow( 'page',
292 array( 'page_id', 'page_latest' ),
293 array( 'page_namespace' => $this->title
->getNamespace(),
294 'page_title' => $this->title
->getDBkey() ),
298 # Page already exists. Import the history, and if necessary
299 # we'll update the latest revision field in the record.
301 $pageId = $page->page_id
;
302 $previousRevId = $page->page_latest
;
304 # Have to create a new article...
305 $newid = $article->insertOn( $dbw );
311 $oldones = '1 = 1'; # All revisions...
313 $oldts = implode( ',',
314 array_map( array( &$dbw, 'addQuotes' ),
315 array_map( array( &$dbw, 'timestamp' ),
318 $oldones = "ar_timestamp IN ( {$oldts} )";
322 * Restore each revision...
324 $result = $dbw->select( 'archive',
336 'ar_namespace' => $this->title
->getNamespace(),
337 'ar_title' => $this->title
->getDBkey(),
341 'ORDER BY' => 'ar_timestamp' )
343 if( $dbw->numRows( $result ) < count( $timestamps ) ) {
344 wfDebug( __METHOD__
.": couldn't find all requested rows\n" );
349 $newRevId = $previousRevId;
352 while( $row = $dbw->fetchObject( $result ) ) {
353 if( $row->ar_text_id
) {
354 // Revision was deleted in 1.5+; text is in
355 // the regular text table, use the reference.
356 // Specify null here so the so the text is
357 // dereferenced for page length info if needed.
360 // Revision was deleted in 1.4 or earlier.
361 // Text is squashed into the archive row, and
362 // a new text table entry will be created for it.
363 $revText = Revision
::getRevisionText( $row, 'ar_' );
365 $revision = new Revision( array(
367 'id' => $row->ar_rev_id
,
369 'comment' => $row->ar_comment
,
370 'user' => $row->ar_user
,
371 'user_text' => $row->ar_user_text
,
372 'timestamp' => $row->ar_timestamp
,
373 'minor_edit' => $row->ar_minor_edit
,
374 'text_id' => $row->ar_text_id
,
376 $newRevId = $revision->insertOn( $dbw );
381 # FIXME: Update latest if newer as well...
383 // Attach the latest revision to the page...
384 $article->updateRevisionOn( $dbw, $revision, $previousRevId );
386 // Update site stats, link tables, etc
387 $article->createUpdates( $revision );
391 Article
::onArticleCreate( $this->title
);
393 Article
::onArticleEdit( $this->title
);
396 # Something went terribly wrong!
399 # Now that it's safely stored, take it out of the archive
400 $dbw->delete( 'archive',
402 'ar_namespace' => $this->title
->getNamespace(),
403 'ar_title' => $this->title
->getDBkey(),
415 * @subpackage SpecialPage
418 var $mAction, $mTarget, $mTimestamp, $mRestore, $mTargetObj;
419 var $mTargetTimestamp, $mAllowed, $mComment;
421 function UndeleteForm( &$request, $par = "" ) {
423 $this->mAction
= $request->getVal( 'action' );
424 $this->mTarget
= $request->getVal( 'target' );
425 $time = $request->getVal( 'timestamp' );
426 $this->mTimestamp
= $time ?
wfTimestamp( TS_MW
, $time ) : '';
427 $this->mFile
= $request->getVal( 'file' );
429 $posted = $request->wasPosted() &&
430 $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
431 $this->mRestore
= $request->getCheck( 'restore' ) && $posted;
432 $this->mPreview
= $request->getCheck( 'preview' ) && $posted;
433 $this->mComment
= $request->getText( 'wpComment' );
436 $this->mTarget
= $par;
438 if ( $wgUser->isAllowed( 'delete' ) && !$wgUser->isBlocked() ) {
439 $this->mAllowed
= true;
441 $this->mAllowed
= false;
442 $this->mTimestamp
= '';
443 $this->mRestore
= false;
445 if ( $this->mTarget
!== "" ) {
446 $this->mTargetObj
= Title
::newFromURL( $this->mTarget
);
448 $this->mTargetObj
= NULL;
450 if( $this->mRestore
) {
451 $timestamps = array();
452 $this->mFileVersions
= array();
453 foreach( $_REQUEST as $key => $val ) {
454 if( preg_match( '/^ts(\d{14})$/', $key, $matches ) ) {
455 array_push( $timestamps, $matches[1] );
458 if( preg_match( '/^fileid(\d+)$/', $key, $matches ) ) {
459 $this->mFileVersions
[] = intval( $matches[1] );
462 rsort( $timestamps );
463 $this->mTargetTimestamp
= $timestamps;
469 if( is_null( $this->mTargetObj
) ) {
470 return $this->showList();
472 if( $this->mTimestamp
!== '' ) {
473 return $this->showRevision( $this->mTimestamp
);
475 if( $this->mFile
!== null ) {
476 return $this->showFile( $this->mFile
);
478 if( $this->mRestore
&& $this->mAction
== "submit" ) {
479 return $this->undelete();
481 return $this->showHistory();
484 /* private */ function showList() {
485 global $wgLang, $wgContLang, $wgOut;
487 # List undeletable articles
488 $result = PageArchive
::listAllPages();
490 if ( $this->mAllowed
) {
491 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
493 $wgOut->setPagetitle( wfMsg( "viewdeletedpage" ) );
495 $wgOut->addWikiText( wfMsg( "undeletepagetext" ) );
497 $undelete =& SpecialPage
::getTitleFor( 'Undelete' );
498 $wgOut->addHTML( "<ul>\n" );
499 while( $row = $result->fetchObject() ) {
500 $title = Title
::makeTitleSafe( $row->ar_namespace
, $row->ar_title
);
501 $link = Linker
::makeKnownLinkObj( $undelete, htmlspecialchars( $title->getPrefixedText() ), 'target=' . $title->getPrefixedUrl() );
502 $revs = wfMsgHtml( 'undeleterevisions', $wgLang->formatNum( $row->count
) );
503 $wgOut->addHtml( "<li>{$link} ({$revs})</li>\n" );
506 $wgOut->addHTML( "</ul>\n" );
511 /* private */ function showRevision( $timestamp ) {
512 global $wgLang, $wgUser, $wgOut;
514 if(!preg_match("/[0-9]{14}/",$timestamp)) return 0;
516 $archive = new PageArchive( $this->mTargetObj
);
517 $rev = $archive->getRevision( $timestamp );
519 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
520 $wgOut->addWikiText( "(" . wfMsg( "undeleterevision",
521 $wgLang->date( $timestamp ) ) . ")\n" );
524 $wgOut->addWikiText( wfMsg( 'undeleterevision-missing' ) );
528 wfRunHooks( 'UndeleteShowRevision', array( $this->mTargetObj
, $rev ) );
530 if( $this->mPreview
) {
531 $wgOut->addHtml( "<hr />\n" );
532 $wgOut->addWikiText( $rev->getText() );
535 $self = SpecialPage
::getTitleFor( "Undelete" );
538 wfElement( 'textarea', array(
540 'cols' => intval( $wgUser->getOption( 'cols' ) ),
541 'rows' => intval( $wgUser->getOption( 'rows' ) ) ),
542 $rev->getText() . "\n" ) .
543 wfOpenElement( 'div' ) .
544 wfOpenElement( 'form', array(
546 'action' => $self->getLocalURL( "action=submit" ) ) ) .
547 wfElement( 'input', array(
550 'value' => $this->mTargetObj
->getPrefixedDbKey() ) ) .
551 wfElement( 'input', array(
553 'name' => 'timestamp',
554 'value' => $timestamp ) ) .
555 wfElement( 'input', array(
557 'name' => 'wpEditToken',
558 'value' => $wgUser->editToken() ) ) .
559 wfElement( 'input', array(
563 wfElement( 'input', array(
565 'value' => wfMsg( 'showpreview' ) ) ) .
566 wfCloseElement( 'form' ) .
567 wfCloseElement( 'div' ) );
571 * Show a deleted file version requested by the visitor.
573 function showFile( $key ) {
577 $store = FileStore
::get( 'deleted' );
578 $store->stream( $key );
581 /* private */ function showHistory() {
582 global $wgLang, $wgUser, $wgOut;
584 if ( $this->mAllowed
) {
585 $wgOut->setPagetitle( wfMsg( "undeletepage" ) );
587 $wgOut->setPagetitle( wfMsg( 'viewdeletedpage' ) );
590 $archive = new PageArchive( $this->mTargetObj
);
591 $text = $archive->getLastRevisionText();
593 if( is_null( $text ) ) {
594 $wgOut->addWikiText( wfMsg( "nohistory" ) );
598 if ( $this->mAllowed
) {
599 $wgOut->addWikiText( wfMsg( "undeletehistory" ) );
601 $wgOut->addWikiText( wfMsg( "undeletehistorynoadmin" ) );
604 # List all stored revisions
605 $revisions = $archive->listRevisions();
606 $files = $archive->listFiles();
608 $haveRevisions = $revisions && $revisions->numRows() > 0;
609 $haveFiles = $files && $files->numRows() > 0;
611 # Batch existence check on user and talk pages
612 if( $haveRevisions ) {
613 $batch = new LinkBatch();
614 while( $row = $revisions->fetchObject() ) {
615 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->ar_user_text
) );
616 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->ar_user_text
) );
619 $revisions->seek( 0 );
622 $batch = new LinkBatch();
623 while( $row = $files->fetchObject() ) {
624 $batch->addObj( Title
::makeTitleSafe( NS_USER
, $row->fa_user_text
) );
625 $batch->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->fa_user_text
) );
631 if ( $this->mAllowed
) {
632 $titleObj = SpecialPage
::getTitleFor( "Undelete" );
633 $action = $titleObj->getLocalURL( "action=submit" );
634 # Start the form here
635 $top = wfOpenElement( 'form', array( 'method' => 'post', 'action' => $action, 'id' => 'undelete' ) );
636 $wgOut->addHtml( $top );
639 # Show relevant lines from the deletion log:
640 $wgOut->addHTML( "<h2>" . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
641 $logViewer = new LogViewer(
644 array( 'page' => $this->mTargetObj
->getPrefixedText(),
645 'type' => 'delete' ) ) ) );
646 $logViewer->showList( $wgOut );
648 if( $this->mAllowed
&& ( $haveRevisions ||
$haveFiles ) ) {
649 # Format the user-visible controls (comment field, submission button)
650 # in a nice little table
651 $table = '<fieldset><table><tr>';
652 $table .= '<td colspan="2">' . wfMsgWikiHtml( 'undeleteextrahelp' ) . '</td></tr><tr>';
653 $table .= '<td align="right"><strong>' . wfMsgHtml( 'undeletecomment' ) . '</strong></td>';
654 $table .= '<td>' . wfInput( 'wpComment', 50, $this->mComment
) . '</td>';
655 $table .= '</tr><tr><td> </td><td>';
656 $table .= wfSubmitButton( wfMsg( 'undeletebtn' ), array( 'name' => 'restore' ) );
657 $table .= wfElement( 'input', array( 'type' => 'reset', 'value' => wfMsg( 'undeletereset' ) ) );
658 $table .= '</td></tr></table></fieldset>';
659 $wgOut->addHtml( $table );
662 $wgOut->addHTML( "<h2>" . htmlspecialchars( wfMsg( "history" ) ) . "</h2>\n" );
664 if( $haveRevisions ) {
665 # The page's stored (deleted) history:
666 $wgOut->addHTML("<ul>");
667 $target = urlencode( $this->mTarget
);
668 while( $row = $revisions->fetchObject() ) {
669 $ts = wfTimestamp( TS_MW
, $row->ar_timestamp
);
670 if ( $this->mAllowed
) {
671 $checkBox = wfCheck( "ts$ts" );
672 $pageLink = Linker
::makeKnownLinkObj( $titleObj,
673 $wgLang->timeanddate( $ts, true ),
674 "target=$target×tamp=$ts" );
677 $pageLink = $wgLang->timeanddate( $ts, true );
679 $userLink = Linker
::userLink( $row->ar_user
, $row->ar_user_text
) . Linker
::userToolLinks( $row->ar_user
, $row->ar_user_text
);
680 $comment = Linker
::commentBlock( $row->ar_comment
);
681 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $comment</li>\n" );
685 $wgOut->addHTML("</ul>");
687 $wgOut->addWikiText( wfMsg( "nohistory" ) );
692 $wgOut->addHtml( "<h2>" . wfMsgHtml( 'imghistory' ) . "</h2>\n" );
693 $wgOut->addHtml( "<ul>" );
694 while( $row = $files->fetchObject() ) {
695 $ts = wfTimestamp( TS_MW
, $row->fa_timestamp
);
696 if ( $this->mAllowed
&& $row->fa_storage_key
) {
697 $checkBox = wfCheck( "fileid" . $row->fa_id
);
698 $key = urlencode( $row->fa_storage_key
);
699 $target = urlencode( $this->mTarget
);
700 $pageLink = Linker
::makeKnownLinkObj( $titleObj,
701 $wgLang->timeanddate( $ts, true ),
702 "target=$target&file=$key" );
705 $pageLink = $wgLang->timeanddate( $ts, true );
707 $userLink = Linker
::userLink( $row->fa_user
, $row->fa_user_text
) . Linker
::userToolLinks( $row->fa_user
, $row->fa_user_text
);
709 wfMsgHtml( 'widthheight',
710 $wgLang->formatNum( $row->fa_width
),
711 $wgLang->formatNum( $row->fa_height
) ) .
713 wfMsgHtml( 'nbytes', $wgLang->formatNum( $row->fa_size
) ) .
715 $comment = Linker
::commentBlock( $row->fa_description
);
716 $wgOut->addHTML( "<li>$checkBox $pageLink . . $userLink $data $comment</li>\n" );
719 $wgOut->addHTML( "</ul>" );
722 if ( $this->mAllowed
) {
723 # Slip in the hidden controls here
724 $misc = wfHidden( 'target', $this->mTarget
);
725 $misc .= wfHidden( 'wpEditToken', $wgUser->editToken() );
726 $wgOut->addHtml( $misc . '</form>' );
732 function undelete() {
734 if( !is_null( $this->mTargetObj
) ) {
735 $archive = new PageArchive( $this->mTargetObj
);
738 $ok = $archive->undelete(
739 $this->mTargetTimestamp
,
741 $this->mFileVersions
);
744 $link = Linker
::makeKnownLinkObj( $this->mTargetObj
);
745 $wgOut->addHtml( wfMsgWikiHtml( 'undeletedpage', $link ) );
749 $wgOut->showFatalError( wfMsg( "cannotundelete" ) );