-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
372 lines (302 loc) · 13 KB
/
test.php
File metadata and controls
372 lines (302 loc) · 13 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
restore_error_handler();
error_reporting(E_ALL);
ini_set('display_errors', true);
if (!defined('AREA') || !defined('CONSOLE')) {
die('Access denied');
}
if ($mode == 'providers') {
$options_bindings = [
'engine_id' => 'e|engine:<number>[,<number>]:Test only engines with specified engine_id. Accepts multiple values, comma-separated.',
'provider' => 'p|provider:<string>[,<string>]:Test randomly selected engines with specified provider. Accepts multiple values, comma-separated.\nBy default only 1 engine per provider is selected, use --limit to change this.',
'product' => 'r|product:<string>:Test only specified single product, --engine is required',
'db_host' => 'h|host:<string>:Override database host',
'debug' => 'd|debug:ALL|<string>[,<string>]:Display debug data for each http request. Accepts multiple values, comma-separated.\nAllowed values are request, response, headers, products, product, ALL',
'limit' => 'l|limit:<number>:Randomly select specified number of engines for each provider',
'logger' => 'g|logger:<callable>:Override default logger, must be valid php callable',
'count' => 'c|count:<max>|<min>,<max>:Limit randomly selected engines to those having number of products less than max or between min and max, exclusive (<, not <=)',
'noreviews' => 'n|noreviews:1:Skip retrieving reviews text for engines that support search by reviews text and have it enabled',
'testing' => 't|testing:1:For providers that perform multiple http requests only perform the first request',
'reviews' => 'w|reviews:1:Limit randomly selected engines to only having search by reviews text enabled',
'json' => 'j|json:1:Output everything as json',
'core' => 'o|core:1:Use reviews table in core db instead of engine db',
'update' => 'u|update:1:Update reviews summary after all (not available with --core)',
'force_update' => 'f|force:1:Update reviews summary even if hash already matches (requires --update, not available with --core)',
'only_update' => 'only_update:1:Update reviews summary from current data, do not fetch anything',
'function' => 'i|function:1:Call fn_fetch_product_reviews() on given engines',
];
foreach ($GLOBALS['argv'] as $arg) {
if ($arg == '--help') {
ProvidersTester::showHelp($options_bindings);
exit;
}
}
$values = ProvidersTester::parseOptions($options_bindings);
if (isset($values['engine_id'])) {
$engine_from_options = true;
$engines = explode(',', $values['engine_id']);
}
if (isset($values['provider'])) {
$provider_from_options = true;
$providers = explode(',', $values['provider']);
}
if (isset($values['limit'])) {
$limit = $values['limit'];
$limit_from_options = true;
}
$sql = 'SELECT DISTINCT reviews_provider FROM ?:engines_extra WHERE reviews_provider ' . (isset($providers) ? 'IN (?a)' : '!= ""');
$sql .= ' AND reviews_provider != "internal"';
if (isset($values['reviews'])) {
$sql .= ' AND reviews_is_search_by_reviews_enabled = "Y"';
}
$_providers = db_get_fields($sql, isset($providers) ? $providers : '');
if (!empty($providers) && empty($_providers)) {
$providers = join(', ', $providers);
exit("Invalid providers: {$providers}\n");
}
$providers = $_providers;
sort($providers);
if (!isset($values['engine_id']) && !isset($values['provider'])) {
ProvidersTester::testFailures($providers);
}
if (empty($engines)) {
$engines = [];
foreach ($providers as $p) {
if (empty($p)) {
continue;
}
$condition = "status = 'A' AND reviews_provider = '{$p}'";
if (!empty($values['reviews'])) {
$condition .= ' AND reviews_is_search_by_reviews_enabled = "Y"';
}
if (!empty($values['count'])) {
$count = explode(',', $values['count']);
if (count($count) == 1 && intval($count[0]) > 0) {
$condition .= ' AND e.products_count < ' . intval($count[0]);
} elseif (count($count) == 2) {
$count = array_map(function($x) {
return intval($x);
}, $count);
if ($count[1] > 0) {
$condition .= ' AND e.products_count BETWEEN ' . join(' AND ', $count);
}
}
}
$_limit = isset($limit) ? $limit : 1;
$_engines = db_get_fields("
SELECT
ee.engine_id
FROM
?:engines_extra ee
JOIN
?:engines e
ON
e.engine_id = ee.engine_id
WHERE
{$condition}
ORDER BY RAND()
LIMIT ?i
", $_limit
);
if (empty($_engines)) {
printf("No engines for provider %s, exit\n", $p);
exit;
}
$engines = array_merge($engines, $_engines);
}
}
foreach ($engines as $engine_id) {
if (empty($engine_id)) {
continue;
}
$engine_data = fn_get_engine_full_data($engine_id);
if (empty($engine_data)) {
printf("No data for engine %s!\n", $engine_id);
continue;
}
if (!empty($values['db_host'])) {
$engine_data['db_host'] = $values['db_host'];
}
if (!empty($values['function'])) {
Registry::set('runtime.engine_data', $engine_data);
$res = fn_fetch_product_reviews($engine_data);
printf("fn_fetch_product_reviews for %s %sUPDATED\n", $engine_data['engine_id'], $res ? '' : 'NOT ');
continue;
}
$result = [
'engine_id' => $engine_id,
'provider' => $engine_data['reviews_provider'],
'products_count' => (int)$engine_data['products_count'],
];
if (empty($values['json'])) {
$format = "Testing %s with engine %s, has %s products\n";
printf($format, $result['provider'], $result['engine_id'], $result['products_count']);
}
try {
$provider = ReviewsProvider::load($engine_data, true);
$logger = isset($values['logger']) && is_callable($values['logger'])
? $values['logger']
: function ($z) use (& $result, $values) {
empty($values['json'])
? print print_r($z, 1) . "\n"
: $result['errors'][] = $z;
};
$provider->setLogger($logger);
if (isset($values['testing'])) {
$provider->setTesting($values['testing']);
}
if (isset($values['debug'])) {
$provider->setDebug($values['debug']);
}
if (!$provider->testConnect()) {
$provider->log('Failed testConnect!');
}
empty($values['core']) ? fn_cs_switch_db($engine_data) : $provider::setDbPrefix('');
$provider::createTable();
if (!isset($values['product']) && empty($values['only_update'])) {
$started = microtime(true);
$totals_count = $provider->getAllProductReviewsTotals();
$new_hash = $provider::getTotalsHash();
$text = sprintf(
"Got %s totals in %.2fs, %s",
$totals_count,
microtime(1) - $started,
empty($new_hash) ? 'NO HASH' : 'hash ' . $new_hash . ($new_hash == $engine_data['reviews_hash'] ? ' matches' : ' != ' . $engine_data['reviews_hash'])
);
$result = array_merge($result, [
'totals_count' => $totals_count,
]);
} else {
$text = 'Skip totals';
}
if ($engine_data['reviews_is_search_by_reviews_enabled'] == 'Y' && !isset($values['noreviews']) && empty($values['only_update'])) {
$started = microtime(true);
$reviews_count = $provider->getAllProductReviews();
$text .= sprintf(", Got %s reviews in %.2fs", $reviews_count, microtime(1) - $started);
$result = array_merge($result, [
'reviews_count' => $reviews_count,
]);
}
if (isset($values['product']) && empty($values['only_update'])) {
$item_id = $values['product'];
$started = microtime(true);
$single = $provider->getProductReviewsTotals($item_id);
$result = array_merge($result, [
'single_item' => array_merge([
'id' => $item_id,
], $single),
]);
$single = array_filter($single, function ($x) {
return is_scalar($x);
});
$single = array_merge(['id' => $item_id], $single);
$text .= sprintf(", Got single: %s in %.2fs", join(" : ", $single), microtime(1) - $started);
}
if (empty($values['core']) && !empty($values['update'])) {
$res = $provider->updateSummaryReviewsTotals(!empty($values['force_update']));
$text .= "\n" . $provider->getLastMessage();
}
print(empty($values['json']) ? "$text\n\n" : json_encode($result) . "\n");
} catch (\Exception $e) {
print " ERROR: " . $e->getMessage() . "\n";
continue;
}
}
exit;
}
class ProvidersTester
{
static function testFailures($providers)
{
static::title('Test failures handling');
try {
static::loadEmptyProvider([]);
} catch (\Exception $e) {
print $e->getMessage() . "\n";
}
try {
static::loadInvalidProvider(['reviews_provider' => 'zzz']);
} catch (\Exception $e) {
print $e->getMessage() . "\n";
}
foreach ($providers as $p) {
$engine_id = db_get_field('SELECT engine_id FROM ?:engines_extra WHERE reviews_provider = ?s ORDER BY RAND() LIMIT 1', $p);
$engine_data = fn_get_engine_full_data($engine_id);
try {
static::loadInvalidRequired($engine_data);
} catch (\Exception $e) {
printf("%s\n", $e->getMessage());
}
}
static::title('Test normal operation');
}
static function title($s)
{
print "\n---------- $s ----------\n";
}
static function loadEmptyProvider($engine_data)
{
unset($engine_data['reviews_provider']);
ReviewsProvider::load($engine_data, true);
}
static function loadInvalidProvider($engine_data)
{
$engine_data['reviews_provider'] .= 'zz';
ReviewsProvider::load($engine_data, true);
}
static function loadInvalidRequired($engine_data)
{
$provider = ReviewsProvider::load($engine_data);
foreach ($provider->getRequiredEngineData() as $k) {
unset($engine_data[$k]);
return ReviewsProvider::load($engine_data, true);
}
}
static function parseOptions($options_bindings)
{
$long = $short = $map = $values = [];
foreach ($options_bindings as $var => $options) {
$type = ':';
$options = explode(':', $options);
$options = reset($options);
foreach (explode('|', $options) as $o) {
strlen($o) == 1 ? $short[] = $o . $type : $long[] = $o . $type;
$map[$o] = $var;
}
}
$options = getopt(join('', $short), $long);
foreach ($map as $option => $var) {
if (isset($options[$option])) {
$values[$var] = $options[$option];
}
}
return $values;
}
static function showHelp($options_bindings)
{
echo "\nReview providers tester usage:\n\n";
$help = [];
$max_len = 0;
foreach ($options_bindings as $var => $options) {
$keys = [];
list($options, $types, $descr) = explode(':', $options);
foreach (explode('|', $options) as $o) {
strlen($o) == 1 ? $keys[] = '-' . $o : $keys[] = '--' . $o;
}
$help[$var] = [
'descr' => $descr,
'keys' => implode(', ', $keys) . ' '. $types,
];
if (strlen($help[$var]['keys']) > $max_len) {
$max_len = strlen($help[$var]['keys']);
}
}
foreach ($help as $data) {
printf(
"%s %s\n",
str_pad($data['keys'], $max_len, ' '),
str_replace('\n', "\n".str_repeat(' ', $max_len + 1), $data['descr'])
);
}
}
}