wildcat on plurk

Plurk.com

  • 工具準備
    1. ANSI2HTML 之 function
      (可參考 bhttpd.c 或是直接問 Google 大神)
    2. blogger XML function
      (參考 http://www.dentedreality.com.au/bloggerapi/ 來修改)
    3. Big5 to UTF-8 function
      PHP 內的 iconv function 有時會怪怪的,
      遇到無法轉過去的字元就傳回空字串 = =
      所以乖乖的用 iconv 的 binary 程式來轉吧 :p
      我在 PipperL 的 Blog
      找到一隻古老的 convert 程式
  • 實作1 – PHP 的 XML-RPC 程式

    採用 PHP CLI mode 來傳遞變數,呼叫檔案,
    轉碼之後塞給 WP 的 XML-RPC
    執行方式為
    /usr/local/bin/php bbs2wp.php 變數1 變數2 變數3 … etc.
    不考慮包在 BBS 的程式來寫的主要原因是:
    既然有寫好的套件就拿來改不是很方便嗎? :p

    大致的程式是這樣的:


    // 工具準備的第二項,改一下變數就可以直接用了
    // 我把 ANSI2HTML 跟 big52utf8 也包在這個 include 檔
    include(‘blogger.php’);

    // argv 的第 0 欄是 執行的程式名稱唷。 :p
    $blogid = $_SERVER["argv"][1];
    $userid = $_SERVER["argv"][2];
    $passwd = $_SERVER["argv"][3];
    $path = $_SERVER["argv"][4];
    $postid = $_SERVER["argv"][5];
    $forward = $_SERVER["argv"][6];
    /*
    然後用 file_get_contents 去抓要轉的那篇文章把它變成 string,
    接著用一些字串的程式,來抓出哪邊是標題,哪邊是內文。
    */
    $post_content = file_get_contents("/home/bbs/$path/$postid" );
    if($forward) $content = $post_content;

    $post_content = strstr($post_content ,"標題:" );

    $title = substr($post_content, 0, strpos($post_content, "n" ));
    $title = substr($title, 5, strlen($title));
    if($forward) $title = "[轉錄] ".$title;

    $post_content = strstr($post_content ,"時間:" );

    if(!$forward)
    $content = substr($post_content, strpos($post_content, "n"),
    strlen($post_content) );

    // 最後幫內文加上標題
    // 成為 WP 吃的 XML 格式,再把它從 Big5 轉成 UTF-8,
    $content = big52utf8("<title> $title </title>n".ANSI_2_HTML($content));

    // 就可以餵給 WP 了!
    blogger_newPost($blogid, $userid, $passwd, $content, 1);

    如果是單人版的 WP, $blogid 當然可以省略 :p
    ps. 程式裡面的中文字都是 UTF-8 格式!

  • 實作2 – bbs 程式之修改(以 WDBBS 為例)
    1. 修改 read.c 加入發佈的熱鍵,可以參考轉錄文章信件的程式

    在 i_read_key() 加入這個熱鍵:


    case ‘!’:
    char fname[512];
    fileheader *fhdr = &headers[locmem->crs_ln - locmem->top_ln];

    if(fhdr->filemode & FILE_REFUSE)
    break;

    setdirpath(fname, currdirect, fhdr->filename);

    if (dashf(fname))
    post2blog(&headers[locmem->crs_ln - locmem->top_ln], currdirect);

    return RC_FULL;

    break;

    2. 既然是仿 mail forward,那就加在 mail.c 吧 :p


    void
    post2blog(fhdr, direct)
    fileheader *fhdr;
    char *direct;
    {
    char buf[STRLEN];
    char *p;

    strncpy(buf, direct, sizeof(buf));
    if (p = strrchr(buf, ‘/’))
    *p = ";

    switch (xmlrpc_wp(buf, fhdr))
    {
    case 0:
    outz("發佈完畢" );
    break;
    case -1:
    outz("發佈失敗" );
    break;
    case -2:
    outz("發佈失敗" );
    }
    }

    int
    xmlrpc_wp(direct, fh)
    char *direct;
    fileheader *fh;
    {
    static char address[60];
    char fname[MAXPATHLEN];
    int forward = 0;
    char genbuf[512],blogid[128],userid[128],passwd[128];

    sprintf(genbuf, "確定將本篇文章發佈到 Blog 嗎(Y/N)?[Y] ");
    getdata(b_lines – 1, 0, genbuf, fname, 3, LCECHO,0);

    if (fname[0] == ‘n’ || fname[0] == ‘N’)
    {
    outz("取消發佈" );
    return 1;
    }

    getdata(b_lines – 1, 0, "請輸入Blog名稱(英文ID):", fname, 20, DOECHO,0);
    if (fname[0])
    strcpy(blogid, fname);
    else
    {
    outz("取消發佈" );
    return 1;
    }

    getdata(b_lines – 1, 0, "請輸入您在 Blog 的登入 ID:", fname, 20, DOECHO,0);
    if (fname[0])
    strcpy(userid, fname);
    else
    {
    outz("取消發佈" );
    return 1;
    }

    getdata(b_lines – 1, 0, "請輸入您在 Blog 的登入密碼:", fname, 20, PASS,0);
    if (fname[0])
    strcpy(passwd, fname);
    else
    {
    outz("取消發佈" );
    return 1;
    }

    outz("發佈中" );

    // 如果作者不是自己,加上轉錄 flag
    if(strcmp(fh->owner, cuser.userid) != 0) forward = 1;

    sprintf(fname ,"%s %s %s %s %s %d",
    blogid, userid, passwd, direct, fh->filename, forward);
    sprintf(genbuf,
    "/usr/local/bin/php /home/bbs/bin/bbs2wp.php %s >> 記錄的檔案", fname);
    system(genbuf);

    return 0;
    }

    這樣就大功告成了!
    如果是要傳遞到別的 Blog System,也可以加上 Blog XML-RPC url 這個變數。

目前 WD 完成度大概有 80%,剩下一些 ANSI 的轉換,還有 UTF-8 日文的問題,有空再慢慢修吧 orz

3 Responses to “BBS to WordPress via XML-RPC 實作說明”

  1. Tsung 說道:

    php 的 iconv 轉換出來空白代表是轉換失敗(應該是 0), 建議可以用 mb_convert_encoding() 看看 :)

  2. 野貓 說道:

    我後來也發現了,
    mb_convert_encoding 不錯用,
    不過 mb_detect_encoding 就沒那麼聰明,
    很多 Big5 的字串還是會判斷成 UTF-8 orz

  3. Benson 說道:

    很實用的文章!謝謝分享

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>