-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmate_devel.drush.inc
executable file
·296 lines (240 loc) · 8.24 KB
/
mate_devel.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
function mate_devel_drush_command() {
$items['mate-reset-pass'] = array(
'callback' => 'mate_devel_reset_pass',
'description' => 'Reset all the users\' passwords to 123 (by default), Also enabled any disabled account',
'arguments' => array(
'pass' => 'Optionally specify a non-default password.',
),
'aliases' => array('nrp'),
);
$items['mate-get-files'] = array(
'callback' => 'mate_devel_get_remote_files',
'description' => 'Download files from other server',
'arguments' => array(
'server' => 'The production site url, by example http://server.nyx.com/',
),
'options' => array(
'proxy_url' => 'The proxy url, by example http://myproxy.corp.com:3128',
'proxy_auth' => 'The credentials in the format [username]:[password]',
'proxy_method' => 'NTLM or BASIC, NTLM by default',
'images' => 'yes if you only need download only images, all for all files. By default all',
),
'aliases' => array('grf'),
);
$items['mate-backpub-db'] = array(
'callback' => 'mate_devel_backpub_db',
'description' => 'Download DB from other server',
'arguments' => array(
'server' => 'The production site url, by example http://server.nyx.com/',
'key' => 'The production site key to get access and download',
),
'options' => array(
'proxy_url' => 'The proxy url, by example http://myproxy.corp.com:3128',
'proxy_auth' => 'The credentials in the format [username]:[password]',
'proxy_method' => 'NTLM or BASIC, NTLM by default',
'images' => 'yes if you only need download only images, all for all files. By default all',
),
'aliases' => array('bckpub'),
);
$items['mate-delete-content'] = array(
'callback' => 'mate_devel_delete_content',
'description' => 'Deletes all nodes of the specified type, or all if no type is specified',
'arguments' => array(
'type' => 'Specify the node type to delete.',
),
'aliases' => array('dc'),
);
$items['mate-delete-set-version'] = array(
'callback' => 'mate_devel_set_version',
'description' => 'Sets the schema version to a module',
'arguments' => array(
'module' => 'The module name',
'version' => 'The version name',
),
'aliases' => array('ssv'),
);
$items['mate-sass'] = array(
'callback' => 'mate_compass',
'description' => 'Run Compass Commands in the active Theme',
'arguments' => array('command' => 'Compass action to Run'),
'aliases' => array('compass'),
);
$items['mate-clean-flood'] = array(
'callback' => 'mate_devel_clean_flood',
'description' => 'Clean the Flood Table',
'aliases' => array('ncf'),
);
return $items;
}
function mate_devel_delete_content($type = NULL) {
// Select the nodes that we want to delete.
$result = db_select('node', 'n')
->fields('n', array('nid'));
if (NULL !== $type) {
$result->condition('type', $type, '=');
}
$deleted_count = 0;
foreach ($result->execute() as $record) {
node_delete($record->nid);
$deleted_count++;
}
// Simple debug message so we can see what had been deleted.
drush_print("$deleted_count nodes have been deleted");
}
function mate_devel_reset_pass($pass = 123) {
require_once 'includes/password.inc';
$hpass = user_hash_password($pass);
$ret = db_query(
"UPDATE {users} set pass = :pass, status = 1",
array(':pass' => $hpass)
);
mate_devel_clean_flood();
if ($ret) {
drush_log(dt("Password changed and accounts activated"), 'success');
}
else {
drush_log(dt("Password change unsuccessful"), 'error');
}
}
function mate_devel_get_remote_files($server) {
require_once drupal_get_path('module', 'mate_devel') . '/Requester.php';
$proxy_url = drush_get_option('proxy_url', NULL);
$auth = drush_get_option('proxy_auth', NULL);
$method = drush_get_option('proxy_method', 'NTLM');
$only_images = drush_get_option('images', 'all');
$url_parts = parse_url($server);
$domain = $url_parts['host'];
$requester = new Requester();
if ($proxy_url !== NULL) {
$requester->setOptionProxy(
array(
'url' => $proxy_url,
'auth' => $auth,
'auth_method' => $method
)
);
}
if (!$requester->ping($server)) {
drush_log("Can't reach the server : $server", 'error');
return;
}
$only_images_filter = '';
if ($only_images === 'yes') {
$only_images_filter = " AND filemime LIKE 'image/%' ";
}
$files = db_query(
"SELECT filename, uri FROM {file_managed}
WHERE uri LIKE 'public://%' $only_images_filter"
);
$files_path = conf_path() . '/files';
if (!file_exists($files_path)) { //Creates files directory if wasn't created before.
mkdir($files_path);
chmod($files_path, 0777);
}
foreach ($files as $file) {
$file_path = str_replace('public://', '', $file->uri);
$file_path_enc = dirname($file_path);
if ($file_path_enc == '.') {
$file_path_enc = '';
}
else {
$file_path_enc = '/' . $file_path_enc;
}
$file_path_enc .= '/' . rawurlencode($file->filename);
$fp = $files_path . '/' . $file_path;
$frp = "sites/$domain/files/" . $file_path_enc;
if (!file_exists($fp)) { //If the file exists avoids the download
if (!file_exists(dirname($fp))) {
mkdir(dirname($fp), 0777, TRUE);
}
$file_url = $server . '/' . $frp;
try {
$requester->setOptionResponseType(Requester::RESPONSE_RAW);
$requester->save($fp, $file_url);
chmod($fp, 0777);
if (drush_get_context('DRUSH_VERBOSE')) {
drush_log($fp);
}
} catch (Exception $e) {
if (drush_get_context('DRUSH_VERBOSE')) {
drush_log($e->getMessage(), 'error');
}
}
}
}
if (drush_get_context('DRUSH_VERBOSE')) {
drush_log('Done', 'success');
}
}
function mate_devel_backpub_db($server, $key) {
require_once drupal_get_path('module', 'mate_devel') . '/Requester.php';
global $base_url;
$url_parts = parse_url($base_url);
$domain = $url_parts['host'];
$proxy_url = drush_get_option('proxy_url', NULL);
$auth = drush_get_option('proxy_auth', NULL);
$method = drush_get_option('proxy_method', 'NTLM');
$local_db_file = '/tmp/db.sql';
$db_url = $server . '/mate_devel/backup/' . $key;
$command = _drush_sql_connect() . ' < ' . $local_db_file;
$requester = new Requester();
if ($proxy_url !== NULL) {
$requester->setOptionProxy(
array(
'url' => $proxy_url,
'auth' => $auth,
'auth_method' => $method
)
);
}
if (!$requester->ping($server)) {
drush_log("Can't reach the server : $server", 'error');
return;
}
drush_log('Getting DB', 'success');
$requester->setOptionResponseType(Requester::RESPONSE_RAW);
$requester->save($local_db_file, $db_url);
drush_log('Importing Db', 'success');
shell_exec($command);
drush_log('Reseting variables', 'success');
//Reset some values for Dev
variable_set('file_public_path', "sites/$domain/files");
variable_set('file_private_path', "sites/$domain/private");
variable_set('preprocess_css', 0);
variable_set('preprocess_js', 0);
variable_set('block_cache', 1);
variable_set('cache', 0);
drush_log('Reseting User passwords', 'success');
mate_devel_reset_pass();
drush_log('Getting files from server', 'success');
mate_devel_get_remote_files($server); //Get Files
drush_log('Done Db Import', 'success');
}
function mate_devel_set_version($module, $version) {
drupal_set_installed_schema_version($module, $version);
}
/**
* Run compass command in the active theme.
*
* Needs to setup your path to compass executable in mate_compass_path path with variable_set.
*
* @param $command : The compass command to run like compile, watch, etc.
*/
function mate_compass($command) {
$theme = variable_get('theme_default');
$compass_path = variable_get('mate_compass_path', '/usr/bin/compass');
if (!file_exists($compass_path)) {
echo "Compass executable was not found in $compass_path, set the right path in mate_compass_path drupal variable\n";
die();
}
$theme_path = drupal_get_path('theme', $theme);
passthru("cd $theme_path;$compass_path $command;");
}
/**
* Clears the flood table.
*/
function mate_devel_clean_flood() {
db_query("TRUNCATE flood");
drush_log(dt("Flood Table Cleared"), 'success');
}