Same name and namespace in other branches
  1. 7.x includes/update.inc \update_get_update_list()
  2. 8.9.x core/includes/update.inc \update_get_update_list()
  3. 9 core/includes/update.inc \update_get_update_list()

Returns a list of all the pending database updates.

Return value

array An associative array keyed by module name which contains all information about database updates that need to be run, and any updates that are not going to proceed due to missing requirements. The system module will always be listed first.

The subarray for each module can contain the following keys:

  • start: The starting update that is to be processed. If this does not exist then do not process any updates for this module as there are other requirements that need to be resolved.
  • warning: Any warnings about why this module can not be updated.
  • pending: An array of all the pending updates for the module including the update number and the description from source code comment for each update function. This array is keyed by the update number.
3 calls to update_get_update_list()
Module::getSchemaUpdates in core/lib/Drupal/Core/Updater/Module.php
Returns available database schema updates once a new version is installed.
UpdateDescriptionTest::testUpdateGetUpdateList in core/tests/Drupal/KernelTests/Core/Extension/UpdateDescriptionTest.php
Tests the list of pending database updates.
UpdatePathTestTrait::runUpdates in core/tests/Drupal/Tests/UpdatePathTestTrait.php
Helper function to run pending database updates.

File

core/includes/update.inc, line 293
Drupal database update API.

Code

function update_get_update_list() {

  // Make sure that the system module is first in the list of updates.
  $ret = [
    'system' => [],
  ];

  /** @var \Drupal\Core\Update\UpdateHookRegistry $update_registry */
  $update_registry = \Drupal::service('update.update_hook_registry');
  $modules = $update_registry
    ->getAllInstalledVersions();

  /** @var \Drupal\Core\Extension\ExtensionList $extension_list */
  $extension_list = \Drupal::service('extension.list.module');

  /** @var array $installed_module_info */
  $installed_module_info = $extension_list
    ->getAllInstalledInfo();
  foreach ($modules as $module => $schema_version) {

    // Skip uninstalled and incompatible modules.
    try {
      if ($schema_version == $update_registry::SCHEMA_UNINSTALLED || $extension_list
        ->checkIncompatibility($module)) {
        continue;
      }
    } catch (UnknownExtensionException $e) {
      $args = [
        '%name' => $module,
        ':url' => 'https://www.drupal.org/node/3137656',
      ];
      \Drupal::messenger()
        ->addWarning(t('Module %name has an entry in the system.schema key/value storage, but is missing from your site. <a href=":url">More information about this error</a>.', $args));
      \Drupal::logger('system')
        ->notice('Module %name has an entry in the system.schema key/value storage, but is missing from your site. <a href=":url">More information about this error</a>.', $args);
      continue;
    }

    // There might be orphaned entries for modules that are in the filesystem
    // but not installed. Also skip those, but warn site admins about it.
    if (empty($installed_module_info[$module])) {
      $args = [
        '%name' => $module,
        ':url' => 'https://www.drupal.org/node/3137656',
      ];
      \Drupal::messenger()
        ->addWarning(t('Module %name has an entry in the system.schema key/value storage, but is not installed. <a href=":url">More information about this error</a>.', $args));
      \Drupal::logger('system')
        ->notice('Module %name has an entry in the system.schema key/value storage, but is not installed. <a href=":url">More information about this error</a>.', $args);
      continue;
    }

    // Display a requirements error if the user somehow has a schema version
    // from the previous Drupal major version.
    if ($schema_version < \Drupal::CORE_MINIMUM_SCHEMA_VERSION) {
      $ret[$module]['warning'] = '<em>' . $module . '</em> module cannot be updated. Its schema version is ' . $schema_version . ', which is from an earlier major release of Drupal. See the <a href="https://www.drupal.org/docs/upgrading-drupal">Upgrading Drupal guide</a>.';
      continue;
    }

    // Otherwise, get the list of updates defined by this module.
    $updates = $update_registry
      ->getAvailableUpdates($module);
    if ($updates) {
      foreach ($updates as $update) {
        if ($update == \Drupal::CORE_MINIMUM_SCHEMA_VERSION) {
          $ret[$module]['warning'] = '<em>' . $module . '</em> 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 <em>' . $module . '</em> module, you will need to download a version of the module with valid updates.';
          continue 2;
        }
        if ($update > $schema_version) {

          // The description for an update comes from its Doxygen.
          $func = new ReflectionFunction($module . '_update_' . $update);
          $patterns = [
            '/^\\s*[\\/*]*/',
            '/(\\n\\s*\\**)(.*)/',
            '/\\/$/',
            '/^\\s*/',
          ];
          $replacements = [
            '',
            '$2',
            '',
            '',
          ];
          $description = preg_replace($patterns, $replacements, $func
            ->getDocComment());
          $ret[$module]['pending'][$update] = "{$update} - {$description}";
          if (!isset($ret[$module]['start'])) {
            $ret[$module]['start'] = $update;
          }
        }
      }
      if (!isset($ret[$module]['start']) && isset($ret[$module]['pending'])) {
        $ret[$module]['start'] = $schema_version;
      }
    }
  }
  if (empty($ret['system'])) {
    unset($ret['system']);
  }
  return $ret;
}