PR

nicky.cgi から WordPress へ記事移動

前回 pc.casey.jp » nicky.cgi から Movable Type へ記事移動 を書いた。

Movable Type形式は画像データをインポートできないが、WordPress形式はインポートできる。WordPressは使いやすいシステムだが、それ故様々な機能が効果的に動作していてなかなか入り込むことが難しい。

というのは、通常ならFTPで画像データを放りこめばよいだろうと考えてしまうのだが、それでは認識されない。

前回、nicky.cgiのデータを解析し、Movable Type形式でのエクスポートまでやった。そして、nicky.cgiからのデータエクスポートもやらなければなと思っていたところだし、最近はphpでCUI/GUIのexeを作って遊んでいる。となれば・・・ということである。

スポンサードリンク

概要

  • コマンドラインから実行する単体exeです。
  • 本プログラムはオープンソース(OSS)でライセンスはGPLです。自由に勝手に使ってください。そのかわり補償などはありません。
  • ソースを公開しますのでWindows以外でも簡単に移植できるでしょう。
  • nicky.cgiからのエクスポートが自分に必要だったのでサラっとMovable Type版を作ったのですが、画像データのインポートがサポートされないようなので別プログラムとして作りました。
  • プログラムはmain.phpとnicky_conv.phpの2つのソースからなっています。
  • 以下にそれを公開します。通常はexe版はこちらです→ nicky_conv_wp_cui (721 ダウンロード ) 。Movable Type版は別のページに置いておきます。
  • WordPress版では画像のインポートもサポートします。
  • xmlは多分めちゃくちゃな解析だとは思いますがWordPress3.0.1のマルチサイト構築してあるものに、テストで1つサイトを作り流しこんで期待した動作をしたのでよしとしています。

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.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 'End';
	exit(0);
?>

nicky_conv.php

<?php
    /* ----------------------------------------------------
     *    nicky.cgi data convert script for WordPress
     *    ( php for win32 command user interface )
     * ----------------------------------------------------
     * History:
     *  - 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->reset();
    }

    // init for nexe data
    function reset(){
        // init
        $this->Author            = 'admin';
        $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;
    }

    // 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");
        }
        $this->Title = $a[1] ? $a[1] : $this->Title;
        $this->Body  = $a[2] ? $a[2] : $this->Body;

        // image?
        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;
        }

        // 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->outputPost = $this->_makePost();

        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 . '<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_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_type>attachment</wp:post_type>';
        $data .= PHP_EOL . '<wp:attachment_url>' . $path . $name . '</wp:attachment_url>';
        $data .= PHP_EOL . '<wp:postmeta>';
        $data .= PHP_EOL . '<wp:meta_key>_wp_attached_file</wp:meta_key>';
        $data .= PHP_EOL . '</wp:postmeta>';
        $data .= PHP_EOL . '</item>';
        return $data;
    }
}
?>

※汚いコードですいません。その場しのぎなので、と言い訳しておきます・・・。

コメント