diff --git a/core/includes/update.inc b/core/includes/update.inc index 67af504..9c9a65c 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -215,19 +215,20 @@ function update_do_one($module, $number, $dependency_map, &$context) { drupal_set_installed_schema_version($module, $number); } - $context['message'] = 'Updating ' . Html::escape($module) . ' module'; + $context['message'] = t('Updating @module', ['@module' => $module]); } /** - * Executes a single hook_post_update_$name(). + * Executes a single hook_post_update_NAME(). * * @param string $function * The function name, that should be executed. * @param array $context * The batch context array. */ -function update_post_update_do_one($function, &$context) { +function update_invoke_post_update($function, &$context) { $ret = []; + list($module, ) = explode('_post_update_', $function); module_load_include('php', $module, $module . '.post_update'); if (function_exists($function)) { @@ -235,10 +236,10 @@ function update_post_update_do_one($function, &$context) { $ret['results']['query'] = $function($context['sandbox']); $ret['results']['success'] = TRUE; - $key_value_store = \Drupal::keyValue('update_test_post_update'); - $executed_updates = $key_value_store->get('core.update__post_update', []); + $key_value_store = \Drupal::keyValue('update__post_update'); + $executed_updates = $key_value_store->get('existing_updates', []); $executed_updates[] = $function; - $key_value_store->set('core.update__post_update', $executed_updates); + $key_value_store->set($module, $executed_updates); } // @TODO We may want to do different error handling for different // exception types, but for now we'll just log the exception and @@ -267,7 +268,7 @@ function update_post_update_do_one($function, &$context) { $context['results']['#abort'][] = $function; } - $context['message'] = 'Updating content after: ' . Html::escape($function); + $context['message'] = t('Post updating @module', ['@module' => $module]); } /** diff --git a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php index ab11469..c088eed 100644 --- a/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php +++ b/core/lib/Drupal/Core/Extension/ExtensionDiscovery.php @@ -91,6 +91,13 @@ class ExtensionDiscovery { protected $fileCache; /** + * The site path. + * + * @var string + */ + protected $sitePath; + + /** * Constructs a new ExtensionDiscovery object. * * @param string $root @@ -180,7 +187,7 @@ public function scan($type, $include_tests = NULL) { $searchdirs[static::ORIGIN_SITE] = \Drupal::service('site.path'); } else { - $searchdirs[static::ORIGIN_SITE] = isset($this->sitePath) ? $this->sitePath : DrupalKernel::findSitePath(Request::createFromGlobals()); + $searchdirs[static::ORIGIN_SITE] = $this->sitePath ?: DrupalKernel::findSitePath(Request::createFromGlobals()); } // Unless an explicit value has been passed, manually check whether we are diff --git a/core/lib/Drupal/Core/Extension/module.api.php b/core/lib/Drupal/Core/Extension/module.api.php index 264dc4c..b4b0cf8 100644 --- a/core/lib/Drupal/Core/Extension/module.api.php +++ b/core/lib/Drupal/Core/Extension/module.api.php @@ -666,7 +666,7 @@ function hook_update_N(&$sandbox) { * stage Drupal is already fully repaired so you can use any API as you wish. * * Note: In contrast to hook_update_N there is no enforced order, so you can - * also use machine names. + * also use machine names for the name of those updates. * * @param array $sandbox * Stores information for batch updates. See above for more information. @@ -675,7 +675,7 @@ function hook_update_N(&$sandbox) { * In case of error, update hooks should throw an instance of * Drupal\Core\Utility\UpdateException with a meaningful message for the user. * If a database query fails for whatever reason, it will throw a - * PDOException.ame for the name of those updates. + * PDOException. * * @return string|null * Optionally, update hooks may return a translated string that will be @@ -684,9 +684,9 @@ function hook_update_N(&$sandbox) { * * @ingroup update_api * - * @see hook_update_N + * @see hook_update_N() */ -function hook_post_update_X(&$sandbox) { +function hook_post_update_NAME(&$sandbox) { $node = \Drupal\node\Entity\Node::load(123); $node->setTitle('foo'); $node->save(); diff --git a/core/lib/Drupal/Core/Update/UpdateRegistry.php b/core/lib/Drupal/Core/Update/UpdateRegistry.php index b7d3b67..949d076 100644 --- a/core/lib/Drupal/Core/Update/UpdateRegistry.php +++ b/core/lib/Drupal/Core/Update/UpdateRegistry.php @@ -12,10 +12,10 @@ use Drupal\Core\KeyValueStore\KeyValueStoreInterface; /** - * Provides all and missing hook_$updateName_X implementations. + * Provides all and missing hook_$updateName_NAME implementations. * * It therefore scans for functions in $module.$updateName.inc named like - * $module_$updateName_X with X being a machine name. + * $module_$updateName_NAME with NAME being a machine name. */ class UpdateRegistry { @@ -68,7 +68,7 @@ public function __construct($root, $site_path, array $enabled_modules, KeyValueS * @return callable[] */ protected function getAvailableUpdateFunctions() { - $regexp = '/^(?.+)_' . $this->updateName . '_(?.+)$/'; + $regexp = '/^(?.+)_' . $this->updateName . '_(?.+)$/'; $functions = get_defined_functions(); $updates = []; @@ -76,7 +76,7 @@ protected function getAvailableUpdateFunctions() { // If this function is a module update function, add it to the list of // module updates. if (preg_match($regexp, $function, $matches)) { - $updates[] = $matches['module'] . '_' . $this->updateName . '_' . $matches['version']; + $updates[] = $matches['module'] . '_' . $this->updateName . '_' . $matches['name']; } } @@ -84,7 +84,7 @@ protected function getAvailableUpdateFunctions() { } /** - * Find all update functions that haven't been executed + * Find all update functions that haven't been executed. * * @return callable[] */ @@ -102,8 +102,8 @@ public function getMissingUpdateFunctions() { $this->loadUpdateFiles($extensions); - // First figure out which hook_update got executed already. - $existing_update_functions = $this->keyValue->get('core.update__' . $this->updateName, []); + // First figure out which hook_{$this->updateName}_NAME got executed already. + $existing_update_functions = $this->keyValue->get('existing_updates', []); $available_update_functions = $this->getAvailableUpdateFunctions(); $not_executed_update_functions = array_diff($available_update_functions, $existing_update_functions); @@ -111,8 +111,14 @@ public function getMissingUpdateFunctions() { return $not_executed_update_functions; } + /** + * Loads all update files for a given list of extension. + * + * @param \Drupal\Core\Extension\Extension[] $module_extensions + * The extensions used for loading. + */ protected function loadUpdateFiles(array $module_extensions) { - // Load all the {$this->updateName}.inc files. + // Load all the {$this->updateName}.php files. foreach ($this->enabledModules as $module) { if (isset($module_extensions[$module])) { $this->loadUpdateFile($module_extensions[$module]); @@ -121,9 +127,10 @@ protected function loadUpdateFiles(array $module_extensions) { } /** - * Loads the {$this->updateName}.inc file for a given extension. + * Loads the {$this->updateName}.php file for a given extension. * * @param \Drupal\Core\Extension\Extension $module + * The extension of the module to load its file. */ protected function loadUpdateFile(Extension $module) { $filename = $this->root . '/' . $module->getPath() . '/' . $module->getName() . ".{$this->updateName}.php"; @@ -133,21 +140,20 @@ protected function loadUpdateFile(Extension $module) { } /** - * Returns a list of all the pending database updates. + * Returns a list of all the pending updates. * * @return 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. + * going to proceed due to missing requirements. * * 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. * - 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. + * the description from source code comment for each update function. + * This array is keyed by the update name. */ public function getMissingUpdateList() { $functions = $this->getMissingUpdateFunctions(); diff --git a/core/modules/system/src/Controller/DbUpdateController.php b/core/modules/system/src/Controller/DbUpdateController.php index ab06793..043e61d 100644 --- a/core/modules/system/src/Controller/DbUpdateController.php +++ b/core/modules/system/src/Controller/DbUpdateController.php @@ -265,7 +265,7 @@ protected function selection(Request $request) { $updates = update_get_update_list(); break; case 'post_update': - $after_update_registry = $this->getUpdateAfterRegistry(); + $after_update_registry = $this->getPostUpdateRegistry(); $updates = $after_update_registry->getMissingUpdateList(); break; } @@ -589,7 +589,7 @@ protected function triggerBatch(Request $request) { } } - $after_update_registry = $this->getUpdateAfterRegistry(); + $after_update_registry = $this->getPostUpdateRegistry(); $post_updates = $after_update_registry->getMissingUpdateFunctions(); if ($post_updates) { @@ -597,7 +597,7 @@ protected function triggerBatch(Request $request) { // functions. $operations[] = ['drupal_flush_all_caches', []]; foreach ($post_updates as $function) { - $operations[] = ['update_post_update_do_one', [$function]]; + $operations[] = ['update_invoke_post_update', [$function]]; } } diff --git a/core/modules/system/src/Tests/Update/UpdatePostUpdateTest.php b/core/modules/system/src/Tests/Update/UpdatePostUpdateTest.php index e82b0c5..8be271f 100644 --- a/core/modules/system/src/Tests/Update/UpdatePostUpdateTest.php +++ b/core/modules/system/src/Tests/Update/UpdatePostUpdateTest.php @@ -20,19 +20,23 @@ class UpdatePostUpdateTest extends UpdatePathTestBase { protected function setDatabaseDumpFiles() { $this->databaseDumpFiles = [ __DIR__ . '/../../../tests/fixtures/update/drupal-8.bare.standard.php.gz', - __DIR__ . '/../../../tests/fixtures/update/drupal-8.update-post-update-after-enabled.php', + __DIR__ . '/../../../tests/fixtures/update/drupal-8.update-test-post-update-enabled.php', ]; } public function testUpdateAfter() { $this->runUpdates(); - $this->assertIdentical([ + $updates = [ 'update_test_post_update_post_update_first', 'update_test_post_update_post_update_second', 'update_test_post_update_post_update_test1', 'update_test_post_update_post_update_test0', - ], \Drupal::state()->get('post_update_test_execution', [])); + ]; + $this->assertIdentical($updates, \Drupal::state()->get('post_update_test_execution', [])); + + $key_value = \Drupal::keyValue('update__post_update'); + $this->assertEqual($updates, $key_value->get('existing_updates')); } } diff --git a/core/modules/system/tests/modules/update_test_post_update/update_test_post_update.yml b/core/modules/system/tests/modules/update_test_post_update/update_test_post_update.info.yml similarity index 100% rename from core/modules/system/tests/modules/update_test_post_update/update_test_post_update.yml rename to core/modules/system/tests/modules/update_test_post_update/update_test_post_update.info.yml diff --git a/core/tests/Drupal/Tests/Core/Update/UpdateRegistryTest.php b/core/tests/Drupal/Tests/Core/Update/UpdateRegistryTest.php index fe79330..e90acaa 100644 --- a/core/tests/Drupal/Tests/Core/Update/UpdateRegistryTest.php +++ b/core/tests/Drupal/Tests/Core/Update/UpdateRegistryTest.php @@ -26,7 +26,7 @@ class UpdateRegistryTest extends UnitTestCase { * {@inheritdoc} */ protected function setUp() { - parent::setUp(); // TODO: Change the autogenerated stub + parent::setUp(); $settings = []; $settings['extension_discovery_scan_tests'] = TRUE; @@ -101,7 +101,7 @@ public function testGetMissingUpdateFunctionsWithNoExistingUpdates() { $this->setupBasicModules(); $key_value = $this->prophesize(KeyValueStoreInterface::class); - $key_value->get('core.update__post_update', [])->wilLReturn([]); + $key_value->get('core.update__post_update', [])->willReturn([]); $key_value = $key_value->reveal(); $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [ @@ -123,7 +123,7 @@ public function testGetMissingUpdateFunctionsWithExistingUpdates() { $this->setupBasicModules(); $key_value = $this->prophesize(KeyValueStoreInterface::class); - $key_value->get('core.update__post_update', [])->wilLReturn(['module_a_post_update_a']); + $key_value->get('core.update__post_update', [])->willReturn(['module_a_post_update_a']); $key_value = $key_value->reveal(); $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [ @@ -145,7 +145,7 @@ public function testGetMissingUpdateList() { $this->setupBasicModules(); $key_value = $this->prophesize(KeyValueStoreInterface::class); - $key_value->get('core.update__post_update', [])->wilLReturn([]); + $key_value->get('core.update__post_update', [])->willReturn([]); $key_value = $key_value->reveal(); $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [ @@ -170,7 +170,7 @@ public function testGetMissingUpdateListWithExistingUpdates() { $this->setupBasicModules(); $key_value = $this->prophesize(KeyValueStoreInterface::class); - $key_value->get('core.update__post_update', [])->wilLReturn(['module_a_post_update_a']); + $key_value->get('core.update__post_update', [])->willReturn(['module_a_post_update_a']); $key_value = $key_value->reveal(); $update_registry = new UpdateRegistry('vfs://drupal', 'sites/default', [