comparison core/modules/update/src/Form/UpdateReady.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\update\Form;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\FileTransfer\Local;
7 use Drupal\Core\Form\FormBase;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\State\StateInterface;
10 use Drupal\Core\Updater\Updater;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12 use Symfony\Component\HttpFoundation\Response;
13
14 /**
15 * Configure update settings for this site.
16 */
17 class UpdateReady extends FormBase {
18
19 /**
20 * The root location under which updated projects will be saved.
21 *
22 * @var string
23 */
24 protected $root;
25
26 /**
27 * The module handler.
28 *
29 * @var \Drupal\Core\Extension\ModuleHandlerInterface
30 */
31 protected $moduleHandler;
32
33 /**
34 * The state key value store.
35 *
36 * @var \Drupal\Core\State\StateInterface
37 */
38 protected $state;
39
40 /**
41 * The Site path.
42 *
43 * @var string
44 */
45 protected $sitePath;
46
47 /**
48 * Constructs a new UpdateReady object.
49 *
50 * @param string $root
51 * The root location under which updated projects will be saved.
52 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
53 * The object that manages enabled modules in a Drupal installation.
54 * @param \Drupal\Core\State\StateInterface $state
55 * The state key value store.
56 * @param string $site_path
57 * The site path.
58 */
59 public function __construct($root, ModuleHandlerInterface $module_handler, StateInterface $state, $site_path) {
60 $this->root = $root;
61 $this->moduleHandler = $module_handler;
62 $this->state = $state;
63 $this->sitePath = $site_path;
64 }
65
66 /**
67 * {@inheritdoc}
68 */
69 public function getFormId() {
70 return 'update_manager_update_ready_form';
71 }
72
73 /**
74 * {@inheritdoc}
75 */
76 public static function create(ContainerInterface $container) {
77 return new static(
78 $container->get('update.root'),
79 $container->get('module_handler'),
80 $container->get('state'),
81 $container->get('site.path')
82 );
83 }
84
85 /**
86 * {@inheritdoc}
87 */
88 public function buildForm(array $form, FormStateInterface $form_state) {
89 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
90 if (!_update_manager_check_backends($form, 'update')) {
91 return $form;
92 }
93
94 $form['backup'] = [
95 '#prefix' => '<strong>',
96 '#markup' => $this->t('Back up your database and site before you continue. <a href=":backup_url">Learn how</a>.', [':backup_url' => 'https://www.drupal.org/node/22281']),
97 '#suffix' => '</strong>',
98 ];
99
100 $form['maintenance_mode'] = [
101 '#title' => $this->t('Perform updates with site in maintenance mode (strongly recommended)'),
102 '#type' => 'checkbox',
103 '#default_value' => TRUE,
104 ];
105
106 $form['actions'] = ['#type' => 'actions'];
107 $form['actions']['submit'] = [
108 '#type' => 'submit',
109 '#value' => $this->t('Continue'),
110 ];
111
112 return $form;
113 }
114
115 /**
116 * {@inheritdoc}
117 */
118 public function submitForm(array &$form, FormStateInterface $form_state) {
119 // Store maintenance_mode setting so we can restore it when done.
120 $_SESSION['maintenance_mode'] = $this->state->get('system.maintenance_mode');
121 if ($form_state->getValue('maintenance_mode') == TRUE) {
122 $this->state->set('system.maintenance_mode', TRUE);
123 }
124
125 if (!empty($_SESSION['update_manager_update_projects'])) {
126 // Make sure the Updater registry is loaded.
127 drupal_get_updaters();
128
129 $updates = [];
130 $directory = _update_manager_extract_directory();
131
132 $projects = $_SESSION['update_manager_update_projects'];
133 unset($_SESSION['update_manager_update_projects']);
134
135 $project_real_location = NULL;
136 foreach ($projects as $project => $url) {
137 $project_location = $directory . '/' . $project;
138 $updater = Updater::factory($project_location, $this->root);
139 $project_real_location = drupal_realpath($project_location);
140 $updates[] = [
141 'project' => $project,
142 'updater_name' => get_class($updater),
143 'local_url' => $project_real_location,
144 ];
145 }
146
147 // If the owner of the last directory we extracted is the same as the
148 // owner of our configuration directory (e.g. sites/default) where we're
149 // trying to install the code, there's no need to prompt for FTP/SSH
150 // credentials. Instead, we instantiate a Drupal\Core\FileTransfer\Local
151 // and invoke update_authorize_run_update() directly.
152 if (fileowner($project_real_location) == fileowner($this->sitePath)) {
153 $this->moduleHandler->loadInclude('update', 'inc', 'update.authorize');
154 $filetransfer = new Local($this->root);
155 $response = update_authorize_run_update($filetransfer, $updates);
156 if ($response instanceof Response) {
157 $form_state->setResponse($response);
158 }
159 }
160 // Otherwise, go through the regular workflow to prompt for FTP/SSH
161 // credentials and invoke update_authorize_run_update() indirectly with
162 // whatever FileTransfer object authorize.php creates for us.
163 else {
164 // The page title must be passed here to ensure it is initially used
165 // when authorize.php loads for the first time with the FTP/SSH
166 // credentials form.
167 system_authorized_init('update_authorize_run_update', __DIR__ . '/../../update.authorize.inc', [$updates], $this->t('Update manager'));
168 $form_state->setRedirectUrl(system_authorized_get_url());
169 }
170 }
171 }
172
173 }