16bfd3f23SEli Fenton<?php 26bfd3f23SEli Fentonif(!defined('DOKU_INC')) die(); 36bfd3f23SEli Fenton 46bfd3f23SEli Fenton/** 56bfd3f23SEli Fenton * Auto-Tooltip DokuWiki plugin 66bfd3f23SEli Fenton * 76bfd3f23SEli Fenton * @license MIT 86bfd3f23SEli Fenton * @author Eli Fenton 96bfd3f23SEli Fenton */ 106bfd3f23SEli Fentonclass helper_plugin_autotooltip extends DokuWiki_Admin_Plugin { 116bfd3f23SEli Fenton private $localRenderer; 12d39942acSEli Fenton private static $metaCache = []; 136bfd3f23SEli Fenton 146bfd3f23SEli Fenton public function __construct() { 156bfd3f23SEli Fenton $this->localRenderer = new Doku_Renderer_xhtml; 166bfd3f23SEli Fenton } 176bfd3f23SEli Fenton 186bfd3f23SEli Fenton 196bfd3f23SEli Fenton /** 209e2add7bSEli Fenton * Return methods of this helper 219e2add7bSEli Fenton * 229e2add7bSEli Fenton * @return array with methods description 239e2add7bSEli Fenton */ 249e2add7bSEli Fenton function getMethods() { 259e2add7bSEli Fenton $result = array(); 269e2add7bSEli Fenton $result[] = array( 279e2add7bSEli Fenton 'name' => 'forText', 289e2add7bSEli Fenton 'desc' => 'Manually construct a tooltip', 299e2add7bSEli Fenton 'params' => array( 309e2add7bSEli Fenton 'content' => 'string', 319e2add7bSEli Fenton 'tooltip' => 'string', 329e2add7bSEli Fenton 'title (optional)' => 'string', 339e2add7bSEli Fenton 'preTitle (optional)' => 'string', 349e2add7bSEli Fenton 'classes (optional)' => 'string', 35*b9ce7d66SEli Fenton 'textClasses (optional)' => 'string', 369e2add7bSEli Fenton ), 379e2add7bSEli Fenton 'return' => array('result' => 'string') 389e2add7bSEli Fenton ); 399e2add7bSEli Fenton $result[] = array( 409e2add7bSEli Fenton 'name' => 'forWikilink', 419e2add7bSEli Fenton 'desc' => 'Generate a tooltip from a wikilink', 429e2add7bSEli Fenton 'params' => array( 439e2add7bSEli Fenton 'id' => 'string', 449e2add7bSEli Fenton 'content (optional)' => 'string', 459e2add7bSEli Fenton 'preTitle (optional)' => 'string', 469e2add7bSEli Fenton 'classes (optional)' => 'string', 47*b9ce7d66SEli Fenton 'textClasses (optional)' => 'string', 489e2add7bSEli Fenton ), 499e2add7bSEli Fenton 'return' => array('result' => 'string') 509e2add7bSEli Fenton ); 519e2add7bSEli Fenton return $result; 529e2add7bSEli Fenton } 539e2add7bSEli Fenton 549e2add7bSEli Fenton 559e2add7bSEli Fenton /** 566bfd3f23SEli Fenton * Return a simple tooltip. 576bfd3f23SEli Fenton * 5807c401fcSEli Fenton * @param string $content - The on-page content. May contain newlines. 59948a1374SEli Fenton * @param string $tooltip - The tooltip content. Newlines will be rendered as line breaks. 60948a1374SEli Fenton * @param string $title - The title inside the tooltip. 61948a1374SEli Fenton * @param string $preTitle - Text to display before the title. Newlines will be rendered as line breaks. 626bfd3f23SEli Fenton * @param string $classes - CSS classes to add to this tooltip. 63*b9ce7d66SEli Fenton * @param string $textClasses - CSS classes to add to the linked text. 646bfd3f23SEli Fenton * @return string 656bfd3f23SEli Fenton */ 66*b9ce7d66SEli Fenton function forText($content, $tooltip, $title='', $preTitle = '', $classes = '', $textClasses = '') { 67b7bccb09SEli Fenton if (empty($classes)) { 68be213c94SEli Fenton $classes = $this->getConf('style'); 69be213c94SEli Fenton } 70b7bccb09SEli Fenton if (empty($classes)) { 71be213c94SEli Fenton $classes = 'default'; 72be213c94SEli Fenton } 734cbd2578SEli Fenton $delay = $this->getConf('delay') ?: 0; 74be213c94SEli Fenton 75be213c94SEli Fenton // Sanitize 76be213c94SEli Fenton $classes = htmlspecialchars($classes); 77be213c94SEli Fenton // Add the plugin prefix to all classes. 78be213c94SEli Fenton $classes = preg_replace('/(\w+)/', 'plugin-autotooltip__$1', $classes); 79be213c94SEli Fenton 80be213c94SEli Fenton $partCount = (empty($title) ? 0 : 1) + (empty($preTitle) ? 0 : 1) + (empty($tooltip) ? 0 : 1); 81be213c94SEli Fenton if ($partCount > 1 || strchr($tooltip, "\n") !== FALSE || strlen($tooltip) > 40) { 82be213c94SEli Fenton $classes .= ' plugin-autotooltip_big'; 836bfd3f23SEli Fenton } 846bfd3f23SEli Fenton 85*b9ce7d66SEli Fenton if (empty($textClasses)) { 86*b9ce7d66SEli Fenton $textClasses = 'plugin-autotooltip_linked'; 87969d3afcSEli Fenton if (strstr($content, '<a ') === FALSE) { 88*b9ce7d66SEli Fenton $textClasses .= ' plugin-autotooltip_simple'; 89969d3afcSEli Fenton } 90969d3afcSEli Fenton } 916bfd3f23SEli Fenton 92e4338dabSEli Fenton $contentParts = []; 93e4338dabSEli Fenton if (!empty($preTitle)) { 94948a1374SEli Fenton $contentParts[] = $this->_formatTT($preTitle); 95e4338dabSEli Fenton } 96e4338dabSEli Fenton if (!empty($title)) { 97e4338dabSEli Fenton $contentParts[] = '<span class="plugin-autotooltip-title">' . $title . '</span>'; 98e4338dabSEli Fenton } 99e4338dabSEli Fenton if (!empty($tooltip)) { 100948a1374SEli Fenton $contentParts[] = $this->_formatTT($tooltip); 101e4338dabSEli Fenton } 102e4338dabSEli Fenton 103*b9ce7d66SEli Fenton return '<span class="' . $textClasses . '" onmouseover="autotooltip.show(this)" onmouseout="autotooltip.hide()" data-delay="' . $delay . '">' . 1046bfd3f23SEli Fenton $content . 10507c401fcSEli Fenton '<span class="plugin-autotooltip-hidden-classes">' . $classes . '</span>' . 106e4338dabSEli Fenton '<span class="plugin-autotooltip-hidden-tip">' . 107e4338dabSEli Fenton implode('<br><br>', $contentParts) . 108e4338dabSEli Fenton '</span>' . 10907c401fcSEli Fenton '</span>'; 1106bfd3f23SEli Fenton } 1116bfd3f23SEli Fenton 1126bfd3f23SEli Fenton 1136bfd3f23SEli Fenton /** 1146bfd3f23SEli Fenton * Render a tooltip, with the title and abstract of a page. 1156bfd3f23SEli Fenton * 1166bfd3f23SEli Fenton * @param string $id - A page id. 117948a1374SEli Fenton * @param string $content - The on-page content. Newlines will be rendered as line breaks. Omit to use the page's title. 118948a1374SEli Fenton * @param string $preTitle - Text to display before the title in the tooltip. Newlines will be rendered as line breaks. 1196bfd3f23SEli Fenton * @param string $classes - CSS classes to add to this tooltip. 120*b9ce7d66SEli Fenton * @param string $textClasses - CSS classes to add to the linked text. 1216bfd3f23SEli Fenton * @return string 1226bfd3f23SEli Fenton */ 123*b9ce7d66SEli Fenton function forWikilink($id, $content = null, $preTitle = '', $classes = '', $textClasses = '') { 124d39942acSEli Fenton global $ID; 125d39942acSEli Fenton $id = resolve_id(getNS($ID), $id, false); 126d39942acSEli Fenton 127d39942acSEli Fenton $meta = self::read_meta_fast($id); 128d39942acSEli Fenton $title = $meta['title']; 1296bfd3f23SEli Fenton 130812ebc9aSEli Fenton $link = $this->localRenderer->internallink($id, $content ?: $title, null, true); 1316bfd3f23SEli Fenton 132d39942acSEli Fenton if (page_exists(preg_replace('/\#.*$/', '', $id))) { 1334cbd2578SEli Fenton $link = $this->stripNativeTooltip($link); 134*b9ce7d66SEli Fenton return $this->forText($link, $meta['abstract'], $title, $preTitle, $classes, $textClasses); 1356bfd3f23SEli Fenton } 1366bfd3f23SEli Fenton else { 1376bfd3f23SEli Fenton return $link; 1386bfd3f23SEli Fenton } 1396bfd3f23SEli Fenton } 1406bfd3f23SEli Fenton 1416bfd3f23SEli Fenton 1426bfd3f23SEli Fenton /** 1434cbd2578SEli Fenton * Strip the native title= tooltip from an anchor tag. 1444cbd2578SEli Fenton * 1454cbd2578SEli Fenton * @param string $link 1464cbd2578SEli Fenton * @return string 1474cbd2578SEli Fenton */ 1484cbd2578SEli Fenton function stripNativeTooltip($link) { 1494cbd2578SEli Fenton return preg_replace('/title="[^"]*"/', '', $link); 1504cbd2578SEli Fenton } 1514cbd2578SEli Fenton 1524cbd2578SEli Fenton 1534cbd2578SEli Fenton /** 154d39942acSEli Fenton * Reads specific metadata about 10x faster than p_get_metadata. p_get_metadata only uses caching for the current 155d39942acSEli Fenton * page, and uses the very slow php serialization. However, in a wiki with infrequently accessed pages, it's 156d39942acSEli Fenton * extremely slow. 157d39942acSEli Fenton * 158d39942acSEli Fenton * @param string $id 159d39942acSEli Fenton * @return array - An array containing 'title' and 'abstract.' 160d39942acSEli Fenton */ 161d39942acSEli Fenton static function read_meta_fast($id) { 162d39942acSEli Fenton global $ID; 163d39942acSEli Fenton $id = resolve_id(getNS($ID), preg_replace('/\#.*$/', '', $id), true); 164d39942acSEli Fenton 165d39942acSEli Fenton if (isset(self::$metaCache[$id])) { 166d39942acSEli Fenton return self::$metaCache[$id]; 167d39942acSEli Fenton } 168d39942acSEli Fenton 169*b9ce7d66SEli Fenton $results = [ 170*b9ce7d66SEli Fenton 'title' => p_get_metadata(cleanID($id), 'title'), 171*b9ce7d66SEli Fenton 'abstract' => p_get_metadata(cleanID($id), 'plugin_description keywords') ?: p_get_metadata(cleanID($id), 'description abstract') 172*b9ce7d66SEli Fenton ]; 173d39942acSEli Fenton 174d39942acSEli Fenton // By default, the abstract starts with the title. Remove it so it's not displayed twice, but still fetch 175d39942acSEli Fenton // both pieces of metadata, in case another plugin rewrote the abstract. 176d39942acSEli Fenton $results['abstract'] = preg_replace( 177d39942acSEli Fenton '/^' . self::_pregEscape($results['title']) . '(\r?\n)+/', 178d39942acSEli Fenton '', 179d39942acSEli Fenton $results['abstract'] 180d39942acSEli Fenton ); 181d39942acSEli Fenton 182d39942acSEli Fenton self::$metaCache[$id] = $results; 183d39942acSEli Fenton return $results; 184d39942acSEli Fenton } 185d39942acSEli Fenton 186d39942acSEli Fenton 187d39942acSEli Fenton /** 1886bfd3f23SEli Fenton * Format tooltip text. 1896bfd3f23SEli Fenton * 1906bfd3f23SEli Fenton * @param string $tt - Tooltip text. 1916bfd3f23SEli Fenton * @return string 1926bfd3f23SEli Fenton */ 1936bfd3f23SEli Fenton private function _formatTT($tt) { 194948a1374SEli Fenton // Convert double-newlines into vertical space. 195948a1374SEli Fenton $tt = preg_replace('/(\r?\n){2,}/', '<br><br>', $tt); 196948a1374SEli Fenton // Single newlines get collapsed, just like in HTML. 197948a1374SEli Fenton return preg_replace('/(\r?\n)/', ' ', $tt); 1986bfd3f23SEli Fenton } 199d852dc10SEli Fenton 200d852dc10SEli Fenton 201d852dc10SEli Fenton /** 202d852dc10SEli Fenton * Escape a string for inclusion in a regular expression, assuming forward slash is used as the delimiter. 203d852dc10SEli Fenton * 204d852dc10SEli Fenton * @param string $r - The regex string, without delimiters. 205d852dc10SEli Fenton * @return string 206d852dc10SEli Fenton */ 207d39942acSEli Fenton private static function _pregEscape($r) { 208d852dc10SEli Fenton return preg_replace('/\//', '\\/', preg_quote($r)); 209d852dc10SEli Fenton } 2106bfd3f23SEli Fenton} 211