]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Merge pull request #3139 from Kdecherf/2502-tag-case
authorJérémy Benoist <j0k3r@users.noreply.github.com>
Sun, 3 Sep 2017 18:25:15 +0000 (20:25 +0200)
committerGitHub <noreply@github.com>
Sun, 3 Sep 2017 18:25:15 +0000 (20:25 +0200)
Ignore tag's case

app/DoctrineMigrations/Version20170719231144.php [new file with mode: 0644]
src/Wallabag/CoreBundle/Entity/Tag.php
src/Wallabag/CoreBundle/Helper/TagsAssigner.php
tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
tests/Wallabag/ImportBundle/Controller/PinboardControllerTest.php
tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php

diff --git a/app/DoctrineMigrations/Version20170719231144.php b/app/DoctrineMigrations/Version20170719231144.php
new file mode 100644 (file)
index 0000000..0f5fa75
--- /dev/null
@@ -0,0 +1,103 @@
+<?php
+
+namespace Application\Migrations;
+
+use Doctrine\DBAL\Migrations\AbstractMigration;
+use Doctrine\DBAL\Schema\Schema;
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * Changed tags to lowercase.
+ */
+class Version20170719231144 extends AbstractMigration implements ContainerAwareInterface
+{
+    /**
+     * @var ContainerInterface
+     */
+    private $container;
+
+    public function setContainer(ContainerInterface $container = null)
+    {
+        $this->container = $container;
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function up(Schema $schema)
+    {
+        $this->skipIf($this->connection->getDatabasePlatform()->getName() === 'sqlite', 'Migration can only be executed safely on \'mysql\' or \'postgresql\'.');
+
+        // Find tags which need to be merged
+        $dupTags = $this->connection->query('
+            SELECT LOWER(label)
+            FROM   ' . $this->getTable('tag') . '
+            GROUP BY LOWER(label)
+            HAVING COUNT(*) > 1'
+        );
+        $dupTags->execute();
+
+        foreach ($dupTags->fetchAll() as $duplicates) {
+            $label = $duplicates['LOWER(label)'];
+
+            // Retrieve all duplicate tags for a given tag
+            $tags = $this->connection->query('
+                SELECT id
+                FROM   ' . $this->getTable('tag') . "
+                WHERE  LOWER(label) = '" . $label . "'
+                ORDER BY id ASC"
+            );
+            $tags->execute();
+
+            $first = true;
+            $newId = null;
+            $ids = [];
+
+            foreach ($tags->fetchAll() as $tag) {
+                // Ignore the first tag as we use it as the new reference tag
+                if ($first) {
+                    $first = false;
+                    $newId = $tag['id'];
+                } else {
+                    $ids[] = $tag['id'];
+                }
+            }
+
+            // Just in case...
+            if (count($ids) > 0) {
+                // Merge tags
+                $this->addSql('
+                    UPDATE ' . $this->getTable('entry_tag') . '
+                    SET    tag_id = ' . $newId . '
+                    WHERE  tag_id IN (' . implode(',', $ids) . ')'
+                );
+
+                // Delete unused tags
+                $this->addSql('
+                    DELETE FROM ' . $this->getTable('tag') . '
+                    WHERE id IN (' . implode(',', $ids) . ')'
+                );
+            }
+        }
+
+        // Iterate over all tags to lowercase them
+        $this->addSql('
+            UPDATE ' . $this->getTable('tag') . '
+            SET label = LOWER(label)'
+        );
+    }
+
+    /**
+     * @param Schema $schema
+     */
+    public function down(Schema $schema)
+    {
+        throw new SkipMigrationException('Too complex ...');
+    }
+
+    private function getTable($tableName)
+    {
+        return $this->container->getParameter('database_table_prefix') . $tableName;
+    }
+}
index c19023af5bffdd8af1175840bea9b15a522a5251..a6dc8c50902ed26f3acb89a65b5dca8d4af073db 100644 (file)
@@ -78,7 +78,7 @@ class Tag
      */
     public function setLabel($label)
     {
-        $this->label = $label;
+        $this->label = mb_convert_case($label, MB_CASE_LOWER);
 
         return $this;
     }
index a2fb0b9a940c25588ec7d588b0ee0922daccb47d..0bfe5c572a5e1d72d338433ad81630c596755114 100644 (file)
@@ -45,7 +45,7 @@ class TagsAssigner
         }
 
         foreach ($tags as $label) {
-            $label = trim($label);
+            $label = trim(mb_convert_case($label, MB_CASE_LOWER));
 
             // avoid empty tag
             if (0 === strlen($label)) {
index be25a8b5e1c91863947795f799417e248e15b81c..5a973a7e0409988ff306e9ee1848ccf2c1c26a81 100644 (file)
@@ -9,6 +9,7 @@ use Wallabag\CoreBundle\Entity\Tag;
 class TagControllerTest extends WallabagCoreTestCase
 {
     public $tagName = 'opensource';
+    public $caseTagName = 'OpenSource';
 
     public function testList()
     {
@@ -36,7 +37,7 @@ class TagControllerTest extends WallabagCoreTestCase
         $form = $crawler->filter('form[name=tag]')->form();
 
         $data = [
-            'tag[label]' => $this->tagName,
+            'tag[label]' => $this->caseTagName,
         ];
 
         $client->submit($form, $data);
@@ -45,6 +46,7 @@ class TagControllerTest extends WallabagCoreTestCase
         // be sure to reload the entry
         $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
         $this->assertCount(1, $entry->getTags());
+        $this->assertContains($this->tagName, $entry->getTags());
 
         // tag already exists and already assigned
         $client->submit($form, $data);
@@ -80,7 +82,7 @@ class TagControllerTest extends WallabagCoreTestCase
         $form = $crawler->filter('form[name=tag]')->form();
 
         $data = [
-            'tag[label]' => 'foo2, bar2',
+            'tag[label]' => 'foo2, Bar2',
         ];
 
         $client->submit($form, $data);
index c307f96c94be3b61f34752b9033fb0e42546bab5..9bb597664de95e959f21dd1db5e32213cbaf1bb1 100644 (file)
@@ -125,7 +125,7 @@ class PinboardControllerTest extends WallabagCoreTestCase
         $tags = $content->getTags();
         $this->assertContains('foot', $tags, 'It includes the "foot" tag');
         $this->assertContains('varnish', $tags, 'It includes the "varnish" tag');
-        $this->assertContains('PHP', $tags, 'It includes the "PHP" tag');
+        $this->assertContains('php', $tags, 'It includes the "php" tag');
         $this->assertSame(3, count($tags));
 
         $this->assertInstanceOf(\DateTime::class, $content->getCreatedAt());
index 25625c35e0eda6bc8f429c0736fa9d2a64156142..4bc982e0ae977c774b8ab259e7b2fa81f1c76e57 100644 (file)
@@ -125,7 +125,7 @@ class WallabagV1ControllerTest extends WallabagCoreTestCase
 
         $tags = $content->getTags();
         $this->assertContains('foot', $tags, 'It includes the "foot" tag');
-        $this->assertContains('Framabag', $tags, 'It includes the "Framabag" tag');
+        $this->assertContains('framabag', $tags, 'It includes the "framabag" tag');
         $this->assertSame(2, count($tags));
 
         $this->assertInstanceOf(\DateTime::class, $content->getCreatedAt());