1 <?php
2 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
3 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4 require_once(DOKU_PLUGIN.'syntax.php');
5 
6 class syntax_plugin_wow extends DokuWiki_Syntax_Plugin
7 {
8 	function getInfo()
9 	{
10 		return array(
11 			'author'	=> 'Sardem FF7',
12 			'email'		=> 'sardemff7.pub@gmail.com',
13 			'date'		=> '2010-03-08',
14 			'name'		=> 'WoW Plugin',
15 			'desc'		=> 'Allow to automatic link and name extract from Wowhead with an ID',
16 			'url'		=> 'http://www.dokuwiki.org/plugin:wow',
17 		);
18 	}
19 
20 	function getType()
21 	{
22 		return 'substition';
23 	}
24 
25 	/* Optionnel
26 	function getAllowedTypes()
27 	{
28 		return array();
29 	}
30 	*/
31 
32 	/* Optionnel
33 	function getPType()
34 	{
35 		return 'normal';
36 	}
37 	*/
38 
39 	function getSort()
40 	{
41 		return 400;
42 	}
43 
44 	function connectTo($mode)
45 	{
46 		$this->Lexer->addSpecialPattern("~~WoW [qisnzr]:\d+~~", $mode, "plugin_wow");
47 /*		$this->Lexer->addEntryPattern("<WoW [qisn]:\d+(:[hj])?>(?=.*</WoW>)", $mode, "plugin_wow"); */
48 	}
49 
50 	/*
51 	function postConnect()
52 	{
53 		$this->Lexer->addExitPattern("</WoW>","plugin_wow");
54 	}
55 	*/
56 
57 	function handle($match, $state, $pos, &$handler)
58 	{
59 		preg_match("#~~WoW ([qisnzr]):(\d+)~~#", $match, $matches);
60 		return array($matches[1], $matches[2], $matches[3]);
61 	}
62 
63 	function cache($type, $id, $name = "")
64 	{
65 	}
66 
67 	function render($mode, &$renderer, $data)
68 	{
69 		if( $mode == "xhtml" )
70 		{
71 			list($type, $id) = $data;
72 
73 			$link = "http://" . $this->getConf('lang') . ".wowhead.com/?";
74 			switch ( $type )
75 			{
76 				case "q" :
77 					$link .= "quest";
78 					break;
79 				case "i" :
80 					$link .= "item";
81 					break;
82 				case "s" :
83 					$link .= "spell";
84 					break;
85 				case "n" :
86 					$link .= "npc";
87 					break;
88 				case "z" :
89 					$link .= "zone";
90 					break;
91 				case "r" :
92 					$link .= "zones";
93 					break;
94 			}
95 			$link .= "=" . $id;
96 
97 
98 			$name = false;
99 			$dir = DOKU_PLUGIN . "/wow/cache/" . $this->getConf('lang');
100 			if ( ! file_exists($dir) )
101 				mkdir($dir);
102 			$file = $dir . "/" . $type . ".txt";
103 			if ( file_exists($file) )
104 			{
105 				foreach ( file($file) as $line )
106 				{
107 					if ( preg_match("/^" . $id . "=(.+)$/", $line, $matches) )
108 					{
109 						$name = $matches[1];
110 						break;
111 					}
112 				}
113 			}
114 
115 			if ( $name == false )
116 			{
117 				require_once(DOKU_INC . '/inc/HTTPClient.php');
118 				$http = new DokuHTTPClient();
119 				$page = $http->get($link);
120 
121 				switch ( $this->getConf('lang') )
122 				{
123 					case "www" :
124 						$names = array(
125 								"Spell",
126 								"Quest",
127 								"NPC",
128 								"Item",
129 								"Zones?"
130 							);
131 						break;
132 					case "fr" :
133 						$names = array(
134 								"Sort",
135 								"Quête",
136 								"PNJ",
137 								"Objet",
138 								"Zones?"
139 							);
140 						break;
141 					case "de" :
142 						$names = array(
143 								"Zauber",
144 								"Quest",
145 								"NPC",
146 								"Gegenstand",
147 								"Zone",
148 								"Gebiete"
149 							);
150 						break;
151 					case "es" :
152 						$names = array(
153 								"Hechizo",
154 								"Misiòn",
155 								"PNJ",
156 								"Objeto",
157 								"Zonas?"
158 							);
159 						break;
160 					case "ru" :
161 						$names = array(
162 								"Заклинание",
163 								"Задание",
164 								"НИП",
165 								"Предмет",
166 								"Игровая зона",
167 								"Местности"
168 							);
169 						break;
170 				}
171 				preg_match('#<title>(.+) - (?:' . implode('|', $names) . ') - World of Warcraft</title>#', $page, $matches);
172 				$name = $matches[1];
173 				file_put_contents($file, $id . '=' . $name . "\n", FILE_APPEND);
174 			}
175 
176 			$renderer->doc .= '<a href="' . $link . '"><img src="' . substr(DOKU_PLUGIN, strlen($_SERVER["DOCUMENT_ROOT"])) . '/wow/images/wowhead.png" alt="Wowhead" />' . $name . '</a>';
177 			return true;
178 		}
179 		return false;
180 	}
181 }
182 ?>
183