Skip to content

Instantly share code, notes, and snippets.

@lnunesbr
Last active May 2, 2019 00:32
Show Gist options
  • Save lnunesbr/bbf2d411baf78a534a3ea554d122eccc to your computer and use it in GitHub Desktop.
Save lnunesbr/bbf2d411baf78a534a3ea554d122eccc to your computer and use it in GitHub Desktop.
This files checks for duplicate modules in your drupal installation, by checking duplicated occurrences of `.info` files. It will print an array with all occurrences of the info files. This is intented to be executed via command line, send the paths to be scanned as params in the php call. Be sure to place the files inside your site's `docroot` …
<?php
/*
This files checks for duplicate modules in your drupal installation, by checking duplicated occurrences of `.info` files.
It will print an array with all occurrences of the info files.
This is intented to be executed via command line, send the paths to be scanned as params in the php call.
Be sure to place the files inside your site's `docroot` location, and do not commit to your production repository.
For example:
```
php php-modules.php /sites/all /profiles/gardens /profiles/profile2
```
*/
echo "\n";
echo date('d/m/Y H:i:s');
echo "\n\n";
if (!is_array($argv) || $argc <= 1) {
echo "Be sure to place this file inside drupal docroot location. \n";
echo "Please provide valid paths as arguments in the php file call. \n";
echo "ex: $ php php-modules.php /sites/all /profiles/gardens\n";
echo "ATTENTION: this do not check for valid paths in the arguments, put correct paths\n";
exit;
}
$params = $argv;
unset($params[0]);
define('DRUPAL_ROOT', getcwd());
$modules = [];
// Locate all .info files in a given location
function locate_info_recursive($path_locate, &$modules) {
$recursive_files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(DRUPAL_ROOT . $path_locate));
foreach($recursive_files as $file)
{
$filename = $file->getFilename();
if (preg_match('/\.info$/', $filename)) {
$path = $file->getPath();
$pathName = $file->getPathName();
$fileLoad = file_get_contents($pathName, FILE_USE_INCLUDE_PATH);
if(preg_match_all('/version\ \=\ (.*)/', $fileLoad, $matches)){
$clean_path = str_replace(DRUPAL_ROOT, '', $path);
$modules[$filename][$clean_path] = $matches[0][0];
}
}
}
}
echo "\n######################################################################\n";
echo "\n";
foreach ($params as $path_check) {
locate_info_recursive($path_check, $modules);
}
//sort modules with duplicates first
array_multisort(array_map('count', $modules), SORT_DESC, $modules);
echo "----------------------------------------------------------------------\n";
foreach ($modules as $mod_name => $mod_occurrences) {
echo "Module Name: " . $mod_name;
echo "\n";
foreach ($mod_occurrences as $mod_path => $mod_version) {
echo " Path: " . $mod_path . " | Version: " . $mod_version;
echo "\n";
}
echo "----------------------------------------------------------------------\n";
}
echo "\n######################################################################\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment