1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Callbacks and related functions invoked by authorize.php to update projects.
|
6
|
*
|
7
|
* We use the Batch API to actually update each individual project on the site.
|
8
|
* All of the code in this file is run at a low bootstrap level (modules are not
|
9
|
* loaded), so these functions cannot assume access to the rest of the code of
|
10
|
* the Update Manager module.
|
11
|
*/
|
12
|
|
13
|
/**
|
14
|
* Updates existing projects when invoked by authorize.php.
|
15
|
*
|
16
|
* Callback for system_authorized_init() in
|
17
|
* update_manager_update_ready_form_submit().
|
18
|
*
|
19
|
* @param $filetransfer
|
20
|
* The FileTransfer object created by authorize.php for use during this
|
21
|
* operation.
|
22
|
* @param $projects
|
23
|
* A nested array of projects to install into the live webroot, keyed by
|
24
|
* project name. Each subarray contains the following keys:
|
25
|
* - project: The canonical project short name.
|
26
|
* - updater_name: The name of the Updater class to use for this project.
|
27
|
* - local_url: The locally installed location of new code to update with.
|
28
|
*/
|
29
|
function update_authorize_run_update($filetransfer, $projects) {
|
30
|
$operations = array();
|
31
|
foreach ($projects as $project => $project_info) {
|
32
|
$operations[] = array(
|
33
|
'update_authorize_batch_copy_project',
|
34
|
array(
|
35
|
$project_info['project'],
|
36
|
$project_info['updater_name'],
|
37
|
$project_info['local_url'],
|
38
|
$filetransfer,
|
39
|
),
|
40
|
);
|
41
|
}
|
42
|
|
43
|
$batch = array(
|
44
|
'title' => t('Installing updates'),
|
45
|
'init_message' => t('Preparing to update your site'),
|
46
|
'operations' => $operations,
|
47
|
'finished' => 'update_authorize_update_batch_finished',
|
48
|
'file' => drupal_get_path('module', 'update') . '/update.authorize.inc',
|
49
|
);
|
50
|
|
51
|
batch_set($batch);
|
52
|
// Invoke the batch via authorize.php.
|
53
|
system_authorized_batch_process();
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* Installs a new project when invoked by authorize.php.
|
58
|
*
|
59
|
* Callback for system_authorized_init() in
|
60
|
* update_manager_install_form_submit().
|
61
|
*
|
62
|
* @param FileTransfer $filetransfer
|
63
|
* The FileTransfer object created by authorize.php for use during this
|
64
|
* operation.
|
65
|
* @param string $project
|
66
|
* The canonical project short name (e.g., {system}.name).
|
67
|
* @param string $updater_name
|
68
|
* The name of the Updater class to use for installing this project.
|
69
|
* @param string $local_url
|
70
|
* The URL to the locally installed temp directory where the project has
|
71
|
* already been downloaded and extracted into.
|
72
|
*/
|
73
|
function update_authorize_run_install($filetransfer, $project, $updater_name, $local_url) {
|
74
|
$operations[] = array(
|
75
|
'update_authorize_batch_copy_project',
|
76
|
array(
|
77
|
$project,
|
78
|
$updater_name,
|
79
|
$local_url,
|
80
|
$filetransfer,
|
81
|
),
|
82
|
);
|
83
|
|
84
|
// @todo Instantiate our Updater to set the human-readable title?
|
85
|
$batch = array(
|
86
|
'title' => t('Installing %project', array('%project' => $project)),
|
87
|
'init_message' => t('Preparing to install'),
|
88
|
'operations' => $operations,
|
89
|
// @todo Use a different finished callback for different messages?
|
90
|
'finished' => 'update_authorize_install_batch_finished',
|
91
|
'file' => drupal_get_path('module', 'update') . '/update.authorize.inc',
|
92
|
);
|
93
|
batch_set($batch);
|
94
|
|
95
|
// Invoke the batch via authorize.php.
|
96
|
system_authorized_batch_process();
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Implements callback_batch_operation().
|
101
|
*
|
102
|
* Copies project to its proper place when authorized to do so.
|
103
|
*
|
104
|
* @param string $project
|
105
|
* The canonical short name of the project being installed.
|
106
|
* @param string $updater_name
|
107
|
* The name of the Updater class to use for installing this project.
|
108
|
* @param string $local_url
|
109
|
* The URL to the locally installed temp directory where the project has
|
110
|
* already been downloaded and extracted into.
|
111
|
* @param FileTransfer $filetransfer
|
112
|
* The FileTransfer object to use for performing this operation.
|
113
|
* @param array $context
|
114
|
* Reference to an array used for Batch API storage.
|
115
|
*/
|
116
|
function update_authorize_batch_copy_project($project, $updater_name, $local_url, $filetransfer, &$context) {
|
117
|
|
118
|
// Initialize some variables in the Batch API $context array.
|
119
|
if (!isset($context['results']['log'])) {
|
120
|
$context['results']['log'] = array();
|
121
|
}
|
122
|
if (!isset($context['results']['log'][$project])) {
|
123
|
$context['results']['log'][$project] = array();
|
124
|
}
|
125
|
|
126
|
if (!isset($context['results']['tasks'])) {
|
127
|
$context['results']['tasks'] = array();
|
128
|
}
|
129
|
|
130
|
// The batch API uses a session, and since all the arguments are serialized
|
131
|
// and unserialized between requests, although the FileTransfer object itself
|
132
|
// will be reconstructed, the connection pointer itself will be lost. However,
|
133
|
// the FileTransfer object will still have the connection variable, even
|
134
|
// though the connection itself is now gone. So, although it's ugly, we have
|
135
|
// to unset the connection variable at this point so that the FileTransfer
|
136
|
// object will re-initiate the actual connection.
|
137
|
unset($filetransfer->connection);
|
138
|
|
139
|
if (!empty($context['results']['log'][$project]['#abort'])) {
|
140
|
$context['finished'] = 1;
|
141
|
return;
|
142
|
}
|
143
|
|
144
|
$updater = new $updater_name($local_url);
|
145
|
|
146
|
try {
|
147
|
if ($updater->isInstalled()) {
|
148
|
// This is an update.
|
149
|
$tasks = $updater->update($filetransfer);
|
150
|
}
|
151
|
else {
|
152
|
$tasks = $updater->install($filetransfer);
|
153
|
}
|
154
|
}
|
155
|
catch (UpdaterException $e) {
|
156
|
_update_batch_create_message($context['results']['log'][$project], t('Error installing / updating'), FALSE);
|
157
|
_update_batch_create_message($context['results']['log'][$project], $e->getMessage(), FALSE);
|
158
|
$context['results']['log'][$project]['#abort'] = TRUE;
|
159
|
return;
|
160
|
}
|
161
|
|
162
|
_update_batch_create_message($context['results']['log'][$project], t('Installed %project_name successfully', array('%project_name' => $project)));
|
163
|
if (!empty($tasks)) {
|
164
|
$context['results']['tasks'] += $tasks;
|
165
|
}
|
166
|
|
167
|
// This particular operation is now complete, even though the batch might
|
168
|
// have other operations to perform.
|
169
|
$context['finished'] = 1;
|
170
|
}
|
171
|
|
172
|
/**
|
173
|
* Implements callback_batch_finished().
|
174
|
*
|
175
|
* Performs actions when the authorized update batch is done.
|
176
|
*
|
177
|
* This processes the results and stashes them into SESSION such that
|
178
|
* authorize.php will render a report. Also responsible for putting the site
|
179
|
* back online and clearing the update status cache after a successful update.
|
180
|
*
|
181
|
* @param $success
|
182
|
* TRUE if the batch operation was successful; FALSE if there were errors.
|
183
|
* @param $results
|
184
|
* An associative array of results from the batch operation.
|
185
|
*/
|
186
|
function update_authorize_update_batch_finished($success, $results) {
|
187
|
foreach ($results['log'] as $project => $messages) {
|
188
|
if (!empty($messages['#abort'])) {
|
189
|
$success = FALSE;
|
190
|
}
|
191
|
}
|
192
|
$offline = variable_get('maintenance_mode', FALSE);
|
193
|
if ($success) {
|
194
|
// Now that the update completed, we need to clear the cache of available
|
195
|
// update data and recompute our status, so prevent show bogus results.
|
196
|
_update_authorize_clear_update_status();
|
197
|
|
198
|
// Take the site out of maintenance mode if it was previously that way.
|
199
|
if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
|
200
|
variable_set('maintenance_mode', FALSE);
|
201
|
$page_message = array(
|
202
|
'message' => t('Update was completed successfully. Your site has been taken out of maintenance mode.'),
|
203
|
'type' => 'status',
|
204
|
);
|
205
|
}
|
206
|
else {
|
207
|
$page_message = array(
|
208
|
'message' => t('Update was completed successfully.'),
|
209
|
'type' => 'status',
|
210
|
);
|
211
|
}
|
212
|
}
|
213
|
elseif (!$offline) {
|
214
|
$page_message = array(
|
215
|
'message' => t('Update failed! See the log below for more information.'),
|
216
|
'type' => 'error',
|
217
|
);
|
218
|
}
|
219
|
else {
|
220
|
$page_message = array(
|
221
|
'message' => t('Update failed! See the log below for more information. Your site is still in maintenance mode.'),
|
222
|
'type' => 'error',
|
223
|
);
|
224
|
}
|
225
|
// Since we're doing an update of existing code, always add a task for
|
226
|
// running update.php.
|
227
|
$results['tasks'][] = t('Your modules have been downloaded and updated.');
|
228
|
$results['tasks'][] = t('<a href="@update">Run database updates</a>', array('@update' => base_path() . 'update.php'));
|
229
|
|
230
|
// Unset the variable since it is no longer needed.
|
231
|
unset($_SESSION['maintenance_mode']);
|
232
|
|
233
|
// Set all these values into the SESSION so authorize.php can display them.
|
234
|
$_SESSION['authorize_results']['success'] = $success;
|
235
|
$_SESSION['authorize_results']['page_message'] = $page_message;
|
236
|
$_SESSION['authorize_results']['messages'] = $results['log'];
|
237
|
$_SESSION['authorize_results']['tasks'] = $results['tasks'];
|
238
|
$_SESSION['authorize_operation']['page_title'] = t('Update manager');
|
239
|
}
|
240
|
|
241
|
/**
|
242
|
* Implements callback_batch_finished().
|
243
|
*
|
244
|
* Performs actions when the authorized install batch is done.
|
245
|
*
|
246
|
* This processes the results and stashes them into SESSION such that
|
247
|
* authorize.php will render a report. Also responsible for putting the site
|
248
|
* back online after a successful install if necessary.
|
249
|
*
|
250
|
* @param $success
|
251
|
* TRUE if the batch operation was a success; FALSE if there were errors.
|
252
|
* @param $results
|
253
|
* An associative array of results from the batch operation.
|
254
|
*/
|
255
|
function update_authorize_install_batch_finished($success, $results) {
|
256
|
foreach ($results['log'] as $project => $messages) {
|
257
|
if (!empty($messages['#abort'])) {
|
258
|
$success = FALSE;
|
259
|
}
|
260
|
}
|
261
|
$offline = variable_get('maintenance_mode', FALSE);
|
262
|
if ($success) {
|
263
|
// Take the site out of maintenance mode if it was previously that way.
|
264
|
if ($offline && isset($_SESSION['maintenance_mode']) && $_SESSION['maintenance_mode'] == FALSE) {
|
265
|
variable_set('maintenance_mode', FALSE);
|
266
|
$page_message = array(
|
267
|
'message' => t('Installation was completed successfully. Your site has been taken out of maintenance mode.'),
|
268
|
'type' => 'status',
|
269
|
);
|
270
|
}
|
271
|
else {
|
272
|
$page_message = array(
|
273
|
'message' => t('Installation was completed successfully.'),
|
274
|
'type' => 'status',
|
275
|
);
|
276
|
}
|
277
|
}
|
278
|
elseif (!$success && !$offline) {
|
279
|
$page_message = array(
|
280
|
'message' => t('Installation failed! See the log below for more information.'),
|
281
|
'type' => 'error',
|
282
|
);
|
283
|
}
|
284
|
else {
|
285
|
$page_message = array(
|
286
|
'message' => t('Installation failed! See the log below for more information. Your site is still in maintenance mode.'),
|
287
|
'type' => 'error',
|
288
|
);
|
289
|
}
|
290
|
|
291
|
// Unset the variable since it is no longer needed.
|
292
|
unset($_SESSION['maintenance_mode']);
|
293
|
|
294
|
// Set all these values into the SESSION so authorize.php can display them.
|
295
|
$_SESSION['authorize_results']['success'] = $success;
|
296
|
$_SESSION['authorize_results']['page_message'] = $page_message;
|
297
|
$_SESSION['authorize_results']['messages'] = $results['log'];
|
298
|
$_SESSION['authorize_results']['tasks'] = $results['tasks'];
|
299
|
$_SESSION['authorize_operation']['page_title'] = t('Update manager');
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* Creates a structure of log messages.
|
304
|
*
|
305
|
* @param array $project_results
|
306
|
* An associative array of results from the batch operation.
|
307
|
* @param string $message
|
308
|
* A string containing a log message.
|
309
|
* @param bool $success
|
310
|
* (optional) TRUE if the operation the message is about was a success, FALSE
|
311
|
* if there were errors. Defaults to TRUE.
|
312
|
*/
|
313
|
function _update_batch_create_message(&$project_results, $message, $success = TRUE) {
|
314
|
$project_results[] = array('message' => $message, 'success' => $success);
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* Clears cached available update status data.
|
319
|
*
|
320
|
* Since this function is run at such a low bootstrap level, the Update Manager
|
321
|
* module is not loaded. So, we can't just call _update_cache_clear(). However,
|
322
|
* the database is bootstrapped, so we can do a query ourselves to clear out
|
323
|
* what we want to clear.
|
324
|
*
|
325
|
* Note that we do not want to just truncate the table, since that would remove
|
326
|
* items related to currently pending fetch attempts.
|
327
|
*
|
328
|
* @see update_authorize_update_batch_finished()
|
329
|
* @see _update_cache_clear()
|
330
|
*/
|
331
|
function _update_authorize_clear_update_status() {
|
332
|
$query = db_delete('cache_update');
|
333
|
$query->condition(
|
334
|
db_or()
|
335
|
->condition('cid', 'update_project_%', 'LIKE')
|
336
|
->condition('cid', 'available_releases::%', 'LIKE')
|
337
|
);
|
338
|
$query->execute();
|
339
|
}
|