-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmtools.drush.inc
117 lines (103 loc) · 3.63 KB
/
mmtools.drush.inc
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
<?php
/**
* @file mmm_revert.drush.inc
*/
use clever_systems\mmm_runtime\Runtime;
/**
* Implements hook_drush_command().
*/
function mmtools_drush_command() {
$items = array();
$items['mmtools-db-caches-clean'] = array(
'aliases' => ['dbcc'],
'description' => 'Clean all unused database caches.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
$items['mmtools-site-install'] = array(
'aliases' => ['mmsi'],
'description' => 'Install site with autodetected defaults.',
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
'arguments' => [
'name' => 'The site name (also lowercased DB prefix), defaults to Dev.'
],
);
return $items;
}
function drush_mmtools_db_caches_clean() {
// @todo Suit to drupal 8.
// Flush unused cache tables.
/** @see drupal_flush_all_caches */
/** @see cache_clear_all */
$core = array('cache', 'cache_path', 'cache_filter', 'cache_bootstrap', 'cache_page');
$cache_tables = array_merge(module_invoke_all('flush_caches'), $core);
foreach ($cache_tables as $table) {
$cache = _cache_get_object($table);
drush_log(dt('Cache class: !table', ['!table' => get_class($cache)]));
if (!$cache instanceof \DrupalDatabaseCache) {
//$cache->clear('*', TRUE);
try {
db_query('truncate table ' . db_escape_table($table));
drush_log(dt('Cleaned cache table !table', ['!table' => $table]), 'ok');
} catch (\PDOException $e) {
// There are caches like ctools_css that don't have a table.
drush_log(dt('Ignored nonexisting cache table !table', ['!table' => $table]));
}
}
else {
drush_log(dt('Ignored DB cache table !table', ['!table' => $table]));
}
}
}
/**
* Implements drush_hook_COMMAND for site-install.
*/
function drush_mmtools_site_install($name = 'Dev') {
if (defined('DRUPAL_ROOT')) {
@include DRUPAL_ROOT . '/../vendor/autoload.php';
}
// We require Runtime here to use its feature to recognize server types.
// We don't require it here to avoid issues with double function definitions.
// @todo Consider splitting recognizing server type and generating settings.
if (!class_exists(Runtime::class)) {
return drush_set_error('mmtools_site_install:needs_runtime', dt('Please install mmm_runtime in your installation.'));
}
$environment = Runtime::getEnvironment();
// We booted to drupal_root, so no settings available. Make fake ones.
$settings = [];
$databases = [];
$db_record =& $databases['default']['default'];
if (!is_array($db_record)) {
$db_record = [];
}
$machine_name = preg_replace('#[^a-z0-9]#u', '_', strtolower($name));
$db_record += [
'database' => $environment->getUser() . '_' . $machine_name,
];
$environment->settings($settings, $databases);
if (
!empty($databases['default']['default']['username'])
&& !empty($databases['default']['default']['host'])
&& !empty($databases['default']['default']['password'])
) {
$username = $db_record['username'];
$host = $db_record['host'];
$password = $db_record['password'];
$db_record += [
'driver' => 'mysql',
'port' => '3306',
'prefix' => '',
];
$driver = $db_record['driver'];
$port = $db_record['port'];
$prefix = $db_record['prefix'];
$database = $db_record['database'];
$db_url = "$driver://$username:$password@$host:$port/$database";
/* @see drush_context_names() */
drush_set_default('db-url', $db_url);
drush_set_default('db-prefix', $prefix);
drush_set_default('account-name', 'gott');
drush_set_default('locale', 'de');
drush_set_default('site-name', dirname(DRUPAL_ROOT));
drush_invoke('site-install');
}
}