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