1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats\Models; |
4
|
|
|
|
5
|
|
|
use Joomla\Database\Query\LimitableInterface; |
6
|
|
|
use Joomla\Model\AbstractDatabaseModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Statistics database model |
10
|
|
|
* |
11
|
|
|
* @since 1.0 |
12
|
|
|
*/ |
13
|
|
|
class StatsModel extends AbstractDatabaseModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The query batch size |
17
|
|
|
* |
18
|
|
|
* @var integer |
19
|
|
|
* @since 1.0 |
20
|
|
|
*/ |
21
|
|
|
private $batchSize = 25000; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Loads the statistics data from the database. |
25
|
|
|
* |
26
|
|
|
* @param string $column A single column to filter on |
27
|
|
|
* |
28
|
|
|
* @return \array[] Array of data arrays. |
29
|
|
|
* |
30
|
|
|
* @since 1.0 |
31
|
|
|
* @throws \InvalidArgumentException |
32
|
|
|
*/ |
33
|
3 |
|
public function getItems($column = null) |
34
|
|
|
{ |
35
|
3 |
|
$db = $this->getDb(); |
36
|
|
|
|
37
|
|
|
// Validate the requested column is actually in the table |
38
|
3 |
|
if ($column !== null) |
39
|
3 |
|
{ |
40
|
|
|
switch ($column) |
41
|
|
|
{ |
42
|
2 |
|
case 'php_version': |
43
|
2 |
|
case 'db_version': |
44
|
2 |
|
case 'db_type': |
45
|
2 |
|
case 'cms_version': |
46
|
2 |
|
case 'server_os': |
47
|
1 |
|
return $db->setQuery( |
48
|
1 |
|
$db->getQuery(true) |
49
|
1 |
|
->select('*') |
50
|
1 |
|
->from('#__jstats_counter_' . $column) |
51
|
1 |
|
)->loadAssocList($column); |
52
|
|
|
break; |
|
|
|
|
53
|
|
|
|
54
|
1 |
|
default: |
55
|
1 |
|
throw new \InvalidArgumentException('An invalid data source was requested.', 404); |
56
|
1 |
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// If fetching all data from the table, we need to break this down a fair bit otherwise we're going to run out of memory |
60
|
1 |
|
$totalRecords = $db->setQuery( |
61
|
1 |
|
$db->getQuery(true) |
62
|
1 |
|
->select('COUNT(unique_id)') |
63
|
1 |
|
->from('#__jstats') |
64
|
1 |
|
)->loadResult(); |
65
|
|
|
|
66
|
1 |
|
$return = []; |
67
|
|
|
|
68
|
1 |
|
$query = $db->getQuery(true) |
69
|
1 |
|
->select(['php_version', 'db_type', 'db_version', 'cms_version', 'server_os']) |
70
|
1 |
|
->from('#__jstats') |
71
|
1 |
|
->group('unique_id'); |
72
|
|
|
|
73
|
1 |
|
$limitable = $query instanceof LimitableInterface; |
74
|
|
|
|
75
|
|
|
// We can't have this as a single array, we run out of memory... This is gonna get interesting... |
76
|
1 |
|
for ($offset = 0; $offset < $totalRecords; $offset + $this->batchSize) |
77
|
|
|
{ |
78
|
|
|
if ($limitable) |
79
|
1 |
|
{ |
80
|
|
|
$query->setLimit($this->batchSize, $offset); |
81
|
|
|
|
82
|
|
|
$db->setQuery($query); |
83
|
|
|
} |
84
|
|
|
else |
85
|
|
|
{ |
86
|
1 |
|
$db->setQuery($query, $offset, $this->batchSize); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$return[] = $db->loadAssocList(); |
90
|
|
|
|
91
|
1 |
|
$offset += $this->batchSize; |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
// Disconnect the DB to free some memory |
95
|
1 |
|
$db->disconnect(); |
96
|
|
|
|
97
|
|
|
// And unset some variables |
98
|
1 |
|
unset($db, $query, $offset, $totalRecords); |
99
|
|
|
|
100
|
1 |
|
return $return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Saves the given data. |
105
|
|
|
* |
106
|
|
|
* @param \stdClass $data Data object to save. |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
* |
110
|
|
|
* @since 1.0 |
111
|
|
|
*/ |
112
|
2 |
|
public function save($data) |
113
|
|
|
{ |
114
|
2 |
|
$db = $this->getDb(); |
115
|
|
|
|
116
|
|
|
// Set the modified date of the record |
117
|
2 |
|
$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat()); |
118
|
|
|
|
119
|
|
|
// Check if a row exists for this unique ID and update the existing record if so |
120
|
2 |
|
$recordExists = $db->setQuery( |
121
|
2 |
|
$db->getQuery(true) |
122
|
2 |
|
->select('unique_id') |
123
|
2 |
|
->from('#__jstats') |
124
|
2 |
|
->where('unique_id = ' . $db->quote($data->unique_id)) |
125
|
2 |
|
)->loadResult(); |
126
|
|
|
|
127
|
|
|
if ($recordExists) |
128
|
2 |
|
{ |
129
|
1 |
|
$db->updateObject('#__jstats', $data, ['unique_id']); |
130
|
1 |
|
} |
131
|
|
|
else |
132
|
|
|
{ |
133
|
1 |
|
$db->insertObject('#__jstats', $data, ['unique_id']); |
|
|
|
|
134
|
|
|
} |
135
|
2 |
|
} |
136
|
|
|
} |
137
|
|
|
|
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.