- # ----- deleteway
- # delete way and constituent nodes from database
- # in: [0] user token (string), [1] way id
- # does: deletes way from db and any constituent nodes not used elsewhere
- # also removes ways/nodes from any relations they're in
- # out: [0] 0 (success), [1] way id (unchanged)
- def deleteway(args) #:doc:
-
- usertoken,way=args
-
- RAILS_DEFAULT_LOGGER.info(" Message: deleteway, id=#{way}")
- uid=getuserid(usertoken)
- if !uid then return -1,"You are not logged in, so the way could not be deleted." end
-
- way=way.to_i
- db_uqn='unin'+(rand*100).to_i.to_s+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquenodes table name, typically 51 chars
- db_now='@now'+(rand*100).to_i.to_s+uid.to_s+way.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()")
-
- # - delete any otherwise unused nodes
-
- createuniquenodes(way,db_uqn,[])
-
- # unless (preserve.empty?) then
- # ActiveRecord::Base.connection.execute("DELETE FROM #{db_uqn} WHERE node_id IN ("+preserve.join(',')+")")
- # end
-
- sql=<<-EOF
- INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tile)
- SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0,cn.tile
- FROM current_nodes AS cn,#{db_uqn}
- WHERE cn.id=node_id
- EOF
- ActiveRecord::Base.connection.insert(sql)
-
- sql=<<-EOF
- UPDATE current_nodes AS cn, #{db_uqn}
- SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
- WHERE cn.id=node_id
- EOF
- ActiveRecord::Base.connection.update(sql)
-
- deleteuniquenoderelations(db_uqn,uid,db_now)
- ActiveRecord::Base.connection.execute("DROP TEMPORARY TABLE #{db_uqn}")
-
- # - delete way
-
- ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},0)")
- ActiveRecord::Base.connection.update("UPDATE current_ways SET user_id=#{uid},timestamp=#{db_now},visible=0 WHERE id=#{way}")
- ActiveRecord::Base.connection.execute("DELETE FROM current_way_nodes WHERE id=#{way}")
- ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
- deleteitemrelations(way,'way',uid,db_now)
- [0,way]
- end
-
- def readwayquery(id,insistonvisible) #:doc:
- sql=<<-EOF
- SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,current_nodes.id,tags,visible
- FROM current_way_nodes,current_nodes
- WHERE current_way_nodes.id=#{id}
- AND current_way_nodes.node_id=current_nodes.id
- EOF
- if insistonvisible then sql+=" AND current_nodes.visible=1 " end
- sql+=" ORDER BY sequence_id"
- ActiveRecord::Base.connection.select_all(sql)
- end
-
- # Get the latest version id of a way
- def getlastversion(id,version) #:doc:
- old_way = OldWay.find(:first, :conditions => ['visible=1 AND id=?' , id], :order => 'version DESC')
- old_way.version
- end
-
- def readwayquery_old(id,version,historic) #:doc:
- # Node handling on undelete (historic=false):
- # - always use the node specified, even if it's moved
-
- # Node handling on revert (historic=true):
- # - if it's a visible node, use a new node id (i.e. not mucking up the old one)
- # which means the SWF needs to allocate new ids
- # - if it's an invisible node, we can reuse the old node id
-
- # get node list from specified version of way,
- # and the _current_ lat/long/tags of each node
-
- row=ActiveRecord::Base.connection.select_one("SELECT timestamp FROM ways WHERE version=#{version} AND id=#{id}")
- waytime=row['timestamp']
-
- sql=<<-EOF
- SELECT cn.id,visible,latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags
- FROM way_nodes wn,current_nodes cn
- WHERE wn.version=#{version}
- AND wn.id=#{id}
- AND wn.node_id=cn.id
- ORDER BY sequence_id
- EOF
- rows=ActiveRecord::Base.connection.select_all(sql)
-
- # if historic (full revert), get the old version of each node
- # - if it's in another way now, generate a new id
- # - if it's not in another way, use the old ID
- if historic then
- rows.each_index do |i|
- sql=<<-EOF
- SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags,cwn.id AS currentway
- FROM nodes n
- LEFT JOIN current_way_nodes cwn
- ON cwn.node_id=n.id
- WHERE n.id=#{rows[i]['id']}
- AND n.timestamp<="#{waytime}"
- AND cwn.id!=#{id}
- ORDER BY n.timestamp DESC
- LIMIT 1
- EOF
- row=ActiveRecord::Base.connection.select_one(sql)
- unless row.nil? then
- nx=row['longitude'].to_f
- ny=row['latitude'].to_f
- if (row['currentway'] && (nx!=rows[i]['longitude'].to_f or ny!=rows[i]['latitude'].to_f or row['tags']!=rows[i]['tags'])) then rows[i]['id']=-1 end
- rows[i]['longitude']=nx
- rows[i]['latitude' ]=ny
- rows[i]['tags' ]=row['tags']
+ # Save a relation.
+ # Returns
+ # 0. 0 (success),
+ # 1. original relation id (unchanged),
+ # 2. new relation id,
+ # 3. version.
+
+ def putrelation(renumberednodes, renumberedways, usertoken, changeset_id, version, relid, tags, members, visible)
+ amf_handle_error("'putrelation' #{relid}", "relation", relid) do
+ user = getuser(usertoken)
+
+ return -1, "You are not logged in, so the relation could not be saved." unless user
+ return -1, t("application.setup_user_auth.blocked") if user.blocks.active.exists?
+ return -1, "You must accept the contributor terms before you can edit." if REQUIRE_TERMS_AGREED && user.terms_agreed.nil?
+
+ return -1, "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1." unless tags_ok(tags)
+ tags = strip_non_xml_chars tags
+
+ relid = relid.to_i
+ visible = visible.to_i.nonzero?
+
+ new_relation = nil
+ relation = nil
+ Relation.transaction do
+ # create a new relation, or find the existing one
+ relation = Relation.find(relid) if relid > 0
+ # We always need a new node, based on the data that has been sent to us
+ new_relation = Relation.new
+
+ # 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
+ typedmembers << [m[0], mid, m[2].delete("\000-\037\ufffe\uffff", "^\011\012\015")] if mid
+ end
+
+ # assign new contents
+ new_relation.members = typedmembers
+ new_relation.tags = tags
+ new_relation.visible = visible
+ new_relation.changeset_id = changeset_id
+ new_relation.version = version
+
+ if relid <= 0
+ # We're creating the relation
+ new_relation.create_with_history(user)
+ elsif visible
+ # We're updating the relation
+ new_relation.id = relid
+ relation.update_from(new_relation, user)
+ else
+ # We're deleting the relation
+ new_relation.id = relid
+ relation.delete_with_history!(new_relation, user)