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