<?php
/**
* Indexes content of entries for quick fixes using a configuration file.
*
* @param string $config_file Path to the configuration file.
* @param array $entries Array of entries to index. Each entry should be an associative array with a 'content' key.
* @return array Indexed entries.
*/
function indexEntries(string $config_file, array $entries): array
{
// Load configuration.
$config = loadConfig($config_file);
if (empty($entries)) {
return []; // Return empty array if no entries provided.
}
// Create an index array.
$index = [];
foreach ($entries as $entry) {
if (isset($entry['content']) && is_string($entry['content'])) {
$content = strtolower($entry['content']); // Convert to lowercase for case-insensitive indexing.
$terms = explode(' ', trim($content)); // Split content into terms.
foreach ($terms as $term) {
if (strlen($term) > 0) {
if (!isset($index[$term])) {
$index[$term] = [];
}
if (!in_array(array_key_exists($entry['id'], $index[$term]), $index[$term])) {
$index[$term][] = $entry['id'];
}
}
}
}
}
return $index;
}
/**
* Loads configuration from a file.
*
* @param string $file_path Path to the configuration file.
* @return array Configuration data.
*/
function loadConfig(string $file_path): array
{
if (file_exists($file_path)) {
$config = json_decode(file_get_contents($file_path), true);
if (json_last_error() !== JSON_ERROR_NONE) {
error_log("Error decoding config file: " . json_last_error_msg());
return [];
}
return $config;
} else {
error_log("Config file not found: " . $file_path);
return [];
}
}
// Example usage (for testing):
/*
$entries = [
['id' => 1, 'content' => 'This is a test entry.'],
['id' => 2, 'content' => 'Another test entry with some different words.'],
['id' => 3, 'content' => 'This is a test entry.'],
];
$config_file = 'config.json'; // Create a config.json file
// Create a dummy config.json file for testing
$config = [
'index_word_length_min' => 2,
'index_word_length_max' => 10,
];
file_put_contents($config_file, json_encode($config));
$index = indexEntries($config_file, $entries);
print_r($index);
*/
?>
Add your comment