From 9f22ae781f98f9be35cb17396f2cc713e1179b0d Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 27 Aug 2020 16:09:59 +0100 Subject: [PATCH] Move commonlib function into lib There seems to be an issue with `MySociety::Format.simplify_url_part` but we're not getting a full stack trace due to the source being outside of app/lib. This change should hopefully give us more information to allow us to debug this issue. See: https://github.com/mysociety/alaveteli/issues/5870 https://github.com/mysociety/alaveteli/issues/5871 https://github.com/mysociety/alaveteli/issues/5873 https://github.com/mysociety/alaveteli/issues/5874 https://github.com/mysociety/alaveteli/issues/5876 https://github.com/mysociety/alaveteli/issues/5877 https://github.com/mysociety/alaveteli/issues/5878 https://github.com/mysociety/alaveteli/issues/5879 --- app/controllers/public_body_controller.rb | 4 +-- app/controllers/user_controller.rb | 4 +-- app/helpers/download_helper.rb | 2 +- .../concerns/public_body_derived_fields.rb | 2 +- app/models/info_request.rb | 2 +- app/models/info_request/sluggable.rb | 2 +- app/models/user.rb | 2 +- config/initializers/alaveteli.rb | 1 + lib/alaveteli_format.rb | 35 +++++++++++++++++++ lib/tasks/import.rake | 2 +- 10 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 lib/alaveteli_format.rb diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb index bc88a12aea0..59434865a21 100644 --- a/app/controllers/public_body_controller.rb +++ b/app/controllers/public_body_controller.rb @@ -21,8 +21,8 @@ def show raise ActiveRecord::RecordNotFound.new("Sorry. No pages after #{MAX_RESULTS / requests_per_page}.") end - if MySociety::Format.simplify_url_part(params[:url_name], 'body') != params[:url_name] - redirect_to :url_name => MySociety::Format.simplify_url_part(params[:url_name], 'body'), :status => :moved_permanently + if Alaveteli::Format.simplify_url_part(params[:url_name], 'body') != params[:url_name] + redirect_to :url_name => Alaveteli::Format.simplify_url_part(params[:url_name], 'body'), :status => :moved_permanently return end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index cc572555c4c..599c6ba4c1e 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -380,8 +380,8 @@ def set_in_pro_area end def normalize_url_name - unless MySociety::Format.simplify_url_part(params[:url_name], 'user') == params[:url_name] - redirect_to :url_name => MySociety::Format.simplify_url_part(params[:url_name], 'user'), :status => :moved_permanently + unless Alaveteli::Format.simplify_url_part(params[:url_name], 'user') == params[:url_name] + redirect_to :url_name => Alaveteli::Format.simplify_url_part(params[:url_name], 'user'), :status => :moved_permanently end end diff --git a/app/helpers/download_helper.rb b/app/helpers/download_helper.rb index e96e007e4b6..bacbf8b47da 100644 --- a/app/helpers/download_helper.rb +++ b/app/helpers/download_helper.rb @@ -3,7 +3,7 @@ # module DownloadHelper def generate_download_filename(resource:, id:, title:, type: nil, ext:) - url_title = MySociety::Format.simplify_url_part( + url_title = Alaveteli::Format.simplify_url_part( title, resource, 32 ) url_title += "-#{type}" if type diff --git a/app/models/concerns/public_body_derived_fields.rb b/app/models/concerns/public_body_derived_fields.rb index b05b573cfad..caeb32f8903 100644 --- a/app/models/concerns/public_body_derived_fields.rb +++ b/app/models/concerns/public_body_derived_fields.rb @@ -41,7 +41,7 @@ def set_first_letter def update_url_name if changed.include?('name') || changed.include?('short_name') - self.url_name = MySociety::Format. + self.url_name = Alaveteli::Format. simplify_url_part(short_or_long_name, 'body') end end diff --git a/app/models/info_request.rb b/app/models/info_request.rb index c3494e7246c..33464a29e82 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -766,7 +766,7 @@ def user_name_slug if external_user_name.nil? fake_slug = "anonymous" else - fake_slug = MySociety::Format.simplify_url_part(external_user_name, 'external_user', 32) + fake_slug = Alaveteli::Format.simplify_url_part(external_user_name, 'external_user', 32) end (public_body.url_name || "") + "_" + fake_slug else diff --git a/app/models/info_request/sluggable.rb b/app/models/info_request/sluggable.rb index d54d82872c4..794c7065a8d 100644 --- a/app/models/info_request/sluggable.rb +++ b/app/models/info_request/sluggable.rb @@ -35,7 +35,7 @@ def url_title(opts = {}) def update_url_title return unless title - url_title = MySociety::Format.simplify_url_part(title, 'request', 32) + url_title = Alaveteli::Format.simplify_url_part(title, 'request', 32) suffix = suffix_number(url_title) unique_url_title = suffix ? "#{url_title}_#{suffix}" : url_title diff --git a/app/models/user.rb b/app/models/user.rb index df4e9895319..6f3c16a83fc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -380,7 +380,7 @@ def name=(name) end def update_url_name - url_name = MySociety::Format.simplify_url_part(read_attribute(:name), 'user', 32) + url_name = Alaveteli::Format.simplify_url_part(read_attribute(:name), 'user', 32) # For user with same name as others, add on arbitary numeric identifier unique_url_name = url_name suffix_num = 2 # as there's already one without numeric suffix diff --git a/config/initializers/alaveteli.rb b/config/initializers/alaveteli.rb index c3166404490..1a5efbc6b6b 100644 --- a/config/initializers/alaveteli.rb +++ b/config/initializers/alaveteli.rb @@ -66,6 +66,7 @@ require 'safe_redirect' require 'alaveteli_pro/metrics_report' require 'alaveteli_pro/webhook_endpoints' +require 'alaveteli_format' AlaveteliLocalization.set_locales(AlaveteliConfiguration::available_locales, AlaveteliConfiguration::default_locale) diff --git a/lib/alaveteli_format.rb b/lib/alaveteli_format.rb new file mode 100644 index 00000000000..176db663082 --- /dev/null +++ b/lib/alaveteli_format.rb @@ -0,0 +1,35 @@ +require 'cgi' +require 'unicode' +require 'unidecoder' + +module Alaveteli + module Format + # Simplified a name to something usable in a URL + def self.simplify_url_part(text, default_name, max_len = nil) + text = text.downcase # this also clones the string, if we use downcase! we modify the original + text = Unicode.normalize_KD(text) + text = text.to_ascii.downcase + + text.gsub!(/(\s|-|_)/, "_") + text.gsub!(/[^a-z0-9_]/, "") + text.gsub!(/_+/, "_") + text.gsub!(/^_*/, "") + text.gsub!(/_*$/, "") + + # If required, trim down to size + if not max_len.nil? + if text.size > max_len + text = text[0..(max_len-1)] + end + # removing trailing _ + text.gsub!(/_*$/, "") + end + # Don't allow short (zero length!), or all numeric (clashes with identifiers) + if text.size < 1 || text.match(/^[0-9]+$/) + text = default_name # just do "user_1", "user_2" etc. + end + + text + end + end +end diff --git a/lib/tasks/import.rake b/lib/tasks/import.rake index 51c8216e75e..5a1d30045a2 100644 --- a/lib/tasks/import.rake +++ b/lib/tasks/import.rake @@ -31,7 +31,7 @@ namespace :import do reader.each do |row_array| row = Hash[headers.zip row_array] name = row['name'] - url_part = MySociety::Format.simplify_url_part(name, 'body') + url_part = Alaveteli::Format.simplify_url_part(name, 'body') name_count[name] += 1 url_part_count[url_part] += 1 number_of_rows += 1