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..765b311 --- /dev/null +++ b/core/modules/update/lib/Drupal/update/Controller/UpdateFetchController.php @@ -0,0 +1,97 @@ +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..8116893 --- /dev/null +++ b/core/modules/update/lib/Drupal/update/UpdateCompareManager.php @@ -0,0 +1,354 @@ +projectStorage('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(); + $this->processInfoList($projects, $module_data, 'module', TRUE); + $this->processInfoList($projects, $theme_data, 'theme', TRUE); + if (config('update.settings')->get('check.disabled_extensions')) { + $this->processInfoList($projects, $module_data, 'module', FALSE); + $this->processInfoList($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; + } + + /** + * Retrieves update storage data or empties it. + * + * Two very expensive arrays computed by this module are the list of all + * installed modules and themes (and .info.yml data, project associations, etc), and + * the current status of the site relative to the currently available releases. + * These two arrays are stored and used whenever possible. The data is cleared + * whenever the administrator visits the status report, available updates + * report, or the module or theme administration pages, since we should always + * recompute the most current values on any of those pages. + * + * Note: while both of these arrays are expensive to compute (in terms of disk + * I/O and some fairly heavy CPU processing), neither of these is the actual + * data about available updates that we have to fetch over the network from + * updates.drupal.org. That information is stored in the + * 'update_available_releases' collection -- it needs to persist longer than 1 + * hour and never get invalidated just by visiting a page on the site. + * + * @param $key + * The key of data to return. Valid options are 'update_project_data' and + * 'update_project_projects'. + * + * @return + * The stored value of the $projects array generated by + * update_calculate_project_data() or update_get_projects(), or an empty array + * when the storage is cleared. + */ + function projectStorage($key) { + $projects = array(); + + // On certain paths, we should clear the data and recompute the projects for + // update status of the site to avoid presenting stale information. + $paths = array( + 'admin/modules', + 'admin/modules/update', + 'admin/appearance', + 'admin/appearance/update', + 'admin/reports', + 'admin/reports/updates', + 'admin/reports/updates/update', + 'admin/reports/status', + 'admin/reports/updates/check', + ); + if (in_array(current_path(), $paths)) { + \Drupal::keyValueExpirable('update')->delete($key); + } + else { + $projects = \Drupal::keyValueExpirable('update')->get($key); + } + return $projects; + } + + /** + * Populates an array of project data. + * + * This iterates over a list of the installed modules or themes and groups + * them by project and status. A few parts of this function assume that + * enabled modules and themes are always processed first, and if disabled + * modules or themes are being processed (there is a setting to control if + * disabled code should be included in the Available updates report or not), + * those are only processed after $projects has been populated with + * information about the enabled code. 'Hidden' modules are always ignored. + * 'Hidden' themes are ignored only if they have no enabled sub-themes. + * This function also records the latest change time on the .info.yml + * files for each module or theme, which is important data which is used when + * deciding if the available update data should be invalidated. + * + * @param $projects + * Reference to the array of project data of what's installed on this site. + * @param $list + * Array of data to process to add the relevant info to the $projects array. + * @param $project_type + * The kind of data in the list. Can be 'module' or 'theme'. + * @param $status + * Boolean that controls what status (enabled or disabled) to process out of + * the $list and add to the $projects array. + * @param $additional_whitelist + * (optional) Array of additional elements to be collected from the .info.yml + * file. Defaults to array(). + * + * @see update_get_projects() + */ + function processInfoList(&$projects, $list, $project_type, $status, $additional_whitelist = array()) { + foreach ($list as $file) { + // A disabled or hidden base theme of an enabled sub-theme still has all + // of its code run by the sub-theme, so we include it in our "enabled" + // projects list. + if ($status && !empty($file->sub_themes)) { + foreach ($file->sub_themes as $key => $name) { + // Build a list of enabled sub-themes. + if ($list[$key]->status) { + $file->enabled_sub_themes[$key] = $name; + } + } + // If the theme is disabled and there are no enabled subthemes, we + // should ignore this base theme for the enabled case. If the site is + // trying to display disabled themes, we'll catch it then. + if (!$file->status && empty($file->enabled_sub_themes)) { + continue; + } + } + // Otherwise, just add projects of the proper status to our list. + elseif ($file->status != $status) { + continue; + } + + // Skip if the .info.yml file is broken. + if (empty($file->info)) { + continue; + } + + // Skip if it's a hidden module or hidden theme without enabled sub-themes. + if (!empty($file->info['hidden']) && empty($file->enabled_sub_themes)) { + continue; + } + + // If the .info.yml doesn't define the 'project', try to figure it out. + if (!isset($file->info['project'])) { + $file->info['project'] = $this->getProjectName($file); + } + + // If we still don't know the 'project', give up. + if (empty($file->info['project'])) { + continue; + } + + // If we don't already know it, grab the change time on the .info.yml file + // itself. Note: we need to use the ctime, not the mtime (modification + // time) since many (all?) tar implementations will go out of their way to + // set the mtime on the files it creates to the timestamps recorded in the + // tarball. We want to see the last time the file was changed on disk, + // which is left alone by tar and correctly set to the time the .info.yml + // file was unpacked. + if (!isset($file->info['_info_file_ctime'])) { + $info_filename = dirname($file->uri) . '/' . $file->name . '.info.yml'; + $file->info['_info_file_ctime'] = filectime($info_filename); + } + + if (!isset($file->info['datestamp'])) { + $file->info['datestamp'] = 0; + } + + $project_name = $file->info['project']; + + // Figure out what project type we're going to use to display this module + // or theme. If the project name is 'drupal', we don't want it to show up + // under the usual "Modules" section, we put it at a special "Drupal Core" + // section at the top of the report. + if ($project_name == 'drupal') { + $project_display_type = 'core'; + } + else { + $project_display_type = $project_type; + } + if (empty($status) && empty($file->enabled_sub_themes)) { + // If we're processing disabled modules or themes, append a suffix. + // However, we don't do this to a a base theme with enabled + // subthemes, since we treat that case as if it is enabled. + $project_display_type .= '-disabled'; + } + // Add a list of sub-themes that "depend on" the project and a list of base + // themes that are "required by" the project. + if ($project_name == 'drupal') { + // Drupal core is always required, so this extra info would be noise. + $sub_themes = array(); + $base_themes = array(); + } + else { + // Add list of enabled sub-themes. + $sub_themes = !empty($file->enabled_sub_themes) ? $file->enabled_sub_themes : array(); + // Add list of base themes. + $base_themes = !empty($file->base_themes) ? $file->base_themes : array(); + } + if (!isset($projects[$project_name])) { + // Only process this if we haven't done this project, since a single + // project can have multiple modules or themes. + $projects[$project_name] = array( + 'name' => $project_name, + // Only save attributes from the .info.yml file we care about so we do + // not bloat our RAM usage needlessly. + 'info' => $this->filterProjectInfo($file->info, $additional_whitelist), + 'datestamp' => $file->info['datestamp'], + 'includes' => array($file->name => $file->info['name']), + 'project_type' => $project_display_type, + 'project_status' => $status, + 'sub_themes' => $sub_themes, + 'base_themes' => $base_themes, + ); + } + elseif ($projects[$project_name]['project_type'] == $project_display_type) { + // Only add the file we're processing to the 'includes' array for this + // project if it is of the same type and status (which is encoded in the + // $project_display_type). This prevents listing all the disabled + // modules included with an enabled project if we happen to be checking + // for disabled modules, too. + $projects[$project_name]['includes'][$file->name] = $file->info['name']; + $projects[$project_name]['info']['_info_file_ctime'] = max($projects[$project_name]['info']['_info_file_ctime'], $file->info['_info_file_ctime']); + $projects[$project_name]['datestamp'] = max($projects[$project_name]['datestamp'], $file->info['datestamp']); + if (!empty($sub_themes)) { + $projects[$project_name]['sub_themes'] += $sub_themes; + } + if (!empty($base_themes)) { + $projects[$project_name]['base_themes'] += $base_themes; + } + } + elseif (empty($status)) { + // If we have a project_name that matches, but the project_display_type + // does not, it means we're processing a disabled module or theme that + // belongs to a project that has some enabled code. In this case, we add + // the disabled thing into a separate array for separate display. + $projects[$project_name]['disabled'][$file->name] = $file->info['name']; + } + } + } + + /** + * Determines what project a given file object belongs to. + * + * @param $file + * A file object as returned by system_get_files_database(). + * + * @return + * The canonical project short name. + * + * @see system_get_files_database() + */ + function getProjectName($file) { + $project_name = ''; + if (isset($file->info['project'])) { + $project_name = $file->info['project']; + } + elseif (isset($file->filename) && (strpos($file->filename, 'core/modules') === 0)) { + $project_name = 'drupal'; + } + return $project_name; + } + + /** + * Filters the project .info.yml data to only save attributes we need. + * + * @param array $info + * Array of .info.yml file data as returned by drupal_parse_info_file(). + * @param $additional_whitelist + * (optional) Array of additional elements to be collected from the .info.yml + * file. Defaults to array(). + * + * @return + * Array of .info.yml file data we need for the update manager. + * + * @see update_process_info_list() + */ + function filterProjectInfo($info, $additional_whitelist = array()) { + $whitelist = array( + '_info_file_ctime', + 'datestamp', + 'major', + 'name', + 'package', + 'project', + 'project status url', + 'version', + ); + $whitelist = array_merge($whitelist, $additional_whitelist); + return array_intersect_key($info, drupal_map_assoc($whitelist)); + } +} 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..1282a50 --- /dev/null +++ b/core/modules/update/lib/Drupal/update/UpdateFetchManager.php @@ -0,0 +1,332 @@ +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. + \Drupal::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 string + * 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 information 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 string + * 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|NULL + * 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..26bd09b 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(); } /** @@ -107,136 +87,7 @@ function update_get_projects() { * @see update_get_projects() */ function update_process_info_list(&$projects, $list, $project_type, $status, $additional_whitelist = array()) { - foreach ($list as $file) { - // A disabled or hidden base theme of an enabled sub-theme still has all - // of its code run by the sub-theme, so we include it in our "enabled" - // projects list. - if ($status && !empty($file->sub_themes)) { - foreach ($file->sub_themes as $key => $name) { - // Build a list of enabled sub-themes. - if ($list[$key]->status) { - $file->enabled_sub_themes[$key] = $name; - } - } - // If the theme is disabled and there are no enabled subthemes, we - // should ignore this base theme for the enabled case. If the site is - // trying to display disabled themes, we'll catch it then. - if (!$file->status && empty($file->enabled_sub_themes)) { - continue; - } - } - // Otherwise, just add projects of the proper status to our list. - elseif ($file->status != $status) { - continue; - } - - // Skip if the .info.yml file is broken. - if (empty($file->info)) { - continue; - } - - // Skip if it's a hidden module or hidden theme without enabled sub-themes. - if (!empty($file->info['hidden']) && empty($file->enabled_sub_themes)) { - continue; - } - - // If the .info.yml doesn't define the 'project', try to figure it out. - if (!isset($file->info['project'])) { - $file->info['project'] = update_get_project_name($file); - } - - // If we still don't know the 'project', give up. - if (empty($file->info['project'])) { - continue; - } - - // If we don't already know it, grab the change time on the .info.yml file - // itself. Note: we need to use the ctime, not the mtime (modification - // time) since many (all?) tar implementations will go out of their way to - // set the mtime on the files it creates to the timestamps recorded in the - // tarball. We want to see the last time the file was changed on disk, - // which is left alone by tar and correctly set to the time the .info.yml - // file was unpacked. - if (!isset($file->info['_info_file_ctime'])) { - $info_filename = dirname($file->uri) . '/' . $file->name . '.info.yml'; - $file->info['_info_file_ctime'] = filectime($info_filename); - } - - if (!isset($file->info['datestamp'])) { - $file->info['datestamp'] = 0; - } - - $project_name = $file->info['project']; - - // Figure out what project type we're going to use to display this module - // or theme. If the project name is 'drupal', we don't want it to show up - // under the usual "Modules" section, we put it at a special "Drupal Core" - // section at the top of the report. - if ($project_name == 'drupal') { - $project_display_type = 'core'; - } - else { - $project_display_type = $project_type; - } - if (empty($status) && empty($file->enabled_sub_themes)) { - // If we're processing disabled modules or themes, append a suffix. - // However, we don't do this to a a base theme with enabled - // subthemes, since we treat that case as if it is enabled. - $project_display_type .= '-disabled'; - } - // Add a list of sub-themes that "depend on" the project and a list of base - // themes that are "required by" the project. - if ($project_name == 'drupal') { - // Drupal core is always required, so this extra info would be noise. - $sub_themes = array(); - $base_themes = array(); - } - else { - // Add list of enabled sub-themes. - $sub_themes = !empty($file->enabled_sub_themes) ? $file->enabled_sub_themes : array(); - // Add list of base themes. - $base_themes = !empty($file->base_themes) ? $file->base_themes : array(); - } - if (!isset($projects[$project_name])) { - // Only process this if we haven't done this project, since a single - // project can have multiple modules or themes. - $projects[$project_name] = array( - 'name' => $project_name, - // Only save attributes from the .info.yml file we care about so we do - // not bloat our RAM usage needlessly. - 'info' => update_filter_project_info($file->info, $additional_whitelist), - 'datestamp' => $file->info['datestamp'], - 'includes' => array($file->name => $file->info['name']), - 'project_type' => $project_display_type, - 'project_status' => $status, - 'sub_themes' => $sub_themes, - 'base_themes' => $base_themes, - ); - } - elseif ($projects[$project_name]['project_type'] == $project_display_type) { - // Only add the file we're processing to the 'includes' array for this - // project if it is of the same type and status (which is encoded in the - // $project_display_type). This prevents listing all the disabled - // modules included with an enabled project if we happen to be checking - // for disabled modules, too. - $projects[$project_name]['includes'][$file->name] = $file->info['name']; - $projects[$project_name]['info']['_info_file_ctime'] = max($projects[$project_name]['info']['_info_file_ctime'], $file->info['_info_file_ctime']); - $projects[$project_name]['datestamp'] = max($projects[$project_name]['datestamp'], $file->info['datestamp']); - if (!empty($sub_themes)) { - $projects[$project_name]['sub_themes'] += $sub_themes; - } - if (!empty($base_themes)) { - $projects[$project_name]['base_themes'] += $base_themes; - } - } - elseif (empty($status)) { - // If we have a project_name that matches, but the project_display_type - // does not, it means we're processing a disabled module or theme that - // belongs to a project that has some enabled code. In this case, we add - // the disabled thing into a separate array for separate display. - $projects[$project_name]['disabled'][$file->name] = $file->info['name']; - } - } + return \Drupal::service('update.compare')->processInfoList($projects, $list, $project_type, $status, $additional_whitelist); } /** @@ -251,14 +102,7 @@ function update_process_info_list(&$projects, $list, $project_type, $status, $ad * @see system_get_files_database() */ function update_get_project_name($file) { - $project_name = ''; - if (isset($file->info['project'])) { - $project_name = $file->info['project']; - } - elseif (isset($file->filename) && (strpos($file->filename, 'core/modules') === 0)) { - $project_name = 'drupal'; - } - return $project_name; + return \Drupal::service('update.compare')->getProjectName($file); } /** @@ -777,28 +621,7 @@ function update_calculate_project_update_status(&$project_data, $available) { * when the storage is cleared. */ function update_project_storage($key) { - $projects = array(); - - // On certain paths, we should clear the data and recompute the projects for - // update status of the site to avoid presenting stale information. - $paths = array( - 'admin/modules', - 'admin/modules/update', - 'admin/appearance', - 'admin/appearance/update', - 'admin/reports', - 'admin/reports/updates', - 'admin/reports/updates/update', - 'admin/reports/status', - 'admin/reports/updates/check', - ); - if (in_array(current_path(), $paths)) { - Drupal::keyValueExpirable('update')->delete($key); - } - else { - $projects = Drupal::keyValueExpirable('update')->get($key); - } - return $projects; + return \Drupal::service('update.compare')->projectStorage($key); } /** @@ -816,16 +639,5 @@ function update_project_storage($key) { * @see update_process_info_list() */ function update_filter_project_info($info, $additional_whitelist = array()) { - $whitelist = array( - '_info_file_ctime', - 'datestamp', - 'major', - 'name', - 'package', - 'project', - 'project status url', - 'version', - ); - $whitelist = array_merge($whitelist, $additional_whitelist); - return array_intersect_key($info, drupal_map_assoc($whitelist)); + return \Drupal::service('update.compare')->filterProjectInfo($info, $additional_whitelist); } diff --git a/core/modules/update/update.fetch.inc b/core/modules/update/update.fetch.inc deleted file mode 100644 index 96f00f1..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. - Drupal::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']) { - Drupal::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 83ba194..87d41b7 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..1c97981 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' diff --git a/core/modules/update/update.services.yml b/core/modules/update/update.services.yml new file mode 100644 index 0000000..a60f412 --- /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