とあるセクシーなデータ分析官

を目指す渋谷で働くソーシャルゲーム分析者の卵

WikiのAPIをとText_Wikiを使って、wiki内から説明文を取得してみた

前回はGoogleでWiki内の記事検索をやりましたが、
そこからの派生でWiki本文を取得してしまおうという試みです。


サンプルプログラム
http://keywood.ryuquo.com/wiki/input.php


まずPEARからText_Wikiをインストールしていることが前提です!
http://pear.php.net/search.php?q=Text_Wiki&in=packages

後はソースをご参考に

    function parseWiki($name)
    {
        require_once (sfConfig::get('sf_lib_dir')."/Text/Wiki.php");
        $context = stream_context_create(array('http' => array(
                        'method' => 'GET',
                        'header' => 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')));

        $url = "http://ja.wikipedia.org/wiki/%E7%89%B9%E5%88%A5:%E3%83%87%E3%83%BC%E3%82%BF%E6%9B%B8%E3%81%8D%E5%87%BA%E3%81%97/{$name}";
        $data = file_get_contents($url, false, $context);

        $wiki = new Text_Wiki();
        $wiki->deleteRule('Wikilink');
        $wiki->setFormatConf('Xhtml', 'translate', 'false');
        $body = $wiki->transform($data, 'xhtml');

        preg_match('|(.*?)<\/text>|is', $body, $match);
        $text = $match[2];

        $before = array('|\[|', '|\]|', '|\{|', '|\}|', '|(.*?)
|', '|(.*?)\}\}|', '|Infobox(.*?)|', '|<br/>|' ,'/\|(.*?)\n/', '|(.*?).jpg|', '|(.*?).png|', '