2   class RelationsController < ApiController
 
   3     before_action :check_api_writable, :only => [:create, :update, :destroy]
 
   4     before_action :authorize, :only => [:create, :update, :destroy]
 
   8     before_action :require_public_data, :only => [:create, :update, :destroy]
 
   9     before_action :set_request_formats, :except => [:create, :update, :destroy]
 
  10     before_action :check_rate_limit, :only => [:create, :update, :destroy]
 
  13       raise OSM::APIBadUserInput, "The parameter relations is required, and must be of the form relations=id[,id[,id...]]" unless params["relations"]
 
  15       ids = params["relations"].split(",").collect(&:to_i)
 
  17       raise OSM::APIBadUserInput, "No relations were given to search for" if ids.empty?
 
  19       @relations = Relation.find(ids)
 
  22       respond_to do |format|
 
  29       relation = Relation.find(params[:id])
 
  31       response.last_modified = relation.timestamp unless params[:full]
 
  39           # with parameter :full
 
  40           # returns representation of one relation object plus all its
 
  41           # members, plus all nodes part of member ways
 
  43           # first find the ids of nodes, ways and relations referenced by this
 
  44           # relation - note that we exclude this relation just in case.
 
  46           node_ids = relation.members.select { |m| m[0] == "Node" }.pluck(1)
 
  47           way_ids = relation.members.select { |m| m[0] == "Way" }.pluck(1)
 
  48           relation_ids = relation.members.select { |m| m[0] == "Relation" && m[1] != relation.id }.pluck(1)
 
  50           # next load the relations and the ways.
 
  52           relations = Relation.where(:id => relation_ids).includes(:relation_tags)
 
  53           ways = Way.where(:id => way_ids).includes(:way_nodes, :way_tags)
 
  55           # now additionally collect nodes referenced by ways. Note how we
 
  56           # recursively evaluate ways but NOT relations.
 
  58           way_node_ids = ways.collect do |way|
 
  59             way.way_nodes.collect(&:node_id)
 
  61           node_ids += way_node_ids.flatten
 
  62           nodes = Node.where(:id => node_ids.uniq).includes(:node_tags)
 
  66             next unless node.visible? # should be unnecessary if data is consistent.
 
  72             next unless way.visible? # should be unnecessary if data is consistent.
 
  77           relations.each do |rel|
 
  78             next unless rel.visible? # should be unnecessary if data is consistent.
 
  85         @relations << relation
 
  88         respond_to do |format|
 
  98       relation = Relation.from_xml(request.raw_post, :create => true)
 
 100       # Assume that Relation.from_xml has thrown an exception if there is an error parsing the xml
 
 101       relation.create_with_history current_user
 
 102       render :plain => relation.id.to_s
 
 106       relation = Relation.find(params[:id])
 
 107       new_relation = Relation.from_xml(request.raw_post)
 
 109       raise OSM::APIBadUserInput, "The id in the url (#{relation.id}) is not the same as provided in the xml (#{new_relation.id})" unless new_relation && new_relation.id == relation.id
 
 111       relation.update_from new_relation, current_user
 
 112       render :plain => relation.version.to_s
 
 116       relation = Relation.find(params[:id])
 
 117       new_relation = Relation.from_xml(request.raw_post)
 
 118       if new_relation && new_relation.id == relation.id
 
 119         relation.delete_with_history!(new_relation, current_user)
 
 120         render :plain => relation.version.to_s