Upgraded drupal core with security updates
[yaffs-website] / web / core / lib / Drupal / Core / Update / UpdateRegistry.php
1 <?php
2
3 namespace Drupal\Core\Update;
4
5 use Drupal\Core\Extension\Extension;
6 use Drupal\Core\Extension\ExtensionDiscovery;
7 use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
8
9 /**
10  * Provides all and missing update implementations.
11  *
12  * Note: This registry is specific to a type of updates, like 'post_update' as
13  * example.
14  *
15  * It therefore scans for functions named like the type of updates, so it looks
16  * like MODULE_UPDATETYPE_NAME() with NAME being a machine name.
17  */
18 class UpdateRegistry {
19
20   /**
21    * The used update name.
22    *
23    * @var string
24    */
25   protected $updateType = 'post_update';
26
27   /**
28    * The app root.
29    *
30    * @var string
31    */
32   protected $root;
33
34   /**
35    * The filename of the log file.
36    *
37    * @var string
38    */
39   protected $logFilename;
40
41   /**
42    * @var string[]
43    */
44   protected $enabledModules;
45
46   /**
47    * The key value storage.
48    *
49    * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
50    */
51   protected $keyValue;
52
53   /**
54    * Should we respect update functions in tests.
55    *
56    * @var bool|null
57    */
58   protected $includeTests = NULL;
59
60   /**
61    * The site path.
62    *
63    * @var string
64    */
65   protected $sitePath;
66
67   /**
68    * Constructs a new UpdateRegistry.
69    *
70    * @param string $root
71    *   The app root.
72    * @param string $site_path
73    *   The site path.
74    * @param string[] $enabled_modules
75    *   A list of enabled modules.
76    * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value
77    *   The key value store.
78    * @param bool|null $include_tests
79    *   (optional) A flag whether to include tests in the scanning of modules.
80    */
81   public function __construct($root, $site_path, array $enabled_modules, KeyValueStoreInterface $key_value, $include_tests = NULL) {
82     $this->root = $root;
83     $this->sitePath = $site_path;
84     $this->enabledModules = $enabled_modules;
85     $this->keyValue = $key_value;
86     $this->includeTests = $include_tests;
87   }
88
89   /**
90    * Gets all available update functions.
91    *
92    * @return callable[]
93    *   A list of update functions.
94    */
95   protected function getAvailableUpdateFunctions() {
96     $regexp = '/^(?<module>.+)_' . $this->updateType . '_(?<name>.+)$/';
97     $functions = get_defined_functions();
98
99     $updates = [];
100     foreach (preg_grep('/_' . $this->updateType . '_/', $functions['user']) as $function) {
101       // If this function is a module update function, add it to the list of
102       // module updates.
103       if (preg_match($regexp, $function, $matches)) {
104         if (in_array($matches['module'], $this->enabledModules)) {
105           $updates[] = $matches['module'] . '_' . $this->updateType . '_' . $matches['name'];
106         }
107       }
108     }
109
110     // Ensure that the update order is deterministic.
111     sort($updates);
112     return $updates;
113   }
114
115   /**
116    * Find all update functions that haven't been executed.
117    *
118    * @return callable[]
119    *   A list of update functions.
120    */
121   public function getPendingUpdateFunctions() {
122     // We need a) the list of active modules (we get that from the config
123     // bootstrap factory) and b) the path to the modules, we use the extension
124     // discovery for that.
125
126     $this->scanExtensionsAndLoadUpdateFiles();
127
128     // First figure out which hook_{$this->updateType}_NAME got executed
129     // already.
130     $existing_update_functions = $this->keyValue->get('existing_updates', []);
131
132     $available_update_functions = $this->getAvailableUpdateFunctions();
133     $not_executed_update_functions = array_diff($available_update_functions, $existing_update_functions);
134
135     return $not_executed_update_functions;
136   }
137
138   /**
139    * Loads all update files for a given list of extension.
140    *
141    * @param \Drupal\Core\Extension\Extension[] $module_extensions
142    *   The extensions used for loading.
143    */
144   protected function loadUpdateFiles(array $module_extensions) {
145     // Load all the {$this->updateType}.php files.
146     foreach ($this->enabledModules as $module) {
147       if (isset($module_extensions[$module])) {
148         $this->loadUpdateFile($module_extensions[$module]);
149       }
150     }
151   }
152
153   /**
154    * Loads the {$this->updateType}.php file for a given extension.
155    *
156    * @param \Drupal\Core\Extension\Extension $module
157    *   The extension of the module to load its file.
158    */
159   protected function loadUpdateFile(Extension $module) {
160     $filename = $this->root . '/' . $module->getPath() . '/' . $module->getName() . ".{$this->updateType}.php";
161     if (file_exists($filename)) {
162       include_once $filename;
163     }
164   }
165
166   /**
167    * Returns a list of all the pending updates.
168    *
169    * @return array[]
170    *   An associative array keyed by module name which contains all information
171    *   about database updates that need to be run, and any updates that are not
172    *   going to proceed due to missing requirements.
173    *
174    *   The subarray for each module can contain the following keys:
175    *   - start: The starting update that is to be processed. If this does not
176    *       exist then do not process any updates for this module as there are
177    *       other requirements that need to be resolved.
178    *   - pending: An array of all the pending updates for the module including
179    *       the description from source code comment for each update function.
180    *       This array is keyed by the update name.
181    */
182   public function getPendingUpdateInformation() {
183     $functions = $this->getPendingUpdateFunctions();
184
185     $ret = [];
186     foreach ($functions as $function) {
187       list($module, $update) = explode("_{$this->updateType}_", $function);
188       // The description for an update comes from its Doxygen.
189       $func = new \ReflectionFunction($function);
190       $description = trim(str_replace(["\n", '*', '/'], '', $func->getDocComment()), ' ');
191       $ret[$module]['pending'][$update] = $description;
192       if (!isset($ret[$module]['start'])) {
193         $ret[$module]['start'] = $update;
194       }
195     }
196     return $ret;
197   }
198
199   /**
200    * Registers that update fucntions got executed.
201    *
202    * @param string[] $function_names
203    *   The executed update functions.
204    *
205    * @return $this
206    */
207   public function registerInvokedUpdates(array $function_names) {
208     $executed_updates = $this->keyValue->get('existing_updates', []);
209     $executed_updates = array_merge($executed_updates, $function_names);
210     $this->keyValue->set('existing_updates', $executed_updates);
211
212     return $this;
213   }
214
215   /**
216    * Returns all available updates for a given module.
217    *
218    * @param string $module_name
219    *   The module name.
220    *
221    * @return callable[]
222    *   A list of update functions.
223    */
224   public function getModuleUpdateFunctions($module_name) {
225     $this->scanExtensionsAndLoadUpdateFiles();
226     $all_functions = $this->getAvailableUpdateFunctions();
227
228     return array_filter($all_functions, function($function_name) use ($module_name) {
229       list($function_module_name, ) = explode("_{$this->updateType}_", $function_name);
230       return $function_module_name === $module_name;
231     });
232   }
233
234   /**
235    * Scans all module + profile extensions and load the update files.
236    */
237   protected function scanExtensionsAndLoadUpdateFiles() {
238     // Scan the module list.
239     $extension_discovery = new ExtensionDiscovery($this->root, FALSE, [], $this->sitePath);
240     $module_extensions = $extension_discovery->scan('module');
241
242     $profile_extensions = $extension_discovery->scan('profile');
243     $extensions = array_merge($module_extensions, $profile_extensions);
244
245     $this->loadUpdateFiles($extensions);
246   }
247
248   /**
249    * Filters out already executed update functions by module.
250    *
251    * @param string $module
252    *   The module name.
253    */
254   public function filterOutInvokedUpdatesByModule($module) {
255     $existing_update_functions = $this->keyValue->get('existing_updates', []);
256
257     $remaining_update_functions = array_filter($existing_update_functions, function($function_name) use ($module) {
258       return strpos($function_name, "{$module}_{$this->updateType}_") !== 0;
259     });
260
261     $this->keyValue->set('existing_updates', array_values($remaining_update_functions));
262   }
263
264 }