[Mvblog-commits] r723 - team/michiel/import_blogs/common
mvblog-commits at lists.three-dimensional.net
mvblog-commits at lists.three-dimensional.net
Sun Dec 9 16:22:54 CET 2007
Author: michiel
Date: 2007-12-09 16:22:53 +0100 (Sun, 09 Dec 2007)
New Revision: 723
Modified:
team/michiel/import_blogs/common/mvblog_common.php
team/michiel/import_blogs/common/mvblog_import_wordpress.php
Log:
import now copies wp-upload user uploaded stuff and replaces the links to them in the content.
Re #37
Modified: team/michiel/import_blogs/common/mvblog_common.php
===================================================================
--- team/michiel/import_blogs/common/mvblog_common.php 2007-12-09 12:14:23 UTC (rev 722)
+++ team/michiel/import_blogs/common/mvblog_common.php 2007-12-09 15:22:53 UTC (rev 723)
@@ -805,7 +805,49 @@
return $data;
}
/* }}} */
-
+ /* filesystem methods */
+ /* copy_files {{{ */
+ /**
+ * Copies files and directories recursive to dirs under site_images
+ *
+ * @param string $src The sourcefile or dir
+ * @param string $dst The dir inside site_images where the files/folders should be copied to
+ * @param int $overwrite_dest if set will overwrite the destination if it already exists
+ */
+ public function copy_files($srcbase, $src, $dest, $overwrite_dest = 0) {
+ // calculate where the site_images dir is
+ $basepath = realpath(dirname(__FILE__)."/../site_images/");
+ $dst = sprintf("%s/%s", $basepath, $dest);
+ //check if the dst dir is there
+ if (!is_dir($dst))
+ if (!mkdir($dst))
+ die("could not create directory $dst");
+ if ($handle = opendir(sprintf("%s/%s", $srcbase, $src))) { //open source directory
+ while (false !== ($file = readdir($handle))) { //loop through all items in the opened source dir
+ if ($file != "." && $file != "..") { //we wont copy the special 'current dir' and 'one dir up' items
+ if ($src)
+ $path = sprintf("%s/%s", $src, $file);
+ else
+ $path = $file;
+ $s = sprintf("%s/%s", $srcbase, $path);
+ $d = sprintf("%s/%s", $dst, $path);
+ if (is_file($s)) {
+ if (is_file($d) || $overwrite_dest) {
+ if (!copy($s, $d))
+ echo "Something went wrong while trying to copy $s to $d<br>";
+ }
+ } elseif (is_dir($s)) {
+ if (!is_dir($d))
+ if (!mkdir($d))
+ echo "Something went wrong while trying to create dir $d<br>\n";
+ $this->copy_files($srcbase, $path, $dest, 1);
+ }
+ }
+ }
+ closedir($handle);
+ }
+ }
+ /* }}} */
/* output methods */
/* html_header: {{{ */
/**
Modified: team/michiel/import_blogs/common/mvblog_import_wordpress.php
===================================================================
--- team/michiel/import_blogs/common/mvblog_import_wordpress.php 2007-12-09 12:14:23 UTC (rev 722)
+++ team/michiel/import_blogs/common/mvblog_import_wordpress.php 2007-12-09 15:22:53 UTC (rev 723)
@@ -55,11 +55,18 @@
echo "<td>WordPress Database password:</td><td><input type=\"password\" style=\"width: 200px;\" name=\"dbpass\" /></td>\n";
echo "</tr><tr>\n";
echo "<td>WordPress Table prefix:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"dbprefix\" /></td>\n";
+ echo "</tr><tr>\n";
+ echo "<td>WordPress Base URL:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"orig_url\" /></td>\n";
+ echo "</tr><tr>\n";
+ echo "<td>WordPress Filesystem Location:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"fs_location\" /></td>\n";
echo "</tr></table>\n";
echo "<input type=\"submit\" value=\"".gettext("import")."\" />\n";
echo "</form>\n";
}
+
public function run_import($data) {
+ // first try to copy all files
+ $this->copy_files($data["fs_location"]."wp-content/uploads", "", "wp-uploads", 1);
$category = array();
// find out if we have enough info for a database connection
if (!array_key_exists("dbserver", $data) || $data["dbserver"] == "" ||
@@ -71,17 +78,29 @@
}
$db = mysql_connect($data["dbserver"], $data["dbuser"], $data["dbpass"]);
mysql_select_db($data["dbname"], $db);
+ //grab WP db version as we only support 2.3 and newer
+ $sql = sprintf("SELECT option_value FROM %soptions WHERE option_name='db_version'", $data["dbprefix"]);
+ $res = mysql_query($sql);
+ $wpdbversion = mysql_result($res, 0);
+ if ($wpdbversion < 5539)
+ die("You need at least WP 2.3.0");
//import categories and maintain array with oldid=>newid so we can link the articles to the correct ids
- $sql = sprintf("SELECT cat_ID, cat_name, category_description FROM %swp_categories", $data["dbprefix"]);
- $res = mysql_query($sql);
+ /*
+ * WP categories are stored in the table <prefix>terms
+ * The table <prefix>term_taxonomy holds a count for them
+ * The table <prefix>term_relationships links articles and term_taxonomy records together
+ * <posts> -> <term_relationships> -> <term_taxonomy> -> <terms>
+ */
+ $sql = sprintf("SELECT %1\$sterms.term_id, %1\$sterms.name, %1\$sterm_taxonomy.term_taxonomy_id from %1\$sterms LEFT JOIN %1\$sterm_taxonomy ON %1\$sterm_taxonomy.term_id = %1\$sterms.term_id", $data["dbprefix"]);
+ $res = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($res)) {
- $cat_sql = sprintf("INSERT INTO categories (name, `desc`, public, active) VALUES ('%s', '%s', 1, 1)",
- $row["cat_name"], $row["category_description"]);
+ $cat_sql = sprintf("INSERT INTO categories (name, `desc`, public, active) VALUES ('%1\$s', '%1\$s', 1, 1)",
+ $row["name"]);
$r = $GLOBALS["admin"]->db->query($cat_sql);
- $category[$row["cat_ID"]] = $GLOBALS["admin"]->db->lastInsertID("categories", "id");
+ $category[$row["term_taxonomy_id"]] = $GLOBALS["admin"]->db->lastInsertID("categories", "id");
}
//select all articles
- $sql = sprintf("SELECT * FROM %swp_posts WHERE post_type='post'", $data["dbprefix"]);
+ $sql = sprintf("SELECT * FROM %sposts WHERE post_type='post'", $data["dbprefix"]);
$res = mysql_query($sql);
while ($row = mysql_fetch_assoc($res)) {
if ($row["comment_status"] == "open")
@@ -97,25 +116,35 @@
else
$last_modified = 0;
//get categories
- $q = sprintf("SELECT category_id FROM %swp_post2cat WHERE post_id = %d", $data["dbprefix"], $row["ID"]);
+ $q = sprintf("SELECT term_taxonomy_id FROM %sterm_relationships WHERE object_id=%d", $data["dbprefix"], $row["ID"]);
$r = mysql_query($q);
$categories = array();
while ($catr = mysql_fetch_assoc($r)) {
- $categories[] = $category[$catr["category_id"]];
+ $categories[] = $category[$catr["term_taxonomy_id"]];
}
- $data["oldsite"] = "http://www.electricmonk.nl/log/";
+ $data["oldsite"] = $data["orig_url"];
if (array_key_exists("oldsite", $data) && $data["oldsite"]) {
- //find all locally hosted images and put them into mvblog space
+ $wpuploadurl = sprintf("%s%s", $data["oldsite"], "wp-content/uploads/");
//find all links pointing to internal links and replace them with the new structure
- preg_match_all("/<[(img)|a][^>]*[.]*>/si", $row["post_content"], $matches);
+ preg_match_all("/<[img|a][^>]*[.]*>/si", $row["post_content"], $matches);
foreach ($matches[0] as $match) {
- //get the src or href attribute
+ //get the href attribute
+ preg_match_all("/href=[\"|\'](.+?)[\"|\']/si", $match, $m);
+ $filtermatch = $m[1];
+ foreach ($filtermatch as $v) {
+ if (strstr($v, $data["oldsite"]) && strstr($v, $wpuploadurl)) {
+ $row["post_content"] = str_replace($v, "site_images/wp-uploads/".str_replace($wpuploadurl, "", $v), $row["post_content"]);
+ }
+ }
+ unset($m);
+ //get the src attribute
preg_match_all("/src=[\"|\'](.+?)[\"|\']/si", $match, $m);
- print_r($match);
- print_r($m);
- preg_match_all("/href=[\"|\'](.+?)[\"|\']/si", $match, $m);
- print_r($m);
- echo "======================\n";
+ $filtermatch = $m[1];
+ foreach ($filtermatch as $v) {
+ if (strstr($v, $data["oldsite"]) && strstr($v, $wpuploadurl)) {
+ $row["post_content"] = str_replace($v, "site_images/wp-uploads/".str_replace($wpuploadurl, "", $v), $row["post_content"]);
+ }
+ }
}
}
$post_sql = sprintf("INSERT INTO articles (date, title, head, body, authors_id, allowanoncomments, active, public, last_modified, modified_by, categories_ids) VALUES (%d, '%s', '%s', '%s', %d, %d, 1, %d, %d, %d, '%s')",
@@ -126,6 +155,7 @@
die($r->getDebugInfo());
}
}
+ echo "All done !";
}
}
?>
More information about the Mvblog-commits
mailing list