Commit 65a1528f authored by webchick's avatar webchick

#341140 by JohnAlbin and chx: Allow drupal_get_path() to be used even when database is down.

parent a4733d86
......@@ -635,20 +635,39 @@ function drupal_get_filename($type, $name, $filename = NULL) {
// the database. This is required because this function is called both
// before we have a database connection (i.e. during installation) and
// when a database connection fails.
elseif (db_is_active() && (($file = db_query("SELECT filename FROM {system} WHERE name = :name AND type = :type", array(':name' => $name, ':type' => $type))->fetchField()) && file_exists($file))) {
$files[$type][$name] = $file;
}
else {
// Fallback to searching the filesystem if the database connection is
// not established or the requested file is not found.
$config = conf_path();
$dir = (($type == 'theme_engine') ? 'themes/engines' : "${type}s");
$file = (($type == 'theme_engine') ? "$name.engine" : "$name.$type");
foreach (array("$config/$dir/$file", "$config/$dir/$name/$file", "$dir/$file", "$dir/$name/$file") as $file) {
try {
$file = db_query("SELECT filename FROM {system} WHERE name = :name AND type = :type", array(':name' => $name, ':type' => $type))->fetchField();
if (file_exists($file)) {
$files[$type][$name] = $file;
break;
}
}
catch (PDOException $e) {
// The database table may not exist because Drupal is not yet installed,
// or the database might be down. We have a fallback for this case so we
// hide the error completely.
}
// Fallback to searching the filesystem if the database could not find the
// file or the file returned by the database is not found.
if (!isset($files[$type][$name])) {
// We have a consistent directory naming: modules, themes...
$dir = $type . 's';
if ($type == 'theme_engine') {
$dir = 'themes/engines';
$mask = "/$name\.engine$/";
}
elseif ($type == 'theme') {
$mask = "/$name\.info$/";
}
else {
$mask = "/$name\.$type$/";
}
if (drupal_function_exists('drupal_system_listing')) {
$matches = drupal_system_listing($mask, $dir, 'name', 0);
if (!empty($matches[$name]->filepath)) {
$files[$type][$name] = $matches[$name]->filepath;
}
}
}
}
......
......@@ -3672,6 +3672,12 @@ function drupal_system_listing($mask, $directory, $key = 'name', $min_depth = 1)
$searchdir[] = "$config/$directory";
}
// If the database is not available, we can't use drupal_function_exists(), so
// we load the file_scan_directory function definition manually.
if (!function_exists('file_scan_directory')) {
require_once DRUPAL_ROOT . '/includes/file.inc';
}
// Get current list of items
foreach ($searchdir as $dir) {
$files = array_merge($files, file_scan_directory($dir, $mask, array('key' => $key, 'min_depth' => $min_depth)));
......
......@@ -227,7 +227,7 @@ class BootstrapVariableTestCase extends DrupalWebTestCase {
}
/**
* Test hook_boot and hook_exit.
* Test hook_boot() and hook_exit().
*/
class HookBootExitTestCase extends DrupalWebTestCase {
......@@ -279,3 +279,37 @@ class HookBootExitTestCase extends DrupalWebTestCase {
}
}
/**
* Test drupal_get_filename()'s availability.
*/
class BootstrapGetFilenameTestCase extends DrupalUnitTestCase {
public static function getInfo() {
return array(
'name' => t('Get Filename Test'),
'description' => t('Test that drupal_get_filename() works correctly when the file is not found in the database.'),
'group' => t('Bootstrap'),
);
}
/**
* Test that drupal_get_filename() works correctly when the file is not found in the database.
*/
function testDrupalGetFilename() {
// Reset the static cache so we can test the "db is not active" code of
// drupal_get_filename().
drupal_static_reset('drupal_get_filename');
// Retrieving the location of a module.
$this->assertIdentical(drupal_get_filename('module', 'php'), 'modules/php/php.module', t('Retrieve module location.'));
// Retrieving the location of a theme.
$this->assertIdentical(drupal_get_filename('theme', 'stark'), 'themes/stark/stark.info', t('Retrieve theme location.'));
// Retrieving the location of a theme engine.
$this->assertIdentical(drupal_get_filename('theme_engine', 'phptemplate'), 'themes/engines/phptemplate/phptemplate.engine', t('Retrieve theme engine location.'));
// Retrieving a file that is definitely not stored in the database.
$this->assertIdentical(drupal_get_filename('profile', 'default'), 'profiles/default/default.profile', t('Retrieve install profile location.'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment