function tripal_core_extensions_get_latest_module_version

2.x tripal_core.extensions.inc tripal_core_extensions_get_latest_module_version($project_name)

Determines the most recent downloadable package for a module.

This function will connect to Drupal.org's RSS feed for a project and determine the most recent version of the module and return the URL for it's package.

Parameters

$project:

1 call to tripal_core_extensions_get_latest_module_version()
tripal_core_extensions_form_submit in tripal_core/includes/tripal_core.extensions.inc
Process the import buttons.

File

tripal_core/includes/tripal_core.extensions.inc, line 699

Code

function tripal_core_extensions_get_latest_module_version($project_name) {
  // First use the Drupal RESTful API to get the project
  $url = "https://www.drupal.org/api-d7/node.json?type=project_module&field_project_machine_name=$project_name";
  $result = json_decode(file_get_contents($url), TRUE);
  $project = $result['list'][0];
  $nid = $project['nid'];

  // Second get the releases for this node and find releases for this Drupal
  $url = "https://www.drupal.org/api-d7/node.json?type=project_release&field_release_project=$nid";
  $result = json_decode(file_get_contents($url), TRUE);
  $releases = $result['list'];
  $drupal_version = VERSION;
  $drupal_major = preg_replace('/^(\d+)\.\d+$/', "$1", $drupal_version);
  $best_release = NULL;
  foreach ($releases as $release) {
    // This release must match the Drupal major version or it won't
    // be compatiable. If it doesn't we'll skip it.
    $release_version = $release['field_release_version'];
    $release_drupal = preg_replace('/^(\d+)\.x-.*$/', "$1", $release_version);
    if ($release_drupal != $drupal_major) {
      continue;
    }

    // Set the current release to be the best one. On successive iterations
    // we'll check to see if that is still true.
    if ($best_release == NULL) {
      $best_release = $release;
      continue;
    }
    if ($release['field_release_version_major'] > $best_release['field_release_version_major']) {
      $best_release = $release;
      continue;
    }
    if ($release['field_release_version_patch'] > $best_release['field_release_version_patch']) {
      $best_release = $release;
      continue;
    }

    // If the best version has no extra part then let's keep it as this is the
    // most stable release.
    if (!$best_release['field_release_version_extra']) {
      continue;
    }

    // Convert the 'extra' part to a numeric value that will let us compare
    // the ranks of the release versions.
    $extra_rank = array(
      'unstable' => 1,
      'alpha' => 2,
      'beta' => 3,
      'rc' => 4,
    );
    $matches = array();
    $this_extra = 0;
    if (preg_match('/^(.*?)(\d+)$/', $release['field_release_version_extra'], $matches)) {
      $this_extra = $extra_rank[$matches[1]] . "." . $matches[2];
    }
    $best_extra = 0;
    if (preg_match('/^(.*?)(\d+)$/', $best_release['field_release_version_extra'], $matches)) {
      $best_extra = $extra_rank[$matches[1]] . "." . $matches[2];
    }
    if ($this_extra > $best_extra) {
      $best_release = $release;
      continue;
    }
  }
  // If we have a best result then build the download string.
  // TODO: we may need to make another web services call to get the actual
  // download path, but for now we'll hard code the construction of it.
  if ($best_release) {
    return 'http://ftp.drupal.org/files/projects/' . $project_name . '-' . $best_release['field_release_version'] . '.tar.gz';
  }
  return '';
}