1<?php 2/** 3 * Partial Search Plugin 4 * 5 * @author David Roy <davidroyapp@gmail.com> 6 */ 7 8if(!defined('DOKU_INC')) die(); 9 10if(!defined('WILDCARD')) define ('WILDCARD', '*'); 11require_once DOKU_INC.'inc/fulltext.php'; 12require_once DOKU_INC.'inc/utf8.php'; 13 14class action_plugin_partialsearch extends DokuWiki_Action_Plugin { 15 16 function register(Doku_Event_Handler $controller) { 17 $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'BEFORE', $this, 'partial_search_before'); 18 $controller->register_hook('SEARCH_QUERY_FULLPAGE', 'AFTER', $this, 'partial_search_after'); 19 $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'BEFORE', $this, 'pagelookup_before'); 20 $controller->register_hook('FULLTEXT_SNIPPET_CREATE', 'AFTER', $this, 'snippet_create_after'); 21 } 22 23 function partial_search_before(&$event, $args) { 24 global $conf; 25 26 if ($this->getConf('enablepartialsearch')) { 27 $this->_partial_search($event, $args, WILDCARD); 28 } 29 } 30 31 function partial_search_after(&$event, $args) { 32 global $conf; 33 34 if ($this->getConf('enablepartialsearch')) { 35 $this->_partial_search($event, $args, ''); 36 } 37 38 if ($this->getConf('enablesearchlookupsnippet')) { 39 $data= array(); 40 $data['id']= $event->data['query']; 41 $data['in_ns']= true; 42 $data['in_title']= true; 43 $data['after']= null; 44 $data['before']= null; 45 $data['has_titles']= true; // for plugin backward compatibility check 46 $pageLookup= _ft_pageLookup($data); 47 foreach($pageLookup as $key=>$value){ 48 if (!isset($event->result[$key])){ 49 $event->result[$key]= $this->getLang('titlehas'); //if assign value 0 then it won't generate snippet 50 } 51 } 52 } 53 } 54 55 function _partial_search(&$event, $args, $surrounding='') { 56 $arr= explode(' ', $event->data['query']); 57 array_walk($arr, function(&$value) use ($surrounding) { $value = $surrounding . trim($value, WILDCARD) . $surrounding; }); 58 $event->data['query']= implode(' ', $arr); 59 } 60 61 function pagelookup_before(&$event, $args) { 62 global $conf; 63 64 if ($this->getConf('enablesearchlookupsnippet') && $event->canPreventDefault){ 65 $event->stopPropagation(); 66 $event->preventDefault(); 67 $event->result=[]; // Empty page lookup result because they've been included in QUERY_FULLPAGE 68 } 69 } 70 71 function snippet_create_after(&$event, $args) { 72 global $conf; 73 74 //$id, $text, $highlight, $snippet 75 extract($event->data, EXTR_REFS); 76 77 if ($this->getConf('addtitletosnippet')) { 78 $title = p_get_first_heading($id); 79 if (isset($title) && trim($title)!==''){ 80 $snippet = $title . '<br/>' . $snippet; 81 } 82 } 83 84 if ($this->getConf('enablesearchlookupsnippet') && (!isset($snippet) || trim($snippet)==='')) { 85 $snippet = utf8_substr($text, 0, 250); 86 } 87 } 88 89} 90