Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Updater;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Url;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * Defines a class for updating modules using
|
Chris@0
|
9 * Drupal\Core\FileTransfer\FileTransfer classes via authorize.php.
|
Chris@0
|
10 */
|
Chris@0
|
11 class Module extends Updater implements UpdaterInterface {
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * Returns the directory where a module should be installed.
|
Chris@0
|
15 *
|
Chris@0
|
16 * If the module is already installed, drupal_get_path() will return a valid
|
Chris@0
|
17 * path and we should install it there. If we're installing a new module, we
|
Chris@0
|
18 * always want it to go into /modules, since that's where all the
|
Chris@0
|
19 * documentation recommends users install their modules, and there's no way
|
Chris@0
|
20 * that can conflict on a multi-site installation, since the Update manager
|
Chris@0
|
21 * won't let you install a new module if it's already found on your system,
|
Chris@0
|
22 * and if there was a copy in the top-level we'd see it.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @return string
|
Chris@0
|
25 * The absolute path of the directory.
|
Chris@0
|
26 */
|
Chris@0
|
27 public function getInstallDirectory() {
|
Chris@0
|
28 if ($this->isInstalled() && ($relative_path = drupal_get_path('module', $this->name))) {
|
Chris@0
|
29 // The return value of drupal_get_path() is always relative to the site,
|
Chris@0
|
30 // so prepend DRUPAL_ROOT.
|
Chris@0
|
31 return DRUPAL_ROOT . '/' . dirname($relative_path);
|
Chris@0
|
32 }
|
Chris@0
|
33 else {
|
Chris@0
|
34 // When installing a new module, prepend the requested root directory.
|
Chris@0
|
35 return $this->root . '/' . $this->getRootDirectoryRelativePath();
|
Chris@0
|
36 }
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * {@inheritdoc}
|
Chris@0
|
41 */
|
Chris@0
|
42 public static function getRootDirectoryRelativePath() {
|
Chris@0
|
43 return 'modules';
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * {@inheritdoc}
|
Chris@0
|
48 */
|
Chris@0
|
49 public function isInstalled() {
|
Chris@0
|
50 // Check if the module exists in the file system, regardless of whether it
|
Chris@0
|
51 // is enabled or not.
|
Chris@0
|
52 $modules = \Drupal::state()->get('system.module.files', []);
|
Chris@0
|
53 return isset($modules[$this->name]);
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * {@inheritdoc}
|
Chris@0
|
58 */
|
Chris@0
|
59 public static function canUpdateDirectory($directory) {
|
Chris@0
|
60 $info = static::getExtensionInfo($directory);
|
Chris@0
|
61
|
Chris@0
|
62 return (isset($info['type']) && $info['type'] == 'module');
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Determines whether this class can update the specified project.
|
Chris@0
|
67 *
|
Chris@0
|
68 * @param string $project_name
|
Chris@0
|
69 * The project to check.
|
Chris@0
|
70 *
|
Chris@0
|
71 * @return bool
|
Chris@0
|
72 */
|
Chris@0
|
73 public static function canUpdate($project_name) {
|
Chris@0
|
74 return (bool) drupal_get_path('module', $project_name);
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Returns available database schema updates once a new version is installed.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @return array
|
Chris@0
|
81 */
|
Chris@0
|
82 public function getSchemaUpdates() {
|
Chris@0
|
83 require_once DRUPAL_ROOT . '/core/includes/install.inc';
|
Chris@0
|
84 require_once DRUPAL_ROOT . '/core/includes/update.inc';
|
Chris@0
|
85
|
Chris@0
|
86 if (!self::canUpdate($this->name)) {
|
Chris@0
|
87 return [];
|
Chris@0
|
88 }
|
Chris@0
|
89 module_load_include('install', $this->name);
|
Chris@0
|
90
|
Chris@0
|
91 if (!$updates = drupal_get_schema_versions($this->name)) {
|
Chris@0
|
92 return [];
|
Chris@0
|
93 }
|
Chris@0
|
94 $modules_with_updates = update_get_update_list();
|
Chris@0
|
95 if ($updates = $modules_with_updates[$this->name]) {
|
Chris@0
|
96 if ($updates['start']) {
|
Chris@0
|
97 return $updates['pending'];
|
Chris@0
|
98 }
|
Chris@0
|
99 }
|
Chris@0
|
100 return [];
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * {@inheritdoc}
|
Chris@0
|
105 */
|
Chris@0
|
106 public function postInstallTasks() {
|
Chris@0
|
107 // Since this is being called outsite of the primary front controller,
|
Chris@0
|
108 // the base_url needs to be set explicitly to ensure that links are
|
Chris@0
|
109 // relative to the site root.
|
Chris@0
|
110 // @todo Simplify with https://www.drupal.org/node/2548095
|
Chris@0
|
111 $default_options = [
|
Chris@0
|
112 '#type' => 'link',
|
Chris@0
|
113 '#options' => [
|
Chris@0
|
114 'absolute' => TRUE,
|
Chris@0
|
115 'base_url' => $GLOBALS['base_url'],
|
Chris@0
|
116 ],
|
Chris@0
|
117 ];
|
Chris@0
|
118 return [
|
Chris@0
|
119 $default_options + [
|
Chris@0
|
120 '#url' => Url::fromRoute('update.module_install'),
|
Chris@0
|
121 '#title' => t('Install another module'),
|
Chris@0
|
122 ],
|
Chris@0
|
123 $default_options + [
|
Chris@0
|
124 '#url' => Url::fromRoute('system.modules_list'),
|
Chris@0
|
125 '#title' => t('Enable newly added modules'),
|
Chris@0
|
126 ],
|
Chris@0
|
127 $default_options + [
|
Chris@0
|
128 '#url' => Url::fromRoute('system.admin'),
|
Chris@0
|
129 '#title' => t('Administration pages'),
|
Chris@0
|
130 ],
|
Chris@0
|
131 ];
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * {@inheritdoc}
|
Chris@0
|
136 */
|
Chris@0
|
137 public function postUpdateTasks() {
|
Chris@0
|
138 // We don't want to check for DB updates here, we do that once for all
|
Chris@0
|
139 // updated modules on the landing page.
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 }
|