|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* (c) Ruben Dorado <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace SiteAnalyzer; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
/** |
|
12
|
|
|
* class HitDAO |
|
13
|
|
|
* |
|
14
|
|
|
* @package SiteAnalyzer |
|
15
|
|
|
* @author Ruben Dorado <[email protected]> |
|
16
|
|
|
* @copyright 2018 Ruben Dorado |
|
17
|
|
|
* @license http://www.opensource.org/licenses/MIT The MIT License |
|
18
|
|
|
*/ |
|
19
|
|
|
class HitDAO |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/* |
|
23
|
|
|
* @param $pdo \PDO |
|
24
|
|
|
* @param $config Configuration |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function checkHitTable($pdo, $config) { |
|
27
|
|
|
try { |
|
28
|
|
|
$db_hit_table = $config->getHitTableName(); |
|
29
|
|
|
$stmt = $pdo->prepare("SELECT * FROM $db_hit_table WHERE 1==0"); |
|
30
|
|
|
$stmt->execute(); |
|
31
|
|
|
} catch (Exception $e) { |
|
32
|
|
|
return false; |
|
33
|
|
|
} |
|
34
|
|
|
return true; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/* |
|
38
|
|
|
* @param $pdo \PDO |
|
39
|
|
|
* @param $config Configuration |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function countHit($pdo, $config, $id, $options = []) { |
|
|
|
|
|
|
43
|
|
|
$db_hit_table = $config->getHitTableName(); |
|
44
|
|
|
$db_url_table = $config->getUrlTableName(); |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$stmt = $pdo->prepare("UPDATE $db_hit_table SET count = count + 1 WHERE id = ?"); |
|
48
|
|
|
$stmt->execute([$id]); |
|
49
|
|
|
if ($stmt->rowCount()==0) { |
|
50
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_hit_table (id, count) VALUES (?, 1)"); |
|
51
|
|
|
$stmt->execute([$id]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$stmt = $pdo->prepare("UPDATE $db_url_table SET count = count + 1 WHERE id = ? and url = ?"); |
|
55
|
|
|
$stmt->execute([$id, $url]); |
|
|
|
|
|
|
56
|
|
|
if ($stmt->rowCount()==0) { |
|
57
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)"); |
|
58
|
|
|
$stmt->execute([$id, $url]); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/* |
|
63
|
|
|
* @param $pdo \PDO |
|
64
|
|
|
* @param $config Configuration |
|
65
|
|
|
* |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getAllHits($pdo, $config) { |
|
68
|
|
|
$resp = []; |
|
69
|
|
|
try { |
|
70
|
|
|
$dbtable = $config->getHitTableName(); |
|
71
|
|
|
$stmt = $pdo->prepare("SELECT id,count FROM $dbtable"); |
|
72
|
|
|
if ($stmt->execute()) { |
|
73
|
|
|
while ($row = $stmt->fetch()) { |
|
74
|
|
|
$resp[] = [$row['id'], $row['count']]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} catch (Exception $e) { |
|
79
|
|
|
throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
return $resp; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.