-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchLogger.hooks.php
112 lines (99 loc) · 2.68 KB
/
SearchLogger.hooks.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
<?php
/**
* Curse Inc.
* Search Logger
* Search Logger Hooks
*
* @author Alexia E. Smith
* @copyright (c) 2014 Curse Inc.
* @license GPL v3.0
* @package Search Logger
* @link https://github.com/HydraWiki/SearchLogger
*
**/
class SearchLoggerHooks {
/**
* Setups and Modifies Database Information
*
* @access public
* @param object [Optional] DatabaseUpdater Object
* @return boolean true
*/
static public function onLoadExtensionSchemaUpdates($updater = null) {
$extDir = __DIR__;
$updater->addExtensionUpdate(['addTable', 'search_log', "{$extDir}/install/sql/searchlogger_table_search_log.sql", true]);
$updater->addExtensionUpdate(['modifyField', 'search_log', 'sid', "{$extDir}/upgrade/sql/search_log/modify_sid_auto_increment.sql", true]);
return true;
}
/**
* Catching searches for logging.
*
* @access public
* @param object SpecialSearch
* @param object OutputPage
* @param string Transformed Search Term
* @return boolean True
*/
static public function onSpecialSearchResultsPrepend($searchPage, $output, $query) {
$db = wfGetDB(DB_MASTER);
$searchTerm = trim(mb_strtolower($query, 'UTF-8'));
if (!empty($searchPage->getRequest()->getVal('go'))) {
$searchMethod = 'go';
} elseif (!empty($searchPage->getRequest()->getVal('fulltext'))) {
$searchMethod = 'fulltext';
} elseif (!empty($searchPage->getRequest()->getVal('suggest'))) {
$searchMethod = 'ajax';
} else {
$searchMethod = 'other';
}
$db->insert(
'search_log',
[
'search_term' => $searchTerm,
'search_method' => $searchMethod,
'timestamp' => time()
],
__METHOD__
);
return true;
}
/**
* Return the top searchs by rank for the given time period.
*
* @access public
* @param integer [Optional] Start Timestamp, Unix Style
* @param integer [Optional] End Timestamp, Unix Style
* @return array Top search terms by rank in descending order. [[rank, term], [rank, term]]
*/
static public function getTopSearchTerms($startTimestamp = null, $endTimestamp = null, $limit = null) {
if ($startTimestamp === null) {
$startTimestamp = 0;
}
if ($endTimestamp === null) {
$endTimestamp = time();
}
$options = [
'GROUP BY' => 'search_term',
'ORDER BY' => 'total DESC'
];
if ($limit !== null) {
$options['LIMIT'] = intval($limit);
}
$db = wfGetDB(DB_SLAVE);
$result = $db->select(
['search_log'],
['count(*) as total', 'search_term'],
[
"timestamp > ".$db->strencode($startTimestamp),
"timestamp < ".$db->strencode($endTimestamp)
],
__METHOD__,
$options
);
$terms = [];
while ($row = $result->fetchRow()) {
$terms[] = [$row['total'], $row['search_term']];
}
return $terms;
}
}