plegall / menalto2piwigo

9 stars 7 forks source link

avoid tag duplicates #7

Closed plegall closed 9 years ago

plegall commented 9 years ago

If the photo in Piwigo already has a tag, no need to create a new tag from Menalto.

rorrison commented 9 years ago

I'm looking into this, in hopes of coming up with a fix despite my minimal php experience. I've just noticed that every time I do an import from gallery3 all of the tags from gallery3 are added to the piwigo_tags table -- regardless of whether they're already there, and regardless of whether they're used in the photos that are imported. Specifically, I'm testing with a single photo that has a single tag, in a test album in both gallery3 and piwigo, although my gallery3 installation has many more albums and photos. Every time I run the Menalto2Piwigo import, 425 more entries are created in the piwigo_tags table. The result is that the test photo ends up with multiple tags that look identical, but actually have different IDs. I would say that inserting all the tags from the Menalto database isn't a big problem, but inserting them even if they already exist is.

rorrison commented 9 years ago

Right, I think I've got a fix for this. I haven't figured out git yet, so can I just post a diff here?

There are two parts:

First ignore new tags that are already in the piwigo_tags table. I figured out how to do this in the gallery3 code, then went to apply it to the new gallery2 code, and it appears to already be there. The fix is to not add the tag to $tag_inserts if it's already there, and don't call mass_inserts if $tags_inserts is empty.

Second, in the call to mass_inserts which adds the new tags to the IMAGE_TAG_TABLE, add a final array( 'ignore' => true ) parameter which prevents duplicate tags from being added to the image. This needs to be done for both the gallery2 and gallery3 sections. (I haven't tested it in the gallery2 version.)

Here's the diff:

506c506,507
<           $image_tag_inserts
---
>           $image_tag_inserts,
>           array( 'ignore' => true )  // ignore duplicated tags added to item
665a667,669
>       // Don't add the tag if it's already in Piwigo
>       if (!isset($piwigo_tags[$row['name']]))
>       {
669a674
>       }
723a729,730
>     if (isset($tag_inserts) and count($tag_inserts) > 0)
>     {
728a736
>     }
763c771,772
<         $image_tag_inserts
---
>         $image_tag_inserts,
>         array( 'ignore' => true )  // ignore duplicated tags added to item

Should I figure out git and do it properly? (Of course I should figure out git and do it properly at some point.)

rorrison commented 9 years ago

My laptop battery is about to die... That's my first pull request ever, so let me know if I've done anything wrong!

plegall commented 9 years ago

Thank you @rorrison your pull-request was absolutely perfect (I'm not a Github Guru yet...). I'm releasing version 2.7.d now :-)