- # 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)
- 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
-
- db_now='@now'+(rand*100).to_i.to_s+uid.to_s+id.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
- ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
-
- id=id.to_i
- visible=visible.to_i
- if visible==0 then
- # if deleting, check node hasn't become part of a way
- inway=ActiveRecord::Base.connection.select_one("SELECT cw.id FROM current_ways cw,current_way_nodes cwn WHERE cw.id=cwn.id AND cw.visible=1 AND cwn.node_id=#{id} LIMIT 1")
- unless inway.nil? then return -1,"The point has since become part of a way, so you cannot save it as a POI." end
- deleteitemrelations(id,'node',uid,db_now)
- end
-
- x=coord2long(x.to_f,masterscale,baselong)
- y=coord2lat(y.to_f,masterscale,basey)
- tagsql="'"+sqlescape(array2tag(tags))+"'"
- lat=(y * 10000000).round
- long=(x * 10000000).round
- tile=QuadTile.tile_for_point(y, x)
-
- if (id>0) then
- ActiveRecord::Base.connection.insert("INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES (#{id},#{lat},#{long},#{db_now},#{uid},#{visible},#{tagsql},#{tile})");
- ActiveRecord::Base.connection.update("UPDATE current_nodes SET latitude=#{lat},longitude=#{long},timestamp=#{db_now},user_id=#{uid},visible=#{visible},tags=#{tagsql},tile=#{tile} WHERE id=#{id}");
- newid=id
- else
- newid=ActiveRecord::Base.connection.insert("INSERT INTO current_nodes (latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES (#{lat},#{long},#{db_now},#{uid},#{visible},#{tagsql},#{tile})");
- ActiveRecord::Base.connection.update("INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tags,tile) VALUES (#{newid},#{lat},#{long},#{db_now},#{uid},#{visible},#{tagsql},#{tile})");
+ # 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) #:doc:
+ amf_handle_error_with_timeout("'getway_old' #{id}, #{timestamp}") do
+ if timestamp == ''
+ # undelete
+ old_way = OldWay.find(:first, :conditions => ['visible = ? AND id = ?', true, id], :order => 'version DESC')
+ points = old_way.get_nodes_undelete unless old_way.nil?
+ else
+ begin
+ # revert
+ timestamp = DateTime.strptime(timestamp.to_s, "%d %b %Y, %H:%M:%S")
+ old_way = OldWay.find(:first, :conditions => ['id = ? AND timestamp <= ?', id, timestamp], :order => 'timestamp DESC')
+ unless old_way.nil?
+ points = old_way.get_nodes_revert(timestamp)
+ if !old_way.visible
+ return [-1, "Sorry, the way was deleted at that time - please revert to a previous version.", id, [], {}, nil, false]
+ end
+ end
+ rescue ArgumentError
+ # thrown by date parsing method. leave old_way as nil for
+ # the error handler below.
+ end
+ end
+
+ if old_way.nil?
+ return [-1, "Sorry, the server could not find a way at that time.", id, [], {}, nil, false]
+ 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 and curway.visible)]
+ end