8 ./mysql2pgsql.perl /dev/stdin /dev/stdout
12 # list the languages to process (refer to List of Wikipedias here: https://en.wikipedia.org/wiki/List_of_Wikipedias)
14 language=( "ar" "bg" "ca" "cs" "da" "de" "en" "es" "eo" "eu" "fa" "fr" "ko" "hi" "hr" "id" "it" "he" "lt" "hu" "ms" "nl" "ja" "no" "pl" "pt" "kk" "ro" "ru" "sk" "sl" "sr" "fi" "sv" "tr" "uk" "vi" "vo" "war" "zh" )
17 # create wikipedia calculation tables
19 echo "CREATE TABLE linkcounts (language text, title text, count integer, sumcount integer, lat double precision, lon double precision);" | psqlcmd
20 echo "CREATE TABLE wikipedia_article (language text NOT NULL, title text NOT NULL, langcount integer, othercount integer, totalcount integer, lat double precision, lon double precision, importance double precision, title_en text, osm_type character(1), osm_id bigint );" | psqlcmd
21 echo "CREATE TABLE wikipedia_redirect (language text, from_title text, to_title text );" | psqlcmd
24 # download individual wikipedia language tables
26 for i in "${language[@]}"
28 wget https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-page.sql.gz
29 wget https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-pagelinks.sql.gz
30 wget https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-langlinks.sql.gz
31 wget https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-redirect.sql.gz
35 # import individual wikipedia language tables
37 for i in "${language[@]}"
39 gzip -dc ${i}wiki-latest-pagelinks.sql.gz | sed "s/\`pagelinks\`/\`${i}pagelinks\`/g" | mysql2pgsqlcmd | psqlcmd
40 gzip -dc ${i}wiki-latest-page.sql.gz | sed "s/\`page\`/\`${i}page\`/g" | mysql2pgsqlcmd | psqlcmd
41 gzip -dc ${i}wiki-latest-langlinks.sql.gz | sed "s/\`langlinks\`/\`${i}langlinks\`/g" | mysql2pgsqlcmd | psqlcmd
42 gzip -dc ${i}wiki-latest-redirect.sql.gz | sed "s/\`redirect\`/\`${i}redirect\`/g" | mysql2pgsqlcmd | psqlcmd
46 # process language tables and associated pagelink counts
48 for i in "${language[@]}"
50 echo "create table ${i}pagelinkcount as select pl_title as title,count(*) as count from ${i}pagelinks where pl_namespace = 0 group by pl_title;" | psqlcmd
51 echo "insert into linkcounts select '${i}',pl_title,count(*) from ${i}pagelinks where pl_namespace = 0 group by pl_title;" | psqlcmd
52 echo "insert into wikipedia_redirect select '${i}',page_title,rd_title from ${i}redirect join ${i}page on (rd_from = page_id) where page_namespace = 0 and rd_namespace = 0;" | psqlcmd
53 echo "alter table ${i}pagelinkcount add column othercount integer;" | psqlcmd
54 echo "update ${i}pagelinkcount set othercount = 0;" | psqlcmd
57 for i in "${language[@]}"
59 for j in "${language[@]}"
61 echo "update ${i}pagelinkcount set othercount = ${i}pagelinkcount.othercount + x.count from (select page_title as title,count from ${i}langlinks join ${i}page on (ll_from = page_id) join ${j}pagelinkcount on (ll_lang = '${j}' and ll_title = title)) as x where x.title = ${i}pagelinkcount.title;" | psqlcmd
63 echo "insert into wikipedia_article select '${i}', title, count, othercount, count+othercount from ${i}pagelinkcount;" | psqlcmd
67 # calculate importance score for each wikipedia page
69 echo "update wikipedia_article set importance = log(totalcount)/log((select max(totalcount) from wikipedia_article))" | psqlcmd
72 # clean up intermediate tables to conserve space
74 for i in "${language[@]}"
76 echo "DROP TABLE ${i}pagelinks;" | psqlcmd
77 echo "DROP TABLE ${i}page;" | psqlcmd
78 echo "DROP TABLE ${i}langlinks;" | psqlcmd
79 echo "DROP TABLE ${i}redirect;" | psqlcmd
80 echo "DROP TABLE ${i}pagelinkcount;" | psqlcmd