aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/general_controller.rb
blob: 3bf113efb0c8911cea73a3c51f0a1c8ac2afae53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# app/controllers/general_controller.rb:
# For pages like front page, general search, that aren't specific to a
# particular model.
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: general_controller.rb,v 1.57 2009-10-03 10:23:43 francis Exp $

require 'lib/xmlsimple'
require 'open-uri'

class GeneralController < ApplicationController

    # New, improved front page!
    def frontpage
        # This is too slow
        #@popular_bodies = PublicBody.find(:all, :select => "*, (select count(*) from info_requests where info_requests.public_body_id = public_bodies.id) as c", :order => "c desc", :limit => 32)

        # Just hardcode some popular authorities for now
        # ('tgq', 'atbra' is for debugging on Francis's development environment)
        @popular_bodies = PublicBody.find(:all, :conditions => ["url_name in (
              'bbc', 
              'dwp', 
              'dh', 
              'snh',
              'royal_mail_group', 
              'mod', 
              'kent_county_council', 
              'wirral_borough_council'
              /* , 'tgq', 'atbra' */
        )"]).sort_by { |pb| pb.url_name }.reverse # just an order that looks better

        # Get some successful requests #
        begin
            query = 'variety:response (status:successful OR status:partially_successful)'
            # query = 'variety:response' # XXX debug
            sortby = "described"
            @xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_title_collapse', 8)
            @successful_request_events = @xapian_object.results.map { |r| r[:model] }
            @successful_request_events = @successful_request_events.sort_by { |e| e.described_at }.reverse
        rescue
            @successful_request_events = []
        end

        cache_in_squid
    end

    # Display WhatDoTheyKnow category from mySociety blog
    def blog
        feed_url = 'http://www.mysociety.org/category/projects/whatdotheyknow/feed/'
        content = open(feed_url).read
        @data = XmlSimple.xml_in(content)
        @channel = @data['channel'][0]
        @items = @channel['item']

        @feed_autodetect = [ { :url => feed_url, :title => "WhatDoTheyKnow blog"} ]

        twitter_url = 'http://api.twitter.com/1/statuses/user_timeline/whatdotheyknow.rss' # @whatdotheyknow
        content = open(twitter_url).read
        @data = XmlSimple.xml_in(content)
        @channel = @data['channel'][0]
        @items = @channel['item'] + @items

        @feed_autodetect += [ { :url => twitter_url, :title => "WhatDoTheyKnow tweets"} ]

        @items.sort! { |a,b| Time.parse(b['pubDate'][0]) <=> Time.parse(a['pubDate'][0]) }
    end

    # Just does a redirect from ?query= search to /query
    def search_redirect
        @query = params[:query]
        @sortby = params[:sortby]
        @bodies = params[:bodies]
        if @query.nil? || @query.empty?
            @query = nil
            @page = 1
            render :action => "search"
        else
            if (@bodies == '1') && (@sortby.nil? || @sortby.empty?)
                @postfix = 'bodies'
            else
                @postfix = @sortby
            end
            redirect_to search_url(@query, @postfix)
        end
    end

    # Actual search
    def search
        # XXX Why is this so complicated with arrays and stuff? Look at the route
        # in config/routes.rb for comments.
        combined = params[:combined]
        @sortby = nil
        @bodies = false # searching from front page, largely for a public authority
        # XXX currently /described isn't linked to anywhere, just used in RSS and for /list/successful
        # This is because it's confusingly different from /newest - but still useful for power users.
        if combined.size > 1 && (['newest', 'described', 'bodies', 'relevant'].include?(combined[-1]))
            @postfix = combined[-1]
            combined = combined[0..-2]
            if @postfix == 'bodies'
                @bodies = true
            else
                @sortby = @postfix
            end
        end
        @query = combined.join("/")

        @inputted_sortby = @sortby
        if @sortby.nil?
            # Parse query, so can work out if it has prefix terms only - if so then it is a
            # structured query which should show newest first, rather than a free text search
            # where we want most relevant as default.
            begin
                dummy_query = ::ActsAsXapian::Search.new([InfoRequestEvent], @query, :limit => 1)
            rescue => e
                flash[:error] = "Your query was not quite right. " + CGI.escapeHTML(e.to_str)
                redirect_to search_url("")
                return
            end
            if dummy_query.has_normal_search_terms?
                @sortby = 'relevant'
            else
                @sortby = 'newest'
            end
        end

        # Query each type separately for separate display (XXX we are calling
        # perform_search multiple times and it clobbers per_page for each one,
        # so set as separate var)
        requests_per_page = 25
        if params[:requests_per_page]
            requests_per_page = params[:requests_per_page].to_i
        end
        @xapian_requests = perform_search([InfoRequestEvent], @query, @sortby, 'request_collapse', requests_per_page)
        @requests_per_page = @per_page
        @xapian_bodies = perform_search([PublicBody], @query, @sortby, nil, 5)
        @bodies_per_page = @per_page
        @xapian_users = perform_search([User], @query, @sortby, nil, 5)
        @users_per_page = @per_page

        @this_page_hits = @xapian_requests.results.size + @xapian_bodies.results.size + @xapian_users.results.size
        @total_hits = @xapian_requests.matches_estimated + @xapian_bodies.matches_estimated + @xapian_users.matches_estimated

        # Spelling and highight words are same for all three queries
        @spelling_correction = @xapian_requests.spelling_correction
        @highlight_words = @xapian_requests.words_to_highlight

        @track_thing = TrackThing.create_track_for_search_query(@query)
        @feed_autodetect = [ { :url => do_track_url(@track_thing, 'feed'), :title => @track_thing.params[:title_in_rss] } ]
    end

    # Jump to a random request
    def random_request
        info_request = InfoRequest.random
        redirect_to request_url(info_request)
    end

    # For debugging
    def fai_test
        sleep 10
        render :text => "awake\n"
    end

end
 
s="nf">expand_and_normalize_parts(part, parent_mail) if part.multipart? part.parts.each{ |sub_part| expand_and_normalize_parts(sub_part, parent_mail) } else part_filename = get_part_file_name(part) charset = part.charset # save this, because overwriting content_type also resets charset # Don't allow nil content_types if get_content_type(part).nil? part.content_type = 'application/octet-stream' end # PDFs often come with this mime type, fix it up for view code if get_content_type(part) == 'application/octet-stream' part_body = get_part_body(part) calc_mime = AlaveteliFileTypes.filename_and_content_to_mimetype(part_filename, part_body) if calc_mime part.content_type = calc_mime end end # Use standard content types for Word documents etc. part.content_type = normalise_content_type(get_content_type(part)) decode_attached_part(part, parent_mail) part.charset = charset end end # Count the parts in a mail part recursively, including any attached messages. # Set the count on the parent mail, and set a url_part_number on the part itself. # Set the count for the first uudecoded part on the parent mail also. def count_parts(part, parent_mail) if part.multipart? part.parts.each { |p| count_parts(p, parent_mail) } else if part.rfc822_attachment count_parts(part.rfc822_attachment, parent_mail) else parent_mail.count_parts_count += 1 part.url_part_number = parent_mail.count_parts_count end end parent_mail.count_first_uudecode_count = parent_mail.count_parts_count end # Choose the best part from alternatives def choose_best_alternative(mail) if mail.html_part return mail.html_part elsif mail.text_part return mail.text_part else return mail.parts.first end end # Expand and normalize the parts of a mail, select the best part # wherever there is an alternative, and then count the returned # leaves and assign url_part values to them def get_attachment_leaves(mail) expand_and_normalize_parts(mail, mail) leaves = _get_attachment_leaves_recursive(mail, nil, mail) mail.count_parts_count = 0 count_parts(mail, mail) return leaves end # Recurse through a mail part, selecting the best part wherever there is # an alternative def _get_attachment_leaves_recursive(part, within_rfc822_attachment, parent_mail) leaves_found = [] if part.multipart? raise "no parts on multipart mail" if part.parts.size == 0 if part.sub_type == 'alternative' best_part = choose_best_alternative(part) leaves_found += _get_attachment_leaves_recursive(best_part, within_rfc822_attachment, parent_mail) else # Add all parts part.parts.each do |sub_part| leaves_found += _get_attachment_leaves_recursive(sub_part, within_rfc822_attachment, parent_mail) end end else # Add all the parts of a decoded attached message if part.rfc822_attachment leaves_found += _get_attachment_leaves_recursive(part.rfc822_attachment, part.rfc822_attachment, parent_mail) else # Store leaf part.within_rfc822_attachment = within_rfc822_attachment leaves_found += [part] end end return leaves_found end # Add selected useful headers from an attached message to its body def extract_attached_message_headers(leaf) body = get_part_body(leaf) # Test to see if we are in the first part of the attached # RFC822 message and it is text, if so add headers. if leaf.within_rfc822_attachment == leaf && get_content_type(leaf) == 'text/plain' headers = "" [ 'Date', 'Subject', 'From', 'To', 'Cc' ].each do |header| if header_value = get_header_string(header, leaf.within_rfc822_attachment) if !header_value.blank? headers = headers + header + ": " + header_value.to_s + "\n" end end end # XXX call _convert_part_body_to_text here, but need to get charset somehow # e.g. http://www.whatdotheyknow.com/request/1593/response/3088/attach/4/Freedom%20of%20Information%20request%20-%20car%20oval%20sticker:%20Article%2020,%20Convention%20on%20Road%20Traffic%201949.txt body = headers + "\n" + body end body end # Generate a hash of the attributes associated with each significant part of a Mail object def get_attachment_attributes(mail) leaves = get_attachment_leaves(mail) attachments = [] for leaf in leaves body = get_part_body(leaf) if leaf.within_rfc822_attachment within_rfc822_subject = leaf.within_rfc822_attachment.subject body = extract_attached_message_headers(leaf) end leaf_attributes = { :url_part_number => leaf.url_part_number, :content_type => get_content_type(leaf), :filename => get_part_file_name(leaf), :charset => leaf.charset, :within_rfc822_subject => within_rfc822_subject, :body => body, :hexdigest => Digest::MD5.hexdigest(body) } attachments << leaf_attributes end return attachments end # Format def address_from_name_and_email(name, email) if !MySociety::Validate.is_valid_email(email) raise "invalid email " + email + " passed to address_from_name_and_email" end if name.nil? return Mail::Address.new(email).to_s end address = Mail::Address.new address.display_name = name address.address = email address.to_s end def address_from_string(string) Mail::Address.new(string).address end end end end