3 namespace Drupal\Core\Updater;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\FileTransfer\FileTransferException;
7 use Drupal\Core\FileTransfer\FileTransfer;
10 * Defines the base class for Updaters used in Drupal.
15 * Directory to install from.
22 * The root directory under which new projects will be copied.
29 * Constructs a new updater.
31 * @param string $source
32 * Directory to install from.
34 * The root directory under which the project will be copied to if it's a
35 * new project. Usually this is the app root (the directory in which the
36 * Drupal site is installed).
38 public function __construct($source, $root) {
39 $this->source = $source;
41 $this->name = self::getProjectName($source);
42 $this->title = self::getProjectTitle($source);
46 * Returns an Updater of the appropriate type depending on the source.
48 * If a directory is provided which contains a module, will return a
51 * @param string $source
52 * Directory of a Drupal project.
54 * The root directory under which the project will be copied to if it's a
55 * new project. Usually this is the app root (the directory in which the
56 * Drupal site is installed).
58 * @return \Drupal\Core\Updater\Updater
59 * A new Drupal\Core\Updater\Updater object.
61 * @throws \Drupal\Core\Updater\UpdaterException
63 public static function factory($source, $root) {
64 if (is_dir($source)) {
65 $updater = self::getUpdaterFromDirectory($source);
68 throw new UpdaterException(t('Unable to determine the type of the source directory.'));
70 return new $updater($source, $root);
74 * Determines which Updater class can operate on the given directory.
76 * @param string $directory
77 * Extracted Drupal project.
80 * The class name which can work with this project type.
82 * @throws \Drupal\Core\Updater\UpdaterException
84 public static function getUpdaterFromDirectory($directory) {
85 // Gets a list of possible implementing classes.
86 $updaters = drupal_get_updaters();
87 foreach ($updaters as $updater) {
88 $class = $updater['class'];
89 if (call_user_func([$class, 'canUpdateDirectory'], $directory)) {
93 throw new UpdaterException(t('Cannot determine the type of project.'));
97 * Determines what the most important (or only) info file is in a directory.
99 * Since there is no enforcement of which info file is the project's "main"
100 * info file, this will get one with the same name as the directory, or the
101 * first one it finds. Not ideal, but needs a larger solution.
103 * @param string $directory
104 * Directory to search in.
107 * Path to the info file.
109 public static function findInfoFile($directory) {
110 $info_files = file_scan_directory($directory, '/.*\.info.yml$/');
114 foreach ($info_files as $info_file) {
115 if (Unicode::substr($info_file->filename, 0, -9) == drupal_basename($directory)) {
116 // Info file Has the same name as the directory, return it.
117 return $info_file->uri;
120 // Otherwise, return the first one.
121 $info_file = array_shift($info_files);
122 return $info_file->uri;
126 * Get Extension information from directory.
128 * @param string $directory
129 * Directory to search in.
134 * @throws \Drupal\Core\Updater\UpdaterException
135 * If the info parser does not provide any info.
137 protected static function getExtensionInfo($directory) {
138 $info_file = static::findInfoFile($directory);
139 $info = \Drupal::service('info_parser')->parse($info_file);
141 throw new UpdaterException(t('Unable to parse info file: %info_file.', ['%info_file' => $info_file]));
148 * Gets the name of the project directory (basename).
150 * @todo It would be nice, if projects contained an info file which could
151 * provide their canonical name.
153 * @param string $directory
156 * The name of the project.
158 public static function getProjectName($directory) {
159 return drupal_basename($directory);
163 * Returns the project name from a Drupal info file.
165 * @param string $directory
166 * Directory to search for the info file.
169 * The title of the project.
171 * @throws \Drupal\Core\Updater\UpdaterException
173 public static function getProjectTitle($directory) {
174 $info_file = self::findInfoFile($directory);
175 $info = \Drupal::service('info_parser')->parse($info_file);
177 throw new UpdaterException(t('Unable to parse info file: %info_file.', ['%info_file' => $info_file]));
179 return $info['name'];
183 * Stores the default parameters for the Updater.
185 * @param array $overrides
186 * An array of overrides for the default parameters.
189 * An array of configuration parameters for an update or install operation.
191 protected function getInstallArgs($overrides = []) {
193 'make_backup' => FALSE,
194 'install_dir' => $this->getInstallDirectory(),
195 'backup_dir' => $this->getBackupDir(),
197 return array_merge($args, $overrides);
201 * Updates a Drupal project and returns a list of next actions.
203 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
204 * Object that is a child of FileTransfer. Used for moving files
206 * @param array $overrides
207 * An array of settings to override defaults; see self::getInstallArgs().
210 * An array of links which the user may need to complete the update
212 * @throws \Drupal\Core\Updater\UpdaterException
213 * @throws \Drupal\Core\Updater\UpdaterFileTransferException
215 public function update(&$filetransfer, $overrides = []) {
217 // Establish arguments with possible overrides.
218 $args = $this->getInstallArgs($overrides);
221 if ($args['make_backup']) {
222 $this->makeBackup($filetransfer, $args['install_dir'], $args['backup_dir']);
226 // This is bad, don't want to delete the install directory.
227 throw new UpdaterException(t('Fatal error in update, cowardly refusing to wipe out the install directory.'));
230 // Make sure the installation parent directory exists and is writable.
231 $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
233 if (is_dir($args['install_dir'] . '/' . $this->name)) {
234 // Remove the existing installed file.
235 $filetransfer->removeDirectory($args['install_dir'] . '/' . $this->name);
238 // Copy the directory in place.
239 $filetransfer->copyDirectory($this->source, $args['install_dir']);
241 // Make sure what we just installed is readable by the web server.
242 $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
245 // @todo Decide if we want to implement this.
248 // For now, just return a list of links of things to do.
249 return $this->postUpdateTasks();
251 catch (FileTransferException $e) {
252 throw new UpdaterFileTransferException(t('File Transfer failed, reason: @reason', ['@reason' => strtr($e->getMessage(), $e->arguments)]));
257 * Installs a Drupal project, returns a list of next actions.
259 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
260 * Object that is a child of FileTransfer.
261 * @param array $overrides
262 * An array of settings to override defaults; see self::getInstallArgs().
265 * An array of links which the user may need to complete the install.
267 * @throws \Drupal\Core\Updater\UpdaterFileTransferException
269 public function install(&$filetransfer, $overrides = []) {
271 // Establish arguments with possible overrides.
272 $args = $this->getInstallArgs($overrides);
274 // Make sure the installation parent directory exists and is writable.
275 $this->prepareInstallDirectory($filetransfer, $args['install_dir']);
277 // Copy the directory in place.
278 $filetransfer->copyDirectory($this->source, $args['install_dir']);
280 // Make sure what we just installed is readable by the web server.
281 $this->makeWorldReadable($filetransfer, $args['install_dir'] . '/' . $this->name);
283 // Potentially enable something?
284 // @todo Decide if we want to implement this.
285 $this->postInstall();
286 // For now, just return a list of links of things to do.
287 return $this->postInstallTasks();
289 catch (FileTransferException $e) {
290 throw new UpdaterFileTransferException(t('File Transfer failed, reason: @reason', ['@reason' => strtr($e->getMessage(), $e->arguments)]));
295 * Makes sure the installation parent directory exists and is writable.
297 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
298 * Object which is a child of FileTransfer.
299 * @param string $directory
300 * The installation directory to prepare.
302 * @throws \Drupal\Core\Updater\UpdaterException
304 public function prepareInstallDirectory(&$filetransfer, $directory) {
305 // Make the parent dir writable if need be and create the dir.
306 if (!is_dir($directory)) {
307 $parent_dir = dirname($directory);
308 if (!is_writable($parent_dir)) {
309 @chmod($parent_dir, 0755);
310 // It is expected that this will fail if the directory is owned by the
311 // FTP user. If the FTP user == web server, it will succeed.
313 $filetransfer->createDirectory($directory);
314 $this->makeWorldReadable($filetransfer, $directory);
316 catch (FileTransferException $e) {
317 // Probably still not writable. Try to chmod and do it again.
318 // @todo Make a new exception class so we can catch it differently.
320 $old_perms = substr(sprintf('%o', fileperms($parent_dir)), -4);
321 $filetransfer->chmod($parent_dir, 0755);
322 $filetransfer->createDirectory($directory);
323 $this->makeWorldReadable($filetransfer, $directory);
324 // Put the permissions back.
325 $filetransfer->chmod($parent_dir, intval($old_perms, 8));
327 catch (FileTransferException $e) {
328 $message = t($e->getMessage(), $e->arguments);
329 $throw_message = t('Unable to create %directory due to the following: %reason', ['%directory' => $directory, '%reason' => $message]);
330 throw new UpdaterException($throw_message);
333 // Put the parent directory back.
334 @chmod($parent_dir, 0555);
340 * Ensures that a given directory is world readable.
342 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
343 * Object which is a child of FileTransfer.
344 * @param string $path
345 * The file path to make world readable.
346 * @param bool $recursive
347 * If the chmod should be applied recursively.
349 public function makeWorldReadable(&$filetransfer, $path, $recursive = TRUE) {
350 if (!is_executable($path)) {
351 // Set it to read + execute.
352 $new_perms = substr(sprintf('%o', fileperms($path)), -4, -1) . "5";
353 $filetransfer->chmod($path, intval($new_perms, 8), $recursive);
360 * @param \Drupal\Core\FileTransfer\FileTransfer $filetransfer
361 * Object which is a child of FileTransfer.
362 * @param string $from
363 * The file path to copy from.
365 * The file path to copy to.
367 * @todo Not implemented: https://www.drupal.org/node/2474355
369 public function makeBackup(FileTransfer $filetransfer, $from, $to) {
373 * Returns the full path to a directory where backups should be written.
375 public function getBackupDir() {
376 return \Drupal::service('stream_wrapper_manager')->getViaScheme('temporary')->getDirectoryPath();
380 * Performs actions after new code is updated.
382 public function postUpdate() {
386 * Performs actions after installation.
388 public function postInstall() {
392 * Returns an array of links to pages that should be visited post operation.
395 * Links which provide actions to take after the install is finished.
397 public function postInstallTasks() {
402 * Returns an array of links to pages that should be visited post operation.
405 * Links which provide actions to take after the update is finished.
407 public function postUpdateTasks() {