- extract( $dbw->tableNames( 'cur', 'archive', 'old' ) );
- $namespace = $this->title->getNamespace();
- $ttl = $this->title->getDBkey();
- $t = $dbw->strencode( $ttl );
-
- # 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';
- }
- $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 = Article::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 ),
- 'inverse_timestamp' => wfInvertTimestamp( wfTimestamp( TS_MW, $s->ar_timestamp ) ),
- ), $fname );
-
- $newid = $dbw->insertId();
- if( $restoreAll ) {
- $oldones = "AND ar_timestamp<" . $dbw->addQuotes( $dbw->timestamp( $max ) );
- }
+ extract( $dbw->tableNames( 'page', 'archive' ) );
+
+ # Does this page already exist? We'll have to update it...
+ $article = new Article( $this->title );
+ $options = ( $wgDBtype == 'postgres' )
+ ? '' // 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;