-
Notifications
You must be signed in to change notification settings - Fork 0
/
drush-script.php
438 lines (346 loc) · 12.7 KB
/
drush-script.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php
/**
* @file
* The PHP page that serves all page requests on a Drupal installation.
*
* The routines here dispatch control to the appropriate handler, which then
* prints the appropriate page.
*
* All Drupal code is released under the GNU General Public License.
* See COPYRIGHT.txt and LICENSE.txt.
*
* TODO: find age of the users in Drupal, and mark them as novices, advanced, etc.
* TODO: Loop over active users to find their age in the community. Is it a healthy number?
*
* nohup sudo php scripts/drush-script.php --verbose yes --status 13 --filename /home/alexmoreno/needs-work.csv &
*
*/
/**
* Root directory of Drupal installation.
*
*/
// From current folder.
// Stage.
// define('DRUPAL_ROOT', "/var/www/staging.devdrupal.org/htdocs/");
// Dev.
define('DRUPAL_ROOT', "/var/www/dev/alexmor-drupal.dev.devdrupal.org/htdocs/");
chdir(DRUPAL_ROOT);
define('SMALL_FILE', 1000);
require_once 'lib.php';
// Fetch command line options.
$short_options = "hl::f::st:lm:vb:env:dcr:de:dc";
$long_options = ["help", "filename:", "status:", "limit:", "verbose:", "env:", "datecreated:", "dateend:", "datechanged:"];
$options = getopt($short_options, $long_options);
$verbose = FALSE;
$fileoutput = "";
if(isset($options["f"]) || isset($options["filename"])) {
$fileoutput = isset($options["f"]) ? $options["f"] : $options["filename"];
}
if(isset($options["lm"]) || isset($options["limit"])) {
$limit = isset($options["lm"]) ? $options["lm"] : $options["limit"];
}
if(isset($options["vb"]) || isset($options["verbose"])) {
$verbose = TRUE;
echo "Verbose enabled, being noisy.";
}
if(isset($options["dcr"]) || isset($options["datecreated"])) {
$datecreated = $options["datecreated"];
if($verbose) {
echo PHP_EOL . "Date created:: " . $datecreated;
}
}
if(isset($options["de"]) || isset($options["dateend"])) {
$dateend = $options["dateend"];
if($verbose) {
echo PHP_EOL . "DateEnd:: " . $dateend;
}
}
if(isset($options["dc"]) || isset($options["datechanged"])) {
$datechanged = $options["datechanged"];
if($verbose) {
echo PHP_EOL . "Date Changed:: " . $datechanged;
}
}
if(isset($options["hl"]) || isset($options["help"])) {
$help = isset($options["hl"]) ? $options["help"] : $options["help"];
print_help_message();
exit(1);
}
if(isset($options["st"]) || isset($options["status"])) {
$status = isset($options["st"]) ? $options["st"] : $options["status"];
// All active issues
if($status == "active") {
$status = [1,13,8,14,15,4,16];
}
// All closed issues
if($status == "fixed") {
$status = [2,7];
}
// All closed issues
if($status == "closed") {
$status = [2,3,5,6,18,17,7];
}
// All issues
if($status == "all") {
$status = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18];
}
if ($verbose) {
echo PHP_EOL;
echo "Setting up status: ";
print_r($status);
}
// Maybe use $ENV instead?
if(isset($options["env"]) || isset($options["environment"])) {
$env = isset($options["env"]) ? $options["env"] : $options["environment"];
if($env == "stage") {
define('DRUPAL_ROOT', "/var/www/staging.devdrupal.org/htdocs/");
chdir(DRUPAL_ROOT);
}
}
}
// Avoiding warning messages
$_SERVER['SCRIPT_NAME'] = '/script.php';
$_SERVER['SCRIPT_FILENAME'] = '/path/to/this/script.php';
$_SERVER['HTTP_HOST'] = 'domain.com';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['REQUEST_METHOD'] = 'POST';
$ip_address = $_SERVER['REMOTE_ADDR'];
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Build the query.
$query = db_select('node', 'n');
// Temporarily limit number of queries if needed for debugging.
if ($limit) {
$query->range(0, $limit);
if ($verbose) {
echo PHP_EOL . "NOTE: limiting to $limit queries by cli." . PHP_EOL . PHP_EOL;
}
}
$query->join('field_data_field_issue_status','fis','n.nid = fis.entity_id');
$query->join('field_data_field_project','fdp','n.nid = fdp.entity_id');
$query->join('field_data_field_issue_version','fiv','n.nid = fiv.entity_id');
// $query->join('field_data_field_release_category','frc','n.nid = frc.entity_id');
if ($verbose) {
echo "Executing query: ";
}
$results = $query
->fields('n', array('nid', 'title', 'created', 'changed', 'uid'))
->fields('fis', array('field_issue_status_value'))
->fields('fdp', array('field_project_target_id'))
->fields('fiv', array('field_issue_version_value'))
->condition('status', 1)
->condition('field_issue_status_value', $status)
->orderBy('created', 'DESC');
if(isset($datecreated)) {
echo "Filtering by date created" . PHP_EOL;
$my_start_date = date('U', mktime(0, 0, 0, "1", "1", $datecreated));
$my_end_date = date('U', mktime(0, 0, 0, "12", "31", $datecreated));
$query->condition('created', array($my_start_date, $my_end_date), 'BETWEEN');
}
if(isset($dc) || isset($datechanged)) {
echo "Filtering by date changed/updated" . PHP_EOL;
$changed_start_date = date('U', mktime(0, 0, 0, "1", "1", $datechanged));
$changed_end_date = date('U', mktime(0, 0, 0, "12", "31", $datechanged));
$query->condition('changed', array($changed_start_date, $changed_end_date), 'BETWEEN');
}
// Get the queries.
$results = $query->execute();
// TODO: Move to its own function.
// Prepare to write the csv.
$csvfile = "issues.csv";
$fileusers = "users.csv";
if ($fileoutput != "") {
$csvfile = $fileoutput;
$fileusers = $fileoutput . '-unique-authors.csv';
$filecommenters = $fileoutput . '-commenters.csv';
}
$fp = fopen($csvfile, 'w');
if ($verbose) {
echo "users cvs: " . $fileusers;
echo " -- commenters cvs: " . $filecommenters;
}
$fpusers = fopen($fileusers, 'w');
$fpcommenters = fopen($filecommenters, 'w');
$issues = Array();
//array_push($issues, Array('Node ID','Title','Created','Updated','Interval (days)','Number comments', 'Status', 'Num. authors', 'Patch size'));
fputcsv($fp, Array('Node ID','Title', 'projectID', 'Version', 'Core version', 'Created','Updated','Interval (days)','Number comments', 'Status', 'Num. authors', 'Patch size', 'tags'));
$interval = 0;
// $commenters = array();
$uniqueAuthors = array();
$allTags = Array();
// Ready to iterate.
foreach($results as $result) {
$created = date('F j, Y, g:i a', $result->created);
$changed = date('F j, Y, g:i a', $result->changed);
$interval = diffDates($created, $changed);
$node = node_load($result->nid);
// Store the current author/user UID.
if (isset($uniqueAuthors[$result->uid])) {
$uniqueAuthors[$result->uid][1]++;
}
else {
$uniqueAuthors[$result->uid][0] = $result->uid;
$uniqueAuthors[$result->uid][1] = 1;
}
if (isDrupalLegacy($result->field_issue_version_value)) {
echo "OLD VERSION OF DRUPAL:: " . $result->field_issue_version_value;
if($coreVersion == "UNKNOWN") {
echo " VERSION ALREADY SET... ERROR!!!";
} else {
$coreVersion = "legacy";
}
}
if (legacyTwoDigits($result->field_issue_version_value)) {
echo " :: legacyTwoDigits:: " . $result->field_issue_version_value;
}
if (modernThreeDigits($result->field_issue_version_value)) {
echo " :: modernThreeDigits:: " . $result->field_issue_version_value;
$coreVersion = "modern";
}
if (legacyDrupalModuleVersion($result->field_issue_version_value)) {
echo " :: legacyDrupalModuleVersion:: " . $result->field_issue_version_value;
$coreVersion = "legacy";
}
// This overrides the previous legacyModuleVersion.
if (legacyDrupalD8Module($result->field_issue_version_value)) {
echo " :: legacyDrupalD8Module D8:: " . $result->field_issue_version_value;
$coreVersion = "modern";
}
if (isDrupalModern($result->field_issue_version_value)) {
echo "DRUPAL 8 OR LATER FOUND isDrupalModern:: " . $result->field_issue_version_value;
$coreVersion = "modern";
}
if ($verbose) {
echo PHP_EOL . PHP_EOL . "NID :: " . $result->nid . " title :: " . $result->title . " - Status :: "
. $result->field_issue_status_value . " project ID :: " . $result->field_project_target_id
. " Author UID: " . $result->uid
. " CREATED: " . date("d m Y",$result->created)
. " CHANGED: " . date("d m Y",$result->changed)
. PHP_EOL . " Version::::" . $result->field_issue_version_value
. " Core Version::::" . $coreVersion;
echo PHP_EOL . PHP_EOL . " ::::::::::::::::" . PHP_EOL;
}
// Get the tags.
$tags = "";
foreach($node->taxonomy_vocabulary_9[und] as $taxonomy) {
$term = taxonomy_term_load($taxonomy['tid']);
$tags = $tags . "," . $term->name;
if(isset($allTags[$term->name])) {
$allTags[$term->name] = $term->name;
}
}
if($tags != "") {
$tags = trim($tags, ',');
if($verbose) {
echo "all tags: ";
print_r($tags);
echo PHP_EOL;
}
}
$comments = comment_get_thread($node, COMMENT_MODE_FLAT, 100);
$filesize = 0;
$numAuthors = array();
foreach($comments as $comment) {
$currentComment = comment_load($comment);
// Get current Author.
$numAuthors[$currentComment->name] = $currentComment->name;
// TODO: Get number of authors per issue. if author is not in array, add a new one
// if patch does not exist, but there is a Gitlab MR, check if there is an API.
// CHECK THE DIFF: https://git.drupalcode.org/project/entity_jump_menu/-/merge_requests/2.diff
foreach($currentComment->field_issue_changes['und'] as $field_file) {
foreach($field_file['new_value'] as $file) {
if (!empty($file['filename'])) {
// If we find a patch.
if (strpos($file['filename'], '.patch')!== false) {
if ($verbose) {
echo "Patch found.";
echo PHP_EOL . "fid:: " . $file['fid'];
echo PHP_EOL . "uri:: " . $file['uri'] . PHP_EOL;
}
// If we find a patch, we store its size.
$filesize = $file['filesize'];
}
if($file['filesize'] < SMALL_FILE) {
// Not doing anything with this for now.
if ($verbose) {
echo "NOTE: Small patch found." . PHP_EOL;
}
}
}
}
}
}
// Move to array.
$output = Array($result->nid,$result->title, $result->field_project_target_id, $result->field_issue_version_value, $coreVersion, $created, $changed, $interval,
count($comments), $result->field_issue_status_value, sizeof($numAuthors), $filesize, $tags);
// Writing to the csv.
fputcsv($fp, $output);
}
fclose($fp);
fclose($fpBlocked);
/*
echo "Commenters: ";
print_r($commenters);
echo "Unique Authors: ";
print_r($uniqueAuthors);
foreach($uniqueAuthors as $uniqueAuthor) {
fputcsv($fpusers, $uniqueAuthor);
}
fclose($fpusers);
fputcsv($fpcommenters, $commenters);
fclose($fpcommenters);
*/
// function cleanTags($tags) {
// }
/**
* Calculating time lapsed between two dates.
*
*/
function diffDates($created, $updated) {
$date1 = new DateTime($created);
$date2 = new DateTime($updated);
$interval = $date1->diff($date2);
return $interval->days;
}
/**
* Printing a help message.
*
*/
function print_help_message() {
echo PHP_EOL . "Arguments: ";
echo PHP_EOL . PHP_EOL . "--status: ";
echo PHP_EOL . " - Fixed: 2";
echo PHP_EOL . " - Works as designed: 6";
echo PHP_EOL . " - Needs review: 8";
echo PHP_EOL . " - Needs work: 13";
echo PHP_EOL . " - RTBC: 14";
echo PHP_EOL . " - All active issues: active";
echo PHP_EOL . " -- DateStart";
echo PHP_EOL . " All statuses";
echo PHP_EOL . " 1|Active";
echo PHP_EOL . " 13|Needs work";
echo PHP_EOL . " 8|Needs review";
echo PHP_EOL . " 14|Reviewed & tested by the community";
echo PHP_EOL . " 15|Patch (to be ported)";
echo PHP_EOL . "4|Postponed";
echo PHP_EOL . "16|Postponed (maintainer needs more info)";
echo PHP_EOL . "2|Fixed --> NOT USED FOR HISTORIC DATA. After 3 months they are moved into Closed(Fixed)";
echo PHP_EOL . "3|Closed (duplicate)";
echo PHP_EOL . "5|Closed (won't fix)";
echo PHP_EOL . "6|Closed (works as designed)";
echo PHP_EOL . "18|Closed (cannot reproduce)";
echo PHP_EOL . "17|Closed (outdated)";
echo PHP_EOL . "7|Closed (fixed)";
echo PHP_EOL;
echo PHP_EOL . " - Examples:";
echo PHP_EOL . " php scripts/drush-script.php --status 14";
echo PHP_EOL . " php scripts/drush-script.php --status active";
echo PHP_EOL . " php scripts/drush-script.php --status active --datestart 2018";
echo PHP_EOL . PHP_EOL . "--environment: stage";
echo PHP_EOL . PHP_EOL ."Example: nohup sudo php scripts/drush-script.php --verbose yes --status active --filename /home/alexmoreno/needs-work.csv &";
echo PHP_EOL . PHP_EOL;
}
/* TODO: CLEANUP */
function fetch_arguments() {
}
?>