Changeset 723 for team

Show
Ignore:
Timestamp:
12/09/07 16:22:53 (13 months ago)
Author:
michiel
Message:

import now copies wp-upload user uploaded stuff and replaces the links to them in the content.
Re #37

Location:
team/michiel/import_blogs/common
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • team/michiel/import_blogs/common/mvblog_common.php

    r683 r723  
    806806        } 
    807807        /* }}} */ 
    808  
     808        /* filesystem methods */ 
     809        /* copy_files {{{ */ 
     810        /** 
     811         * Copies files and directories recursive to dirs under site_images 
     812         * 
     813         * @param string $src The sourcefile or dir 
     814         * @param string $dst The dir inside site_images where the files/folders should be copied to 
     815         * @param int $overwrite_dest if set will overwrite the destination if it already exists 
     816         */ 
     817        public function copy_files($srcbase, $src, $dest, $overwrite_dest = 0) { 
     818                // calculate where the site_images dir is 
     819                $basepath = realpath(dirname(__FILE__)."/../site_images/"); 
     820                $dst = sprintf("%s/%s", $basepath, $dest); 
     821                //check if the dst dir is there 
     822                if (!is_dir($dst)) 
     823                        if (!mkdir($dst)) 
     824                                die("could not create directory $dst"); 
     825                if ($handle = opendir(sprintf("%s/%s", $srcbase, $src))) { //open source directory 
     826                        while (false !== ($file = readdir($handle))) { //loop through all items in the opened source dir 
     827                                if ($file != "." && $file != "..") { //we wont copy the special 'current dir' and 'one dir up' items 
     828                                        if ($src) 
     829                                                $path = sprintf("%s/%s", $src, $file); 
     830                                        else 
     831                                                $path = $file; 
     832                                                $s = sprintf("%s/%s", $srcbase, $path); 
     833                                                $d = sprintf("%s/%s", $dst, $path); 
     834                                        if (is_file($s)) { 
     835                                                if (is_file($d) || $overwrite_dest) { 
     836                                                        if (!copy($s, $d)) 
     837                                                                echo "Something went wrong while trying to copy $s to $d<br>"; 
     838                                                } 
     839                                        } elseif (is_dir($s)) { 
     840                                                if (!is_dir($d)) 
     841                                                        if (!mkdir($d)) 
     842                                                                echo "Something went wrong while trying to create dir $d<br>\n"; 
     843                                                $this->copy_files($srcbase, $path, $dest, 1); 
     844                                        } 
     845                                } 
     846                        } 
     847                        closedir($handle); 
     848                } 
     849        } 
     850        /* }}} */ 
    809851        /* output methods */ 
    810852        /* html_header: {{{ */ 
  • team/michiel/import_blogs/common/mvblog_import_wordpress.php

    r569 r723  
    5656                echo "</tr><tr>\n"; 
    5757                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"; 
    5862                echo "</tr></table>\n"; 
    5963                echo "<input type=\"submit\" value=\"".gettext("import")."\" />\n"; 
    6064                echo "</form>\n"; 
    6165        } 
     66 
    6267        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); 
    6370                $category = array(); 
    6471                // find out if we have enough info for a database connection 
     
    7279                $db = mysql_connect($data["dbserver"], $data["dbuser"], $data["dbpass"]); 
    7380                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"); 
    7487                //import categories and maintain array with oldid=>newid so we can link the articles to the correct ids 
    75                 $sql = sprintf("SELECT cat_ID, cat_name, category_description FROM %swp_categories", $data["dbprefix"]); 
    76                 $res = mysql_query($sql); 
     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()); 
    7796                while ($row = mysql_fetch_assoc($res)) { 
    78                         $cat_sql = sprintf("INSERT INTO categories (name, `desc`, public, active) VALUES ('%s', '%s', 1, 1)", 
    79                                 $row["cat_name"], $row["category_description"]); 
     97                        $cat_sql = sprintf("INSERT INTO categories (name, `desc`, public, active) VALUES ('%1\$s', '%1\$s', 1, 1)", 
     98                                $row["name"]); 
    8099                        $r = $GLOBALS["admin"]->db->query($cat_sql); 
    81                         $category[$row["cat_ID"]] = $GLOBALS["admin"]->db->lastInsertID("categories", "id"); 
     100                        $category[$row["term_taxonomy_id"]] = $GLOBALS["admin"]->db->lastInsertID("categories", "id"); 
    82101                } 
    83102                //select all articles 
    84                 $sql = sprintf("SELECT * FROM %swp_posts WHERE post_type='post'", $data["dbprefix"]); 
     103                $sql = sprintf("SELECT * FROM %sposts WHERE post_type='post'", $data["dbprefix"]); 
    85104                $res = mysql_query($sql); 
    86105                while ($row = mysql_fetch_assoc($res)) { 
     
    98117                                $last_modified = 0; 
    99118                        //get categories 
    100                         $q = sprintf("SELECT category_id FROM %swp_post2cat WHERE post_id = %d", $data["dbprefix"], $row["ID"]); 
     119                        $q = sprintf("SELECT term_taxonomy_id FROM %sterm_relationships WHERE object_id=%d", $data["dbprefix"], $row["ID"]); 
    101120                        $r = mysql_query($q); 
    102121                        $categories = array(); 
    103122                        while ($catr = mysql_fetch_assoc($r)) { 
    104                                 $categories[] = $category[$catr["category_id"]]; 
     123                                $categories[] = $category[$catr["term_taxonomy_id"]]; 
    105124                        } 
    106                         $data["oldsite"] = "http://www.electricmonk.nl/log/"; 
     125                        $data["oldsite"] = $data["orig_url"]; 
    107126                        if (array_key_exists("oldsite", $data) && $data["oldsite"]) { 
    108                                 //find all locally hosted images and put them into mvblog space 
     127                                $wpuploadurl = sprintf("%s%s", $data["oldsite"], "wp-content/uploads/"); 
    109128                                //find all links pointing to internal links and replace them with the new structure 
    110                                 preg_match_all("/<[(img)|a][^>]*[.]*>/si", $row["post_content"], $matches); 
     129                                preg_match_all("/<[img|a][^>]*[.]*>/si", $row["post_content"], $matches); 
    111130                                foreach ($matches[0] as $match) { 
    112                                         //get the src or href attribute 
     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 
    113141                                        preg_match_all("/src=[\"|\'](.+?)[\"|\']/si", $match, $m); 
    114                                         print_r($match); 
    115                                         print_r($m); 
    116                                         preg_match_all("/href=[\"|\'](.+?)[\"|\']/si", $match, $m); 
    117                                         print_r($m); 
    118                                         echo "======================\n"; 
     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                                        } 
    119148                                } 
    120149                        } 
     
    127156                        } 
    128157                } 
     158                echo "All done !"; 
    129159        } 
    130160}