pc.casey.jp » nicky.cgi から WordPress へ記事移動 のプログラムをバージョンアップ(Ver.0.02a)しました。
日時(日付ではなくて)のないものや、タイトルのないものの受け入れがWordPressは苦手なようですので、プログラムを少し改変しました:
スポンサードリンク
- 記事本文1999年から2010年までの3,101件中3,100件の移動に成功しました。
- 画像のインポートはできましたが、これまでのバージョンでは「未使用」となっていました。これを記事と画像を関連付けるようにしました。
- 上記の影響によって1,990/01/01 00:00:00 以前の受け入れはうまく行かないかもしれません。
- プログラムがさらに汚くなりましたがインポートがうまく行かない問題を回避するための姑息な手段が盛りだくさんです。
- 相変わらずコメントは無視です。筆者がコメントを受け付けるような使い方をほとんどしていなかったためです。
- それでも何度かやらないと入らないことがあります。何度か試せる構造になっていますので記事数を見比べてください。特にタイトルがないものが苦手のようです。(なのでタイトルがないものは勝手に付与します)
バージョン内容
- 日付をUnixtimeに変換してIDに見立て、画像と記事に結びつけるIDとして利用した。これによってメディアライブラリで未使用になることを防ぎ、かつ、記事とリンクできるようにした。
- 数値抑制のために、1,990/01/01 00:00:00を引いた。
- 処理件数を返却するfunctionを追加した。
- 完了時に処理件数を返却する。
- いろいろやってもIDの重複をさけられないときは停止することにした。
- <BR>タグを改行に変換するようにした。
- 一部の記事が正しくインポートされない問題を回避した(それでも何度かやらないと入らないこともある)。
※おそらくこれが最終バージョンになるでしょう。なぜなら筆者がインポート・エクスポートをまもなく終えるからです。
コード
main.php
<?php
/* ----------------------------------------------------
* nicky.cgi data convert script for Movable Type
* ( php for win32 command user interface )
* ----------------------------------------------------
* Usage:
* nicky_conv_cui.exe "c:\nicky\2009" "http://old.yourdomain.jp/nicky/2009/"
*
* History:
* - 2010.10.11 Ver.0.02a display proc count.
* - 2010.09.04 Ver.0.01a first.
*
* Script=Shift-JIS, Import=EUC, Output=UTF-8
* Copyright 2010 CASEY, GPL
*/
require_once("nicky_conv.php");
$n = new nicky_conv();
$filetype = "nky";
$outputFileName = "data.txt";
// get data path --------------------------------------
if(empty($argv[1])){
print 'Ex. nicky_conv_cui.exe "path" ["image_url_path"]';
exit(0);
}
$baseDir = $argv[1] . '\\';
print "Target:" .$baseDir . PHP_EOL;
// get replace url ------------------------------------
if(!empty($argv[2])){
print "imgPath:" .$argv[2] . PHP_EOL;
$n->setImagePath($argv[2]);
}else{
print "imgPath: Nothing" . PHP_EOL;
}
// get script path ------------------------------------
$thisPath = getcwd();
print "pgPath:" . $thisPath . PHP_EOL;
$outputFileName = $thisPath . '\\' . $outputFileName;
if(file_exists($outputFileName)){
unlink($outputFileName);
}
// start conv -----------------------------------------
$DATA = null;
$dir = opendir($baseDir);
$DATA .= $n->getHeader();
while($file = readdir($dir)) {
if(is_file($baseDir.$file)) {
if (preg_match("/\.$filetype/i", $file)) {
$n->reset();
$n->readData($baseDir.$file);
// post
$data = $n->outputPost();
print $data;
$DATA .= mb_convert_encoding($data, "UTF-8", "SJIS");
// image
$data = $n->outputImage();
print $data;
$DATA .= $data;
}
}
}
$DATA .= $n->getFooter();
closedir($dir);
// write
$fp = fopen($outputFileName, "a");
fwrite($fp, $DATA);
fclose($fp);
// end proc ------------------------------------------
print PHP_EOL . 'End' . $n->getCount();
exit(0);
?>
nicky_conv.php
<?php
/* ----------------------------------------------------
* nicky.cgi data convert script for WordPress
* ( php for win32 command user interface )
* ----------------------------------------------------
* History:
* - 2010.10.11 Ver.0.02a
* - Date to ID.
* - Linked images and articles id.
* - <BR> to PHP_EOL.
* - Fix, Importer does not recognize some of the articles.
* - add funcion getCount
* - 2010.09.04 Ver.0.01a first
*
* Script=Shift-JIS, Import=EUC, Output=UTF-8
* Copyright 2010 CASEY, GPL
*/
class nicky_conv{
// init
function nicky_conv(){
$this->Ver = "0.02a";
$this->Id = 1;
$this->Cnt = 1;
$this->LastId = null;
$this->reset();
}
// init for nexe data
function reset(){
// init
$this->Author = 'admin';
$this->Category = 'oldnicky';
$this->Title = 'no title';
$this->Status = 'publish';
$this->AllowComments = 'close';
$this->AllowPings = 'open';
$this->Body = null;
$this->Date = '1970/01/01 00:00:00';
$this->outputImage = null;
$this->outputPost = null;
$this->Cnt++;
}
// return get count
function getCount(){
return $this->Cnt - 2;
}
// return image data
function outputImage(){
return $this->outputImage;
}
// return post data
function outputPost(){
return $this->outputPost;
}
// set image path
function setImagePath($url){
$this->ImagePath = $url;
}
// read data (*.nky, EUC)
function readData($file){
$output = null;
// read
$data = @file_get_contents($file);
$arr = split("\x01", $data);
// convert
$a = array();
foreach($arr as $k => $v){
$a[$k] = mb_convert_encoding($v, "SJIS", "EUC");
}
// BODY a[2]
$a[2] = mb_eregi_replace('<BR>', PHP_EOL, $a[2]);
$this->Body = $a[2] ? $a[2] : $this->Body;
// DATE arr[0] = date or date time
mb_ereg('(\d+)[^\d.+](\d+)[^\d.+](\d+)', $arr[0], $dt_arr);
$dt = sprintf("%s/%s/%s", $dt_arr[1], $dt_arr[2], $dt_arr[3]);
if(mb_ereg('(\d+):(\d+)', $a[0], $dt_arr)){
$dt .= sprintf(" %s:%s:%s", $dt_arr[1], $dt_arr[2], '00');
}else{
$dt . '00:00:00';
}
$this->Date = $dt;
$this->Id = strtotime($dt) - strtotime('1990/01/01 00:00:00');
if($this->LastId == $this->Id){
$this->Id += $this->Cnt;
}
// TITLE a[1] (after Id)
if($this->LastId == $this->Id){
print PHP_EOL . 'Error!!' . 'No.' . $this->Id . ' at ' . $dt;
exit(-1);
}
$this->LastId = $this->Id;
//$this->Title = $a[1] ? $a[1] : $this->Title;
$this->Title = $a[1] ? $a[1] : $this->Date . '.' .$this->Cnt;
//if(!$a[1]){ print_r($a); print_r($arr); } // debug
// IMAGE? (after get date)
if($a[3]){
$this->outputImage = $this->_makeImage($this->ImagePath, $a[3]);
$tag = sprintf('<img alt="%s" src="%s" /><br /><br />', $a[3], $this->ImagePath . $a[3]);
$this->Body = $tag . $this->Body;
}
// DATA
$this->outputPost = $this->_makePost();
// end
return true;
}
// return data file header
function getHeader(){
$data = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<rss version="2.0" xmlns:excerpt="http://wordpress.org/export/1.0/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.0/">';
$data .= '<channel>';
$data .= '<wp:wxr_version>1.0</wp:wxr_version>';
return $data;
}
// return data file footer
function getFooter(){
$data = null;
$data .='</channel>';
$data .='</rss>';
return $data;
}
// make post data format
function _makePost(){
$data = null;
$data .= PHP_EOL . '<item>';
$data .= PHP_EOL . '<title>' . $this->Title . '</title>';
$data .= PHP_EOL . '<category><![CDATA[' .$this->Category. ']]></category>';
$data .= PHP_EOL . '<dc:creator><![CDATA['. $this->Author .']]></dc:creator>';
$data .= PHP_EOL . '<content:encoded><![CDATA['. $this->Body .']]></content:encoded>';
$data .= PHP_EOL . '<excerpt:encoded><![CDATA[]]></excerpt:encoded>';
$data .= PHP_EOL . '<wp:post_id>' .$this->Id. '</wp:post_id>';
$data .= PHP_EOL . '<wp:post_date>'. $this->Date .'</wp:post_date>';
$data .= PHP_EOL . '<wp:comment_status>'. $this->AllowComments .'</wp:comment_status>';
$data .= PHP_EOL . '<wp:ping_status>'. $this->AllowPings .'</wp:ping_status>';
$data .= PHP_EOL . '<wp:status>'. $this->Status .'</wp:status>';
$data .= PHP_EOL . '<wp:post_parent>0</wp:post_parent>';
$data .= PHP_EOL . '<wp:menu_order>0</wp:menu_order>';
$data .= PHP_EOL . '<wp:post_type>post</wp:post_type>';
$data .= PHP_EOL . '<wp:is_sticky>0</wp:is_sticky>';
$data .= PHP_EOL . '</item>';
return $data;
}
// make image data format
function _makeImage($path, $name){
$data = null;
$data .= PHP_EOL . '<item>';
$data .= PHP_EOL . '<title>'.$name.'</title> ';
$data .= PHP_EOL . '<dc:creator><![CDATA['. $this->Author .']]></dc:creator>';
$data .= PHP_EOL . '<wp:post_date>'.$this->Date.'</wp:post_date>';
$data .= PHP_EOL . '<wp:post_type>attachment</wp:post_type>';
$data .= PHP_EOL . '<wp:attachment_url>' . $path . $name . '</wp:attachment_url>';
$data .= PHP_EOL . '<wp:post_parent>' .$this->Id. '</wp:post_parent>';
$data .= PHP_EOL . '<wp:postmeta><wp:meta_key>_wp_attached_file</wp:meta_key></wp:postmeta>';
$data .= PHP_EOL . '</item>';
return $data;
}
}
?>
※面倒な人はダウンロードして使ってね。
使い方の例
こんな感じに:
nicky_conv_cui.exe "D:\www\cgi\diary\2008" "http://www.oldyoursite.jp/cgi/diary/2008/"


コメント
すみません、私はあなたのプログラムにNICKY!のログをエクスポートするには問題がある。多言語対応のためにNICKY!でUTF-8をエンコード。この原因は、テキストログはエクスポート後に読めなくなる…どうすればいい?