Code Snippets

Code Snippets bwaye3
Drupal Version

This section is for sharing sample code that does useful things for site administrators and developers.  Please do not add anything directly to this page, but rather create a sub page for each individual snippet.

Listing Enabled Modules Using a bash Script

Listing Enabled Modules Using a bash Script
Category
afrank30
Drupal Version
Tags

A shell script that prints out a list of enabled modules in all accessible databases, designed for ssh use when you need to check multiple Drupal 6 or 7 databases at one time.

Editor's Note:  A similar concept would work just as well for Drupal 8, but the database table names and structures have changed in Drupal 8, so you would need to adjust the script accordingly.  Also, be aware that this script will not work on any Drupal instance where you have used a custom table prefix for all of your Drupal site tables.

With thanks to Abdul Manaf on DBA.stackexchange for the basic code.


# mysql credential
user="root"
pass="pwd"

all_dbs="$(mysql -u $user -p$pass -Bse 'show databases')"        

# Exclude databases you don't want to check, such as non-Drupal 6 or 7 dbs.
# Then output database name and an alphabetized list of Enabled modules.
for db in $all_dbs
     do
        if [[ "$db" != "information_schema" &&
            "$db" != "mysql" &&
            "$db" != "performance_schema" &&
            "$db" != "test" ]] ; then
            echo '====================='
            echo "DATABASE: $db (ENABLED Modules)"
            echo "---------------------------"
            mysql -u$user -p$pass $db -sN -e "SELECT name FROM system WHERE status = 1 AND type = 'module' ORDER BY name ASC;" 
        fi 
     done

Listing Enabled Modules Using phpMyAdmin

Listing Enabled Modules Using phpMyAdmin
Category
afrank30
Drupal Version
Tags

Below is SQL code that prints out SQL statements that you can copy and paste back into phpMyAdmin (or any MySQL management tool that can connect to your Drupal database).  These statements will help you get a list of enabled modules, when you need to check multiple Drupal 6 or 7 databases at one time.

With thanks to Aaron Brown on DBA.stackexchange for the basic code.

Be careful to copy the backquotes/backticks (that look a little like left-leaning apostrophes) exactly.


SELECT CONCAT('SELECT \`name\` FROM ', schema_name, '.\`system\` WHERE \`status\` =\'1\' AND \`type\` =\'module\' ORDER BY \`name\` ASC;')
FROM information_schema.schemata
WHERE schema_name NOT IN ('information_schema','mysql','performance_schema','test', 'excluded_database');

This will print out a series of SQL statements, like the one below, which you could paste into the SQL tab of phpmyAdmin, in order to create lists of enabled modules.  This can be helpful if you don't have ssh access to your site, but still want an easy way to get a simple text list of enabled modules.

SELECT `name` FROM ms6_sample1.`system` WHERE `status` ='1' AND `type` ='module' ORDER BY `name` ASC; SELECT `name` FROM ms7_sample2.`system` WHERE `status` ='1' AND `type` ='module' ORDER BY `name` ASC;