| 1 | <?php |
||
| 36 | class StatisticsRepository |
||
| 37 | { |
||
| 38 | /** |
||
| 39 | * Fetches must popular search keys words from the table tx_solr_statistics |
||
| 40 | * |
||
| 41 | * @param int $rootPageId |
||
| 42 | * @param int $days number of days of history to query |
||
| 43 | * @param int $limit |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | public function getSearchStatistics($rootPageId, $days = 30, $limit = 10) |
||
| 47 | { |
||
| 48 | $now = time(); |
||
| 49 | $timeStart = (int) ($now - 86400 * intval($days)); // 86400 seconds/day |
||
| 50 | $rootPageId = (int) $rootPageId; |
||
| 51 | $limit = (int) $limit; |
||
| 52 | |||
| 53 | $statisticsRows = $this->getDatabase()->exec_SELECTgetRows( |
||
| 54 | 'keywords, count(keywords) as count, num_found as hits', |
||
| 55 | 'tx_solr_statistics', |
||
| 56 | 'tstamp > ' . $timeStart . ' AND root_pid = ' . $rootPageId, |
||
| 57 | 'keywords, num_found', |
||
| 58 | 'count DESC, hits DESC, keywords ASC', |
||
| 59 | $limit |
||
| 60 | ); |
||
| 61 | |||
| 62 | $statisticsRows = $this->mergeRowsWithSameKeyword($statisticsRows); |
||
|
1 ignored issue
–
show
|
|||
| 63 | |||
| 64 | $sumCount = $statisticsRows['sumCount']; |
||
| 65 | foreach ($statisticsRows as $statisticsRow) { |
||
| 66 | $sumCount += $statisticsRow['count']; |
||
| 67 | } |
||
| 68 | |||
| 69 | $statisticsRows = array_map(function ($row) use ($sumCount) { |
||
| 70 | $row['percent'] = $row['count'] * 100 / $sumCount; |
||
| 71 | |||
| 72 | return $row; |
||
| 73 | }, $statisticsRows); |
||
| 74 | |||
| 75 | return $statisticsRows; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Find Top search keywords with results |
||
| 80 | * |
||
| 81 | * @param int $rootPageId |
||
| 82 | * @param int $days number of days of history to query |
||
| 83 | * @param int $limit |
||
| 84 | * @return array |
||
| 85 | */ |
||
| 86 | public function getTopKeyWordsWithHits($rootPageId, $days = 30, $limit = 10) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Find Top search keywords without results |
||
| 93 | * |
||
| 94 | * @param int $rootPageId |
||
| 95 | * @param int $days number of days of history to query |
||
| 96 | * @param int $limit |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | public function getTopKeyWordsWithoutHits($rootPageId, $days = 30, $limit = 10) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Find Top search keywords with or without results |
||
| 106 | * |
||
| 107 | * @param int $rootPageId |
||
| 108 | * @param int $days number of days of history to query |
||
| 109 | * @param int $limit |
||
| 110 | * @param bool $withoutHits |
||
| 111 | * @return array |
||
| 112 | */ |
||
| 113 | protected function getTopKeyWordsWithOrWithoutHits($rootPageId, $days = 30, $limit, $withoutHits) |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * This method groups rows with the same term and different cound and hits |
||
| 146 | * and calculates the average. |
||
| 147 | * |
||
| 148 | * @param array $statisticsRows |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | protected function mergeRowsWithSameKeyword(array $statisticsRows) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get number of queries over time |
||
| 176 | * |
||
| 177 | * @param int $rootPageId |
||
| 178 | * @param int $days number of days of history to query |
||
| 179 | * @param int $bucketSeconds Seconds per bucket |
||
| 180 | * @return array [labels, data] |
||
| 181 | */ |
||
| 182 | public function getQueriesOverTime($rootPageId, $days = 30, $bucketSeconds = 3600) |
||
| 197 | |||
| 198 | |||
| 199 | /** |
||
| 200 | * This method is used to get an average value from merged statistic rows. |
||
| 201 | * |
||
| 202 | * @param array $mergedRow |
||
| 203 | * @param array $statisticsRow |
||
| 204 | * @return float|int |
||
| 205 | */ |
||
| 206 | protected function getAverageFromField(array &$mergedRow, array $statisticsRow, $fieldName) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @return \TYPO3\CMS\Core\Database\DatabaseConnection |
||
| 223 | */ |
||
| 224 | protected function getDatabase() |
||
| 228 | } |
||
| 229 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.