Source for file mvblog_import_wordpress.php

Documentation is available at mvblog_import_wordpress.php

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

Documentation generated on Fri, 28 Dec 2007 13:17:40 +0100 by phpDocumentor 1.4.1