3 namespace Drupal\update;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
7 use GuzzleHttp\ClientInterface;
8 use GuzzleHttp\Exception\RequestException;
11 * Fetches project information from remote locations.
13 class UpdateFetcher implements UpdateFetcherInterface {
15 use DependencySerializationTrait;
18 * URL to check for updates, if a given project doesn't define its own.
20 const UPDATE_DEFAULT_URL = 'http://updates.drupal.org/release-history';
23 * The fetch url configured in the update settings.
32 * @var \Drupal\Core\Config\Config
34 protected $updateSettings;
37 * The HTTP client to fetch the feed data with.
39 * @var \GuzzleHttp\ClientInterface
41 protected $httpClient;
44 * Constructs a UpdateFetcher.
46 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
48 * @param \GuzzleHttp\ClientInterface $http_client
49 * A Guzzle client object.
51 public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $http_client) {
52 $this->fetchUrl = $config_factory->get('update.settings')->get('fetch.url');
53 $this->httpClient = $http_client;
54 $this->updateSettings = $config_factory->get('update.settings');
60 public function fetchProjectData(array $project, $site_key = '') {
61 $url = $this->buildFetchUrl($project, $site_key);
64 $data = (string) $this->httpClient
65 ->get($url, ['headers' => ['Accept' => 'text/xml']])
68 catch (RequestException $exception) {
69 watchdog_exception('update', $exception);
77 public function buildFetchUrl(array $project, $site_key = '') {
78 $name = $project['name'];
79 $url = $this->getFetchBaseUrl($project);
80 $url .= '/' . $name . '/' . \Drupal::CORE_COMPATIBILITY;
82 // Only append usage information if we have a site key and the project is
83 // enabled. We do not want to record usage statistics for disabled projects.
84 if (!empty($site_key) && (strpos($project['project_type'], 'disabled') === FALSE)) {
85 // Append the site key.
86 $url .= (strpos($url, '?') !== FALSE) ? '&' : '?';
88 $url .= rawurlencode($site_key);
90 // Append the version.
91 if (!empty($project['info']['version'])) {
93 $url .= rawurlencode($project['info']['version']);
96 // Append the list of modules or themes enabled.
97 $list = array_keys($project['includes']);
99 $url .= rawurlencode(implode(',', $list));
107 public function getFetchBaseUrl($project) {
108 if (isset($project['info']['project status url'])) {
109 $url = $project['info']['project status url'];
112 $url = $this->fetchUrl;
114 $url = static::UPDATE_DEFAULT_URL;