Trying to add records to a table with MYSQL and php, with a forced unique identifier

I am trying to modify a script that was developed to import article records from a Joomla (1.5.x) database into a WordPress 3.2.1 table for posts. It is a script that migrates content from Joomla to WordPress.

The issue I had with the script is that it did not maintain the unique identifier (‘id’ in Joomla, and ‘ID’ in WordPress). Based on my understanding, this makes it a lot more complicated (much more work) to deal with redirecting all the Joomla permalinks over to the new (and obviously different) WordPress permalinks. If the ID was the same in WP as it was in Joomla then some basic rewrite rules in htaccess would be enough to perform the redirections.

Read More

So I want to see if I can modify the code to force the ID rather than it being generated in consecutive order as records are inserted into the table.

The script I am modifying is available here: http://wordpress.org/extend/plugins/joomla-to-wordpress-migrator/
The file in question is called: joomla2wp-mig.php

The array is being created at around line 1049 and 1081.
At line 1049 it is:

 $wp_posts[] = array(
        'ID' => $R->id,  //I ADDED THIS IN
        'post_author' => $user_id,
        'post_category' => array($wp_cat_id),
        'post_content' => $post_content, 
        'post_date' => $R->created,
        'post_date_gmt' => $R->created,
        'post_modified' => $R->modified,
        'post_modified_gmt' => $R->modified,
        'post_title' => $R->title,
        'post_status' => 'publish',
        'comment_status' => 'open',
        'ping_status' => 'open',
        'post_name' => $R->alias,
        'tags_input' => $R->metakey, 
        'post_type' => 'post'
      );

And at line 1081 it is:

 $array = array(
 "ID"        => $item['ID'],    //I ADDED THIS IN
 "post_author"       => $user_id,
 "post_parent"       => intval($wp_cat_id),
 "post_content"      => $item['post_content'],
 "post_date"         => $item['post_date'],
 "post_date_gmt"     => $item['post_date_gmt'],
 "post_modified"     => $item['post_modified'],
 "post_modified_gmt" => $item['post_modified_gmt'],
 "post_title"        => $item['post_title'],
 "post_status"       => $item['post_status'],
 "comment_status"    => $item['comment_status'],
 "ping_status"       => $item['ping_status'],
 "post_name"         => $item['post_name'],
 "post_type"         => $item['post_type']
      );

I have commented the ID line which I have added into the top of each of these bits of array code.

The INSERT command is being implimented around line 1097

The INSERT command is put together like this:

$insert_sql = "INSERT INTO " . $j2wp_wp_tb_prefix . "posts" . " set ";

$inserted = 0;
foreach ($array as $k => $v) 
{
  if($k AND $v) 
  {
    if($inserted > 0) 
      $insert_sql .= ",";
    $insert_sql .= " ".$k." = '".mysql_escape_string(str_replace("`","",$v))."'";
    ++$inserted;
  }
}    
$sql_query[] = $insert_sql;

}

It uses the MYSQL function INSERT INTO… SET (as opposed to INSERT INTO… VALUE)

The challenge I have is this:
The array did not include the ID, so I have added this in.

Having made this change, when I run the script it will appear (at the WordPress UI end) to run fine, but no records are inserted, even though it says it was successful.

I found I could get around that issue by setting up a fresh wp_posts table with X number of blank records. Let’s say I am importing 100 articles, then I would put 100 records into the table, and they would have ID 1 to 100. When I run my modified code it will happily update and populate these existing records. What I don’t understand is why it will not create new records when I force the unique identifier (ID) to what I want it as.

I am wondering if I need to use the INSERT INTO… VALUE command instead of INSERT INTO… SET

I was going to test that out, but to be honest I am not a programmer and am just winging it as I go along. So I had not idea how to rewrite the PHP in order to impliment the structure required for the VALUE command in place of SET.

Any help or suggestions would be greatly appreciate.

I gather I have to rewrite the last bit of code I provded above.

There is some discussion on this matter at the wordpress support forums. One user (spiff06) very kindly helped troubleshoot the issue with me. We came unstuck around getting the code to insert new records with a forced identifier, and I went with what we referred to as the “messy” option (which is the method I mentioned above… setting up a table with the required number of blank records).

Even though I’ve used that “messy” method for my own site, it is my wish to make this process work cleanly for other users who are on Joomla 1.5.x and are switching to WP instead of upgrading to a newer Joomla release (which is a big process, so many are just jumping ot WP, like me).

With much thanks…

Jonathan

Related posts

Leave a Reply

2 comments

  1. You can try the following:

    1. Change the structure of the imported mysql table (post@wordpress). Change the id field so it is not anymore an autoincrement field. Let it being just an integer field.

    2. Import the values. This way you can put any values in the field ID without limitations at all.

    3. After the importation change again the structure of the table to set the ID field to be again an autoincrement field.

  2. I never found a truly automated / scripted way of doing this. I ended up doing a workaround:

    For now I’ve imported all my posts the “messy” way, by prepopulating the table.

    THE WORKSROUND METHOD
    Prepopulate the wp_posts table in the WP database with as many records as you require (look in Joomla to see how many records you have). I had 398, so I added 398 records to wp_posts.

    HOW? I did it by exporting the emtpy wp_posts table to a .csv file. I then opened this in Excel (Numbers, or OpenOffice would also do). In the spreadsheet application it was easy to autofill 1 to 398 in the ID column.

    I then reimported that .csv file into wp_posts. This gave me a wp_posts with 398 record rows with 1 to 398 in the ID field.

    I then ran version 1.5.4 of Mambo/Joomla to WordPress migrator, which can be installed from within WordPress.

    End result?

    All posts have the same ID as the original Joomla articles.