1<?php
2/**
3 * DokuWiki Plugin daummovie (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  V_L <dryoo@live.com>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12class syntax_plugin_daummovie extends DokuWiki_Syntax_Plugin {
13    /**
14     * @return string Syntax mode type
15     */
16    public function getType() {
17        return 'container';
18    }
19    /**
20     * @return string Paragraph type
21     */
22    public function getPType() {
23        return 'normal';
24    }
25    /**
26     * @return int Sort order - Low numbers go before high numbers
27     */
28    public function getSort() {
29        return 319;
30    }
31
32    /**
33     * Connect lookup pattern to lexer.
34     *
35     * @param string $mode Parser mode
36     */
37    public function connectTo($mode) {
38        $this->Lexer->addSpecialPattern
39			('\{\{daummovie>[^}]*\}\}',$mode,'plugin_daummovie');
40    }
41
42    /**
43     * Handle matches of the daummovie syntax
44     *
45     * @param string $match The match of the syntax
46     * @param int    $state The state of the handler
47     * @param int    $pos The position in the document
48     * @param Doku_Handler    $handler The handler
49     * @return array Data for the renderer
50     */
51    public function handle($match, $state, $pos, Doku_Handler $handler){
52
53        $data = array();
54        $match = substr($match,12,-2); //strip markup from start and end
55        $apikey= $this->getConf('daumapikey');
56		$request = 'http://apis.daum.net/contents/movie?apikey='.$apikey
57				.'&output=json&result=3&q='.urlencode($match);
58
59
60
61				for ($tries=1; $tries<10;$tries++) {
62					$response = json_decode($this->curl($request));
63					if ($response->channel->item[0]->title[0]->content!=null) break;
64					usleep(100000);
65				}
66
67
68
69
70		$movie=$response->channel->item[0];
71
72
73		$data['thumbnail']	=$movie->thumbnail[0]->content;
74		$data['title']		=$movie->title[0]->content;
75		$data['link']		=$movie->title[0]->link;
76		$data['eng_title']	=$movie->eng_title[0]->content;
77		$data['year']		=$movie->year[0]->content;
78		$data['director']	=$this->_join2($movie->director,false);
79		$data['nation']		=$movie->nation[0]->content;
80		$data['grades']		=$movie->grades[0]->content;
81		$data['genre']		=$this->_join2($movie->genre,false);
82		$data['actor']		=$this->_join2($movie->actor,false);
83		$data['open_info']	=$this->_join2($movie->open_info);
84		$data['query']		= $match." (".$tries.") ".$response->code;
85        return $data;
86    }
87
88    /**
89     * Render xhtml output or metadata
90     *
91     * @param string         $mode      Renderer mode (supported modes: xhtml)
92     * @param Doku_Renderer  $renderer  The renderer
93     * @param array          $data      The data from the handler() function
94     * @return bool If rendering was successful.
95     */
96    public function render($mode, Doku_Renderer $renderer, $data) {
97        if($mode != 'xhtml') return false;
98		if ($data['title']==null)
99		{   $renderer->doc.="<small>[DaumMovie: ".$data['query']."]</small>";
100			$renderer->doc.= date(DATE_RFC2822);
101                return false;}
102		$renderer->doc.= "<div class=\"daummovie\">";
103		$renderer->doc.= "<img src=\"".$data['thumbnail']."\" alt=\"\" style=\"margin-right:1em;Max-width:110px;\">";
104
105
106		$renderer->doc.= "<a href=\"".$data['link']."\" class=\"urlextern\" rel=\"nofollow\">".$data['title']." <small>".$data['year']."</small></a><br>";
107		$renderer->doc.= $data['eng_title']."<br>";
108		$renderer->doc.= "<strong>감독</strong> : ".$data['director']."<br>";
109		$renderer->doc.= "<strong>평점</strong> : ".$data['grades']."<br>";
110		$renderer->doc.= "<strong>정보</strong> : ".$data['nation']." ".$data['open_info']."<br>";
111		$renderer->doc.= "<strong>분류</strong> : ".$data['genre']."<br>";
112		$renderer->doc.= "<strong>출연</strong> : ".$data['actor']."<br></small></div>";
113
114     //   $renderer->doc.= "<a href=\"".$data['link']."\" rel=\"nofollow\">".$data['title']." <small>".$data['year']."</small></a><br><small>";
115
116   /*     $wdata.="".$data['eng_title']." \r\n ";
117        $wdata.="     * 감독 : [[".$data['director']."]]  \r\n";
118        $wdata.="     * 평점 : ".$data['grades']."  \r\n";
119        $wdata.="     * 정보 : [[".$data['nation']."]] ".$data['open_info']." \r\n";
120        $wdata.="     * 분류 : ".$data['genre']."  \r\n";
121        $wdata.="     * 출연 : ".$data['actor']."  \r\n";
122        $renderer->doc.=p_render('xhtml',p_get_instructions($wdata),$info); */
123
124		//$renderer->doc.= "</small></div>";
125		//$renderer->doc.= $data['query'];
126
127		return true;
128
129}
130	function _join($xxx,$link=false){
131		if ($xxx)
132		{
133			foreach ($xxx as $val) {
134				if ($link) $out.=" [[".$val->content."]] ";
135                   else $out.="".$val->content." ";
136			}
137          return $out;
138	  } else
139		  return false;
140	}
141
142    function _join2($xxx,$link=false){
143        if ($xxx)
144        {
145            foreach ($xxx as $val) {
146                if ($link) $out.=" [[".$val->content."]] ";
147                   else {
148                        $_link=explode('|',$val->content);
149
150                       // $out.="<img src='".$_link[1]."' style='max-width:20px' >";
151                        $out.=$_link[0];
152                        if ($_link[2])
153                        $out.="(".$_link[2].")";
154                        $out.=" ";
155                   }
156            }
157          return $out;
158      } else
159          return false;
160    }
161
162
163	function curl($url,$param=false)
164	{ // http://www.partner114.com/bbs/board.php?bo_table=B07&wr_id=126
165		$ch = curl_init();
166		curl_setopt($ch, CURLOPT_URL, $url);
167		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
168		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
169        if ($param) curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
170		$g = curl_exec($ch);
171		curl_close($ch);
172		return $g;
173	}
174
175}
176
177// vim:ts=4:sw=4:et:
178