From 9ca8723880c7d8e2bb7caf825678d44dcbe933eb Mon Sep 17 00:00:00 2001
From: Angie Byron <webchick@24967.no-reply.drupal.org>
Date: Sun, 27 Dec 2009 03:37:54 +0000
Subject: [PATCH] #358815 by David_Rothstein, meatsack, and John Morahan: Fixed
 performance of drupal_get_install_files() with large file trees.

---
 includes/bootstrap.inc | 26 ++++++++++++++++----------
 includes/install.inc   | 33 ++++++---------------------------
 2 files changed, 22 insertions(+), 37 deletions(-)

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 59bc27e3e3c0..29b925350514 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -658,12 +658,14 @@ function drupal_get_filename($type, $name, $filename = NULL) {
   // when a database connection fails.
   else {
     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;
+      if (function_exists('db_query')) {
+        $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;
+        }
       }
     }
-    catch (PDOException $e) {
+    catch (Exception $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.
@@ -675,21 +677,25 @@ function drupal_get_filename($type, $name, $filename = NULL) {
       $dir = $type . 's';
       if ($type == 'theme_engine') {
         $dir = 'themes/engines';
-        $mask = "/$name\.engine$/";
+        $extension = 'engine';
       }
       elseif ($type == 'theme') {
-        $mask = "/$name\.info$/";
+        $extension = 'info';
       }
       else {
-        $mask = "/$name\.$type$/";
+        $extension = $type;
       }
 
       if (!function_exists('drupal_system_listing')) {
         require_once DRUPAL_ROOT . '/includes/common.inc';
       }
-      $matches = drupal_system_listing($mask, $dir, 'name', 0);
-      if (!empty($matches[$name]->uri)) {
-        $files[$type][$name] = $matches[$name]->uri;
+      // Scan the appropriate directories for all files with the requested
+      // extension, not just the file we are currently looking for. This
+      // prevents unnecessary scans from being repeated when this function is
+      // called more than once in the same page request.
+      $matches = drupal_system_listing("/\.$extension$/", $dir, 'name', 0);
+      foreach ($matches as $matched_name => $file) {
+        $files[$type][$matched_name] = $file->uri;
       }
     }
   }
diff --git a/includes/install.inc b/includes/install.inc
index 02d2e3452203..0b62f9a241ec 100644
--- a/includes/install.inc
+++ b/includes/install.inc
@@ -478,21 +478,6 @@ function drupal_rewrite_settings($settings = array(), $prefix = '') {
   }
 }
 
-/**
- * Get list of all .install files.
- *
- * @param $module_list
- *   An array of modules to search for their .install files.
- */
-function drupal_get_install_files($module_list = array()) {
-  $installs = array();
-  foreach ($module_list as $module) {
-    $installs = array_merge($installs, drupal_system_listing('/' . $module . '.install$/', 'modules'));
-  }
-  return $installs;
-}
-
-
 /**
  * Verify an install profile for installation.
  *
@@ -988,14 +973,11 @@ function drupal_check_profile($profile) {
 
   $info = install_profile_info($profile);
 
-  // Get a list of all .install files.
-  $installs = drupal_get_install_files($info['dependencies']);
-
-  // Collect requirement testing results
+  // Collect requirement testing results.
   $requirements = array();
-  foreach ($installs as $install) {
-    require_once DRUPAL_ROOT . '/' . $install->uri;
-    $function = $install->name . '_requirements';
+  foreach ($info['dependencies'] as $module) {
+    module_load_install($module);
+    $function = $module . '_requirements';
     if (function_exists($function)) {
       $requirements = array_merge($requirements, $function('install'));
     }
@@ -1031,11 +1013,8 @@ function drupal_requirements_severity(&$requirements) {
  *   TRUE/FALSE depending on the requirements are in place.
  */
 function drupal_check_module($module) {
-  // Include install file
-  $install = drupal_get_install_files(array($module));
-  if (isset($install[$module])) {
-    require_once DRUPAL_ROOT . '/' . $install[$module]->uri;
-
+  module_load_install($module);
+  if (module_hook($module, 'requirements')) {
     // Check requirements
     $requirements = module_invoke($module, 'requirements', 'install');
     if (is_array($requirements) && drupal_requirements_severity($requirements) == REQUIREMENT_ERROR) {
-- 
GitLab