- # 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
-
- # ----- putway
- # saves a way to the database
- # in: [0] user token (string),
- # [1] original way id (may be negative),
- # [2] array of points (as getway/getway_old),
- # [3] hash of way tags,
- # [4] original way version (0 if not a reverted/undeleted way),
- # [5] baselong, [6] basey, [7] masterscale
- # does: saves way to the database
- # all constituent nodes are created/updated as necessary
- # (or deleted if they were in the old version and are otherwise unused)
- # out: [0] 0 (code for success), [1] original way id (unchanged),
- # [2] new way id, [3] hash of renumbered nodes (old id=>new id),
- # [4] xmin, [5] xmax, [6] ymin, [7] ymax (unprojected bbox)
- def putway(args,renumberednodes) #:doc:
- RAILS_DEFAULT_LOGGER.info(" putway started")
- usertoken,originalway,points,attributes,oldversion,baselong,basey,masterscale=args
- uid=getuserid(usertoken)
- if !uid then return -1,"You are not logged in, so the way could not be saved." end
-
- 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
-
- # -- 3. read original way into memory
-
- if originalway < 0
- way = Way.new
- uniques = []
- else
- way = Way.find(originalway)
- uniques = way.unshared_node_ids
- end
-
- # -- 4. get version by inserting new row into ways
-
- 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)
- if !fpcomp(lat, node.lat) or !fpcomp(lon, node.lon) or
- Tags.join(n[4]) != node.tags 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
-
- points.each_index do |i|
- xs=coord2long(points[i][0],masterscale,baselong)
- ys=coord2lat(points[i][1],masterscale,basey)
- xmin=[xs,xmin].min; xmax=[xs,xmax].max
- ymin=[ys,ymin].min; ymax=[ys,ymax].max
- node=points[i][2].to_i
- tagstr=array2tag(points[i][4])
- tagsql="'"+sqlescape(tagstr)+"'"
- lat=(ys * 10000000).round
- long=(xs * 10000000).round
- tile=QuadTile.tile_for_point(ys, xs)
-
- way.tags = attributes
- way.nds = nodes
- way.user_id = uid
- way.visible = true
- way.save_with_history!
-
- [0, originalway, way.id, renumberednodes]
- end
-
- # ----- putpoi
- # save POI to the database
- # in: [0] user token (string),
- # [1] original node id (may be negative),
- # [2] projected longitude, [3] projected latitude,
- # [4] hash of tags, [5] visible (0 to delete, 1 otherwise),
- # [6] baselong, [7] basey, [8] masterscale
- # does: saves POI node to the database
- # refuses save if the node has since become part of a way
- # out: [0] 0 (success), [1] original node id (unchanged),
- # [2] new node id
- def putpoi(args) #:doc:
- usertoken,id,x,y,tags,visible,baselong,basey,masterscale=args
- uid=getuserid(usertoken)
- if !uid then return -1,"You are not logged in, so the point could not be saved." end
-
- 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
-
- # ----- getpoi
- # read POI from database
- # (only called on revert: POIs are usually read by whichways)
- # in: [0] node id, [1] baselong, [2] basey, [3] masterscale
- # does: reads POI
- # out: [0] id (unchanged), [1] projected long, [2] projected lat,
- # [3] hash of tags
- def getpoi(args) #:doc:
- id,baselong,basey,masterscale = args
-
- n = Node.find(id.to_i)
- if n
- return [n.id, n.lon_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash]
- else
- return [nil,nil,nil,'']
+ # Get an old version of a way, and all constituent nodes.
+ #
+ # For undelete (version<0), always uses the most recent version of each node,
+ # even if it's moved. For revert (version >= 0), uses the node in existence
+ # at the time, generating a new id if it's still visible and has been moved/
+ # retagged.
+ #
+ # Returns:
+ # 0. success code,
+ # 1. id,
+ # 2. array of points,
+ # 3. hash of tags,
+ # 4. version,
+ # 5. is this the current, visible version? (boolean)
+
+ def getway_old(id, timestamp)
+ amf_handle_error_with_timeout("'getway_old' #{id}, #{timestamp}", "way", id) do
+ if timestamp == ""
+ # undelete
+ old_way = OldWay.where(:visible => true, :way_id => id).unredacted.order("version DESC").first
+ points = old_way.get_nodes_undelete unless old_way.nil?
+ else
+ begin
+ # revert
+ timestamp = Time.zone.strptime(timestamp.to_s, "%d %b %Y, %H:%M:%S")
+ old_way = OldWay.where("way_id = ? AND timestamp <= ?", id, timestamp).unredacted.order("timestamp DESC").first
+ unless old_way.nil?
+ if old_way.visible
+ points = old_way.get_nodes_revert(timestamp)
+ else
+ return [-1, "Sorry, the way was deleted at that time - please revert to a previous version.", id]
+ end
+ end
+ rescue ArgumentError
+ # thrown by date parsing method. leave old_way as nil for
+ # the error handler below.
+ old_way = nil
+ end
+ end
+
+ if old_way.nil?
+ return [-1, "Sorry, the server could not find a way at that time.", id]
+ else
+ curway = Way.find(id)
+ old_way.tags["history"] = "Retrieved from v#{old_way.version}"
+ return [0, "", id, points, old_way.tags, curway.version, (curway.version == old_way.version && curway.visible)]
+ end