public function DatabaseSanitize::getUnspecifiedTables in Database Sanitize 8
Gets a list of tables in the database not specified in sanitize YML files.
Parameters
string $yml_file_path: Optional parameter, the YML file path.
Return value
array The list of tables not specified in sanitize YAML files.
Throws
\Exception
File
- src/
DatabaseSanitize.php, line 178
Class
- DatabaseSanitize
- Class DatabaseSanitize.
Namespace
Drupal\database_sanitizeCode
public function getUnspecifiedTables($yml_file_path = NULL) {
if ($yml_file_path) {
if (!file_exists($yml_file_path)) {
throw new \Exception("File does not exist {$yml_file_path}");
}
$file_content = file_get_contents($yml_file_path);
}
else {
$file_content = $this
->getDatabaseSanitizeYmlFileContent();
}
// Get a list of all tables on the database.
$db_tables = \Drupal::database()
->query('show tables')
->fetchCol();
if (empty($file_content)) {
return $db_tables;
}
try {
$parsed_file = Yaml::parse($file_content);
} catch (ParseException $exception) {
$message = $exception
->getMessage();
$this->logger
->error("Unable to parse the sanitize YAML file. @message", [
'@message' => $message,
]);
return $db_tables;
}
if (is_null($parsed_file) || !array_key_exists('sanitize', $parsed_file)) {
$this->logger
->error("The 'sanitize' key is not defined");
return $db_tables;
}
if (empty($parsed_file['sanitize'])) {
return $db_tables;
}
$yml_tables = [];
foreach ($parsed_file['sanitize'] as $machine_name => $tables) {
foreach ($tables as $table_name => $definition) {
if (is_array($definition) && !empty(array_filter($definition)) && !array_key_exists('description', $definition)) {
$this->logger
->warning('Table \'@table_name\' defined by \'@machine_name\' does not specify a \'description\' key', [
'@table_name' => $table_name,
'@machine_name' => $machine_name,
]);
continue;
}
if (is_array($definition) && !empty(array_filter($definition)) && !array_key_exists('query', $definition)) {
$this->logger
->warning('Table \'@table_name\' defined by \'@machine_name\' does not specify a \'query\' key', [
'@table_name' => $table_name,
'@machine_name' => $machine_name,
]);
continue;
}
if (in_array($table_name, $yml_tables)) {
continue;
}
// Support for tables with wildcards in the end.
if (substr($table_name, -1) == '*') {
$table_pattern = substr($table_name, 0, -1);
foreach ($db_tables as $db_table) {
if (substr($db_table, 0, strlen($table_pattern)) === $table_pattern) {
array_push($yml_tables, $db_table);
}
}
continue;
}
array_push($yml_tables, $table_name);
}
}
$missing = array_diff($db_tables, $yml_tables);
if (is_array($missing) && empty($missing)) {
$this->logger
->info('All database tables are already specified in sanitize YML files');
return [];
}
sort($missing);
return $missing;
}