root/trunk/common/mvblog_import_wordpress.php

Revision 776, 7.1 KB (checked in by michiel, 9 months ago)

update copyright year.

Closes #180

Line 
1<?php
2/**
3 * MvBlog -- An open source no-nosense blogtool
4 *
5 * Copyright (C) 2005-2008, Michiel van Baak
6 * Michiel van Baak <mvanbaak@users.sourceforge.net>
7 *
8 * See http://dev.mvblog.org for more information on MvBlog.
9 * That page also provides Bugtrackers, Filereleases etc.
10 *
11 * This program is free software, distributed under the terms of
12 * the GNU General Public License Version 2. See the LICENSE file
13 * at the top of the source tree.
14 *
15 * @package MvBlog
16 * @author Michiel van Baak
17 * @version %%VERSION%%
18 * @copyright 2005-2008 Michiel van Baak
19 */
20
21/**
22 * Class that holds methods for WordPress import
23 * @package MvBlog
24 */
25Class MvBlog_import_wordpress extends MvBlog_common {
26    public function __construct($options) {
27        if (!array_key_exists("importaction", $options)) {
28            $this->show_main();
29        } else {
30            switch ($options["importaction"]) {
31            case "run_import" :
32                $this->run_import($_REQUEST);
33                break;
34            default:
35                echo "nothing to do";
36                break;
37            }
38        }
39    }
40    public function show_main() {
41        echo "<p>This importer allows you to import WordPress data into MvBlog.<br />";
42        echo "Your Mileage may vary.<br /><br /></p>\n";
43        echo "<p>Your WordPress database settings<br />\n";
44        echo "<form name=\"wp_import\" action=\"index.php\" method=\"post\">\n";
45        echo "<input type=\"hidden\" name=\"action\" value=\"import\" />\n";
46        echo "<input type=\"hidden\" name=\"type\" value=\"wordpress\" />\n";
47        echo "<input type=\"hidden\" name=\"importaction\" value=\"run_import\" />\n";
48        echo "<table border=\"0\"><tr>\n";
49        echo "<td>WordPress Database server:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"dbserver\" /></td>\n";
50        echo "</tr><tr>\n";
51        echo "<td>WordPress Database name:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"dbname\" /></td>\n";
52        echo "</tr><tr>\n";
53        echo "<td>WordPress Database user:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"dbuser\" /></td>\n";
54        echo "</tr><tr>\n";
55        echo "<td>WordPress Database password:</td><td><input type=\"password\" style=\"width: 200px;\" name=\"dbpass\" /></td>\n";
56        echo "</tr><tr>\n";
57        echo "<td>WordPress Table prefix:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"dbprefix\" /></td>\n";
58        echo "</tr><tr>\n";
59        echo "<td>WordPress Base URL:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"orig_url\" /></td>\n";
60        echo "</tr><tr>\n";
61        echo "<td>WordPress Filesystem Location:</td><td><input type=\"text\" style=\"width: 200px;\" name=\"fs_location\" /></td>\n";
62        echo "</tr></table>\n";
63        echo "<input type=\"submit\" value=\"".gettext("import")."\" />\n";
64        echo "</form>\n";
65    }
66
67    public function run_import($data) {
68        // first try to copy all files
69        $this->copy_files($data["fs_location"]."wp-content/uploads", "", "wp-uploads", 1);
70        $category = array();
71        // find out if we have enough info for a database connection
72        if (!array_key_exists("dbserver", $data) || $data["dbserver"] == "" ||
73            !array_key_exists("dbname", $data) || $data["dbname"] == "" ||
74            !array_key_exists("dbuser", $data) || $data["dbuser"] == "" ||
75            !array_key_exists("dbpass", $data) || $data["dbpass"] == "") {
76            echo "not enough information.";
77            return false;
78        }
79        $db = mysql_connect($data["dbserver"], $data["dbuser"], $data["dbpass"]);
80        mysql_select_db($data["dbname"], $db);
81        //grab WP db version as we only support 2.3 and newer
82        $sql = sprintf("SELECT option_value FROM %soptions WHERE option_name='db_version'", $data["dbprefix"]);
83        $res = mysql_query($sql);
84        $wpdbversion = mysql_result($res, 0);
85        if ($wpdbversion < 5539)
86            die("You need at least WP 2.3.0");
87        //import categories and maintain array with oldid=>newid so we can link the articles to the correct ids
88        /*
89         * WP categories are stored in the table <prefix>terms
90         * The table <prefix>term_taxonomy holds a count for them
91         * The table <prefix>term_relationships links articles and term_taxonomy records together
92         * <posts> -> <term_relationships> -> <term_taxonomy> -> <terms>
93         */
94        $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"]);
95        $res = mysql_query($sql) or die(mysql_error());
96        while ($row = mysql_fetch_assoc($res)) {
97            $cat_sql = sprintf("INSERT INTO categories (name, `desc`, public, active) VALUES ('%1\$s', '%1\$s', 1, 1)",
98                $row["name"]);
99            $r = $GLOBALS["admin"]->db->query($cat_sql);
100            $category[$row["term_taxonomy_id"]] = $GLOBALS["admin"]->db->lastInsertID("categories", "id");
101        }
102        //select all articles
103        $sql = sprintf("SELECT * FROM %sposts WHERE post_type='post'", $data["dbprefix"]);
104        $res = mysql_query($sql);
105        while ($row = mysql_fetch_assoc($res)) {
106            if ($row["comment_status"] == "open")
107                $allow_anon_comment = 1;
108            else
109                $allow_anon_comment = 0;
110            if ($row["post_status"] == "publish" || $row["post_status"] == "static")
111                $public = 1;
112            else
113                $public = 0;
114            if (strtotime($row["post_modified"]))
115                $last_modified = strtotime($row["post_modified"]);
116            else
117                $last_modified = 0;
118            //get categories
119            $q = sprintf("SELECT term_taxonomy_id FROM %sterm_relationships WHERE object_id=%d", $data["dbprefix"], $row["ID"]);
120            $r = mysql_query($q);
121            $categories = array();
122            while ($catr = mysql_fetch_assoc($r)) {
123                $categories[] = $category[$catr["term_taxonomy_id"]];
124            }
125            $data["oldsite"] = $data["orig_url"];
126            if (array_key_exists("oldsite", $data) && $data["oldsite"]) {
127                $wpuploadurl = sprintf("%s%s", $data["oldsite"], "wp-content/uploads/");
128                //find all links pointing to internal links and replace them with the new structure
129                preg_match_all("/<[img|a][^>]*[.]*>/si", $row["post_content"], $matches);
130                foreach ($matches[0] as $match) {
131                    //get the href attribute
132                    preg_match_all("/href=[\"|\'](.+?)[\"|\']/si", $match, $m);
133                    $filtermatch = $m[1];
134                    foreach ($filtermatch as $v) {
135                        if (strstr($v, $data["oldsite"]) && strstr($v, $wpuploadurl)) {
136                            $row["post_content"] = str_replace($v, "site_images/wp-uploads/".str_replace($wpuploadurl, "", $v), $row["post_content"]);
137                        }
138                    }
139                    unset($m);
140                    //get the src attribute
141                    preg_match_all("/src=[\"|\'](.+?)[\"|\']/si", $match, $m);
142                    $filtermatch = $m[1];
143                    foreach ($filtermatch as $v) {
144                        if (strstr($v, $data["oldsite"]) && strstr($v, $wpuploadurl)) {
145                            $row["post_content"] = str_replace($v, "site_images/wp-uploads/".str_replace($wpuploadurl, "", $v), $row["post_content"]);
146                        }
147                    }
148                }
149            }
150            $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')",
151                strtotime($row["post_date"]), addslashes($row["post_title"]), addslashes($row["post_excerpt"]), addslashes($row["post_content"]), $_SESSION["author_id"],
152                $allow_anon_comment, $public, $last_modified, $_SESSION["author_id"], implode(",", $categories));
153            $r = $GLOBALS["admin"]->db->query($post_sql);
154            if (PEAR::isError($r)) {
155                die($r->getDebugInfo());
156            }
157        }
158        echo "All done !";
159    }
160}
161?>
Note: See TracBrowser for help on using the browser.