]> git.openstreetmap.org Git - nominatim.git/blob - data-sources/wikipedia-wikidata/import_wikipedia.sh
refactor import_wiki* script for readability
[nominatim.git] / data-sources / wikipedia-wikidata / import_wikipedia.sh
1 #!/bin/bash
2
3 psqlcmd() {
4      psql --quiet wikiprocessingdb
5 }
6
7 mysql2pgsqlcmd() {
8      ./mysql2pgsql.perl /dev/stdin /dev/stdout
9 }
10
11 download() {
12      echo "Downloading $1"
13      wget --quiet --no-clobber --tries 3
14 }
15
16
17 # languages to process (refer to List of Wikipedias here: https://en.wikipedia.org/wiki/List_of_Wikipedias)
18 # requires Bash 4.0
19 readarray -t LANGUAGES < languages.txt
20
21
22
23 echo "====================================================================="
24 echo "Create wikipedia calculation tables"
25 echo "====================================================================="
26
27 echo "CREATE TABLE linkcounts (
28         language text,
29         title    text,
30         count    integer,
31         sumcount integer,
32         lat      double precision,
33         lon      double precision
34      );"  | psqlcmd
35
36 echo "CREATE TABLE wikipedia_article (
37         language    text NOT NULL,
38         title       text NOT NULL,
39         langcount   integer,
40         othercount  integer,
41         totalcount  integer,
42         lat double  precision,
43         lon double  precision,
44         importance  double precision,
45         title_en    text,
46         osm_type    character(1),
47         osm_id      bigint
48       );" | psqlcmd
49
50 echo "CREATE TABLE wikipedia_redirect (
51         language   text,
52         from_title text,
53         to_title   text
54      );" | psqlcmd
55
56
57
58
59
60 echo "====================================================================="
61 echo "Download individual wikipedia language tables"
62 echo "====================================================================="
63
64
65 for i in "${LANGUAGES[@]}"
66 do
67     echo "Language: $i"
68
69     # english is the largest
70     # 1.7G  enwiki-latest-page.sql.gz
71     # 6.2G  enwiki-latest-pagelinks.sql.gz
72     # 355M  enwiki-latest-langlinks.sql.gz
73     # 128M  enwiki-latest-redirect.sql.gz
74
75     # example of smaller languge turkish
76     #  53M  trwiki-latest-page.sql.gz
77     # 176M  trwiki-latest-pagelinks.sql.gz
78     # 106M  trwiki-latest-langlinks.sql.gz
79     # 3.2M  trwiki-latest-redirect.sql.gz
80
81     download https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-page.sql.gz
82     download https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-pagelinks.sql.gz
83     download https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-langlinks.sql.gz
84     download https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-redirect.sql.gz
85 done
86
87
88
89
90
91 echo "====================================================================="
92 echo "Import individual wikipedia language tables"
93 echo "====================================================================="
94
95 for i in "${LANGUAGES[@]}"
96 do
97     echo "Language: $i"
98
99     echo "Importing ${i}wiki-latest-pagelinks"
100     gzip -dc ${i}wiki-latest-pagelinks.sql.gz | sed "s/\`pagelinks\`/\`${i}pagelinks\`/g" | mysql2pgsqlcmd | psqlcmd
101
102     echo "Importing ${i}wiki-latest-page"
103     gzip -dc ${i}wiki-latest-page.sql.gz      | sed "s/\`page\`/\`${i}page\`/g"           | mysql2pgsqlcmd | psqlcmd
104
105     echo "Importing ${i}wiki-latest-langlinks"
106     gzip -dc ${i}wiki-latest-langlinks.sql.gz | sed "s/\`langlinks\`/\`${i}langlinks\`/g" | mysql2pgsqlcmd | psqlcmd
107
108     echo "Importing ${i}wiki-latest-redirect"
109     gzip -dc ${i}wiki-latest-redirect.sql.gz  | sed "s/\`redirect\`/\`${i}redirect\`/g"   | mysql2pgsqlcmd | psqlcmd
110 done
111
112
113
114
115
116 echo "====================================================================="
117 echo "Process language tables and associated pagelink counts"
118 echo "====================================================================="
119
120
121 for i in "${LANGUAGES[@]}"
122 do
123     echo "Language: $i"
124
125     echo "CREATE TABLE ${i}pagelinkcount
126           AS
127           SELECT pl_title AS title,
128                  COUNT(*) AS count
129           FROM ${i}pagelinks
130           WHERE pl_namespace = 0
131           GROUP BY pl_title
132           ;" | psqlcmd
133
134     echo "INSERT INTO linkcounts
135           SELECT '${i}',
136                  pl_title,
137                  COUNT(*)
138           FROM ${i}pagelinks
139           WHERE pl_namespace = 0
140           GROUP BY pl_title
141           ;" | psqlcmd
142
143     echo "INSERT INTO wikipedia_redirect
144           SELECT '${i}',
145                  page_title,
146                  rd_title
147           FROM ${i}redirect
148           JOIN ${i}page ON (rd_from = page_id)
149           WHERE page_namespace = 0
150             AND rd_namespace = 0
151           ;" | psqlcmd
152
153     echo "ALTER TABLE ${i}pagelinkcount
154           ADD COLUMN othercount integer
155           ;" | psqlcmd
156
157     echo "UPDATE ${i}pagelinkcount
158           SET othercount = 0
159           ;" | psqlcmd
160
161     for j in "${LANGUAGES[@]}"
162     do
163         echo "UPDATE ${i}pagelinkcount
164               SET othercount = ${i}pagelinkcount.othercount + x.count
165               FROM (
166                 SELECT page_title AS title,
167                        count
168                 FROM ${i}langlinks
169                 JOIN ${i}page ON (ll_from = page_id)
170                 JOIN ${j}pagelinkcount ON (ll_lang = '${j}' AND ll_title = title)
171               ) AS x
172               WHERE x.title = ${i}pagelinkcount.title
173               ;" | psqlcmd
174     done
175
176     echo "INSERT INTO wikipedia_article
177           SELECT '${i}',
178                  title,
179                  count,
180                  othercount,
181                  count + othercount
182           FROM ${i}pagelinkcount
183           ;" | psqlcmd
184 done
185
186
187
188
189
190 echo "====================================================================="
191 echo "Calculate importance score for each wikipedia page"
192 echo "====================================================================="
193
194 echo "UPDATE wikipedia_article
195       SET importance = LOG(totalcount)/LOG((SELECT MAX(totalcount) FROM wikipedia_article))
196       ;" | psqlcmd
197
198
199
200
201
202 echo "====================================================================="
203 echo "Clean up intermediate tables to conserve space"
204 echo "====================================================================="
205
206 for i in "${LANGUAGES[@]}"
207 do
208     echo "DROP TABLE ${i}pagelinks;"     | psqlcmd
209     echo "DROP TABLE ${i}page;"          | psqlcmd
210     echo "DROP TABLE ${i}langlinks;"     | psqlcmd
211     echo "DROP TABLE ${i}redirect;"      | psqlcmd
212     echo "DROP TABLE ${i}pagelinkcount;" | psqlcmd
213 done