-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPWImportService.lua
More file actions
1010 lines (919 loc) · 41 KB
/
PWImportService.lua
File metadata and controls
1010 lines (919 loc) · 41 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
PWImportService.lua
Copyright (C) 2026 Fiona Boston <fiona@fbphotography.uk>.
This file is part of PiwigoPublish
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
]]
---@diagnostic disable: undefined-global
local PWIMportService = {}
local SPECIAL_PREFIX = "※" -- U+203B Reference Mark used by another plugin to identify super collections
local PublishLocks = PublishLocks or {}
-- *************************************************
local function acquirePublishLock(serviceId)
while PublishLocks[serviceId] do
LrTasks.sleep(0.2)
end
PublishLocks[serviceId] = true
end
-- *************************************************
local function releasePublishLock(serviceId)
PublishLocks[serviceId] = false
end
-- *************************************************
-- Define a value_equal function for the popup_menu
local function valueEqual(a, b)
return a == b
end
-- *************************************************
local function buildPhotoSet(pubPhotos)
-- build set of photos for comparison
local set = {}
for _, pubPhoto in pairs(pubPhotos) do
set[pubPhoto:getPhoto()] = true
end
return set
end
-- *************************************************
local function setsEqual(a, b)
-- compare 2 sets and return true if they are equal
for k in pairs(a) do
if not b[k] then
return false
end
end
for k in pairs(b) do
if not a[k] then
return false
end
end
return true
end
-- *************************************************
local function isSpecialCollection(name)
return type(name) == "string"
and name:sub(1, #SPECIAL_PREFIX) == SPECIAL_PREFIX
end
-- *************************************************
local function buildChildrenIndex(collMap)
local index = {}
for _, node in pairs(collMap) do
local pid = node.parentId or "__root__"
index[pid] = index[pid] or {}
table.insert(index[pid], node)
end
return index
end
-- *************************************************
local function processSmartCollectionQueue(smartColls, publishService, propertyTable, index, progressScope, stats)
log:info("processSmartCollectionQueue - processing smart collection " .. index .. " of " .. #smartColls)
local catalog = LrApplication.activeCatalog()
local serviceId = publishService.localIdentifier
-- now get lock to prevent other publishNow processes running concurrently
acquirePublishLock(serviceId)
if not smartColls or index > #smartColls then
releasePublishLock(serviceId)
progressScope:done()
LrDialogs.message("Clone completed", "Publish Service Clone Complete.", "info")
return
end
local entry = smartColls[index]
if not entry then
releasePublishLock(serviceId)
progressScope:done()
LrDialogs.message("Clone completed", "Publish Service Clone Complete.", "info")
return -- done
end
local collection = entry.collection
local node = entry.node
local name = node.name
local extra = node.extra or nil
local remoteAlbumId = node.remoteId
local albumUrl = propertyTable.host .. "/index.php?/category/" .. remoteAlbumId
local oldPubPhotos
if extra then
oldPubPhotos = extra.pubphotos or {}
end
if #oldPubPhotos == 0 then
releasePublishLock(serviceId)
return
end
log:info("processSmartCollectionQueue - processing smart collection " ..
name .. " with " .. #oldPubPhotos .. " photos")
-- set flags to ensure correct render process runs
local collectionInfo = collection:getCollectionInfoSummary()
local collectionSettings = collectionInfo.collectionSettings
local publishSettings = collectionInfo.publishSettings
PWStatusManager.setisCloningSync(publishService, true)
local serviceState = PWStatusManager.getServiceState(publishService)
log:info("processSmartCollectionQueue - serviceState\n" .. utils.serialiseVar(serviceState))
-- we only care about oldPubPhotos that have a remote Id - i.e. have been uploaded to Piwigo
-- 1. Create a lookup table for the old published photos
-- store in PWStatusManager to ensure visibility in render process
local RemoteInfoTable = {}
local collId = collection.localIdentifier
log:info("processSmartCollectionQueue - building RemoteInfoTable for collection " ..
collection:getName() .. " (" .. collId .. ")")
for _, oldPubPhoto in ipairs(oldPubPhotos) do
local lrPhoto = oldPubPhoto:getPhoto()
local photoId = lrPhoto.localIdentifier
local remoteId = oldPubPhoto:getRemoteId() or ""
local remoteUrl = oldPubPhoto:getRemoteUrl() or ""
RemoteInfoTable[photoId] = RemoteInfoTable[photoId] or {}
RemoteInfoTable[photoId] = {
remoteId = remoteId,
remoteUrl = remoteUrl,
}
end
-- need to use local collectionInfo = publishedCollection:getCollectionInfoSummary()
-- to pass data to the publish process,
PWStatusManager.storeRemoteInfo(publishService, collId, RemoteInfoTable)
serviceState = PWStatusManager.getServiceState(publishService)
log:info("processSmartCollectionQueue - serviceState after RemoteInfoTable build\n" ..
utils.serialiseVar(serviceState))
-- add serviceState to collectionSettings to ensure it is passed to renderPhotos
collectionSettings.serviceState = serviceState
catalog:withWriteAccessDo("Add serviceState to collection", function()
collection:setCollectionSettings(collectionSettings)
end)
-- now force publish and subsequent render process
collection:publishNow(function(status)
log:info("smart collection - dummy publish complete for " .. collection:getName())
local lrPhotos = collection:getPhotos()
log:info("smart collection - there are " ..
(#collection:getPublishedPhotos() or 0) .. " published photos in the collection")
--PWStatusManager.setisCloningSync(publishService, false)
-- The images are now 'published' but were never actually uploaded - can now add remoteIds etc
for pp, thispubPhoto in pairs(collection:getPublishedPhotos()) do
log:info("smart collection " .. collection:getName() .. " - processing published photo " .. pp)
local thisLrPhoto = thispubPhoto:getPhoto()
local thisLrPhotoId = thisLrPhoto.localIdentifier
local remoteInfo = RemoteInfoTable[thisLrPhotoId]
if remoteInfo then
-- Found the old published photo
-- check image exists on Piwigo before setting metadata
local remoteId = remoteInfo.remoteId
local oldremoteUrl = remoteInfo.remoteUrl
local rtnStatus = PiwigoAPI.checkPhoto(propertyTable, remoteId)
if rtnStatus.status then
local imageDets = rtnStatus.imageDets
local remoteUrl = imageDets.page_url or oldremoteUrl
-- apply the metadata to the published photo
catalog:withWriteAccessDo("Add Piwigo details to image", function()
thispubPhoto:setRemoteId(remoteId)
thispubPhoto:setRemoteUrl(remoteUrl)
end)
local pluginData = {
pwHostURL = propertyTable.host,
albumName = name,
albumUrl = propertyTable.host .. "/index.php?/category/" .. remoteAlbumId,
imageUrl = remoteUrl,
pwUploadDate = os.date("%Y-%m-%d"),
pwUploadTime = os.date("%H:%M:%S"),
pwCommentSync = ""
}
PiwigoAPI.storeMetaData(catalog, thisLrPhoto, pluginData)
end
stats.imagesCloned = stats.imagesCloned + 1
progressScope:setPortionComplete(stats.imagesCloned, stats.images)
progressScope:setCaption("Cloning Images... " ..
stats.imagesCloned .. " of " .. stats.images .. " images")
end
end
-- now remove serviceeState from collectionSettings
collectionSettings.serviceState = nil
catalog:withWriteAccessDo("Add serviceState to collection", function()
collection:setCollectionSettings(collectionSettings)
end)
releasePublishLock(serviceId)
log:info("processSmartCollectionQueue - finished processing collection " .. collection:getName())
-- move to next smart collection
LrTasks.startAsyncTask(function()
processSmartCollectionQueue(smartColls, publishService, propertyTable, index + 1, progressScope, stats)
end)
end)
end
-- *************************************************
local function populateCollections(stdColls, smartColls, publishService, propertyTable, pwDetails, progressScope, stats)
-- add images to collections
local catalog = LrApplication.activeCatalog()
local serviceId = publishService.localIdentifier
if stdColls then
for sc, entry in pairs(stdColls) do
local collection = entry.collection
local node = entry.node
local name = node.name
local extra = node.extra or nil
local oldPubPhotos
local oldlrPhotos
local publishedIds
if extra then
oldPubPhotos = extra.pubphotos or {}
oldlrPhotos = extra.lrPhotos or {}
publishedIds = extra.publishedIds or {}
end
log:info("processing collection " .. name .. " with " .. #oldPubPhotos .. " photos")
local lrPhotosToAdd = {} -- photos to add without piwigo metadata
if #oldPubPhotos > 0 then
local albumUrl = collection:getRemoteUrl()
for pp, pubPhoto in pairs(oldPubPhotos) do
local lrPhoto = pubPhoto:getPhoto()
local photoId = lrPhoto.localIdentifier
if pwDetails.isPiwigo and pwDetails.isSameHost then
-- this is the same Piwigo host then we can copy remote ids etc
local pubFlag = false
local remoteId = pubPhoto:getRemoteId()
-- check remote id - does image exist on Piwigo
local rtnStatus = PiwigoAPI.checkPhoto(propertyTable, remoteId)
if rtnStatus.status and albumUrl then
-- album and photo exists on Piwigo - add and set metadata
local imageDets = rtnStatus.imageDets
-- image and album exists on Piwigo so set metadata
local remoteUrl = imageDets.page_url
pubFlag = true
catalog:withWriteAccessDo("Add Photo to collection", function()
collection:addPhotoByRemoteId(lrPhoto, remoteId, remoteUrl, pubFlag)
end)
-- now set metadata
local pluginData = {
pwHostURL = propertyTable.host,
albumName = name,
albumUrl = albumUrl,
imageUrl = remoteUrl,
pwUploadDate = os.date("%Y-%m-%d"),
pwUploadTime = os.date("%H:%M:%S"),
pwCommentSync = ""
}
PiwigoAPI.storeMetaData(catalog, lrPhoto, pluginData)
stats.imagesCloned = stats.imagesCloned + 1
progressScope:setPortionComplete(stats.imagesCloned, stats.images)
progressScope:setCaption("Cloning Images... " ..
stats.imagesCloned .. " of " .. stats.images .. " images")
else
-- album / photo don't exist on Piwigo
-- add photo but don't set and piwigo details
table.insert(lrPhotosToAdd, lrPhoto)
end
else
-- non piwigo or different piwigo host service being cloned - add photos but don't set any piwigo details
table.insert(lrPhotosToAdd, lrPhoto)
end
end
end
-- now add any remaining lrPhotos that were not published photos - from oldlrPhotos
for _, lrPhoto in pairs(oldlrPhotos) do
local photoId = lrPhoto.localIdentifier
if not publishedIds[photoId] then
table.insert(lrPhotosToAdd, lrPhoto)
end
end
if #lrPhotosToAdd > 0 then
catalog:withWriteAccessDo("Add Photos to collection", function()
collection:addPhotos(lrPhotosToAdd)
end)
stats.imagesCloned = stats.imagesCloned + #lrPhotosToAdd
progressScope:setPortionComplete(stats.imagesCloned, stats.images)
progressScope:setCaption("Cloning Images... " ..
stats.imagesCloned .. " of " .. stats.images .. " images")
end
end
end
-- now add Piwigo metadata to images in smartCollections
if #smartColls > 0 and pwDetails.isPiwigo and pwDetails.isSameHost then
processSmartCollectionQueue(smartColls, publishService, propertyTable, 1, progressScope, stats)
else
progressScope:done()
if pwDetails.isPiwigo and pwDetails.isSameHost then
LrDialogs.message("Clone completed", "Publish Service Clone Complete.", "info")
else
LrDialogs.message("Clone completed",
"Clone Complete. Check and Link Piwigo Structure will now be run", "info")
--LrTasks.startAsyncTask(function()
PiwigoAPI.validatePiwigoStructure(propertyTable)
--end)
end
end
end
-- *************************************************
local function createTree(nodes, parentSet, publishService, created, childrenIndex, statusData, propertyTable, pwDetails,
stdColls, smartColls, progressScope, stats)
-- nodes is a table of collection / sets details
local catalog = LrApplication.activeCatalog()
local serviceId = publishService.localIdentifier
for _, node in ipairs(nodes) do
local name = node.name
local extra = node.extra or nil
local isSpecialColl = false
-- Special collection renaming
if isSpecialCollection(node.name) and parentSet then
-- Use the parent collection/set name
name = "[Photos in " .. parentSet:getName() .. " ]"
isSpecialColl = true
-- ensure correct remoteId is used
end
local remoteAlbumId = node.remoteId
local comment = ""
local status = "public" -- ensure default is public
local isSmartColl = false
local searchDesc
if extra then
if extra.collSettings then
comment = extra.collSettings.comment or ""
status = extra.collSettings.status or "public"
end
isSmartColl = extra.isSmartColl
searchDesc = extra.searchDesc
end
local newCollorSet
if node.kind == "set" then
catalog:withWriteAccessDo("Create PublishedCollectionSet ", function()
newCollorSet = publishService:createPublishedCollectionSet(name, parentSet, true)
end)
if newCollorSet == nil then
LrErrors.throwUserError("Error in createCollection: Failed to create PublishedCollectionSet " ..
name .. " under parent " .. parentSet:getName())
return
end
created[node.id] = newCollorSet
-- now add remoteids and urls to collections and collection sets, and description and status
local albumUrl = ""
if pwDetails.isPiwigo then
local collectionSettings = newCollorSet:getCollectionSetInfoSummary().collectionSettings or {}
-- album settings set to correspond to service being cloned
collectionSettings.albumDescription = comment
if status == "private" then
collectionSettings.albumPrivate = true
else
collectionSettings.albumPrivate = false
end
if remoteAlbumId then
local thisCat = PiwigoAPI.pwCategoriesGetThis(propertyTable, remoteAlbumId)
if thisCat then
-- use settings directly from Piwigo, overriding local settings
collectionSettings.albumDescription = thisCat.description or ""
if thisCat.status == "public" then
collectionSettings.albumPrivate = false
else
collectionSettings.albumPrivate = true
end
albumUrl = propertyTable.host .. "/index.php?/category/" .. remoteAlbumId
catalog:withWriteAccessDo("Add Piwigo details to collections", function()
newCollorSet:setRemoteId(remoteAlbumId)
newCollorSet:setRemoteUrl(albumUrl)
newCollorSet:setCollectionSetSettings(collectionSettings)
end)
end
end
end
stats.collectionsCloned = stats.collectionsCloned + 1
progressScope:setPortionComplete(stats.collectionsCloned, stats.collections)
progressScope:setCaption("Cloning Collections... " ..
stats.collectionsCloned ..
" of " .. stats.collections + stats.collectionSets + stats.smartCollections .. " collections")
-- recurse into children (if any)
local children = childrenIndex[node.id]
if children then
createTree(children, newCollorSet, publishService, created, childrenIndex, statusData, propertyTable,
pwDetails, stdColls, smartColls, progressScope, stats)
end
elseif node.kind == "collection" then
if isSmartColl then
catalog:withWriteAccessDo("Create PublishedCollection ", function()
newCollorSet = publishService:createPublishedSmartCollection(name, searchDesc, parentSet, true)
end)
-- build table of smart collections for later processing
if pwDetails.isPiwigo and pwDetails.isSameHost then
table.insert(smartColls, {
collection = newCollorSet,
node = node,
})
end
else
if newCollorSet == nil then
catalog:withWriteAccessDo("Create PublishedCollection ", function()
newCollorSet = publishService:createPublishedCollection(name, parentSet, true)
end)
end
if newCollorSet == nil then
LrErrors.throwUserError("Error in createCollection: Failed to create PublishedCollection " ..
name .. " under parent " .. parentSet:getName())
return
end
-- build table of collections for later processing
table.insert(stdColls, {
collection = newCollorSet,
node = node,
})
end
created[node.id] = newCollorSet
-- now add remoteids and urls to collections and collection sets, and description and status
local albumUrl = ""
if pwDetails.isPiwigo and pwDetails.isSameHost then
-- this is the same Piwigo host then we can copy remote ids etc
local collectionSettings = newCollorSet:getCollectionInfoSummary().collectionSettings or {}
collectionSettings.albumDescription = comment
if status == "private" then
collectionSettings.albumPrivate = true
else
collectionSettings.albumPrivate = false
end
if remoteAlbumId then
-- check if remoote album exists and add to collection if so
local thisCat = PiwigoAPI.pwCategoriesGetThis(propertyTable, remoteAlbumId)
if thisCat then
collectionSettings.albumDescription = thisCat.description or ""
if thisCat.status == "public" then
collectionSettings.albumPrivate = false
else
collectionSettings.albumPrivate = true
end
albumUrl = propertyTable.host .. "/index.php?/category/" .. remoteAlbumId
catalog:withWriteAccessDo("Add Piwigo details to collections", function()
newCollorSet:setRemoteId(remoteAlbumId)
newCollorSet:setRemoteUrl(albumUrl)
newCollorSet:setCollectionSettings(collectionSettings)
end)
end
end
end
stats.collectionsCloned = stats.collectionsCloned + 1
progressScope:setPortionComplete(stats.collectionsCloned, stats.collections)
progressScope:setCaption("Cloning Collections... " ..
stats.collectionsCloned ..
" of " .. stats.collections + stats.collectionSets + stats.smartCollections .. " collections")
end
end
end
-- *************************************************
local function importService(propertyTable, thisService, impService, serviceIndex, pwDetails, stats)
-- build service
local catalog = LrApplication.activeCatalog()
local collMap = serviceIndex
local childrenIndex = buildChildrenIndex(serviceIndex)
local created = {}
log:info("importService - importing " .. impService:getName() .. " to " .. thisService:getName())
log:info("pwDetails - " .. utils.serialiseVar(pwDetails))
--
local statusData = {
existing = 0,
collectionSets = 0,
collections = 0,
errors = 0,
maxDepth = 0
}
-- start at roots (parentId == nil)
local nodes = childrenIndex["__root__"] or {}
local parentSet = nil
local smartColls = {}
local stdColls = {}
local progressScope = LrProgressScope {
title = "Cloning Collections...",
caption = "Starting...",
functionContext = context,
}
stats.collectionsCloned = 0
-- build collection structure
createTree(nodes, parentSet, thisService, created, childrenIndex, statusData, propertyTable, pwDetails, stdColls,
smartColls, progressScope, stats)
progressScope:done()
stats.imagesCloned = 0
progressScope = LrProgressScope {
title = "Cloning Images...",
caption = "Starting...",
functionContext = context,
}
-- add images
populateCollections(stdColls, smartColls, thisService, propertyTable, pwDetails, progressScope, stats)
return true
end
-- *************************************************
local function importServicePrelim(propertyTable, thisService, impService)
-- check and verify selected service
local catalog = LrApplication.activeCatalog()
local thisHost = propertyTable.host
local thisUser = propertyTable.userName
-- Check attributes and configuration of impService
local publishSettings = impService:getPublishSettings()
-- check for specific fields to make sure this is a Piwigo related service
-- we will clone non-Piwigo services but only try and populate Piwigo specific data if it is a Piwigo related service
local pwDetails = {
isPiwigo = false,
isSameHost = false,
user = "",
host = "",
}
if publishSettings.LR_exportServiceProviderTitle == "Piwigo" then
-- this is a legacy Piwigo Publish service
pwDetails.isPiwigo = true
pwDetails.host = publishSettings.MM_ServerUrl
if string.sub(pwDetails.host, -1) == "/" then
if pwDetails.host == propertyTable.host .. "/" then
pwDetails.isSameHost = true
end
else
if pwDetails.host == propertyTable.host then
pwDetails.isSameHost = true
end
end
pwDetails.user = publishSettings.AP_authUser
end
if publishSettings.LR_exportServiceProviderTitle == "Piwigo Publisher" then
-- this is another instance of this service
pwDetails.isPiwigo = true
pwDetails.host = publishSettings.host
if pwDetails.host == propertyTable.host then
pwDetails.isSameHost = true
end
pwDetails.user = publishSettings.userName
end
local indexByPath, indexById = PiwigoAPI.buildNormalisedCollectionTables(impService)
local stats = {
collections = 0,
smartCollections = 0,
remColls = 0,
pwColls = 0,
collectionSets = 0,
images = 0,
remImages = 0,
pwImages = 0,
unpublishedCollImages = 0,
unpublishedSmartCollImages = 0,
}
local canClone = true
for id, collDets in pairs(indexById) do
local extraCollDets = {}
local thisColl = catalog:getPublishedCollectionByLocalIdentifier(id)
if thisColl then
local colType = thisColl:type()
local info = {}
local pubphotos = nil
local lrPhotos = nil
local isSmartColl = false
local searchDesc = {}
if colType == "LrPublishedCollection" then
info = thisColl:getCollectionInfoSummary()
pubphotos = thisColl:getPublishedPhotos()
lrPhotos = thisColl:getPhotos()
-- check for smart collection
isSmartColl = thisColl:isSmartCollection()
if isSmartColl then
-- count number of lrPhotos in collection
-- a different to number of published photos indicates un published photos
-- which means we cannot proceed with cloning
local numlrPhotos = #lrPhotos
local numPubPhotos = #pubphotos
if numlrPhotos > numPubPhotos then
stats.unpublishedSmartCollImages = stats.unpublishedSmartCollImages +
(numlrPhotos - numPubPhotos)
canClone = false
end
searchDesc = thisColl:getSearchDescription()
stats.smartCollections = stats.smartCollections + 1
else
local numlrPhotos = #thisColl:getPhotos()
local numPubPhotos = #thisColl:getPublishedPhotos()
if numlrPhotos > numPubPhotos then
stats.unpublishedCollImages = stats.unpublishedCollImages + (numlrPhotos - numPubPhotos)
end
stats.collections = stats.collections + 1
end
elseif colType == "LrPublishedCollectionSet" then
info = thisColl:getCollectionSetInfoSummary()
stats.collectionSets = stats.collectionSets + 1
end
local parentColl = thisColl:getParent()
local remoteId = thisColl:getRemoteId() or ""
local remoteUrl = thisColl:getRemoteUrl() or ""
local collSettings
local pubSettings
if info then
collSettings = info.collectionSettings
pubSettings = info.publishSettings
end
local publishedIds = {}
if pubphotos then
for pp, pubPhoto in pairs(pubphotos) do
stats.images = stats.images + 1
local lrPhoto = pubPhoto:getPhoto()
publishedIds[lrPhoto.localIdentifier] = true
local fileName = ""
local remId = pubPhoto:getRemoteId() or ""
local remUrl = pubPhoto:getRemoteUrl() or ""
if remId ~= "" then
stats.pwImages = stats.pwImages + 1
else
if isSmartColl then
stats.unpublishedSmartCollImages = stats.unpublishedSmartCollImages + 1
end
end
if lrPhoto then
fileName = lrPhoto:getFormattedMetadata("fileName")
end
end
end
extraCollDets.parentColl = parentColl
extraCollDets.remoteId = remoteId
extraCollDets.remoteUrl = remoteUrl
extraCollDets.isSmartColl = isSmartColl
extraCollDets.searchDesc = searchDesc
extraCollDets.collSettings = collSettings
extraCollDets.lrPhotos = lrPhotos
extraCollDets.pubphotos = pubphotos
extraCollDets.publishedIds = publishedIds
collDets.extra = extraCollDets
end
end
-- display dialog to confirm details before proceeding
local text1 = "Cloning from " .. impService:getName() .. " (" .. impService:getPluginId() .. ")"
local textTo = thisService:getName() .. " Publish Service"
local text2 = ""
local text3 = ""
local text4 = ""
local text5 = ""
if pwDetails.isPiwigo then
if pwDetails.isSameHost then
text2 = "The service being cloned is a Piwigo service connected to the same Piwigo host as this service."
if not canClone then
text3 =
"** The service being cloned has smart collections with unpublished images - please fix before cloning **"
if stats.unpublishedSmartCollImages > 0 then
text5 = "*** Unpublished Smart Collection Images : " .. stats.unpublishedSmartCollImages .. " ***"
end
else
text3 = "Collections/Sets will be cloned as will links to Piwigo albums and images if present"
if stats.unpublishedCollImages > 0 then
text4 = "*** Unpublished Collection Images : " .. stats.unpublishedCollImages .. " ***"
else
text4 = ""
end
text5 = ""
end
else
text2 =
"The service being cloned is a Piwigo service but connected to a different Piwigo host."
text3 = "Collections/Sets and images will be cloned but links to Piwigo will not"
end
else
text2 = "The service being cloned is not a Piwigo service."
text3 = "Collections/Sets and images will be cloned but no links to Piwigo albums or images will be made"
end
local f = LrView.osFactory()
local c = f:column {
spacing = f:dialog_spacing(),
f:row {
f:column {
spacing = f:control_spacing(),
f:spacer { height = 3 },
f:row {
f:static_text {
title = "Clone : ",
font = "<system>",
alignment = 'right',
height_in_lines = 1,
},
--},
--f:row {
f:static_text {
title = impService:getName() .. " (" .. impService:getPluginId() .. ")",
font = "<system/bold>",
alignment = 'left',
height_in_lines = 1,
},
},
f:row {
f:static_text {
title = " to : ",
font = "<system>",
alignment = 'right',
height_in_lines = 1,
},
--},
--f:row {
f:static_text {
title = thisService:getName(),
font = "<system/bold>",
alignment = 'left',
height_in_lines = 1,
},
},
f:spacer { height = 3 },
f:row {
f:static_text {
title = text2,
font = "<system/bold>",
alignment = 'left',
fill_horizontal = 1,
height_in_lines = 1,
},
},
f:spacer { height = 1 },
f:row {
f:static_text {
title = text3,
font = "<system/bold>",
alignment = 'left',
fill_horizontal = 1,
height_in_lines = 1,
},
},
-- display details to be cloned
f:row {
f:static_text {
alignment = 'center',
title = "Collection Sets to clone : " .. stats.collectionSets,
font = "<system>",
fill_horizontal = 1,
}
},
f:row {
f:static_text {
alignment = 'center',
title = "Collections to clone : " .. stats.collections,
font = "<system>",
fill_horizontal = 1,
}
},
f:row {
f:static_text {
alignment = 'center',
title = "Smart Collections to clone : " .. stats.smartCollections,
font = "<system>",
fill_horizontal = 1,
}
},
f:row {
f:static_text {
title = "Images to clone : " .. stats.images,
font = "<system>",
alignment = 'center',
fill_horizontal = 1,
}
},
f:row {
f:static_text {
title = text4,
font = "<system/bold>",
alignment = 'center',
fill_horizontal = 1,
}
},
f:row {
f:static_text {
title = text5,
font = "<system/bold>",
alignment = 'center',
fill_horizontal = 1,
}
},
},
},
}
if canClone then
local dialog = LrDialogs.presentModalDialog({
title = "Confirm details of Publish Service to be cloned",
contents = c,
actionVerb = "Clone",
cancelVerb = "Cancel",
})
if dialog == "ok" then
local rv = importService(propertyTable, thisService, impService, indexById, pwDetails, stats)
end
else
local dialog = LrDialogs.presentModalDialog({
title = "Details of Publish Service to be cloned",
contents = c,
actionVerb = "Cancel Clone",
cancelVerb = "< exclude >",
})
end
end
-- *************************************************
function PWIMportService.selectService(propertyTable)
-- start of clone service function
local foundService, thisService = PiwigoAPI.getPublishService(propertyTable)
if not foundService or not thisService then
log:info("PWIMportService.selectService - cannot find this Publish Service")
return false
end
local catalog = LrApplication.activeCatalog()
-- check if current service has any published collections/sets - exit if so
local childColls = thisService:getChildCollections() or nil
local childCollSets = thisService:getChildCollectionSets() or nil
if not (utils.nilOrEmpty(childColls)) or not (utils.nilOrEmpty(childCollSets)) then
--if thisService:getChildCollections() or thisService:getChildCollectionSets() then
LrDialogs.message("Error", "Cannot clone into this service as it already contains Published Collections or Sets.",
"info")
return
end
LrFunctionContext.callWithContext("PWImportServiceContext", function(context)
-- Create a property table inside the context
local allServices = catalog:getPublishServices() or {}
if #allServices == 0 then
LrDialogs.message("No publish services found.")
return
end
local serviceItems = {}
local serviceNames = {}
-- build list of publish services excluding this one
local availableServices = {}
for i, s in pairs(allServices) do
if s:getName() ~= thisService:getName() then
table.insert(availableServices, s)
end
end
for i, s in ipairs(availableServices) do
table.insert(serviceItems, {
title = s:getName(),
value = s,
})
table.insert(serviceNames, {
title = s:getName(),
value = i
})
end
local props = LrBinding.makePropertyTable(context)
local bind = LrView.bind
props = bind {
selectedService = 1, -- default to first service
}
local f = LrView.osFactory()
local c = f:column {
spacing = f:dialog_spacing(),
f:row {
-- TOP: icon + version block
f:picture {
alignment = 'left',
value = iconPath,
},
f:column {
spacing = f:control_spacing(),
f:spacer { height = 3 },
f:row {
f:static_text {
title = " Clone selected Publish Service to " .. thisService:getName() .. " Publish Service",
font = "<system/bold>",
alignment = 'left',
fill_horizontal = 1,
height_in_lines = 2,
},
},
},
},
f:row {
spacing = f:label_spacing(),
f:spacer { height = 3 },
f:static_text {
title = " ",
alignment = 'left',
},
f:static_text {
title = "Select Publish Service to clone:",
alignment = 'right',
width = 200,
},
f:popup_menu {
value = LrView.bind { key = 'selectedService', bind_to_object = props },
items = serviceNames,
value_equal = valueEqual,
width = 250,
},
},
f:row {
f:spacer { height = 2 },
f:static_text {
title = " Please ensure selected service is up to date - i.e. with no outstanding photographs to be published",
alignment = 'left',
},
}
}
local dialog = LrDialogs.presentModalDialog({
title = "Piwigo Publisher - Clone Existing Publish Service",
contents = c,
actionVerb = "Next",
cancelVerb = "Cancel",
})
if dialog == "ok" then
-- get the actual service object
local serviceNo = props.selectedService
-- get service object for selected service
local selService = serviceItems[serviceNo].value
if not selService then
LrDialogs.message("Error", "Could not find publish service", "error")
return