3 namespace Drupal\Core\Updater;
5 use Drupal\Core\FileTransfer\FileTransferException;
6 use Drupal\Core\FileTransfer\FileTransfer;
9 * Defines the base class for Updaters used in Drupal.
14 * Directory to install from.
21 * The root directory under which new projects will be copied.
28 * Constructs a new updater.
30 * @param string $source
31 * Directory to install from.
33 * The root directory under which the project will be copied to if it's a
34 * new project. Usually this is the app root (the directory in which the
35 * Drupal site is installed).
37 public function __construct($source, $root) {
38 $this->source = $source;
40 $this->name = self::getProjectName($source);
41 $this->title = self::getProjectTitle($source);
45 * Returns an Updater of the appropriate type depending on the source.
47 * If a directory is provided which contains a module, will return a
50 * @param string $source
51 * Directory of a Drupal project.
53 * The root directory under which the project will be copied to if it's a
54 * new project. Usually this is the app root (the directory in which the
55 * Drupal site is installed).
57 * @return \Drupal\Core\Updater\Updater
58 * A new Drupal\Core\Updater\Updater object.
60 * @throws \Drupal\Core\Updater\UpdaterException
62 public static function factory($source, $root) {
63 if (is_dir($source)) {
64 $updater = self::getUpdaterFromDirectory($source);
67 throw new UpdaterException(t('Unable to determine the type of the source directory.'));
69 return new $updater($source, $root);
73 * Determines which Updater class can operate on the given directory.
75 * @param string $directory
76 * Extracted Drupal project.
79 * The class name which can work with this project type.
81 * @throws \Drupal\Core\Updater\UpdaterException
83 public static function getUpdaterFromDirectory($directory) {
84 // Gets a list of possible implementing classes.
85 $updaters = drupal_get_updaters();
86 foreach ($updaters as $updater) {
87 $class = $updater['class'];
88 if (call_user_func([$class, 'canUpdateDirectory'], $directory)) {
92 throw new UpdaterException(t('Cannot determine the type of project.'));
96 * Determines what the most important (or only) info file is in a directory.
98 * Since there is no enforcement of which info file is the project's "main"
99 * info file, this will get one with the same name as the directory, or the
100 * first one it finds. Not ideal, but needs a larger solution.
102 * @param string $directory
103 * Directory to search in.
106 * Path to the info file.
108 public static function findInfoFile($directory) {
109 $info_files = file_scan_directory($directory, '/.*\.info.yml$/');
113 foreach ($info_files as $info_file) {
114 if (mb_substr($info_file->filename, 0, -9) == drupal_basename($directory)) {
115 // Info file Has the same name as the directory, return it.
116 return $info_file->uri;
119 // Otherwise, return the first one.
120 $info_file = array_shift($info_files);
121 return $info_file->uri;
125 * Get Extension information from directory.
127 * @param string $directory
128 * Directory to search in.
133 * @throws \Drupal\Core\Updater\UpdaterException
134 * If the info parser does not provide any info.
136 protected static function getExtensionInfo($directory) {
137 $info_file = static::findInfoFile($directory);
138 $info = \Drupal::service('info_parser')->parse($info_file);
140 throw new UpdaterException(t('Unable to parse info file: %info_file.', ['%info_file' => $info_file]));
147 * Gets the name of the project directory (basename).
149 * @todo It would be nice, if projects contained an info file which could
150 * provide their canonical name.
152 * @param string $directory
155 * The name of the project.
157 public static function getProjectName($directory) {
158 return drupal_basename($directory);
162 * Returns the project name from a Drupal info file.
164 * @param string $directory
165 * Directory to search for the info file.
168 * The title of the project.
170 * @throws \Drupal\Core\Updater\UpdaterException
172 public static function getProjectTitle($directory) {
173 $info_file = self::findInfoFile($directory);
174 $info = \Drupal::service('info_parser')->parse($info_file);
176 throw new UpdaterException(t('Unable to parse info file: %info_file.', ['%info_file' => $info_file]));
178 return $info['name'];
182 * Stores the default parameters for the Updater.
184 * @param array $overrides
185 * An array of overrides for the default parameters.
188 * An array of configuration parameters for an update or install operation.
190 protected function getInstallArgs($overrides = []) {
192 'make_backup' => FALSE,
193 'install_dir' => $this->getInstallDirectory(),
194 'backup_dir' => $this->getBackupDir(),
196 return array_merge($args, $overrides);
200 * Updates a Drupal project and returns a list of next actions.
202 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
203 * Object that is a child of FileTransfer. Used for moving files
205 * @param array $overrides
206 * An array of settings to override defaults; see self::getInstallArgs().
209 * An array of links which the user may need to complete the update
211 * @throws \Drupal\Core\Updater\UpdaterException
212 * @throws \Drupal\Core\Updater\UpdaterFileTransferException
214 public function update(&$filetransfer, $overrides = []) {
216 // Establish arguments with possible overrides.
217 $args = $this->getInstallArgs($overrides);
220 if ($args['make_backup']) {
221 $this->makeBackup($filetransfer, $args['install_dir'], $args['backup_dir']);
225 // This is bad, don't want to delete the install directory.
226 throw new UpdaterException(t('Fatal error in update, cowardly refusing to wipe out the install directory.'));
229 // Make sure the installation parent directory exists and is writable.
230 $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
232 if (is_dir($args['install_dir'] . '/' . $this->name)) {
233 // Remove the existing installed file.
234 $filetransfer->removeDirectory($args['install_dir'] . '/' . $this->name);
237 // Copy the directory in place.
238 $filetransfer->copyDirectory($this->source, $args['install_dir']);
240 // Make sure what we just installed is readable by the web server.
241 $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
244 // @todo Decide if we want to implement this.
247 // For now, just return a list of links of things to do.
248 return $this->postUpdateTasks();
250 catch (FileTransferException $e) {
251 throw new UpdaterFileTransferException(t('File Transfer failed, reason: @reason', ['@reason' => strtr($e->getMessage(), $e->arguments)]));
256 * Installs a Drupal project, returns a list of next actions.
258 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
259 * Object that is a child of FileTransfer.
260 * @param array $overrides
261 * An array of settings to override defaults; see self::getInstallArgs().
264 * An array of links which the user may need to complete the install.
266 * @throws \Drupal\Core\Updater\UpdaterFileTransferException
268 public function install(&$filetransfer, $overrides = []) {
270 // Establish arguments with possible overrides.
271 $args = $this->getInstallArgs($overrides);
273 // Make sure the installation parent directory exists and is writable.
274 $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
276 // Copy the directory in place.
277 $filetransfer->copyDirectory($this->source, $args['install_dir']);
279 // Make sure what we just installed is readable by the web server.
280 $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
282 // Potentially enable something?
283 // @todo Decide if we want to implement this.
284 $this->postInstall();
285 // For now, just return a list of links of things to do.
286 return $this->postInstallTasks();
288 catch (FileTransferException $e) {
289 throw new UpdaterFileTransferException(t('File Transfer failed, reason: @reason', ['@reason' => strtr($e->getMessage(), $e->arguments)]));
294 * Makes sure the installation parent directory exists and is writable.
296 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
297 * Object which is a child of FileTransfer.
298 * @param string $directory
299 * The installation directory to prepare.
301 * @throws \Drupal\Core\Updater\UpdaterException
303 public function prepareInstallDirectory(&$filetransfer, $directory) {
304 // Make the parent dir writable if need be and create the dir.
305 if (!is_dir($directory)) {
306 $parent_dir = dirname($directory);
307 if (!is_writable($parent_dir)) {
308 @chmod($parent_dir, 0755);
309 // It is expected that this will fail if the directory is owned by the
310 // FTP user. If the FTP user == web server, it will succeed.
312 $filetransfer->createDirectory($directory);
313 $this->makeWorldReadable($filetransfer, $directory);
315 catch (FileTransferException $e) {
316 // Probably still not writable. Try to chmod and do it again.
317 // @todo Make a new exception class so we can catch it differently.
319 $old_perms = substr(sprintf('%o', fileperms($parent_dir)), -4);
320 $filetransfer->chmod($parent_dir, 0755);
321 $filetransfer->createDirectory($directory);
322 $this->makeWorldReadable($filetransfer, $directory);
323 // Put the permissions back.
324 $filetransfer->chmod($parent_dir, intval($old_perms, 8));
326 catch (FileTransferException $e) {
327 $message = t($e->getMessage(), $e->arguments);
328 $throw_message = t('Unable to create %directory due to the following: %reason', ['%directory' => $directory, '%reason' => $message]);
329 throw new UpdaterException($throw_message);
332 // Put the parent directory back.
333 @chmod($parent_dir, 0555);
339 * Ensures that a given directory is world readable.
341 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
342 * Object which is a child of FileTransfer.
343 * @param string $path
344 * The file path to make world readable.
345 * @param bool $recursive
346 * If the chmod should be applied recursively.
348 public function makeWorldReadable(&$filetransfer, $path, $recursive = TRUE) {
349 if (!is_executable($path)) {
350 // Set it to read + execute.
351 $new_perms = substr(sprintf('%o', fileperms($path)), -4, -1) . "5";
352 $filetransfer->chmod($path, intval($new_perms, 8), $recursive);
359 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
360 * Object which is a child of FileTransfer.
361 * @param string $from
362 * The file path to copy from.
364 * The file path to copy to.
366 * @todo Not implemented: https://www.drupal.org/node/2474355
368 public function makeBackup(FileTransfer $filetransfer, $from, $to) {
372 * Returns the full path to a directory where backups should be written.
374 public function getBackupDir() {
375 return \Drupal::service('stream_wrapper_manager')->getViaScheme('temporary')->getDirectoryPath();
379 * Performs actions after new code is updated.
381 public function postUpdate() {
385 * Performs actions after installation.
387 public function postInstall() {
391 * Returns an array of links to pages that should be visited post operation.
394 * Links which provide actions to take after the install is finished.
396 public function postInstallTasks() {
401 * Returns an array of links to pages that should be visited post operation.
404 * Links which provide actions to take after the update is finished.
406 public function postUpdateTasks() {