+def getlastversion(id,version)
+ row=ActiveRecord::Base.connection.select_one("SELECT version FROM ways WHERE id=#{id} AND visible=1 ORDER BY version DESC LIMIT 1")
+ row['version']
+end
+
+def readwayquery_old(id,version,historic)
+ # 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,
+ # and use this (though with a new id) if it differs from the current one
+ if historic then
+ rows.each_index do |i|
+ sql=<<-EOF
+ SELECT latitude*0.0000001 AS latitude,longitude*0.0000001 AS longitude,tags
+ FROM nodes
+ WHERE id=#{rows[i]['id']}
+ AND timestamp<="#{waytime}"
+ ORDER BY 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 (nx!=rows[i]['longitude'].to_f or ny!=rows[i]['latitude'].to_f or row['tags']!=rows[i]['tags']) then
+ rows[i]['id']=-1
+ # This generates a new node id if x/y/tags differ from current node.
+ # Strictly speaking, it need only do this for uniquenodes, but we're
+ # not generating uniquenodes for historic ways (yet!).
+ end
+ rows[i]['longitude']=nx
+ rows[i]['latitude' ]=ny
+ rows[i]['tags' ]=row['tags']
+ end
+ end
+ end
+ rows
+end
+