PageRenderTime 32ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/lib/Drupal/Core/Updater/Updater.php

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