PukiWikiにHTTP経由でデータをインポートする

紙copi のデータを PukiWiki に移行しようと思って適当なスクリプトを書いた。
wiki フォーマットで書いていないので見た目は乱れるがMacを使うことが多くなったのでPukiWiki一本にまとめることにした。

<?php
/**
 *  PukiWiki HTTP API
 *
 *  @since  2009.1.18
 */
require_once 'Zend/Http/Client.php';
class Pukiwiki_API
{
    const DEFAULT_ENCODING = 'EUC-JP';
    private $_http;
    public function __construct($serverName, $httpAuthUserName, $httpAuthPassword)
    {
        $this->_http = new Zend_Http_Client($serverName, array('keepalive' => true));
        $this->_http->setAuth($httpAuthUserName, $httpAuthPassword);
    }
    public function write($title, $body)
    {
        $this->_http->setParameterPost(array(
                        'encode_hint' => $this->encode('ぷ'),
                        'template_page' => '',
                        'cmd' => 'edit',
                        'page' => $this->encode($title),
                        'digest' => md5(''),
                        'msg' => $this->encode($body),
                        'write' => $this->encode('ページの更新'),
                        'notimestamp' => 'true', // 効いてる??
                        'original' => $this->encode($body),
                    ));
        $response = $this->_http->request('POST');
        //echo mb_convert_encoding($response->getBody(), 'UTF-8', self::DEFAULT_ENCODING);
    }
    private function encode($str)
    {
        return mb_convert_encoding($str, self::DEFAULT_ENCODING, 'UTF-8');
    }
}

呼び出し側は

<?php
require_once 'Pukiwiki_API.php';
$p = new Pukiwiki_API('http://PukiWikiを設置したサーバ/', 'ベーシック認証用のユーザ名', $argv[1]);
$path = realpath($argv[2]); // 紙copi のディレクトリを指定
$pathLen = strlen($path);
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object) {
    if ($object->isFile()) {
        $pageName = "kami" . substr($name, $pathLen);
        $body = mb_convert_encoding(file_get_contents($name), 'UTF-8', 'SJIS');
        echo $pageName, "\n";
        $p->write($pageName, $body);
    }
}
This entry was posted in いじる. Bookmark the permalink. Both comments and trackbacks are currently closed.

Page optimized by WP Minify WordPress Plugin