/
API.php
96 lines (88 loc) · 2.73 KB
/
API.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
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
* @version $Id$
*
* @category Piwik_Plugins
* @package Piwik_DBStats
*/
/**
*
* @package Piwik_DBStats
*/
class Piwik_DBStats_API
{
static private $instance = null;
static public function getInstance()
{
if (self::$instance == null)
{
self::$instance = new self;
}
return self::$instance;
}
public function getDBStatus()
{
Piwik::checkUserIsSuperUser();
$configDb = Zend_Registry::get('config')->database->toArray();
// we decode the password. Password is html encoded because it's enclosed between " double quotes
$configDb['password'] = htmlspecialchars_decode($configDb['password']);
if(!isset($configDb['port']))
{
// before 0.2.4 there is no port specified in config file
$configDb['port'] = '3306';
}
$link = mysql_connect($configDb['host'], $configDb['username'], $configDb['password']);
$status = mysql_stat($link);
mysql_close($link);
return $status;
}
public function getTableStatus($table, $field = '')
{
Piwik::checkUserIsSuperUser();
$db = Zend_Registry::get('db');
// http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
$tables = $db->fetchAll("SHOW TABLE STATUS LIKE ". $db->quote($table));
if(!isset($tables[0])) {
throw new Exception('Error, table or field not found');
}
if ($field == '')
{
return $tables[0];
}
else
{
return $tables[0][$field];
}
}
public function getAllTablesStatus()
{
Piwik::checkUserIsSuperUser();
$db = Zend_Registry::get('db');
// http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html
$tablesPiwik = Piwik::getTablesInstalled();
$total = array('Name' => 'Total', 'Data_length' => 0, 'Index_length' => 0, 'Rows' => 0);
$table = array();
foreach($tablesPiwik as $tableName)
{
$t = $this->getTableStatus($tableName);
$total['Data_length'] += $t['Data_length'];
$total['Index_length'] += $t['Index_length'];
$total['Rows'] += $t['Rows'];
$t['Total_length'] = Piwik::getPrettySizeFromBytes($t['Index_length']+$t['Data_length']);
$t['Data_length'] = Piwik::getPrettySizeFromBytes($t['Data_length']);
$t['Index_length'] = Piwik::getPrettySizeFromBytes($t['Index_length']);
$t['Rows'] = Piwik::getPrettySizeFromBytes($t['Rows']);
$table[] = $t;
}
$total['Total_length'] = Piwik::getPrettySizeFromBytes($total['Data_length']+$total['Index_length']);
$total['Data_length'] = Piwik::getPrettySizeFromBytes($total['Data_length']);
$total['Index_length'] = Piwik::getPrettySizeFromBytes($total['Index_length']);
$total['TotalRows'] = Piwik::getPrettySizeFromBytes($total['Rows']);
$table['Total'] = $total;
return $table;
}
}