You are here

public function SiteAuditCheckExtensionsDuplicate::calculateScore in Site Audit 7

Same name and namespace in other branches
  1. 8.2 Check/Extensions/Duplicate.php \SiteAuditCheckExtensionsDuplicate::calculateScore()

Implements \SiteAudit\Check\Abstract\calculateScore().

Overrides SiteAuditCheckAbstract::calculateScore

File

Check/Extensions/Duplicate.php, line 102
Contains \SiteAudit\Check\Extensions\Duplicate.

Class

SiteAuditCheckExtensionsDuplicate
Class SiteAuditCheckExtensionsDuplicate.

Code

public function calculateScore() {
  $this->registry['extensions_dupe'] = array();
  $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
  $command = "find {$drupal_root} -xdev -type f -name '*.info' -o -path './" . variable_get('file_public_path', conf_path() . '/files') . "' -prune";
  exec($command, $result);
  foreach ($result as $path) {
    $path_parts = explode('/', $path);
    $name = substr(array_pop($path_parts), 0, -5);

    // Safe duplicates.
    if (in_array($name, array(
      'drupal_system_listing_compatible_test',
      'drupal_system_listing_incompatible_test',
    ))) {
      continue;
    }
    if (!isset($this->registry['extensions_dupe'][$name])) {
      $this->registry['extensions_dupe'][$name] = array();
    }
    $extension_info = array(
      'path' => substr($path, strlen($drupal_root) + 1),
      'version' => NULL,
    );
    $info = file($drupal_root . '/' . $extension_info['path']);
    foreach ($info as $line) {
      if (strpos($line, 'version') === 0) {
        $version = explode('=', $line);
        if (isset($version[1])) {
          $extension_info['version'] = trim(str_replace('"', '', $version[1]));
        }
      }
    }
    $this->registry['extensions_dupe'][$name][] = $extension_info;
  }

  // Review the detected extensions.
  foreach ($this->registry['extensions_dupe'] as $extension_name => $extension_infos) {

    // No duplicates.
    if (count($extension_infos) == 1) {
      unset($this->registry['extensions_dupe'][$extension_name]);
      continue;
    }

    // If every path is within an installation profile, ignore.
    $paths_in_profile = 0;
    foreach ($extension_infos as $index => $extension_info) {
      if (strpos($extension_info['path'], 'profiles/') === 0) {
        $paths_in_profile++;
      }
    }
    if ($paths_in_profile == count($extension_infos)) {
      unset($this->registry['extensions_dupe'][$extension_name]);
      continue;
    }

    // Allow overrides of installation profile extensions.
    $extension_object = $this->registry['extensions'][$extension_name];
    if (isset($extension_object->info['version']) && $extension_object->info['version'] && $paths_in_profile && drush_get_extension_status($extension_object) == 'enabled' && strpos($extension_object->uri, 'profiles/') === FALSE) {
      $skip = TRUE;
      foreach ($extension_infos as $extension_info) {

        // Not within the profile and there's version information.
        if (strpos($extension_info['path'], 'profiles/') !== FALSE && $extension_info['version']) {

          // If the installed version is equal or newer to the enabled.
          if (version_compare($extension_object->info['version'], $extension_info['version']) < 1) {
            $skip = FALSE;
            break;
          }
        }
      }
      if ($skip === TRUE) {
        unset($this->registry['extensions_dupe'][$extension_name]);
      }
    }
  }

  // Determine score.
  if (count($this->registry['extensions_dupe'])) {
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN;
  }
  return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
}