From 1c0dcc995d27eff708ee27c6c645b2e9a43a6ce2 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sat, 12 Mar 2005 08:06:46 +0000 Subject: [PATCH] * Make undeletion (more or less) work with new schema * Add ar_rev_id field to archive to preserve revision IDs across deletion+undeletion --- includes/Article.php | 17 +- includes/Revision.php | 2 + includes/SpecialUndelete.php | 205 +++++++++--------- maintenance/archives/patch-archive-rev_id.sql | 6 + maintenance/tables.sql | 1 + maintenance/updaters.inc | 1 + 6 files changed, 122 insertions(+), 110 deletions(-) create mode 100644 maintenance/archives/patch-archive-rev_id.sql diff --git a/includes/Article.php b/includes/Article.php index 23db0ea5c5..cf226d85ec 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -1589,15 +1589,16 @@ class Article { $dbw->insertSelect( 'archive', array( 'page','revision', 'text' ), array( - 'ar_namespace' => 'page_namespace', - 'ar_title' => 'page_title', - 'ar_text' => 'old_text', - 'ar_comment' => 'rev_comment', - 'ar_user' => 'rev_user', - 'ar_user_text' => 'rev_user_text', - 'ar_timestamp' => 'rev_timestamp', + 'ar_namespace' => 'page_namespace', + 'ar_title' => 'page_title', + 'ar_text' => 'old_text', + 'ar_comment' => 'rev_comment', + 'ar_user' => 'rev_user', + 'ar_user_text' => 'rev_user_text', + 'ar_timestamp' => 'rev_timestamp', 'ar_minor_edit' => 'rev_minor_edit', - 'ar_flags' => 0, + 'ar_flags' => 'old_flags', + 'ar_rev_id' => 'rev_id', ), array( 'page_namespace' => $ns, 'page_title' => $t, diff --git a/includes/Revision.php b/includes/Revision.php index 317a832861..30f0e65539 100644 --- a/includes/Revision.php +++ b/includes/Revision.php @@ -406,6 +406,8 @@ class Revision { ), $fname ); + $this->mId = $revisionId; + wfProfileOut( $fname ); return $revisionId; } diff --git a/includes/SpecialUndelete.php b/includes/SpecialUndelete.php index f964d58792..fc0aa337fb 100644 --- a/includes/SpecialUndelete.php +++ b/includes/SpecialUndelete.php @@ -131,127 +131,128 @@ class PageArchive { function undelete( $timestamps ) { global $wgUser, $wgOut, $wgLang, $wgDeferredUpdateList; global $wgUseSquid, $wgInternalServer, $wgLinkCache; + global $wgDBtype; $fname = "doUndeleteArticle"; $restoreAll = empty( $timestamps ); $restoreRevisions = count( $timestamps ); $dbw =& wfGetDB( DB_MASTER ); - extract( $dbw->tableNames( 'cur', 'archive', 'old' ) ); - $namespace = $this->title->getNamespace(); - $ttl = $this->title->getDBkey(); - $t = $dbw->strencode( $ttl ); + extract( $dbw->tableNames( 'page', 'archive' ) ); - # Move article and history from the "archive" table - $sql = "SELECT COUNT(*) AS count FROM $cur WHERE cur_namespace={$namespace} AND cur_title='{$t}'"; - global $wgDBtype; - if( $wgDBtype != 'PostgreSQL' ) { # HACKHACKHACKHACK - $sql .= ' FOR UPDATE'; + # Does this page already exist? We'll have to update it... + $article = new Article( $this->title ); + $options = ( $wgDBtype == 'PostgreSQL' ) + ? '' // pg doesn't support this? + : 'FOR UPDATE'; + $page = $dbw->selectRow( 'page', + array( 'page_id', 'page_latest' ), + array( 'page_namespace' => $this->title->getNamespace(), + 'page_title' => $this->title->getDBkey() ), + $fname, + $options ); + if( $page ) { + # Page already exists. Import the history, and if necessary + # we'll update the latest revision field in the record. + $newid = 0; + $pageId = $page->page_id; + $previousRevId = $page->page_latest; + $previousTimestamp = $page->rev_timestamp; + } else { + # Have to create a new article... + $newid = $article->insertOn( $dbw ); + $pageId = $newid; + $previousRevId = 0; + $previousTimestamp = 0; } - $res = $dbw->query( $sql, $fname ); - $row = $dbw->fetchObject( $res ); - $now = wfTimestampNow(); - - if( $row->count == 0) { - # Have to create new article... - $sql = "SELECT ar_text,ar_comment,ar_user,ar_user_text,ar_timestamp,ar_minor_edit,ar_flags FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' "; - if( !$restoreAll ) { - $max = $dbw->addQuotes( $dbw->timestamp( array_shift( $timestamps ) ) ); - $sql .= "AND ar_timestamp={$max} "; - } - $sql .= "ORDER BY ar_timestamp DESC LIMIT 1 FOR UPDATE"; - $res = $dbw->query( $sql, $fname ); - $s = $dbw->fetchObject( $res ); - if( $restoreAll ) { - $max = $s->ar_timestamp; - } - $text = Revision::getRevisionText( $s, "ar_" ); - - $redirect = MagicWord::get( MAG_REDIRECT ); - $redir = $redirect->matchStart( $text ) ? 1 : 0; - - $rand = wfRandom(); - $dbw->insert( 'cur', array( - 'cur_id' => $dbw->nextSequenceValue( 'cur_cur_id_seq' ), - 'cur_namespace' => $namespace, - 'cur_title' => $ttl, - 'cur_text' => $text, - 'cur_comment' => $s->ar_comment, - 'cur_user' => $s->ar_user, - 'cur_timestamp' => $s->ar_timestamp, - 'cur_minor_edit' => $s->ar_minor_edit, - 'cur_user_text' => $s->ar_user_text, - 'cur_is_redirect' => $redir, - 'cur_random' => $rand, - 'cur_touched' => $dbw->timestamp( $now ), - ), $fname ); - - $newid = $dbw->insertId(); - if( $restoreAll ) { - $oldones = "AND ar_timestamp<" . $dbw->addQuotes( $dbw->timestamp( $max ) ); - } + + if( $restoreAll ) { + $oldones = '1'; # All revisions... } else { - # If already exists, put history entirely into old table - $oldones = ""; - $newid = 0; - - # But to make the history list show up right, we need to touch it. - $sql = "UPDATE $cur SET cur_touched='{$now}' WHERE cur_namespace={$namespace} AND cur_title='{$t}'"; - $dbw->query( $sql, $fname ); + $oldts = implode( ',', + array_map( array( &$dbw, 'addQuotes' ), + array_map( array( &$dbw, 'timestamp' ), + $timestamps ) ) ); - # FIXME: Sometimes restored entries will be _newer_ than the current version. - # We should merge. + $oldones = "ar_timestamp IN ( {$oldts} )"; } - if( !$restoreAll ) { - $oldts = array(); - foreach( $timestamps as $ts ) { - array_push( $oldts, $dbw->addQuotes( $dbw->timestamp( $ts ) ) ); - } - $oldts = join( ",", $oldts ); - $oldones = "AND ar_timestamp IN ( {$oldts} )"; - } - $sql = "INSERT INTO $old (old_namespace,old_title,old_text," . - "old_comment,old_user,old_user_text,old_timestamp,old_minor_edit," . - "old_flags) SELECT ar_namespace,ar_title,ar_text,ar_comment," . - "ar_user,ar_user_text,ar_timestamp,ar_minor_edit,ar_flags " . - "FROM $archive WHERE ar_namespace={$namespace} AND ar_title='{$t}' {$oldones}"; - if( $restoreAll || !empty( $oldts ) ) { - $dbw->query( $sql, $fname ); - } - - # Finally, clean up the link tables - if( $newid ) { - $wgLinkCache = new LinkCache(); - # Select for update - $wgLinkCache->forUpdate( true ); - # Create a dummy OutputPage to update the outgoing links - $dummyOut = new OutputPage(); - $dummyOut->addWikiText( $text ); - - $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() ); - array_push( $wgDeferredUpdateList, $u ); - - Article::onArticleCreate( $this->title ); - - #TODO: SearchUpdate, etc. + /** + * Restore each revision... + */ + $result = $dbw->select( 'archive', + /* fields */ array( + 'ar_rev_id', + 'ar_text', + 'ar_comment', + 'ar_user', + 'ar_user_text', + 'ar_timestamp', + 'ar_minor_edit', + 'ar_flags' ), + /* WHERE */ array( + 'ar_namespace' => $this->title->getNamespace(), + 'ar_title' => $this->title->getDBkey(), + $oldones ), + $fname, + /* options */ array( + 'ORDER BY' => 'ar_timestamp' ) + ); + $revision = null; + while( $row = $dbw->fetchObject( $result ) ) { + $revision = new Revision( array( + 'page' => $pageId, + 'id' => $row->ar_rev_id, + 'text' => Revision::getRevisionText( $row, 'ar_' ), + 'comment' => $row->ar_comment, + 'user' => $row->ar_user, + 'user_text' => $row->ar_user_text, + 'timestamp' => $row->ar_timestamp, + 'minor_edit' => $row->ar_minor_edit, + ) ); + $revision->insertOn( $dbw ); } + + if( $revision ) { + # FIXME: Update latest if newer as well... + if( $newid ) { + # FIXME: update article count if changed... + $article->updateRevisionOn( $dbw, $revision->getId(), $previousRevId, + Article::isRedirect( $revision->getText() ) ); + + # Finally, clean up the link tables + $wgLinkCache = new LinkCache(); + # Select for update + $wgLinkCache->forUpdate( true ); + + # Create a dummy OutputPage to update the outgoing links + $dummyOut = new OutputPage(); + $dummyOut->addWikiText( $revision->getText() ); - # Now that it's safely stored, take it out of the archive - $sql = "DELETE FROM $archive WHERE ar_namespace={$namespace} AND " . - "ar_title='{$t}'"; - if( !$restoreAll ) { - $sql .= " AND ar_timestamp IN ( {$oldts}"; + $u = new LinksUpdate( $newid, $this->title->getPrefixedDBkey() ); + array_push( $wgDeferredUpdateList, $u ); + + #TODO: SearchUpdate, etc. + } + if( $newid ) { - if( !empty( $oldts ) ) $sql .= ","; - $sql .= $max; + Article::onArticleCreate( $this->title ); + } else { + Article::onArticleEdit( $this->title ); } - $sql .= ")"; + } else { + # Something went terribly worong! } - $dbw->query( $sql, $fname ); + # Now that it's safely stored, take it out of the archive + $dbw->delete( 'archive', + /* WHERE */ array( + 'ar_namespace' => $this->title->getNamespace(), + 'ar_title' => $this->title->getDBkey(), + $oldones ), + $fname ); - # Touch the log? + # Touch the log! $log = new LogPage( 'delete' ); if( $restoreAll ) { $reason = ""; diff --git a/maintenance/archives/patch-archive-rev_id.sql b/maintenance/archives/patch-archive-rev_id.sql new file mode 100644 index 0000000000..375001b8e1 --- /dev/null +++ b/maintenance/archives/patch-archive-rev_id.sql @@ -0,0 +1,6 @@ +-- New field in archive table to preserve revision IDs across undeletion. +-- Added 2005-03-10 + +ALTER TABLE /*$wgDBprefix*/archive + ADD + ar_rev_id int(8) unsigned; diff --git a/maintenance/tables.sql b/maintenance/tables.sql index 0588b94220..ddc5ea1d8b 100644 --- a/maintenance/tables.sql +++ b/maintenance/tables.sql @@ -91,6 +91,7 @@ CREATE TABLE /*$wgDBprefix*/archive ( ar_timestamp char(14) binary NOT NULL default '', ar_minor_edit tinyint(1) NOT NULL default '0', ar_flags tinyblob NOT NULL default '', + ar_rev_id int(8) unsigned, KEY name_title_timestamp (ar_namespace,ar_title,ar_timestamp) ); diff --git a/maintenance/updaters.inc b/maintenance/updaters.inc index 018488511a..9a973c0aa8 100644 --- a/maintenance/updaters.inc +++ b/maintenance/updaters.inc @@ -35,6 +35,7 @@ $wgNewFields = array( array( 'user_rights', 'ur_user', 'patch-rename-user_groups-and_rights.sql' ), array( 'group', 'group_rights', 'patch-userlevels-rights.sql' ), array( 'logging', 'log_params', 'patch-log_params.sql' ), + array( 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ), ); function add_table( $name, $patch ) { -- 2.20.1