Skip to content

Commit

Permalink
Fix warnings in search_counter
Browse files Browse the repository at this point in the history
Right now the website breaks if the .json files are missing, or they
contain broken data. Log these cases to see how frequent they are.
  • Loading branch information
flodolo committed Aug 19, 2016
1 parent b3be110 commit 37b5b0b
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions app/inc/search_counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,51 @@

use Json\Json;

// Create a JSON file logging locale/number of requests
$json_data = new Json;

// Create a JSON file logging locale/number of requests
$local_filename = CACHE_PATH . 'stats_locales.json';
$stats = $json_data
->setURI($local_filename)
->fetchContent();
if (file_exists($local_filename)) {
$stats = $json_data
->setURI($local_filename)
->fetchContent();
} else {
$stats = [];
}

// Save JSON only if PHP was able to read the existing stats
if (is_array($stats)) {
$stats[$locale] = array_key_exists($locale, $stats) ? $stats[$locale] += 1 : 1;
$json_data->saveFile($stats, $local_filename);
} else {
$logger->addError('stats_locales.json exists but couldn\'t extract a valid array from JSON');
}

$stats[$locale] = array_key_exists($locale, $stats) ? $stats[$locale] += 1 : 1;
$json_data->saveFile($stats, $local_filename);

// Create a JSON file logging search options to determine if some are unused
$local_filename = CACHE_PATH . 'stats_requests.json';
$stats = $json_data
->setURI($local_filename)
->fetchContent();
if (file_exists($local_filename)) {
$stats = $json_data
->setURI($local_filename)
->fetchContent();
} else {
$stats = [];
}

foreach ($check as $k => $v) {
if (in_array($k, $search->getFormCheckboxes()) && $v == 1) {
$stats[$k] = array_key_exists($k, $stats) ? $stats[$k] += 1 : 1;
}
// Save JSON only if PHP was able to read the existing stats
if (is_array($stats)) {
foreach ($check as $k => $v) {
if (in_array($k, $search->getFormCheckboxes()) && $v == 1) {
$stats[$k] = array_key_exists($k, $stats) ? $stats[$k] += 1 : 1;
}

if (in_array($k, array_diff($search->getFormSearchOptions(), $search->getFormCheckboxes()))) {
$stats[$v] = array_key_exists($v, $stats) ? $stats[$v] += 1 : 1;
}
if (in_array($k, array_diff($search->getFormSearchOptions(), $search->getFormCheckboxes()))) {
$stats[$v] = array_key_exists($v, $stats) ? $stats[$v] += 1 : 1;
}

$json_data->saveFile($stats, $local_filename);
$json_data->saveFile($stats, $local_filename);
}
} else {
$logger->addError('stats_requests.json exists but couldn\'t extract a valid array from JSON');
}
unset($stats);

0 comments on commit 37b5b0b

Please sign in to comment.