Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Updater;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\FileTransfer\FileTransferException;
|
Chris@0
|
6 use Drupal\Core\FileTransfer\FileTransfer;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Defines the base class for Updaters used in Drupal.
|
Chris@0
|
10 */
|
Chris@0
|
11 class Updater {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Directory to install from.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @var string
|
Chris@0
|
17 */
|
Chris@0
|
18 public $source;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@0
|
21 * The root directory under which new projects will be copied.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @var string
|
Chris@0
|
24 */
|
Chris@0
|
25 protected $root;
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * Constructs a new updater.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @param string $source
|
Chris@0
|
31 * Directory to install from.
|
Chris@0
|
32 * @param string $root
|
Chris@0
|
33 * The root directory under which the project will be copied to if it's a
|
Chris@0
|
34 * new project. Usually this is the app root (the directory in which the
|
Chris@0
|
35 * Drupal site is installed).
|
Chris@0
|
36 */
|
Chris@0
|
37 public function __construct($source, $root) {
|
Chris@0
|
38 $this->source = $source;
|
Chris@0
|
39 $this->root = $root;
|
Chris@0
|
40 $this->name = self::getProjectName($source);
|
Chris@0
|
41 $this->title = self::getProjectTitle($source);
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Returns an Updater of the appropriate type depending on the source.
|
Chris@0
|
46 *
|
Chris@0
|
47 * If a directory is provided which contains a module, will return a
|
Chris@0
|
48 * ModuleUpdater.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @param string $source
|
Chris@0
|
51 * Directory of a Drupal project.
|
Chris@0
|
52 * @param string $root
|
Chris@0
|
53 * The root directory under which the project will be copied to if it's a
|
Chris@0
|
54 * new project. Usually this is the app root (the directory in which the
|
Chris@0
|
55 * Drupal site is installed).
|
Chris@0
|
56 *
|
Chris@0
|
57 * @return \Drupal\Core\Updater\Updater
|
Chris@0
|
58 * A new Drupal\Core\Updater\Updater object.
|
Chris@0
|
59 *
|
Chris@0
|
60 * @throws \Drupal\Core\Updater\UpdaterException
|
Chris@0
|
61 */
|
Chris@0
|
62 public static function factory($source, $root) {
|
Chris@0
|
63 if (is_dir($source)) {
|
Chris@0
|
64 $updater = self::getUpdaterFromDirectory($source);
|
Chris@0
|
65 }
|
Chris@0
|
66 else {
|
Chris@0
|
67 throw new UpdaterException(t('Unable to determine the type of the source directory.'));
|
Chris@0
|
68 }
|
Chris@0
|
69 return new $updater($source, $root);
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Determines which Updater class can operate on the given directory.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @param string $directory
|
Chris@0
|
76 * Extracted Drupal project.
|
Chris@0
|
77 *
|
Chris@0
|
78 * @return string
|
Chris@0
|
79 * The class name which can work with this project type.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @throws \Drupal\Core\Updater\UpdaterException
|
Chris@0
|
82 */
|
Chris@0
|
83 public static function getUpdaterFromDirectory($directory) {
|
Chris@0
|
84 // Gets a list of possible implementing classes.
|
Chris@0
|
85 $updaters = drupal_get_updaters();
|
Chris@0
|
86 foreach ($updaters as $updater) {
|
Chris@0
|
87 $class = $updater['class'];
|
Chris@0
|
88 if (call_user_func([$class, 'canUpdateDirectory'], $directory)) {
|
Chris@0
|
89 return $class;
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|
Chris@0
|
92 throw new UpdaterException(t('Cannot determine the type of project.'));
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Determines what the most important (or only) info file is in a directory.
|
Chris@0
|
97 *
|
Chris@0
|
98 * Since there is no enforcement of which info file is the project's "main"
|
Chris@0
|
99 * info file, this will get one with the same name as the directory, or the
|
Chris@0
|
100 * first one it finds. Not ideal, but needs a larger solution.
|
Chris@0
|
101 *
|
Chris@0
|
102 * @param string $directory
|
Chris@0
|
103 * Directory to search in.
|
Chris@0
|
104 *
|
Chris@0
|
105 * @return string
|
Chris@0
|
106 * Path to the info file.
|
Chris@0
|
107 */
|
Chris@0
|
108 public static function findInfoFile($directory) {
|
Chris@0
|
109 $info_files = file_scan_directory($directory, '/.*\.info.yml$/');
|
Chris@0
|
110 if (!$info_files) {
|
Chris@0
|
111 return FALSE;
|
Chris@0
|
112 }
|
Chris@0
|
113 foreach ($info_files as $info_file) {
|
Chris@18
|
114 if (mb_substr($info_file->filename, 0, -9) == \Drupal::service('file_system')->basename($directory)) {
|
Chris@0
|
115 // Info file Has the same name as the directory, return it.
|
Chris@0
|
116 return $info_file->uri;
|
Chris@0
|
117 }
|
Chris@0
|
118 }
|
Chris@0
|
119 // Otherwise, return the first one.
|
Chris@0
|
120 $info_file = array_shift($info_files);
|
Chris@0
|
121 return $info_file->uri;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * Get Extension information from directory.
|
Chris@0
|
126 *
|
Chris@0
|
127 * @param string $directory
|
Chris@0
|
128 * Directory to search in.
|
Chris@0
|
129 *
|
Chris@0
|
130 * @return array
|
Chris@0
|
131 * Extension info.
|
Chris@0
|
132 *
|
Chris@0
|
133 * @throws \Drupal\Core\Updater\UpdaterException
|
Chris@0
|
134 * If the info parser does not provide any info.
|
Chris@0
|
135 */
|
Chris@0
|
136 protected static function getExtensionInfo($directory) {
|
Chris@0
|
137 $info_file = static::findInfoFile($directory);
|
Chris@0
|
138 $info = \Drupal::service('info_parser')->parse($info_file);
|
Chris@0
|
139 if (empty($info)) {
|
Chris@0
|
140 throw new UpdaterException(t('Unable to parse info file: %info_file.', ['%info_file' => $info_file]));
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 return $info;
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * Gets the name of the project directory (basename).
|
Chris@0
|
148 *
|
Chris@0
|
149 * @todo It would be nice, if projects contained an info file which could
|
Chris@0
|
150 * provide their canonical name.
|
Chris@0
|
151 *
|
Chris@0
|
152 * @param string $directory
|
Chris@0
|
153 *
|
Chris@0
|
154 * @return string
|
Chris@0
|
155 * The name of the project.
|
Chris@0
|
156 */
|
Chris@0
|
157 public static function getProjectName($directory) {
|
Chris@18
|
158 return \Drupal::service('file_system')->basename($directory);
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 /**
|
Chris@0
|
162 * Returns the project name from a Drupal info file.
|
Chris@0
|
163 *
|
Chris@0
|
164 * @param string $directory
|
Chris@0
|
165 * Directory to search for the info file.
|
Chris@0
|
166 *
|
Chris@0
|
167 * @return string
|
Chris@0
|
168 * The title of the project.
|
Chris@0
|
169 *
|
Chris@0
|
170 * @throws \Drupal\Core\Updater\UpdaterException
|
Chris@0
|
171 */
|
Chris@0
|
172 public static function getProjectTitle($directory) {
|
Chris@0
|
173 $info_file = self::findInfoFile($directory);
|
Chris@0
|
174 $info = \Drupal::service('info_parser')->parse($info_file);
|
Chris@0
|
175 if (empty($info)) {
|
Chris@0
|
176 throw new UpdaterException(t('Unable to parse info file: %info_file.', ['%info_file' => $info_file]));
|
Chris@0
|
177 }
|
Chris@0
|
178 return $info['name'];
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 /**
|
Chris@0
|
182 * Stores the default parameters for the Updater.
|
Chris@0
|
183 *
|
Chris@0
|
184 * @param array $overrides
|
Chris@0
|
185 * An array of overrides for the default parameters.
|
Chris@0
|
186 *
|
Chris@0
|
187 * @return array
|
Chris@0
|
188 * An array of configuration parameters for an update or install operation.
|
Chris@0
|
189 */
|
Chris@0
|
190 protected function getInstallArgs($overrides = []) {
|
Chris@0
|
191 $args = [
|
Chris@0
|
192 'make_backup' => FALSE,
|
Chris@0
|
193 'install_dir' => $this->getInstallDirectory(),
|
Chris@0
|
194 'backup_dir' => $this->getBackupDir(),
|
Chris@0
|
195 ];
|
Chris@0
|
196 return array_merge($args, $overrides);
|
Chris@0
|
197 }
|
Chris@0
|
198
|
Chris@0
|
199 /**
|
Chris@0
|
200 * Updates a Drupal project and returns a list of next actions.
|
Chris@0
|
201 *
|
Chris@0
|
202 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
|
Chris@0
|
203 * Object that is a child of FileTransfer. Used for moving files
|
Chris@0
|
204 * to the server.
|
Chris@0
|
205 * @param array $overrides
|
Chris@0
|
206 * An array of settings to override defaults; see self::getInstallArgs().
|
Chris@0
|
207 *
|
Chris@0
|
208 * @return array
|
Chris@0
|
209 * An array of links which the user may need to complete the update
|
Chris@0
|
210 *
|
Chris@0
|
211 * @throws \Drupal\Core\Updater\UpdaterException
|
Chris@0
|
212 * @throws \Drupal\Core\Updater\UpdaterFileTransferException
|
Chris@0
|
213 */
|
Chris@0
|
214 public function update(&$filetransfer, $overrides = []) {
|
Chris@0
|
215 try {
|
Chris@0
|
216 // Establish arguments with possible overrides.
|
Chris@0
|
217 $args = $this->getInstallArgs($overrides);
|
Chris@0
|
218
|
Chris@0
|
219 // Take a Backup.
|
Chris@0
|
220 if ($args['make_backup']) {
|
Chris@0
|
221 $this->makeBackup($filetransfer, $args['install_dir'], $args['backup_dir']);
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 if (!$this->name) {
|
Chris@0
|
225 // This is bad, don't want to delete the install directory.
|
Chris@0
|
226 throw new UpdaterException(t('Fatal error in update, cowardly refusing to wipe out the install directory.'));
|
Chris@0
|
227 }
|
Chris@0
|
228
|
Chris@0
|
229 // Make sure the installation parent directory exists and is writable.
|
Chris@0
|
230 $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
|
Chris@0
|
231
|
Chris@0
|
232 if (is_dir($args['install_dir'] . '/' . $this->name)) {
|
Chris@0
|
233 // Remove the existing installed file.
|
Chris@0
|
234 $filetransfer->removeDirectory($args['install_dir'] . '/' . $this->name);
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 // Copy the directory in place.
|
Chris@0
|
238 $filetransfer->copyDirectory($this->source, $args['install_dir']);
|
Chris@0
|
239
|
Chris@0
|
240 // Make sure what we just installed is readable by the web server.
|
Chris@0
|
241 $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
|
Chris@0
|
242
|
Chris@0
|
243 // Run the updates.
|
Chris@0
|
244 // @todo Decide if we want to implement this.
|
Chris@0
|
245 $this->postUpdate();
|
Chris@0
|
246
|
Chris@0
|
247 // For now, just return a list of links of things to do.
|
Chris@0
|
248 return $this->postUpdateTasks();
|
Chris@0
|
249 }
|
Chris@0
|
250 catch (FileTransferException $e) {
|
Chris@0
|
251 throw new UpdaterFileTransferException(t('File Transfer failed, reason: @reason', ['@reason' => strtr($e->getMessage(), $e->arguments)]));
|
Chris@0
|
252 }
|
Chris@0
|
253 }
|
Chris@0
|
254
|
Chris@0
|
255 /**
|
Chris@0
|
256 * Installs a Drupal project, returns a list of next actions.
|
Chris@0
|
257 *
|
Chris@0
|
258 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
|
Chris@0
|
259 * Object that is a child of FileTransfer.
|
Chris@0
|
260 * @param array $overrides
|
Chris@0
|
261 * An array of settings to override defaults; see self::getInstallArgs().
|
Chris@0
|
262 *
|
Chris@0
|
263 * @return array
|
Chris@0
|
264 * An array of links which the user may need to complete the install.
|
Chris@0
|
265 *
|
Chris@0
|
266 * @throws \Drupal\Core\Updater\UpdaterFileTransferException
|
Chris@0
|
267 */
|
Chris@0
|
268 public function install(&$filetransfer, $overrides = []) {
|
Chris@0
|
269 try {
|
Chris@0
|
270 // Establish arguments with possible overrides.
|
Chris@0
|
271 $args = $this->getInstallArgs($overrides);
|
Chris@0
|
272
|
Chris@0
|
273 // Make sure the installation parent directory exists and is writable.
|
Chris@0
|
274 $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
|
Chris@0
|
275
|
Chris@0
|
276 // Copy the directory in place.
|
Chris@0
|
277 $filetransfer->copyDirectory($this->source, $args['install_dir']);
|
Chris@0
|
278
|
Chris@0
|
279 // Make sure what we just installed is readable by the web server.
|
Chris@0
|
280 $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
|
Chris@0
|
281
|
Chris@0
|
282 // Potentially enable something?
|
Chris@0
|
283 // @todo Decide if we want to implement this.
|
Chris@0
|
284 $this->postInstall();
|
Chris@0
|
285 // For now, just return a list of links of things to do.
|
Chris@0
|
286 return $this->postInstallTasks();
|
Chris@0
|
287 }
|
Chris@0
|
288 catch (FileTransferException $e) {
|
Chris@0
|
289 throw new UpdaterFileTransferException(t('File Transfer failed, reason: @reason', ['@reason' => strtr($e->getMessage(), $e->arguments)]));
|
Chris@0
|
290 }
|
Chris@0
|
291 }
|
Chris@0
|
292
|
Chris@0
|
293 /**
|
Chris@0
|
294 * Makes sure the installation parent directory exists and is writable.
|
Chris@0
|
295 *
|
Chris@0
|
296 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
|
Chris@0
|
297 * Object which is a child of FileTransfer.
|
Chris@0
|
298 * @param string $directory
|
Chris@0
|
299 * The installation directory to prepare.
|
Chris@0
|
300 *
|
Chris@0
|
301 * @throws \Drupal\Core\Updater\UpdaterException
|
Chris@0
|
302 */
|
Chris@0
|
303 public function prepareInstallDirectory(&$filetransfer, $directory) {
|
Chris@0
|
304 // Make the parent dir writable if need be and create the dir.
|
Chris@0
|
305 if (!is_dir($directory)) {
|
Chris@0
|
306 $parent_dir = dirname($directory);
|
Chris@0
|
307 if (!is_writable($parent_dir)) {
|
Chris@0
|
308 @chmod($parent_dir, 0755);
|
Chris@0
|
309 // It is expected that this will fail if the directory is owned by the
|
Chris@0
|
310 // FTP user. If the FTP user == web server, it will succeed.
|
Chris@0
|
311 try {
|
Chris@0
|
312 $filetransfer->createDirectory($directory);
|
Chris@0
|
313 $this->makeWorldReadable($filetransfer, $directory);
|
Chris@0
|
314 }
|
Chris@0
|
315 catch (FileTransferException $e) {
|
Chris@0
|
316 // Probably still not writable. Try to chmod and do it again.
|
Chris@0
|
317 // @todo Make a new exception class so we can catch it differently.
|
Chris@0
|
318 try {
|
Chris@0
|
319 $old_perms = substr(sprintf('%o', fileperms($parent_dir)), -4);
|
Chris@0
|
320 $filetransfer->chmod($parent_dir, 0755);
|
Chris@0
|
321 $filetransfer->createDirectory($directory);
|
Chris@0
|
322 $this->makeWorldReadable($filetransfer, $directory);
|
Chris@0
|
323 // Put the permissions back.
|
Chris@0
|
324 $filetransfer->chmod($parent_dir, intval($old_perms, 8));
|
Chris@0
|
325 }
|
Chris@0
|
326 catch (FileTransferException $e) {
|
Chris@0
|
327 $message = t($e->getMessage(), $e->arguments);
|
Chris@0
|
328 $throw_message = t('Unable to create %directory due to the following: %reason', ['%directory' => $directory, '%reason' => $message]);
|
Chris@0
|
329 throw new UpdaterException($throw_message);
|
Chris@0
|
330 }
|
Chris@0
|
331 }
|
Chris@0
|
332 // Put the parent directory back.
|
Chris@0
|
333 @chmod($parent_dir, 0555);
|
Chris@0
|
334 }
|
Chris@0
|
335 }
|
Chris@0
|
336 }
|
Chris@0
|
337
|
Chris@0
|
338 /**
|
Chris@0
|
339 * Ensures that a given directory is world readable.
|
Chris@0
|
340 *
|
Chris@0
|
341 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
|
Chris@0
|
342 * Object which is a child of FileTransfer.
|
Chris@0
|
343 * @param string $path
|
Chris@0
|
344 * The file path to make world readable.
|
Chris@0
|
345 * @param bool $recursive
|
Chris@0
|
346 * If the chmod should be applied recursively.
|
Chris@0
|
347 */
|
Chris@0
|
348 public function makeWorldReadable(&$filetransfer, $path, $recursive = TRUE) {
|
Chris@0
|
349 if (!is_executable($path)) {
|
Chris@0
|
350 // Set it to read + execute.
|
Chris@0
|
351 $new_perms = substr(sprintf('%o', fileperms($path)), -4, -1) . "5";
|
Chris@0
|
352 $filetransfer->chmod($path, intval($new_perms, 8), $recursive);
|
Chris@0
|
353 }
|
Chris@0
|
354 }
|
Chris@0
|
355
|
Chris@0
|
356 /**
|
Chris@0
|
357 * Performs a backup.
|
Chris@0
|
358 *
|
Chris@0
|
359 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
|
Chris@0
|
360 * Object which is a child of FileTransfer.
|
Chris@0
|
361 * @param string $from
|
Chris@0
|
362 * The file path to copy from.
|
Chris@0
|
363 * @param string $to
|
Chris@0
|
364 * The file path to copy to.
|
Chris@0
|
365 *
|
Chris@0
|
366 * @todo Not implemented: https://www.drupal.org/node/2474355
|
Chris@0
|
367 */
|
Chris@0
|
368 public function makeBackup(FileTransfer $filetransfer, $from, $to) {
|
Chris@0
|
369 }
|
Chris@0
|
370
|
Chris@0
|
371 /**
|
Chris@0
|
372 * Returns the full path to a directory where backups should be written.
|
Chris@0
|
373 */
|
Chris@0
|
374 public function getBackupDir() {
|
Chris@0
|
375 return \Drupal::service('stream_wrapper_manager')->getViaScheme('temporary')->getDirectoryPath();
|
Chris@0
|
376 }
|
Chris@0
|
377
|
Chris@0
|
378 /**
|
Chris@0
|
379 * Performs actions after new code is updated.
|
Chris@0
|
380 */
|
Chris@0
|
381 public function postUpdate() {
|
Chris@0
|
382 }
|
Chris@0
|
383
|
Chris@0
|
384 /**
|
Chris@0
|
385 * Performs actions after installation.
|
Chris@0
|
386 */
|
Chris@0
|
387 public function postInstall() {
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 /**
|
Chris@0
|
391 * Returns an array of links to pages that should be visited post operation.
|
Chris@0
|
392 *
|
Chris@0
|
393 * @return array
|
Chris@0
|
394 * Links which provide actions to take after the install is finished.
|
Chris@0
|
395 */
|
Chris@0
|
396 public function postInstallTasks() {
|
Chris@0
|
397 return [];
|
Chris@0
|
398 }
|
Chris@0
|
399
|
Chris@0
|
400 /**
|
Chris@0
|
401 * Returns an array of links to pages that should be visited post operation.
|
Chris@0
|
402 *
|
Chris@0
|
403 * @return array
|
Chris@0
|
404 * Links which provide actions to take after the update is finished.
|
Chris@0
|
405 */
|
Chris@0
|
406 public function postUpdateTasks() {
|
Chris@0
|
407 return [];
|
Chris@0
|
408 }
|
Chris@0
|
409
|
Chris@0
|
410 }
|