-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNewSiteResolver.php
More file actions
205 lines (175 loc) · 7.03 KB
/
Copy pathNewSiteResolver.php
File metadata and controls
205 lines (175 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace WordPress\Blueprints\SiteResolver;
use WordPress\Blueprints\DataReference\DataReference;
use WordPress\Blueprints\DataReference\File;
use WordPress\Blueprints\Exception\BlueprintExecutionException;
use WordPress\Blueprints\Progress\Tracker;
use WordPress\Blueprints\Runtime;
use WordPress\Blueprints\VersionStrings\VersionConstraint;
use WordPress\HttpClient\Client;
use WordPress\Zip\ZipFilesystem;
use function WordPress\Filesystem\copy_between_filesystems;
use function WordPress\Filesystem\wp_join_unix_paths;
class NewSiteResolver {
static public function resolve( Runtime $runtime, Tracker $progress, ?VersionConstraint $wpVersionConstraint = null, ?string $recommendedWpVersion = 'latest' ) {
$progress->split( [
'resolve_assets' => 2,
'install_wordpress' => 1,
] );
// Ensure document root directory exists (LocalFilesystem::create creates it)
$targetFs = $runtime->getTargetFilesystem();
if ( count( $targetFs->ls( '/' ) ) > 0 ) {
throw new BlueprintExecutionException( 'The target site root directory must be empty in the create-new-site mode, but it wasn\'t.' );
}
// Unzip WordPress core into document root
$wpZip = self::resolveWordPressZipUrl( $runtime->getHttpClient(), $recommendedWpVersion );
$assets = [
'wordpress' => DataReference::create( $wpZip ),
];
if ( $runtime->getConfiguration()->getDatabaseEngine() === 'sqlite' ) {
$assets['sqlite-integration'] = $runtime->getConfiguration()->getSqliteIntegrationPlugin();
}
$runtime->getDataReferenceResolver()->startEagerResolution( $assets, $progress['resolve_assets'] );
$progress['resolve_assets']->setCaption( 'Downloading WordPress' );
$resolved = $runtime->resolve( $assets['wordpress'] );
if ( ! $resolved instanceof File ) {
throw new BlueprintExecutionException( 'Provided zip reference does not resolve to a file' );
}
$zipFs = ZipFilesystem::create( $resolved->getStream() );
$path_in_zip = '/';
if ( ! $zipFs->exists( '/wp-content' ) && $zipFs->exists( '/wordpress' ) ) {
$path_in_zip = '/wordpress';
}
$progress['install_wordpress']->set( 0.2, 'Setting up WordPress files' );
copy_between_filesystems( [
'source_filesystem' => $zipFs,
'source_path' => $path_in_zip,
'target_filesystem' => $targetFs,
'target_path' => '/',
'recursive' => true,
] );
$progress['install_wordpress']->set( 0.6, 'Installing WordPress' );
// If SQLite integration zip provided, unzip into appropriate folder
if ( $runtime->getConfiguration()->getDatabaseEngine() === 'sqlite' ) {
$progress['resolve_assets']->setCaption( 'Downloading SQLite integration plugin' );
$resolved = $runtime->resolve( $assets['sqlite-integration'] );
if ( ! $resolved instanceof File ) {
throw new BlueprintExecutionException( 'Provided zip reference does not resolve to a file' );
}
$zipFs = ZipFilesystem::create( $resolved->getStream() );
$targetPath = '/wp-content/plugins/sqlite-database-integration';
$sourcePath = '/';
if ( $zipFs->exists( 'sqlite-database-integration' ) ) {
$sourcePath = '/sqlite-database-integration';
}
copy_between_filesystems( [
'source_filesystem' => $zipFs,
'source_path' => $sourcePath,
'target_filesystem' => $targetFs,
'target_path' => $targetPath,
'recursive' => true,
] );
$targetFs->copy(
wp_join_unix_paths( $targetPath, 'db.copy' ),
'/wp-content/db.php'
);
}
// 3. Install WordPress if not installed yet.
// Technically, this is a "new site" resolver, but it's entirely possible
// the developer-provided WordPress zip already has a sqlite database with the
// a WordPress site installed..
$installCheck = $runtime->evalPhpCodeInSubProcess(
<<<'PHP'
<?php
$wp_load = getenv('DOCROOT') . '/wp-load.php';
if (!file_exists($wp_load)) {
append_output('0');
exit;
}
require $wp_load;
append_output( function_exists('is_blog_installed') && is_blog_installed() ? '1' : '0' );
PHP
)->outputFileContent;
if ( trim( $installCheck ) !== '1' ) {
if ( ! $targetFs->exists( '/wp-config.php' ) ) {
if ( $targetFs->exists( 'wp-config-sample.php' ) ) {
$targetFs->copy( 'wp-config-sample.php', 'wp-config.php' );
} else {
throw new BlueprintExecutionException( 'Neither wp-config.php, nor wp-config-sample.php was found in the WordPress archive.' );
}
}
// Perform installation using WP-CLI
// @TODO (low priority): Remove the WP-CLI dependency to lower the download size for blueprints.phar.
$progress['install_wordpress']->set( 0.7, 'Installing WordPress' );
$wp_cli_path = $runtime->getWpCliPath();
$runtime->runShellCommand( [
'php',
$wp_cli_path,
'core',
'install',
// For Docker compatibility. If we got this far, Blueprint runner was already
// allowed to run as root.
'--allow-root',
'--url=' . $runtime->getConfiguration()->getTargetSiteUrl(),
'--title=WordPress Site',
'--admin_user=admin',
'--admin_password=password',
'--admin_email=admin@example.com',
'--skip-email',
] );
}
$progress->finish();
}
static private function resolveWordPressZipUrl( Client $client, string $version_string ): string {
if ( $version_string === 'latest' ) {
return 'https://wordpress.org/latest.zip';
}
if (
strncmp( $version_string, 'https://', strlen( 'https://' ) ) === 0 ||
strncmp( $version_string, 'http://', strlen( 'http://' ) ) === 0
) {
return $version_string;
}
if ( $version_string === 'nightly' ) {
return 'https://wordpress.org/nightly-builds/wordpress-latest.zip';
}
$latestVersions = $client->fetch( 'https://api.wordpress.org/core/version-check/1.7/?channel=beta' )->json();
$latestVersions = array_filter( $latestVersions['offers'], function ( $v ) {
return $v['response'] === 'autoupdate';
} );
foreach ( $latestVersions as $apiVersion ) {
if ( $version_string === 'beta' && strpos( $apiVersion['version'], 'beta' ) !== false ) {
return $apiVersion['download'];
} elseif (
$version_string === 'latest' &&
strpos( $apiVersion['version'], 'beta' ) === false
) {
// The first non-beta item in the list is the latest version.
return $apiVersion['download'];
} elseif (
substr( $apiVersion['version'], 0, strlen( $version_string ) ) ===
$version_string
) {
return $apiVersion['download'];
} elseif (
preg_match( '/^\d+\.\d+$/', $version_string ) &&
$version_string === $apiVersion['partial_version']
) {
// When the Blueprint provides a version like 6.6, we must match on the partial
// version, e.g. "6.6"
return $apiVersion['download'];
}
}
/**
* If we didn't get a useful match in the API response, it could be version that's not
* the latest in its channel. Let's assume that if the versioning scheme seems to fit
* that hypothesis.
*/
if(preg_match('/^\d+\.\d+\.\d+$/', $version_string)) {
return 'https://downloads.wordpress.org/release/wordpress-' . $version_string . '.zip';
}
throw new BlueprintExecutionException(
sprintf( 'Invalid WordPress version constraint: %s', $version_string )
);
}
}