#!/usr/bin/env python # -*- coding: utf-8 -*- """ A simple demo program that grown beyond, see locateCoordv1.py for the simpler version This should really be scale back infavor of a better backend implementation """ import wikipedia import oursql, cgi import cgitb; cgitb.enable(logdir='tracebacks') # XXX copied from ghel.py globes = ( # Only object larger than XX are included. # Moons are indented, only important/well studied are listed "Sun", # Terrestrials "Mercury", "Venus", "Earth", "Moon", "Mars", "Phobos", "Deimos", # Asteroid belt "Ceres", # Jovian Planets (Gas Giants) "Jupiter", "Ganymede", "Callisto", "Io", "Europa", "Amalthea", "Thebe", "Adrastea", "Metis",# and 55 more "Saturn", "Mimas", "Enceladus", "Tethys", "Dione", "Rhea", "Titan", "Hyperion", "Iapetus", "Phoebe", "Janus", "Epimetheus", # and 50 more # TODO needs review "Pan", "Atlas", "Pandora", "Prometheus", "Janus", "Telesto", "Calypso", "Helene", "Uranus", "Miranda", "Ariel", "Umbriel", "Titania", "Oberon", "Puck", # and 21 more # TODO needs review "Cordelia", "Ophelia", "Bianca", "Cressida", "Desdemona", "Juliet", "Portia", "Rosalind", "Belinda", "Puck", "Neptune", "Triton", "Nereid", "Proteus", # and 10 more # TODO needs review "Naiad", "Thalassa", "Despina", "Galatea", "Larissa", # Kuiper belt "Pluto", "Charon", "Haumea", "Makemake", "Eris" ) connections = {} def getConn(dbname, host=None): if not dbname.endswith('_p'): dbname+='_p' if (host,dbname) not in connections: connections[host,dbname] = oursql.connect( db=dbname, host=host or dbname.replace('_', '-')+'.userdb.toolserver.org', read_default_file="/home/dispenser/.my.cnf", charset=None, use_unicode=False ) return connections[host,dbname] def main(): form = cgi.FieldStorage() site = wikipedia.getSite() # Support older URL where dbname was the table name if form.getfirst('dbname', '').startswith('coord_'): try:site = wikipedia.Site(form.getfirst('dbname')[6:], 'wikipedia') except:pass ghel_table= "u_dispenser_p.coord_%s" % site.dbName() globe = form.getfirst('globe', '') # try: def get_form_float(name, defaultValue): try:return float(form.getfirst(name, defaultValue)) except ValueError:return defaultValue lat1 = get_form_float('lat1', 0.0) lat2 = get_form_float('lat2', 0.0) lon1 = get_form_float('lon1', 0.0) lon2 = get_form_float('lon2', 0.0) limit = 500 offset = int(get_form_float('offset', 0)) if not any((lat1, lat2, lon1, lon2)): # based on http://toolserver.org/~kolossos/wp-world/umkreis-source.php # by http://de.wikipedia.org/wiki/Benutzer:Kolossos import math range_deg = (get_form_float('range_km', 0) or 0.0001)/111.3 # lat = get_form_float('lat', 0.0) lat1 = lat - range_deg/2.0 lat2 = lat + range_deg/2.0 # lon = (get_form_float('lon', 0.0)+180) % 360 - 180 lon1 = lon - range_deg/2.0/math.cos(lat/57.27) lon2 = lon + range_deg/2.0/math.cos(lat/57.27) # Swap if relations are reversed if lat1 > lat2: lat1, lat2 = lat2, lat1 if lon1 > lon2: lon1, lon2 = lon2, lon1 except (TypeError, ValueError) as errmsg: wikipedia.output("Input error: \03{lightred}%s\03{default}"%(errmsg,)) return wikipedia.logtime('Finished reading inputs') conn = getConn(site.dbName()) cursor = conn.cursor() cursor.execute("""/* locateCoord LIMIT:30 */ SELECT DATE_FORMAT(IFNULL(UPDATE_TIME, CREATE_TIME), '%d %M %Y at %H:%i UTC') FROM information_schema.tables WHERE TABLE_SCHEMA=? AND TABLE_NAME=? """, ghel_table.partition('.')[::2]) updated = cursor.fetchall() if updated: updated=updated[0][0] wikipedia.logtime('Lookup tables Last-Modified') print """
Locate articles in geographic region
%s
Site: %s
Globe:
to latitude
to longitude
Last updated on %s | database dumps | documentation
""" % ( '' if globe in ('', 'Earth') else '', ( 90.0-lat2)//2, (180.0+lon1)//2, ( lon2-lon1)//2, ( lat2-lat1)//2, ''%wikipedia.escape(site.dbName()), '\n'.join( '%s'%( ' selected="selected"' if globe==s else '', wikipedia.escape(s), ) for s in globes ),# if globe in globes else ''%wikipedia.escape(globe), lat1, lat2, lon1, lon2, updated, ) where_conditions = [] where_data = [] def addWhere(condition, params=()): where_conditions.append(condition) for param in params: where_data.append(param) addWhere("MBRWithIn(gc_location, Envelope(LineString(Point(?, ?), Point(?, ?))))", (lat1,lon1,lat2,lon2)) if globe: addWhere("gc_globe = ?", ('' if globe=='Earth' else globe,)) if form.getfirst('region'): addWhere("gc_region LIKE ?", ("/%s%%"%form.getfirst('region'),)) addWhere("gc_region LIKE ?", ("%s%%" %form.getfirst('region'),)) if form.getfirst('type') != None: addWhere("gc_type REGEXP ?", (form.getfirst('type'),)) if form.getfirst('namespace'): addWhere("page_namespace != ?" if form.getfirst('invert') else "page_namespace = ?", (form.getfirst('namespace'),)) if form.getfirst('title'): addWhere("page_title REGEXP ?", (form.getfirst('title'),)) if form.getfirst('name'): addWhere("gc_name REGEXP ?", (form.getfirst('name'),)) wikipedia.logtime('Setup query') try: cursor.execute("""/* locateCoord LIMIT:120 NM */ SELECT gc_from, gc_lat, gc_lon, gc_head, gc_dim, gc_type, gc_size, gc_region, gc_globe, gc_primary, gc_name, CONCAT(ns_name, IF(ns_name="", '', ':'), page_title) AS title FROM """+ghel_table+""" JOIN page ON page_id=gc_from JOIN toolserver.namespacename ON ns_id=page_namespace AND ns_is_favorite=1 AND dbname=(SELECT DATABASE()) WHERE """+' AND '.join(where_conditions)+""" LIMIT ? OFFSET ?; """, tuple(where_data)+(limit+1, offset, )) except oursql.Error as (errno, strerror, extra): if errno == 1146: wikipedia.output(strerror) return else: raise wikipedia.logtime('Ran query') print '' print '' for heading in 'Lat|Lon|Dim|Type|Size|Region|Globe|Name'.split('|'): print ''%heading print '' print '' for gc_from, gc_lat, gc_lon, gc_heading, gc_dim, gc_type, gc_typesize, gc_region, gc_globe, gc_primary, gc_name, fullpagename in cursor.fetchmany(500): print '' % ( '' if gc_primary else ' class="nonprimary"' if gc_name.replace(' ', '_')==fullpagename else ' class="nonprimary nameistitle"', gc_lat, gc_lon, gc_dim or '', gc_type or '', gc_typesize or '', gc_region or '', gc_globe or '', site.hostname(), wikipedia.urllib.quote(fullpagename), gc_name.replace('_', ' ') or '[empty string]', ) print '' print '
%s
%#.7g%#.7g%s%s%s%s%s%s
' try: if cursor.fetchone(): print '

Next 500

' % '?dbname=%s&lat1=%s&lat2=%s&lon1=%s&lon2=%s&offset=%s' % (site.dbName(), lat1, lat2, lon1, lon2, offset+500) except oursql.ProgrammingError as (errno, errmsg, extra): # No more results if errno != None: raise if __name__ == "__main__" and wikipedia.handleUrlAndHeader(allowBots=True): try: wikipedia.startContent(form=False, head="""""") main() except oursql.Error as (errno, strerror, extra): if errno in (1040, 1226, 1317, 2006, 2013): print '' print '

oursql Error (%d): %s

' % (errno, wikipedia.escape(strerror)) else: raise finally: wikipedia.endContent() wikipedia.stopme()