Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace search_solr\check;
18
 
19
use core\check\check;
20
use core\check\result;
21
use core\output\html_writer;
22
 
23
/**
24
 * Check that the connection to Solr works.
25
 *
26
 * @package search_solr
27
 * @copyright 2024 The Open University
28
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
class connection extends check {
31
    #[\Override]
32
    public function get_name(): string {
33
        return get_string('pluginname', 'search_solr');
34
    }
35
 
36
    #[\Override]
37
    public function get_action_link(): ?\action_link {
38
        return new \action_link(
39
            new \moodle_url('/admin/settings.php', ['section' => 'searchsolr']),
40
            get_string('settings'));
41
    }
42
 
43
 
44
    #[\Override]
45
    public function get_result(): result {
46
        global $CFG;
47
 
48
        $result = result::OK;
49
        $resultstr = '';
50
        $resultdetails = '';
51
 
52
        try {
53
            // We do not use manager::instance as this will already try to connect to the engine,
54
            // we only want to do the specific get_status call below and nothing else. So use
55
            // search_engine_instance. We know it will be a Solr instance if we got here.
56
            /** @var \search_solr\engine $engine */
57
            $engine = \core_search\manager::search_engine_instance();
58
 
59
            // Get engine status.
60
            $status = $engine->get_status(5);
61
 
62
            $time = number_format($status['time'], 2) . 's';
63
            $resultstr = get_string('check_time', 'search_solr', $time);
64
        } catch (\Throwable $t) {
65
            $status = [
66
                'connected' => false,
67
                'foundcore' => false,
68
                'error' => 'Exception when creating search manager: ' . $t->getMessage(),
69
                'exception' => $t,
70
            ];
71
        }
72
 
73
        if (!$status['connected']) {
74
            // No connection at all.
75
            $result = result::ERROR;
76
            $resultstr = get_string('check_notconnected', 'search_solr');
77
            $resultdetails .= \html_writer::tag('p', s($status['error']));
78
 
79
        } else if (!$status['foundcore']) {
80
            // There's a connection, but the core doesn't seem to exist.
81
            $result = result::ERROR;
82
            $resultstr = get_string('check_nocore', 'search_solr');
83
            $resultdetails .= \html_writer::tag('p', s($status['error']));
84
 
85
        } else {
86
            // Errors related to finding the core size only show if the size warning is configured.
87
            $sizelimit = get_config('search_solr', 'indexsizelimit');
88
            if (!array_key_exists('indexsize', $status)) {
89
                if ($sizelimit) {
90
                    $result = result::ERROR;
91
                    $resultstr = get_string('check_nosize', 'search_solr');
92
                    $resultdetails .= \html_writer::tag('p', s($status['error']));
93
                }
94
            } else {
95
                // Show the index size in result, even if we aren't checking it.
96
                $sizestr = get_string(
97
                    'indexsize',
98
                    'search_solr',
99
                    display_size($status['indexsize']),
100
                );
101
                $resultdetails .= \html_writer::tag('p', $sizestr);
102
                if ($sizelimit) {
103
                    // Error at specified index size, warning at 90% of it.
104
                    $sizewarning = ($sizelimit * 9) / 10;
105
                    if ($status['indexsize'] > $sizewarning) {
106
                        if ($status['indexsize'] > $sizelimit) {
107
                            $resultstr = get_string('check_indextoobig', 'search_solr');
108
                            $result = result::ERROR;
109
                        } else {
110
                            // We don't say it's too big because it isn't yet, just show the size.
111
                            $resultstr = $sizestr;
112
                            $result = result::WARNING;
113
                        }
114
                    }
115
                }
116
            }
117
        }
118
 
119
        $ex = $status['exception'] ?? null;
120
        if ($ex) {
121
            $resultdetails .= \html_writer::tag('pre', str_replace($CFG->dirroot, '', s($ex->getTraceAsString())));
122
        }
123
 
124
        return new result($result, $resultstr, $resultdetails);
125
    }
126
}