3 # A small hack to recalculate all existing thumbnails (including mipmaps
4 # if you so desire), for use when we change something in the scaling/encoding
5 # pipeline. You may want to run it piece by piece if you don't want huge
6 # incremental backups, though.
8 # Run as www-data, e.g. with "sudo -u www-data ./update-image-cache.pl".
9 # You can also give it arguments if you want to use multiple threads, with
10 # something like "update-image-cache.pl 0 4" to run as core 0 of four cores.
11 # Remember to adjust $threshold first in any case.
18 use Sesse::pr0n::Config;
20 require Sesse::pr0n::Config_local;
24 package Apache2::ServerUtil;
29 print STDERR $_[1], "\n";
32 package FakeApacheReq;
37 OverloadMode => 'off',
38 OverloadEnableThreshold => '100000.0',
46 print STDERR $_[1], "\n";
49 print STDERR $_[1], "\n";
52 print STDERR $_[1], "\n";
55 use Sesse::pr0n::Common;
59 if ($a == -1 && $b != -1) {
62 if ($a != -1 && $b == -1) {
70 my @sr = sort { ($a->[0] != $b->[0]) ? (byres($a->[0], $b->[0])) : (byres($a->[1], $b->[1])) } @res;
78 # Don't regenerate thumbnails that were made after this. Set this to approximately
79 # when you upgraded pr0n to the version with the new image processing code.
80 my $threshold = `date +%s -d '2009-10-24 11:30'`;
82 my $regen_mipmaps = 0;
83 my $core_id = $ARGV[0] // 0;
84 my $num_cores = $ARGV[1] // 1;
86 my $dbh = DBI->connect("dbi:Pg:dbname=pr0n;host=" . $Sesse::pr0n::Config::db_host,
87 $Sesse::pr0n::Config::db_username, $Sesse::pr0n::Config::db_password)
88 or die "Couldn't connect to PostgreSQL database: " . DBI->errstr;
89 $dbh->{RaiseError} = 1;
91 my $r = bless {}, 'FakeApacheReq';
93 my $q = $dbh->prepare('SELECT id,filename,width,height FROM images WHERE id % ? = ? ORDER BY id DESC');
94 $q->execute($num_cores, $core_id);
96 while (my $ref = $q->fetchrow_hashref) {
97 my $id = $ref->{'id'};
98 my $dir = POSIX::floor($id / 256);
100 my @files = glob("../cache/$dir/$id-*.jpg");
101 if (!$regen_mipmaps) {
102 @files = grep { !/mipmap/ } @files;
109 my $mtime = (stat($c))[9];
110 if ($mtime < $threshold) {
113 if ($c =~ /$id-(\d+)-(\d+)\.jpg/ || $c =~ /$id-(-1)-(-1)\.jpg/) {
114 push @bothres, [$1, $2];
115 } elsif ($c =~ /$id-(\d+)-(\d+)-nobox\.jpg/ || $c =~ /$id-(-1)-(-1)-nobox\.jpg/) {
116 push @noboxres, [$1, $2];
117 } elsif ($c =~ /$id-(\d+)-(\d+)-box\.png/ || $c =~ /$id-(-1)-(-1)-box\.png/) {
118 push @boxres, [$1, $2];
121 next unless $any_old;
123 if (scalar @bothres > 0) {
124 Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 'both', sort_res(@bothres));
126 if (scalar @noboxres > 0) {
127 Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 'nobox', sort_res(@noboxres));
129 if (scalar @boxres > 0) {
130 Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 'box', sort_res(@boxres));
133 my @newfiles = glob("../cache/$dir/$id-*.jpg");
134 my %a = map { $_ => 1 } @files;
135 my %b = map { $_ => 1 } @newfiles;
138 if (!exists($b{$f})) {
139 print STDERR "Garbage-collected $f\n";