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@17
|
52 /** @var \Drupal\Core\Extension\ExtensionList $module_extension_list */
|
Chris@17
|
53 $module_extension_list = \Drupal::service('extension.list.module');
|
Chris@17
|
54 return $module_extension_list->exists($this->name);
|
Chris@0
|
55 }
|
Chris@0
|
56
|
Chris@0
|
57 /**
|
Chris@0
|
58 * {@inheritdoc}
|
Chris@0
|
59 */
|
Chris@0
|
60 public static function canUpdateDirectory($directory) {
|
Chris@0
|
61 $info = static::getExtensionInfo($directory);
|
Chris@0
|
62
|
Chris@0
|
63 return (isset($info['type']) && $info['type'] == 'module');
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * Determines whether this class can update the specified project.
|
Chris@0
|
68 *
|
Chris@0
|
69 * @param string $project_name
|
Chris@0
|
70 * The project to check.
|
Chris@0
|
71 *
|
Chris@0
|
72 * @return bool
|
Chris@0
|
73 */
|
Chris@0
|
74 public static function canUpdate($project_name) {
|
Chris@0
|
75 return (bool) drupal_get_path('module', $project_name);
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * Returns available database schema updates once a new version is installed.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @return array
|
Chris@0
|
82 */
|
Chris@0
|
83 public function getSchemaUpdates() {
|
Chris@0
|
84 require_once DRUPAL_ROOT . '/core/includes/install.inc';
|
Chris@0
|
85 require_once DRUPAL_ROOT . '/core/includes/update.inc';
|
Chris@0
|
86
|
Chris@0
|
87 if (!self::canUpdate($this->name)) {
|
Chris@0
|
88 return [];
|
Chris@0
|
89 }
|
Chris@0
|
90 module_load_include('install', $this->name);
|
Chris@0
|
91
|
Chris@0
|
92 if (!$updates = drupal_get_schema_versions($this->name)) {
|
Chris@0
|
93 return [];
|
Chris@0
|
94 }
|
Chris@0
|
95 $modules_with_updates = update_get_update_list();
|
Chris@0
|
96 if ($updates = $modules_with_updates[$this->name]) {
|
Chris@0
|
97 if ($updates['start']) {
|
Chris@0
|
98 return $updates['pending'];
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|
Chris@0
|
101 return [];
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * {@inheritdoc}
|
Chris@0
|
106 */
|
Chris@0
|
107 public function postInstallTasks() {
|
Chris@17
|
108 // Since this is being called outside of the primary front controller,
|
Chris@0
|
109 // the base_url needs to be set explicitly to ensure that links are
|
Chris@0
|
110 // relative to the site root.
|
Chris@0
|
111 // @todo Simplify with https://www.drupal.org/node/2548095
|
Chris@0
|
112 $default_options = [
|
Chris@0
|
113 '#type' => 'link',
|
Chris@0
|
114 '#options' => [
|
Chris@0
|
115 'absolute' => TRUE,
|
Chris@0
|
116 'base_url' => $GLOBALS['base_url'],
|
Chris@0
|
117 ],
|
Chris@0
|
118 ];
|
Chris@0
|
119 return [
|
Chris@0
|
120 $default_options + [
|
Chris@0
|
121 '#url' => Url::fromRoute('update.module_install'),
|
Chris@0
|
122 '#title' => t('Install another module'),
|
Chris@0
|
123 ],
|
Chris@0
|
124 $default_options + [
|
Chris@0
|
125 '#url' => Url::fromRoute('system.modules_list'),
|
Chris@0
|
126 '#title' => t('Enable newly added modules'),
|
Chris@0
|
127 ],
|
Chris@0
|
128 $default_options + [
|
Chris@0
|
129 '#url' => Url::fromRoute('system.admin'),
|
Chris@0
|
130 '#title' => t('Administration pages'),
|
Chris@0
|
131 ],
|
Chris@0
|
132 ];
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 /**
|
Chris@0
|
136 * {@inheritdoc}
|
Chris@0
|
137 */
|
Chris@0
|
138 public function postUpdateTasks() {
|
Chris@0
|
139 // We don't want to check for DB updates here, we do that once for all
|
Chris@0
|
140 // updated modules on the landing page.
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 }
|