<?php
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once(DOKU_PLUGIN.'syntax.php');

class syntax_plugin_wow extends DokuWiki_Syntax_Plugin
{
	function getInfo()
	{
		return array(
			'author'	=> 'Sardem FF7',
			'email'		=> 'sardemff7.pub@gmail.com',
			'date'		=> '2010-03-08',
			'name'		=> 'WoW Plugin',
			'desc'		=> 'Allow to automatic link and name extract from Wowhead with an ID',
			'url'		=> 'http://www.dokuwiki.org/plugin:wow',
		);
	}
	
	function getType()
	{
		return 'substition';
	}
	
	/* Optionnel
	function getAllowedTypes()
	{
		return array();
	}
	*/
	
	/* Optionnel
	function getPType()
	{
		return 'normal';
	}
	*/
	
	function getSort()
	{
		return 400;
	}
	
	function connectTo($mode)
	{
		$this->Lexer->addSpecialPattern("~~WoW [qisnzr]:\d+~~", $mode, "plugin_wow");
/*		$this->Lexer->addEntryPattern("<WoW [qisn]:\d+(:[hj])?>(?=.*</WoW>)", $mode, "plugin_wow"); */
	}
	
	/*
	function postConnect()
	{
		$this->Lexer->addExitPattern("</WoW>","plugin_wow");
	}
	*/
	
	function handle($match, $state, $pos, &$handler)
	{
		preg_match("#~~WoW ([qisnzr]):(\d+)~~#", $match, $matches);
		return array($matches[1], $matches[2], $matches[3]);
	}
	
	function cache($type, $id, $name = "")
	{
	}
	
	function render($mode, &$renderer, $data)
	{
		if( $mode == "xhtml" )
		{
			list($type, $id) = $data;
			
			$link = "http://" . $this->getConf('lang') . ".wowhead.com/?";
			switch ( $type )
			{
				case "q" :
					$link .= "quest";
					break;
				case "i" :
					$link .= "item";
					break;
				case "s" :
					$link .= "spell";
					break;
				case "n" :
					$link .= "npc";
					break;
				case "z" :
					$link .= "zone";
					break;
				case "r" :
					$link .= "zones";
					break;
			}
			$link .= "=" . $id;
			
			
			$name = false;
			$dir = DOKU_PLUGIN . "/wow/cache/" . $this->getConf('lang');
			if ( ! file_exists($dir) )
				mkdir($dir);
			$file = $dir . "/" . $type . ".txt";
			if ( file_exists($file) )
			{
				foreach ( file($file) as $line )
				{
					if ( preg_match("/^" . $id . "=(.+)$/", $line, $matches) )
					{
						$name = $matches[1];
						break;
					}
				}
			}
			
			if ( $name == false )
			{
				require_once(DOKU_INC . '/inc/HTTPClient.php');
				$http = new DokuHTTPClient();
				$page = $http->get($link);
				
				switch ( $this->getConf('lang') )
				{
					case "www" :
						$names = array(
								"Spell",
								"Quest",
								"NPC",
								"Item",
								"Zones?"
							);
						break;
					case "fr" :
						$names = array(
								"Sort",
								"Quête",
								"PNJ",
								"Objet",
								"Zones?"
							);
						break;
					case "de" :
						$names = array(
								"Zauber",
								"Quest",
								"NPC",
								"Gegenstand",
								"Zone",
								"Gebiete"
							);
						break;
					case "es" :
						$names = array(
								"Hechizo",
								"Misiòn",
								"PNJ",
								"Objeto",
								"Zonas?"
							);
						break;
					case "ru" :
						$names = array(
								"Заклинание",
								"Задание",
								"НИП",
								"Предмет",
								"Игровая зона",
								"Местности"
							);
						break;
				}
				preg_match('#<title>(.+) - (?:' . implode('|', $names) . ') - World of Warcraft</title>#', $page, $matches);
				$name = $matches[1];
				file_put_contents($file, $id . '=' . $name . "\n", FILE_APPEND);
			}
			
			$renderer->doc .= '<a href="' . $link . '"><img src="' . substr(DOKU_PLUGIN, strlen($_SERVER["DOCUMENT_ROOT"])) . '/wow/images/wowhead.png" alt="Wowhead" />' . $name . '</a>';
			return true;
		}
		return false;
	}
}
?>
