1<?php
2
3use ComboStrap\ExceptionBadArgument;
4use ComboStrap\ExceptionCompile;
5use ComboStrap\ExecutionContext;
6use ComboStrap\FileSystems;
7use ComboStrap\HttpResponseStatus;
8use ComboStrap\Json;
9use ComboStrap\LogUtility;
10use ComboStrap\LinkMarkup;
11use ComboStrap\Mime;
12use ComboStrap\PluginUtility;
13use ComboStrap\Search;
14use ComboStrap\Site;
15use ComboStrap\SiteConfig;
16use ComboStrap\Sqlite;
17use ComboStrap\StringUtility;
18
19require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
20
21
22/**
23 *
24 *
25 * This action enhance the search of page in:
26 * linkwiz: the editor toolbar action
27 *
28 * The entry point is in the {@link Ajax::callLinkwiz()}
29 * function
30 *
31 */
32class action_plugin_combo_linkwizard extends DokuWiki_Action_Plugin
33{
34
35    const CONF_ENABLE_ENHANCED_LINK_WIZARD = "enableEnhancedLinkWizard";
36    const CANONICAL = "linkwizard";
37    const CALL = "linkwiz";
38    const MINIMAL_WORD_LENGTH = 3;
39
40
41    /**
42     * @param Doku_Event_Handler $controller
43     */
44    function register(Doku_Event_Handler $controller)
45    {
46
47        /**
48         * https://www.dokuwiki.org/devel:event:search_query_pagelookup
49         */
50        $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'BEFORE', $this, 'linkWizard', array());
51
52
53    }
54
55
56    /**
57     * Modify the returned pages
58     * The {@link callLinkWiz} of inc/Ajax.php do
59     * just a page search with {@link ft_pageLookup()}
60     * https://www.dokuwiki.org/search
61     * @param Doku_Event $event
62     * @param $params
63     * The path are initialized in {@link init_paths}
64     * @return void
65     */
66    function linkWizard(Doku_Event $event, $params)
67    {
68
69        global $INPUT;
70
71        $postCall = $INPUT->post->str('call');
72        if ($postCall !== self::CALL) {
73            return;
74        }
75
76        if (SiteConfig::getConfValue(self::CONF_ENABLE_ENHANCED_LINK_WIZARD, 1) === 0) {
77            return;
78        }
79
80
81        $searchTerm = $event->data["id"]; // yes id is the search term
82        $pages = Search::getPages($searchTerm);
83
84
85        /**
86         * Adapted from {@link Ajax::callLinkwiz()}
87         * because link-wizard delete the pages in the same namespace and shows the group instead
88         * Breaking the flow
89         */
90
91        global $lang;
92        if (!count($pages)) {
93            ExecutionContext::getActualOrCreateFromEnv()
94                ->response()
95                ->setStatus(HttpResponseStatus::ALL_GOOD)
96                ->setBody("<div>" . $lang['nothingfound'] . "</div>", Mime::getHtml())
97                ->end();
98            return;
99        }
100
101        // output the found data
102        $even = 1;
103        $html = "";
104        $lowerSearchTerm = strtolower($searchTerm);
105        foreach ($pages as $page) {
106            try {
107                $id = $page->getWikiId();
108            } catch (ExceptionBadArgument $e) {
109                LogUtility::internalError("The selection should return wiki path", self::CANONICAL, $e);
110                continue;
111            }
112            $path = $page->getPathObject()->toAbsoluteId();
113            /**
114             * The name is the label that is put
115             * punt in the markup link
116             * We set it in lowercase then.
117             * Nobody want uppercase letter in their link label
118             */
119            $name = strtolower($page->getNameOrDefault());
120            $title = $page->getTitleOrDefault();
121            $h1 = $page->getH1OrDefault();
122            $even *= -1; //zebra
123            $link = wl($id);
124            $evenOrOdd = (($even > 0) ? 'even' : 'odd');
125            $label = null;
126            if (strpos(strtolower($title), $lowerSearchTerm) !== false) {
127                $label = "Title: $title";
128            }
129            if ($label === null && strpos(strtolower($h1), $lowerSearchTerm) !== false) {
130                $label = "H1: $h1";
131            } else {
132                $label = "Title: $title";
133            }
134            $class = "";
135            if (!FileSystems::exists($page->getPathObject())) {
136                $errorClass = LinkMarkup::getHtmlClassNotExist();
137                $class = "class=\"$errorClass\"";
138            }
139            /**
140             * Because path is used in the title to create the link
141             * by {@link file linkwiz.js} we set a title on the span
142             */
143            $html .= <<<EOF
144<div class="$evenOrOdd">
145   <a href="$link" title="$path" $class>$path</a><span title="$label">$name</span>
146</div>
147EOF;
148        }
149        ExecutionContext::getActualOrCreateFromEnv()
150            ->response()
151            ->setStatus(HttpResponseStatus::ALL_GOOD)
152            ->setBody($html, Mime::getHtml())
153            ->end();
154
155    }
156
157
158}
159