]> git.openstreetmap.org Git - rails.git/blob - app/controllers/old_controller.rb
Make sure we give iD uncompressed XML for a trace
[rails.git] / app / controllers / old_controller.rb
1 # this class pulls together the logic for all the old_* controllers
2 # into one place. as it turns out, the API methods for historical
3 # nodes, ways and relations are basically identical.
4 class OldController < ApplicationController
5   require "xml/libxml"
6
7   skip_before_action :verify_authenticity_token
8   before_action :setup_user_auth, :only => [:history, :version]
9   before_action :authorize, :only => [:redact]
10   before_action :authorize_moderator, :only => [:redact]
11   before_action :require_allow_write_api, :only => [:redact]
12   before_action :check_api_readable
13   before_action :check_api_writable, :only => [:redact]
14   after_action :compress_output
15   around_action :api_call_handle_error, :api_call_timeout
16   before_action :lookup_old_element, :except => [:history]
17   before_action :lookup_old_element_versions, :only => [:history]
18
19   def history
20     # the .where() method used in the lookup_old_element_versions
21     # call won't throw an error if no records are found, so we have
22     # to do that ourselves.
23     fail OSM::APINotFoundError.new if @elements.empty?
24
25     doc = OSM::API.new.get_xml_doc
26
27     visible_elements = if show_redactions?
28                          @elements
29                        else
30                          @elements.unredacted
31                        end
32
33     visible_elements.each do |element|
34       doc.root << element.to_xml_node
35     end
36
37     render :text => doc.to_s, :content_type => "text/xml"
38   end
39
40   def version
41     if @old_element.redacted? && !show_redactions?
42       render :text => "", :status => :forbidden
43
44     else
45       response.last_modified = @old_element.timestamp
46
47       doc = OSM::API.new.get_xml_doc
48       doc.root << @old_element.to_xml_node
49
50       render :text => doc.to_s, :content_type => "text/xml"
51     end
52   end
53
54   def redact
55     redaction_id = params["redaction"]
56     if redaction_id.nil?
57       # if no redaction ID was provided, then this is an unredact
58       # operation.
59       @old_element.redact!(nil)
60     else
61       # if a redaction ID was specified, then set this element to
62       # be redacted in that redaction.
63       redaction = Redaction.find(redaction_id.to_i)
64       @old_element.redact!(redaction)
65     end
66
67     # just return an empty 200 OK for success
68     render :text => ""
69   end
70
71   private
72
73   def show_redactions?
74     @user && @user.moderator? && params[:show_redactions] == "true"
75   end
76 end