xref: /plugin/combo/action/linkwizard.php (revision 4cadd4f8c541149bdda95f080e38a6d4e3a640ca)
1<?php
2
3use ComboStrap\ExceptionCombo;
4use ComboStrap\Json;
5use ComboStrap\LogUtility;
6use ComboStrap\PluginUtility;
7use ComboStrap\Sqlite;
8use ComboStrap\StringUtility;
9
10require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
11
12
13/**
14 * Set the home of the web site documentation
15 */
16class action_plugin_combo_linkwizard extends DokuWiki_Action_Plugin
17{
18
19    const CONF_ENABLE_ENHANCED_LINK_WIZARD = "enableEnhancedLinkWizard";
20    const CANONICAL = "linkwizard";
21
22    /**
23     * @param Doku_Event_Handler $controller
24     */
25    function register(Doku_Event_Handler $controller)
26    {
27
28        /**
29         * https://www.dokuwiki.org/devel:event:search_query_pagelookup
30         */
31        $controller->register_hook('SEARCH_QUERY_PAGELOOKUP', 'AFTER', $this, 'searchPage', array());
32
33    }
34
35
36    /**
37     * Modify the returned pages
38     * The {@link callLinkWiz} of inc/Ajax.php do
39     * just a page search with {@link ft_pageLookup()}
40     * https://www.dokuwiki.org/search
41     * @param Doku_Event $event
42     * @param $params
43     * The path are initialized in {@link init_paths}
44     * @return void
45     */
46    function searchPage(Doku_Event $event, $params)
47    {
48        global $INPUT;
49        /**
50         * linkwiz is the editor toolbar action
51         * qsearch is the search button
52         */
53        $postCall = $INPUT->post->str('call');
54        if (!(in_array($postCall, ["linkwiz", "qsearch", action_plugin_combo_search::CALL]))) {
55            return;
56        }
57        if (PluginUtility::getConfValue(self::CONF_ENABLE_ENHANCED_LINK_WIZARD, 1) === 0) {
58            return;
59        }
60        $sqlite = Sqlite::createOrGetSqlite();
61        if ($sqlite === null) {
62            return;
63        }
64
65        $searchTerm = $event->data["id"]; // yes id is the search term
66        $minimalWordLength = 3;
67        if (strlen($searchTerm) < $minimalWordLength) {
68            return;
69        }
70        $searchTermWords = StringUtility::getWords($searchTerm);
71        if (sizeOf($searchTermWords) === 0) {
72            return;
73        }
74        $sqlParameters = [];
75        $sqlPredicates = [];
76        foreach ($searchTermWords as $searchTermWord) {
77            if (strlen($searchTermWord) < $minimalWordLength) {
78                continue;
79            }
80            $pattern = "%$searchTermWord%";
81            $sqlParameters = array_merge([$pattern, $pattern, $pattern, $pattern, $pattern], $sqlParameters);
82            $sqlPredicates[] = "(id like ? COLLATE NOCASE or H1 like ? COLLATE NOCASE or title like ? COLLATE NOCASE or name like ? COLLATE NOCASE or path like ? COLLATE NOCASE)";
83        }
84        $sqlPredicate = implode(" and ", $sqlPredicates);
85        $searchTermSql = <<<EOF
86select id as "id", title as "title" from pages where $sqlPredicate order by id ASC;
87EOF;
88        $rows = [];
89        $request = $sqlite
90            ->createRequest()
91            ->setQueryParametrized($searchTermSql, $sqlParameters);
92        try {
93            $rows = $request
94                ->execute()
95                ->getRows();
96        } catch (ExceptionCombo $e) {
97            LogUtility::msg("Error while trying to retrieve a list of page", LogUtility::LVL_MSG_ERROR, self::CANONICAL);
98        } finally {
99            $request->close();
100        }
101
102        foreach ($rows as $row) {
103            $event->result[$row["id"]] = $row["title"];
104        }
105
106
107    }
108
109
110}
111