wikidata_position_history 1.6.0 → 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +88 -2
- data/lib/query_service.rb +18 -1
- data/lib/sparql/{bio_data.rb → bio_query.rb} +4 -10
- data/lib/sparql/item_query.rb +35 -2
- data/lib/sparql/{mandates.rb → mandates_query.rb} +10 -35
- data/lib/sparql/position_query.rb +76 -0
- data/lib/wikidata_position_history.rb +4 -3
- data/lib/wikidata_position_history/checks.rb +34 -4
- data/lib/wikidata_position_history/output_row.rb +143 -0
- data/lib/wikidata_position_history/report.rb +84 -32
- data/lib/wikidata_position_history/template.rb +42 -6
- data/lib/wikidata_position_history/version.rb +1 -1
- data/wikidata_position_history.gemspec +1 -1
- metadata +8 -7
- data/lib/sparql/position_data.rb +0 -66
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e59634d8233371767e5a151e60d8bb5f3c8f5703302654c59f5db407faea4a4
|
4
|
+
data.tar.gz: 763b5794f41ad395b045fedfb7b132fc6139698e3514df0caf96a47bd5d224d2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ad33ca550ad0a5a83ffd42bc03ce8de8df93f77b932ec8ae74d21c528160e8b47d55ff8569641d34801e7f181f0b3b9c856e2d27f5aef91914d73522e5e63de
|
7
|
+
data.tar.gz: 7dba23307f13045710a4bc3b7c0869e024fcf206b023e9740c3c8c5366ea7aa260a1dcc342cf7c0875ed1b635c2bc3b96e350720e32c01e35051b1bc1d37e747
|
data/CHANGELOG.md
CHANGED
@@ -1,12 +1,98 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
# [1.11.0] 2020-09-14
|
4
|
+
|
5
|
+
## Enhancements
|
6
|
+
|
7
|
+
* Wikidata now has a new {{QB}} template to link to _both_ an Item and
|
8
|
+
its Talk page. As the PositionHolderHistory template for a position
|
9
|
+
often lives on its Talk page, any link to a position item now uses
|
10
|
+
this template, for easier of access to that. (Requested by Tagishsimon:
|
11
|
+
https://twitter.com/Tagishsimon/status/1304363879322079233)
|
12
|
+
|
13
|
+
* The change to how warnings templates work from the last release (i.e.
|
14
|
+
using on-wiki templates, so they can be improved or translated, or used
|
15
|
+
to look for problems via backlinks) definitely seems to have been a good
|
16
|
+
idea, so now the warnings for having an unexpected number of inception
|
17
|
+
or abolition dates have also been adjusted to work the same way.
|
18
|
+
|
19
|
+
# [1.10.0] 2020-09-13
|
20
|
+
|
21
|
+
## Enhancements
|
22
|
+
|
23
|
+
* If some other position has a "replaces" (P1365) or "replaced by"
|
24
|
+
(P1366) pointing to _this_ position, but this position doesn't have
|
25
|
+
the reciprocal inverse claims back to that one, include them as a
|
26
|
+
successor/predecessor, but warn that it’s only an indirect connection.
|
27
|
+
|
28
|
+
* The warnings in the above case now use an on-wiki template for their
|
29
|
+
text. This means they can be translated into other languages, and also
|
30
|
+
means that backlinks to these templates, via WhatLinksHere, can act as
|
31
|
+
a TODO list. The other warnings will be migrated to this approach Real
|
32
|
+
Soon Now™.
|
33
|
+
|
34
|
+
# [1.9.0] 2020-09-11
|
35
|
+
|
36
|
+
## Enhancements
|
37
|
+
|
38
|
+
* If a position has any successor or predecessor offices (in "replaces"
|
39
|
+
(P1365) or "replaced by" (P1366)) the report will now display those.
|
40
|
+
|
41
|
+
# [1.8.0] 2020-09-09
|
42
|
+
|
43
|
+
## Enhancements
|
44
|
+
|
45
|
+
* If the very latest person we know of having held this position also
|
46
|
+
has a 'replaced by' qualifier, that’s a sign that the successor should
|
47
|
+
really also have a suitable P39, and actually appear here too. So we
|
48
|
+
want to display a warning in such cases. Likewise if the earliest person
|
49
|
+
we know if also has a 'replaces'
|
50
|
+
|
51
|
+
* This report is meant to be used with positions that are held by only a
|
52
|
+
single person at a time. Using it to produce a report of everyone who
|
53
|
+
has been, say, a Member of the UK Parliament, is the sort of thing that
|
54
|
+
will cause all manner of havoc, as it will try to display tens of
|
55
|
+
thousands of people, all of whom have overlaps with other members etc.
|
56
|
+
So we now sanity-check first of all that the position isn't legislative,
|
57
|
+
and produce a nice "Don’t do that!" message in such cases.
|
58
|
+
|
59
|
+
* It seems that the recent ability to handle dates that are only know at
|
60
|
+
decade-level precision isn’t actually enough, as we have some that we
|
61
|
+
only know at century-level precision! (For example, that the position of
|
62
|
+
Lord Chancellor of Ireland was created some time in the 12th Century.)
|
63
|
+
Such dates will now appear in a nicer format.
|
64
|
+
|
65
|
+
* Sometimes updating the table looks like something has changed, but
|
66
|
+
really the only difference is that a few of people who have no dates
|
67
|
+
are shuffled around a bit in the list. This is because we previously
|
68
|
+
only sorted by date order, so people with no dates were effectively in a
|
69
|
+
random order. Now we sort those people by ID too, which should minimise
|
70
|
+
the number of times an update will appear in your watchlist, only to
|
71
|
+
discover nothing significant actually happened.
|
72
|
+
|
73
|
+
# [1.7.0] 2020-09-08
|
74
|
+
|
75
|
+
## Enchancements
|
76
|
+
|
77
|
+
* Yesterday’s future, when we said we’d do something a little better
|
78
|
+
with positions that have multiple inception or abolition dates, has
|
79
|
+
arrived. Now we display all of them (with a warning), rather than just
|
80
|
+
picking one semi-randomly.
|
81
|
+
|
82
|
+
## Improvements
|
83
|
+
|
84
|
+
* A query like https://w.wiki/bVz is taking about 6 seconds to run.
|
85
|
+
Changing that to https://w.wiki/bW3 drops that to about half a second.
|
86
|
+
If you were to guess that the first has now been replaced by the
|
87
|
+
second, you’d be entirely correct.
|
88
|
+
|
3
89
|
# [1.6.0] 2020-09-07
|
4
90
|
|
5
91
|
## Enhancements
|
6
92
|
|
7
93
|
* If a position has an inception date and/or abolition date, those will
|
8
94
|
now also be displayed. (If a position has more than one of either of
|
9
|
-
those — which really shouldn
|
95
|
+
those — which really shouldn’t happen, but sometimes does — then the
|
10
96
|
behaviour may not be particularly sensible. Later evolutions of this
|
11
97
|
feature will hopefully handle that better.)
|
12
98
|
|
@@ -23,7 +109,7 @@
|
|
23
109
|
|
24
110
|
* When showing the results for a position from long long ago (such as
|
25
111
|
the High Kings of Ireland), display the dates as "862 – 879" not as
|
26
|
-
"
|
112
|
+
"0862 – 0879"
|
27
113
|
* If we only know that someone took (or left) office sometime in a given
|
28
114
|
decade (i.e. at date precision 8), display that as (say) "1930s"
|
29
115
|
|
data/lib/query_service.rb
CHANGED
@@ -37,6 +37,10 @@ module QueryService
|
|
37
37
|
@url = url
|
38
38
|
end
|
39
39
|
|
40
|
+
def eql?(other)
|
41
|
+
id == other.id
|
42
|
+
end
|
43
|
+
|
40
44
|
def id
|
41
45
|
url.split('/').last unless url.to_s.empty?
|
42
46
|
end
|
@@ -45,6 +49,18 @@ module QueryService
|
|
45
49
|
"{{Q|#{id}}}" if id
|
46
50
|
end
|
47
51
|
|
52
|
+
def qblink
|
53
|
+
"{{QB|#{id}}}" if id
|
54
|
+
end
|
55
|
+
|
56
|
+
def qlink_i
|
57
|
+
"''#{qlink}''" if qlink
|
58
|
+
end
|
59
|
+
|
60
|
+
def qblink_i
|
61
|
+
"''#{qblink}''" if qblink
|
62
|
+
end
|
63
|
+
|
48
64
|
private
|
49
65
|
|
50
66
|
attr_reader :url
|
@@ -54,7 +70,7 @@ module QueryService
|
|
54
70
|
class WikidataDate
|
55
71
|
include Comparable
|
56
72
|
|
57
|
-
DATELEN = { '11' => 10, '10' => 7, '9' => 4, '8' => 4 }.freeze
|
73
|
+
DATELEN = { '11' => 10, '10' => 7, '9' => 4, '8' => 4, '7' => 2 }.freeze
|
58
74
|
|
59
75
|
def initialize(str, precision)
|
60
76
|
@str = str
|
@@ -105,6 +121,7 @@ module QueryService
|
|
105
121
|
end
|
106
122
|
|
107
123
|
def precisioned_string
|
124
|
+
return "#{truncated_string}. century" if precision == '7'
|
108
125
|
return "#{truncated_string}s" if precision == '8'
|
109
126
|
|
110
127
|
truncated_string
|
@@ -7,7 +7,7 @@ module WikidataPositionHistory
|
|
7
7
|
# This is distinct from the mandate query itself to avoid complex
|
8
8
|
# GROUP BY scenarios where people have multiple values for
|
9
9
|
# biographical properties.
|
10
|
-
class
|
10
|
+
class BioQuery < ItemQuery
|
11
11
|
def raw_sparql
|
12
12
|
<<~SPARQL
|
13
13
|
# holder-biodata
|
@@ -24,13 +24,9 @@ module WikidataPositionHistory
|
|
24
24
|
end
|
25
25
|
|
26
26
|
# Represents a single row returned from the Position query
|
27
|
-
class
|
28
|
-
def initialize(row)
|
29
|
-
@row = row
|
30
|
-
end
|
31
|
-
|
27
|
+
class BioRow < SPARQL::QueryRow
|
32
28
|
def person
|
33
|
-
|
29
|
+
item_from(:item)
|
34
30
|
end
|
35
31
|
|
36
32
|
def image_title
|
@@ -47,10 +43,8 @@ module WikidataPositionHistory
|
|
47
43
|
|
48
44
|
private
|
49
45
|
|
50
|
-
attr_reader :row
|
51
|
-
|
52
46
|
def image_url
|
53
|
-
|
47
|
+
raw(:image)
|
54
48
|
end
|
55
49
|
end
|
56
50
|
end
|
data/lib/sparql/item_query.rb
CHANGED
@@ -4,7 +4,7 @@ require 'erb'
|
|
4
4
|
|
5
5
|
module WikidataPositionHistory
|
6
6
|
module SPARQL
|
7
|
-
#
|
7
|
+
# Abstract class to turn raw SPARQL into result objects
|
8
8
|
class ItemQuery
|
9
9
|
def initialize(itemid)
|
10
10
|
@itemid = itemid
|
@@ -23,12 +23,45 @@ module WikidataPositionHistory
|
|
23
23
|
attr_reader :itemid
|
24
24
|
|
25
25
|
def sparql
|
26
|
-
raw_sparql %
|
26
|
+
raw_sparql % sparql_args
|
27
|
+
end
|
28
|
+
|
29
|
+
def sparql_args
|
30
|
+
itemid
|
27
31
|
end
|
28
32
|
|
29
33
|
def json
|
30
34
|
@json ||= QueryService::Query.new(sparql).results
|
31
35
|
end
|
32
36
|
end
|
37
|
+
|
38
|
+
# Abstract class to represents a single row returned from a query
|
39
|
+
class QueryRow
|
40
|
+
def initialize(row)
|
41
|
+
@row = row
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
attr_reader :row
|
47
|
+
|
48
|
+
def item_from(key)
|
49
|
+
value = raw(key)
|
50
|
+
return if value.to_s.empty?
|
51
|
+
|
52
|
+
QueryService::WikidataItem.new(value)
|
53
|
+
end
|
54
|
+
|
55
|
+
def date_from(key, precision_key)
|
56
|
+
trunc = raw(key).to_s[0..9]
|
57
|
+
return if trunc.empty?
|
58
|
+
|
59
|
+
QueryService::WikidataDate.new(trunc, raw(precision_key))
|
60
|
+
end
|
61
|
+
|
62
|
+
def raw(key)
|
63
|
+
row.dig(key, :value)
|
64
|
+
end
|
65
|
+
end
|
33
66
|
end
|
34
67
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module WikidataPositionHistory
|
4
4
|
module SPARQL
|
5
5
|
# SPARQL for fetching all officeholdings of a position
|
6
|
-
class
|
6
|
+
class MandatesQuery < ItemQuery
|
7
7
|
def raw_sparql
|
8
8
|
<<~SPARQL
|
9
9
|
# position-mandates
|
@@ -21,24 +21,20 @@ module WikidataPositionHistory
|
|
21
21
|
OPTIONAL { ?posn pq:P1545 ?ordinal }
|
22
22
|
OPTIONAL { ?posn pq:P5102 ?nature }
|
23
23
|
}
|
24
|
-
ORDER BY DESC(?start_date)
|
24
|
+
ORDER BY DESC(?start_date) ?item
|
25
25
|
SPARQL
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
# Represents a single row returned from the Mandates query
|
31
|
-
class
|
32
|
-
def initialize(row)
|
33
|
-
@row = row
|
34
|
-
end
|
35
|
-
|
31
|
+
class MandateRow < SPARQL::QueryRow
|
36
32
|
def ordinal
|
37
|
-
|
33
|
+
raw(:ordinal)
|
38
34
|
end
|
39
35
|
|
40
36
|
def officeholder
|
41
|
-
|
37
|
+
item_from(:item)
|
42
38
|
end
|
43
39
|
|
44
40
|
# TODO: rename or remove. 'item' is meaningless/ambiguous
|
@@ -46,14 +42,17 @@ module WikidataPositionHistory
|
|
46
42
|
officeholder.qlink
|
47
43
|
end
|
48
44
|
|
45
|
+
# TODO: switch to item_from
|
49
46
|
def prev
|
50
47
|
QueryService::WikidataItem.new(row.dig(:prev, :value)).qlink
|
51
48
|
end
|
52
49
|
|
50
|
+
# TODO: switch to item_from
|
53
51
|
def next
|
54
52
|
QueryService::WikidataItem.new(row.dig(:next, :value)).qlink
|
55
53
|
end
|
56
54
|
|
55
|
+
# TODO: switch to item_from
|
57
56
|
def nature
|
58
57
|
QueryService::WikidataItem.new(row.dig(:nature, :value)).id
|
59
58
|
end
|
@@ -63,35 +62,11 @@ module WikidataPositionHistory
|
|
63
62
|
end
|
64
63
|
|
65
64
|
def start_date
|
66
|
-
|
67
|
-
|
68
|
-
QueryService::WikidataDate.new(start_date_raw, start_date_precision)
|
65
|
+
date_from(:start_date, :start_precision)
|
69
66
|
end
|
70
67
|
|
71
68
|
def end_date
|
72
|
-
|
73
|
-
|
74
|
-
QueryService::WikidataDate.new(end_date_raw, end_date_precision)
|
75
|
-
end
|
76
|
-
|
77
|
-
def start_date_raw
|
78
|
-
row.dig(:start_date, :value).to_s[0..9]
|
79
|
-
end
|
80
|
-
|
81
|
-
def end_date_raw
|
82
|
-
row.dig(:end_date, :value).to_s[0..9]
|
83
|
-
end
|
84
|
-
|
85
|
-
def start_date_precision
|
86
|
-
row.dig(:start_precision, :value)
|
69
|
+
date_from(:end_date, :end_precision)
|
87
70
|
end
|
88
|
-
|
89
|
-
def end_date_precision
|
90
|
-
row.dig(:end_precision, :value)
|
91
|
-
end
|
92
|
-
|
93
|
-
private
|
94
|
-
|
95
|
-
attr_reader :row
|
96
71
|
end
|
97
72
|
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WikidataPositionHistory
|
4
|
+
module SPARQL
|
5
|
+
# SPARQL for fetching metadata about a position
|
6
|
+
class PositionQuery < ItemQuery
|
7
|
+
def raw_sparql
|
8
|
+
<<~SPARQL
|
9
|
+
# position-metadata
|
10
|
+
|
11
|
+
SELECT DISTINCT ?item ?inception ?inception_precision ?abolition ?abolition_precision
|
12
|
+
?replaces ?replacedBy ?derivedReplaces ?derivedReplacedBy
|
13
|
+
?isPosition ?isLegislator
|
14
|
+
WHERE {
|
15
|
+
VALUES ?item { wd:%s }
|
16
|
+
BIND(EXISTS { wd:%s wdt:P279+ wd:Q4164871 } as ?isPosition)
|
17
|
+
BIND(EXISTS { wd:%s wdt:P279+ wd:Q4175034 } as ?isLegislator)
|
18
|
+
OPTIONAL { ?item p:P571 [ a wikibase:BestRank ;
|
19
|
+
psv:P571 [ wikibase:timeValue ?inception; wikibase:timePrecision ?inception_precision ]
|
20
|
+
] }
|
21
|
+
OPTIONAL { ?item p:P576 [ a wikibase:BestRank ;
|
22
|
+
psv:P576 [ wikibase:timeValue ?abolition; wikibase:timePrecision ?abolition_precision ]
|
23
|
+
] }
|
24
|
+
OPTIONAL { ?item wdt:P1365 ?replaces }
|
25
|
+
OPTIONAL { ?item wdt:P1366 ?replacedBy }
|
26
|
+
OPTIONAL { ?derivedReplaces wdt:P1366 ?item }
|
27
|
+
OPTIONAL { ?derivedReplacedBy wdt:P1365 ?item }
|
28
|
+
}
|
29
|
+
SPARQL
|
30
|
+
end
|
31
|
+
|
32
|
+
def sparql_args
|
33
|
+
[itemid] * 3
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Represents a single row returned from the Position query
|
39
|
+
class PositionRow < SPARQL::QueryRow
|
40
|
+
def item
|
41
|
+
item_from(:item)
|
42
|
+
end
|
43
|
+
|
44
|
+
def inception_date
|
45
|
+
date_from(:inception, :inception_precision)
|
46
|
+
end
|
47
|
+
|
48
|
+
def abolition_date
|
49
|
+
date_from(:abolition, :abolition_precision)
|
50
|
+
end
|
51
|
+
|
52
|
+
def replaces
|
53
|
+
item_from(:replaces)
|
54
|
+
end
|
55
|
+
|
56
|
+
def replaced_by
|
57
|
+
item_from(:replacedBy)
|
58
|
+
end
|
59
|
+
|
60
|
+
def derived_replaces
|
61
|
+
item_from(:derivedReplaces)
|
62
|
+
end
|
63
|
+
|
64
|
+
def derived_replaced_by
|
65
|
+
item_from(:derivedReplacedBy)
|
66
|
+
end
|
67
|
+
|
68
|
+
def position?
|
69
|
+
raw(:isPosition) == 'true'
|
70
|
+
end
|
71
|
+
|
72
|
+
def legislator?
|
73
|
+
raw(:isLegislator) == 'true'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -2,11 +2,12 @@
|
|
2
2
|
|
3
3
|
require 'query_service'
|
4
4
|
require 'sparql/item_query'
|
5
|
-
require 'sparql/
|
6
|
-
require 'sparql/
|
7
|
-
require 'sparql/
|
5
|
+
require 'sparql/position_query'
|
6
|
+
require 'sparql/bio_query'
|
7
|
+
require 'sparql/mandates_query'
|
8
8
|
require 'wikidata_position_history/checks'
|
9
9
|
require 'wikidata_position_history/template'
|
10
|
+
require 'wikidata_position_history/output_row'
|
10
11
|
require 'wikidata_position_history/report'
|
11
12
|
require 'wikidata_position_history/version'
|
12
13
|
|
@@ -25,11 +25,11 @@ module WikidataPositionHistory
|
|
25
25
|
current.prev
|
26
26
|
end
|
27
27
|
|
28
|
-
def
|
28
|
+
def later_holder?
|
29
29
|
!!later
|
30
30
|
end
|
31
31
|
|
32
|
-
def
|
32
|
+
def earlier_holder?
|
33
33
|
!!earlier
|
34
34
|
end
|
35
35
|
end
|
@@ -92,7 +92,7 @@ module WikidataPositionHistory
|
|
92
92
|
# Does the 'replaces' match the previous item in the list?
|
93
93
|
class WrongPredecessor < Check
|
94
94
|
def problem?
|
95
|
-
|
95
|
+
earlier_holder? && !!predecessor && (earlier.item != predecessor)
|
96
96
|
end
|
97
97
|
|
98
98
|
def headline
|
@@ -104,10 +104,25 @@ module WikidataPositionHistory
|
|
104
104
|
end
|
105
105
|
end
|
106
106
|
|
107
|
+
# Is there a 'replaces' but no previous item in the list?
|
108
|
+
class MissingPredecessor < Check
|
109
|
+
def problem?
|
110
|
+
predecessor && !earlier_holder?
|
111
|
+
end
|
112
|
+
|
113
|
+
def headline
|
114
|
+
'Inconsistent predecessor'
|
115
|
+
end
|
116
|
+
|
117
|
+
def possible_explanation
|
118
|
+
"#{current.item} has a {{P|1365}} of #{predecessor}, but does not follow anyone here"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
107
122
|
# Does the 'replaced by' match the next item in the list?
|
108
123
|
class WrongSuccessor < Check
|
109
124
|
def problem?
|
110
|
-
|
125
|
+
later_holder? && !!successor && (later.item != successor)
|
111
126
|
end
|
112
127
|
|
113
128
|
def headline
|
@@ -119,6 +134,21 @@ module WikidataPositionHistory
|
|
119
134
|
end
|
120
135
|
end
|
121
136
|
|
137
|
+
# Is there a 'replaced by' but no next item in the list?
|
138
|
+
class MissingSuccessor < Check
|
139
|
+
def problem?
|
140
|
+
successor && !later_holder?
|
141
|
+
end
|
142
|
+
|
143
|
+
def headline
|
144
|
+
'Inconsistent successor'
|
145
|
+
end
|
146
|
+
|
147
|
+
def possible_explanation
|
148
|
+
"#{current.item} has a {{P|1366}} of #{successor}, but is not followed by anyone here"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
122
152
|
# Does the end date overlap with the successor's start date?
|
123
153
|
class Overlap < Check
|
124
154
|
def problem?
|
@@ -0,0 +1,143 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module WikidataPositionHistory
|
4
|
+
# simplified version of a WikidataPositionHistory::Check
|
5
|
+
Warning = Struct.new(:headline, :explanation)
|
6
|
+
|
7
|
+
class OutputRow
|
8
|
+
# Date for a single mandate row, to be passed to the report template
|
9
|
+
class Mandate
|
10
|
+
CHECKS = [Check::MissingFields, Check::Overlap,
|
11
|
+
Check::WrongPredecessor, Check::MissingPredecessor,
|
12
|
+
Check::WrongSuccessor, Check::MissingSuccessor].freeze
|
13
|
+
|
14
|
+
def initialize(later, current, earlier)
|
15
|
+
@later = later
|
16
|
+
@current = current
|
17
|
+
@earlier = earlier
|
18
|
+
end
|
19
|
+
|
20
|
+
def ordinal_string
|
21
|
+
ordinal = current.ordinal or return ''
|
22
|
+
"#{ordinal}."
|
23
|
+
end
|
24
|
+
|
25
|
+
def person
|
26
|
+
current.item
|
27
|
+
end
|
28
|
+
|
29
|
+
def dates
|
30
|
+
dates = [current.start_date, current.end_date]
|
31
|
+
return '' if dates.compact.empty?
|
32
|
+
|
33
|
+
dates.join(' – ')
|
34
|
+
end
|
35
|
+
|
36
|
+
def acting?
|
37
|
+
current.acting?
|
38
|
+
end
|
39
|
+
|
40
|
+
def warnings
|
41
|
+
CHECKS.map { |klass| klass.new(later, current, earlier) }.select(&:problem?)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
attr_reader :later, :current, :earlier
|
47
|
+
end
|
48
|
+
|
49
|
+
# Base class for the Inception/Abolition date rows
|
50
|
+
class PositionDate
|
51
|
+
def initialize(metadata)
|
52
|
+
@metadata = metadata
|
53
|
+
end
|
54
|
+
|
55
|
+
def date
|
56
|
+
return if dates.empty?
|
57
|
+
|
58
|
+
dates.join(' / ')
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
attr_reader :metadata
|
64
|
+
|
65
|
+
def position_id
|
66
|
+
metadata.position.id
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Data for the Inception date of the position
|
71
|
+
class Inception < PositionDate
|
72
|
+
def warnings
|
73
|
+
count = dates.count
|
74
|
+
return [] if count == 1
|
75
|
+
return [Warning.new('Missing field', "{{PositionHolderHistory/warning_no_inception_date|item=#{position_id}}}")] if count.zero?
|
76
|
+
|
77
|
+
[Warning.new('Multiple values', "{{PositionHolderHistory/warning_multiple_inception_dates|item=#{position_id}}}")]
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def dates
|
83
|
+
metadata.inception_dates
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# Data for the Abolition date of the position
|
88
|
+
class Abolition < PositionDate
|
89
|
+
def warnings
|
90
|
+
return [] unless dates.count > 1
|
91
|
+
|
92
|
+
[Warning.new('Multiple values', "{{PositionHolderHistory/warning_multiple_abolition_dates|item=#{position_id}}}")]
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def dates
|
98
|
+
metadata.abolition_dates
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Data for related position: e.g. Successor/Predecessor
|
103
|
+
class RelatedPosition
|
104
|
+
def initialize(metadata)
|
105
|
+
@metadata = metadata
|
106
|
+
end
|
107
|
+
|
108
|
+
def position
|
109
|
+
return if implied_list.empty?
|
110
|
+
|
111
|
+
(implied_list.direct.map(&:qblink) + implied_list.indirect_only.map(&:qblink_i)).join(', ')
|
112
|
+
end
|
113
|
+
|
114
|
+
def warnings
|
115
|
+
implied_list.indirect_only.map do |from|
|
116
|
+
Warning.new('Indirect only', "{{PositionHolderHistory/#{indirect_warning_template}|from=#{from.id}|to=#{metadata.position.id}}}")
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
private
|
121
|
+
|
122
|
+
attr_reader :metadata
|
123
|
+
|
124
|
+
def indirect_warning_template
|
125
|
+
format('warning_indirect_%s', self.class.name.split('::').last.downcase)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# Data for the position that comes after this one
|
130
|
+
class Successor < RelatedPosition
|
131
|
+
def implied_list
|
132
|
+
metadata.replaced_by_combined
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Data for the position that came before this one
|
137
|
+
class Predecessor < RelatedPosition
|
138
|
+
def implied_list
|
139
|
+
metadata.replaces_combined
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
@@ -1,43 +1,96 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module WikidataPositionHistory
|
4
|
-
#
|
5
|
-
|
6
|
-
|
4
|
+
# A list made up of both direct and indirect claims, where we
|
5
|
+
# can tell which came from which, when required
|
6
|
+
class ImpliedList
|
7
|
+
def initialize(direct, indirect)
|
8
|
+
@direct = direct
|
9
|
+
@indirect = indirect
|
10
|
+
end
|
7
11
|
|
8
|
-
def
|
9
|
-
|
10
|
-
@current = current
|
11
|
-
@earlier = earlier
|
12
|
+
def empty?
|
13
|
+
all.empty?
|
12
14
|
end
|
13
15
|
|
14
|
-
def
|
15
|
-
|
16
|
-
"#{ordinal}."
|
16
|
+
def all
|
17
|
+
direct | indirect
|
17
18
|
end
|
18
19
|
|
19
|
-
def
|
20
|
-
|
20
|
+
def both
|
21
|
+
direct & indirect
|
21
22
|
end
|
22
23
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
24
|
+
def direct_only
|
25
|
+
direct - indirect
|
26
|
+
end
|
27
|
+
|
28
|
+
def indirect_only
|
29
|
+
indirect - direct
|
30
|
+
end
|
31
|
+
|
32
|
+
attr_reader :direct, :indirect
|
33
|
+
end
|
34
|
+
|
35
|
+
# Data about the position itself, to be passed to the report template
|
36
|
+
class Metadata
|
37
|
+
def initialize(rows)
|
38
|
+
@rows = rows
|
39
|
+
end
|
40
|
+
|
41
|
+
def position
|
42
|
+
rows.map(&:item).first
|
43
|
+
end
|
44
|
+
|
45
|
+
def predecessor
|
46
|
+
@predecessor ||= OutputRow::Predecessor.new(self)
|
47
|
+
end
|
48
|
+
|
49
|
+
def successor
|
50
|
+
@successor ||= OutputRow::Successor.new(self)
|
51
|
+
end
|
26
52
|
|
27
|
-
|
53
|
+
def inception
|
54
|
+
@inception ||= OutputRow::Inception.new(self)
|
28
55
|
end
|
29
56
|
|
30
|
-
def
|
31
|
-
|
57
|
+
def abolition
|
58
|
+
@abolition ||= OutputRow::Abolition.new(self)
|
32
59
|
end
|
33
60
|
|
34
|
-
def
|
35
|
-
|
61
|
+
def position?
|
62
|
+
# this should be the same everywhere
|
63
|
+
rows.map(&:position?).first
|
64
|
+
end
|
65
|
+
|
66
|
+
def legislator?
|
67
|
+
# this should be the same everywhere
|
68
|
+
rows.map(&:legislator?).first
|
69
|
+
end
|
70
|
+
|
71
|
+
def replaces_combined
|
72
|
+
@replaces_combined ||= ImpliedList.new(uniq_by_id(:replaces), uniq_by_id(:derived_replaces))
|
73
|
+
end
|
74
|
+
|
75
|
+
def replaced_by_combined
|
76
|
+
@replaced_by_combined ||= ImpliedList.new(uniq_by_id(:replaced_by), uniq_by_id(:derived_replaced_by))
|
77
|
+
end
|
78
|
+
|
79
|
+
def inception_dates
|
80
|
+
rows.map(&:inception_date).compact.uniq(&:to_s).sort
|
81
|
+
end
|
82
|
+
|
83
|
+
def abolition_dates
|
84
|
+
rows.map(&:abolition_date).compact.uniq(&:to_s).sort
|
36
85
|
end
|
37
86
|
|
38
87
|
private
|
39
88
|
|
40
|
-
attr_reader :
|
89
|
+
attr_reader :rows
|
90
|
+
|
91
|
+
def uniq_by_id(method)
|
92
|
+
rows.map(&method).compact.uniq(&:id).sort_by(&:id)
|
93
|
+
end
|
41
94
|
end
|
42
95
|
|
43
96
|
# The entire wikitext generated for this report
|
@@ -50,9 +103,10 @@ module WikidataPositionHistory
|
|
50
103
|
attr_reader :position_id, :template_class
|
51
104
|
|
52
105
|
def wikitext
|
106
|
+
return legislator_template if metadata.legislator?
|
53
107
|
return no_items_output if mandates.empty?
|
54
108
|
|
55
|
-
output
|
109
|
+
template_class.new(template_params).output
|
56
110
|
end
|
57
111
|
|
58
112
|
def header
|
@@ -60,7 +114,7 @@ module WikidataPositionHistory
|
|
60
114
|
end
|
61
115
|
|
62
116
|
def position_dates
|
63
|
-
dates = [metadata.
|
117
|
+
dates = [metadata.inception.date, metadata.abolition.date]
|
64
118
|
return '' if dates.compact.empty?
|
65
119
|
|
66
120
|
format('(%s)', dates.join(' – '))
|
@@ -81,13 +135,11 @@ module WikidataPositionHistory
|
|
81
135
|
private
|
82
136
|
|
83
137
|
def metadata
|
84
|
-
|
85
|
-
# multiple dates
|
86
|
-
@metadata ||= SPARQL::PositionData.new(position_id).results_as(PositionData).first
|
138
|
+
@metadata ||= Metadata.new(SPARQL::PositionQuery.new(position_id).results_as(PositionRow))
|
87
139
|
end
|
88
140
|
|
89
141
|
def biodata
|
90
|
-
@biodata ||= SPARQL::
|
142
|
+
@biodata ||= SPARQL::BioQuery.new(position_id).results_as(BioRow)
|
91
143
|
end
|
92
144
|
|
93
145
|
def biodata_for(officeholder)
|
@@ -99,25 +151,25 @@ module WikidataPositionHistory
|
|
99
151
|
end
|
100
152
|
|
101
153
|
def sparql
|
102
|
-
@sparql ||= SPARQL::
|
154
|
+
@sparql ||= SPARQL::MandatesQuery.new(position_id)
|
103
155
|
end
|
104
156
|
|
105
157
|
def mandates
|
106
|
-
@mandates ||= sparql.results_as(
|
158
|
+
@mandates ||= sparql.results_as(MandateRow)
|
107
159
|
end
|
108
160
|
|
109
161
|
def no_items_output
|
110
162
|
"\n{{PositionHolderHistory/error_no_holders|id=#{position_id}}}\n"
|
111
163
|
end
|
112
164
|
|
113
|
-
def
|
114
|
-
|
165
|
+
def legislator_template
|
166
|
+
"\n{{PositionHolderHistory/error_legislator|id=#{position_id}}}\n"
|
115
167
|
end
|
116
168
|
|
117
169
|
def table_rows
|
118
170
|
padded_mandates.each_cons(3).map do |later, current, earlier|
|
119
171
|
{
|
120
|
-
mandate:
|
172
|
+
mandate: OutputRow::Mandate.new(later, current, earlier),
|
121
173
|
bio: biodata_for(current.officeholder),
|
122
174
|
}
|
123
175
|
end
|
@@ -22,10 +22,28 @@ module WikidataPositionHistory
|
|
22
22
|
def template_text
|
23
23
|
<<~ERB
|
24
24
|
{| class="wikitable" style="text-align: center; border: none;"
|
25
|
-
<% if metadata.
|
25
|
+
<% if metadata.abolition.date -%>
|
26
26
|
|-
|
27
|
-
| colspan="
|
28
|
-
| style="
|
27
|
+
| colspan="2" style="border: none; background: #fff; font-size: 1.15em; text-align: right;" | '''Position abolished''':
|
28
|
+
| style="border: none; background: #fff; text-align: left;" | <%= metadata.abolition.date %>
|
29
|
+
| style="border: none; background: #fff; text-align: left;" | \
|
30
|
+
<% metadata.abolition.warnings.each do |warning| -%>
|
31
|
+
<span style="display: block">[[File:Pictogram voting comment.svg|15px|link=]] <span style="color: #d33; font-weight: bold; vertical-align: middle;"><%= warning.headline %></span> <ref><%= warning.explanation %></ref></span>\
|
32
|
+
<% end %>
|
33
|
+
<% end -%>
|
34
|
+
<% if metadata.successor.position -%>
|
35
|
+
|-
|
36
|
+
| colspan="2" style="border: none; background: #fff; font-size: 1.15em; text-align: right;" | '''Replaced by''':
|
37
|
+
| style=" border: none; background: #fff; text-align: left;" | <%= metadata.successor.position %>
|
38
|
+
| style=" border: none; background: #fff; text-align: left;" | \
|
39
|
+
<% metadata.successor.warnings.each do |warning| -%>
|
40
|
+
<span style="display: block">[[File:Pictogram voting comment.svg|15px|link=]] <span style="color: #d33; font-weight: bold; vertical-align: middle;"><%= warning.headline %></span> <ref><%= warning.explanation %></ref></span>\
|
41
|
+
<% end %>
|
42
|
+
<% end -%>
|
43
|
+
<% if metadata.successor.position || metadata.abolition.date -%>
|
44
|
+
|-
|
45
|
+
| colspan="3" style="padding:0.5em; border: none; background: #fff"> |
|
46
|
+
| colspan="1" style="padding:0.5em; border: none; background: #fff"> |
|
29
47
|
<% end -%>
|
30
48
|
<% table_rows.map(&:values).each do |mandate, bio| -%>
|
31
49
|
|-
|
@@ -37,10 +55,28 @@ module WikidataPositionHistory
|
|
37
55
|
<span style="display: block">[[File:Pictogram voting comment.svg|15px|link=]] <span style="color: #d33; font-weight: bold; vertical-align: middle;"><%= warning.headline %></span> <ref><%= warning.explanation %></ref></span>\
|
38
56
|
<% end %>
|
39
57
|
<% end -%>
|
40
|
-
<% if metadata.
|
58
|
+
<% if metadata.successor.position || metadata.abolition.date -%>
|
59
|
+
|-
|
60
|
+
| colspan="3" style="padding:0.5em; border: none; background: #fff"> |
|
61
|
+
| colspan="1" style="padding:0.5em; border: none; background: #fff"> |
|
62
|
+
<% end -%>
|
63
|
+
<% if metadata.inception.date -%>
|
64
|
+
|-
|
65
|
+
| colspan="2" style="border: none; background: #fff; font-size: 1.15em; text-align: right;" | '''Position created''':
|
66
|
+
| style="border: none; background: #fff; text-align: left;" | <%= metadata.inception.date %>
|
67
|
+
| style="padding:0.5em 2em 0.5em 1em; border: none; background: #fff; text-align: left;" | \
|
68
|
+
<% metadata.inception.warnings.each do |warning| -%>
|
69
|
+
<span style="display: block">[[File:Pictogram voting comment.svg|15px|link=]] <span style="color: #d33; font-weight: bold; vertical-align: middle;"><%= warning.headline %></span> <ref><%= warning.explanation %></ref></span>\
|
70
|
+
<% end %>
|
71
|
+
<% end -%>
|
72
|
+
<% if metadata.predecessor.position -%>
|
41
73
|
|-
|
42
|
-
| colspan="
|
43
|
-
| style="
|
74
|
+
| colspan="2" style=" border: none; background: #fff; font-size: 1.15em; text-align: right;" | '''Replaces''':
|
75
|
+
| style="border: none; background: #fff; text-align: left;" | <%= metadata.predecessor.position %>
|
76
|
+
| style="padding:0.5em 2em 0.5em 1em; border: none; background: #fff; text-align: left;" | \
|
77
|
+
<% metadata.predecessor.warnings.each do |warning| -%>
|
78
|
+
<span style="display: block">[[File:Pictogram voting comment.svg|15px|link=]] <span style="color: #d33; font-weight: bold; vertical-align: middle;"><%= warning.headline %></span> <ref><%= warning.explanation %></ref></span>\
|
79
|
+
<% end %>
|
44
80
|
<% end -%>
|
45
81
|
|}
|
46
82
|
|
@@ -36,5 +36,5 @@ Gem::Specification.new do |spec|
|
|
36
36
|
spec.add_development_dependency 'rubocop-performance', '~> 1.8.0'
|
37
37
|
spec.add_development_dependency 'rubocop-rspec', '~> 1.43.2'
|
38
38
|
spec.add_development_dependency 'warning', '~> 1.1'
|
39
|
-
spec.add_development_dependency 'webmock', '~> 3.
|
39
|
+
spec.add_development_dependency 'webmock', '~> 3.9.1'
|
40
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wikidata_position_history
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tony Bowden
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-09-
|
12
|
+
date: 2020-09-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: mediawiki-replaceable-content
|
@@ -171,14 +171,14 @@ dependencies:
|
|
171
171
|
requirements:
|
172
172
|
- - "~>"
|
173
173
|
- !ruby/object:Gem::Version
|
174
|
-
version: 3.
|
174
|
+
version: 3.9.1
|
175
175
|
type: :development
|
176
176
|
prerelease: false
|
177
177
|
version_requirements: !ruby/object:Gem::Requirement
|
178
178
|
requirements:
|
179
179
|
- - "~>"
|
180
180
|
- !ruby/object:Gem::Version
|
181
|
-
version: 3.
|
181
|
+
version: 3.9.1
|
182
182
|
description:
|
183
183
|
email:
|
184
184
|
- tony@tmtm.com
|
@@ -204,12 +204,13 @@ files:
|
|
204
204
|
- exe/position-history-for-item
|
205
205
|
- exe/update_wikidata_page
|
206
206
|
- lib/query_service.rb
|
207
|
-
- lib/sparql/
|
207
|
+
- lib/sparql/bio_query.rb
|
208
208
|
- lib/sparql/item_query.rb
|
209
|
-
- lib/sparql/
|
210
|
-
- lib/sparql/
|
209
|
+
- lib/sparql/mandates_query.rb
|
210
|
+
- lib/sparql/position_query.rb
|
211
211
|
- lib/wikidata_position_history.rb
|
212
212
|
- lib/wikidata_position_history/checks.rb
|
213
|
+
- lib/wikidata_position_history/output_row.rb
|
213
214
|
- lib/wikidata_position_history/report.rb
|
214
215
|
- lib/wikidata_position_history/template.rb
|
215
216
|
- lib/wikidata_position_history/version.rb
|
data/lib/sparql/position_data.rb
DELETED
@@ -1,66 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module WikidataPositionHistory
|
4
|
-
module SPARQL
|
5
|
-
# SPARQL for fetching metadata about a position
|
6
|
-
class PositionData < ItemQuery
|
7
|
-
def raw_sparql
|
8
|
-
<<~SPARQL
|
9
|
-
# position-metadata
|
10
|
-
|
11
|
-
SELECT DISTINCT ?inception ?inception_precision ?abolition ?abolition_precision ?isPosition
|
12
|
-
WHERE {
|
13
|
-
VALUES ?item { wd:%s }
|
14
|
-
BIND(EXISTS { ?item wdt:P279+ wd:Q4164871 } as ?isPosition)
|
15
|
-
OPTIONAL { ?item p:P571/psv:P571 [ wikibase:timeValue ?inception; wikibase:timePrecision ?inception_precision ] }
|
16
|
-
OPTIONAL { ?item p:P576/psv:P576 [ wikibase:timeValue ?abolition; wikibase:timePrecision ?abolition_precision ] }
|
17
|
-
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
|
18
|
-
}
|
19
|
-
SPARQL
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
# Represents a single row returned from the Position query
|
25
|
-
class PositionData
|
26
|
-
def initialize(row)
|
27
|
-
@row = row
|
28
|
-
end
|
29
|
-
|
30
|
-
def inception_date
|
31
|
-
return if inception_date_raw.empty?
|
32
|
-
|
33
|
-
QueryService::WikidataDate.new(inception_date_raw, inception_date_precision)
|
34
|
-
end
|
35
|
-
|
36
|
-
def abolition_date
|
37
|
-
return if abolition_date_raw.empty?
|
38
|
-
|
39
|
-
QueryService::WikidataDate.new(abolition_date_raw, abolition_date_precision)
|
40
|
-
end
|
41
|
-
|
42
|
-
def position?
|
43
|
-
row.dig(:isPosition, :value) == 'true'
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
attr_reader :row
|
49
|
-
|
50
|
-
def inception_date_raw
|
51
|
-
row.dig(:inception, :value).to_s[0..9]
|
52
|
-
end
|
53
|
-
|
54
|
-
def abolition_date_raw
|
55
|
-
row.dig(:abolition, :value).to_s[0..9]
|
56
|
-
end
|
57
|
-
|
58
|
-
def inception_date_precision
|
59
|
-
row.dig(:inception_precision, :value)
|
60
|
-
end
|
61
|
-
|
62
|
-
def abolition_date_precision
|
63
|
-
row.dig(:abolition_precision, :value)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|