]> git.mjollnir.org Git - s9y.git/commitdiff
WP 2.3 importer, experimental
authorgarvinhicking <garvinhicking>
Fri, 19 Oct 2007 09:36:27 +0000 (09:36 +0000)
committergarvinhicking <garvinhicking>
Fri, 19 Oct 2007 09:36:27 +0000 (09:36 +0000)
docs/NEWS
include/admin/importers/wordpress.inc.php

index 780df6b4ae1d26d6d38499184392bc8696e07834..39dc7ee88035dade6f4ad0efe21b8680050d9ac3 100644 (file)
--- 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.
 
index 1d928d00151adfe313a0a1bfa6511c614ea39b01..b9ba7187483cec024df2479d2f8d8b1887f65b55 100644 (file)
@@ -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...<br />\n";
+            if ($debug) echo "Importing categories (WP 2.2 style)...<br />\n";
 
             // Get all the info we need
             for ($x=0 ; $x<mysql_num_rows($res) ; $x++) {
@@ -200,6 +202,75 @@ class Serendipity_Import_WordPress extends Serendipity_Import {
             if ($debug) echo "Rebuilt category tree.<br />\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)...<br />\n";
+
+            // Get all the info we need
+            for ($x=0 ; $x<mysql_num_rows($res) ; $x++) {
+                $categories[] = mysql_fetch_assoc($res);
+            }
+    
+            // Insert all categories as top level (we need to know everyone's ID before we can represent the hierarchy).
+            for ($x=0, $c = sizeof($categories) ; $x < $c ; $x++) {
+                $cat = array('category_name'        => $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.<br />\n";
+            if ($debug) echo "Rebuilding category tree...<br />\n";
+            serendipity_rebuildCategoryTree();
+            if ($debug) echo "Rebuilt category tree.<br />\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...<br />\n";
+            if ($debug) echo "Importing category associations (WP 2.2 style)...<br />\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.<br />\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)...<br />\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.<br />\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) {