checkRight('module_admin', $myModule->module_id, $xoopsUser->getGroups())) die( 'only admin can access this area' ) ; include(XOOPS_ROOT_PATH.'/header.php'); require_once dirname(__FILE__).'/include/comment_functions.php'; include_once XOOPS_ROOT_PATH.'/include/comment_constants.php'; $step = preg_replace( '/[^a-zA-Z0-9_-]/' , '' , @$_POST['step'] ); if (!$step) $step = 0; switch($step) { case 0: ?>

こんにちは! あなたの Movable Type 記事の全てを d3blogへインポートします。
  (カテゴリは、1つしか読み込まれません。)
 その前に、 (import.php) を開いて、どのファイルから読み込むか1行編集してください。

define('MTEXPORT', '');

これを、下のように(但し実際のファイルのフルパスに)編集します。

define('MTEXPORT', '/full/path/to/your/import.txt');

この作業は、セキュリティの観点から手動で行う必要があります。

これが済めば、インポート作業の準備は完了です。  インポート作業は、少々時間がかかる場合があります。 :)

また、インポートする記事毎に、ユーザー名が一致するかチェックを行い、該当ユーザー名がDBに無い時はインポートを行いませんので、ご注意を。 一旦取り込んだ記事やコメント・トラックバックと重複データの場合は、インポートをスキップして次の記事のインポートを継続します。


module; $xoopsModuleConfig =& $myModule->module_config; $comment_handler =& xoops_gethandler( 'comment' ) ; $tablecomments = $comment_handler->getObjects(); $entry_handler =& $myModule->getHandler('entry'); $tableposts = $entry_handler->getObjects(); $category_handler =& $myModule->getHandler('category'); $tablecategories = $category_handler->getObjects(); $tb_handler =& $myModule->getHandler('trackback'); $tabletb = $entry_handler->getObjects(); $comment_config = $xoopsModule->getInfo('comments'); $com_modid = $xoopsModule->getVar('mid'); //var_dump($comment_config); //var_dump($com_modid); $moddir = $xoopsModule->getVar('dirname'); $db =& Database::getInstance(); $module_handler =& xoops_gethandler( 'module' ) ; $mymid = $xoopsModule->getVar('mid'); $table_name = 'entry'; $to_table = $db->prefix( $mydirname.'_'.$table_name ); $table_name = 'category'; $category_table = $db->prefix( $mydirname.'_'.$table_name ); $entry =& $entry_handler->create(); if ('' != MTEXPORT && !file_exists(MTEXPORT)) { echo("The file you specified does not seem to exist. Please check the path you've given."); break; } if ('' == MTEXPORT) { echo ("You must edit the MTEXPORT line as described on the previous page to continue."); break; } //" function checkauthor($author) { global $db; // Checks if an author exists, creates it if it doesn't, and returns author_id $users_handler =& xoops_gethandler( 'user' ) ; $criteria = new CriteriaCompo(new Criteria('uname', $author)); $uobj = $users_handler->getObjects($criteria); if ( isset($uobj) && is_array($uobj) && count($uobj) == 1) { $user_id = (int)$uobj[0]->get('uid'); } else { $user_id = 0; //インポートしない。 } echo $user_id.":".$author; return $user_id; } // Bring in the data $datalines = file(MTEXPORT); // Read the file into an array $importdata = implode('', $datalines); // squish it $mt_enc = mb_detect_encoding($importdata); $importdata = mb_convert_encoding($importdata, MTDBCODE, $mt_enc); $posts = explode("--------", $importdata); $i = -1; echo "
    "; foreach ($posts as $post) { if ('' != trim($post)) { ++$i; unset($post_categories); $post = preg_replace("/(\r\n|\n|\r)/", "\n", $post); echo "
  1. Importing post... "; //initiate(0); // Take the pings out first preg_match("|(-----\n\nPING:.*)|s", $post, $pings); $post = preg_replace("|(-----\n\nPING:.*)|s", '', $post); // Then take the comments out preg_match("|(-----\nCOMMENT:.*)|s", $post, $comments); $post = preg_replace("|(-----\nCOMMENT:.*)|s", '', $post); // We ignore the keywords $post = preg_replace("|(-----\nKEYWORDS:.*)|s", '', $post); // We want the excerpt preg_match("|-----\nEXCERPT:(.*)|s", $post, $excerpt); $excerpt = addslashes(trim($excerpt[1])); $post = preg_replace("|(-----\nEXCERPT:.*)|s", '', $post); // We're going to put extended body into main body with a seperator tag preg_match("|-----\nEXTENDED BODY:(.*)|s", $post, $extended); $extended = trim($extended[1]); $post = preg_replace("|(-----\nEXTENDED BODY:.*)|s", '', $post); // Now for the main body preg_match("|-----\nBODY:(.*)|s", $post, $body); $body = trim($body[1]); $post_content = addslashes($body . $extended); $post = preg_replace("|(-----\nBODY:.*)|s", '', $post); // Grab the metadata from what's left $metadata = explode("\n", $post); foreach ($metadata as $line) { preg_match("/^(.*?):(.*)/", $line, $token); $key = trim($token[1]); $value = trim($token[2]); // Now we decide what it is and what to do with it switch($key) { case '': break; case 'AUTHOR': $post_author = checkauthor($value); $entry->setVar('uid',$post_author); break; case 'TITLE': $post_title = addslashes($value); $entry->setVar ( 'title', addslashes($value) ); echo ''.stripslashes($post_title).'... '; break; case 'STATUS': // "publish" and "draft" enumeration items match up; no change required $post_allow_status = $value; $entry->setVar ( 'approved', 1 ); break; case 'ALLOW COMMENTS': $post_allow_comments = $value; if ($post_allow_comments == 1) { $comment_status = 1; } else { $comment_status = 0; } break; case 'CONVERT BREAKS': $post_convert_breaks = $value; break; case 'ALLOW PINGS': $post_allow_pings = trim($metadata[2][0]); if ($post_allow_pings == 1) { $post_allow_pings = 'open'; } else { $post_allow_pings = 'closed'; } break; case 'PRIMARY CATEGORY': $post_categories[] = addslashes($value); break; case 'CATEGORY': $post_categories[] = addslashes($value); break; case 'DATE': $post_date = strtotime($value); // $entry['created'] = $post_date; $entry->setVar ( 'created', $post_date); $entry->setVar ( 'published', $post_date); $entry->setVar ( 'modified', $post_date); $post_date1 = date('Y-m-d H:i:s', $post_date); break; default: break; //memo 参考サイト //http://blog.sakanapenguin.net/20070603/recent-mygentoo/ } // end switch } // End foreach $entry->setVar('cid', 1); $entry->setVar('dohtml', 1); $entry->setVar('dobr', 0); $entry->setVar('doxcode', 1); $entry->setVar('doimage', 1); $entry->setVar('groups', '|0|'); $entry->setVar('reads', 0); $entry->setVar('comments', 0); $entry->setVar('trackbacks', 0); $entry->setVar('excerpt', $body); $entry->setVar('body', $extended); // 記事が既に存在するかどうかのチェック $criteria = new CriteriaCompo(new Criteria('title', $entry->getVar('title'))); $criteria->add(new Criteria('created',$entry->getVar('created'))); $entry_obj = $entry_handler->getObjects($criteria) ; if ($post_author == 0) { echo "記事執筆者名がDB未登録:スキップ"; } elseif ($entry_obj) { echo "インポート済みの記事:スキップ"; // var_dump($entry); } else { // DBテーブルへの書き込み if (false != $entry_handler->insert($entry)) { // ** インポートした記事のbidを読み出す ** $b_id = (int)$entry->getVar('bid'); } //var_dump($b_id); //   カテゴリテーブル操作ループ if (0 != count($post_categories)) { foreach ($post_categories as $post_category) { // See if the category exists yet $sql = sprintf("SELECT cid from $category_table WHERE name = '$post_category'"); $re = $db->query($sql); if (!$re = $db->queryF($sql)){ die("sql=".$sql." DB ERROR: Failed to select from ".$category_table); } $category_row = $db->fetchArray($re); $cat_id = (int)$category_row[cid]; //var_dump($category_row); //var_dump($cat_id); if (0 != $cat_id){ } else { if ('' != trim($post_category)) { $sql = sprintf("INSERT INTO $category_table (cid, pid, name, weight, imgurl, created) VALUES (0, 0, '$post_category', 0, '', $post_date)"); if (!$rs = $db->queryF($sql)){ die("sql=".$sql." DB ERROR: Failed to insert to ".$category_table); } $sql = sprintf("SELECT cid from $category_table WHERE name = '$post_category'"); $re = $db->query($sql); if (!$re = $db->queryF($sql)){ die("sql=".$sql." DB ERROR: Failed to select from ".$category_table); } $category_row = $db->fetchArray($re); $cat_id = (int)$category_row[cid]; } //var_dump($cat_id); } if ('' == trim($post_category)) $cat_id = 1; } // end category loop } // エントリ中のカテゴリのアップデート $result = $db -> queryF( "UPDATE $to_table SET cid = $cat_id WHERE bid = $b_id" ); } // エントリのインポート、ここまで。 // ** 記事のbidを読み出す。 ここでは既存記事へのコメントや、 //    トラックバックデータのみの追加も可能 ** $sql = sprintf("SELECT * from $to_table WHERE title = '$post_title' AND created = $post_date"); $rm = $db->query($sql); if (!$rm){ die("sql=".$sql." DB ERROR: Failed to select from ".$to_table); } $entry =& $entry_handler->create(); $enrty = $db->fetchArray($rm); $b_id = (int)$enrty['bid']; //var_dump($b_id); // 執筆者チェック、投稿チェック。 if (($post_author != 0) && ($b_id != 0)) { // Now for comments $com_count = 0; // コメント数カウント $comments = explode("-----\nCOMMENT:", $comments[0]); //var_dump($comments); foreach ($comments as $comment) { if ('' != trim($comment)) { // Author preg_match("|AUTHOR:(.*)|", $comment, $comment_author); $comment_author = addslashes(trim($comment_author[1])); $comment = preg_replace('|(\n?AUTHOR:.*)|', '', $comment); preg_match("|EMAIL:(.*)|", $comment, $comment_email); $comment_email = addslashes(trim($comment_email[1])); $comment = preg_replace('|(\n?EMAIL:.*)|', '', $comment); preg_match("|IP:(.*)|", $comment, $comment_ip); $comment_ip = trim($comment_ip[1]); $comment = preg_replace('|(\n?IP:.*)|', '', $comment); preg_match("|URL:(.*)|", $comment, $comment_url); $comment_url = addslashes(trim($comment_url[1])); $comment = preg_replace('|(\n?URL:.*)|', '', $comment); preg_match("|DATE:(.*)|", $comment, $comment_date); $comment_date = trim($comment_date[1]); $comment_date = strtotime($comment_date); $comment = preg_replace('|(\n?DATE:.*)|', '', $comment); $comment_content = addslashes(trim($comment)); $comment_content = str_replace('-----', '', $comment_content); $com_count = $com_count+1; // コメント数カウント $com_authid = checkauthor($comment_author); if ( $com_count == 1 ) { $com_title = "Re:".$post_title; } else { $com_title = "Re^".$com_count.":".$post_title; } $com_mode = 'flat'; $com_order = XOOPS_COMMENT_OLD1ST; $com_pid = 0; $com_itemid = 0; $com_rootid = 0; $com_status = 2; $dosmiley = 1; $doxcode = 0; $dobr = 1; $dohtml = 0; $doimage = 1; $com_icon = ''; $noname = 0; $com_id = 0; // ** コメントの重複チェック。** //from message module $criteria = new CriteriaCompo(new Criteria('com_title', $com_title)); $criteria->add(new Criteria('com_created', $comment_date), 'AND'); $com_obj1 =& $comment_handler->create(); $com_obj1 = $comment_handler->getObjects($criteria); if ( isset($com_obj1) && is_array($com_obj1) && count($com_obj1) == 1) { echo " 同一コメントあり: スキップ。"; } else { //var_dump($com_obj1); $com_obj =& $comment_handler->create(); $accesserror = false; //var_dump($com_title); //var_dump($com_obj); $com_obj->setVar('com_modid', $com_modid); $com_obj->setVar('com_itemid', $b_id); $com_obj->setVar('com_icon', $com_icon); $com_obj->setVar('com_created', $comment_date); $com_obj->setVar('com_modified', $comment_date); $com_obj->setVar('com_uid',$com_authid); $com_obj->setVar('com_ip', $comment_ip); $com_obj->setVar('com_title', $com_title); // ゲストコメントは、本分の頭に 投稿者名を付加 if ($com_authid == 0) $comment_content = $comment_author. " wrote: \n". $comment_content; $com_obj->setVar('com_text', $comment_content); $com_obj->setVar('com_status', $com_status); $com_obj->setVar('dohtml', $dohtml); $com_obj->setVar('dosmiley', $dosmiley); $com_obj->setVar('doxcode', $doxcode); $com_obj->setVar('doimage', $doimage); $com_obj->setVar('dobr', $dobr); if (false != $comment_handler->insert($com_obj)) { $newcid = $com_obj->getVar('com_id'); if ($com_rootid == 0) { if ( $com_count == 1 ) { $com_rootid = $newcid; $com_lastrootid = $newcid; } else { $com_rootid = $com_lastrootid; $com_pid = $com_lastrootid; } if (!$comment_handler->updateByField($com_obj, 'com_rootid', $com_rootid)) { $comment_handler->delete($com_obj); } if (!$comment_handler->updateByField($com_obj, 'com_pid', $com_pid)) { $comment_handler->delete($com_obj); } } echo " Comment added."; } else { echo " Comment add failed."; } } } } // コメント数を、投稿本文のコメント数フィールドに反映 $result = $db -> queryF( "UPDATE $to_table SET comments = $com_count WHERE bid = $b_id" ); // Finally the pings // fix the double newline on the first one $pings[0] = str_replace("-----\n\n", "-----\n", $pings[0]); $pings = explode("-----\nPING:", $pings[0]); $tb_count = 0; // tb数カウント foreach ($pings as $ping) { if ('' != trim($ping)) { // 'Author' preg_match("|BLOG NAME:(.*)|", $ping, $blog_name); $blog_name = addslashes(trim($blog_name[1])); $ping = preg_replace('|(\n?BLOG NAME:.*)|', '', $ping); $comment_email = ''; preg_match("|IP:(.*)|", $ping, $comment_ip); $comment_ip = trim($comment_ip[1]); $ping = preg_replace('|(\n?IP:.*)|', '', $ping); preg_match("|URL:(.*)|", $ping, $comment_url); $comment_url = addslashes(trim($comment_url[1])); $ping = preg_replace('|(\n?URL:.*)|', '', $ping); preg_match("|DATE:(.*)|", $ping, $comment_date); $comment_date = trim($comment_date[1]); $comment_date = strtotime($comment_date); $ping = preg_replace('|(\n?DATE:.*)|', '', $ping); preg_match("|TITLE:(.*)|", $ping, $ping_title); $ping_title = addslashes(trim($ping_title[1])); $ping = preg_replace('|(\n?TITLE:.*)|', '', $ping); $comment_content = addslashes(trim($ping)); $comment_content = str_replace('-----', '', $comment_content); $tb_count = $tb_count+1; // tb数カウント $tb_approved = 1; $tb_direction = 2; // Check if it's already there $criteria = new CriteriaCompo(new Criteria('title', $ping_title)); $criteria->add(new Criteria('created', $comment_date), 'AND'); $tb_obj1 =& $tb_handler->create(); $tb_obj1 = $tb_handler->getObjects($criteria); if ( isset($tb_obj1) && is_array($tb_obj1) && count($tb_obj1) == 1) { echo " 同一trackbackあり: スキップ。"; } else { $tb_obj =& $tb_handler->create(); $tb_obj->setVar('blog_name', $blog_name); $tb_obj->setVar('title', $ping_title); $tb_obj->setVar('host', $comment_ip); $tb_obj->setVar('url', $comment_url); $tb_obj->setVar('created', $comment_date); $tb_obj->setVar('created', $comment_date); $tb_obj->setVar('excerpt', $comment_content); $tb_obj->setVar('approved', $tb_approved ); $tb_obj->setVar('direction', $tb_direction ); $tb_obj->setVar('bid', $b_id); //トラックバックのインサート if (false != $tb_handler->insert($tb_obj)) { echo " Trackback added."; } else { echo " Trackback add failed."; } } } } // トラックバック数を、投稿本文のトラックバック数フィールドに反映 $result = $db -> queryF( "UPDATE $to_table SET trackbacks = $tb_count WHERE bid = $b_id" ); } // コメント・pingのインポート、ここまで。 echo "
  2. "; flush(); // n } } ?>

All done. Have fun!