-
Notifications
You must be signed in to change notification settings - Fork 2
/
reinstall.php
155 lines (139 loc) · 4.79 KB
/
reinstall.php
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
<?php
/**
* @file
* Script to destroy and re-install Drupal 8.
*
* Place this script into your Drupal document root; i.e., /reinstall.php
* and request it in your browser.
*
* Optional GET query parameters:
* - delete=1: Deletes settings.php and the files directory in the site
* directory (but not the site directory itself).
* - main=1: Allows to reinstall/delete the main site. By default, only a
* [multi-]site may be reinstalled/deleted.
*
* You may pass additional query parameters like 'langcode' and 'profile', which
* will be forwarded to /install.php; e.g.:
* http://drupal8.test/reinstall.php?delete=1&langcode=en&profile=testing
*/
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Database\Database;
use Drupal\Core\DrupalKernel;
use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Site\Settings;
use Drupal\Core\Site\Site;
use Symfony\Component\HttpFoundation\Request;
const MAINTENANCE_MODE = 'install';
require_once __DIR__ . '/core/vendor/autoload.php';
require_once __DIR__ . '/core/includes/bootstrap.inc';
// Find and prime the correct site directory like the installer.
// The site directory may be empty.
if (!function_exists('conf_path')) {
Site::initInstaller(__DIR__);
}
else {
$site_path = conf_path(FALSE);
}
$settings['cache']['default'] = 'cache.backend.memory';
$conf['lock_backend'] = 'Drupal\Core\Lock\NullLockBackend';
// Prevent _drupal_bootstrap_configuration() from redirecting in case
// settings.php contains no $databases yet.
// @see drupal_installation_attempted()
$install_state['dummy'] = 'no_redirect';
if (method_exists('Drupal\Core\DrupalKernel', 'bootConfiguration')) {
$kernel = DrupalKernel::bootConfiguration(Request::createFromGlobals());
}
else {
drupal_bootstrap(DRUPAL_BOOTSTRAP_CONFIGURATION);
}
// Restore native PHP error/exception handlers.
restore_error_handler();
restore_exception_handler();
// Ensure required settings exist in case settings.php was not rewritten yet.
if (!Settings::getInstance()->get('hash_salt')) {
new Settings(array(
'hash_salt' => 'reinstall',
));
}
if (!function_exists('conf_path')) {
$site_path = Site::getPath();
}
if (empty($_GET['main']) && ($site_path === '' || $site_path === 'sites/default')) {
print "Destruction of site <code>'$site_path'</code> is not allowed. Use the <kbd>?main=1</kbd> query parameter to unlock this protection.";
exit;
}
// Purge PHP storage.
// @see drupal_rebuild()
PhpStorageFactory::get('service_container')->deleteAll();
PhpStorageFactory::get('twig')->deleteAll();
// Drop all database tables.
$info = Database::getConnectionInfo();
if (isset($info['default'])) {
if ($info['default']['driver'] != 'sqlite') {
$connection = Database::getConnection();
$tables = $connection->schema()->findTables($info['default']['prefix']['default'] . '%');
foreach ($tables as $table) {
$connection->schema()->dropTable($table);
}
}
// Delete SQLite database file, if applicable.
elseif (!empty($info['default']['database'])) {
Database::removeConnection('default');
$sqlite_db = DRUPAL_ROOT . '/' . $info['default']['database'];
if (file_exists($sqlite_db)) {
chmod($sqlite_db, 0777);
unlink($sqlite_db);
}
}
}
// Delete all (active) configuration.
// glob() requires an absolute path on Windows.
// realpath() returns FALSE if the directory does not exist.
try {
if ($dir = realpath(config_get_config_directory())) {
$files = glob($dir . '/*.' . FileStorage::getFileExtension());
foreach ($files as $file) {
unlink($file);
}
}
}
catch (\Exception $e) {
}
// Delete settings.php and the files directory in the site directory, if requested.
if (!empty($_GET['delete'])) {
if (!function_exists('conf_path')) {
$settings_file = Site::getPath('settings.php');
$files_dir = Site::getPath('files');
}
else {
$settings_file = $site_path . '/settings.php';
$files_dir = $site_path . '/files';
}
if (file_exists($settings_file)) {
chmod($settings_file, 0777);
unlink($settings_file);
}
if (is_dir($files_dir)) {
// Does not need a filter, because we want to delete all contents.
$flags = \FilesystemIterator::UNIX_PATHS;
$flags |= \FilesystemIterator::SKIP_DOTS;
$directory = new \RecursiveDirectoryIterator($files_dir, $flags);
$iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($iterator as $file) {
chmod($file->getPathname(), 0777);
if ($file->isDir()) {
rmdir($file->getPathname());
}
else {
unlink($file->getPathname());
}
}
chmod($files_dir, 0777);
rmdir($files_dir);
}
}
$url = 'install.php';
if (!empty($_GET) && $query = http_build_query(array_diff_key($_GET, array('delete' => 0, 'main' => 0)))) {
$url .= '?' . $query;
}
header('Location: ' . $url, TRUE, 302);