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;
108 my $mtime = (stat($c))[9];
109 if ($mtime < $threshold) {
112 if ($c =~ /$id-(\d+)-(\d+)-nobox\.jpg/ || $c =~ /$id-(-1)-(-1)-nobox\.jpg/) {
113 push @noboxres, [$1, $2];
114 } elsif ($c =~ /$id-(\d+)-(\d+)-box\.png/ || $c =~ /$id-(-1)-(-1)-box\.png/) {
115 push @boxres, [$1, $2];
118 next unless $any_old;
120 if (scalar @noboxres > 0) {
121 Sesse::pr0n::Common::ensure_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, sort_res(@noboxres));
123 if (scalar @boxres > 0) {
124 Sesse::pr0n::Common::ensure_infobox_cached($r, $ref->{'filename'}, $id, $ref->{'width'}, $ref->{'height'}, 1, sort_res(@boxres));
127 my @newfiles = glob("../cache/$dir/$id-*.jpg");
128 my %a = map { $_ => 1 } @files;
129 my %b = map { $_ => 1 } @newfiles;
132 if (!exists($b{$f})) {
133 print STDERR "Garbage-collected $f\n";