-
Notifications
You must be signed in to change notification settings - Fork 2
/
process_files.php
104 lines (84 loc) · 3.14 KB
/
process_files.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
<?php
// Need this to overcome some Pharborist errors
function myErrorHandler($errno, $errstr, $errfile, $errline) {
if ( E_RECOVERABLE_ERROR===$errno ) {
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
}
return false;
}
set_error_handler('myErrorHandler');
require "vendor/autoload.php";
$redis = new Predis\Client(NULL, ['prefix' => 'gdtaf:']);
$dir = "/Users/rocketeerbkw/gotta_download_them_all/allmodules/";
try {
// Iterate through all the modules we have downloaded.
$all_modules = new FilesystemIterator($dir);
$c = 0;
foreach ($all_modules as $module_dir_path => $module_dir_info) {
// Skip files that might be at this level. Only interested in module
// directories.
if (!$module_dir_info->isDir()) {
continue;
}
// Look for a .info file
chdir($module_dir_path);
$info_files = glob('*.info');
if ($info_files === FALSE || count($info_files) === 0) {
// No .info files
continue;
}
// Let's assume there's only one .info and it's the first result.
$module_name = str_replace('.info', '', $info_files[0]);
// For every module, get all the functions it defines.
$cur_module = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($module_dir_path, FilesystemIterator::SKIP_DOTS));
foreach ($cur_module as $module_path => $module_info) {
// Skip directorys (just in case).
if ($module_info->isDir()) {
continue;
}
// Skip git stuff.
if (strstr($module_info->getPath(), '.git') !== FALSE) {
continue;
}
// Only interested in module, install, inc, php extensions.
// TODO skip tpl.php and api.php files?
if (!in_array($module_info->getExtension(), array('module', 'install', 'inc', 'php'))) {
continue;
}
// The path of the file if the module folder was root.
$path_from_module = str_replace($dir, '', $module_info->getPathName());
// Check if we've processed this file before.
if ($redis->sismember('processed_files', $path_from_module)) {
//print $path_from_module . ': already processed.' . PHP_EOL;
continue;
}
print $path_from_module . ': ';
// Find and track each function found in the file.
try {
$tree = Pharborist\Parser::parseFile($module_info->getPathName());
$functions = $tree->children(Pharborist\Filter::isInstanceOf(
'\Pharborist\Functions\FunctionDeclarationNode'
));
foreach ($functions as $func) {
// Get the name of the function w/o the module namespace
$clean_func = preg_replace('/^_?' . $module_name . '/i', 'MODULE', $func->getName());
$redis->zincrby('function_list', 1, $clean_func);
print '.';
}
$redis->sadd('processed_files', $path_from_module);
print ' Done.' . PHP_EOL;
}
catch (Exception $e) {
print ' Error ' . $e->getMessage() . PHP_EOL;
}
}
$c++;
if ($c > 10) {
// For debugging, only process $c modules then quit.
//break;
}
}
}
catch (UnexpectedValueException $e) {
print "Directory path error: " . $e->getMessage() . PHP_EOL;
}