aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/alaveteli_localization.rb21
-rw-r--r--lib/i18n_fixes.rb23
-rw-r--r--lib/mail_handler/backends/mail_extensions.rb20
-rw-r--r--lib/routing_filters.rb2
-rw-r--r--lib/tasks/submodules.rake28
-rw-r--r--lib/tasks/temp.rake55
6 files changed, 132 insertions, 17 deletions
diff --git a/lib/alaveteli_localization.rb b/lib/alaveteli_localization.rb
new file mode 100644
index 000000000..6daab124a
--- /dev/null
+++ b/lib/alaveteli_localization.rb
@@ -0,0 +1,21 @@
+class AlaveteliLocalization
+ class << self
+ def set_locales(available_locales, default_locale)
+ # fallback locale and available locales
+ available_locales = available_locales.split(/ /)
+ FastGettext.default_available_locales = available_locales
+ I18n.locale = default_locale
+ I18n.available_locales = available_locales.map { |locale_name| locale_name.to_sym }
+ I18n.default_locale = default_locale
+ end
+
+ def set_default_text_domain(name, path)
+ FastGettext.add_text_domain name, :path => path, :type => :po
+ FastGettext.default_text_domain = name
+ end
+
+ def set_default_locale_urls(include_default_locale_in_urls)
+ RoutingFilter::Locale.include_default_locale = include_default_locale_in_urls
+ end
+ end
+end
diff --git a/lib/i18n_fixes.rb b/lib/i18n_fixes.rb
index 82d1b2c3a..9c1206215 100644
--- a/lib/i18n_fixes.rb
+++ b/lib/i18n_fixes.rb
@@ -17,7 +17,7 @@ end
def n_(*keys)
# The last parameter should be the values to do the interpolation with
if keys.count > 3
- options = keys.pop
+ options = keys.pop
else
options = {}
end
@@ -33,7 +33,7 @@ def gettext_interpolate(string, values)
safe = string.html_safe?
string = string.to_str.gsub(MATCH) do
pattern, key = $1, $1.to_sym
-
+
if !values.include?(key)
raise I18n::MissingInterpolationArgument.new(pattern, string)
else
@@ -50,7 +50,7 @@ end
module I18n
- # used by Globalize plugin.
+ # used by Globalize plugin.
# XXX much of this stuff should (might?) be in newer versions of Rails
@@fallbacks = nil
class << self
@@ -120,7 +120,7 @@ module I18n
@defaults = defaults.map { |default| compute(default, false) }.flatten
end
attr_reader :defaults
-
+
def [](locale)
raise InvalidLocale.new(locale) if locale.nil?
locale = locale.to_sym
@@ -138,7 +138,7 @@ module I18n
end
protected
-
+
def compute(tags, include_defaults = true)
result = Array(tags).collect do |tag|
tags = I18n::Locale::Tag::Simple.tag(tag).self_and_parents.map! { |t| t.to_sym }
@@ -161,7 +161,18 @@ module GettextI18nRails
class Backend
def available_locales
FastGettext.available_locales.map{|l| l.to_sym} || []
- end
+ end
end
end
+# Monkeypatch Globalize to compensate for the way gettext_i18n_rails patches
+# I18n.locale= so that it changes underscores in locale names (as used in the gettext world)
+# to the dashes that I18n prefers
+module Globalize
+ class << self
+ def locale
+ read_locale || I18n.locale.to_s.gsub('-', '_').to_sym
+ end
+ end
+end
+
diff --git a/lib/mail_handler/backends/mail_extensions.rb b/lib/mail_handler/backends/mail_extensions.rb
index 87d8710f7..029331802 100644
--- a/lib/mail_handler/backends/mail_extensions.rb
+++ b/lib/mail_handler/backends/mail_extensions.rb
@@ -87,7 +87,10 @@ module Mail
# invalid character at the end of the string, even
# with UTF-8//IGNORE:
# http://po-ru.com/diary/fixing-invalid-utf-8-in-ruby-revisited/
- str = Iconv.conv('UTF-8//IGNORE', fix_encoding(encoding), str + " ")[0...-4]
+ begin
+ str = Iconv.conv('UTF-8//IGNORE', fix_encoding(encoding), str + " ")[0...-4]
+ rescue Iconv::InvalidEncoding
+ end
end
str
end
@@ -123,6 +126,21 @@ module Mail
end
class Ruby19
+ def Ruby19.b_value_decode(str)
+ match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
+ if match
+ encoding = match[1]
+ str = Ruby19.decode_base64(match[2])
+ # Rescue an ArgumentError arising from an unknown encoding.
+ begin
+ str.force_encoding(fix_encoding(encoding))
+ rescue ArgumentError
+ end
+ end
+ decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
+ decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
+ end
+
def Ruby19.q_value_decode(str)
match = str.match(/\=\?(.+)?\?[Qq]\?(.+)?\?\=/m)
if match
diff --git a/lib/routing_filters.rb b/lib/routing_filters.rb
index 32dafc651..a9a62b8db 100644
--- a/lib/routing_filters.rb
+++ b/lib/routing_filters.rb
@@ -7,7 +7,7 @@ module RoutingFilter
end
# And override the generation logic to use FastGettext.locale
# rather than I18n.locale (the latter is what rails uses
- # internally and may look like `en_US`, whereas the latter is
+ # internally and may look like `en-US`, whereas the latter is
# was FastGettext and other POSIX-based systems use, and will
# look like `en_US`
def around_generate(*args, &block)
diff --git a/lib/tasks/submodules.rake b/lib/tasks/submodules.rake
new file mode 100644
index 000000000..426192713
--- /dev/null
+++ b/lib/tasks/submodules.rake
@@ -0,0 +1,28 @@
+
+namespace :submodules do
+
+ desc "Check the status of the project's submodules"
+ task :check => :environment do
+ commit_info = `git submodule status commonlib`
+ case commit_info[0,1]
+ when '+'
+ $stderr.puts "Error: Currently checked out submodule commit for commonlib"
+ $stderr.puts "does not match the commit expected by this version of Alaveteli."
+ $stderr.puts "You can update it with 'git submodule update'."
+ exit(1)
+ when '-'
+ $stderr.puts "Error: Submodule commonlib needs to be initialized."
+ $stderr.puts "You can do this by running 'git submodule update --init'."
+ exit(1)
+ when 'U'
+ $stderr.puts "Error: Submodule commonlib has merge conflicts."
+ $stderr.puts "You'll need to resolve these to run Alaveteli."
+ exit(1)
+ when ' '
+ exit(0)
+ else
+ raise "Unexpected status character in response to 'git submodule status commonlib': #{commit_info[0,1]}"
+ end
+ end
+
+end
diff --git a/lib/tasks/temp.rake b/lib/tasks/temp.rake
index fcabb23de..f746338f0 100644
--- a/lib/tasks/temp.rake
+++ b/lib/tasks/temp.rake
@@ -1,5 +1,36 @@
namespace :temp do
+ desc "Fix the history of requests where the described state doesn't match the latest status value
+ used by search, by adding an edit event that will correct the latest status"
+ task :fix_bad_request_states => :environment do
+ dryrun = ENV['DRYRUN'] != '0'
+ if dryrun
+ puts "This is a dryrun"
+ end
+
+ InfoRequest.find_each() do |info_request|
+ next if info_request.url_title == 'holding_pen'
+ last_info_request_event = info_request.info_request_events[-1]
+ if last_info_request_event.latest_status != info_request.described_state
+ puts "#{info_request.id} #{info_request.url_title} #{last_info_request_event.latest_status} #{info_request.described_state}"
+ params = { :script => 'rake temp:fix_bad_request_states',
+ :user_id => nil,
+ :old_described_state => info_request.described_state,
+ :described_state => info_request.described_state
+ }
+ if ! dryrun
+ info_request.info_request_events.create!(:last_described_at => last_info_request_event.described_at + 1.second,
+ :event_type => 'status_update',
+ :described_state => info_request.described_state,
+ :calculated_state => info_request.described_state,
+ :params => params)
+ info_request.info_request_events.each{ |event| event.mark_needs_xapian_index }
+ end
+ end
+
+ end
+ end
+
def disable_duplicate_account(user, count, dryrun)
dupe_email = "duplicateemail#{count}@example.com"
puts "Updating #{user.email} to #{dupe_email} for user #{user.id}"
@@ -13,15 +44,21 @@ namespace :temp do
total_messages = 0
messages_to_reparse = 0
IncomingMessage.find_each :include => :foi_attachments do |im|
- reparse = im.foi_attachments.any? { |fa| ! File.exists? fa.filepath }
- total_messages += 1
- messages_to_reparse += 1 if reparse
- if total_messages % 1000 == 0
- puts "Considered #{total_messages} received emails."
- end
- unless dry_run
- im.parse_raw_email! true if reparse
- sleep 2
+ begin
+ reparse = im.foi_attachments.any? { |fa| ! File.exists? fa.filepath }
+ total_messages += 1
+ messages_to_reparse += 1 if reparse
+ if total_messages % 1000 == 0
+ puts "Considered #{total_messages} received emails."
+ end
+ unless dry_run
+ im.parse_raw_email! true if reparse
+ sleep 2
+ end
+ rescue StandardError => e
+ puts "There was a #{e.class} exception reparsing IncomingMessage with ID #{im.id}"
+ puts e.backtrace
+ puts e.message
end
end
message = dry_run ? "Would reparse" : "Reparsed"