From 7f9571780b3e3693694bb5e7746396401592e0d2 Mon Sep 17 00:00:00 2001 From: garvinhicking Date: Fri, 19 Oct 2007 09:36:27 +0000 Subject: [PATCH] WP 2.3 importer, experimental --- docs/NEWS | 3 + include/admin/importers/wordpress.inc.php | 104 ++++++++++++++++++++-- 2 files changed, 101 insertions(+), 6 deletions(-) diff --git a/docs/NEWS b/docs/NEWS index 780df6b..39dc7ee 100644 --- a/docs/NEWS +++ b/docs/NEWS @@ -3,6 +3,9 @@ Version 1.3 () ------------------------------------------------------------------------ + * Updated WordPress imported to be able to import from a 2.3 + structure (experimental) (garvinhicking) + * Patch PEAR.php for better detection, if already included. Thanks to Assen Tchorbadjiev. diff --git a/include/admin/importers/wordpress.inc.php b/include/admin/importers/wordpress.inc.php index 1d928d0..b9ba718 100644 --- a/include/admin/importers/wordpress.inc.php +++ b/include/admin/importers/wordpress.inc.php @@ -143,14 +143,16 @@ class Serendipity_Import_WordPress extends Serendipity_Import { unset($users); } - /* Categories */ + $no_cat = false; + + /* Categories (WP < 2.3 style) */ $res = @$this->nativeQuery("SELECT cat_ID, cat_name, category_description, category_parent FROM {$this->data['prefix']}categories ORDER BY category_parent, cat_ID;", $wpdb); if (!$res) { - printf(COULDNT_SELECT_CATEGORY_INFO, mysql_error($wpdb)); + $no_cat = mysql_error($wpdb); } else { - if ($debug) echo "Importing categories...
\n"; + if ($debug) echo "Importing categories (WP 2.2 style)...
\n"; // Get all the info we need for ($x=0 ; $x\n"; } + /* Categories (WP >= 2.3 style) */ + $res = @$this->nativeQuery("SELECT taxonomy.description AS category_description, + taxonomy.parent AS category_parent, + taxonomy.term_taxonomy_id AS cat_ID, + terms.name AS cat_name + + FROM {$this->data['prefix']}term_taxonomy AS taxonomy + + JOIN {$this->data['prefix']}terms AS terms + ON taxonomy.term_id = terms.term_id + + WHERE taxonomy.taxonomy = 'category' + ORDER BY taxonomy.parent, taxonomy.term_taxonomy", $wpdb); + if (!$res && !$no_cat) { + $no_cat = mysql_error($wpdb); + } elseif ($res) { + $no_cat = false; + if ($debug) echo "Importing categories (WP 2.3 style)...
\n"; + + // Get all the info we need + for ($x=0 ; $x $categories[$x]['cat_name'], + 'category_description' => $categories[$x]['category_description'], + 'parentid' => 0, + 'category_left' => 0, + 'category_right' => 0); + + serendipity_db_insert('category', $this->strtrRecursive($cat)); + $categories[$x]['categoryid'] = serendipity_db_insert_id('category', 'categoryid'); + + // Set association. + $assoc['categories'][$categories[$x]['cat_ID']] = $categories[$x]['categoryid']; + } + + foreach ($categories as $cat) { + if ($cat['category_parent'] != 0) { + // Find the parent + $par_id = 0; + foreach ($categories as $possible_par) { + if ($possible_par['cat_ID'] == $cat['category_parent']) { + $par_id = $possible_par['categoryid']; + break; + } + } + + if ($par_id != 0) { + serendipity_db_query("UPDATE {$serendipity['dbPrefix']}category + SET parentid={$par_id} + WHERE categoryid={$cat['categoryid']};"); + } + } + } + + // Clean memory + unset($categories); + + if ($debug) echo "Imported categories.
\n"; + if ($debug) echo "Rebuilding category tree...
\n"; + serendipity_rebuildCategoryTree(); + if ($debug) echo "Rebuilt category tree.
\n"; + } + if ($no_cat) { + printf(COULDNT_SELECT_CATEGORY_INFO, $no_cat); + } /* Entries */ if (serendipity_db_bool($this->data['import_all'])) { @@ -241,12 +312,29 @@ class Serendipity_Import_WordPress extends Serendipity_Import { unset($entries); } - /* Entry/category */ + /* Entry/category (WP < 2.3 style)*/ + $no_entrycat = false; $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}post2cat;", $wpdb); if (!$res) { - printf(COULDNT_SELECT_ENTRY_INFO, mysql_error($wpdb)); + $no_entrycat = mysql_error($wpdb); } else { - if ($debug) echo "Importing category associations...
\n"; + if ($debug) echo "Importing category associations (WP 2.2 style)...
\n"; + while ($a = mysql_fetch_assoc($res)) { + $data = array('entryid' => $assoc['entries'][$a['post_id']], + 'categoryid' => $assoc['categories'][$a['category_id']]); + serendipity_db_insert('entrycat', $this->strtrRecursive($data)); + } + if ($debug) echo "Imported category associations.
\n"; + } + + /* Entry/category (WP > 2.3 style)*/ + $res = @$this->nativeQuery("SELECT rel.object_id AS post_id, + rel.term_taxonomy_id AS category_id + FROM {$this->data['prefix']}term_relationships AS rel;", $wpdb); + if (!$res && !$no_entrycat) { + printf(COULDNT_SELECT_ENTRY_INFO, mysql_error($wpdb)); + } elseif ($res) { + if ($debug) echo "Importing category associations (WP 2.3 style)...
\n"; while ($a = mysql_fetch_assoc($res)) { $data = array('entryid' => $assoc['entries'][$a['post_id']], 'categoryid' => $assoc['categories'][$a['category_id']]); @@ -255,6 +343,10 @@ class Serendipity_Import_WordPress extends Serendipity_Import { if ($debug) echo "Imported category associations.
\n"; } + if ($no_entrycat) { + printf(COULDNT_SELECT_ENTRY_INFO, mysql_error($wpdb)); + } + /* Comments */ $res = @$this->nativeQuery("SELECT * FROM {$this->data['prefix']}comments;", $wpdb); if (!$res) { -- 2.39.5