]>
Commit | Line | Data |
---|---|---|
41b9d8ec TC |
1 | #!/usr/bin/perl -w |
2 | use strict; | |
f8282904 TC |
3 | use FindBin; |
4 | use lib "$FindBin::Bin/../modules"; | |
771ab646 | 5 | use BSE::API qw(bse_init); |
f40af7e2 | 6 | use BSE::TB::Images; |
41b9d8ec | 7 | use Articles; |
771ab646 TC |
8 | use BSE::CfgInfo qw(cfg_image_dir); |
9 | use File::Spec::Functions qw(catfile); | |
10 | ||
11 | bse_init(".."); | |
41b9d8ec | 12 | |
d09682dd | 13 | my %articleIds = ( -1 => 1 ); |
f40af7e2 | 14 | my $images = BSE::TB::Images->new; |
41b9d8ec TC |
15 | my @images = $images->all; |
16 | ||
17 | ++$|; | |
18 | print <<EOS; | |
19 | Content-Type: text/plain | |
20 | ||
21 | Image Cleanup Tool | |
22 | ------------------ | |
23 | ||
24 | Removing image records that have no article | |
25 | EOS | |
26 | ||
27 | # first remove any image records that don't have a valid article id | |
28 | for 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 | ||
41 | print "\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 | ||
46 | my %names = map { $_->{image}, $_->{id} } @images; | |
47 | ||
957a90ca TC |
48 | print "\n\nScanning for thumbnails\n"; |
49 | ||
50 | my @articleids = Articles->allids; | |
51 | for 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 |
59 | print "\nRemoving unused images\n"; |
60 | ||
771ab646 TC |
61 | my $image_dir = cfg_image_dir(); |
62 | opendir IMG, $image_dir | |
63 | or do { print "Cannot open $image_dir: $!\n"; exit }; | |
41b9d8ec TC |
64 | while (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 | ||
76 | print "\nDone\n"; | |
77 | ||
78 | __END__ | |
79 | ||
80 | =head1 NAME | |
81 | ||
82 | imageclean.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 |
90 | This will remove B<any> images in the configured managed images |
91 | directory that have names starting with 8 or more digits if | |
92 | they don't exist in the C<image> table as a record with a current | |
93 | article number. | |
41b9d8ec TC |
94 | |
95 | If you need image names of this form, put them elsewhere, or | |
771ab646 | 96 | reconfigure the managed images directory. |
41b9d8ec TC |
97 | |
98 | =head1 DESCRIPTION | |
99 | ||
100 | Scans the C<image> table looking for images that don't have an | |
101 | article, and for image files that don't have image records. | |
102 | ||
103 | The first is required due to a bug in older versions that left the | |
104 | image records around when deleting an article. It's also a recovery | |
105 | tool just in case the database loses referential integrity, since | |
106 | MySQL doesn't enforce it. | |
107 | ||
108 | The second is required for two reasons: | |
109 | ||
110 | =over | |
111 | ||
112 | =item | |
113 | ||
114 | older versions didn't remove the image files when images were removed | |
115 | ||
116 | =item | |
117 | ||
771ab646 TC |
118 | you may have deleted articles with images under an older version, |
119 | which would have left the image records (and the image files) | |
41b9d8ec TC |
120 | |
121 | =back | |
122 | ||
123 | =head1 AUTHOR | |
124 | ||
125 | Tony Cook <tony@develop-help.com> | |
126 | ||
127 | =cut |