-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathDatabaseStatistics.php
More file actions
130 lines (120 loc) · 3.62 KB
/
Copy pathDatabaseStatistics.php
File metadata and controls
130 lines (120 loc) · 3.62 KB
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\ServerInfo;
use OCP\DB\Exception;
use OCP\IConfig;
use OCP\IDBConnection;
class DatabaseStatistics {
protected IConfig $config;
protected IDBConnection $connection;
public function __construct(IConfig $config, IDBConnection $connection) {
$this->config = $config;
$this->connection = $connection;
}
/**
* @return array{type: string, version: string, size: string}
*/
public function getDatabaseStatistics(): array {
return [
'type' => $this->config->getSystemValueString('dbtype'),
'version' => $this->databaseVersion(),
'size' => $this->databaseSize(),
];
}
protected function databaseVersion(): string {
switch ($this->config->getSystemValue('dbtype')) {
case 'sqlite':
case 'sqlite3':
$sql = 'SELECT sqlite_version() AS version';
break;
case 'oci':
$sql = 'SELECT VERSION FROM PRODUCT_COMPONENT_VERSION';
break;
case 'mysql':
case 'pgsql':
default:
$sql = 'SELECT VERSION() AS version';
break;
}
try {
$result = $this->connection->executeQuery($sql);
$version = $result->fetchColumn();
$result->closeCursor();
if ($version) {
return $this->cleanVersion($version);
}
} catch (Exception $e) {
}
return 'N/A';
}
/**
* Copy of phpBB's get_database_size()
* @link https://github.com/phpbb/phpbb/blob/release-3.1.6/phpBB/includes/functions_admin.php#L2908-L3043
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*/
protected function databaseSize(): string {
$database_size = false;
// This code is heavily influenced by a similar routine in phpMyAdmin 2.2.0
switch ($this->config->getSystemValue('dbtype')) {
case 'mysql':
$mysqlEngine = ['MyISAM', 'InnoDB', 'Aria'];
$db_name = $this->config->getSystemValue('dbname');
$sql = 'SHOW TABLE STATUS FROM `' . $db_name . '`';
$result = $this->connection->executeQuery($sql);
$database_size = 0;
while ($row = $result->fetch()) {
if (isset($row['Engine']) && in_array($row['Engine'], $mysqlEngine)) {
$database_size += $row['Data_length'] + $row['Index_length'];
}
}
$result->closeCursor();
break;
case 'sqlite':
case 'sqlite3':
if (file_exists($this->config->getSystemValue('dbhost'))) {
$database_size = filesize($this->config->getSystemValue('dbhost'));
} else {
$params = $this->connection->getInner()->getParams();
if (file_exists($params['path'])) {
$database_size = filesize($params['path']);
}
}
break;
case 'pgsql':
$database = $this->config->getSystemValueString('dbname');
$sql = 'SELECT pg_database_size(:dbname) as size';
$result = $this->connection->executeQuery($sql, ['dbname' => $database]);
$database_size = $result->fetchOne();
$result->closeCursor();
break;
case 'oci':
$sql = 'SELECT SUM(bytes) as dbsize
FROM user_segments';
$result = $this->connection->executeQuery($sql);
$database_size = ($row = $result->fetchColumn()) ? (int)$row : false;
$result->closeCursor();
break;
}
return ($database_size !== false) ? (string)$database_size : 'N/A';
}
/**
* Try to strip away additional information
*
* @param string $version E.g. `5.6.27-0ubuntu0.14.04.1`
* @return string `5.6.27`
*/
protected function cleanVersion(string $version): string {
$matches = [];
preg_match('/^(\d+)(\.\d+)(\.\d+)/', $version, $matches);
if (isset($matches[0])) {
return $matches[0];
}
return $version;
}
}