Last active
October 8, 2017 20:11
-
-
Save martinmev/1397516 to your computer and use it in GitHub Desktop.
Drupal, Galleria field - mass image upload; Ubercart - add images to product. Python scripts create sql commands. Z Shell functions for easy use included.
This file contains 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
setopt extended_glob csh_null_glob | |
maintenance() { | |
echo "Maintenance mode." | |
cat <<EOF | mysql -u drupal -pPASSWORD drupal && echo "OK." | |
DELETE FROM variable WHERE name = 'site_offline'; | |
INSERT INTO variable (name, value) VALUES ('site_offline', 's:1:"1";'); | |
DELETE FROM cache WHERE cid = 'variables'; | |
EOF | |
} | |
online() { | |
echo "Online mode." | |
cat <<EOF | mysql -u drupal -pPASSWORD drupal && echo "OK." | |
UPDATE variable SET value = 's:1:"0";' WHERE name = 'site_offline'; | |
DELETE FROM cache WHERE cid = 'variables'; | |
EOF | |
} | |
getVid() { | |
if [ -n "$2" ] | |
then | |
echo "select nid,vid,title from node_revisions where nid=$2" | mysql -u USER -p$1 -h HOST DATABASE | |
else | |
echo "Usage: getVid [Password] [Nid]" | |
fi | |
} | |
prepare() { | |
if [ -z "$4" ] | |
then | |
echo "Usage: prepare [new photo directory] [nid] [vid] [delta]" | |
else | |
if [ -e "/var/www/sites/default/files/$1" ] | |
then | |
echo "Directory $1 exists." | |
else | |
IMAGEDIR=$1 | |
mkdir "/var/www/sites/default/files/$IMAGEDIR" | |
cp -- *.JPG *.jpg *.jpeg *.png *.PNG "/var/www/sites/default/files/$IMAGEDIR/" | |
chown -R www-data.www-data "/var/www/sites/default/files/$IMAGEDIR/" | |
chmod ogu+r "/var/www/sites/default/files/$IMAGEDIR/" | |
for x in galleria_teaser gallery_assist-default-thumbnail-100 node-gallery-display medium nail imagefield_thumbs teaser_sticky ; do mkdir "$x" ; mkdir "$x/$IMAGEDIR" ; done | |
online | |
for x in galleria_teaser gallery_assist-default-thumbnail-100 node-gallery-display medium nail teaser_sticky ; do for f in *.JPG *.jpg *.jpeg *.png *.PNG ; do wget -O "$x/$IMAGEDIR/$f" "http://localhost/sites/default/files/imagecache/$x/$IMAGEDIR/$f"; done; done | |
maintenance | |
cp -r -- gallery_assist-default-thumbnail-100/* imagefield_thumbs/ | |
for x in galleria_teaser gallery_assist-default-thumbnail-100 node-gallery-display medium nail imagefield_thumbs teaser_sticky ; do ls -1 "$x/$IMAGEDIR" | wc --lines ; done | |
mkdir imagecache | |
mv -- galler* medium/ nail/ node-gallery-display/ teaser_sticky/ imagecache/ | |
mkdir "$IMAGEDIR" | |
mv -- *.JPG *.jpg *.jpeg *.png *.PNG "$IMAGEDIR" | |
~/files2galleriasql.py "$2" "$3" "$4" "$IMAGEDIR"/* >../photo.sql | |
fi | |
fi | |
} | |
mo() { | |
encfs /root/data /root/dec | |
} | |
um(){ | |
umount /root/dec | |
} | |
i () { | |
nodeId="$1" | |
shift | |
~/images2product.py "$nodeId" 10 $@ | ~/DB/console.sh | |
} | |
This file contains 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
#!/usr/bin/env python2.7 | |
""" | |
Usage: | |
files2galleriasql.py [Node Id] [Revision Id] [delta] [image1] [image2] ... | |
The sql script (fills Drupal Galleria field) is written to the standard output. | |
Obtaining revision id from nid (node id): | |
select * from node_revisions where nid=[nid]\G | |
Obtaining revision id and last delta: | |
select vid,max(delta) from content_field_galleria where nid=[nid]\G | |
Empty galleria field: | |
delta = 0, field_galleria_fid = NULL, field_galleria_list = NULL, field_galleria_data = NULL | |
Requires: Python Imaging Library (PIL) http://www.pythonware.com/products/pil/ (package python-imaging on Ubuntu) | |
Drupal path to the images: sites/default/files/ | |
Drupal image cache: sites/default/files/{imagefield_thumbs,imagecache/node-gallery-display} (generated automatically) | |
############### | |
Pre-generating imagecache (preset) images (galleria_teaser gallery_assist-default-thumbnail-100 node-gallery-display medium nail imagefield_thumbs teaser_sticky) | |
############### | |
See attached Z Shell .zshrc file. | |
# imagefield_thumbs = gallery_assist-default-thumbnail-100 | |
export IMAGEDIR=name_of_new_image_directory | |
mkdir "/var/www/sites/default/files/$IMAGEDIR" | |
cp *.JPG *.jpg *.png "/var/www/sites/default/files/$IMAGEDIR/" | |
for x in galleria_teaser gallery_assist-default-thumbnail-100 node-gallery-display medium nail imagefield_thumbs teaser_sticky ; do mkdir "$x" ; mkdir "$x/$IMAGEDIR" ; done | |
for x in galleria_teaser gallery_assist-default-thumbnail-100 node-gallery-display medium nail teaser_sticky ; do for f in *.JPG *.jpg *.png ; do wget -O "$x/$IMAGEDIR/$f" "http://localhost/sites/default/files/imagecache/$x/$IMAGEDIR/$f"; done; done | |
cp -r gallery_assist-default-thumbnail-100/* imagefield_thumbs/ | |
for x in galleria_teaser gallery_assist-default-thumbnail-100 node-gallery-display medium nail imagefield_thumbs teaser_sticky ; do ls -1 "$x/$IMAGEDIR" | wc --lines ; done | |
mkdir imagecache | |
mv galler* medium/ nail/ node-gallery-display/ teaser_sticky/ imagecache/ | |
mkdir "$IMAGEDIR" | |
mv *.JPG *.jpg *.png "$IMAGEDIR" | |
~/files2galleriasql.py 361 363 0 $IMAGEDIR/* >test.sql | |
# Test environment: upload images and directories to the production environment (directory sites/default/files/) | |
""" | |
import PIL.Image | |
import os.path | |
import sys | |
import time | |
class Img(): | |
def __init__(self,f): | |
self.image=PIL.Image.open(f) | |
self.mime='image/'+self.image.format.lower() | |
(self.width,self.height) = self.image.size | |
self.image.fp.seek(0,2) | |
self.filesize=self.image.fp.tell() | |
self.filename = f | |
def sql(self,nodeId,revisionId,delta): | |
sql = """ | |
insert into files (uid,filename,filepath,filemime,filesize,status,timestamp) | |
values('1','%s','sites/default/files/%s','%s','%s','1','%s'); | |
set @fid = last_insert_id(); | |
insert into filefield_meta(fid,width,height,duration,audio_format,audio_sample_rate,audio_channel_mode,audio_bitrate,audio_bitrate_mode,tags) | |
values (@fid,%s,%s,0,'',0,'',0,'','a:0:{}'); | |
INSERT INTO `content_field_galleria`(vid,nid,delta,field_galleria_fid,field_galleria_list,field_galleria_data) | |
VALUES (%s,%s,%s,@fid,1,concat('a:13:{s:11:\"description\";s:0:\"\";s:3:\"fid\";s:',length(@fid),':\"',@fid,'\";s:5:\"width\";i:%s;s:6:\"height\";i:%s;s:8:\"duration\";i:0;s:12:\"audio_format\";s:0:\"\";s:17:\"audio_sample_rate\";i:0;s:18:\"audio_channel_mode\";s:0:\"\";s:13:\"audio_bitrate\";i:0;s:18:\"audio_bitrate_mode\";s:0:\"\";s:4:\"tags\";a:0:{}s:3:\"alt\";s:0:\"\";s:5:\"title\";s:0:\"\";}')); | |
""" % (self.filename,self.filename,self.mime,self.filesize,int(time.time()),\ | |
self.width,self.height,\ | |
revisionId,nodeId,delta,self.width,self.height) | |
return sql | |
if __name__ == '__main__': | |
if len(sys.argv)<5: | |
print __doc__ | |
exit(1) | |
try: | |
nodeId = int(sys.argv[1]) | |
revisionId = int(sys.argv[2]) | |
delta = int(sys.argv[3]) | |
except ValueError: | |
raise RuntimeError('nodeId, revisionId, delta must be integer') | |
print 'SET NAMES utf8;' | |
print """delete from content_field_galleria | |
where vid=%s and nid=%s | |
and field_galleria_fid IS NULL | |
and field_galleria_list IS NULL | |
and field_galleria_data IS NULL; | |
""" % (revisionId,nodeId) | |
for x in range(4,len(sys.argv)): | |
try: | |
img = Img(sys.argv[x]) | |
except IOError: | |
raise RuntimeError("Can't open file "+sys.argv[x]) | |
print img.sql(nodeId,revisionId,delta) | |
delta+=1 | |
This file contains 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
Method imagecache_image_flush doesn't remove (pre-generated) images from the disc. | |
It is called when the article has been edited. | |
diff --git a/www/sites/all/modules/imagecache/imagecache.module b/www/sites/all/modules/imagecache/imagecache.module | |
index 1584304..6aa5501 100644 | |
--- a/www/sites/all/modules/imagecache/imagecache.module | |
+++ b/www/sites/all/modules/imagecache/imagecache.module | |
@@ -1127,11 +1127,6 @@ function imagecache_preset_flush($preset) { | |
* The Drupal file path to the original image. | |
*/ | |
function imagecache_image_flush($path) { | |
- foreach (imagecache_presets() as $preset) { | |
- $derivative_path = imagecache_create_path($preset['presetname'], $path); | |
- module_invoke_all('imagecache_image_flush', $derivative_path, $preset, $path); | |
- file_delete($derivative_path); | |
- } | |
} | |
function imagecache_action($actionid) { |
This file contains 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
#!/usr/bin/env python2.7 | |
""" | |
Usage: | |
images2product.py [Node Id] [delta] [image1] [image2] ... | |
The sql script (fills ...) is written to the standard output. | |
~/images2product.py 361 363 0 $IMAGEDIR/* >test.sql | |
""" | |
import PIL.Image | |
import os.path | |
import sys | |
import time | |
class Img(): | |
def __init__(self,f): | |
self.image=PIL.Image.open(f) | |
self.mime='image/'+self.image.format.lower() | |
(self.width,self.height) = self.image.size | |
self.image.fp.seek(0,2) | |
self.filesize=self.image.fp.tell() | |
self.filename = f | |
def sql(self,nodeId,delta): | |
sql = """ | |
insert into files (uid,filename,filepath,filemime,filesize,status,timestamp) | |
values('1','%s','sites/default/files/%s','%s','%s','1','%s'); | |
set @fid = last_insert_id(); | |
insert into filefield_meta(fid,width,height,duration,audio_format,audio_sample_rate,audio_channel_mode,audio_bitrate,audio_bitrate_mode,tags) | |
values (@fid,%s,%s,0,'',0,'',0,'','a:0:{}'); | |
INSERT INTO `content_field_image_cache`(vid,nid,delta, | |
field_image_cache_fid, | |
field_image_cache_list, | |
field_image_cache_data) | |
VALUES ((select max(vid) from node_revisions where nid=%d),%s,%s,@fid,1,concat('a:13:{s:11:\"description\";s:0:\"\";s:3:\"fid\";s:',length(@fid),':\"',@fid,'\";s:5:\"width\";i:%s;s:6:\"height\";i:%s;s:8:\"duration\";i:0;s:12:\"audio_format\";s:0:\"\";s:17:\"audio_sample_rate\";i:0;s:18:\"audio_channel_mode\";s:0:\"\";s:13:\"audio_bitrate\";i:0;s:18:\"audio_bitrate_mode\";s:0:\"\";s:4:\"tags\";a:0:{}s:3:\"alt\";s:0:\"\";s:5:\"title\";s:0:\"\";}')); | |
""" % (self.filename,self.filename,self.mime,self.filesize,int(time.time()),\ | |
self.width,self.height,\ | |
nodeId,nodeId,delta,self.width,self.height) | |
return sql | |
if __name__ == '__main__': | |
if len(sys.argv)<4: | |
print __doc__ | |
exit(1) | |
try: | |
nodeId = int(sys.argv[1]) | |
delta = int(sys.argv[2]) | |
except ValueError: | |
raise RuntimeError('nodeId, revisionId, delta must be integer') | |
print 'SET NAMES utf8;' | |
print """delete from content_field_image_cache | |
where nid=%d | |
and field_image_cache_fid IS NULL | |
and field_image_cache_list IS NULL | |
and field_image_cache_data IS NULL; | |
""" % (nodeId,) | |
for x in range(3,len(sys.argv)): | |
try: | |
img = Img(sys.argv[x]) | |
except IOError: | |
raise RuntimeError("Can't open file "+sys.argv[x]) | |
print img.sql(nodeId,delta) | |
delta+=1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment