From 6b4ac0c042a0dd01b11c32584e81aa070b95258a Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 11 Oct 2019 16:48:01 -0500 Subject: [PATCH] wiki pages: fix title normalization. Fix inconsistent title normalization. Strip whitespace and underscores from the ends of the title and strip consecutive underscores. --- app/models/wiki_page.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index afe64d5ec..5e1ddd79a 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -22,7 +22,7 @@ class WikiPage < ApplicationRecord module SearchMethods def titled(title) - where("title = ?", title.mb_chars.downcase.tr(" ", "_")) + where(title: normalize_title(title)) end def active @@ -59,7 +59,7 @@ class WikiPage < ApplicationRecord q = q.text_attribute_matches(:body, params[:body_matches], index_column: :body_index, ts_config: "danbooru") if params[:title].present? - q = q.where("title LIKE ? ESCAPE E'\\\\'", params[:title].mb_chars.downcase.strip.tr(" ", "_").to_escaped_for_sql_like) + q = q.where_like(:title, normalize_title(params[:title])) end if params[:other_names_match].present? @@ -127,8 +127,12 @@ class WikiPage < ApplicationRecord save! end + def self.normalize_title(title) + title.downcase.gsub(/[[:space:]]+/, "_").gsub(/__/, "_").gsub(/\A_|_\z/, "") + end + def normalize_title - self.title = title.mb_chars.downcase.tr(" ", "_") + self.title = WikiPage.normalize_title(title) end def normalize_other_names