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