diff --git a/core/modules/update/lib/Drupal/update/Controller/UpdateFetchController.php b/core/modules/update/lib/Drupal/update/Controller/UpdateFetchController.php new file mode 100644 index 0000000..e6e4655 --- /dev/null +++ b/core/modules/update/lib/Drupal/update/Controller/UpdateFetchController.php @@ -0,0 +1,103 @@ +refresh(); + $batch = array( + 'operations' => array( + array('\Drupal\update\Controller\UpdateFetchController::fetchDataBatch', array()), + ), + 'finished' => 'update_fetch_data_finished', + 'title' => t('Checking available update data'), + 'progress_message' => t('Trying to check available update data ...'), + 'error_message' => t('Error checking available update data.'), + ); + batch_set($batch); + batch_process('admin/reports/updates'); + } + + /** + * Batch callback: Processes a step in batch for fetching available update data. + * + * @param $context + * Reference to an array used for Batch API storage. + */ + public static function fetchDataBatch(&$context) { + $queue = \Drupal::queue('update_fetch_tasks'); + if (empty($context['sandbox']['max'])) { + $context['finished'] = 0; + $context['sandbox']['max'] = $queue->numberOfItems(); + $context['sandbox']['progress'] = 0; + $context['message'] = t('Checking available update data ...'); + $context['results']['updated'] = 0; + $context['results']['failures'] = 0; + $context['results']['processed'] = 0; + } + + // Grab another item from the fetch queue. + for ($i = 0; $i < 5; $i++) { + if ($item = $queue->claimItem()) { + if (\Drupal::service('update.fetch')->processFetchTask($item->data)) { + $context['results']['updated']++; + $context['message'] = t('Checked available update data for %title.', array('%title' => $item->data['info']['name'])); + } + else { + $context['message'] = t('Failed to check available update data for %title.', array('%title' => $item->data['info']['name'])); + $context['results']['failures']++; + } + $context['sandbox']['progress']++; + $context['results']['processed']++; + $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; + $queue->deleteItem($item); + } + else { + // If the queue is currently empty, we're done. It's possible that + // another thread might have added new fetch tasks while we were + // processing this batch. In that case, the usual 'finished' math could + // get confused, since we'd end up processing more tasks that we thought + // we had when we started and initialized 'max' with numberOfItems(). By + // forcing 'finished' to be exactly 1 here, we ensure that batch + // processing is terminated. + $context['finished'] = 1; + return; + } + } + } + +} diff --git a/core/modules/update/lib/Drupal/update/Tests/UpdateCoreTest.php b/core/modules/update/lib/Drupal/update/Tests/UpdateCoreTest.php index bfbd4a0..8c432dc 100644 --- a/core/modules/update/lib/Drupal/update/Tests/UpdateCoreTest.php +++ b/core/modules/update/lib/Drupal/update/Tests/UpdateCoreTest.php @@ -6,6 +6,7 @@ */ namespace Drupal\update\Tests; +use Drupal\update\UpdateFetchManager; /** * Tests behavior related to discovering and listing updates to Drupal core. @@ -195,6 +196,7 @@ function testServiceUnavailable() { * Tests that exactly one fetch task per project is created and not more. */ function testFetchTasks() { + $service = new UpdateFetchManager(); $projecta = array( 'name' => 'aaa_update_test', ); @@ -203,18 +205,18 @@ function testFetchTasks() { ); $queue = \Drupal::queue('update_fetch_tasks'); $this->assertEqual($queue->numberOfItems(), 0, 'Queue is empty'); - update_create_fetch_task($projecta); + $service->createFetchTask($projecta); $this->assertEqual($queue->numberOfItems(), 1, 'Queue contains one item'); - update_create_fetch_task($projectb); + $service->createFetchTask($projectb); $this->assertEqual($queue->numberOfItems(), 2, 'Queue contains two items'); // Try to add project a again. - update_create_fetch_task($projecta); + $service->createFetchTask($projecta); $this->assertEqual($queue->numberOfItems(), 2, 'Queue still contains two items'); // Clear storage and try again. update_storage_clear(); drupal_static_reset('_update_create_fetch_task'); - update_create_fetch_task($projecta); + $service->createFetchTask($projecta); $this->assertEqual($queue->numberOfItems(), 2, 'Queue contains two items'); } diff --git a/core/modules/update/lib/Drupal/update/Tests/UpdateCoreUnitTest.php b/core/modules/update/lib/Drupal/update/Tests/UpdateCoreUnitTest.php index ac26bd7..f9a285e 100644 --- a/core/modules/update/lib/Drupal/update/Tests/UpdateCoreUnitTest.php +++ b/core/modules/update/lib/Drupal/update/Tests/UpdateCoreUnitTest.php @@ -8,6 +8,8 @@ namespace Drupal\update\Tests; use Drupal\simpletest\UnitTestBase; +use Drupal\update\UpdateFetchManager; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Tests update functionality unrelated to the database. @@ -31,13 +33,13 @@ public static function getInfo() { function setUp() { parent::setUp(); - module_load_include('inc', 'update', 'update.fetch'); } /** * Tests that _update_build_fetch_url() builds the URL correctly. */ function testUpdateBuildFetchUrl() { + $service = new UpdateFetchManager(); //first test that we didn't break the trivial case $project['name'] = 'update_test'; $project['project_type'] = ''; @@ -46,14 +48,16 @@ function testUpdateBuildFetchUrl() { $project['includes'] = array('module1' => 'Module 1', 'module2' => 'Module 2'); $site_key = ''; $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY; - $url = _update_build_fetch_url($project, $site_key); + //$url = \Drupal::service('update.fetch')->buildFetchUrl($project, $site_key); + $url = $service->buildFetchUrl($project, $site_key); $this->assertEqual($url, $expected, "'$url' when no site_key provided should be '$expected'."); //For disabled projects it shouldn't add the site key either. $site_key = 'site_key'; $project['project_type'] = 'disabled'; $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY; - $url = _update_build_fetch_url($project, $site_key); + //$url = \Drupal::service('update.fetch')->buildFetchUrl($project, $site_key); + $url = $service->buildFetchUrl($project, $site_key); $this->assertEqual($url, $expected, "'$url' should be '$expected' for disabled projects."); //for enabled projects, adding the site key @@ -61,7 +65,8 @@ function testUpdateBuildFetchUrl() { $expected = 'http://www.example.com/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY; $expected .= '?site_key=site_key'; $expected .= '&list=' . rawurlencode('module1,module2'); - $url = _update_build_fetch_url($project, $site_key); + //$url = \Drupal::service('update.fetch')->buildFetchUrl($project, $site_key); + $url = $service->buildFetchUrl($project, $site_key); $this->assertEqual($url, $expected, "When site_key provided, '$url' should be '$expected'."); // http://drupal.org/node/1481156 test incorrect logic when URL contains @@ -70,7 +75,8 @@ function testUpdateBuildFetchUrl() { $expected = 'http://www.example.com/?project=/' . $project['name'] . '/' . DRUPAL_CORE_COMPATIBILITY; $expected .= '&site_key=site_key'; $expected .= '&list=' . rawurlencode('module1,module2'); - $url = _update_build_fetch_url($project, $site_key); + //$url = \Drupal::service('update.fetch')->buildFetchUrl($project, $site_key); + $url = $service->buildFetchUrl($project, $site_key); $this->assertEqual($url, $expected, "When ? is present, '$url' should be '$expected'."); } diff --git a/core/modules/update/lib/Drupal/update/UpdateCompareManager.php b/core/modules/update/lib/Drupal/update/UpdateCompareManager.php new file mode 100644 index 0000000..0df98de --- /dev/null +++ b/core/modules/update/lib/Drupal/update/UpdateCompareManager.php @@ -0,0 +1,88 @@ +get('check.disabled_extensions')) { + update_process_info_list($projects, $module_data, 'module', FALSE); + update_process_info_list($projects, $theme_data, 'theme', FALSE); + } + // Allow other modules to alter projects before fetching and comparing. + drupal_alter('update_projects', $projects); + // Store the site's project data for at most 1 hour. + \Drupal::keyValueExpirable('update')->setWithExpire('update_project_projects', $projects, 3600); + } + } + return $projects; + } +} diff --git a/core/modules/update/lib/Drupal/update/UpdateFetchManager.php b/core/modules/update/lib/Drupal/update/UpdateFetchManager.php new file mode 100644 index 0000000..4e758ae --- /dev/null +++ b/core/modules/update/lib/Drupal/update/UpdateFetchManager.php @@ -0,0 +1,330 @@ +get('update_fetch_task')->getAll(); + } + if (empty($fetch_tasks[$project['name']])) { + $queue = \Drupal::queue('update_fetch_tasks'); + $queue->createItem($project); + \Drupal::service('keyvalue')->get('update_fetch_task')->set($project['name'], $project); + $fetch_tasks[$project['name']] = REQUEST_TIME; + } + } + + /** + * Attempts to drain the queue of tasks for release history data to fetch. + */ + public function fetchData() { + $queue = \Drupal::queue('update_fetch_tasks'); + $end = time() + config('update.settings')->get('fetch.timeout'); + while (time() < $end && ($item = $queue->claimItem())) { + $this->processFetchTask($item->data); + $queue->deleteItem($item); + } + } + + /** + * Processes a task to fetch available update data for a single project. + * + * Once the release history XML data is downloaded, it is parsed and saved in an + * entry just for that project. + * + * @param $project + * Associative array of information about the project to fetch data for. + * + * @return + * TRUE if we fetched parsable XML, otherwise FALSE. + */ + public function processFetchTask($project) { + global $base_url; + $update_config = config('update.settings'); + $fail = &drupal_static(__FUNCTION__, array()); + // This can be in the middle of a long-running batch, so REQUEST_TIME won't + // necessarily be valid. + $request_time_difference = time() - REQUEST_TIME; + if (empty($fail)) { + // If we have valid data about release history XML servers that we have + // failed to fetch from on previous attempts, load that. + $fail = \Drupal::keyValueExpirable('update')->get('fetch_failures'); + } + + $max_fetch_attempts = $update_config->get('fetch.max_attempts'); + + $success = FALSE; + $available = array(); + $site_key = Crypt::hmacBase64($base_url, drupal_get_private_key()); + $url = $this->buildFetchUrl($project, $site_key); + $fetch_url_base = $this->getFetchUrlBase($project); + $project_name = $project['name']; + + if (empty($fail[$fetch_url_base]) || $fail[$fetch_url_base] < $max_fetch_attempts) { + try { + $data = \Drupal::httpClient() + ->get($url, array('Accept' => 'text/xml')) + ->send() + ->getBody(TRUE); + } + catch (RequestException $exception) { + watchdog_exception('update', $exception); + } + } + + if (!empty($data)) { + $available = $this->parseXml($data); + // @todo: Purge release data we don't need (http://drupal.org/node/238950). + if (!empty($available)) { + // Only if we fetched and parsed something sane do we return success. + $success = TRUE; + } + } + else { + $available['project_status'] = 'not-fetched'; + if (empty($fail[$fetch_url_base])) { + $fail[$fetch_url_base] = 1; + } + else { + $fail[$fetch_url_base]++; + } + } + + $frequency = $update_config->get('check.interval_days'); + $available['last_fetch'] = REQUEST_TIME + $request_time_difference; + \Drupal::keyValueExpirable('update_available_releases')->setWithExpire($project_name, $available, $request_time_difference + (60 * 60 * 24 * $frequency)); + + // Stash the $fail data back in the DB for the next 5 minutes. + \Drupal::keyValueExpirable('update')->setWithExpire('fetch_failures', $fail, $request_time_difference + (60 * 5)); + + // Whether this worked or not, we did just (try to) check for updates. + state()->set('update.last_check', REQUEST_TIME + $request_time_difference); + + // Now that we processed the fetch task for this project, clear out the + // record for this task so we're willing to fetch again. + \Drupal::service('keyvalue')->get('update_fetch_task')->delete($project_name); + + return $success; + } + + /** + * Generates the URL to fetch information about project updates. + * + * This figures out the right URL to use, based on the project's .info.yml file + * and the global defaults. Appends optional query arguments when the site is + * configured to report usage stats. + * + * @param $project + * The array of project information from updateGetProjects(). + * @param $site_key + * (optional) The anonymous site key hash. Defaults to an empty string. + * + * @return + * The URL for fetching information about updates to the specified project. + * + * @see update_fetch_data(updateFetchData + * @see _update_process_fetch_task() + * @see update_get_projects() + */ + public function buildFetchUrl($project, $site_key = '') { + $name = $project['name']; + $url = $this->getFetchUrlBase($project); + $url .= '/' . $name . '/' . DRUPAL_CORE_COMPATIBILITY; + + // Only append usage infomation if we have a site key and the project is + // enabled. We do not want to record usage statistics for disabled projects. + if (!empty($site_key) && (strpos($project['project_type'], 'disabled') === FALSE)) { + // Append the site key. + $url .= (strpos($url, '?') !== FALSE) ? '&' : '?'; + $url .= 'site_key='; + $url .= rawurlencode($site_key); + + // Append the version. + if (!empty($project['info']['version'])) { + $url .= '&version='; + $url .= rawurlencode($project['info']['version']); + } + + // Append the list of modules or themes enabled. + $list = array_keys($project['includes']); + $url .= '&list='; + $url .= rawurlencode(implode(',', $list)); + } + return $url; + } + + /** + * Returns the base of the URL to fetch available update data for a project. + * + * @param $project + * The array of project information from updateGetProjects(). + * + * @return + * The base of the URL used for fetching available update data. This does + * not include the path elements to specify a particular project, version, + * site_key, etc. + * + * @see _update_build_fetch_url() + */ + public function getFetchUrlBase($project) { + if (isset($project['info']['project status url'])) { + $url = $project['info']['project status url']; + } + else { + $url = config('update.settings')->get('fetch.url'); + if (empty($url)) { + $url = UPDATE_DEFAULT_URL; + } + } + return $url; + } + + /** + * Parses the XML of the Drupal release history info files. + * + * @param $raw_xml + * A raw XML string of available release data for a given project. + * + * @return + * Array of parsed data about releases for a given project, or NULL if there + * was an error parsing the string. + */ + public function parseXml($raw_xml) { + try { + $xml = new SimpleXMLElement($raw_xml); + } + catch (Exception $e) { + // SimpleXMLElement::__construct produces an E_WARNING error message for + // each error found in the XML data and throws an exception if errors + // were detected. Catch any exception and return failure (NULL). + return; + } + // If there is no valid project data, the XML is invalid, so return failure. + if (!isset($xml->short_name)) { + return; + } + $short_name = (string) $xml->short_name; + $data = array(); + foreach ($xml as $k => $v) { + $data[$k] = (string) $v; + } + $data['releases'] = array(); + if (isset($xml->releases)) { + foreach ($xml->releases->children() as $release) { + $version = (string) $release->version; + $data['releases'][$version] = array(); + foreach ($release->children() as $k => $v) { + $data['releases'][$version][$k] = (string) $v; + } + $data['releases'][$version]['terms'] = array(); + if ($release->terms) { + foreach ($release->terms->children() as $term) { + if (!isset($data['releases'][$version]['terms'][(string) $term->name])) { + $data['releases'][$version]['terms'][(string) $term->name] = array(); + } + $data['releases'][$version]['terms'][(string) $term->name][] = (string) $term->value; + } + } + } + } + return $data; + } + + /** + * Performs any notifications that should be done once cron fetches new data. + * + * This method checks the status of the site using the new data and, depending + * on the configuration of the site, notifies administrators via e-mail if there + * are new releases or missing security updates. + * + * @see update_requirements() + */ + public function cronNotify() { + $update_config = config('update.settings'); + module_load_install('update'); + $status = update_requirements('runtime'); + $params = array(); + $notify_all = ($update_config->get('notification.threshold') == 'all'); + foreach (array('core', 'contrib') as $report_type) { + $type = 'update_' . $report_type; + if (isset($status[$type]['severity']) + && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UPDATE_NOT_CURRENT))) { + $params[$report_type] = $status[$type]['reason']; + } + } + if (!empty($params)) { + $notify_list = $update_config->get('notification.emails'); + if (!empty($notify_list)) { + $default_langcode = language_default()->langcode; + foreach ($notify_list as $target) { + if ($target_user = user_load_by_mail($target)) { + $target_langcode = user_preferred_langcode($target_user); + } + else { + $target_langcode = $default_langcode; + } + $message = drupal_mail('update', 'status_notify', $target, $target_langcode, $params); + // Track when the last mail was successfully sent to avoid sending + // too many e-mails. + if ($message['result']) { + state()->set('update.last_email_notification', REQUEST_TIME); + } + } + } + } + } + + /** + * Clears out all the available update data and initiates re-fetching. + */ + public function refresh() { + + // Since we're fetching new available update data, we want to clear + // of both the projects we care about, and the current update status of the + // site. We do *not* want to clear the cache of available releases just yet, + // since that data (even if it's stale) can be useful during + // updateGetProjects(); for example, to modules that implement + // hook_system_info_alter() such as cvs_deploy. + \Drupal::keyValueExpirable('update')->delete('update_project_projects'); + \Drupal::keyValueExpirable('update')->delete('update_project_data'); + + $projects = \Drupal::service('update.compare')->getProjects(); + + // Now that we have the list of projects, we should also clear the available + // release data, since even if we fail to fetch new data, we need to clear + // out the stale data at this point. + \Drupal::keyValueExpirable('update_available_releases')->deleteAll(); + + foreach ($projects as $key => $project) { + $this->createFetchTask($project); + } + } + +} diff --git a/core/modules/update/update.compare.inc b/core/modules/update/update.compare.inc index c8f5958..dd768e3 100644 --- a/core/modules/update/update.compare.inc +++ b/core/modules/update/update.compare.inc @@ -53,27 +53,7 @@ * @see update_project_storage() */ function update_get_projects() { - $projects = &drupal_static(__FUNCTION__, array()); - if (empty($projects)) { - // Retrieve the projects from storage, if present. - $projects = update_project_storage('update_project_projects'); - if (empty($projects)) { - // Still empty, so we have to rebuild. - $module_data = system_rebuild_module_data(); - $theme_data = system_rebuild_theme_data(); - update_process_info_list($projects, $module_data, 'module', TRUE); - update_process_info_list($projects, $theme_data, 'theme', TRUE); - if (config('update.settings')->get('check.disabled_extensions')) { - update_process_info_list($projects, $module_data, 'module', FALSE); - update_process_info_list($projects, $theme_data, 'theme', FALSE); - } - // Allow other modules to alter projects before fetching and comparing. - drupal_alter('update_projects', $projects); - // Store the site's project data for at most 1 hour. - Drupal::keyValueExpirable('update')->setWithExpire('update_project_projects', $projects, 3600); - } - } - return $projects; + return \Drupal::service('update.compare')->getProjects(); } /** diff --git a/core/modules/update/update.fetch.inc b/core/modules/update/update.fetch.inc deleted file mode 100644 index 0d64ca4..0000000 --- a/core/modules/update/update.fetch.inc +++ /dev/null @@ -1,423 +0,0 @@ - array( - array('update_fetch_data_batch', array()), - ), - 'finished' => 'update_fetch_data_finished', - 'title' => t('Checking available update data'), - 'progress_message' => t('Trying to check available update data ...'), - 'error_message' => t('Error checking available update data.'), - 'file' => drupal_get_path('module', 'update') . '/update.fetch.inc', - ); - batch_set($batch); - batch_process('admin/reports/updates'); -} - -/** - * Batch callback: Processes a step in batch for fetching available update data. - * - * @param $context - * Reference to an array used for Batch API storage. - */ -function update_fetch_data_batch(&$context) { - $queue = Drupal::queue('update_fetch_tasks'); - if (empty($context['sandbox']['max'])) { - $context['finished'] = 0; - $context['sandbox']['max'] = $queue->numberOfItems(); - $context['sandbox']['progress'] = 0; - $context['message'] = t('Checking available update data ...'); - $context['results']['updated'] = 0; - $context['results']['failures'] = 0; - $context['results']['processed'] = 0; - } - - // Grab another item from the fetch queue. - for ($i = 0; $i < 5; $i++) { - if ($item = $queue->claimItem()) { - if (_update_process_fetch_task($item->data)) { - $context['results']['updated']++; - $context['message'] = t('Checked available update data for %title.', array('%title' => $item->data['info']['name'])); - } - else { - $context['message'] = t('Failed to check available update data for %title.', array('%title' => $item->data['info']['name'])); - $context['results']['failures']++; - } - $context['sandbox']['progress']++; - $context['results']['processed']++; - $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max']; - $queue->deleteItem($item); - } - else { - // If the queue is currently empty, we're done. It's possible that - // another thread might have added new fetch tasks while we were - // processing this batch. In that case, the usual 'finished' math could - // get confused, since we'd end up processing more tasks that we thought - // we had when we started and initialized 'max' with numberOfItems(). By - // forcing 'finished' to be exactly 1 here, we ensure that batch - // processing is terminated. - $context['finished'] = 1; - return; - } - } -} - -/** - * Batch callback: Performs actions when all fetch tasks have been completed. - * - * @param $success - * TRUE if the batch operation was successful; FALSE if there were errors. - * @param $results - * An associative array of results from the batch operation, including the key - * 'updated' which holds the total number of projects we fetched available - * update data for. - */ -function update_fetch_data_finished($success, $results) { - if ($success) { - if (!empty($results)) { - if (!empty($results['updated'])) { - drupal_set_message(format_plural($results['updated'], 'Checked available update data for one project.', 'Checked available update data for @count projects.')); - } - if (!empty($results['failures'])) { - drupal_set_message(format_plural($results['failures'], 'Failed to get available update data for one project.', 'Failed to get available update data for @count projects.'), 'error'); - } - } - } - else { - drupal_set_message(t('An error occurred trying to get available update data.'), 'error'); - } -} - -/** - * Attempts to drain the queue of tasks for release history data to fetch. - */ -function _update_fetch_data() { - $queue = Drupal::queue('update_fetch_tasks'); - $end = time() + config('update.settings')->get('fetch.timeout'); - while (time() < $end && ($item = $queue->claimItem())) { - _update_process_fetch_task($item->data); - $queue->deleteItem($item); - } -} - -/** - * Processes a task to fetch available update data for a single project. - * - * Once the release history XML data is downloaded, it is parsed and saved in an - * entry just for that project. - * - * @param $project - * Associative array of information about the project to fetch data for. - * - * @return - * TRUE if we fetched parsable XML, otherwise FALSE. - */ -function _update_process_fetch_task($project) { - global $base_url; - $update_config = config('update.settings'); - $fail = &drupal_static(__FUNCTION__, array()); - // This can be in the middle of a long-running batch, so REQUEST_TIME won't - // necessarily be valid. - $request_time_difference = time() - REQUEST_TIME; - if (empty($fail)) { - // If we have valid data about release history XML servers that we have - // failed to fetch from on previous attempts, load that. - $fail = Drupal::keyValueExpirable('update')->get('fetch_failures'); - } - - $max_fetch_attempts = $update_config->get('fetch.max_attempts'); - - $success = FALSE; - $available = array(); - $site_key = Crypt::hmacBase64($base_url, drupal_get_private_key()); - $url = _update_build_fetch_url($project, $site_key); - $fetch_url_base = _update_get_fetch_url_base($project); - $project_name = $project['name']; - - if (empty($fail[$fetch_url_base]) || $fail[$fetch_url_base] < $max_fetch_attempts) { - try { - $data = Drupal::httpClient() - ->get($url, array('Accept' => 'text/xml')) - ->send() - ->getBody(TRUE); - } - catch (RequestException $exception) { - watchdog_exception('update', $exception); - } - } - - if (!empty($data)) { - $available = update_parse_xml($data); - // @todo: Purge release data we don't need (http://drupal.org/node/238950). - if (!empty($available)) { - // Only if we fetched and parsed something sane do we return success. - $success = TRUE; - } - } - else { - $available['project_status'] = 'not-fetched'; - if (empty($fail[$fetch_url_base])) { - $fail[$fetch_url_base] = 1; - } - else { - $fail[$fetch_url_base]++; - } - } - - $frequency = $update_config->get('check.interval_days'); - $available['last_fetch'] = REQUEST_TIME + $request_time_difference; - Drupal::keyValueExpirable('update_available_releases')->setWithExpire($project_name, $available, $request_time_difference + (60 * 60 * 24 * $frequency)); - - // Stash the $fail data back in the DB for the next 5 minutes. - Drupal::keyValueExpirable('update')->setWithExpire('fetch_failures', $fail, $request_time_difference + (60 * 5)); - - // Whether this worked or not, we did just (try to) check for updates. - state()->set('update.last_check', REQUEST_TIME + $request_time_difference); - - // Now that we processed the fetch task for this project, clear out the - // record for this task so we're willing to fetch again. - drupal_container()->get('keyvalue')->get('update_fetch_task')->delete($project_name); - - return $success; -} - -/** - * Clears out all the available update data and initiates re-fetching. - */ -function _update_refresh() { - module_load_include('inc', 'update', 'update.compare'); - - // Since we're fetching new available update data, we want to clear - // of both the projects we care about, and the current update status of the - // site. We do *not* want to clear the cache of available releases just yet, - // since that data (even if it's stale) can be useful during - // update_get_projects(); for example, to modules that implement - // hook_system_info_alter() such as cvs_deploy. - Drupal::keyValueExpirable('update')->delete('update_project_projects'); - Drupal::keyValueExpirable('update')->delete('update_project_data'); - - $projects = update_get_projects(); - - // Now that we have the list of projects, we should also clear the available - // release data, since even if we fail to fetch new data, we need to clear - // out the stale data at this point. - Drupal::keyValueExpirable('update_available_releases')->deleteAll(); - - foreach ($projects as $key => $project) { - update_create_fetch_task($project); - } -} - -/** - * Adds a task to the queue for fetching release history data for a project. - * - * We only create a new fetch task if there's no task already in the queue for - * this particular project (based on 'update_fetch_task' key-value collection). - * - * @param $project - * Associative array of information about a project as created by - * update_get_projects(), including keys such as 'name' (short name), and the - * 'info' array with data from a .info.yml file for the project. - * - * @see update_get_projects() - * @see update_get_available() - * @see update_refresh() - * @see update_fetch_data() - * @see _update_process_fetch_task() - */ -function _update_create_fetch_task($project) { - $fetch_tasks = &drupal_static(__FUNCTION__, array()); - if (empty($fetch_tasks)) { - $fetch_tasks = drupal_container()->get('keyvalue')->get('update_fetch_task')->getAll(); - } - if (empty($fetch_tasks[$project['name']])) { - $queue = Drupal::queue('update_fetch_tasks'); - $queue->createItem($project); - drupal_container()->get('keyvalue')->get('update_fetch_task')->set($project['name'], $project); - $fetch_tasks[$project['name']] = REQUEST_TIME; - } -} - -/** - * Generates the URL to fetch information about project updates. - * - * This figures out the right URL to use, based on the project's .info.yml file - * and the global defaults. Appends optional query arguments when the site is - * configured to report usage stats. - * - * @param $project - * The array of project information from update_get_projects(). - * @param $site_key - * (optional) The anonymous site key hash. Defaults to an empty string. - * - * @return - * The URL for fetching information about updates to the specified project. - * - * @see update_fetch_data() - * @see _update_process_fetch_task() - * @see update_get_projects() - */ -function _update_build_fetch_url($project, $site_key = '') { - $name = $project['name']; - $url = _update_get_fetch_url_base($project); - $url .= '/' . $name . '/' . DRUPAL_CORE_COMPATIBILITY; - - // Only append usage infomation if we have a site key and the project is - // enabled. We do not want to record usage statistics for disabled projects. - if (!empty($site_key) && (strpos($project['project_type'], 'disabled') === FALSE)) { - // Append the site key. - $url .= (strpos($url, '?') !== FALSE) ? '&' : '?'; - $url .= 'site_key='; - $url .= rawurlencode($site_key); - - // Append the version. - if (!empty($project['info']['version'])) { - $url .= '&version='; - $url .= rawurlencode($project['info']['version']); - } - - // Append the list of modules or themes enabled. - $list = array_keys($project['includes']); - $url .= '&list='; - $url .= rawurlencode(implode(',', $list)); - } - return $url; -} - -/** - * Returns the base of the URL to fetch available update data for a project. - * - * @param $project - * The array of project information from update_get_projects(). - * - * @return - * The base of the URL used for fetching available update data. This does - * not include the path elements to specify a particular project, version, - * site_key, etc. - * - * @see _update_build_fetch_url() - */ -function _update_get_fetch_url_base($project) { - if (isset($project['info']['project status url'])) { - $url = $project['info']['project status url']; - } - else { - $url = config('update.settings')->get('fetch.url'); - if (empty($url)) { - $url = UPDATE_DEFAULT_URL; - } - } - return $url; -} - -/** - * Performs any notifications that should be done once cron fetches new data. - * - * This method checks the status of the site using the new data and, depending - * on the configuration of the site, notifies administrators via e-mail if there - * are new releases or missing security updates. - * - * @see update_requirements() - */ -function _update_cron_notify() { - $update_config = config('update.settings'); - module_load_install('update'); - $status = update_requirements('runtime'); - $params = array(); - $notify_all = ($update_config->get('notification.threshold') == 'all'); - foreach (array('core', 'contrib') as $report_type) { - $type = 'update_' . $report_type; - if (isset($status[$type]['severity']) - && ($status[$type]['severity'] == REQUIREMENT_ERROR || ($notify_all && $status[$type]['reason'] == UPDATE_NOT_CURRENT))) { - $params[$report_type] = $status[$type]['reason']; - } - } - if (!empty($params)) { - $notify_list = $update_config->get('notification.emails'); - if (!empty($notify_list)) { - $default_langcode = language_default()->langcode; - foreach ($notify_list as $target) { - if ($target_user = user_load_by_mail($target)) { - $target_langcode = user_preferred_langcode($target_user); - } - else { - $target_langcode = $default_langcode; - } - $message = drupal_mail('update', 'status_notify', $target, $target_langcode, $params); - // Track when the last mail was successfully sent to avoid sending - // too many e-mails. - if ($message['result']) { - state()->set('update.last_email_notification', REQUEST_TIME); - } - } - } - } -} - -/** - * Parses the XML of the Drupal release history info files. - * - * @param $raw_xml - * A raw XML string of available release data for a given project. - * - * @return - * Array of parsed data about releases for a given project, or NULL if there - * was an error parsing the string. - */ -function update_parse_xml($raw_xml) { - try { - $xml = new SimpleXMLElement($raw_xml); - } - catch (Exception $e) { - // SimpleXMLElement::__construct produces an E_WARNING error message for - // each error found in the XML data and throws an exception if errors - // were detected. Catch any exception and return failure (NULL). - return; - } - // If there is no valid project data, the XML is invalid, so return failure. - if (!isset($xml->short_name)) { - return; - } - $short_name = (string) $xml->short_name; - $data = array(); - foreach ($xml as $k => $v) { - $data[$k] = (string) $v; - } - $data['releases'] = array(); - if (isset($xml->releases)) { - foreach ($xml->releases->children() as $release) { - $version = (string) $release->version; - $data['releases'][$version] = array(); - foreach ($release->children() as $k => $v) { - $data['releases'][$version][$k] = (string) $v; - } - $data['releases'][$version]['terms'] = array(); - if ($release->terms) { - foreach ($release->terms->children() as $term) { - if (!isset($data['releases'][$version]['terms'][(string) $term->name])) { - $data['releases'][$version]['terms'][(string) $term->name] = array(); - } - $data['releases'][$version]['terms'][(string) $term->name][] = (string) $term->value; - } - } - } - } - return $data; -} diff --git a/core/modules/update/update.module b/core/modules/update/update.module index ddbc398..eb74472 100644 --- a/core/modules/update/update.module +++ b/core/modules/update/update.module @@ -175,10 +175,8 @@ function update_menu() { ); $items['admin/reports/updates/check'] = array( 'title' => 'Manual update check', - 'page callback' => 'update_manual_status', - 'access arguments' => array('administer site configuration'), + 'route_name' => 'update_check_manual', 'type' => MENU_CALLBACK, - 'file' => 'update.fetch.inc', ); // We want action links for updating projects at a few different locations: @@ -289,8 +287,8 @@ function update_cron() { // If the configured update interval has elapsed, we want to invalidate // the data for all projects, attempt to re-fetch, and trigger any // configured notifications about the new status. - update_refresh(); - update_fetch_data(); + \Drupal::service('update.fetch')->refresh(); + \Drupal::service('update.fetch')->fetchData(); } else { // Otherwise, see if any individual projects are now stale or still @@ -301,8 +299,7 @@ function update_cron() { if ((REQUEST_TIME - $last_email_notice) > $interval) { // If configured time between notifications elapsed, send email about // updates possibly available. - module_load_include('inc', 'update', 'update.fetch'); - _update_cron_notify(); + \Drupal::service('update.fetch')->cronNotify(); } // Clear garbage from disk. @@ -390,11 +387,11 @@ function update_get_available($refresh = FALSE) { // Grab whatever data we currently have. $available = Drupal::keyValueExpirable('update_available_releases')->getAll(); - $projects = update_get_projects(); + $projects = \Drupal::service('update.compare')->getProjects(); foreach ($projects as $key => $project) { // If there's no data at all, we clearly need to fetch some. if (empty($available[$key])) { - update_create_fetch_task($project); + \Drupal::service('update.fetch')->createFetchTask($project); $needs_refresh = TRUE; continue; } @@ -417,14 +414,14 @@ function update_get_available($refresh = FALSE) { // If we think this project needs to fetch, actually create the task now // and remember that we think we're missing some data. if (!empty($available[$key]['fetch_status']) && $available[$key]['fetch_status'] == UPDATE_FETCH_PENDING) { - update_create_fetch_task($project); + \Drupal::service('update.fetch')->createFetchTask($project); $needs_refresh = TRUE; } } if ($needs_refresh && $refresh) { // Attempt to drain the queue of fetch tasks. - update_fetch_data(); + \Drupal::service('update.fetch')->fetchData(); // After processing the queue, we've (hopefully) got better data, so pull // the latest data again and use that directly. $available = Drupal::keyValueExpirable('update_available_releases')->getAll(); @@ -434,38 +431,14 @@ function update_get_available($refresh = FALSE) { } /** - * Creates a new fetch task after loading the necessary include file. - * - * @param $project - * Associative array of information about a project. See update_get_projects() - * for the keys used. - * - * @see _update_create_fetch_task() - */ -function update_create_fetch_task($project) { - module_load_include('inc', 'update', 'update.fetch'); - return _update_create_fetch_task($project); -} - -/** * Refreshes the release data after loading the necessary include file. * * @see _update_refresh() */ function update_refresh() { - module_load_include('inc', 'update', 'update.fetch'); - return _update_refresh(); + return \Drupal::service('update.fetch')->refresh(); } -/** - * Attempts to fetch update data after loading the necessary include file. - * - * @see _update_fetch_data() - */ -function update_fetch_data() { - module_load_include('inc', 'update', 'update.fetch'); - return _update_fetch_data(); -} /** * Implements hook_mail(). @@ -810,3 +783,29 @@ function update_delete_file_if_stale($path) { } } } + +/** + * Batch callback: Performs actions when all fetch tasks have been completed. + * + * @param $success + * TRUE if the batch operation was successful; FALSE if there were errors. + * @param $results + * An associative array of results from the batch operation, including the key + * 'updated' which holds the total number of projects we fetched available + * update data for. + */ +function update_fetch_data_finished($success, $results) { + if ($success) { + if (!empty($results)) { + if (!empty($results['updated'])) { + drupal_set_message(format_plural($results['updated'], 'Checked available update data for one project.', 'Checked available update data for @count projects.')); + } + if (!empty($results['failures'])) { + drupal_set_message(format_plural($results['failures'], 'Failed to get available update data for one project.', 'Failed to get available update data for @count projects.'), 'error'); + } + } + } + else { + drupal_set_message(t('An error occurred trying to get available update data.'), 'error'); + } +} diff --git a/core/modules/update/update.routing.yml b/core/modules/update/update.routing.yml index 16ac4b4..76bd488 100644 --- a/core/modules/update/update.routing.yml +++ b/core/modules/update/update.routing.yml @@ -11,3 +11,10 @@ update_status: _content: '\Drupal\update\Controller\UpdateController::updateStatus' requirements: _permission: 'administer site configuration' + +update_check_manual: + pattern: '/admin/reports/updates/check' + defaults: + _content: '\Drupal\update\Controller\UpdateFetchController::updateManualStatus' + requirements: + _permission: 'administer site configuration' \ No newline at end of file diff --git a/core/modules/update/update.services.yml b/core/modules/update/update.services.yml new file mode 100644 index 0000000..502986f --- /dev/null +++ b/core/modules/update/update.services.yml @@ -0,0 +1,5 @@ +services: + update.fetch: + class: Drupal\update\UpdateFetchManager + update.compare: + class: Drupal\update\UpdateCompareManager \ No newline at end of file