Retrieve a list of module versions

<?php
$dir = 'public://directory';
$files = file_scan_directory($dir, '/\.info/');

$modules = array();
foreach ($files as $key => $file) {
  $fragments = explode('/', str_replace(trim($dir, '/') . '/', '', $file->uri));
  if (count($fragments) == 2) {
    $module = @reset($fragments);
    $info = drupal_parse_info_file($file->uri);
    $modules[$module] = $info;
  }
  else {
    unset($files[$key]);
  }
}
dpm($modules);
?>

Optionally download them:

<?php
foreach ($modules as $module => $info) {
  $output .= 'wget http://ftp.drupal.org/files/projects/' . $module . '-' .  $info['version'] . '.tar.gz' . "\n";
}
dpm($output);
?>

Extended:

<?php
/**
 * Fetch project info via XML from a central server.
 */
function ys_check_module_versions($dir, $verbose = TRUE) {

  $projects = _ys_system_rebuild_module_data($dir);

  /**
   * Load update information from drupal.org
   */
  module_load_include('inc', 'update', 'update.fetch');

  $available = array();

  foreach ($projects as $project_name => $project) {
    $name = $project['name'];
    $info = $project['info'];
    $core = (!empty($info['core']) ? $info['core'] : DRUPAL_CORE_COMPATIBILITY);
    $url = _update_get_fetch_url_base($project);
    $url .= '/' . $name . '/' . $core;

    $xml = drupal_http_request($url);
    if (isset($xml->data)) {
      $available[$project_name] = update_parse_xml($xml->data);
    }
  }

  if ($available) {
    module_load_include('inc', 'update', 'update.compare');

    $columns = array(
      t('Module name'),
      t('Current version'),
      t('Recommended version'),
      t('Security update'),
    );
    $output[] = implode(' | ', $columns);
    $output[] = implode('|', array_fill(0, count($columns), '-------------'));

    $version_message = 'Current version is !existing_version, recommended version is !recommended.';
    $statusses = array(
      0 => 'Could not fetch data for %project. ' . $version_message,
      1 => 'Project %project needs security update(s). ' . $version_message,
      2 => 'Current release for %project has been unpublished and is no longer available. ' . $version_message,
      3 => 'Current release for %project is no longer supported by the project maintainer. ' . $version_message,
      4 => 'Project %project has a new release available, but it is not a security release. ' . $version_message,
      5 => 'Project %project is up to date',
    );

    update_process_project_info($projects);

    foreach ($projects as $project_name => $project) {

      update_calculate_project_update_status($project_name, $project, $available[$project_name]);

      $status = (isset($project['status']) ? $project['status'] : -10);

      foreach (array('existing_version', 'recommended') as $key) {
        ${$key} = t('n.a.');
        if (!empty($project[$key])) {
          ${$key} = $project[$key];
          $download_link = $available[$project_name]['releases'][${$key}]['download_link'];
          ${$key} = '[' . ${$key} . '](' . $download_link . ')';
        }
      }

      $output[$project_name] = implode(' | ', array(
        '[' . $project['info']['name'] . '](https://www.drupal.org/project/' . $project['info']['project'] . ')',
        $existing_version,
        $recommended,
        ($status == 1 ? t('TRUE') : t('FALSE')),
      ));

      // Display the results as Drupal messages.
      if ($verbose) {
        $replacements =  array(
          '%project' => $project['info']['name'],
        );
        $status_message = t((isset($statusses[$status]) ? $statusses[$status] : $statusses[0]), $replacements);

        if ($status == 5) {
          drupal_set_message(t($status_message, $replacements));
        }
        elseif (!empty($project['recommended'])) {
          $msg_type = ($status < 2 ? 'error' : 'warning');
          $replacements['!existing_version'] = $project['existing_version'];
          $replacements['!recommended'] = $project['recommended'];
          drupal_set_message(t($status_message, $replacements), $msg_type);
        }
      }
    }
  }
  return implode("\n", $output);
}

function _ys_system_rebuild_module_data($dir) {
  // Find modules
  $modules = drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.module$/', $dir, 'name', 0);

  // Set defaults for module info.
  $defaults = array(
    'dependencies' => array(),
    'description' => '',
    'package' => 'Other',
    'version' => NULL,
    'php' => DRUPAL_MINIMUM_PHP,
    'files' => array(),
    'bootstrap' => 0,
  );

  // Read info files for each module.
  foreach ($modules as $key => $module) {

    if (count(explode('/', str_replace(trim($dir, '/') . '/', '', $module->uri))) > 2) {
      unset($modules[$key]);
      continue;
    }

    // The module system uses the key 'filename' instead of 'uri' so copy the
    // value so it will be used by the modules system.
    $modules[$key]->filename = $module->uri;

    // Look for the info file.
    $module->info = drupal_parse_info_file(dirname($module->uri) . '/' . $module->name . '.info');

    // Skip modules that don't provide info.
    if (empty($module->info)) {
      unset($modules[$key]);
      continue;
    }

    // Merge in defaults and save.
    $modules[$key]->info = $module->info + $defaults;

    $modules[$key] = (array) $modules[$key];
  }

  return $modules;
}
?>

Reactie toevoegen

Plaats hier uw naam.