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