+ return [relid, {}, []] if rel.nil? or !rel.visible
+
+ [relid, rel.tags, rel.members]
+ end
+
+ # Find relations with specified name/id.
+ # Returns array of relations, each in same form as getrelation.
+
+ def findrelations(searchterm)
+ rels = []
+ if searchterm.to_i>0 then
+ rel = Relation.find(searchterm.to_i)
+ if rel and rel.visible then
+ rels.push([rel.id, rel.tags, rel.members])
+ end
+ else
+ RelationTag.find(:all, :limit => 11, :conditions => ["match(v) against (?)", searchterm] ).each do |t|
+ if t.relation.visible then
+ rels.push([t.relation.id, t.relation.tags, t.relation.members])
+ end
+ end
+ end
+ rels
+ end
+
+ # Save a relation.
+ # Returns
+ # 0. 0 (success),
+ # 1. original relation id (unchanged),
+ # 2. new relation id.
+
+ def putrelation(renumberednodes, renumberedways, usertoken, relid, tags, members, visible) #:doc:
+ uid = getuserid(usertoken)
+ if !uid then return -1,"You are not logged in, so the relation could not be saved." end
+
+ relid = relid.to_i
+ visible = (visible.to_i != 0)
+
+ # create a new relation, or find the existing one
+ if relid <= 0
+ rel = Relation.new
+ else
+ rel = Relation.find(relid)
+ end
+
+ # check the members are all positive, and correctly type
+ typedmembers = []
+ members.each do |m|
+ mid = m[1].to_i
+ if mid < 0
+ mid = renumberednodes[mid] if m[0] == 'node'
+ mid = renumberedways[mid] if m[0] == 'way'
+ end
+ if mid
+ typedmembers << [m[0], mid, m[2]]
+ end
+ end
+
+ # assign new contents
+ rel.members = typedmembers
+ rel.tags = tags
+ rel.visible = visible
+ rel.user_id = uid
+
+ # check it then save it
+ # BUG: the following is commented out because it always fails on my
+ # install. I think it's a Rails bug.
+
+ #if !rel.preconditions_ok?
+ # return -2, "Relation preconditions failed"
+ #else
+ rel.save_with_history!
+ #end
+
+ [0, relid, rel.id]
+ end
+
+ # Save a way to the database, including all nodes. Any nodes in the previous
+ # version and no longer used are deleted.
+ #
+ # Returns:
+ # 0. '0' (code for success),
+ # 1. original way id (unchanged),
+ # 2. new way id,
+ # 3. hash of renumbered nodes (old id=>new id)
+
+ def putway(renumberednodes, usertoken, originalway, points, attributes) #:doc:
+
+ # -- Initialise and carry out checks
+
+ uid = getuserid(usertoken)
+ if !uid then return -1,"You are not logged in, so the way could not be saved." end
+
+ originalway = originalway.to_i
+
+ points.each do |a|
+ if a[2] == 0 or a[2].nil? then return -2,"Server error - node with id 0 found in way #{originalway}." end
+ if a[1] == 90 then return -2,"Server error - node with lat -90 found in way #{originalway}." end
+ end
+
+ if points.length < 2 then return -2,"Server error - way is only #{points.length} points long." end
+
+ # -- Get unique nodes
+
+ if originalway < 0
+ way = Way.new
+ uniques = []
+ else
+ way = Way.find(originalway)
+ uniques = way.unshared_node_ids
+ end
+
+ # -- Compare nodes and save changes to any that have changed
+
+ nodes = []
+
+ points.each do |n|
+ lon = n[0].to_f
+ lat = n[1].to_f
+ id = n[2].to_i
+ savenode = false
+
+ if renumberednodes[id]
+ id = renumberednodes[id]
+ elsif id < 0
+ # Create new node
+ node = Node.new
+ savenode = true
+ else
+ node = Node.find(id)
+ nodetags=node.tags_as_hash
+ nodetags.delete('created_by')
+ if !fpcomp(lat, node.lat) or !fpcomp(lon, node.lon) or
+ n[4] != nodetags or !node.visible?
+ savenode = true
+ end
+ end
+
+ if savenode
+ node.user_id = uid
+ node.lat = lat
+ node.lon = lon
+ node.tags = Tags.join(n[4])
+ node.visible = true
+ node.save_with_history!
+
+ if id != node.id
+ renumberednodes[id] = node.id
+ id = node.id
+ end
+ end
+
+ uniques = uniques - [id]
+ nodes.push(id)
+ end
+
+ # -- Delete any unique nodes
+
+ uniques.each do |n|
+ deleteitemrelations(n, 'node')
+
+ node = Node.find(n)
+ node.user_id = uid
+ node.visible = false
+ node.save_with_history!
+ end
+
+ # -- Save revised way
+
+ way.tags = attributes
+ way.nds = nodes
+ way.user_id = uid
+ way.visible = true
+ way.save_with_history!
+
+ [0, originalway, way.id, renumberednodes]
+ end
+
+ # Save POI to the database.
+ # Refuses save if the node has since become part of a way.
+ # Returns:
+ # 0. 0 (success),
+ # 1. original node id (unchanged),
+ # 2. new node id.
+
+ def putpoi(usertoken, id, lon, lat, tags, visible) #:doc:
+ uid = getuserid(usertoken)
+ if !uid then return -1,"You are not logged in, so the point could not be saved." end
+
+ id = id.to_i
+ visible = (visible.to_i == 1)
+
+ if id > 0 then
+ node = Node.find(id)
+
+ if !visible then
+ unless node.ways.empty? then return -1,"The point has since become part of a way, so you cannot save it as a POI." end
+ deleteitemrelations(id, 'node')
+ end
+ else
+ node = Node.new
+ end
+
+ node.user_id = uid
+ node.lat = lat
+ node.lon = lon
+ node.tags = Tags.join(tags)
+ node.visible = visible
+ node.save_with_history!
+
+ [0, id, node.id]
+ end
+
+ # Read POI from database
+ # (only called on revert: POIs are usually read by whichways).
+ #
+ # Returns array of id, long, lat, hash of tags.