You are here

function _get_github_repo_tree in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 modules/CiteProc/biblio_citeproc.admin.inc \_get_github_repo_tree()

File

modules/CiteProc/biblio_citeproc.admin.inc, line 284

Code

function _get_github_repo_tree($path = '') {
  $options = array();
  $tree = array();
  $tree_url = 'https://api.github.com/repos/citation-style-language/styles-distribution/contents';
  if (!empty($path)) {
    $tree_url .= '/' . $path;
  }
  $result = drupal_http_request($tree_url);
  if ($result->code == 200) {
    $tree = json_decode($result->data);
  }
  else {
    $message = t('Attempt to get list of styles from GitHub resulted in an HTTP error: !code.', array(
      '!code' => $result->code,
    ));
    $cache = cache_get('biblio_citeproc_styles');
    if ($cache) {
      $message .= ' ' . t('I will use cached data instead.');
      $mess_type = 'warning';
      $options = $cache->data;
    }
    else {
      $message .= ' ' . t('I have no cached data, so you will not be able to install new styles at this time.');
      $mess_type = 'error';
    }
    drupal_set_message(check_plain($message), $mess_type);
    return $options;
  }
  foreach ($tree as $file) {
    if ($file->type == 'file' && strstr($file->name, '.csl')) {
      $options[$file->path] = basename($file->name);
    }
    elseif ($file->type == 'dir') {
      $options = array_merge($options, _get_github_repo_tree($file->name));
    }
  }
  return $options;
}