Update of /cvsroot/php-blog/serendipity
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18536
Modified Files:
Tag: branch-smarty
NEWS serendipity_admin_interop.inc.php
serendipity_functions.inc.php
Log Message:
Add import code for MT and WP.
Index: serendipity_functions.inc.php
===================================================================
RCS file: /cvsroot/php-blog/serendipity/serendipity_functions.inc.php,v
retrieving revision 1.419.2.47
retrieving revision 1.419.2.48
diff -u -d -r1.419.2.47 -r1.419.2.48
--- serendipity_functions.inc.php 11 Oct 2004 09:21:06 -0000 1.419.2.47
+++ serendipity_functions.inc.php 11 Oct 2004 22:32:47 -0000 1.419.2.48
@@ -1334,13 +1334,14 @@
if (!is_array($ca) || serendipity_db_bool($ca['allow_comments'])) {
$title = serendipity_db_escape_string(isset($commentInfo['title']) ? $commentInfo['title'] : '');
$comments = $commentInfo['comment'];
- $ip = $_SERVER['REMOTE_ADDR'];
+ $ip = isset($commentInfo['ip']) ? $commentInfo['ip'] : $_SERVER['REMOTE_ADDR'];
$commentsFixed = serendipity_db_escape_string($commentInfo['comment']);
$name = serendipity_db_escape_string($commentInfo['name']);
$url = serendipity_db_escape_string($commentInfo['url']);
$email = serendipity_db_escape_string($commentInfo['email']);
$parentid = (isset($commentInfo['parent_id']) && is_numeric($commentInfo['parent_id'])) ? $commentInfo['parent_id'] : 0;
- $status = (serendipity_db_bool($ca['moderate_comments'])) ? 'pending' : 'approved';
+ $status = isset($commentInfo['status']) ? $commentInfo['status'] : (serendipity_db_bool($ca['moderate_comments']) ? 'pending' : 'approved');
+ $t = isset($commentInfo['time']) ? $commentInfo['time'] : time();
if (isset($commentInfo['subscribe'])) {
$subscribe = 'true';
@@ -1348,7 +1349,6 @@
$subscribe = 'false';
}
- $t = time();
$query = "INSERT INTO {$serendipity['dbPrefix']}comments (entry_id, parent_id, ip, author, email, url, body, type, timestamp, title, subscribed, status)";
$query .= " VALUES ('$id', '$parentid', '$ip', '$name', '$email', '$url', '$commentsFixed', '$type', '$t', '$title', '$subscribe', '$status')";
@@ -2259,6 +2259,298 @@
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}references WHERE entry_id='$id'");
}
+/**
+* Import from Movable Type's format.
+**/
+function serendipity_importMT($contents, $force = false) {
+ global $serendipity;
+
+ if ( $serendipity['serendipityUserlevel'] != USERLEVEL_ADMIN ) {
+ return array(MUST_BE_ADMIN);
+ }
+
+ $authors = array();
+ $categories = serendipity_fetchCategories();
+ $tasks = array();
+
+ $entries = array();
+ $n = 0;
+ foreach ( preg_split('/\r?\n--------\r?\n/', $contents) as $entry ) {
+ if ( preg_match_all('/([A-Z ]+):( |\r?\n)(.*)\r?\n(-----)?/', $entry, $res) ) {
+ $entries[$n]['categories'] = array();
+ for ( $x=0 ; $x<sizeof($res[1]) ; $x++ ) {
+ switch ( $res[1][$x] ) {
+ case 'AUTHOR':
+ if ( !isset($authors[$res[3][$x]]) ) {
+ $au_inf = serendipity_fetchAuthor($res[3][$x]);
+ if ( !is_array($au_inf) ) {
+ $tasks[] = sprintf(CREATE_AUTHOR, htmlspecialchars($res[3][$x]));
+ $au_inf = serendipity_fetchAuthor($serendipity['authorid']);
+ }
+ $authors[$res[3][$x]] = $au_inf[0];
+ }
+ $entries[$n]['authorid'] = $authors[$res[3][$x]]['authorid'];
+ $entries[$n]['author'] = $authors[$res[3][$x]]['username'];
+ break;
+ case 'TITLE':
+ $entries[$n]['title'] = $res[3][$x];
+ break;
+ case 'STATUS':
+ $entries[$n]['isdraft'] = ($res[3][$x] == 'Publish') ? 'false' : 'true';
+ break;
+ case 'ALLOW COMMENTS':
+ $entries[$n]['allow_comments'] = ($res[3][$x] == '1') ? 'true' : 'false';
+ break;
+ case 'DATE':
+ $entries[$n]['timestamp'] = strtotime($res[3][$x]);
+ if ( $entries[$n]['timestamp'] == 0 ) {
+ $entries[$n]['timestamp'] = time();
+ }
+ break;
+ case 'BODY':
+ $entries[$n]['body'] = $res[3][$x];
+ break;
+ case 'EXTENDED BODY':
+ $entries[$n]['extended'] = $res[3][$x];
+ break;
+ case 'PRIMARY CATEGORY':
+ case 'CATEGORY':
+ $cat_found = false;
+ if ( is_array($categories) ) {
+ for ( $y=0 ; $y<sizeof($categories) ; $y++ ) {
+ if ( $categories[$y]['category_name'] == $res[3][$x] ) {
+ $cat_found = true;
+ break;
+ }
+ }
+ }
+ if ( $cat_found && !in_array($categories[$y]['categoryid'], $entries[$n]['categories']) ) {
+ $entries[$n]['categories'][] = $categories[$y]['categoryid'];
+ $entries[$n]['categories'][] = $categories[$y]['categoryid'];
+ }
+ else {
+ $tasks[] = sprintf(CREATE_CATEGORY, htmlspecialchars($res[3][$x]));
+ }
+ break;
+ }
+ }
+ }
+ $n++;
+ }
+
+ if ( !sizeof($tasks) || $force == true ) {
+ foreach ( $entries as $entry ) {
+ if ( !is_int($r = serendipity_updertEntry($entry)) ) {
+ echo '<div class="important">' . $r . '</div>';
+ }
+ }
+ return true;
+ }
+ else {
+ return array_unique($tasks);
+ }
+}
+
+/**
+* Import from a WordPress installation.
+**/
+function serendipity_importWP($wp) {
+ global $serendipity;
+
+ $wp['prefix'] = serendipity_db_escape_string($wp['prefix']);
+ $users = array();
+ $categories = array();
+ $entries = array();
+
+ if ( $serendipity['serendipityUserlevel'] < USERLEVEL_ADMIN ) {
+ return MUST_BE_ADMIN;
+ }
+
+ if ( !extension_loaded('mysql') ) {
+ return 'You must have the MySQL extension in order to access the WordPress database.';
+ }
+
+ $wpdb = @mysql_connect($wp['host'], $wp['user'], $wp['pass']);
+ if ( !$wpdb ) {
+ return 'Could not connect to MySQL database: ' . mysql_error($wpdb);
+ }
+
+ if ( !@mysql_select_db($wp['name']) ) {
+ return 'Could not select database: ' . mysql_error($wpdb);
+ }
+
+ /* Users */
+ $res = @mysql_query("SELECT ID, user_login, user_pass, user_email, user_level FROM {$wp['prefix']}users;", $wpdb);
+ if ( !$res ) {
+ return 'Could not select user information: ' . mysql_error($wpdb);
+ }
+
+ for ( $x=0 ; $x<mysql_num_rows($res) ; $x++ ) {
+ $users[$x] = mysql_fetch_assoc($res);
+
+ $data = array('right_publish' => ($users[$x]['user_level'] >= 1) ? 1 : 0,
+ 'username' => $users[$x]['user_login'],
+ 'password' => $users[$x]['user_pass']); // WP uses md5, too.
+
+ if ( $users[$x]['user_level'] <= 1 ) {
+ $data['userlevel'] = USERLEVEL_EDITOR;
+ } elseif ( $users[$x]['user_level'] < 5 ) {
+ $data['userlevel'] = USERLEVEL_CHIEF;
+ } else {
+ $data['userlevel'] = USERLEVEL_ADMIN;
+ }
+
+ serendipity_db_insert('authors', $data);
+ $users[$x]['authorid'] = serendipity_db_insert_id('authors', 'authorid');
+ }
+
+ /* Categories */
+ $res = @mysql_query("SELECT cat_ID, cat_name, category_description, category_parent FROM {$wp['prefix']}categories ORDER BY category_parent, cat_ID;", $wpdb);
+ if ( !$res ) {
+ return 'Could not select category information: ' . mysql_error($wpdb);
+ }
+
+ // 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 ; $x<sizeof($categories) ; $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', $cat);
+ $categories[$x]['categoryid'] = serendipity_db_insert_id('category', 'categoryid');
+ }
+
+ // There has to be a more efficient way of doing this...
+ 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']};");
+ } // else { echo "D'oh! " . random_string_of_profanity(); }
+ }
+ }
+
+ serendipity_rebuildCategoryTree();
+
+ /* Entries */
+ $res = @mysql_query("SELECT * FROM {$wp['prefix']}posts ORDER BY post_date;", $wpdb);
+ if ( !$res ) {
+ return 'Could not select entry information: ' . mysql_error($wpdb);
+ }
+
+ for ( $x=0 ; $x<mysql_num_rows($res) ; $x++ ) {
+ $entries[$x] = mysql_fetch_assoc($res);
+
+ $entry = array('title' => $entries[$x]['post_title'],
+ 'isdraft' => ($entries[$x]['post_status'] == 'publish') ? 'false' : 'true',
+ 'allow_comments' => ($entries[$x]['comment_status '] == 'open ') ? 'true' : 'false',
+ 'timestamp' => strtotime($entries[$x]['post_date']),
+ 'body' => $entries[$x]['post_content']);
+
+ foreach ( $users as $user ) {
+ if ( $user['ID'] == $entries[$x]['post_author'] ) {
+ $entry['authorid'] = $user['authorid'];
+ break;
+ }
+ }
+
+ if ( !is_int($entries[$x]['entryid'] = serendipity_updertEntry($entry)) ) {
+ return $entries[$x]['entryid'];
+ }
+ }
+
+ /* Entry/category */
+ $res = @mysql_query("SELECT * FROM {$wp['prefix']}post2cat;", $wpdb);
+ if ( !$res ) {
+ return 'Could not select entry/category information: ' . mysql_error($wpdb);
+ }
+
+ while ( $a = mysql_fetch_assoc($res) ) {
+ foreach ( $categories as $category ) {
+ if ( $category['cat_ID'] == $a['category_id'] ) {
+ foreach ( $entries as $entry ) {
+ if ( $a['post_id'] == $entry['ID'] ) {
+ $data = array('entryid' => $entry['entryid'],
+ 'categoryid' => $category['categoryid']);
+ serendipity_db_insert('entrycat', $data);
+ break;
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ /* Comments */
+ $res = @mysql_query("SELECT * FROM {$wp['prefix']}comments;", $wpdb);
+ if ( !$res ) {
+ return 'Could not select comment information: ' . mysql_error($wpdb);
+ }
+
+ while ( $a = mysql_fetch_assoc($res) ) {
+ foreach ( $entries as $entry ) {
+ if ( $entry['ID'] == $a['comment_post_ID'] ) {
+ $comment = array('entry_id ' => $entry['entryid'],
+ 'parent_id' => 0,
+ 'timestamp' => strtotime($a['comment_date']),
+ 'author' => $a['comment_author'],
+ 'email' => $a['comment_author_email'],
+ 'url' => $a['comment_author_url'],
+ 'ip' => $a['comment_author_IP'],
+ 'status' => 'approved',
+ 'body' => $a['comment_content'],
+ 'type' => 'NORMAL');
+
+ serendipity_db_insert('comments', $comment);
+ serendipity_approveComment($cid, $id, true);
+ }
+ }
+ }
+
+ // That was fun.
+ return false;
+}
+
+/**
+* Returns the maximum file upload size, in bytes (0 if none).
+**/
+function serendipity_maxfileuploadsize() {
+ if ( $val = trim(ini_get('upload_max_filesize')) == '' )
+ return 0;
+
+ switch(substr($val, -1)) {
+ case 'k':
+ case 'K':
+ return (int) $val * 1024;
+ break;
+ case 'm':
+ case 'M':
+ return (int) $val * 1048576;
+ break;
+ default:
+ return $val;
+ }
+}
+
+function serendipity_fetchAuthor($author) {
+ global $serendipity;
+
+ return serendipity_db_query("SELECT * FROM {$serendipity['dbPrefix']}authors WHERE " . (is_numeric($author) ? "authorid={$author};" : "username='{$author}';"));
+}
/**
* Split up a filename
Index: serendipity_admin_interop.inc.php
===================================================================
RCS file: /cvsroot/php-blog/serendipity/serendipity_admin_interop.inc.php,v
retrieving revision 1.7.2.1
retrieving revision 1.7.2.2
diff -u -d -r1.7.2.1 -r1.7.2.2
--- serendipity_admin_interop.inc.php 27 Sep 2004 10:13:39 -0000 1.7.2.1
+++ serendipity_admin_interop.inc.php 11 Oct 2004 22:32:47 -0000 1.7.2.2
@@ -18,11 +18,29 @@
<a href="<?php echo $serendipity['serendipityHTTPPath']; ?>rss.php?version=2.0&all=1"><?php echo VIEW_FEED_EXPORT; ?></a>
<?php
exit;
+} else if (isset($serendipity['POST']['import_mt'])) {
+ $tasks = serendipity_importMT(file_get_contents($_FILES['serendipity']['tmp_name']['mt_dat']), isset($serendipity['POST']['mt_force']));
+
+ if ( is_array($tasks) ) {
+ echo MAX_COMPAT_OLD_BLOG . "\n<ul>\n";
+ foreach ( array_unique($tasks) as $task ) {
+ echo " <li>$task</li>\n";
+ }
+ echo "</ul>\n" . IMPORT_AGAIN_FORCE;
+ }
+ else {
+ echo ENTRIES_SUCCESSFULLY_INSERTED . "\n<br />\n";
+ }
+} else if (isset($serendipity['POST']['wp']['submit'])) {
+ if ( $wp_err = serendipity_importWP($serendipity['POST']['wp']) ) {
+ echo '<div class="serendipity_msg_important">' . var_dump($wp_err) . '</div>';
+ }
}
+
?>
<p>
- <h3><?php echo IMPORT; ?></h3>
+ <h3><?php echo IMPORT; ?> RSS</h3>
<form method="POST">
<div>
@@ -49,6 +67,57 @@
</p>
<p>
+ <h3><?php echo IMPORT . ' ' . MT_DATA_FILE; ?></h3>
+
+ <form method="post" enctype="multipart/form-data">
+<?php if (serendipity_maxFileUploadSize()) { ?>
+ <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo serendipity_maxFileUploadSize(); ?>" />
+<?php } ?>
+ <div>
+ <label for="serendipity[mt_dat]"><?php echo MT_DATA_FILE; ?></label>
+ <input id="serendipity[mt_dat]" type="file" name="serendipity[mt_dat]" size="30" />
+ </div>
+ <div>
+ <label for="serendipity[mt_force]"><?php echo FORCE; ?></label>
+ <input id="serendipity[mt_force]" type="checkbox" name="serendipity[mt_force]" size="30" />
+ </div>
+ <div>
+ <input type="submit" name="serendipity[import_mt]" value="<?php echo IMPORT . ' ' . MT_DATA_FILE; ?>" />
+ </div>
+ </form>
+</p>
+
+<p>
+ <h3><?php echo IMPORT . ' ' . WORDPRESS_DATA; ?></h3>
+
+ <form method="post">
+ <div>
+ <label for="serendipity_wp_host"><?php echo INSTALL_DBHOST; ?>:</label>
+ <input type="text" name="serendipity[wp][host]" value="<?php echo isset($serendipity['POST']['wp']['host']) ? $serendipity['POST']['wp']['host'] : $serendipity['dbHost']; ?>" id="serendipity_wp_host"/>
+ </div>
+ <div>
+ <label for="serendipity_wp_user"><?php echo INSTALL_DBUSER; ?>:</label>
+ <input type="text" name="serendipity[wp][user]" value="<?php echo isset($serendipity['POST']['wp']['user']) ? $serendipity['POST']['wp']['user'] : $serendipity['dbUser']; ?>" id="serendipity_wp_user"/>
+ </div>
+ <div>
+ <label for="serendipity_wp_pass"><?php echo INSTALL_DBPASS; ?>:</label>
+ <input type="password" name="serendipity[wp][pass]" <?php echo isset($serendipity['POST']['wp']['pass']) ? 'value="' . $serendipity['POST']['wp']['pass'] . '" ' : ($serendipity['serendipityUserlevel'] >= USERLEVEL_ADMIN ? 'value="' . $serendipity['dbPass'] . '" ' : '') ?>id="serendipity_wp_pass"/>
+ </div>
+ <div>
+ <label for="serendipity_wp_name"><?php echo INSTALL_DBNAME; ?>:</label>
+ <input type="text" name="serendipity[wp][name]" value="<?php echo isset($serendipity['POST']['wp']['name']) ? $serendipity['POST']['wp']['name'] : $serendipity['dbName']; ?>" id="serendipity_wp_name"/>
+ </div>
+ <div>
+ <label for="serendipity_wp_prefix"><?php echo INSTALL_DBPREFIX; ?>:</label>
+ <input type="text" name="serendipity[wp][prefix]" value="<?php echo isset($serendipity['POST']['wp']['prefix']) ? $serendipity['POST']['wp']['prefix'] : 'wp_'; ?>" id="serendipity_wp_prefix"/>
+ </div>
+ <div>
+ <input type="submit" name="serendipity[wp][submit]" value="<?php echo IMPORT . ' ' . WORDPRESS_DATA; ?>" />
+ </div>
+ </form>
+</p>
+
+<p>
<h3><?php echo EXPORT; ?></h3>
<form method="POST">
Index: NEWS
===================================================================
RCS file: /cvsroot/php-blog/serendipity/NEWS,v
retrieving revision 1.214.2.30
retrieving revision 1.214.2.31
diff -u -d -r1.214.2.30 -r1.214.2.31
--- NEWS 11 Oct 2004 09:21:06 -0000 1.214.2.30
+++ NEWS 11 Oct 2004 22:32:46 -0000 1.214.2.31
@@ -3,6 +3,9 @@
Version 0.8 ()
------------------------------------------------------------------------
+ * Added import tool for Movable Type data files and WordPress
+ databases. (tadpole9)
+
* Syndication plugin offers to show full feed including extended
entry (garvinhicking)
|