Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Drupal database update API.
|
Chris@0
|
6 *
|
Chris@0
|
7 * This file contains functions to perform database updates for a Drupal
|
Chris@0
|
8 * installation. It is included and used extensively by update.php.
|
Chris@0
|
9 */
|
Chris@0
|
10
|
Chris@0
|
11 use Drupal\Component\Graph\Graph;
|
Chris@17
|
12 use Drupal\Core\Update\UpdateKernel;
|
Chris@0
|
13 use Drupal\Core\Utility\Error;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Disables any extensions that are incompatible with the current core version.
|
Chris@0
|
17 */
|
Chris@0
|
18 function update_fix_compatibility() {
|
Chris@17
|
19 // Fix extension objects if the update is being done via Drush 8. In non-Drush
|
Chris@17
|
20 // environments this will already be fixed by the UpdateKernel this point.
|
Chris@17
|
21 UpdateKernel::fixSerializedExtensionObjects(\Drupal::getContainer());
|
Chris@17
|
22
|
Chris@0
|
23 $extension_config = \Drupal::configFactory()->getEditable('core.extension');
|
Chris@0
|
24 $save = FALSE;
|
Chris@0
|
25 foreach (['module', 'theme'] as $type) {
|
Chris@0
|
26 foreach ($extension_config->get($type) as $name => $weight) {
|
Chris@0
|
27 if (update_check_incompatibility($name, $type)) {
|
Chris@0
|
28 $extension_config->clear("$type.$name");
|
Chris@0
|
29 $save = TRUE;
|
Chris@0
|
30 }
|
Chris@0
|
31 }
|
Chris@0
|
32 }
|
Chris@0
|
33 if ($save) {
|
Chris@0
|
34 $extension_config->set('module', module_config_sort($extension_config->get('module')));
|
Chris@0
|
35 $extension_config->save();
|
Chris@0
|
36 }
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Tests the compatibility of a module or theme.
|
Chris@0
|
41 */
|
Chris@0
|
42 function update_check_incompatibility($name, $type = 'module') {
|
Chris@0
|
43 static $themes, $modules;
|
Chris@0
|
44
|
Chris@0
|
45 // Store values of expensive functions for future use.
|
Chris@0
|
46 if (empty($themes) || empty($modules)) {
|
Chris@0
|
47 // We need to do a full rebuild here to make sure the database reflects any
|
Chris@0
|
48 // code changes that were made in the filesystem before the update script
|
Chris@0
|
49 // was initiated.
|
Chris@0
|
50 $themes = \Drupal::service('theme_handler')->rebuildThemeData();
|
Chris@0
|
51 $modules = system_rebuild_module_data();
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 if ($type == 'module' && isset($modules[$name])) {
|
Chris@0
|
55 $file = $modules[$name];
|
Chris@0
|
56 }
|
Chris@0
|
57 elseif ($type == 'theme' && isset($themes[$name])) {
|
Chris@0
|
58 $file = $themes[$name];
|
Chris@0
|
59 }
|
Chris@0
|
60 if (!isset($file)
|
Chris@0
|
61 || !isset($file->info['core'])
|
Chris@0
|
62 || $file->info['core'] != \Drupal::CORE_COMPATIBILITY
|
Chris@0
|
63 || version_compare(phpversion(), $file->info['php']) < 0) {
|
Chris@0
|
64 return TRUE;
|
Chris@0
|
65 }
|
Chris@0
|
66 return FALSE;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /**
|
Chris@0
|
70 * Returns whether the minimum schema requirement has been satisfied.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return array
|
Chris@0
|
73 * A requirements info array.
|
Chris@0
|
74 */
|
Chris@0
|
75 function update_system_schema_requirements() {
|
Chris@0
|
76 $requirements = [];
|
Chris@0
|
77
|
Chris@0
|
78 $system_schema = drupal_get_installed_schema_version('system');
|
Chris@0
|
79
|
Chris@0
|
80 $requirements['minimum schema']['title'] = 'Minimum schema version';
|
Chris@0
|
81 if ($system_schema >= \Drupal::CORE_MINIMUM_SCHEMA_VERSION) {
|
Chris@0
|
82 $requirements['minimum schema'] += [
|
Chris@0
|
83 'value' => 'The installed schema version meets the minimum.',
|
Chris@0
|
84 'description' => 'Schema version: ' . $system_schema,
|
Chris@0
|
85 ];
|
Chris@0
|
86 }
|
Chris@0
|
87 else {
|
Chris@0
|
88 $requirements['minimum schema'] += [
|
Chris@0
|
89 'value' => 'The installed schema version does not meet the minimum.',
|
Chris@0
|
90 'severity' => REQUIREMENT_ERROR,
|
Chris@12
|
91 '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
|
92 ];
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 return $requirements;
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * Checks update requirements and reports errors and (optionally) warnings.
|
Chris@0
|
100 */
|
Chris@0
|
101 function update_check_requirements() {
|
Chris@0
|
102 // Check requirements of all loaded modules.
|
Chris@0
|
103 $requirements = \Drupal::moduleHandler()->invokeAll('requirements', ['update']);
|
Chris@0
|
104 $requirements += update_system_schema_requirements();
|
Chris@0
|
105 return $requirements;
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * Forces a module to a given schema version.
|
Chris@0
|
110 *
|
Chris@0
|
111 * This function is rarely necessary.
|
Chris@0
|
112 *
|
Chris@0
|
113 * @param string $module
|
Chris@0
|
114 * Name of the module.
|
Chris@0
|
115 * @param string $schema_version
|
Chris@0
|
116 * The schema version the module should be set to.
|
Chris@0
|
117 */
|
Chris@0
|
118 function update_set_schema($module, $schema_version) {
|
Chris@0
|
119 \Drupal::keyValue('system.schema')->set($module, $schema_version);
|
Chris@18
|
120 \Drupal::service('extension.list.profile')->reset();
|
Chris@18
|
121 \Drupal::service('extension.list.module')->reset();
|
Chris@18
|
122 \Drupal::service('extension.list.theme_engine')->reset();
|
Chris@18
|
123 \Drupal::service('extension.list.theme')->reset();
|
Chris@18
|
124 drupal_static_reset('drupal_get_installed_schema_version');
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * Implements callback_batch_operation().
|
Chris@0
|
129 *
|
Chris@0
|
130 * Performs one update and stores the results for display on the results page.
|
Chris@0
|
131 *
|
Chris@0
|
132 * If an update function completes successfully, it should return a message
|
Chris@0
|
133 * as a string indicating success, for example:
|
Chris@0
|
134 * @code
|
Chris@0
|
135 * return t('New index added successfully.');
|
Chris@0
|
136 * @endcode
|
Chris@0
|
137 *
|
Chris@0
|
138 * Alternatively, it may return nothing. In that case, no message
|
Chris@0
|
139 * will be displayed at all.
|
Chris@0
|
140 *
|
Chris@0
|
141 * If it fails for whatever reason, it should throw an instance of
|
Chris@0
|
142 * Drupal\Core\Utility\UpdateException with an appropriate error message, for
|
Chris@0
|
143 * example:
|
Chris@0
|
144 * @code
|
Chris@0
|
145 * use Drupal\Core\Utility\UpdateException;
|
Chris@0
|
146 * throw new UpdateException(t('Description of what went wrong'));
|
Chris@0
|
147 * @endcode
|
Chris@0
|
148 *
|
Chris@0
|
149 * If an exception is thrown, the current update and all updates that depend on
|
Chris@0
|
150 * it will be aborted. The schema version will not be updated in this case, and
|
Chris@0
|
151 * all the aborted updates will continue to appear on update.php as updates
|
Chris@0
|
152 * that have not yet been run.
|
Chris@0
|
153 *
|
Chris@0
|
154 * If an update function needs to be re-run as part of a batch process, it
|
Chris@0
|
155 * should accept the $sandbox array by reference as its first parameter
|
Chris@0
|
156 * and set the #finished property to the percentage completed that it is, as a
|
Chris@0
|
157 * fraction of 1.
|
Chris@0
|
158 *
|
Chris@0
|
159 * @param $module
|
Chris@0
|
160 * The module whose update will be run.
|
Chris@0
|
161 * @param $number
|
Chris@0
|
162 * The update number to run.
|
Chris@0
|
163 * @param $dependency_map
|
Chris@0
|
164 * An array whose keys are the names of all update functions that will be
|
Chris@0
|
165 * performed during this batch process, and whose values are arrays of other
|
Chris@0
|
166 * update functions that each one depends on.
|
Chris@0
|
167 * @param $context
|
Chris@0
|
168 * The batch context array.
|
Chris@0
|
169 *
|
Chris@0
|
170 * @see update_resolve_dependencies()
|
Chris@0
|
171 */
|
Chris@0
|
172 function update_do_one($module, $number, $dependency_map, &$context) {
|
Chris@0
|
173 $function = $module . '_update_' . $number;
|
Chris@0
|
174
|
Chris@0
|
175 // If this update was aborted in a previous step, or has a dependency that
|
Chris@0
|
176 // was aborted in a previous step, go no further.
|
Chris@0
|
177 if (!empty($context['results']['#abort']) && array_intersect($context['results']['#abort'], array_merge($dependency_map, [$function]))) {
|
Chris@0
|
178 return;
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 $ret = [];
|
Chris@0
|
182 if (function_exists($function)) {
|
Chris@0
|
183 try {
|
Chris@0
|
184 $ret['results']['query'] = $function($context['sandbox']);
|
Chris@0
|
185 $ret['results']['success'] = TRUE;
|
Chris@0
|
186 }
|
Chris@0
|
187 // @TODO We may want to do different error handling for different
|
Chris@0
|
188 // exception types, but for now we'll just log the exception and
|
Chris@0
|
189 // return the message for printing.
|
Chris@0
|
190 // @see https://www.drupal.org/node/2564311
|
Chris@0
|
191 catch (Exception $e) {
|
Chris@0
|
192 watchdog_exception('update', $e);
|
Chris@0
|
193
|
Chris@0
|
194 $variables = Error::decodeException($e);
|
Chris@0
|
195 unset($variables['backtrace']);
|
Chris@0
|
196 $ret['#abort'] = ['success' => FALSE, 'query' => t('%type: @message in %function (line %line of %file).', $variables)];
|
Chris@0
|
197 }
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 if (isset($context['sandbox']['#finished'])) {
|
Chris@0
|
201 $context['finished'] = $context['sandbox']['#finished'];
|
Chris@0
|
202 unset($context['sandbox']['#finished']);
|
Chris@0
|
203 }
|
Chris@0
|
204
|
Chris@0
|
205 if (!isset($context['results'][$module])) {
|
Chris@0
|
206 $context['results'][$module] = [];
|
Chris@0
|
207 }
|
Chris@0
|
208 if (!isset($context['results'][$module][$number])) {
|
Chris@0
|
209 $context['results'][$module][$number] = [];
|
Chris@0
|
210 }
|
Chris@0
|
211 $context['results'][$module][$number] = array_merge($context['results'][$module][$number], $ret);
|
Chris@0
|
212
|
Chris@0
|
213 if (!empty($ret['#abort'])) {
|
Chris@0
|
214 // Record this function in the list of updates that were aborted.
|
Chris@0
|
215 $context['results']['#abort'][] = $function;
|
Chris@0
|
216 }
|
Chris@0
|
217
|
Chris@0
|
218 // Record the schema update if it was completed successfully.
|
Chris@0
|
219 if ($context['finished'] == 1 && empty($ret['#abort'])) {
|
Chris@0
|
220 drupal_set_installed_schema_version($module, $number);
|
Chris@0
|
221 }
|
Chris@0
|
222
|
Chris@0
|
223 $context['message'] = t('Updating @module', ['@module' => $module]);
|
Chris@0
|
224 }
|
Chris@0
|
225
|
Chris@0
|
226 /**
|
Chris@0
|
227 * Executes a single hook_post_update_NAME().
|
Chris@0
|
228 *
|
Chris@0
|
229 * @param string $function
|
Chris@0
|
230 * The function name, that should be executed.
|
Chris@0
|
231 * @param array $context
|
Chris@0
|
232 * The batch context array.
|
Chris@0
|
233 */
|
Chris@0
|
234 function update_invoke_post_update($function, &$context) {
|
Chris@0
|
235 $ret = [];
|
Chris@0
|
236
|
Chris@0
|
237 // If this update was aborted in a previous step, or has a dependency that was
|
Chris@0
|
238 // aborted in a previous step, go no further.
|
Chris@0
|
239 if (!empty($context['results']['#abort'])) {
|
Chris@0
|
240 return;
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 list($module, $name) = explode('_post_update_', $function, 2);
|
Chris@0
|
244 module_load_include('php', $module, $module . '.post_update');
|
Chris@0
|
245 if (function_exists($function)) {
|
Chris@0
|
246 try {
|
Chris@0
|
247 $ret['results']['query'] = $function($context['sandbox']);
|
Chris@0
|
248 $ret['results']['success'] = TRUE;
|
Chris@0
|
249
|
Chris@0
|
250 if (!isset($context['sandbox']['#finished']) || (isset($context['sandbox']['#finished']) && $context['sandbox']['#finished'] >= 1)) {
|
Chris@0
|
251 \Drupal::service('update.post_update_registry')->registerInvokedUpdates([$function]);
|
Chris@0
|
252 }
|
Chris@0
|
253 }
|
Chris@0
|
254 // @TODO We may want to do different error handling for different exception
|
Chris@0
|
255 // types, but for now we'll just log the exception and return the message
|
Chris@0
|
256 // for printing.
|
Chris@0
|
257 // @see https://www.drupal.org/node/2564311
|
Chris@0
|
258 catch (Exception $e) {
|
Chris@0
|
259 watchdog_exception('update', $e);
|
Chris@0
|
260
|
Chris@0
|
261 $variables = Error::decodeException($e);
|
Chris@0
|
262 unset($variables['backtrace']);
|
Chris@0
|
263 $ret['#abort'] = [
|
Chris@0
|
264 'success' => FALSE,
|
Chris@0
|
265 'query' => t('%type: @message in %function (line %line of %file).', $variables),
|
Chris@0
|
266 ];
|
Chris@0
|
267 }
|
Chris@0
|
268 }
|
Chris@0
|
269
|
Chris@0
|
270 if (isset($context['sandbox']['#finished'])) {
|
Chris@0
|
271 $context['finished'] = $context['sandbox']['#finished'];
|
Chris@0
|
272 unset($context['sandbox']['#finished']);
|
Chris@0
|
273 }
|
Chris@0
|
274 if (!isset($context['results'][$module][$name])) {
|
Chris@0
|
275 $context['results'][$module][$name] = [];
|
Chris@0
|
276 }
|
Chris@0
|
277 $context['results'][$module][$name] = array_merge($context['results'][$module][$name], $ret);
|
Chris@0
|
278
|
Chris@0
|
279 if (!empty($ret['#abort'])) {
|
Chris@0
|
280 // Record this function in the list of updates that were aborted.
|
Chris@0
|
281 $context['results']['#abort'][] = $function;
|
Chris@0
|
282 }
|
Chris@0
|
283
|
Chris@0
|
284 $context['message'] = t('Post updating @module', ['@module' => $module]);
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * Returns a list of all the pending database updates.
|
Chris@0
|
289 *
|
Chris@0
|
290 * @return
|
Chris@0
|
291 * An associative array keyed by module name which contains all information
|
Chris@0
|
292 * about database updates that need to be run, and any updates that are not
|
Chris@0
|
293 * going to proceed due to missing requirements. The system module will
|
Chris@0
|
294 * always be listed first.
|
Chris@0
|
295 *
|
Chris@0
|
296 * The subarray for each module can contain the following keys:
|
Chris@0
|
297 * - start: The starting update that is to be processed. If this does not
|
Chris@0
|
298 * exist then do not process any updates for this module as there are
|
Chris@0
|
299 * other requirements that need to be resolved.
|
Chris@0
|
300 * - warning: Any warnings about why this module can not be updated.
|
Chris@0
|
301 * - pending: An array of all the pending updates for the module including
|
Chris@0
|
302 * the update number and the description from source code comment for
|
Chris@0
|
303 * each update function. This array is keyed by the update number.
|
Chris@0
|
304 */
|
Chris@0
|
305 function update_get_update_list() {
|
Chris@0
|
306 // Make sure that the system module is first in the list of updates.
|
Chris@0
|
307 $ret = ['system' => []];
|
Chris@0
|
308
|
Chris@0
|
309 $modules = drupal_get_installed_schema_version(NULL, FALSE, TRUE);
|
Chris@0
|
310 foreach ($modules as $module => $schema_version) {
|
Chris@0
|
311 // Skip uninstalled and incompatible modules.
|
Chris@0
|
312 if ($schema_version == SCHEMA_UNINSTALLED || update_check_incompatibility($module)) {
|
Chris@0
|
313 continue;
|
Chris@0
|
314 }
|
Chris@0
|
315 // Display a requirements error if the user somehow has a schema version
|
Chris@0
|
316 // from the previous Drupal major version.
|
Chris@0
|
317 if ($schema_version < \Drupal::CORE_MINIMUM_SCHEMA_VERSION) {
|
Chris@0
|
318 $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. You will need to <a href="https://www.drupal.org/node/2127611">migrate the data for this module</a> instead.';
|
Chris@0
|
319 continue;
|
Chris@0
|
320 }
|
Chris@0
|
321 // Otherwise, get the list of updates defined by this module.
|
Chris@0
|
322 $updates = drupal_get_schema_versions($module);
|
Chris@0
|
323 if ($updates !== FALSE) {
|
Chris@0
|
324 // \Drupal::moduleHandler()->invoke() returns NULL for non-existing hooks,
|
Chris@0
|
325 // so if no updates are removed, it will == 0.
|
Chris@0
|
326 $last_removed = \Drupal::moduleHandler()->invoke($module, 'update_last_removed');
|
Chris@0
|
327 if ($schema_version < $last_removed) {
|
Chris@0
|
328 $ret[$module]['warning'] = '<em>' . $module . '</em> 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 <em>' . $module . '</em> module, you will first <a href="https://www.drupal.org/upgrade">need to upgrade</a> to the last version in which these updates were available.';
|
Chris@0
|
329 continue;
|
Chris@0
|
330 }
|
Chris@0
|
331
|
Chris@14
|
332 foreach ($updates as $update) {
|
Chris@0
|
333 if ($update == \Drupal::CORE_MINIMUM_SCHEMA_VERSION) {
|
Chris@0
|
334 $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 install a version of the module with valid updates.';
|
Chris@0
|
335 continue 2;
|
Chris@0
|
336 }
|
Chris@0
|
337 if ($update > $schema_version) {
|
Chris@0
|
338 // The description for an update comes from its Doxygen.
|
Chris@0
|
339 $func = new ReflectionFunction($module . '_update_' . $update);
|
Chris@0
|
340 $description = str_replace(["\n", '*', '/'], '', $func->getDocComment());
|
Chris@0
|
341 $ret[$module]['pending'][$update] = "$update - $description";
|
Chris@0
|
342 if (!isset($ret[$module]['start'])) {
|
Chris@0
|
343 $ret[$module]['start'] = $update;
|
Chris@0
|
344 }
|
Chris@0
|
345 }
|
Chris@0
|
346 }
|
Chris@0
|
347 if (!isset($ret[$module]['start']) && isset($ret[$module]['pending'])) {
|
Chris@0
|
348 $ret[$module]['start'] = $schema_version;
|
Chris@0
|
349 }
|
Chris@0
|
350 }
|
Chris@0
|
351 }
|
Chris@0
|
352
|
Chris@0
|
353 if (empty($ret['system'])) {
|
Chris@0
|
354 unset($ret['system']);
|
Chris@0
|
355 }
|
Chris@0
|
356 return $ret;
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@0
|
359 /**
|
Chris@0
|
360 * Resolves dependencies in a set of module updates, and orders them correctly.
|
Chris@0
|
361 *
|
Chris@0
|
362 * This function receives a list of requested module updates and determines an
|
Chris@0
|
363 * appropriate order to run them in such that all update dependencies are met.
|
Chris@0
|
364 * Any updates whose dependencies cannot be met are included in the returned
|
Chris@0
|
365 * array but have the key 'allowed' set to FALSE; the calling function should
|
Chris@0
|
366 * take responsibility for ensuring that these updates are ultimately not
|
Chris@0
|
367 * performed.
|
Chris@0
|
368 *
|
Chris@0
|
369 * In addition, the returned array also includes detailed information about the
|
Chris@0
|
370 * dependency chain for each update, as provided by the depth-first search
|
Chris@0
|
371 * algorithm in Drupal\Component\Graph\Graph::searchAndSort().
|
Chris@0
|
372 *
|
Chris@0
|
373 * @param $starting_updates
|
Chris@0
|
374 * An array whose keys contain the names of modules with updates to be run
|
Chris@0
|
375 * and whose values contain the number of the first requested update for that
|
Chris@0
|
376 * module.
|
Chris@0
|
377 *
|
Chris@0
|
378 * @return
|
Chris@0
|
379 * An array whose keys are the names of all update functions within the
|
Chris@0
|
380 * provided modules that would need to be run in order to fulfill the
|
Chris@0
|
381 * request, arranged in the order in which the update functions should be
|
Chris@0
|
382 * run. (This includes the provided starting update for each module and all
|
Chris@0
|
383 * subsequent updates that are available.) The values are themselves arrays
|
Chris@0
|
384 * containing all the keys provided by the
|
Chris@0
|
385 * Drupal\Component\Graph\Graph::searchAndSort() algorithm, which encode
|
Chris@0
|
386 * detailed information about the dependency chain for this update function
|
Chris@0
|
387 * (for example: 'paths', 'reverse_paths', 'weight', and 'component'), as
|
Chris@0
|
388 * well as the following additional keys:
|
Chris@0
|
389 * - 'allowed': A boolean which is TRUE when the update function's
|
Chris@0
|
390 * dependencies are met, and FALSE otherwise. Calling functions should
|
Chris@0
|
391 * inspect this value before running the update.
|
Chris@0
|
392 * - 'missing_dependencies': An array containing the names of any other
|
Chris@0
|
393 * update functions that are required by this one but that are unavailable
|
Chris@0
|
394 * to be run. This array will be empty when 'allowed' is TRUE.
|
Chris@0
|
395 * - 'module': The name of the module that this update function belongs to.
|
Chris@0
|
396 * - 'number': The number of this update function within that module.
|
Chris@0
|
397 *
|
Chris@0
|
398 * @see \Drupal\Component\Graph\Graph::searchAndSort()
|
Chris@0
|
399 */
|
Chris@0
|
400 function update_resolve_dependencies($starting_updates) {
|
Chris@0
|
401 // Obtain a dependency graph for the requested update functions.
|
Chris@0
|
402 $update_functions = update_get_update_function_list($starting_updates);
|
Chris@0
|
403 $graph = update_build_dependency_graph($update_functions);
|
Chris@0
|
404
|
Chris@0
|
405 // Perform the depth-first search and sort on the results.
|
Chris@0
|
406 $graph_object = new Graph($graph);
|
Chris@0
|
407 $graph = $graph_object->searchAndSort();
|
Chris@0
|
408 uasort($graph, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
|
Chris@0
|
409
|
Chris@0
|
410 foreach ($graph as $function => &$data) {
|
Chris@0
|
411 $module = $data['module'];
|
Chris@0
|
412 $number = $data['number'];
|
Chris@0
|
413 // If the update function is missing and has not yet been performed, mark
|
Chris@0
|
414 // it and everything that ultimately depends on it as disallowed.
|
Chris@0
|
415 if (update_is_missing($module, $number, $update_functions) && !update_already_performed($module, $number)) {
|
Chris@0
|
416 $data['allowed'] = FALSE;
|
Chris@0
|
417 foreach (array_keys($data['paths']) as $dependent) {
|
Chris@0
|
418 $graph[$dependent]['allowed'] = FALSE;
|
Chris@0
|
419 $graph[$dependent]['missing_dependencies'][] = $function;
|
Chris@0
|
420 }
|
Chris@0
|
421 }
|
Chris@0
|
422 elseif (!isset($data['allowed'])) {
|
Chris@0
|
423 $data['allowed'] = TRUE;
|
Chris@0
|
424 $data['missing_dependencies'] = [];
|
Chris@0
|
425 }
|
Chris@0
|
426 // Now that we have finished processing this function, remove it from the
|
Chris@0
|
427 // graph if it was not part of the original list. This ensures that we
|
Chris@0
|
428 // never try to run any updates that were not specifically requested.
|
Chris@0
|
429 if (!isset($update_functions[$module][$number])) {
|
Chris@0
|
430 unset($graph[$function]);
|
Chris@0
|
431 }
|
Chris@0
|
432 }
|
Chris@0
|
433
|
Chris@0
|
434 return $graph;
|
Chris@0
|
435 }
|
Chris@0
|
436
|
Chris@0
|
437 /**
|
Chris@0
|
438 * Returns an organized list of update functions for a set of modules.
|
Chris@0
|
439 *
|
Chris@0
|
440 * @param $starting_updates
|
Chris@0
|
441 * An array whose keys contain the names of modules and whose values contain
|
Chris@0
|
442 * the number of the first requested update for that module.
|
Chris@0
|
443 *
|
Chris@0
|
444 * @return
|
Chris@0
|
445 * An array containing all the update functions that should be run for each
|
Chris@0
|
446 * module, including the provided starting update and all subsequent updates
|
Chris@0
|
447 * that are available. The keys of the array contain the module names, and
|
Chris@0
|
448 * each value is an ordered array of update functions, keyed by the update
|
Chris@0
|
449 * number.
|
Chris@0
|
450 *
|
Chris@0
|
451 * @see update_resolve_dependencies()
|
Chris@0
|
452 */
|
Chris@0
|
453 function update_get_update_function_list($starting_updates) {
|
Chris@0
|
454 // Go through each module and find all updates that we need (including the
|
Chris@0
|
455 // first update that was requested and any updates that run after it).
|
Chris@0
|
456 $update_functions = [];
|
Chris@0
|
457 foreach ($starting_updates as $module => $version) {
|
Chris@0
|
458 $update_functions[$module] = [];
|
Chris@0
|
459 $updates = drupal_get_schema_versions($module);
|
Chris@0
|
460 if ($updates !== FALSE) {
|
Chris@0
|
461 $max_version = max($updates);
|
Chris@0
|
462 if ($version <= $max_version) {
|
Chris@0
|
463 foreach ($updates as $update) {
|
Chris@0
|
464 if ($update >= $version) {
|
Chris@0
|
465 $update_functions[$module][$update] = $module . '_update_' . $update;
|
Chris@0
|
466 }
|
Chris@0
|
467 }
|
Chris@0
|
468 }
|
Chris@0
|
469 }
|
Chris@0
|
470 }
|
Chris@0
|
471 return $update_functions;
|
Chris@0
|
472 }
|
Chris@0
|
473
|
Chris@0
|
474 /**
|
Chris@0
|
475 * Constructs a graph which encodes the dependencies between module updates.
|
Chris@0
|
476 *
|
Chris@0
|
477 * This function returns an associative array which contains a "directed graph"
|
Chris@0
|
478 * representation of the dependencies between a provided list of update
|
Chris@0
|
479 * functions, as well as any outside update functions that they directly depend
|
Chris@0
|
480 * on but that were not in the provided list. The vertices of the graph
|
Chris@0
|
481 * represent the update functions themselves, and each edge represents a
|
Chris@0
|
482 * requirement that the first update function needs to run before the second.
|
Chris@0
|
483 * For example, consider this graph:
|
Chris@0
|
484 *
|
Chris@0
|
485 * system_update_8001 ---> system_update_8002 ---> system_update_8003
|
Chris@0
|
486 *
|
Chris@0
|
487 * Visually, this indicates that system_update_8001() must run before
|
Chris@0
|
488 * system_update_8002(), which in turn must run before system_update_8003().
|
Chris@0
|
489 *
|
Chris@0
|
490 * The function takes into account standard dependencies within each module, as
|
Chris@0
|
491 * shown above (i.e., the fact that each module's updates must run in numerical
|
Chris@0
|
492 * order), but also finds any cross-module dependencies that are defined by
|
Chris@0
|
493 * modules which implement hook_update_dependencies(), and builds them into the
|
Chris@0
|
494 * graph as well.
|
Chris@0
|
495 *
|
Chris@0
|
496 * @param $update_functions
|
Chris@0
|
497 * An organized array of update functions, in the format returned by
|
Chris@0
|
498 * update_get_update_function_list().
|
Chris@0
|
499 *
|
Chris@0
|
500 * @return
|
Chris@0
|
501 * A multidimensional array representing the dependency graph, suitable for
|
Chris@0
|
502 * passing in to Drupal\Component\Graph\Graph::searchAndSort(), but with extra
|
Chris@0
|
503 * information about each update function also included. Each array key
|
Chris@0
|
504 * contains the name of an update function, including all update functions
|
Chris@0
|
505 * from the provided list as well as any outside update functions which they
|
Chris@0
|
506 * directly depend on. Each value is an associative array containing the
|
Chris@0
|
507 * following keys:
|
Chris@0
|
508 * - 'edges': A representation of any other update functions that immediately
|
Chris@0
|
509 * depend on this one. See Drupal\Component\Graph\Graph::searchAndSort() for
|
Chris@0
|
510 * more details on the format.
|
Chris@0
|
511 * - 'module': The name of the module that this update function belongs to.
|
Chris@0
|
512 * - 'number': The number of this update function within that module.
|
Chris@0
|
513 *
|
Chris@0
|
514 * @see \Drupal\Component\Graph\Graph::searchAndSort()
|
Chris@0
|
515 * @see update_resolve_dependencies()
|
Chris@0
|
516 */
|
Chris@0
|
517 function update_build_dependency_graph($update_functions) {
|
Chris@0
|
518 // Initialize an array that will define a directed graph representing the
|
Chris@0
|
519 // dependencies between update functions.
|
Chris@0
|
520 $graph = [];
|
Chris@0
|
521
|
Chris@0
|
522 // Go through each update function and build an initial list of dependencies.
|
Chris@0
|
523 foreach ($update_functions as $module => $functions) {
|
Chris@0
|
524 $previous_function = NULL;
|
Chris@0
|
525 foreach ($functions as $number => $function) {
|
Chris@0
|
526 // Add an edge to the directed graph representing the fact that each
|
Chris@0
|
527 // update function in a given module must run after the update that
|
Chris@0
|
528 // numerically precedes it.
|
Chris@0
|
529 if ($previous_function) {
|
Chris@0
|
530 $graph[$previous_function]['edges'][$function] = TRUE;
|
Chris@0
|
531 }
|
Chris@0
|
532 $previous_function = $function;
|
Chris@0
|
533
|
Chris@0
|
534 // Define the module and update number associated with this function.
|
Chris@0
|
535 $graph[$function]['module'] = $module;
|
Chris@0
|
536 $graph[$function]['number'] = $number;
|
Chris@0
|
537 }
|
Chris@0
|
538 }
|
Chris@0
|
539
|
Chris@0
|
540 // Now add any explicit update dependencies declared by modules.
|
Chris@0
|
541 $update_dependencies = update_retrieve_dependencies();
|
Chris@0
|
542 foreach ($graph as $function => $data) {
|
Chris@0
|
543 if (!empty($update_dependencies[$data['module']][$data['number']])) {
|
Chris@0
|
544 foreach ($update_dependencies[$data['module']][$data['number']] as $module => $number) {
|
Chris@0
|
545 $dependency = $module . '_update_' . $number;
|
Chris@0
|
546 $graph[$dependency]['edges'][$function] = TRUE;
|
Chris@0
|
547 $graph[$dependency]['module'] = $module;
|
Chris@0
|
548 $graph[$dependency]['number'] = $number;
|
Chris@0
|
549 }
|
Chris@0
|
550 }
|
Chris@0
|
551 }
|
Chris@0
|
552
|
Chris@0
|
553 return $graph;
|
Chris@0
|
554 }
|
Chris@0
|
555
|
Chris@0
|
556 /**
|
Chris@0
|
557 * Determines if a module update is missing or unavailable.
|
Chris@0
|
558 *
|
Chris@0
|
559 * @param $module
|
Chris@0
|
560 * The name of the module.
|
Chris@0
|
561 * @param $number
|
Chris@0
|
562 * The number of the update within that module.
|
Chris@0
|
563 * @param $update_functions
|
Chris@0
|
564 * An organized array of update functions, in the format returned by
|
Chris@0
|
565 * update_get_update_function_list(). This should represent all module
|
Chris@0
|
566 * updates that are requested to run at the time this function is called.
|
Chris@0
|
567 *
|
Chris@0
|
568 * @return
|
Chris@0
|
569 * TRUE if the provided module update is not installed or is not in the
|
Chris@0
|
570 * provided list of updates to run; FALSE otherwise.
|
Chris@0
|
571 */
|
Chris@0
|
572 function update_is_missing($module, $number, $update_functions) {
|
Chris@0
|
573 return !isset($update_functions[$module][$number]) || !function_exists($update_functions[$module][$number]);
|
Chris@0
|
574 }
|
Chris@0
|
575
|
Chris@0
|
576 /**
|
Chris@0
|
577 * Determines if a module update has already been performed.
|
Chris@0
|
578 *
|
Chris@0
|
579 * @param $module
|
Chris@0
|
580 * The name of the module.
|
Chris@0
|
581 * @param $number
|
Chris@0
|
582 * The number of the update within that module.
|
Chris@0
|
583 *
|
Chris@0
|
584 * @return
|
Chris@0
|
585 * TRUE if the database schema indicates that the update has already been
|
Chris@0
|
586 * performed; FALSE otherwise.
|
Chris@0
|
587 */
|
Chris@0
|
588 function update_already_performed($module, $number) {
|
Chris@0
|
589 return $number <= drupal_get_installed_schema_version($module);
|
Chris@0
|
590 }
|
Chris@0
|
591
|
Chris@0
|
592 /**
|
Chris@0
|
593 * Invokes hook_update_dependencies() in all installed modules.
|
Chris@0
|
594 *
|
Chris@0
|
595 * This function is similar to \Drupal::moduleHandler()->invokeAll(), with the
|
Chris@0
|
596 * main difference that it does not require that a module be enabled to invoke
|
Chris@0
|
597 * its hook, only that it be installed. This allows the update system to
|
Chris@0
|
598 * properly perform updates even on modules that are currently disabled.
|
Chris@0
|
599 *
|
Chris@0
|
600 * @return
|
Chris@0
|
601 * An array of return values obtained by merging the results of the
|
Chris@0
|
602 * hook_update_dependencies() implementations in all installed modules.
|
Chris@0
|
603 *
|
Chris@0
|
604 * @see \Drupal\Core\Extension\ModuleHandlerInterface::invokeAll()
|
Chris@0
|
605 * @see hook_update_dependencies()
|
Chris@0
|
606 */
|
Chris@0
|
607 function update_retrieve_dependencies() {
|
Chris@0
|
608 $return = [];
|
Chris@0
|
609 // Get a list of installed modules, arranged so that we invoke their hooks in
|
Chris@0
|
610 // the same order that \Drupal::moduleHandler()->invokeAll() does.
|
Chris@0
|
611 foreach (\Drupal::keyValue('system.schema')->getAll() as $module => $schema) {
|
Chris@0
|
612 if ($schema == SCHEMA_UNINSTALLED) {
|
Chris@0
|
613 // Nothing to upgrade.
|
Chris@0
|
614 continue;
|
Chris@0
|
615 }
|
Chris@0
|
616 $function = $module . '_update_dependencies';
|
Chris@0
|
617 // Ensure install file is loaded.
|
Chris@0
|
618 module_load_install($module);
|
Chris@0
|
619 if (function_exists($function)) {
|
Chris@0
|
620 $updated_dependencies = $function();
|
Chris@0
|
621 // Each implementation of hook_update_dependencies() returns a
|
Chris@0
|
622 // multidimensional, associative array containing some keys that
|
Chris@0
|
623 // represent module names (which are strings) and other keys that
|
Chris@0
|
624 // represent update function numbers (which are integers). We cannot use
|
Chris@0
|
625 // array_merge_recursive() to properly merge these results, since it
|
Chris@0
|
626 // treats strings and integers differently. Therefore, we have to
|
Chris@0
|
627 // explicitly loop through the expected array structure here and perform
|
Chris@0
|
628 // the merge manually.
|
Chris@0
|
629 if (isset($updated_dependencies) && is_array($updated_dependencies)) {
|
Chris@0
|
630 foreach ($updated_dependencies as $module_name => $module_data) {
|
Chris@0
|
631 foreach ($module_data as $update_version => $update_data) {
|
Chris@0
|
632 foreach ($update_data as $module_dependency => $update_dependency) {
|
Chris@0
|
633 // If there are redundant dependencies declared for the same
|
Chris@0
|
634 // update function (so that it is declared to depend on more than
|
Chris@0
|
635 // one update from a particular module), record the dependency on
|
Chris@0
|
636 // the highest numbered update here, since that automatically
|
Chris@0
|
637 // implies the previous ones. For example, if one module's
|
Chris@0
|
638 // implementation of hook_update_dependencies() required this
|
Chris@0
|
639 // ordering:
|
Chris@0
|
640 //
|
Chris@0
|
641 // system_update_8002 ---> user_update_8001
|
Chris@0
|
642 //
|
Chris@0
|
643 // but another module's implementation of the hook required this
|
Chris@0
|
644 // one:
|
Chris@0
|
645 //
|
Chris@0
|
646 // system_update_8003 ---> user_update_8001
|
Chris@0
|
647 //
|
Chris@0
|
648 // we record the second one, since system_update_8002() is always
|
Chris@0
|
649 // guaranteed to run before system_update_8003() anyway (within
|
Chris@0
|
650 // an individual module, updates are always run in numerical
|
Chris@0
|
651 // order).
|
Chris@0
|
652 if (!isset($return[$module_name][$update_version][$module_dependency]) || $update_dependency > $return[$module_name][$update_version][$module_dependency]) {
|
Chris@0
|
653 $return[$module_name][$update_version][$module_dependency] = $update_dependency;
|
Chris@0
|
654 }
|
Chris@0
|
655 }
|
Chris@0
|
656 }
|
Chris@0
|
657 }
|
Chris@0
|
658 }
|
Chris@0
|
659 }
|
Chris@0
|
660 }
|
Chris@0
|
661
|
Chris@0
|
662 return $return;
|
Chris@0
|
663 }
|
Chris@0
|
664
|
Chris@0
|
665 /**
|
Chris@0
|
666 * Replace permissions during update.
|
Chris@0
|
667 *
|
Chris@0
|
668 * This function can replace one permission to several or even delete an old
|
Chris@0
|
669 * one.
|
Chris@0
|
670 *
|
Chris@0
|
671 * @param array $replace
|
Chris@0
|
672 * An associative array. The keys are the old permissions the values are lists
|
Chris@0
|
673 * of new permissions. If the list is an empty array, the old permission is
|
Chris@0
|
674 * removed.
|
Chris@0
|
675 */
|
Chris@0
|
676 function update_replace_permissions($replace) {
|
Chris@0
|
677 $prefix = 'user.role.';
|
Chris@0
|
678 $cut = strlen($prefix);
|
Chris@0
|
679 $role_names = \Drupal::service('config.storage')->listAll($prefix);
|
Chris@0
|
680 foreach ($role_names as $role_name) {
|
Chris@0
|
681 $rid = substr($role_name, $cut);
|
Chris@0
|
682 $config = \Drupal::config("user.role.$rid");
|
Chris@0
|
683 $permissions = $config->get('permissions') ?: [];
|
Chris@0
|
684 foreach ($replace as $old_permission => $new_permissions) {
|
Chris@0
|
685 if (($index = array_search($old_permission, $permissions)) !== FALSE) {
|
Chris@0
|
686 unset($permissions[$index]);
|
Chris@0
|
687 $permissions = array_unique(array_merge($permissions, $new_permissions));
|
Chris@0
|
688 }
|
Chris@0
|
689 }
|
Chris@0
|
690 $config
|
Chris@0
|
691 ->set('permissions', $permissions)
|
Chris@0
|
692 ->save();
|
Chris@0
|
693 }
|
Chris@0
|
694 }
|