]> git.imager.perl.org - bse.git/blame - site/cgi-bin/admin/imageclean.pl
consistently load the managed images directory from the config file
[bse.git] / site / cgi-bin / admin / imageclean.pl
CommitLineData
41b9d8ec
TC
1#!/usr/bin/perl -w
2use strict;
f8282904
TC
3use FindBin;
4use lib "$FindBin::Bin/../modules";
771ab646 5use BSE::API qw(bse_init);
f40af7e2 6use BSE::TB::Images;
41b9d8ec 7use Articles;
771ab646
TC
8use BSE::CfgInfo qw(cfg_image_dir);
9use File::Spec::Functions qw(catfile);
10
11bse_init("..");
41b9d8ec 12
d09682dd 13my %articleIds = ( -1 => 1 );
f40af7e2 14my $images = BSE::TB::Images->new;
41b9d8ec
TC
15my @images = $images->all;
16
17++$|;
18print <<EOS;
19Content-Type: text/plain
20
21Image Cleanup Tool
22------------------
23
24Removing image records that have no article
25EOS
26
27# first remove any image records that don't have a valid article id
28for my $image (@images) {
29 print ".";
30 # do we know about this article id?
31 unless (exists $articleIds{$image->{articleId}}) {
32 $articleIds{$image->{articleId}} =
33 defined(Articles->getByPkey($image->{articleId}));
34 }
35 unless ($articleIds{$image->{articleId}}) {
36 $image->remove();
37 print "x";
38 }
39}
40
41print "\n\nRebuilding image list and indexing\n";
42# rebuild the images list
f40af7e2 43$images = BSE::TB::Images->new;
41b9d8ec
TC
44@images= $images->all;
45
46my %names = map { $_->{image}, $_->{id} } @images;
47
957a90ca
TC
48print "\n\nScanning for thumbnails\n";
49
50my @articleids = Articles->allids;
51for my $id (@articleids) {
52 my $article = Articles->getByPkey($id)
53 or next;
54 if ($article->{thumbImage}) {
55 $names{$article->{thumbImage}} = "a$id";
56 }
57}
58
41b9d8ec
TC
59print "\nRemoving unused images\n";
60
771ab646
TC
61my $image_dir = cfg_image_dir();
62opendir IMG, $image_dir
63 or do { print "Cannot open $image_dir: $!\n"; exit };
41b9d8ec
TC
64while (defined(my $file = readdir IMG)) {
65 if ($file =~ /^\d{8}/) {
66 print ".";
771ab646 67 unless ($names{$file} || !-f catfile($image_dir, $file)) {
41b9d8ec 68 print "x";
771ab646
TC
69
70 unlink catfile($image_dir, $file)
71 or print "\nCould not remove $image_dir$file: $!\n";
41b9d8ec
TC
72 }
73 }
74}
75
76print "\nDone\n";
77
78__END__
79
80=head1 NAME
81
82imageclean.pl - clean up the images directory and image records
83
84=head1 SYNOPSIS
85
86 (called as a CGI script, no values passed in)
87
88=head1 WARNING
89
771ab646
TC
90This will remove B<any> images in the configured managed images
91directory that have names starting with 8 or more digits if
92they don't exist in the C<image> table as a record with a current
93article number.
41b9d8ec
TC
94
95If you need image names of this form, put them elsewhere, or
771ab646 96reconfigure the managed images directory.
41b9d8ec
TC
97
98=head1 DESCRIPTION
99
100Scans the C<image> table looking for images that don't have an
101article, and for image files that don't have image records.
102
103The first is required due to a bug in older versions that left the
104image records around when deleting an article. It's also a recovery
105tool just in case the database loses referential integrity, since
106MySQL doesn't enforce it.
107
108The second is required for two reasons:
109
110=over
111
112=item
113
114older versions didn't remove the image files when images were removed
115
116=item
117
771ab646
TC
118you may have deleted articles with images under an older version,
119which would have left the image records (and the image files)
41b9d8ec
TC
120
121=back
122
123=head1 AUTHOR
124
125Tony Cook <tony@develop-help.com>
126
127=cut