1<?php
2if (!defined('DOKU_INC')) die();
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once DOKU_INC . 'inc/parser/xhtml.php';
5
6/**
7 * Auto-Tooltip DokuWiki plugin, for use with the ActionRenderer plugin. This will run only if
8 * ActionRenderer is the current renderer plugin. For improved performance, use the AutoTooltip
9 * renderer plugin instead.
10 *
11 * @license    MIT
12 * @author     Eli Fenton
13 */
14class action_plugin_autotooltip extends DokuWiki_Action_Plugin {
15	/** @type helper_plugin_autotooltip m_helper */
16	private $m_helper;
17	private $m_disable;
18
19	public function __construct() {
20		$this->m_disable = !plugin_load('renderer', 'actionrenderer');
21		if (!$this->m_disable) {
22			global $ID;
23			$this->m_helper = plugin_load('helper', 'autotooltip');
24		}
25	}
26
27	public function register(Doku_Event_Handler $controller) {
28		if ($this->m_disable) {
29			return;
30		}
31		$controller->register_hook('PLUGIN_ACTIONRENDERER_METHOD_EXECUTE', 'BEFORE', $this, 'actionrenderer');
32	}
33
34	/**
35	 * Intercept Doku_Renderer_xhtml:internallink to give every wikilink a tooltip!
36	 *
37	 * @param Doku_Event $event
38	 * @param array $param
39	 */
40	function actionrenderer(Doku_Event $event, $param) {
41		if ($event->data['method'] == 'internallink') {
42			$renderer = $event->data['renderer'];
43			$args = $event->data['arguments'];
44
45			$id = $args[0];
46			$name = $args[1] ?: 'null';
47			$search = $args[2] ?: null;
48			$returnonly = $args[3] ?: false;
49			$linktype = $args[4] ?: 'content';
50
51			global $ID;
52			if (!$this->m_helper->isExcluded($ID) && page_exists($id) && $id != $ID) {
53				$event->preventDefault();
54
55				// If we call $renderer->internallink directly here, it will cause infinite recursion,
56				// so we need this call_user_func_array hack.
57				$link = call_user_func_array(
58					array($renderer, 'parent::internallink'),
59					array($id, $name, $search, true, $linktype)
60				);
61
62				$meta = $this->m_helper->read_meta_fast($id);
63				$abstract = $meta['abstract'];
64				$link = $this->m_helper->stripNativeTooltip($link);
65				$link = $this->m_helper->forText($link, $abstract, $meta['title']);
66
67				if (!$returnonly) {
68					$renderer->doc .= $link;
69				}
70				return $link;
71			}
72		}
73	}
74}
75