--- /dev/null
+<?php # $Id: wordpress.inc.php,v 1.16 2005/05/17 11:34:48 garvinhicking Exp $\r
+# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)\r
+# All rights reserved. See LICENSE file for licensing details\r
+\r
+/*****************************************************************\r
+ * WordPress PostgreSQL Importer, by Devrim GUNDUZ *\r
+ *****************************************************************/\r
+\r
+class Serendipity_Import_WordPress_PG extends Serendipity_Import {\r
+ var $info = array('software' => 'WordPress PostgreSQL');\r
+ var $data = array();\r
+ var $inputFields = array();\r
+\r
+\r
+ function Serendipity_Import_WordPress_PG($data) {\r
+ $this->data = $data;\r
+ $this->inputFields = array(array('text' => INSTALL_DBHOST,\r
+ 'type' => 'input',\r
+ 'name' => 'host'),\r
+\r
+ array('text' => INSTALL_DBUSER,\r
+ 'type' => 'input',\r
+ 'name' => 'user'),\r
+\r
+ array('text' => INSTALL_DBPASS,\r
+ 'type' => 'protected',\r
+ 'name' => 'password'),\r
+\r
+ array('text' => INSTALL_DBPORT,\r
+ 'type' => 'input',\r
+ 'name' => 'port'),\r
+\r
+ array('text' => INSTALL_DBNAME,\r
+ 'type' => 'input',\r
+ 'name' => 'name'),\r
+\r
+ array('text' => INSTALL_DBPREFIX,\r
+ 'type' => 'input',\r
+ 'name' => 'prefix'),\r
+\r
+ array('text' => CHARSET,\r
+ 'type' => 'list',\r
+ 'name' => 'charset',\r
+ 'value' => 'UTF-8',\r
+ 'default' => $this->getCharsets(true)),\r
+\r
+ array('text' => CONVERT_HTMLENTITIES,\r
+ 'type' => 'bool',\r
+ 'name' => 'use_strtr',\r
+ 'default' => 'true'),\r
+\r
+ array('text' => ACTIVATE_AUTODISCOVERY,\r
+ 'type' => 'bool',\r
+ 'name' => 'autodiscovery',\r
+ 'default' => 'false')\r
+ );\r
+ }\r
+\r
+ function validateData() {\r
+ return sizeof($this->data);\r
+ }\r
+\r
+ function getInputFields() {\r
+ return $this->inputFields;\r
+ }\r
+\r
+ function import() {\r
+ global $serendipity;\r
+\r
+ // Save this so we can return it to its original value at the end of this method.\r
+ $noautodiscovery = isset($serendipity['noautodiscovery']) ? $serendipity['noautodiscovery'] : false;\r
+\r
+ if ($this->data['autodiscovery'] == 'false') {\r
+ $serendipity['noautodiscovery'] = 1;\r
+ }\r
+\r
+ $this->getTransTable();\r
+\r
+ $this->data['prefix'] = serendipity_db_escape_string($this->data['prefix']);\r
+ $users = array();\r
+ $categories = array();\r
+ $entries = array();\r
+\r
+ if ( !extension_loaded('pgsql') ) {\r
+ return PGSQL_REQUIRED;;\r
+ }\r
+\r
+ $wpdb = pg_connect("$this->data['host'], $this->data['port'], $this->data['user'], $this->data['pass'], $this->data['name']");\r
+ if ( !$wpdb ) {\r
+ return sprintf(PGSQL_COULDNT_CONNECT, $this->data['pass']);\r
+ }\r
+\r
+ /* Users */\r
+ $res = pg_query($wpdb, "SELECT ID, user_login, user_pass, user_email, user_level FROM {$this->data['prefix']}users;");\r
+ if ( !$res ) {\r
+ return sprintf(COULDNT_SELECT_USER_INFO, pg_last_error($wpdb));\r
+ }\r
+\r
+ for ( $x=0 ; $x<pg_num_rows($res) ; $x++ ) {\r
+ $users[$x] = pg_fetch_assoc($res);\r
+\r
+ $data = array('right_publish' => ($users[$x]['user_level'] >= 1) ? 1 : 0,\r
+ 'realname' => $users[$x]['user_login'],\r
+ 'username' => $users[$x]['user_login'],\r
+ 'password' => $users[$x]['user_pass']); // WP uses md5, too.\r
+\r
+ if ( $users[$x]['user_level'] <= 1 ) {\r
+ $data['userlevel'] = USERLEVEL_EDITOR;\r
+ } elseif ( $users[$x]['user_level'] < 5 ) {\r
+ $data['userlevel'] = USERLEVEL_CHIEF;\r
+ } else {\r
+ $data['userlevel'] = USERLEVEL_ADMIN;\r
+ }\r
+\r
+ if ($serendipity['serendipityUserlevel'] < $data['userlevel']) {\r
+ $data['userlevel'] = $serendipity['serendipityUserlevel'];\r
+ }\r
+\r
+ serendipity_db_insert('authors', $this->strtrRecursive($data));\r
+ $users[$x]['authorid'] = serendipity_db_insert_id('authors', 'authorid');\r
+ }\r
+\r
+ /* Categories */\r
+ $res = @pg_query($wpdb, "SELECT cat_ID, cat_name, category_description, category_parent FROM {$this->data['prefix']}categories ORDER BY category_parent, cat_ID;");\r
+ if ( !$res ) {\r
+ return sprintf(COULDNT_SELECT_CATEGORY_INFO, pg_last_error($wpdb));\r
+ }\r
+\r
+ // Get all the info we need\r
+ for ( $x=0 ; $x<pg_num_rows($res) ; $x++ )\r
+ $categories[] = pg_fetch_assoc($res);\r
+\r
+ // Insert all categories as top level (we need to know everyone's ID before we can represent the hierarchy).\r
+ for ( $x=0 ; $x<sizeof($categories) ; $x++ ) {\r
+ $cat = array('category_name' => $categories[$x]['cat_name'],\r
+ 'category_description' => $categories[$x]['category_description'],\r
+ 'parentid' => 0, // <---\r
+ 'category_left' => 0,\r
+ 'category_right' => 0);\r
+\r
+ serendipity_db_insert('category', $this->strtrRecursive($cat));\r
+ $categories[$x]['categoryid'] = serendipity_db_insert_id('category', 'categoryid');\r
+ }\r
+\r
+ // There has to be a more efficient way of doing this...\r
+ foreach ( $categories as $cat ) {\r
+ if ( $cat['category_parent'] != 0 ) {\r
+ // Find the parent\r
+ $par_id = 0;\r
+ foreach ( $categories as $possible_par ) {\r
+ if ( $possible_par['cat_ID'] == $cat['category_parent'] ) {\r
+ $par_id = $possible_par['categoryid'];\r
+ break;\r
+ }\r
+ }\r
+\r
+ if ( $par_id != 0 ) {\r
+ serendipity_db_query("UPDATE {$serendipity['dbPrefix']}category SET parentid={$par_id} WHERE categoryid={$cat['categoryid']};");\r
+ } // else { echo "D'oh! " . random_string_of_profanity(); }\r
+ }\r
+ }\r
+\r
+ serendipity_rebuildCategoryTree();\r
+\r
+ /* Entries */\r
+ $res = @pg_query($wpdb, "SELECT * FROM {$this->data['prefix']}posts ORDER BY post_date;");\r
+ if ( !$res ) {\r
+ return sprintf(COULDNT_SELECT_ENTRY_INFO, pg_last_error($wpdb));\r
+ }\r
+\r
+ for ( $x=0 ; $x<pg_num_rows($res) ; $x++ ) {\r
+ $entries[$x] = pg_fetch_assoc($res);\r
+\r
+ $entry = array('title' => $this->decode($entries[$x]['post_title']), // htmlentities() is called later, so we can leave this.\r
+ 'isdraft' => ($entries[$x]['post_status'] == 'publish') ? 'false' : 'true',\r
+ 'allow_comments' => ($entries[$x]['comment_status'] == 'open' ) ? 'true' : 'false',\r
+ 'timestamp' => strtotime($entries[$x]['post_date']),\r
+ 'body' => $this->strtr($entries[$x]['post_content']));\r
+\r
+ foreach ( $users as $user ) {\r
+ if ( $user['ID'] == $entries[$x]['post_author'] ) {\r
+ $entry['authorid'] = $user['authorid'];\r
+ break;\r
+ }\r
+ }\r
+\r
+ if ( !is_int($entries[$x]['entryid'] = serendipity_updertEntry($entry)) ) {\r
+ return $entries[$x]['entryid'];\r
+ }\r
+ }\r
+\r
+ /* Entry/category */\r
+ $res = @pg_query($wpdb, "SELECT * FROM {$this->data['prefix']}post2cat;");\r
+ if ( !$res ) {\r
+ return sprintf(COULDNT_SELECT_ENTRY_INFO, pg_last_error($wpdb));\r
+ }\r
+\r
+ while ( $a = pg_fetch_assoc($res) ) {\r
+ foreach ( $categories as $category ) {\r
+ if ( $category['cat_ID'] == $a['category_id'] ) {\r
+ foreach ( $entries as $entry ) {\r
+ if ( $a['post_id'] == $entry['ID'] ) {\r
+ $data = array('entryid' => $entry['entryid'],\r
+ 'categoryid' => $category['categoryid']);\r
+ serendipity_db_insert('entrycat', $this->strtrRecursive($data));\r
+ break;\r
+ }\r
+ }\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ /* Comments */\r
+ $res = @pg_query($wpdb, "SELECT * FROM {$this->data['prefix']}comments;");\r
+ if ( !$res ) {\r
+ return sprintf(COULDNT_SELECT_COMMENT_INFO, pg_last_error($wpdb));\r
+ }\r
+\r
+ while ( $a = pg_fetch_assoc($res) ) {\r
+ foreach ( $entries as $entry ) {\r
+ if ( $entry['ID'] == $a['comment_post_ID'] ) {\r
+ $comment = array('entry_id ' => $entry['entryid'],\r
+ 'parent_id' => 0,\r
+ 'timestamp' => strtotime($a['comment_date']),\r
+ 'author' => $a['comment_author'],\r
+ 'email' => $a['comment_author_email'],\r
+ 'url' => $a['comment_author_url'],\r
+ 'ip' => $a['comment_author_IP'],\r
+ 'status' => (empty($a['comment_approved']) || $a['comment_approved'] == '1') ? 'approved' : 'pending',\r
+ 'subscribed'=> 'false',\r
+ 'body' => $a['comment_content'],\r
+ 'type' => 'NORMAL');\r
+\r
+ serendipity_db_insert('comments', $this->strtrRecursive($comment));\r
+ if ($comment['status'] == 'approved') {\r
+ $cid = serendipity_db_insert_id('comments', 'id');\r
+ serendipity_approveComment($cid, $entry['entryid'], true);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ $serendipity['noautodiscovery'] = $noautodiscovery;\r
+\r
+ // That was fun.\r
+ return true;\r
+ }\r
+}\r
+\r
+return 'Serendipity_Import_WordPress_PG';\r
+\r
+/* vim: set sts=4 ts=4 expandtab : */\r
+?>\r