Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
PdoMysql
100.00% covered (success)
100.00%
54 / 54
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 search
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
1 / 1
9
 setRelevanceRanking
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * phpMyFAQ MySQL (PDO_MYSQL) search classes.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2025-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2025-02-12
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Search\Database;
21
22use Exception;
23use phpMyFAQ\Configuration;
24use phpMyFAQ\Search\SearchDatabase;
25
26/**
27 * Class PdoMysql
28 *
29 * @package phpMyFAQ\Search\Database
30 */
31class PdoMysql extends SearchDatabase implements DatabaseInterface
32{
33    /**
34     * Constructor.
35     */
36    public function __construct(Configuration $configuration)
37    {
38        parent::__construct($configuration);
39        $this->relevanceSupport = true;
40    }
41
42    /**
43     * Prepares the search and executes it.
44     *
45     * @param  string $searchTerm Search term
46     * @throws Exception
47     */
48    #[\Override]
49    public function search(string $searchTerm): mixed
50    {
51        if (is_numeric($searchTerm) && (bool) $this->configuration->get(item: 'search.searchForSolutionId')) {
52            return parent::search($searchTerm);
53        }
54
55        $relevance = (bool) $this->configuration->get(item: 'search.enableRelevance');
56        $orderBy = '';
57        $columns = $this->getResultColumns();
58        if ($this->relevanceSupport && $relevance) {
59            $columns .= ', ' . $this->setRelevanceRanking($searchTerm);
60            $orderBy = 'ORDER BY score DESC';
61        }
62        if (!$this->relevanceSupport || !$relevance) {
63            $orderBy = '';
64        }
65
66        $chars = [
67            "\xe2\x80\x98",
68            "\xe2\x80\x99",
69            "\xe2\x80\x9c",
70            "\xe2\x80\x9d",
71            "\xe2\x80\x93",
72            "\xe2\x80\x94",
73            "\xe2\x80\xa6",
74        ];
75        $replace = ["'", "'", '"', '"', '-', '--', '...'];
76        $searchTerm = str_replace($chars, $replace, $searchTerm);
77        $query = sprintf(
78            "
79                SELECT
80                    %s
81                FROM 
82                    %s %s %s
83                WHERE
84                    MATCH (%s) AGAINST ('%s' IN BOOLEAN MODE)
85                    %s
86                    %s",
87            $columns,
88            $this->getTable(),
89            $this->getJoinedTable(),
90            $this->getJoinedColumns(),
91            $this->getMatchingColumns(),
92            $this->configuration->getDb()->escape($searchTerm),
93            $this->getConditions(),
94            $orderBy,
95        );
96        $this->resultSet = $this->configuration->getDb()->query($query);
97        // Fallback for searches with less than three characters
98        if (false !== $this->resultSet && 0 === $this->configuration->getDb()->numRows($this->resultSet)) {
99            $query = sprintf(
100                '
101                    SELECT
102                        %s
103                    FROM 
104                        %s %s %s
105                    WHERE
106                        %s
107                        %s',
108                $this->getResultColumns(),
109                $this->getTable(),
110                $this->getJoinedTable(),
111                $this->getJoinedColumns(),
112                $this->getMatchClause($searchTerm),
113                $this->getConditions(),
114            );
115
116            $this->resultSet = $this->configuration->getDb()->query($query);
117        }
118
119        return $this->resultSet;
120    }
121
122    /**
123     * Add the matching columns into the columns for the relevance ranking
124     */
125    public function setRelevanceRanking(string $searchTerm): string
126    {
127        return sprintf(
128            "MATCH (%s) AGAINST ('%s' IN BOOLEAN MODE) as score",
129            $this->getMatchingColumns(),
130            $this->configuration->getDb()->escape($searchTerm),
131        );
132    }
133}