1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
<?php
/**
* globalsearch.php - controller to perform global search operations and provide settings.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Thomas Hackl <thomas.hackl@uni-passau.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 4.1
*/
class GlobalSearchController extends AuthenticatedController
{
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
if (in_array($action, ['settings', 'saveconfig'])) {
$GLOBALS['perm']->check('root');
}
}
/**
* Perform search in all registered modules for the given search term.
*/
public function find_action($limit)
{
$limit = min(100, (int)$limit);
// Perform search by mysqli (=async) or by PDO (=sync)?
$async = Config::get()->GLOBALSEARCH_ASYNC_QUERIES
&& extension_loaded('mysqli');
if ($async) {
// throw exceptions on mysqli error
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ERROR;
}
// Now load all modules
$modules = GlobalSearchModule::getActiveSearchModules();
$search = trim(Request::get('search'));
$filter = json_decode(Request::get('filters'), true);
$result = [];
foreach ($modules as $className) {
$partSQL = $className::getSQL($search, $filter, $limit);
// No valid sql? Leave.
if (!$partSQL) {
continue;
}
// Global config setting says to use mysqli
if ($async) {
$mysqli = new mysqli($GLOBALS['DB_STUDIP_HOST'], $GLOBALS['DB_STUDIP_USER'],
$GLOBALS['DB_STUDIP_PASSWORD'], $GLOBALS['DB_STUDIP_DATABASE']);
mysqli_set_charset($mysqli, 'UTF8');
if ($mysqli->multi_query($partSQL . '; SELECT FOUND_ROWS() as found_rows;')) {
do {
if ($res = $mysqli->store_result()) {
$all_links[$className][] = $res->fetch_all(MYSQLI_ASSOC);
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
}
$entries = $all_links[$className][0];
$entries_count = (int)$all_links[$className][1][0]['found_rows'];
// Global config setting calls for PDO
} else {
$entries = DBManager::get()->fetchAll($partSQL);
$entries_count_array = DBManager::get()->fetchAll('SELECT FOUND_ROWS() as found_rows');
$entries_count = (int)$entries_count_array[0]['found_rows'];
}
// No results? Leave.
if (!is_array($entries)) {
continue;
}
// Walk through results
$found = [];
foreach ($entries as $one) {
// Filter item and add to result if necessary.
if ($item = $className::filter($one, $search)) {
$found[] = $item;
}
}
// Nothing found? Leave.
if (count($found) === 0) {
continue;
}
$result[$className] = [
'name' => $className::getName(),
'fullsearch' => $className::getSearchURL($search),
'content' => $found,
// If we found more results than needed, indicate a "more" link
// for full search.
'more' => count($found) > Config::get()->GLOBALSEARCH_MAX_RESULT_OF_TYPE,
// If there are more results than our arbitrary LIMIT, a plus
// ('+') should be shown besides the category result count
'plus' => count($found) < $entries_count,
];
}
GlobalSearchModule::clearCache();
// Sort
$positions = array_flip($modules);
uksort($result, function($a, $b) use ($positions) {
return $positions[$a] - $positions[$b];
});
// Send me an answer
$this->render_json($result);
}
/**
* Provide a GUI for configuring the search module order and other settings.
*/
public function settings_action()
{
PageLayout::setTitle(_('Globale Suche: Einstellungen'));
Navigation::activateItem('/admin/config/globalsearch');
$this->config = Config::get()->GLOBALSEARCH_MODULES;
$this->modules = [];
foreach ($this->config as $className => $config) {
if (class_exists($className)) {
$this->modules[$className] = new $className();
}
}
// Search declared classes for GlobalSearchModules
foreach (get_declared_classes() as $className) {
if (is_subclass_of($className, 'GlobalSearchModule')) {
// Add new classes at module array end and not activated.
if (!isset($this->modules[$className])) {
$this->modules[$className] = new $className();
}
}
}
}
/**
* Saves the set values to global configuration.
*/
public function saveconfig_action()
{
CSRFProtection::verifyUnsafeRequest();
$config = [];
foreach (Request::getArray('modules') as $module) {
$config[$module['class']] = [
'active' => !empty($module['active']),
'fulltext' => is_a($module['class'], 'GlobalSearchFulltext', true) && !empty($module['fulltext'])
];
}
Config::get()->store('GLOBALSEARCH_ASYNC_QUERIES', Request::int('async_queries', 0));
Config::get()->store('GLOBALSEARCH_MAX_RESULT_OF_TYPE', Request::int('entries_per_type', 3));
Config::get()->store('GLOBALSEARCH_MODULES', $config);
PageLayout::postSuccess(_('Die Einstellungen wurden gespeichert.'));
$this->redirect('globalsearch/settings');
}
}
|