Chris@0: getEditable('core.extension'); Chris@0: $save = FALSE; Chris@0: foreach (['module', 'theme'] as $type) { Chris@0: foreach ($extension_config->get($type) as $name => $weight) { Chris@0: if (update_check_incompatibility($name, $type)) { Chris@0: $extension_config->clear("$type.$name"); Chris@0: $save = TRUE; Chris@0: } Chris@0: } Chris@0: } Chris@0: if ($save) { Chris@0: $extension_config->set('module', module_config_sort($extension_config->get('module'))); Chris@0: $extension_config->save(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the compatibility of a module or theme. Chris@0: */ Chris@0: function update_check_incompatibility($name, $type = 'module') { Chris@0: static $themes, $modules; Chris@0: Chris@0: // Store values of expensive functions for future use. Chris@0: if (empty($themes) || empty($modules)) { Chris@0: // We need to do a full rebuild here to make sure the database reflects any Chris@0: // code changes that were made in the filesystem before the update script Chris@0: // was initiated. Chris@0: $themes = \Drupal::service('theme_handler')->rebuildThemeData(); Chris@0: $modules = system_rebuild_module_data(); Chris@0: } Chris@0: Chris@0: if ($type == 'module' && isset($modules[$name])) { Chris@0: $file = $modules[$name]; Chris@0: } Chris@0: elseif ($type == 'theme' && isset($themes[$name])) { Chris@0: $file = $themes[$name]; Chris@0: } Chris@0: if (!isset($file) Chris@0: || !isset($file->info['core']) Chris@0: || $file->info['core'] != \Drupal::CORE_COMPATIBILITY Chris@0: || version_compare(phpversion(), $file->info['php']) < 0) { Chris@0: return TRUE; Chris@0: } Chris@0: return FALSE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns whether the minimum schema requirement has been satisfied. Chris@0: * Chris@0: * @return array Chris@0: * A requirements info array. Chris@0: */ Chris@0: function update_system_schema_requirements() { Chris@0: $requirements = []; Chris@0: Chris@0: $system_schema = drupal_get_installed_schema_version('system'); Chris@0: Chris@0: $requirements['minimum schema']['title'] = 'Minimum schema version'; Chris@0: if ($system_schema >= \Drupal::CORE_MINIMUM_SCHEMA_VERSION) { Chris@0: $requirements['minimum schema'] += [ Chris@0: 'value' => 'The installed schema version meets the minimum.', Chris@0: 'description' => 'Schema version: ' . $system_schema, Chris@0: ]; Chris@0: } Chris@0: else { Chris@0: $requirements['minimum schema'] += [ Chris@0: 'value' => 'The installed schema version does not meet the minimum.', Chris@0: 'severity' => REQUIREMENT_ERROR, Chris@12: 'description' => 'Your system schema version is ' . $system_schema . '. Updating directly from a schema version prior to 8000 is not supported. You must upgrade your site to Drupal 8 first, see https://www.drupal.org/docs/8/upgrade.', Chris@0: ]; Chris@0: } Chris@0: Chris@0: return $requirements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks update requirements and reports errors and (optionally) warnings. Chris@0: */ Chris@0: function update_check_requirements() { Chris@0: // Check requirements of all loaded modules. Chris@0: $requirements = \Drupal::moduleHandler()->invokeAll('requirements', ['update']); Chris@0: $requirements += update_system_schema_requirements(); Chris@0: return $requirements; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Forces a module to a given schema version. Chris@0: * Chris@0: * This function is rarely necessary. Chris@0: * Chris@0: * @param string $module Chris@0: * Name of the module. Chris@0: * @param string $schema_version Chris@0: * The schema version the module should be set to. Chris@0: */ Chris@0: function update_set_schema($module, $schema_version) { Chris@0: \Drupal::keyValue('system.schema')->set($module, $schema_version); Chris@18: \Drupal::service('extension.list.profile')->reset(); Chris@18: \Drupal::service('extension.list.module')->reset(); Chris@18: \Drupal::service('extension.list.theme_engine')->reset(); Chris@18: \Drupal::service('extension.list.theme')->reset(); Chris@18: drupal_static_reset('drupal_get_installed_schema_version'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Implements callback_batch_operation(). Chris@0: * Chris@0: * Performs one update and stores the results for display on the results page. Chris@0: * Chris@0: * If an update function completes successfully, it should return a message Chris@0: * as a string indicating success, for example: Chris@0: * @code Chris@0: * return t('New index added successfully.'); Chris@0: * @endcode Chris@0: * Chris@0: * Alternatively, it may return nothing. In that case, no message Chris@0: * will be displayed at all. Chris@0: * Chris@0: * If it fails for whatever reason, it should throw an instance of Chris@0: * Drupal\Core\Utility\UpdateException with an appropriate error message, for Chris@0: * example: Chris@0: * @code Chris@0: * use Drupal\Core\Utility\UpdateException; Chris@0: * throw new UpdateException(t('Description of what went wrong')); Chris@0: * @endcode Chris@0: * Chris@0: * If an exception is thrown, the current update and all updates that depend on Chris@0: * it will be aborted. The schema version will not be updated in this case, and Chris@0: * all the aborted updates will continue to appear on update.php as updates Chris@0: * that have not yet been run. Chris@0: * Chris@0: * If an update function needs to be re-run as part of a batch process, it Chris@0: * should accept the $sandbox array by reference as its first parameter Chris@0: * and set the #finished property to the percentage completed that it is, as a Chris@0: * fraction of 1. Chris@0: * Chris@0: * @param $module Chris@0: * The module whose update will be run. Chris@0: * @param $number Chris@0: * The update number to run. Chris@0: * @param $dependency_map Chris@0: * An array whose keys are the names of all update functions that will be Chris@0: * performed during this batch process, and whose values are arrays of other Chris@0: * update functions that each one depends on. Chris@0: * @param $context Chris@0: * The batch context array. Chris@0: * Chris@0: * @see update_resolve_dependencies() Chris@0: */ Chris@0: function update_do_one($module, $number, $dependency_map, &$context) { Chris@0: $function = $module . '_update_' . $number; Chris@0: Chris@0: // If this update was aborted in a previous step, or has a dependency that Chris@0: // was aborted in a previous step, go no further. Chris@0: if (!empty($context['results']['#abort']) && array_intersect($context['results']['#abort'], array_merge($dependency_map, [$function]))) { Chris@0: return; Chris@0: } Chris@0: Chris@0: $ret = []; Chris@0: if (function_exists($function)) { Chris@0: try { Chris@0: $ret['results']['query'] = $function($context['sandbox']); Chris@0: $ret['results']['success'] = TRUE; Chris@0: } Chris@0: // @TODO We may want to do different error handling for different Chris@0: // exception types, but for now we'll just log the exception and Chris@0: // return the message for printing. Chris@0: // @see https://www.drupal.org/node/2564311 Chris@0: catch (Exception $e) { Chris@0: watchdog_exception('update', $e); Chris@0: Chris@0: $variables = Error::decodeException($e); Chris@0: unset($variables['backtrace']); Chris@0: $ret['#abort'] = ['success' => FALSE, 'query' => t('%type: @message in %function (line %line of %file).', $variables)]; Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($context['sandbox']['#finished'])) { Chris@0: $context['finished'] = $context['sandbox']['#finished']; Chris@0: unset($context['sandbox']['#finished']); Chris@0: } Chris@0: Chris@0: if (!isset($context['results'][$module])) { Chris@0: $context['results'][$module] = []; Chris@0: } Chris@0: if (!isset($context['results'][$module][$number])) { Chris@0: $context['results'][$module][$number] = []; Chris@0: } Chris@0: $context['results'][$module][$number] = array_merge($context['results'][$module][$number], $ret); Chris@0: Chris@0: if (!empty($ret['#abort'])) { Chris@0: // Record this function in the list of updates that were aborted. Chris@0: $context['results']['#abort'][] = $function; Chris@0: } Chris@0: Chris@0: // Record the schema update if it was completed successfully. Chris@0: if ($context['finished'] == 1 && empty($ret['#abort'])) { Chris@0: drupal_set_installed_schema_version($module, $number); Chris@0: } Chris@0: Chris@0: $context['message'] = t('Updating @module', ['@module' => $module]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Executes a single hook_post_update_NAME(). Chris@0: * Chris@0: * @param string $function Chris@0: * The function name, that should be executed. Chris@0: * @param array $context Chris@0: * The batch context array. Chris@0: */ Chris@0: function update_invoke_post_update($function, &$context) { Chris@0: $ret = []; Chris@0: Chris@0: // If this update was aborted in a previous step, or has a dependency that was Chris@0: // aborted in a previous step, go no further. Chris@0: if (!empty($context['results']['#abort'])) { Chris@0: return; Chris@0: } Chris@0: Chris@0: list($module, $name) = explode('_post_update_', $function, 2); Chris@0: module_load_include('php', $module, $module . '.post_update'); Chris@0: if (function_exists($function)) { Chris@0: try { Chris@0: $ret['results']['query'] = $function($context['sandbox']); Chris@0: $ret['results']['success'] = TRUE; Chris@0: Chris@0: if (!isset($context['sandbox']['#finished']) || (isset($context['sandbox']['#finished']) && $context['sandbox']['#finished'] >= 1)) { Chris@0: \Drupal::service('update.post_update_registry')->registerInvokedUpdates([$function]); Chris@0: } Chris@0: } Chris@0: // @TODO We may want to do different error handling for different exception Chris@0: // types, but for now we'll just log the exception and return the message Chris@0: // for printing. Chris@0: // @see https://www.drupal.org/node/2564311 Chris@0: catch (Exception $e) { Chris@0: watchdog_exception('update', $e); Chris@0: Chris@0: $variables = Error::decodeException($e); Chris@0: unset($variables['backtrace']); Chris@0: $ret['#abort'] = [ Chris@0: 'success' => FALSE, Chris@0: 'query' => t('%type: @message in %function (line %line of %file).', $variables), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: Chris@0: if (isset($context['sandbox']['#finished'])) { Chris@0: $context['finished'] = $context['sandbox']['#finished']; Chris@0: unset($context['sandbox']['#finished']); Chris@0: } Chris@0: if (!isset($context['results'][$module][$name])) { Chris@0: $context['results'][$module][$name] = []; Chris@0: } Chris@0: $context['results'][$module][$name] = array_merge($context['results'][$module][$name], $ret); Chris@0: Chris@0: if (!empty($ret['#abort'])) { Chris@0: // Record this function in the list of updates that were aborted. Chris@0: $context['results']['#abort'][] = $function; Chris@0: } Chris@0: Chris@0: $context['message'] = t('Post updating @module', ['@module' => $module]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns a list of all the pending database updates. Chris@0: * Chris@0: * @return Chris@0: * An associative array keyed by module name which contains all information Chris@0: * about database updates that need to be run, and any updates that are not Chris@0: * going to proceed due to missing requirements. The system module will Chris@0: * always be listed first. Chris@0: * Chris@0: * The subarray for each module can contain the following keys: Chris@0: * - start: The starting update that is to be processed. If this does not Chris@0: * exist then do not process any updates for this module as there are Chris@0: * other requirements that need to be resolved. Chris@0: * - warning: Any warnings about why this module can not be updated. Chris@0: * - pending: An array of all the pending updates for the module including Chris@0: * the update number and the description from source code comment for Chris@0: * each update function. This array is keyed by the update number. Chris@0: */ Chris@0: function update_get_update_list() { Chris@0: // Make sure that the system module is first in the list of updates. Chris@0: $ret = ['system' => []]; Chris@0: Chris@0: $modules = drupal_get_installed_schema_version(NULL, FALSE, TRUE); Chris@0: foreach ($modules as $module => $schema_version) { Chris@0: // Skip uninstalled and incompatible modules. Chris@0: if ($schema_version == SCHEMA_UNINSTALLED || update_check_incompatibility($module)) { Chris@0: continue; Chris@0: } Chris@0: // Display a requirements error if the user somehow has a schema version Chris@0: // from the previous Drupal major version. Chris@0: if ($schema_version < \Drupal::CORE_MINIMUM_SCHEMA_VERSION) { Chris@0: $ret[$module]['warning'] = '' . $module . ' module cannot be updated. Its schema version is ' . $schema_version . ', which is from an earlier major release of Drupal. You will need to migrate the data for this module instead.'; Chris@0: continue; Chris@0: } Chris@0: // Otherwise, get the list of updates defined by this module. Chris@0: $updates = drupal_get_schema_versions($module); Chris@0: if ($updates !== FALSE) { Chris@0: // \Drupal::moduleHandler()->invoke() returns NULL for non-existing hooks, Chris@0: // so if no updates are removed, it will == 0. Chris@0: $last_removed = \Drupal::moduleHandler()->invoke($module, 'update_last_removed'); Chris@0: if ($schema_version < $last_removed) { Chris@0: $ret[$module]['warning'] = '' . $module . ' module cannot be updated. Its schema version is ' . $schema_version . '. Updates up to and including ' . $last_removed . ' have been removed in this release. In order to update ' . $module . ' module, you will first need to upgrade to the last version in which these updates were available.'; Chris@0: continue; Chris@0: } Chris@0: Chris@14: foreach ($updates as $update) { Chris@0: if ($update == \Drupal::CORE_MINIMUM_SCHEMA_VERSION) { Chris@0: $ret[$module]['warning'] = '' . $module . ' module cannot be updated. It contains an update numbered as ' . \Drupal::CORE_MINIMUM_SCHEMA_VERSION . ' which is reserved for the earliest installation of a module in Drupal ' . \Drupal::CORE_COMPATIBILITY . ', before any updates. In order to update ' . $module . ' module, you will need to install a version of the module with valid updates.'; Chris@0: continue 2; Chris@0: } Chris@0: if ($update > $schema_version) { Chris@0: // The description for an update comes from its Doxygen. Chris@0: $func = new ReflectionFunction($module . '_update_' . $update); Chris@0: $description = str_replace(["\n", '*', '/'], '', $func->getDocComment()); Chris@0: $ret[$module]['pending'][$update] = "$update - $description"; Chris@0: if (!isset($ret[$module]['start'])) { Chris@0: $ret[$module]['start'] = $update; Chris@0: } Chris@0: } Chris@0: } Chris@0: if (!isset($ret[$module]['start']) && isset($ret[$module]['pending'])) { Chris@0: $ret[$module]['start'] = $schema_version; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: if (empty($ret['system'])) { Chris@0: unset($ret['system']); Chris@0: } Chris@0: return $ret; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resolves dependencies in a set of module updates, and orders them correctly. Chris@0: * Chris@0: * This function receives a list of requested module updates and determines an Chris@0: * appropriate order to run them in such that all update dependencies are met. Chris@0: * Any updates whose dependencies cannot be met are included in the returned Chris@0: * array but have the key 'allowed' set to FALSE; the calling function should Chris@0: * take responsibility for ensuring that these updates are ultimately not Chris@0: * performed. Chris@0: * Chris@0: * In addition, the returned array also includes detailed information about the Chris@0: * dependency chain for each update, as provided by the depth-first search Chris@0: * algorithm in Drupal\Component\Graph\Graph::searchAndSort(). Chris@0: * Chris@0: * @param $starting_updates Chris@0: * An array whose keys contain the names of modules with updates to be run Chris@0: * and whose values contain the number of the first requested update for that Chris@0: * module. Chris@0: * Chris@0: * @return Chris@0: * An array whose keys are the names of all update functions within the Chris@0: * provided modules that would need to be run in order to fulfill the Chris@0: * request, arranged in the order in which the update functions should be Chris@0: * run. (This includes the provided starting update for each module and all Chris@0: * subsequent updates that are available.) The values are themselves arrays Chris@0: * containing all the keys provided by the Chris@0: * Drupal\Component\Graph\Graph::searchAndSort() algorithm, which encode Chris@0: * detailed information about the dependency chain for this update function Chris@0: * (for example: 'paths', 'reverse_paths', 'weight', and 'component'), as Chris@0: * well as the following additional keys: Chris@0: * - 'allowed': A boolean which is TRUE when the update function's Chris@0: * dependencies are met, and FALSE otherwise. Calling functions should Chris@0: * inspect this value before running the update. Chris@0: * - 'missing_dependencies': An array containing the names of any other Chris@0: * update functions that are required by this one but that are unavailable Chris@0: * to be run. This array will be empty when 'allowed' is TRUE. Chris@0: * - 'module': The name of the module that this update function belongs to. Chris@0: * - 'number': The number of this update function within that module. Chris@0: * Chris@0: * @see \Drupal\Component\Graph\Graph::searchAndSort() Chris@0: */ Chris@0: function update_resolve_dependencies($starting_updates) { Chris@0: // Obtain a dependency graph for the requested update functions. Chris@0: $update_functions = update_get_update_function_list($starting_updates); Chris@0: $graph = update_build_dependency_graph($update_functions); Chris@0: Chris@0: // Perform the depth-first search and sort on the results. Chris@0: $graph_object = new Graph($graph); Chris@0: $graph = $graph_object->searchAndSort(); Chris@0: uasort($graph, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']); Chris@0: Chris@0: foreach ($graph as $function => &$data) { Chris@0: $module = $data['module']; Chris@0: $number = $data['number']; Chris@0: // If the update function is missing and has not yet been performed, mark Chris@0: // it and everything that ultimately depends on it as disallowed. Chris@0: if (update_is_missing($module, $number, $update_functions) && !update_already_performed($module, $number)) { Chris@0: $data['allowed'] = FALSE; Chris@0: foreach (array_keys($data['paths']) as $dependent) { Chris@0: $graph[$dependent]['allowed'] = FALSE; Chris@0: $graph[$dependent]['missing_dependencies'][] = $function; Chris@0: } Chris@0: } Chris@0: elseif (!isset($data['allowed'])) { Chris@0: $data['allowed'] = TRUE; Chris@0: $data['missing_dependencies'] = []; Chris@0: } Chris@0: // Now that we have finished processing this function, remove it from the Chris@0: // graph if it was not part of the original list. This ensures that we Chris@0: // never try to run any updates that were not specifically requested. Chris@0: if (!isset($update_functions[$module][$number])) { Chris@0: unset($graph[$function]); Chris@0: } Chris@0: } Chris@0: Chris@0: return $graph; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an organized list of update functions for a set of modules. Chris@0: * Chris@0: * @param $starting_updates Chris@0: * An array whose keys contain the names of modules and whose values contain Chris@0: * the number of the first requested update for that module. Chris@0: * Chris@0: * @return Chris@0: * An array containing all the update functions that should be run for each Chris@0: * module, including the provided starting update and all subsequent updates Chris@0: * that are available. The keys of the array contain the module names, and Chris@0: * each value is an ordered array of update functions, keyed by the update Chris@0: * number. Chris@0: * Chris@0: * @see update_resolve_dependencies() Chris@0: */ Chris@0: function update_get_update_function_list($starting_updates) { Chris@0: // Go through each module and find all updates that we need (including the Chris@0: // first update that was requested and any updates that run after it). Chris@0: $update_functions = []; Chris@0: foreach ($starting_updates as $module => $version) { Chris@0: $update_functions[$module] = []; Chris@0: $updates = drupal_get_schema_versions($module); Chris@0: if ($updates !== FALSE) { Chris@0: $max_version = max($updates); Chris@0: if ($version <= $max_version) { Chris@0: foreach ($updates as $update) { Chris@0: if ($update >= $version) { Chris@0: $update_functions[$module][$update] = $module . '_update_' . $update; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: return $update_functions; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Constructs a graph which encodes the dependencies between module updates. Chris@0: * Chris@0: * This function returns an associative array which contains a "directed graph" Chris@0: * representation of the dependencies between a provided list of update Chris@0: * functions, as well as any outside update functions that they directly depend Chris@0: * on but that were not in the provided list. The vertices of the graph Chris@0: * represent the update functions themselves, and each edge represents a Chris@0: * requirement that the first update function needs to run before the second. Chris@0: * For example, consider this graph: Chris@0: * Chris@0: * system_update_8001 ---> system_update_8002 ---> system_update_8003 Chris@0: * Chris@0: * Visually, this indicates that system_update_8001() must run before Chris@0: * system_update_8002(), which in turn must run before system_update_8003(). Chris@0: * Chris@0: * The function takes into account standard dependencies within each module, as Chris@0: * shown above (i.e., the fact that each module's updates must run in numerical Chris@0: * order), but also finds any cross-module dependencies that are defined by Chris@0: * modules which implement hook_update_dependencies(), and builds them into the Chris@0: * graph as well. Chris@0: * Chris@0: * @param $update_functions Chris@0: * An organized array of update functions, in the format returned by Chris@0: * update_get_update_function_list(). Chris@0: * Chris@0: * @return Chris@0: * A multidimensional array representing the dependency graph, suitable for Chris@0: * passing in to Drupal\Component\Graph\Graph::searchAndSort(), but with extra Chris@0: * information about each update function also included. Each array key Chris@0: * contains the name of an update function, including all update functions Chris@0: * from the provided list as well as any outside update functions which they Chris@0: * directly depend on. Each value is an associative array containing the Chris@0: * following keys: Chris@0: * - 'edges': A representation of any other update functions that immediately Chris@0: * depend on this one. See Drupal\Component\Graph\Graph::searchAndSort() for Chris@0: * more details on the format. Chris@0: * - 'module': The name of the module that this update function belongs to. Chris@0: * - 'number': The number of this update function within that module. Chris@0: * Chris@0: * @see \Drupal\Component\Graph\Graph::searchAndSort() Chris@0: * @see update_resolve_dependencies() Chris@0: */ Chris@0: function update_build_dependency_graph($update_functions) { Chris@0: // Initialize an array that will define a directed graph representing the Chris@0: // dependencies between update functions. Chris@0: $graph = []; Chris@0: Chris@0: // Go through each update function and build an initial list of dependencies. Chris@0: foreach ($update_functions as $module => $functions) { Chris@0: $previous_function = NULL; Chris@0: foreach ($functions as $number => $function) { Chris@0: // Add an edge to the directed graph representing the fact that each Chris@0: // update function in a given module must run after the update that Chris@0: // numerically precedes it. Chris@0: if ($previous_function) { Chris@0: $graph[$previous_function]['edges'][$function] = TRUE; Chris@0: } Chris@0: $previous_function = $function; Chris@0: Chris@0: // Define the module and update number associated with this function. Chris@0: $graph[$function]['module'] = $module; Chris@0: $graph[$function]['number'] = $number; Chris@0: } Chris@0: } Chris@0: Chris@0: // Now add any explicit update dependencies declared by modules. Chris@0: $update_dependencies = update_retrieve_dependencies(); Chris@0: foreach ($graph as $function => $data) { Chris@0: if (!empty($update_dependencies[$data['module']][$data['number']])) { Chris@0: foreach ($update_dependencies[$data['module']][$data['number']] as $module => $number) { Chris@0: $dependency = $module . '_update_' . $number; Chris@0: $graph[$dependency]['edges'][$function] = TRUE; Chris@0: $graph[$dependency]['module'] = $module; Chris@0: $graph[$dependency]['number'] = $number; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $graph; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if a module update is missing or unavailable. Chris@0: * Chris@0: * @param $module Chris@0: * The name of the module. Chris@0: * @param $number Chris@0: * The number of the update within that module. Chris@0: * @param $update_functions Chris@0: * An organized array of update functions, in the format returned by Chris@0: * update_get_update_function_list(). This should represent all module Chris@0: * updates that are requested to run at the time this function is called. Chris@0: * Chris@0: * @return Chris@0: * TRUE if the provided module update is not installed or is not in the Chris@0: * provided list of updates to run; FALSE otherwise. Chris@0: */ Chris@0: function update_is_missing($module, $number, $update_functions) { Chris@0: return !isset($update_functions[$module][$number]) || !function_exists($update_functions[$module][$number]); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if a module update has already been performed. Chris@0: * Chris@0: * @param $module Chris@0: * The name of the module. Chris@0: * @param $number Chris@0: * The number of the update within that module. Chris@0: * Chris@0: * @return Chris@0: * TRUE if the database schema indicates that the update has already been Chris@0: * performed; FALSE otherwise. Chris@0: */ Chris@0: function update_already_performed($module, $number) { Chris@0: return $number <= drupal_get_installed_schema_version($module); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Invokes hook_update_dependencies() in all installed modules. Chris@0: * Chris@0: * This function is similar to \Drupal::moduleHandler()->invokeAll(), with the Chris@0: * main difference that it does not require that a module be enabled to invoke Chris@0: * its hook, only that it be installed. This allows the update system to Chris@0: * properly perform updates even on modules that are currently disabled. Chris@0: * Chris@0: * @return Chris@0: * An array of return values obtained by merging the results of the Chris@0: * hook_update_dependencies() implementations in all installed modules. Chris@0: * Chris@0: * @see \Drupal\Core\Extension\ModuleHandlerInterface::invokeAll() Chris@0: * @see hook_update_dependencies() Chris@0: */ Chris@0: function update_retrieve_dependencies() { Chris@0: $return = []; Chris@0: // Get a list of installed modules, arranged so that we invoke their hooks in Chris@0: // the same order that \Drupal::moduleHandler()->invokeAll() does. Chris@0: foreach (\Drupal::keyValue('system.schema')->getAll() as $module => $schema) { Chris@0: if ($schema == SCHEMA_UNINSTALLED) { Chris@0: // Nothing to upgrade. Chris@0: continue; Chris@0: } Chris@0: $function = $module . '_update_dependencies'; Chris@0: // Ensure install file is loaded. Chris@0: module_load_install($module); Chris@0: if (function_exists($function)) { Chris@0: $updated_dependencies = $function(); Chris@0: // Each implementation of hook_update_dependencies() returns a Chris@0: // multidimensional, associative array containing some keys that Chris@0: // represent module names (which are strings) and other keys that Chris@0: // represent update function numbers (which are integers). We cannot use Chris@0: // array_merge_recursive() to properly merge these results, since it Chris@0: // treats strings and integers differently. Therefore, we have to Chris@0: // explicitly loop through the expected array structure here and perform Chris@0: // the merge manually. Chris@0: if (isset($updated_dependencies) && is_array($updated_dependencies)) { Chris@0: foreach ($updated_dependencies as $module_name => $module_data) { Chris@0: foreach ($module_data as $update_version => $update_data) { Chris@0: foreach ($update_data as $module_dependency => $update_dependency) { Chris@0: // If there are redundant dependencies declared for the same Chris@0: // update function (so that it is declared to depend on more than Chris@0: // one update from a particular module), record the dependency on Chris@0: // the highest numbered update here, since that automatically Chris@0: // implies the previous ones. For example, if one module's Chris@0: // implementation of hook_update_dependencies() required this Chris@0: // ordering: Chris@0: // Chris@0: // system_update_8002 ---> user_update_8001 Chris@0: // Chris@0: // but another module's implementation of the hook required this Chris@0: // one: Chris@0: // Chris@0: // system_update_8003 ---> user_update_8001 Chris@0: // Chris@0: // we record the second one, since system_update_8002() is always Chris@0: // guaranteed to run before system_update_8003() anyway (within Chris@0: // an individual module, updates are always run in numerical Chris@0: // order). Chris@0: if (!isset($return[$module_name][$update_version][$module_dependency]) || $update_dependency > $return[$module_name][$update_version][$module_dependency]) { Chris@0: $return[$module_name][$update_version][$module_dependency] = $update_dependency; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $return; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Replace permissions during update. Chris@0: * Chris@0: * This function can replace one permission to several or even delete an old Chris@0: * one. Chris@0: * Chris@0: * @param array $replace Chris@0: * An associative array. The keys are the old permissions the values are lists Chris@0: * of new permissions. If the list is an empty array, the old permission is Chris@0: * removed. Chris@0: */ Chris@0: function update_replace_permissions($replace) { Chris@0: $prefix = 'user.role.'; Chris@0: $cut = strlen($prefix); Chris@0: $role_names = \Drupal::service('config.storage')->listAll($prefix); Chris@0: foreach ($role_names as $role_name) { Chris@0: $rid = substr($role_name, $cut); Chris@0: $config = \Drupal::config("user.role.$rid"); Chris@0: $permissions = $config->get('permissions') ?: []; Chris@0: foreach ($replace as $old_permission => $new_permissions) { Chris@0: if (($index = array_search($old_permission, $permissions)) !== FALSE) { Chris@0: unset($permissions[$index]); Chris@0: $permissions = array_unique(array_merge($permissions, $new_permissions)); Chris@0: } Chris@0: } Chris@0: $config Chris@0: ->set('permissions', $permissions) Chris@0: ->save(); Chris@0: } Chris@0: }