Skip to content

Commit

Permalink
Add suitable links to tags overview page where possible
Browse files Browse the repository at this point in the history
For some tags such as wikipedia, website, url, wikidata, phone, etc.
this will add one or more links to relevant web sites to the tags
overview tab.
  • Loading branch information
joto committed Oct 31, 2020
1 parent c25ffa7 commit 5c0c095
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -28,12 +28,12 @@ It uses:
* Ruby (must be at least 2.4)
* [Sinatra web framework](http://www.sinatrarb.com/) and other ruby libraries
* curl binary
* sqlite3 binary (version 3.9 or above with FTS5 support)
* sqlite3 binary (version 3.9 or above with FTS5 and regexp support)
* Optional: Parallel bzip (pzbip2)

Install the Debian/Ubuntu packages:
```sh
$ sudo apt-get install curl sqlite3
$ sudo apt-get install curl sqlite3 sqlite3-pcre
$ sudo apt-get install ruby-passenger libapache2-mod-passenger
```

Expand Down
4 changes: 3 additions & 1 deletion taginfo-config-example.json
Expand Up @@ -48,7 +48,9 @@
// web server config.
"download_dir": "../../download",
// Path to binaries (like 'taginfo-stats' and 'taginfo-similarity').
"bin_dir": "../../bin"
"bin_dir": "../../bin",
// Path to sqlite regexp extension.
"sqlite3_pcre_extension": "/usr/lib/sqlite3/pcre.so"
},
// "XAPI" and "JOSM" buttons on key/tag pages
"xapi": {
Expand Down
1 change: 1 addition & 0 deletions web/i18n/en.yml
Expand Up @@ -49,6 +49,7 @@ taginfo:
key_combinations: Combinations
comparison: Key/Tag Comparison
overview: Overview
links: Links
data_from: Data from
data_from_description: Last update of taginfo database
instance:
Expand Down
4 changes: 4 additions & 0 deletions web/lib/sql.rb
Expand Up @@ -28,6 +28,10 @@ def initialize
@db = SQLite3::Database.new(filename, { :readonly => true })
@db.results_as_hash = true

pcre_extension = TaginfoConfig.get('paths.sqlite3_pcre_extension')
if pcre_extension
@db.load_extension(pcre_extension)
end
@db.execute('PRAGMA journal_mode = OFF')
@db.execute('SELECT * FROM languages') do |row|
Language.new(row)
Expand Down
80 changes: 80 additions & 0 deletions web/lib/taglinks.rb
@@ -0,0 +1,80 @@

class TagLink

attr_reader :title, :url

def initialize(title, url)
@title = title
@url = url
end

def html
external_link('', @title, @url, true)
end

end

class TagMatch

attr_reader :regex

def initialize(regex, func)
@regex = regex
@func = func
end

def match(value)
value =~ @regex
end

def call(value)
@func.call(value)
end

def links(value)
if match(value)
return call(value).map{ |link| link.html }
end
[]
end

end

def tag_match(regex, func)
TagMatch.new(regex, func)
end

# https://wikistats.wmcloud.org/wikimedias_csv.php
# grep ,wikipedia, wikimedias.csv | cut -d, -f2 | sort
TAGLINKS = {
'addr:country': tag_match(%r{^[A-Z][A-Z]$}, lambda { |value|
return [ TagLink.new('ISO: ' + value, 'https://www.iso.org/obp/ui/#iso:code:3166:' + value),
TagLink.new('Wikipedia: ' + value, 'https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#' + value) ]
}),
phone: tag_match(%r{^\+[0-9. -]+$}, lambda { |value| return [ TagLink.new('Phone number: ' + value, 'tel:' + value.gsub(/[ .-]+/, '-')) ] }),
'ref:bag': tag_match(%r{^[0-9]+$}, lambda { |value|
id = value.rjust(16, '0')
return [ TagLink.new('Basisregistratie Adressen en Gebouwen (BAG): ' + id, 'https://bagviewer.kadaster.nl/lvbag/bag-viewer/index.html#?searchQuery=' + id) ]
}),
species: tag_match(%r{^[a-zA-Z -]+$}, lambda { |value| return [ TagLink.new('Wikispecies: ' + value, 'https://species.wikimedia.org/wiki/' + value) ] }),
url: tag_match(%r{^https?://}, lambda { |value| return [ TagLink.new('Website', value) ] }),
website: tag_match(%r{^https?://}, lambda { |value| return [ TagLink.new('Website', value) ] }),
wikidata: tag_match(%r{^Q[0-9]{1,10}$}, lambda { |value| return [ TagLink.new('Wikidata: ' + value, 'https://www.wikidata.org/wiki/' + value) ] }),
wikipedia: tag_match(%r{^[a-z-]{2,8}:}, lambda { |value|
c = value.split(':')
if $WIKIPEDIA_SITES.include?(c[0])
return [ TagLink.new('Wikipedia', "https://#{c[0]}.wikipedia.org/wiki/#{c[1]}") ]
end
return []
})
}

def get_links(key, value)
tm = TAGLINKS[key.to_sym]
if tm
return tm.links(value)
end

return []
end

31 changes: 31 additions & 0 deletions web/lib/ui/taginfo.rb
Expand Up @@ -149,6 +149,37 @@ def get_commit
erb :'taginfo/project_error_log'
end

get '/taginfo/taglinks' do
@title = 'Taglinks'
@section = 'taginfo'
@section_title = t.taginfo.meta

@data = {}
TAGLINKS.each do |key, match|
row = @db.select("SELECT count_all, values_all FROM db.keys").
condition("key = ?", key.to_s).
get_first_row

@data[key] = row

matching = 0

pcre_extension = TaginfoConfig.get('paths.sqlite3_pcre_extension')
if pcre_extension
matching = @db.count('db.tags').
condition("key = ?", key.to_s).
condition("value REGEXP ?", match.regex.to_s).
get_first_i
end

@data[key]['values_match'] = matching;

@data[key]['links'] = match.call('VALUE');
end

erb :'taginfo/taglinks'
end

get '/taginfo/wiki-problems' do
@title = "Wiki problems"
@section = 'taginfo'
Expand Down
2 changes: 2 additions & 0 deletions web/lib/ui/tags.rb
Expand Up @@ -53,6 +53,8 @@ class Taginfo < Sinatra::Base
@img_width = TaginfoConfig.get('geodistribution.width') * TaginfoConfig.get('geodistribution.scale_image')
@img_height = TaginfoConfig.get('geodistribution.height') * TaginfoConfig.get('geodistribution.scale_image')

@links = get_links(@key, @value)

javascript_for(:flexigrid)
javascript "#{ r18n.locale.code }/tag"
erb :tag
Expand Down
2 changes: 2 additions & 0 deletions web/taginfo.rb
Expand Up @@ -46,6 +46,7 @@
require 'rack/contrib'

require 'lib/utils.rb'
require 'lib/taglinks.rb'
require 'lib/config.rb'
require 'lib/javascript.rb'
require 'lib/language.rb'
Expand Down Expand Up @@ -117,6 +118,7 @@ def next_update
expires 0, :no_cache

@db = SQL::Database.new.attach_sources
$WIKIPEDIA_SITES = @db.execute('SELECT prefix FROM wikipedia_sites').map{ |row| row['prefix'] }

@data_until = DATA_UNTIL.sub(/:..$/, '')
@data_until_m = DATA_UNTIL.sub(' ', 'T') + 'Z'
Expand Down
6 changes: 6 additions & 0 deletions web/views/tag.erb
Expand Up @@ -52,8 +52,14 @@
<h2><%= h(t.taginfo.overview) %></h2>
<table id="grid-overview">
</table>
<% if @has_rtype_link or @links.size > 0 %>
<h2><%= h(t.taginfo.links) %></h2>
<% end %>
<% if @has_rtype_link %>
<p><%= h(t.pages.tag.overview.see_also) %>: <span id="relationlink"></span></p>
<% end %>
<% @links.each do |link| %>
<p><%= link %></p>
<% end %>
</div>
<div id="combinations">
Expand Down
1 change: 1 addition & 0 deletions web/views/taginfo/index.erb
Expand Up @@ -13,6 +13,7 @@
<li><a href="/taginfo/config">Configuration</a></li>
<li><a href="/taginfo/stats">Statistics</a></li>
<li><a href="/taginfo/projects">Projects</a></li>
<li><a href="/taginfo/taglinks">Tag links</a></li>
<li><a href="/taginfo/wiki-problems">Problems encountered when parsing wiki</a></li>
</ul>
<h2>Software Version</h2>
Expand Down
22 changes: 22 additions & 0 deletions web/views/taginfo/taglinks.erb
@@ -0,0 +1,22 @@
<div class="pre" lang="en" dir="ltr">
<h1>Tag links</h1>
<p>The following keys support tag links, i.e. autogenerated links from tag
values to. The regular expression must match for this to work.</p>
</div>
<div class="box">
<table class="list">
<thead><th>Key</th><th>Count</th><th>Unique values</th><th>Matching values</th><th>Regex</th><th>Links (simplified display)</th></thead>
<tbody>
<% TAGLINKS.each_with_index do |(key, match), n| c = (n%2!=0) ? ' even' : '' %>
<tr>
<td class="<%= c %>"><a href="/keys/<%= h(key) %>"><%= h(key) %></a></td>
<td class="tr<%= c %>"><%= @data[key]['count_all'].to_s_with_ts %></td>
<td class="tr<%= c %>"><%= @data[key]['values_all'].to_s_with_ts %></td>
<td class="tr<%= c %>"><%= @data[key]['values_match'].to_s_with_ts %> (<%= (@data[key]['values_match'] * 100 / @data[key]['values_all']).to_i %>%)</td>
<td class="<%= c %>"><tt><%= h(match.regex.inspect[1..-2]) %></tt></td>
<td class="<%= c %>"><ul><%= @data[key]['links'].map{ |tl| '<li>' + h(tl.title) + ' &rarr; ' + h(tl.url) + '</li>' }.join %></ul></td>
</tr>
<% end %>
</tbody>
</table>
</div>

0 comments on commit 5c0c095

Please sign in to comment.