aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/alaveteli_external_command.rb2
-rw-r--r--lib/public_body_categories.rb29
-rw-r--r--lib/quiet_opener.rb4
-rw-r--r--lib/rack_quote_monkeypatch.rb4
-rw-r--r--lib/tasks/temp.rake49
-rw-r--r--lib/tasks/themes.rake87
6 files changed, 155 insertions, 20 deletions
diff --git a/lib/alaveteli_external_command.rb b/lib/alaveteli_external_command.rb
index b1d4f17d1..7d32be904 100644
--- a/lib/alaveteli_external_command.rb
+++ b/lib/alaveteli_external_command.rb
@@ -30,7 +30,7 @@ module AlaveteliExternalCommand
if opts.has_key? :append_to
xc.out = opts[:append_to]
end
- xc.run(opts[:stdin_string], opts[:env] || {})
+ xc.run(opts[:stdin_string] || "", opts[:env] || {})
if xc.status != 0
# Error
$stderr.puts("Error from #{program_name} #{args.join(' ')}:")
diff --git a/lib/public_body_categories.rb b/lib/public_body_categories.rb
index b8058bf9e..b4aa71a40 100644
--- a/lib/public_body_categories.rb
+++ b/lib/public_body_categories.rb
@@ -7,9 +7,15 @@
# $Id: public_body_categories.rb,v 1.1 2009-09-14 14:45:48 francis Exp $
class PublicBodyCategories
-
- attr_reader :with_description, :with_headings, :tags, :by_tag, :singular_by_tag
-
+
+ attr_reader :with_description,
+ :with_headings,
+ :tags,
+ :by_tag,
+ :singular_by_tag,
+ :by_heading,
+ :headings
+
def initialize(categories)
@with_headings = categories
# Arranged in different ways for different sorts of displaying
@@ -17,8 +23,21 @@ class PublicBodyCategories
@tags = @with_description.map() { |a| a[0] }
@by_tag = Hash[*@with_description.map() { |a| a[0..1] }.flatten]
@singular_by_tag = Hash[*@with_description.map() { |a| [a[0],a[2]] }.flatten]
+ @by_heading = {}
+ heading = nil
+ @headings = []
+ @with_headings.each do |row|
+ if ! row.instance_of?(Array)
+ heading = row
+ @headings << row
+ @by_heading[row] = []
+ else
+ @by_heading[heading] << row[0]
+ end
+ end
end
+
def PublicBodyCategories.get
load_categories if @@CATEGORIES.empty?
@@CATEGORIES[I18n.locale.to_s] || @@CATEGORIES[I18n.default_locale.to_s] || PublicBodyCategories.new([])
@@ -28,10 +47,10 @@ class PublicBodyCategories
def PublicBodyCategories.add(locale, categories)
@@CATEGORIES[locale.to_s] = PublicBodyCategories.new(categories)
end
-
+
private
@@CATEGORIES = {}
-
+
def PublicBodyCategories.load_categories()
I18n.available_locales.each do |locale|
begin
diff --git a/lib/quiet_opener.rb b/lib/quiet_opener.rb
index 8cedad250..bde645d0b 100644
--- a/lib/quiet_opener.rb
+++ b/lib/quiet_opener.rb
@@ -5,7 +5,7 @@ require 'net/http/local'
def quietly_try_to_open(url)
begin
result = open(url).read.strip
- rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
+ rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET
Rails.logger.warn("Unable to open third-party URL #{url}")
result = ""
end
@@ -24,7 +24,7 @@ def quietly_try_to_purge(host, url)
result_body = response.body
}
end
- rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
+ rescue OpenURI::HTTPError, SocketError, Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET
Rails.logger.warn("PURGE: Unable to reach host #{host}")
end
if result == "200"
diff --git a/lib/rack_quote_monkeypatch.rb b/lib/rack_quote_monkeypatch.rb
index be079c6a2..b477ac0cb 100644
--- a/lib/rack_quote_monkeypatch.rb
+++ b/lib/rack_quote_monkeypatch.rb
@@ -2,9 +2,9 @@
# current version of Rails won't use that. So for now, monkeypatch,
# This can be dropped when we move to Rails 3.
#
-# See https://github.com/sebbacon/alaveteli/issues/38 for Alaveteli
+# See https://github.com/mysociety/alaveteli/issues/38 for Alaveteli
# bug report
-#
+#
# More info about the monkeypatch:
# http://thewebfellas.com/blog/2010/7/15/rails-2-3-8-rack-1-1-and-the-curious-case-of-the-missing-quotes
diff --git a/lib/tasks/temp.rake b/lib/tasks/temp.rake
index ce04c7ddd..9decc13db 100644
--- a/lib/tasks/temp.rake
+++ b/lib/tasks/temp.rake
@@ -1,15 +1,44 @@
namespace :temp do
- desc "Remove plaintext passwords from post_redirect params"
- task :remove_post_redirect_passwords => :environment do
- PostRedirect.find_each(:conditions => ['post_params_yaml is not null']) do |post_redirect|
- if post_redirect.post_params && post_redirect.post_params[:signchangeemail] && post_redirect.post_params[:signchangeemail][:password]
- params = post_redirect.post_params
- params[:signchangeemail].delete(:password)
- post_redirect.post_params = params
- post_redirect.save!
- end
+ desc "Remove plaintext passwords from post_redirect params"
+ task :remove_post_redirect_passwords => :environment do
+ PostRedirect.find_each(:conditions => ['post_params_yaml is not null']) do |post_redirect|
+ if post_redirect.post_params && post_redirect.post_params[:signchangeemail] && post_redirect.post_params[:signchangeemail][:password]
+ params = post_redirect.post_params
+ params[:signchangeemail].delete(:password)
+ post_redirect.post_params = params
+ post_redirect.save!
+ end
+ end
+ end
+
+ desc 'Remove file caches for requests that are not publicly visible or have been destroyed'
+ task :remove_obsolete_info_request_caches => :environment do
+ dryrun = ENV['DRYRUN'] == '0' ? false : true
+ verbose = ENV['VERBOSE'] == '0' ? false : true
+ if dryrun
+ puts "Running in dryrun mode"
+ end
+ request_cache_path = File.join(Rails.root, 'cache', 'views', 'request', '*', '*')
+ Dir.glob(request_cache_path) do |request_subdir|
+ info_request_id = File.basename(request_subdir)
+ puts "Looking for InfoRequest with id #{info_request_id}" if verbose
+ begin
+ info_request = InfoRequest.find(info_request_id)
+ puts "Got InfoRequest #{info_request_id}" if verbose
+ if ! info_request.all_can_view?
+ puts "Deleting cache at #{request_subdir} for hidden/requester_only InfoRequest #{info_request_id}"
+ if ! dryrun
+ FileUtils.rm_rf(request_subdir)
+ end
+ end
+ rescue ActiveRecord::RecordNotFound
+ puts "Deleting cache at #{request_subdir} for deleted InfoRequest #{info_request_id}"
+ if ! dryrun
+ FileUtils.rm_rf(request_subdir)
+ end
+ end
+ end
end
- end
end
diff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rake
new file mode 100644
index 000000000..2b1dbb3a9
--- /dev/null
+++ b/lib/tasks/themes.rake
@@ -0,0 +1,87 @@
+
+namespace :themes do
+
+ def plugin_dir
+ File.join(Rails.root,"vendor","plugins")
+ end
+
+ def theme_dir(theme_name)
+ File.join(plugin_dir, theme_name)
+ end
+
+ def install_theme_using_git(name, uri, verbose=false, options={})
+ mkdir_p(install_path = theme_dir(name))
+ Dir.chdir install_path do
+ init_cmd = "git init"
+ init_cmd += " -q" if options[:quiet] and not verbose
+ puts init_cmd if verbose
+ system(init_cmd)
+ base_cmd = "git pull --depth 1 #{uri}"
+ # Is there a tag for this version of Alaveteli?
+ usage_tag = "use-with-alaveteli-#{ALAVETELI_VERSION}"
+ # Query the remote repository passing flags for tags
+ version_tag = `git ls-remote --tags #{uri} #{usage_tag}`
+ if /^[a-z0-9]+\s+refs\/tags\/#{Regexp.escape(usage_tag)}$/.match(version_tag)
+ # If we got a tag, pull that instead of HEAD
+ puts "Using tag #{usage_tag}" if verbose
+ base_cmd += " refs/tags/#{usage_tag}"
+ else
+ puts "No specific tag for this version: using HEAD" if verbose
+ end
+ base_cmd += " -q" if options[:quiet] and not verbose
+ puts base_cmd if verbose
+ if system(base_cmd)
+ puts "removing: .git .gitignore" if verbose
+ rm_rf %w(.git .gitignore)
+ else
+ rm_rf install_path
+ raise "#{base_cmd} failed! Stopping."
+ end
+ end
+ end
+
+ def uninstall(theme_name, verbose=false)
+ dir = theme_dir(theme_name)
+ if File.directory?(dir)
+ run_hook(theme_name, 'uninstall', verbose)
+ puts "Removing '#{dir}'" if verbose
+ rm_r dir
+ else
+ puts "Plugin doesn't exist: #{dir}"
+ end
+ end
+
+ def run_hook(theme_name, hook_name, verbose=false)
+ hook_file = File.join(theme_dir(theme_name), "#{hook_name}.rb")
+ if File.exist? hook_file
+ puts "Running #{hook_name} hook for #{theme_name}" if verbose
+ load hook_file
+ end
+ end
+
+ def installed?(theme_name)
+ File.directory?(theme_dir(theme_name))
+ end
+
+ def install_theme(theme_url, verbose, deprecated=false)
+ deprecation_string = deprecated ? " using deprecated THEME_URL" : ""
+ theme_name = File.basename(theme_url, '.git')
+ puts "Installing theme #{theme_name}#{deprecation_string} from #{theme_url}"
+ uninstall(theme_name, verbose) if installed?(theme_name)
+ install_theme_using_git(theme_name, theme_url, verbose)
+ run_hook(theme_name, 'install', verbose)
+ run_hook(theme_name, 'post_install', verbose)
+ end
+
+ desc "Install themes specified in the config file's THEME_URLS"
+ task :install => :environment do
+ verbose = true
+ theme_urls = MySociety::Config.get("THEME_URLS", [])
+ theme_urls.each{ |theme_url| install_theme(theme_url, verbose) }
+ theme_url = MySociety::Config.get("THEME_URL", "")
+ if ! theme_url.blank?
+ # Old version of the above, for backwards compatibility
+ install_theme(theme_url, verbose, deprecated=true)
+ end
+ end
+end \ No newline at end of file