-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPodium.php
283 lines (243 loc) · 11 KB
/
Podium.php
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
require "PodiumModule.php";
require "PodiumFulltext.php";
/**
* Podium.php
*
* Quickaccess anything
*
* @author Florian Bieringer <florian.bieringer@uni-passau.de>
*/
class Podium extends StudIPPlugin implements SystemPlugin
{
const SHOW_ALL_AVATARS = false;
const MAX_RESULT_OF_TYPE = 6;
private static $types = array();
public function __construct()
{
parent::__construct();
/* Init html and js */
bindtextdomain('podium', __DIR__ . '/locale');
self::addStylesheet('/assets/style.less', array("media" => "all"));
PageLayout::addScript($this->getPluginURL() . '/assets/podium.js');
PageLayout::addBodyElements('<div id="podiumwrapper">
<div id="podium">
<div id="podiuminput">
<input type="text" placeholder="' . dgettext('podium', 'Suchbegriff') . '">
<div id="podiumclose">
'.Assets::img('icons/64/blue/decline.png').'
</div>
</div>
<ul id="podiumlist"></ul>
<div class="podium_help">
<dl>
<dt>' . dgettext('podium', '[STRG] + [Leertaste]') . '</dt>
<dd>' . dgettext('podium', 'Tastenkombination zum Öffnen und Schließen') . '</dd>
<dt>' . dgettext('podium', '[ALT] oder Klick auf Überschrift') . '</dt>
<dd>' . dgettext('podium', 'Erweitert die ausgewählte Suchkategorie. Bei einem weiteren Klick wird an die entsprechende Vollsuche weitergeleitet.') . '</dd>
<dt>' . dgettext('podium', 'Dateisuche') . '</dt>
<dd>' . dgettext('podium', 'Die Dateisuche kann über einen Schrägstrich (/) verfeinert werden. Beispiel: "Meine Veranstaltung/Datei" zeigt alle Dateien die das Wort "Datei" enthalten und in "Meine Veranstaltung" sind an. Die Veranstaltung kann auch auf einen Teil (z.B. Veran/Datei) oder auf die Großbuchstaben bzw. auch deren Abkürzung (z.B. MV/Datei oder V/Datei) beschränkt werden.') . '</dd>
<dt>' . dgettext('podium', 'Platzhalter') . '</dt>
<dd>' . dgettext('podium', '_ ist Platzhalter für ein beliebiges Zeichen.') . '</dd>
<dd>' . dgettext('podium', '% ist Platzhalter für beliebig viele Zeichen.') . '</dd>
<dd>' . dgettext('podium', 'Me_er findet Treffer für Me<mark>y</mark>er und Me<mark>i</mark>er. M__er findet zusätzlich auch M<mark>ay</mark>er und M<mark>ai</mark>er. M%er findet alle vorherigen Treffer aber auch M<mark>ünchn</mark>er.') . '</dd>
</dl>
</div>
</div>
</div>');
/* Add podium icon */
PageLayout::addBodyElements(Assets::img('icons/16/white/search.png', array('id' => 'podiumicon')));
/* Add podium navigation */
try {
Navigation::addItem('/admin/podium', new AutoNavigation(dgettext('podium', 'Podium'), PluginEngine::GetURL($this, array(), 'settings/modules')));
Navigation::addItem('/admin/podium/modules', new AutoNavigation(dgettext('podium', 'Module'), PluginEngine::GetURL($this, array(), 'settings/modules')));
Navigation::addItem('/admin/podium/buzzword', new AutoNavigation(dgettext('podium', 'Stichworte'), PluginEngine::GetURL($this, array(), 'settings/buzzwords')));
Navigation::addItem('/admin/podium/faillog', new AutoNavigation(dgettext('podium', 'Erfolglose Suchen'), PluginEngine::GetURL($this, array(), 'settings/faillog')));
} catch (InvalidArgumentException $e) {
}
}
/**
* Adds an PodiumModule to the search (must implement PodiumModule)
* @param $class Your search class
*/
public static function registerPodiumModule($class)
{
if (in_array($class::getPodiumId(), Config::get()->PODIUM_FULLTEXT_MODULES)) {
$search = 'getFulltextSearch';
} else {
$search = 'getPodiumSearch';
}
self::addType($class::getPodiumId(), $class::getPodiumName(), array($class, $search), array($class, 'podiumFilter'));
}
/**
* Add a type to podium
*
* @param $index string typeindexname
* @param $name string typename
* @param $sql function Callback to retrieve the sql
* @param $filter function Callback to get array formated result
*/
public static function addType($index, $name, $sql, $filter)
{
self::$types[$index] = array(
'name' => $name,
'sql' => $sql,
'filter' => $filter);
}
/**
* Removes a type from podium
*
* @param $index typeindexname
*/
public static function removeType($index)
{
unset(self::$types[$index]);
}
public static function getTypes()
{
self::loadDefaultModules();
return self::$types;
}
private static function loadDefaultModules()
{
foreach (glob(__DIR__ . '/models/*.php') as $file) {
require_once $file;
Podium::registerPodiumModule(basename($file, '.php'));
}
}
/**
* Kickoff function to start query
*/
public function find_action()
{
// Now load all modules
Podium::loadDefaultModules();
$search = trim(studip_utf8decode(Request::get('search')));
$sql = "";
$result = array();
$types = self::$types;
foreach ($types as $id => $type) {
if (self::isActiveModule($id)) {
$partSQL = $type['sql']($search);
if ($partSQL) {
$new = mysqli_connect($GLOBALS['DB_STUDIP_HOST'], $GLOBALS['DB_STUDIP_USER'], $GLOBALS['DB_STUDIP_PASSWORD'], $GLOBALS['DB_STUDIP_DATABASE']);
$new->query($type['sql']($search), MYSQLI_ASYNC);
$new->podiumid = $id;
$all_links[] = $new;
}
}
}
$time = microtime(1);
$read = $error = $reject = array();
while (count($read) + count($error) + count($reject) < count($all_links)) {
// Parse all links
$error = $reject = $read = $all_links;
// Poll will reject connection that have no query running
mysqli_poll($read, $error, $reject, 1);
foreach ($read as $r) {
if ($r && $set = $r->reap_async_query()) {
$id = $r->podiumid;
// If you wanna benchmark the results
if (Request::submitted("benchmark")) {
var_dump($id,microtime(1)-$time);
}
while ($data = $set->fetch_assoc()) {
if (sizeof($result[$id]['content']) < self::MAX_RESULT_OF_TYPE) {
$arg = $data['type'] && count($data) == 2 ? $data['id'] : $data;
if ($item = $types[$id]['filter']($arg, $search)) {
$result[$id]['name'] = $types[$id]['name'];
$result[$id]['content'][] = $item;
}
}
}
}
}
}
// Sort
$result = array_filter(array_merge(Config::get()->PODIUM_MODULES_ORDER, $result));
// Write faillog if required
if (!$result && Config::get()->PODIUM_FAILLOG) {
DBManager::get()->execute("INSERT INTO podium_faillog (input, count) VALUES (?, 1) ON DUPLICATE KEY UPDATE count=count+1", array($search));
}
// Send me an answer
echo json_encode(studip_utf8encode($result));
die;
}
/**
* Function to mark a querystring in a resultstring
*
* @param $string
* @param $query
* @param bool|true $filename
* @return mixed
*/
public static function mark($string, $query, $longtext = false, $filename = true)
{
// Secure
$string = htmlReady($string);
if (strpos($query, '/') !== FALSE) {
$args = explode('/', $query);
if ($filename) {
return self::mark($string, trim($args[1]));
}
return self::mark($string, trim($args[0]));
} else {
$query = trim($query);
}
// Replace direct string
$result = preg_replace("/$query/i", "<mark>$0</mark>", $string, -1, $found);
if ($found) {
// Check for overlength
if ($longtext && strlen($result) > 200) {
$start = max(array(0, stripos($result, '<mark>') - 20));
$space = stripos($result, ' ', $start);
$start = $space < $start + 20 ? $space : $start;
return substr($result, $start, 200);
}
return $result;
}
// Replace camelcase
$replacement = "$" . (++$i);
foreach (str_split(strtoupper($query)) as $letter) {
$queryletter[] = "($letter)";
$replacement .= "<mark>$" . ++$i . "</mark>$" . ++$i;
}
$pattern = "/([\w\W]*)" . join('([\w\W]*)', $queryletter) . "/";
$result = preg_replace($pattern, $replacement, $string, -1, $found);
if ($found) {
return $result;
}
return $string;
}
public static function isActiveModule($moduleId)
{
return !in_array($moduleId, Config::get()->PODIUM_MODULES);
}
public static function isFulltextModule($moduleId) {
$re = new ReflectionClass($moduleId);
return $re->implementsInterface("PodiumFulltext");
}
public static function isActiveFulltextModule($moduleId) {
return in_array($moduleId, Config::get()->PODIUM_FULLTEXT_MODULES);
}
public static function getModule($name) {
$modules = self::getTypes();
return $modules[$name]['sql'][0];
}
public function benchmark_action() {
require "models/PodiumUser.php";
$n = PodiumUser::getPodiumSearch(Request::get("test"));
$f = PodiumUser::getFulltextSearch(Request::get("test"));
$ns = DBManager::get()->prepare($n);
$fs = DBManager::get()->prepare($f);
$time = microtime(1);
$ns->execute();
var_dump(microtime(1)-$time);
$time = microtime(1);
$fs->execute();
var_dump(microtime(1)-$time);
var_dump($ns->fetchAll());
var_dump($fs->fetchAll());
die;
}
}