-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
show_folder_size.php
178 lines (147 loc) · 4.67 KB
/
show_folder_size.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
declare(strict_types=1);
include __DIR__ . '/lib/vendor/autoload.php';
use Jfcherng\Roundcube\Plugin\ShowFolderSize\AbstractRoundcubePlugin;
final class show_folder_size extends AbstractRoundcubePlugin
{
/**
* {@inheritdoc}
*/
public $task = 'mail';
/**
* {@inheritdoc}
*/
public $actions = [
'get-folder-size' => 'getFolderSizeAction',
];
/**
* {@inheritdoc}
*/
public $hooks = [];
/**
* {@inheritdoc}
*/
public function init(): void
{
parent::init();
$this->add_texts($this->localizationDir, true);
// only shown in the main "mail" page
if ($this->rcmail->action === '') {
$this->include_stylesheet("{$this->skinPath}/main.css");
$this->include_script('assets/main.min.js');
$this->addPluginButtons();
}
}
/**
* {@inheritdoc}
*/
public function getDefaultPluginPreferences(): array
{
return [];
}
/**
* Handler for plugin's "get-folder-size" action.
*/
public function getFolderSizeAction(): void
{
/** @var rcmail_output_json */
$output = $this->rcmail->output;
// sanitize: _callback
$callback = filter_input(\INPUT_POST, '_callback');
$sizes = $this->getFolderSizes();
$callback && $output->command($callback, $sizes);
$output->send();
}
/**
* Add plugin buttons.
*/
private function addPluginButtons(): void
{
$this->add_buttons_mailboxoptions([
[
'type' => 'link-menuitem',
'label' => "{$this->ID}.show_folder_size",
'class' => 'show-folder-size active',
'href' => '#',
'command' => "plugin.{$this->ID}.show-data",
],
]);
}
/**
* Get size for all folders.
*
* @return array an array in the form of [
* folder_1 => [size_1, cumulative_1],
* ... ]
*/
private function getFolderSizes(): array
{
$storage = $this->rcmail->get_storage();
$folders = array_unique($storage->list_folders());
/** @var array<string,int> $rawSizes the non-cumulative folder sizes */
$rawSizes = [];
foreach ($folders as $folder) {
$rawSizes[$folder] = $storage->folder_size($folder);
}
$cumulativeSizes = $this->calcualteCumulativeSizes($rawSizes);
$ret = [];
foreach ($folders as $folder) {
$ret[$folder] = [$rawSizes[$folder], $cumulativeSizes[$folder]];
}
return $ret;
}
/**
* Calculates cumulative folder sizes from raw sizes.
*
* @param array $rawSizes the raw sizes
*
* @return array<string,int> the cumulative folder sizes
*/
private function calcualteCumulativeSizes(array $rawSizes): array
{
/** @var string[] $folders sorted folder names by name length ascending */
$folders = array_map('strval', array_keys($rawSizes));
usort($folders, static function (string $folder1, string $folder2): int {
return \strlen($folder1) <=> \strlen($folder2);
});
/** @var array<string,string[]> $children folder => its child folders */
$children = [];
for ($i = \count($folders) - 1; $i >= 0; --$i) {
$child = $folders[$i];
for ($j = $i; $j >= 0; --$j) {
$parent = $folders[$j];
if ($this->isFolderParentAndChild($parent, $child, true)) {
$children[$parent] = $children[$parent] ?? [];
$children[$parent][] = $child;
}
}
}
/** @var array<string,int> $sumSizes the cumulative folder sizes */
$sumSizes = [];
foreach ($folders as $folder) {
$sumSizes[$folder] = 0;
foreach ($children[$folder] as $child) {
$sumSizes[$folder] += $rawSizes[$child];
}
}
return $sumSizes;
}
/**
* Determine if folders are in a parent-child relationship.
*
* @param string $parent the parent
* @param string $child the child
* @param bool $sameIsTrue return true if folders are the same
*
* @return bool true if parent and child folder, false otherwise
*/
private function isFolderParentAndChild(string $parent, string $child, bool $sameIsTrue = false): bool
{
static $delimiter = '/';
if ($parent === $child) {
return $sameIsTrue;
}
$parent .= $delimiter;
return substr($child, 0, \strlen($parent)) === $parent;
}
}