1<?php 2 3use ComboStrap\ExceptionCombo; 4use ComboStrap\MarkupRef; 5use ComboStrap\Mime; 6use ComboStrap\Page; 7 8require_once(__DIR__ . '/../ComboStrap/PluginUtility.php'); 9 10 11/** 12 * Ajax search data 13 */ 14class action_plugin_combo_search extends DokuWiki_Action_Plugin 15{ 16 17 const CALL = "combo-search"; 18 19 20 /** 21 * @param Doku_Event_Handler $controller 22 */ 23 function register(Doku_Event_Handler $controller) 24 { 25 26 /** 27 * The ajax api to return data 28 */ 29 $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'searchAjaxHandler'); 30 31 } 32 33 /** 34 * @param Doku_Event $event 35 * Adapted from callQSearch in Ajax.php 36 */ 37 function searchAjaxHandler(Doku_Event $event) 38 { 39 40 $call = $event->data; 41 if ($call !== self::CALL) { 42 return; 43 } 44 //no other ajax call handlers needed 45 $event->stopPropagation(); 46 $event->preventDefault(); 47 48 49 /** 50 * Shared check between post and get HTTP method 51 */ 52 $query = $_GET["q"]; 53 if ($query === null) { 54 /** 55 * With {@link TestRequest} 56 * for instance 57 */ 58 $query = $_REQUEST["q"]; 59 } 60 if (empty($query)) return; 61 62 63 $query = urldecode($query); 64 65 $inTitle = useHeading('navigation'); 66 $pages = ft_pageLookup($query, true, $inTitle); 67 $count = count($pages); 68 if (!$count) { 69 \ComboStrap\HttpResponse::create(\ComboStrap\HttpResponse::STATUS_ALL_GOOD) 70 ->sendMessage(["No pages found"]); 71 return; 72 } 73 74 $maxElements = 50; 75 if ($count > $maxElements) { 76 array_splice($pages, 0, $maxElements); 77 } 78 79 $data = []; 80 foreach ($pages as $id => $title) { 81 $page = Page::createPageFromId($id); 82 $linkUtility = MarkupRef::createFromPageId($id); 83 try { 84 $html = $linkUtility->toAttributes()->toHtmlEnterTag("a") . $page->getTitleOrDefault() . "</a>"; 85 } catch (ExceptionCombo $e) { 86 $html = "Unable to render the link for the page ($id). Error: {$e->getMessage()}"; 87 } 88 $data[] = $html; 89 } 90 $dataJson = json_encode($data); 91 \ComboStrap\HttpResponse::create(\ComboStrap\HttpResponse::STATUS_ALL_GOOD) 92 ->send($dataJson, Mime::JSON); 93 94 } 95 96 97} 98