-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbodhy.drush.inc
348 lines (276 loc) · 9.55 KB
/
bodhy.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
function bodhy_drush_command() {
$items = array();
$items['update-node'] = array(
'description' => "Rewrites a node body",
'arguments' => array(
'nid' => 'nid of node to update',
'filename' => 'file with contents',
),
'options' => array(),
'examples' => array(
'drush update-node 500 test.txt',
),
'aliases' => array('un'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL //DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
);
$items['enable_security_email_notifications'] = array(
'description' => 'Enables email notifications to given address',
'arguments' => array('email' => 'email to receive notifications'),
'aliases' => array('eesn'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
);
$items['filter-nodes'] = array(
'description' => "filters all nodes bodies replacing strings matching a given regexp",
'arguments' => array(
'nid' => 'number nid or all',
'pattern' => 'regular expression',
'replacement' => 'string replacement',
),
'aliases' => array('fn'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
);
$items['generate-password'] = array(
'description' => "Generate user password",
'arguments' => array(
'user' => 'all or user',
'user_name' =>'user name',
),
'aliases' => array('genpass'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
);
$items['rename-user'] = array(
'description' => "rename user name",
'arguments' => array(
'userName' => 'Old user name',
'newUserName' =>'New user name',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
);
$items['rename-uid'] = array(
'description' => "rename user name",
'arguments' => array(
'userUid' => 'number uid',
'newUserName' =>'New user name',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
);
$items['search-nodes'] = array(
'description' => "search entry regexps in the body nodes",
'arguments' => array(
'pattern' => 'regular expression',
),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
);
$items['enable-caching'] = array(
'description' => "enable caching, without compression",
'aliases' => array('ec'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
);
$items['disable-caching'] = array(
'description' => "disable caching",
'aliases' => array('dc'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL
);
return $items;
}
function bodhy_drush_help() {
}
function drush_bodhy_update_node($nid, $filename) {
$node = node_load($nid);
if (empty($node)) {
echo "can't load nid=" . $nid . "\n";
exit(1);
}
$content = file_get_contents($filename);
if (empty($content)) {
echo "can't read file data\n";
exit(2);
}
$node->body = $content;
node_save($node);
}
function drush_bodhy_enable_security_email_notifications($email) {
variable_set('update_check_disabled', 0);
variable_set('update_check_frequency', 1);
variable_set('update_notification_threshold', 'security');
if (preg_match("/^[a-z0-9-.]+@[a-z0-9-.]+$/", $email)) {
variable_set("update_notify_emails", array(0 => $email));
drush_log("email notifications enabled", 'ok');
} else
drush_log("can't parse email", 'error');
}
function drush_bodhy_filter_nodes(string $nid, string $pattern, string $replacement) {
// Цикл перебора всех нод
if (drush_drupal_major_version() < 7) {
echo ("only works in Drupal 7 \n");
exit(1);
}
if ($nid=='all'){
$results = db_query('SELECT nid FROM {node}');
while ($result=drush_db_fetch_object($results)){
$node = node_load($result->nid);
if(ereg($pattern, $node->body['und']['0']['value'])) {
$str_node=("node/" . $result->nid);
$aliases = db_query('Select source, alias FROM url_alias WHERE source = :source', array(':source'=>$str_node));
$alias=drush_db_fetch_object($aliases);
echo ($str_node . " - ");
echo ($alias->alias . "\n");
$node->body['und']['0']['value'] =ereg_replace($pattern,$replacement,$node->body['und']['0']['value']);
node_save($node);
}
}
} else {
if (!preg_match("/^[0-9]+$/", $nid)) {
echo "nid must be a number \n";
exit(2);
}
$node = node_load($nid);
if (empty($node)) {
echo "can't load nid=" . $nid . "\n";
exit(3);
}
$node->body['und']['0']['value'] =ereg_replace($pattern,$replacement,$node->body['und']['0']['value']);
node_save($node);
}
}
function drush_bodhy_generate_password($user, $user_name=""){
$edit = array();
//Смена пароля у всех юзеров
if ($user=='all'){
$results=db_query('SELECT uid FROM {users} WHERE uid!=0');
while ( $result = drush_db_fetch_object($results)){
$account = user_load($result->uid);
$edit['name'] = $account->name;
$edit['pass'] = gen_passwd();
user_save($account, $edit);
echo ("login: " . $account->name . "\n");
echo ("password: " . $edit['pass'] . "\n");
echo ("\n");
}
} elseif($user=='user' && $user_name!=""){
//$results=db_query('SELECT uid FROM {users} WHERE name = :name', array(':name'=>$user_name));
$results=drush_db_select('users', 'uid', 'name IN (:name)', array(':name'=>$user_name) );
$edit['name'] = $user_name;
$edit['pass'] = gen_passwd();
$account = drush_db_fetch_object($results);
if (empty($account)) {
echo "can't load account=" . $user_name . "\n";
exit(4);
}
$account = user_load($account->uid);
user_save($account, $edit);
echo ("login: " . $account->name . "\n");
echo ("password: " . $edit['pass'] . "\n");
} else echo "please specify a user name \n";
}
function drush_bodhy_rename_user($userName="", $newUserName=""){
if ($userName=="" || $newUserName==""){
echo "please specify the user name and the new user name \n";
exit(1);
}
$edit = array();
// Проверяем не повторяется ли новое имя
// $results=db_query('SELECT uid FROM {users} WHERE name = :name', array(':name'=>$newUserName));
$results=drush_db_select('users', 'uid', 'name IN (:name)', array(':name'=>$newUserName) );
$account = drush_db_fetch_object($results);
if (!empty($account)) {
echo "New name exists \n";
exit(2);
}
//$results=db_query('SELECT uid FROM {users} WHERE name = :name', array(':name'=>$userName));
$results=drush_db_select('users', 'uid', 'name IN (:name)', array(':name'=>$userName) );
$account = drush_db_fetch_object($results);
if (empty($account)) {
echo "can't load account=" . $userName . "\n";
exit(3);
}
if (!preg_match("/^[-_a-zA-Zа-яА-Я0-9]+$/", $newUserName)) {
echo "New user name is invalid \n";
exit(4);
}
$edit['name'] = $newUserName;
$account = user_load($account->uid);
user_save($account, $edit);
}
function drush_bodhy_rename_uid($userUid="", $newUserName=""){
if ($userUid=="" || $newUserName==""){
echo "please specify the uid and the new user name \n";
exit(1);
}
$edit = array();
// Проверяем не повторяется ли новое имя
// $results=db_query('SELECT uid FROM {users} WHERE name = :name', array(':name'=>$newUserName));
$results=drush_db_select('users', 'uid', 'name IN (:name)', array(':name'=>$newUserName));
$account = drush_db_fetch_object($results);
if (!preg_match("/^[0-9]+$/", $userUid)) {
echo "uid must be a number \n";
exit(2);
}
if (!empty($account)) {
echo "Name exists \n";
exit(3);
}
$edit['name'] = $newUserName;
$account = user_load($userUid);
if (empty($account)) {
echo "can't load account uid=" . $userUid . "\n";
exit(4);
}
if (!preg_match("/^[-_a-zA-Zа-яА-Я0-9]+$/", $newUserName)) {
echo "New user name is invalid \n";
exit(5);
}
user_save($account, $edit);
}
function drush_bodhy_search_nodes(string $pattern){
if (drush_drupal_major_version() < 7) {
echo ("only works in Drupal 7 \n");
exit(1);
}
if ($pattern=="") {
echo "please specify the regexp \n";
exit(2);
}
$results = db_query('SELECT nid FROM {node}');
while ($result=drush_db_fetch_object($results)){
$node = node_load($result->nid);
if(ereg($pattern, $node->body['und']['0']['value'])){
$str_node=("node/" . $result->nid);
$aliases = db_query('Select source, alias FROM url_alias WHERE source = :source', array(':source'=>$str_node));
$alias=drush_db_fetch_object($aliases);
echo ($str_node . " - ");
echo ($alias->alias . "\n");
}
}
}
function drush_bodhy_enable_caching() {
variable_set('cache', '1');
variable_set('block_cache', '1');
variable_set('preprocess_css', '1');
variable_set('preprocess_js', '1');
variable_set('page_compression', '0');
setup_prepro("prepro/cache");
}
function drush_bodhy_disable_caching() {
variable_set('cache', '0');
variable_set('block_cache', '0');
variable_set('preprocess_css', '0');
variable_set('preprocess_js', '0');
variable_set('page_compression', '0');
setup_prepro("prepro/onload");
drush_log(dt('caching related settings modified, cleaning caches'), 'success');
drush_cache_command_clear('all');
}
// Генерация пароля
function gen_passwd() {
return trim(shell_exec("apg -x 6 -n 1"));
}
function setup_prepro($mode) {
$prepro = variable_get('prepro',null);
if ($prepro) {
$prepro['filetypes']['sass']['cache_handler'] = $mode;
$prepro['filetypes']['scss']['cache_handler'] = $mode;
variable_set('prepro', $prepro);
}
}