33
33
$ creator = $ user ? $ user ->username : '' ;
34
34
$ created = time ();
35
35
36
+ /**
37
+ * Check if the user has dropped their connection and delete the wiki if so
38
+ *
39
+ * We could check for dropped connections with register_shutdown_function(), but
40
+ * that could happen in the middle of a shell command. If we tried to delete
41
+ * a wiki while a shell command was running (e.g composer update) we may still
42
+ * be left with stray files (e.g. /vendor)
43
+ *
44
+ * Instead manually check the connection at 'safe' times in between API requests
45
+ * or shell commands.
46
+ */
47
+ function check_connection () {
48
+ if ( connection_status () !== CONNECTION_NORMAL ) {
49
+ abandon ( 'User disconnected early ' );
50
+ }
51
+ }
52
+
53
+ // Don't kill the process automatcally
54
+ ignore_user_abort ( true );
55
+
36
56
// Create an entry for the wiki before we have resolved patches.
37
57
// Will be updated later.
38
58
insert_wiki_data ( $ namePath , $ creator , $ created , $ branchDesc );
@@ -153,6 +173,7 @@ function set_progress( float $pc, string $label ) {
153
173
$ o = 'CURRENT_REVISION ' ;
154
174
}
155
175
$ data = gerrit_query ( "changes/?q=change: $ query&o=LABELS&o= $ o " , true );
176
+ check_connection ();
156
177
157
178
if ( count ( $ data ) === 0 ) {
158
179
$ patch = htmlentities ( $ patch );
@@ -198,6 +219,7 @@ function set_progress( float $pc, string $label ) {
198
219
// The patch doesn't have V+2, check if the uploader is trusted
199
220
$ uploaderId = $ data [0 ]['revisions ' ][$ revision ]['uploader ' ]['_account_id ' ];
200
221
$ uploader = gerrit_query ( 'accounts/ ' . $ uploaderId , true );
222
+ check_connection ();
201
223
if ( !is_trusted_user ( $ uploader ['email ' ] ) ) {
202
224
abandon ( "Patch must be approved (Verified+2) by jenkins-bot, or uploaded by a trusted user " );
203
225
}
@@ -220,6 +242,7 @@ function set_progress( float $pc, string $label ) {
220
242
221
243
// Look at all commits in this patch's tree for cross-repo dependencies to add
222
244
$ data = gerrit_query ( "changes/ $ id/revisions/ $ revision/related " , true );
245
+ check_connection ();
223
246
// Ancestor commits only, not descendants
224
247
$ foundCurr = false ;
225
248
foreach ( $ data ['changes ' ] as $ change ) {
@@ -232,6 +255,7 @@ function set_progress( float $pc, string $label ) {
232
255
233
256
foreach ( $ relatedChanges as [ $ c , $ r ] ) {
234
257
$ data = gerrit_query ( "changes/ $ c/revisions/ $ r/commit " , true );
258
+ check_connection ();
235
259
236
260
preg_match_all ( '/^Depends-On: (.+)$/m ' , $ data ['message ' ], $ m );
237
261
foreach ( $ m [1 ] as $ changeid ) {
@@ -266,6 +290,7 @@ function set_progress( float $pc, string $label ) {
266
290
list ( $ t , $ r , $ p ) = $ matches ;
267
291
268
292
$ data = gerrit_query ( "changes/ $ r/revisions/ $ p/commit " , true );
293
+ check_connection ();
269
294
if ( $ data ) {
270
295
$ t = $ t . ': ' . $ data [ 'subject ' ];
271
296
get_linked_tasks ( $ data ['message ' ], $ linkedTasks );
@@ -332,6 +357,7 @@ function set_progress( float $pc, string $label ) {
332
357
foreach ( $ repos as $ source => $ target ) {
333
358
set_progress ( $ repoProgress , "Updating repositories ( $ n/ $ repoCount)... " );
334
359
360
+ check_connection ();
335
361
$ error = shell_echo ( __DIR__ . '/new/updaterepos.sh ' ,
336
362
$ baseEnv + [
337
363
'REPO_SOURCE ' => $ source ,
@@ -347,6 +373,7 @@ function set_progress( float $pc, string $label ) {
347
373
}
348
374
349
375
// Just creates empty folders so no need for progress update
376
+ check_connection ();
350
377
$ error = shell_echo ( __DIR__ . '/new/precheckout.sh ' , $ baseEnv );
351
378
if ( $ error ) {
352
379
abandon ( "Could not create directories for wiki " );
@@ -360,6 +387,7 @@ function set_progress( float $pc, string $label ) {
360
387
foreach ( $ repos as $ source => $ target ) {
361
388
set_progress ( $ repoProgress , "Checking out repositories ( $ n/ $ repoCount)... " );
362
389
390
+ check_connection ();
363
391
$ error = shell_echo ( __DIR__ . '/new/checkout.sh ' ,
364
392
$ baseEnv + [
365
393
'BRANCH ' => $ branch ,
@@ -377,6 +405,7 @@ function set_progress( float $pc, string $label ) {
377
405
378
406
// TODO: Make this a loop
379
407
set_progress ( 60 , 'Fetching submodules... ' );
408
+ check_connection ();
380
409
$ error = shell_echo ( __DIR__ . '/new/submodules.sh ' , $ baseEnv );
381
410
if ( $ error ) {
382
411
abandon ( "Could not fetch submodules " );
@@ -397,6 +426,7 @@ static function ( string $repo ) use ( $repos ): bool {
397
426
foreach ( $ composerInstallRepos as $ i => $ repo ) {
398
427
$ n = $ i + 1 ;
399
428
set_progress ( $ repoProgress , "Fetching dependencies ( $ n/ $ repoCount)... " );
429
+ check_connection ();
400
430
$ error = shell_echo ( __DIR__ . '/new/composerinstall.sh ' ,
401
431
$ baseEnv + [
402
432
// Variable used by composer itself, not our script
@@ -413,6 +443,7 @@ static function ( string $repo ) use ( $repos ): bool {
413
443
414
444
set_progress ( 65 , 'Installing your wiki... ' );
415
445
446
+ check_connection ();
416
447
$ error = shell_echo ( __DIR__ . '/new/install.sh ' ,
417
448
$ baseEnv + [
418
449
'WIKINAME ' => $ wikiName ,
@@ -433,6 +464,7 @@ static function ( string $repo ) use ( $repos ): bool {
433
464
foreach ( $ commands as $ i => $ command ) {
434
465
$ n = $ i + 1 ;
435
466
set_progress ( $ progress , "Fetching and applying patches ( $ n/ $ count)... " );
467
+ check_connection ();
436
468
$ error = shell_echo ( $ command [1 ], $ baseEnv + $ command [0 ] );
437
469
if ( $ error ) {
438
470
abandon ( "Could not apply patch {$ patchesApplied [$ i ]}" );
@@ -442,6 +474,7 @@ static function ( string $repo ) use ( $repos ): bool {
442
474
443
475
set_progress ( 90 , 'Setting up wiki content... ' );
444
476
477
+ check_connection ();
445
478
$ error = shell_echo ( __DIR__ . '/new/postinstall.sh ' ,
446
479
$ baseEnv + [
447
480
'MAINPAGE ' => $ mainPage ,
0 commit comments