/
UpdateAttachmentsPlugin.pm
360 lines (297 loc) · 12.1 KB
/
UpdateAttachmentsPlugin.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# copyright 2007-2009 SvenDowideit@fosiki.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html
package Foswiki::Plugins::UpdateAttachmentsPlugin;
use strict;
our $VERSION = '$Rev$';
our $RELEASE = '2.0';
our $SHORTDESCRIPTION =
'A batched alternative to AutoAttachments (adds and removes attachements)';
our $NO_PREFS_IN_TOPIC = 1;
my $debug = 0;
sub initPlugin {
my ( $topic, $web, $user, $installWeb ) = @_;
# check for Plugins.pm versions
if ( $Foswiki::Plugins::VERSION < 2.1 ) {
Foswiki::Func::writeWarning(
"Version mismatch between UpdateAttachmentsPlugin and Plugins.pm");
return 0;
}
$debug = $Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{Debug} || 0;
Foswiki::Func::registerRESTHandler( 'update', \&restUpdate );
return 1;
}
sub restUpdate {
my $session = shift;
my $web = $session->{webName};
#force autoattach off, as we need to know the real META
my $cfgAutoAttach = $Foswiki::cfg{AutoAttachPubFiles};
$Foswiki::cfg{AutoAttachPubFiles} = 0;
#TODO: consider the lighter weight aproach of scanning timestamps in the pub dir
# checking&updating only those topics that have newer timestamp pub files
# (it would only helps adding attachments, not removing them)
my $topicsTested = 0;
my $topicsUpdated = 0;
my $attachmentsIgnored = 0;
my $attachmentsRemoved = 0;
my $attachmentsAdded = 0;
my $attachmentsUpdated = 0;
my $detailedReport = '';
my $webObject = Foswiki::Meta->new( $session, $web );
unless ($webObject->haveAccess('VIEW') && $webObject->haveAccess('CHANGE') ) {
print STDERR "Check for VIEW and CHANGE on $web web failed\n" if $debug;
$webObject->finish();
return "Access denied on $web web. UpdateAttachments not possible\n";
}
if ($Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{CheckUPDATEATACHPermission}) {
unless ( $webObject->haveAccess('UPDATEATTACH') ) {
print STDERR "Check for UPDATEATTACH on $web web failed\n" if $debug;
$webObject->finish();
return "UPDATEATTACH permission denied on $web web. UpdateAttachments not possible\n";
}
}
my @topicNames = Foswiki::Func::getTopicList($web);
foreach my $topic (@topicNames) {
print STDERR "=============== Processing $topic in $web ==============\n" if $debug;
my $changed = 0;
my $topicObject = Foswiki::Meta->new( $session, $web, $topic );
# Change topic context so topic can override attributes of attachments in their settings
Foswiki::Func::pushTopicContext($web, $topic);
if (!$topicObject->haveAccess('VIEW')) {
$detailedReport .=
"bypassed $web.$topic - no permission to VIEW <br/>\n";
$topicObject->finish();
next;
}
if (!$topicObject->haveAccess('CHANGE')) {
$detailedReport .=
"bypassed $web.$topic - no permission to CHANGE <br/>\n";
$topicObject->finish();
next;
}
$topicObject->loadVersion();
my @knownAttachments = $topicObject->find('FILEATTACHMENT');
my (
$attachmentsFoundInPub, $attachmentsRemovedFromMeta,
$attachmentsAddedToMeta, $attachmentsUpdatedInMeta
) = synchroniseAttachmentsList( $topicObject, \@knownAttachments );
Foswiki::Func::popTopicContext();
# @validAttachmentsFound will contain the replacment attachment Metadata
my @validAttachmentsFound;
foreach my $foundAttachment (@$attachmentsFoundInPub) {
# test if the attachment filename is valid without having to
# be sanitized. If not, ignore it.
my $validated = Foswiki::Sandbox::validateAttachmentName(
$foundAttachment->{name} );
unless ( defined $validated
&& $validated eq $foundAttachment->{name} )
{
$detailedReport .=
'AutoAttachPubFiles ignoring '
. $foundAttachment->{name} . ' in '
. $topicObject->getPath()
. ' - not a valid Foswiki Attachment filename';
}
else {
push @validAttachmentsFound, $foundAttachment;
}
}
$topicObject->putAll( 'FILEATTACHMENT', @validAttachmentsFound )
if @validAttachmentsFound;
#TODO: actually test that they are the same! (update size, date etc)
print STDERR "$web.$topic has "
. scalar(@knownAttachments)
. " in meta, "
. scalar(@$attachmentsFoundInPub)
. " in pub dir\n"
if $debug;
$changed =
scalar(@$attachmentsRemovedFromMeta) +
scalar(@$attachmentsAddedToMeta) +
scalar(@$attachmentsUpdatedInMeta);
if ($changed) {
$topicObject->save( comment => 'UpdateAttachments' );
print STDERR "updating the attachments of $web.$topic\n" if $debug;
$topicsUpdated++;
$attachmentsRemoved += scalar(@$attachmentsRemovedFromMeta);
$attachmentsAdded += scalar(@$attachmentsAddedToMeta);
$attachmentsUpdated += scalar(@$attachmentsUpdatedInMeta);
$detailedReport .= "Updating $web.$topic <br/>\n";
foreach my $attach (@$attachmentsRemovedFromMeta) {
$detailedReport .= "Removed $attach <br/>";
}
foreach my $attach (@$attachmentsAddedToMeta) {
$detailedReport .= "Added $attach <br/>";
}
foreach my $attach (@$attachmentsUpdatedInMeta) {
$detailedReport .= "Updated $attach <br/>";
}
}
$topicsTested++;
$topicObject->finish();
#TODO: $Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{BatchLimit}
}
$webObject->finish();
print STDERR
"UpdateAttachments checked $topicsTested, updated $topicsUpdated, removed $attachmentsRemoved attachments, $attachmentsIgnored ignored"
if $debug;
# Restore auto-attach setting. (This *really* ought to be disabled if using this plugin
$Foswiki::cfg{AutoAttachPubFiles} = $cfgAutoAttach;
return <<HERE
UpdateAttachments Topics checked $topicsTested, updated $topicsUpdated, <br/>
Attachments updated $attachmentsUpdated, added $attachmentsAdded, removed $attachmentsRemoved, ignored $attachmentsIgnored <br/><br/>
$detailedReport
HERE
}
=begin TML
---++ ObjectMethod synchroniseAttachmentsList(\@old) -> @new
*Copied directly from Foswiki::Store::VC::Handler.pm*
Synchronise the attachment list from meta-data with what's actually
stored in the DB. Returns an ARRAY of FILEATTACHMENTs. These can be
put in the new tom.
=cut
# IDEA On Windows machines where the underlying filesystem can store arbitary
# meta data against files, this might replace/fulfil the COMMENT purpose
sub synchroniseAttachmentsList {
my ( $topicObject, $attachmentsKnownInMeta ) = @_;
my %filesListedInPub = _getAttachmentStats($topicObject);
my %filesListedInMeta = ();
my %filesToAddToMeta = ();
my @filesRemovedFromMeta = ();
my @filesAddedToMeta = ();
my @filesUpdatedInMeta = ();
# You need the following lines if you want metadata to supplement
# the filesystem
if ( defined $attachmentsKnownInMeta ) {
%filesListedInMeta =
map { $_->{name} => $_ } @$attachmentsKnownInMeta;
}
foreach my $file ( keys %filesListedInPub ) {
if ( $filesListedInMeta{$file} ) {
if ( $filesListedInMeta{$file}{size} ne
$filesListedInPub{$file}{size}
|| $filesListedInMeta{$file}{date} ne
$filesListedInPub{$file}{date} )
{
$filesListedInPub{$file}{autoattached} = "1";
push @filesUpdatedInMeta, $file;
print STDERR "Updating $file \n" if $debug;
}
# Bring forward any missing yet wanted attribute
foreach my $field qw(comment attr user version autoattached) {
if ( $filesListedInMeta{$file}{$field} ) {
$filesListedInPub{$file}{$field} =
$filesListedInMeta{$file}{$field};
}
}
}
else {
#default attachment owner to {AttachAsUser}
push @filesAddedToMeta, $file;
print STDERR "Adding $file \n" if $debug;
$filesListedInPub{$file}{autoattached} = "1";
if (
(
defined(
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}
{AttachAsUser}
)
)
&& ( $Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}
{AttachAsUser} ne '' )
)
{
$filesListedInPub{$file}{user} =
Foswiki::Func::getCanonicalUserID(
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}
{AttachAsUser} );
}
}
}
# A comparison of the keys of the $filesListedInMeta and %filesListedInPub
# would show files that were in Meta but have disappeared from Pub.
foreach my $file ( keys %filesListedInMeta ) {
if ( !$filesListedInPub{$file} ) {
if (
$Foswiki::cfg{Plugins}{UpdateAttachmentsPlugin}{RemoveMissing} )
{
print STDERR "Removing $file \n" if $debug;
push @filesRemovedFromMeta, $file;
}
else {
$filesListedInPub{$file} = $filesListedInMeta{$file};
print STDERR "Retained missing attachment $file\n" if $debug;
}
}
}
# Do not change this from array to hash, you would lose the
# proper attachment sequence
my @deindexedBecauseMetaDoesnotIndexAttachments = values(%filesListedInPub);
return \@deindexedBecauseMetaDoesnotIndexAttachments,
\@filesRemovedFromMeta, \@filesAddedToMeta, \@filesUpdatedInMeta;
}
=begin TML
---++ ObjectMethod getAttachmentList() -> @list
Get list of attachment names actually stored for topic.
=cut
sub getAttachmentList {
my $topicObject = shift;
my $dir =
"$Foswiki::cfg{PubDir}/"
. $topicObject->web() . "/"
. $topicObject->topic();
my $dh;
opendir( $dh, $dir ) || return ();
my @files = grep { !/^[.*_]/ && !/,v$/ } readdir($dh);
closedir($dh);
return @files;
}
# returns {} of filename => { key => value, key2 => value }
# for any given web, topic
sub _getAttachmentStats {
my $topicObject = shift;
my %attachmentList = ();
my $dir =
"$Foswiki::cfg{PubDir}/"
. $topicObject->web() . '/'
. $topicObject->topic();
foreach my $attachment ( getAttachmentList($topicObject) ) {
my @stat = stat( $dir . "/" . $attachment );
$attachmentList{$attachment} =
_constructAttributesForAutoAttached( $attachment, \@stat );
}
return %attachmentList;
}
# as long as stat is defined, return an emulated set of attributes for that
# attachment.
sub _constructAttributesForAutoAttached {
my ( $file, $stat ) = @_;
my %pairs = (
name => $file,
version => '',
path => $file,
size => $stat->[7],
date => $stat->[9],
# user => 'UnknownUser', #safer _not_ to default - Foswiki will fill it in when it needs to
comment => '',
attr => '',
);
if ( $#$stat > 0 ) {
return \%pairs;
}
else {
return;
}
}
1;