Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/python # -*- coding: utf-8 -*- Update the statistics of the monuments database at c:Commons:Monuments_database/Statistics
FIXME: Too much code duplication. Should probably just have one list of the possible fields for the functions to work on. """
close_database_connection, connect_to_monuments_database )
''' Return the result of the query '''
"""Output the statistics in wikitext on Commons.""" ('country', None), ('lang', '[[:en:List of ISO 639-1 codes|lang]]'), ('all', 'total'), ('name', None), ('address', None), ('municipality', None), ('coordinates', None), ('image', None), ('commonscat', None), ('article', None), ('wikidata', None), ('adm0', '[[:en:ISO 3166-1 alpha-2#Officially assigned code elements|adm0]]'), ('adm1', '[[:en:ISO 3166-2#Current codes|adm1]]'), ('adm2', None), ('adm3', None), ('adm4', None), ('source', 'source pages') ])
'[//tools.wmflabs.org/heritage/api/api.php?' 'action=statistics&stcountry={country}&format=html&limit=0 {country}]') '[//tools.wmflabs.org/heritage/api/api.php?' 'action=adminlevels&format=json&admtree={adm0iso} {adm1}] ' '<small>({adm1_p}%)</small>' )
'|- class="sortbottom"\n' '| || || {all} ' '|| {name} <small>({name_p}%)</small> ' '|| {address} <small>({address_p}%)</small> ' '|| {municipality} <small>({municipality_p}%)</small> ' '|| {coordinates} <small>({coordinates_p}%)</small> ' '|| {image} <small>({image_p}%)</small> ' '|| {commonscat} <small>({commonscat_p}%)</small> ' '|| {article} <small>({article_p}%)</small> ' '|| {wikidata} <small>({wikidata_p}%)</small> ' '|| {adm0} <small>({adm0_p}%)</small> ' '|| {adm1} <small>({adm1_p}%)</small> ' '|| {adm2} <small>({adm2_p}%)</small> ' '|| {adm3} <small>({adm3_p}%)</small> ' '|| {adm4} <small>({adm4_p}%)</small> ' '|| {source} ' )
'|-\n' '|| {country} || {lang}' '|| colspan="15" | Datasource [//tools.wmflabs.org/heritage/monuments_config/{country}_{lang}.json ({country}, {lang})] is configured, but no monuments are in the database.\n' )
'country': country_format.format(**data), 'lang': data.get('lang'), 'all': data.get('all'), 'source': data.get('source'), 'adm1': adm1_format.format( adm1_p=compute_percentage( data.get('adm1'), data.get('all')), **data) } data.get(key), data.get('all'))
# construct total percentages totals[col], totals['all'])
''' Do a bunch of queries to gather the statistics. '''
('name', 'name'), ('address', 'address'), ('municipality', 'municipality'), ('coordinates', ('lat', 'lon')), ('image', 'image'), ('commonscat', 'commonscat'), ('article', 'monument_article'), ('wikidata', 'wd_item'), ('adm0', 'adm0'), ('adm1', 'adm1'), ('adm2', 'adm2'), ('adm3', 'adm3'), ('adm4', 'adm4'), ]
"SELECT COUNT(*) " "FROM monuments_all " "WHERE country='%s' AND lang='%s'") "SELECT adm0 " "FROM monuments_all " "WHERE country='%s' AND lang='%s' AND NOT (adm0='' OR adm0 IS NULL) " "LIMIT 1") "SELECT COUNT(DISTINCT(source)) " "FROM monuments_all " "WHERE country='%s' AND lang='%s'")
"SELECT COUNT(*) " "FROM monuments_all " "WHERE country='%s' AND lang='%s' AND ") else:
''' Get the languages for a certain country code. ''' "SELECT DISTINCT(lang) " "FROM monuments_all " "WHERE country=%s")
# print query % (country,)
result.append(language)
''' Get the list of country codes. ''' "SELECT DISTINCT(country) " "FROM monuments_all")
result.append(country)
"""The main loop.""" conn = None cursor = None skip_wd = False (conn, cursor) = connect_to_monuments_database()
for arg in pywikibot.handleArgs(): option, sep, value = arg.partition(':') if option == '-skip_wd': skip_wd = True else: raise Exception( 'Bad parameters. Expected "-skip_wd" or pywikibot args. ' 'Found "{}"'.format(option))
statistics = {}
for (countrycode, lang), countryconfig in mconfig.filtered_countries( skip_wd=skip_wd, skip_wlpa=True): if countrycode not in statistics: statistics[countrycode] = {} if lang not in statistics[countrycode]: statistics[countrycode][lang] = {}
for country in getCountries(conn, cursor): for language in getLanguages(country, conn, cursor): statistics[country][language] = get_summary_statistics( country, language, conn, cursor)
outputStatistics(statistics) close_database_connection(conn, cursor)
main() |