From e2c43fef802dbdc7ab046bf5c5cc6fd61eb5d634 Mon Sep 17 00:00:00 2001 From: shibafu Date: Thu, 30 Jul 2020 22:42:10 +0900 Subject: [PATCH] tags.normalized_name --- app/Console/Commands/NormalizeTags.php | 19 ++++++++--- app/Tag.php | 10 ++++++ app/Utilities/Formatter.php | 11 +++---- ..._30_221302_add_normalized_name_to_tags.php | 32 +++++++++++++++++++ 4 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 database/migrations/2020_07_30_221302_add_normalized_name_to_tags.php diff --git a/app/Console/Commands/NormalizeTags.php b/app/Console/Commands/NormalizeTags.php index 4e9d8ed..9d6cf30 100644 --- a/app/Console/Commands/NormalizeTags.php +++ b/app/Console/Commands/NormalizeTags.php @@ -4,6 +4,7 @@ namespace App\Console\Commands; use App\Tag; use App\Utilities\Formatter; +use DB; use Illuminate\Console\Command; class NormalizeTags extends Command @@ -42,9 +43,19 @@ class NormalizeTags extends Command */ public function handle() { - foreach (Tag::query()->orderBy('name')->cursor() as $tag) { - $normalizedName = $this->formatter->normalizeToSearchIndex($tag->name); - $this->line("{$tag->name} : {$normalizedName}"); - } + $start = hrtime(true); + + DB::transaction(function () { + /** @var Tag $tag */ + foreach (Tag::query()->cursor() as $tag) { + $normalizedName = $this->formatter->normalizeTagName($tag->name); + $this->line("{$tag->name} : {$normalizedName}"); + $tag->normalized_name = $normalizedName; + $tag->save(); + } + }); + + $elapsed = (hrtime(true) - $start) / 1e+9; + $this->info("Done! ({$elapsed} sec)"); } } diff --git a/app/Tag.php b/app/Tag.php index df057f5..895492e 100644 --- a/app/Tag.php +++ b/app/Tag.php @@ -2,6 +2,7 @@ namespace App; +use App\Utilities\Formatter; use Illuminate\Database\Eloquent\Model; class Tag extends Model @@ -15,6 +16,15 @@ class Tag extends Model 'name' ]; + protected static function boot() + { + parent::boot(); + + self::creating(function (Tag $tag) { + $tag->normalized_name = app(Formatter::class)->normalizeTagName($tag->name); + }); + } + public function ejaculations() { return $this->belongsToMany('App\Ejaculation')->withTimestamps(); diff --git a/app/Utilities/Formatter.php b/app/Utilities/Formatter.php index aa09c78..f15901a 100644 --- a/app/Utilities/Formatter.php +++ b/app/Utilities/Formatter.php @@ -133,14 +133,11 @@ class Formatter return $bytes . 'B'; } - public function normalizeToSearchIndex(string $text): string + public function normalizeTagName(string $name) { - $text = \Normalizer::normalize($text, \Normalizer::FORM_KC); -// $text = \Transliterator::create('Katakana-Hiragana')->transliterate($text); - $text = mb_convert_kana($text, 'c'); - $text = preg_replace('/[^\p{L}\p{N}]/u', '', $text); - $text = mb_strtolower($text); + $name = \Normalizer::normalize($name, \Normalizer::FORM_KC); + $name = mb_strtolower($name); - return $text; + return $name; } } diff --git a/database/migrations/2020_07_30_221302_add_normalized_name_to_tags.php b/database/migrations/2020_07_30_221302_add_normalized_name_to_tags.php new file mode 100644 index 0000000..eead2a0 --- /dev/null +++ b/database/migrations/2020_07_30_221302_add_normalized_name_to_tags.php @@ -0,0 +1,32 @@ +string('normalized_name')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('tags', function (Blueprint $table) { + $table->dropColumn('normalized_name'); + }); + } +}