Forked from thsutton/make-drupal-file-field-private.sh
Last active
August 29, 2015 14:14
-
-
Save chales/74a4247f2d801e089122 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# This script will hold your hand during the process of converting an existing Drupal 7 file field from public to private. | |
# | |
# http://twitter.com/thsutton | |
# http://www.linkedin.com/pub/thomas-sutton/55/391/350 | |
# http://thomas-sutton.id.au/ | |
set -eu | |
# | |
# CONFIGURATION | |
# | |
# Field machine name | |
FIELD="field_file" | |
# Path to drush | |
DRUSH="drush" | |
# | |
# PROCESS | |
# | |
# Check that there *is* a private file system configured. | |
if $DRUSH vget file_private_path --exact >/dev/null 2>&1; then | |
# @todo Check that the directory exists, is writable, etc. | |
true | |
else | |
echo | |
echo "[error] Please configure a private file system directory." > /dev/stderr | |
echo | |
exit; | |
fi | |
# Drop temporary tables, if they exist. | |
$($DRUSH sql-connect) -BNe "DROP TABLE IF EXISTS temp_field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "DROP TABLE IF EXISTS temp_field_revision_${FIELD};" | |
# Create temporary tables. | |
$($DRUSH sql-connect) -BNe "CREATE TABLE temp_field_data_${FIELD} LIKE field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "CREATE TABLE temp_field_revision_${FIELD} LIKE field_revision_${FIELD};" | |
# Copy data into temporary tables. | |
$($DRUSH sql-connect) -BNe "INSERT INTO temp_field_data_${FIELD} SELECT * FROM field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "INSERT INTO temp_field_revision_${FIELD} SELECT * FROM field_revision_${FIELD};" | |
# Truncate the data tables. | |
$($DRUSH sql-connect) -BNe "DELETE FROM field_data_${FIELD} WHERE 1=1;" | |
$($DRUSH sql-connect) -BNe "DELETE FROM field_revision_${FIELD} WHERE 1=1;" | |
# Download and install the module. | |
if $DRUSH pm-info filefield_paths 2>/dev/null | egrep '^\s+Status\s+:' | grep -qi enabled; then | |
echo "filefield_paths already installed" | |
else | |
$DRUSH dl filefield_paths | |
$DRUSH en filefield_paths | |
fi | |
# Tell the user to configure the field to use private file system. | |
echo ; echo ; echo | |
echo "GO AND UPDATE ${FIELD} TO USE PRIVATE FILE SYSTEM" | |
echo ; echo ; echo | |
echo "Change made?: [Yn]" | |
while read complete; do | |
case "$complete" in | |
"y"|"Y"|"yes") | |
echo 'Good!' | |
break | |
;; | |
*) | |
echo 'Well hurry up!' | |
;; | |
esac | |
done | |
# Copy data back to original tables. | |
$($DRUSH sql-connect) -BNe "INSERT INTO field_data_${FIELD} SELECT * FROM temp_field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "INSERT INTO field_revision_${FIELD} SELECT * FROM temp_field_revision_${FIELD};" | |
# Drop the temporary tables. | |
$($DRUSH sql-connect) -BNe "DROP TABLE temp_field_data_${FIELD};" | |
$($DRUSH sql-connect) -BNe "DROP TABLE temp_field_revision_${FIELD};" | |
echo ; echo ; echo | |
echo "Now edit ${FIELD} again and tick the 'Retroactive update' checkbox in the 'FILE (FIELD) PATH SETTINGS' fieldset." | |
echo ; echo ; echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment