1<?php
2/**
3 * Zotero Plugin: Links quotes to Zotero sources
4 *
5 * Syntax: \cite[Page]{ShortName}
6 *
7 * @license	GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author	Stefan Macke <me@stefan-macke.de>
9 */
10
11if (!defined('DOKU_INC'))
12{
13	define('DOKU_INC', realpath(dirname(__FILE__) . '/../../') . DIRECTORY_SEPARATOR);
14}
15if (!defined('DOKU_PLUGIN'))
16{
17	define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
18}
19require_once(DOKU_PLUGIN . 'syntax.php');
20
21require_once("ZoteroEntry.php");
22require_once("FeedZoteroRepository.php");
23require_once("TextZoteroRepository.php");
24require_once("IniZoteroConfig.php");
25require_once("WebZoteroFeedReader.php");
26
27class syntax_plugin_zotero extends DokuWiki_Syntax_Plugin
28{
29	/**
30	 * @var ZoteroRepository
31	 */
32	private $repo = null;
33
34	/**
35	 * @var ZoteroConfig
36	 */
37	private $config = null;
38
39	function getInfo()
40	{
41		return array(
42			'author' => 'Stefan Macke',
43			'email'  => 'me@stefan-macke.de',
44			'date'   => '2013-03-03',
45			'name'   => 'Zotero Plugin',
46			'desc'   => 'Links quotes to Zotero sources',
47			'url'    => 'http://blog.macke.it',
48		);
49	}
50
51	function getType()
52	{
53		return 'substition';
54	}
55
56	function getSort()
57	{
58		return 50;
59	}
60
61	function connectTo($mode)
62	{
63		$this->Lexer->addSpecialPattern('\\\cite.*?\}', $mode, 'plugin_zotero');
64	}
65
66	function handle($match, $state, $pos, Doku_Handler $handler)
67	{
68		$citeKey = "";
69		$pageRef = "";
70		$matches = array();
71		if (preg_match("/\\\cite(\[([a-zA-Z0-9 \.,\-:]*)\])?\{([a-zA-Z0-9\-:]*?)\}/", $match, $matches))
72		{
73			$pageRef = $matches[2];
74			$citeKey = $matches[3];
75		}
76		else
77		{
78			return $this->outputError("invalid citation: " . $match);
79		}
80
81		$output = "";
82		try
83		{
84			$this->config = new IniConfig();
85			$cachePage = $this->config->getCachePage();
86
87			$this->repo = new TextZoteroRepository($cachePage, $this->config);
88			$output = $this->createSourceOutput($citeKey, $pageRef);
89
90			return $this->output($output);
91		}
92		catch (Exception $e)
93		{
94			return $this->outputError($e->getMessage());
95		}
96	}
97
98	private function output($text)
99	{
100		return '<span class="ZoteroSource">' . $text. '</span>';
101	}
102
103	private function outputError($message)
104	{
105		return $this->output('<span class="error">ERROR: ' . $message . "</span>");
106	}
107
108	private function createSourceOutput($citeKey, $pageRef)
109	{
110		$parentheses = explode(",", $this->config->getConfig("WikiOutput", "parentheses"));
111		if (count($parentheses) != 2)
112		{
113			throw new ZoteroConfigException("configuration not set correctly: WikiOutput.parentheses");
114		}
115
116		$entry = $this->getZoteroEntry($citeKey);
117
118		$output = $parentheses[0] . $this->getZoteroLink($entry);
119		$output = $this->addPageRefToOutput($output, $pageRef);
120		$output .= $parentheses[1];
121
122		return $output;
123	}
124
125	private function addPageRefToOutput($output, $pageRef)
126	{
127		if ($pageRef != "")
128		{
129			if (preg_match("/^[0-9\-f\.]+$/", $pageRef))
130			{
131				if (isset($this->config['WikiOutput']['pagePrefix']))
132				{
133					$pageRef = $this->config['WikiOutput']['pagePrefix'] . $pageRef;
134				}
135			}
136			$output .= ", " . $pageRef;
137		}
138		return $output;
139	}
140
141	private function getZoteroEntry($citeKey)
142	{
143		try
144		{
145			return $this->repo->getEntryByCiteKey($citeKey);
146		}
147		catch (ZoteroEntryNotFoundException $e)
148		{
149			if ($this->config->getConfig('ZoteroAccess', 'autoupdate') == 1)
150			{
151				$cachePage = $this->config->getCachePage();
152				$feedReader = new WebZoteroFeedReader($this->config);
153				$webRepo = new FeedZoteroRepository($feedReader, $this->config);
154				$entries = $webRepo->getAllEntries();
155				$this->repo->updateAndSaveEntries($entries);
156				return $this->repo->getEntryByCiteKey($citeKey);
157			}
158			else
159			{
160				throw $e;
161			}
162		}
163	}
164
165	private function getZoteroLink(ZoteroEntry $entry)
166	{
167		return '<a href="' . $this->config->getUrlForEntry($entry) . '" title="' . htmlentities($entry->getShortInfo($format)) . '">' . htmlentities($entry->getCiteKey()) . "</a>";
168	}
169
170	function render($mode, Doku_Renderer $renderer, $data)
171	{
172		if($mode == 'xhtml')
173		{
174			$renderer->doc .= $data;
175			return true;
176		}
177		return false;
178	}
179}
180?>
181