1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\qa\Cache; |
4
|
|
|
|
5
|
|
|
use Drupal\qa\BaseControl; |
6
|
|
|
|
7
|
|
|
class Size extends BaseControl { |
8
|
|
|
const DATA_SIZE_LIMIT = 524288; // Memcache default entry limit: 1024*1024 * 0.5 for safety |
9
|
|
|
const DATA_SUMMARY_LENGTH = 1024; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param string $bin_name |
13
|
|
|
* |
14
|
|
|
* @return array |
15
|
|
|
* - name: the name of the checked bin |
16
|
|
|
* - status: 0 for KO, 1 for OK |
17
|
|
|
* - result: information in case of failed check. |
18
|
|
|
*/ |
19
|
|
|
function checkBin($bin_name) { |
|
|
|
|
20
|
|
|
$ret = array( |
21
|
|
|
'name' => $bin_name, |
22
|
|
|
); |
23
|
|
|
$ret['status'] = FALSE; |
24
|
|
|
$arg = array('@name' => $bin_name); |
25
|
|
|
|
26
|
|
|
if (!db_table_exists($bin_name)) { |
|
|
|
|
27
|
|
|
$ret['result'] = t('Bin @name is missing in the database.', $arg); |
|
|
|
|
28
|
|
|
return $ret; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$sql = "SELECT cid, data, expire, created, serialized FROM {$bin_name} ORDER BY cid"; |
32
|
|
|
$q = db_query($sql); |
|
|
|
|
33
|
|
|
if (!$q) { |
34
|
|
|
$ret['result'] = t('Failed fetching database data for bin @name.', $arg); |
35
|
|
|
return $ret; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
list($status, $result) = $this->checkBinContents($q); |
39
|
|
|
|
40
|
|
|
$ret['status'] = $status; |
41
|
|
|
$ret['result'] = $result; |
42
|
|
|
|
43
|
|
|
return $ret; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Check the contents of an existing and accessible bin. |
48
|
|
|
* |
49
|
|
|
* @param $q |
50
|
|
|
* The DBTNG query object for the bin contents, already queried. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
* - 0 : status bool |
54
|
|
|
* - 1 : result array |
55
|
|
|
*/ |
56
|
|
|
protected function checkBinContents($q) { |
57
|
|
|
$status = TRUE; |
58
|
|
|
$result = array(); |
59
|
|
|
foreach ($q->fetchAll() as $row) { |
60
|
|
|
// Cache drivers will need to serialize anyway. |
61
|
|
|
$data = $row->serialized ? $row->data : serialize($row->data); |
62
|
|
|
$len = strlen($data); |
63
|
|
|
if ($len == 0 || $len >= static::DATA_SIZE_LIMIT) { |
64
|
|
|
$status = FALSE; |
65
|
|
|
$result[] = array( |
66
|
|
|
$row->cid, |
67
|
|
|
number_format($len, 0, ',', ''), |
68
|
|
|
check_plain(drupal_substr($data, 0, static::DATA_SUMMARY_LENGTH)) . '…', |
|
|
|
|
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return array($status, $result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc] |
78
|
|
|
*/ |
79
|
|
|
public function init() { |
80
|
|
|
$this->package_name = __NAMESPACE__; |
81
|
|
|
$this->title = t('Suspicious cache content'); |
|
|
|
|
82
|
|
|
$this->description = t('Look for empty or extra-long (>= 1 MB) cache content.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Does the passed schema match the expected cache schema structure ? |
87
|
|
|
* |
88
|
|
|
* @param array $schema |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public static function isSchemaCache(array $schema) { |
93
|
|
|
$reference_schema_keys = array( |
94
|
|
|
'cid', |
95
|
|
|
'created', |
96
|
|
|
'data', |
97
|
|
|
'expire', |
98
|
|
|
'serialized' |
99
|
|
|
); |
100
|
|
|
$keys = array_keys($schema['fields']); |
101
|
|
|
sort($keys); |
102
|
|
|
$ret = $keys == $reference_schema_keys; |
103
|
|
|
|
104
|
|
|
return $ret; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public static function getAllBins($rebuild = FALSE) { |
108
|
|
|
$schema = drupal_get_complete_schema($rebuild); |
|
|
|
|
109
|
|
|
$ret = array(); |
110
|
|
|
foreach ($schema as $name => $info) { |
111
|
|
|
if (static::isSchemaCache($info)) { |
112
|
|
|
$ret[] = $name; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
sort($ret); |
116
|
|
|
|
117
|
|
|
return $ret; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
function run() { |
|
|
|
|
121
|
|
|
$pass = parent::run(); |
122
|
|
|
$bins = self::getAllBins(TRUE); |
123
|
|
|
foreach ($bins as $bin_name) { |
124
|
|
|
$pass->record($this->checkBin($bin_name)); |
125
|
|
|
} |
126
|
|
|
$pass->life->end(); |
127
|
|
|
|
128
|
|
|
if ($pass->status) { |
129
|
|
|
$pass->result = format_plural(count($bins), '1 bin checked, not containing suspicious values', |
|
|
|
|
130
|
|
|
'@count bins checked, none containing suspicious values', array()); |
131
|
|
|
} |
132
|
|
|
else { |
133
|
|
|
$info = format_plural(count($bins), '1 view checked and containing suspicious values', |
134
|
|
|
'@count bins checked, @bins containing suspicious values', array( |
135
|
|
|
'@bins' => count($pass->result), |
136
|
|
|
)); |
137
|
|
|
|
138
|
|
|
// Prepare for theming |
139
|
|
|
$result = array(); |
140
|
|
|
// @XXX May be inconsistent with non-BMP strings ? |
141
|
|
|
uksort($pass->result, 'strcasecmp'); |
142
|
|
|
foreach ($pass->result as $bin_name => $bin_report) { |
143
|
|
|
foreach ($bin_report as $entry) { |
144
|
|
|
array_unshift($entry, $bin_name); |
145
|
|
|
$result[] = $entry; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
$header = array( |
149
|
|
|
t('Bin'), |
|
|
|
|
150
|
|
|
t('CID'), |
151
|
|
|
t('Length'), |
152
|
|
|
t('Beginning of data'), |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$build = array( |
156
|
|
|
'info' => array( |
157
|
|
|
'#markup' => $info, |
158
|
|
|
), |
159
|
|
|
'list' => array( |
160
|
|
|
'#markup' => '<p>' . t('Checked: @checked', array('@checked' => implode(', ', $bins))) . "</p>\n", |
161
|
|
|
), |
162
|
|
|
'table' => array( |
163
|
|
|
'#theme' => 'table', |
164
|
|
|
'#header' => $header, |
165
|
|
|
'#rows' => $result, |
166
|
|
|
) |
167
|
|
|
); |
168
|
|
|
$pass->result = drupal_render($build); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
return $pass; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.