def test_loc_objloc(self): loc = Template('Object location') loc.add(1, "one") set_location(self.tree, None) set_object_location(self.tree, loc) # Might be better without the spurious newline, but this will do. self.assertEqual( str(self.tree), """ == {{int:filedesc}} == {{Information |description={{en|1=Street Waste Bin Waste bin on the street outside Crosby and Blundellsands Station}} |date=2010-04-11 |source=From [http://www.geograph.org.uk/photo/1801330 geograph.org.uk] |author=[http://www.geograph.org.uk/profile/46411 Paul Glover] |permission= |other_versions= }} {{Object location|one}} == {{int:license-header}} == {{Geograph|1801330|Paul Glover}} [[Category:Streets in Sefton]] [[Category:Geograph images in Merseyside]] """)
def test_objloc(self): loc = Template('Object location') loc.add(1, "one") set_object_location(self.tree, loc) self.assertEqual( str(self.tree), "{{Information}}\n{{location dec}}\n{{Object location|one}}")
def process_page(self, page): location_added = False location_replaced = False location_removed = False location_was_mine = False object_location_added = False creditline_added = False revid = page.latest_revision_id tree = mwparserfromhell.parse(page.text) gridimage_id = get_gridimage_id(tree) c = geodb.cursor() c.execute( """ SELECT * FROM gridimage_base NATURAL JOIN gridimage_geo NATURAL JOIN gridimage_extra WHERE gridimage_id = ? """, (gridimage_id, )) row = c.fetchone() if row == None: raise NotInGeographDatabase("Geograph ID %d not in database" % (gridimage_id, )) try: location_template = get_location(tree) except IndexError: location_template = None new_location = location_from_row(row) minor = True bot.log("Existing location: %s" % (location_template, )) if (location_template != None and location_template.name == 'Location dec' and self.is_original_location(page, location_template) and self.is_geographbot_upload(page) and new_location != location_template): bot.log("Proposed location: %s" % (new_location, )) if (new_location != None and new_location.get('prec').value != '1000'): set_location(tree, new_location) azon, azno, distance = (az_dist_between_locations( location_template, new_location)) bot.log("Distance moved: %.1f m" % (distance, )) if distance > float(str(new_location.get('prec').value)): minor = False location_replaced = True else: set_location(tree, None) minor = False location_removed = True if (new_location != None and location_template == new_location and new_location.get('prec').value == '1000'): set_location(tree, None) minor = False location_removed = True location_was_mine = True if (location_template == None and new_location != None and new_location.get('prec').value != '1000'): set_location(tree, new_location) minor = False location_added = True bot.log("New camera location: %s" % (new_location, )) if not has_object_location(tree): objloc = object_location_from_row(row) if (objloc.get('prec').value == '1000' and not (location_removed or location_template == None)): bot.log("Skipping object location: precision is 1km") else: bot.log("New object location: %s" % (objloc, )) set_object_location(tree, objloc) minor = False object_location_added = True creditline = creditline_from_row(row) if (can_add_creditline(tree, creditline) and (self.unmodified_on_geograph_since_upload(page, row) or self.is_original_title(page, row['title']))): add_creditline(tree, creditline) creditline_added = True minor = False else: bot.log("Cannot add credit line") newtext = str(tree) if newtext != page.text: if location_replaced: if object_location_added: summary = ( "Replace dubious [[User:GeographBot|GeographBot]]-" "sourced camera location (moved %.1f m %s) and " "add object location, both from Geograph (%s)" % (distance, format_direction(azon), format_row(row))) else: summary = ( "Replace dubious [[User:GeographBot|GeographBot]]-" "sourced camera location (moved %.1f m %s), " "from Geograph (%s)" % (distance, format_direction(azon), format_row(row))) elif location_removed: if location_was_mine: summary = ( "Remove vague camera location (probably added by me)") else: summary = ( "Remove dubious [[User:GeographBot|GeographBot]]-" "sourced camera location") if object_location_added: summary += (" and add object location from Geograph (%s)" % (format_row(row), )) elif location_added: if object_location_added: summary = ( "Add camera and object locations from Geograph (%s)" % (format_row(row), )) else: summary = ("Add camera location from Geograph (%s)" % (format_row(row), )) elif object_location_added: summary = ("Add object location from Geograph (%s)" % (format_row(row), )) else: summary = "" if creditline_added: if summary == "": summary = "Add credit line with title from Geograph" else: summary += "; add credit line with title from Geograph" bot.log("edit summary: %s" % (summary, )) # Before we save, make sure pywikibot's view of the latest # revision hasn't changed. If it has, that invalidates # our parse tree, and we need to start again. if page.latest_revision_id != revid: bot.log("page has changed (%d != %d): restarting edit" % (page.latest_revision_id, revid)) self.process_page(page) return page.text = newtext page.save(summary, minor=minor)
def process_page(self, page): camera_action = None object_action = None sdc_camera_action = None sdc_object_action = None creditline_added = False sdc_edits = {} revid = page.latest_revision_id tree = mwparserfromhell.parse(page.text) try: gridimage_id = get_gridimage_id(tree) except ValueError as e: raise BadTemplate(str(e)) except IndexError as e: raise BadTemplate(str(e)) mapit = MapItSettings() c = geodb.cursor() c.execute(""" SELECT * FROM gridimage_base NATURAL JOIN gridimage_geo NATURAL JOIN gridimage_extra WHERE gridimage_id = ? """, (gridimage_id,)) row = c.fetchone() if row == None: raise NotInGeographDatabase("Geograph ID %d not in database" % (gridimage_id,)) try: old_location = get_location(tree) except IndexError: old_location = None try: old_object_location = get_object_location(tree) except IndexError: old_object_location = None minor = False # May need fixing bot.log("Old cam: %s" % (old_location,)) bot.log("Old obj: %s" % (old_object_location,)) if old_location == None and old_object_location == None: minor = False mapit.allowed = True # No geocoding at all: add from Geograph new_location = location_from_row(row, mapit=mapit) new_object_location = object_location_from_row(row, mapit=mapit) if new_location and new_location.get('prec').value != '1000': set_location(tree, new_location) camera_action = 'add' set_object_location(tree, new_object_location) object_action = 'add' else: oldcamparam = location_params(old_location) oldobjparam = location_params(old_object_location) if ((old_location == None or re.match(r'^geograph(-|$)', oldcamparam.get('source',''))) and (old_object_location == None or re.match(r'^geograph(-|$)', oldobjparam.get('source','')))): bot.log("Old geocoding is from Geograph") # Existing geocoding all from Geograph, so updating # from Geograph OK if needed. new_location = location_from_row(row, mapit=mapit) new_object_location = object_location_from_row(row, mapit=mapit) # Should we update locations? should_set_cam = self.should_set_location( old_location, new_location, "camera") should_set_obj = self.should_set_location( old_object_location, new_object_location, "object") if ((should_set_cam and old_location != None) or (should_set_obj and old_object_location != None)): # Check if SDC has location templates. statements = self.get_sdc_statements(page) for s in statements.get('P1259', []): if (should_set_cam and old_location != None and statement_matches_template(s, old_location)): s_new = camera_statement_from_row(row) if s_new == None: s_new = dict(id=s['id'], remove="") bot.log("Removing %s statement %s" % (s['mainsnak']['property'], s['id'])) sdc_camera_action = 'remove' else: s_new['id'] = s['id'] bot.log("Updating %s statement %s" % (s['mainsnak']['property'], s['id'])) sdc_camera_action = 'update' sdc_edits.setdefault('claims', []) sdc_edits['claims'].append(s_new) for s in statements.get('P625', []): if (should_set_obj and old_object_location != None and statement_matches_template(s, old_object_location)): s_new = object_statement_from_row(row) if s_new == None: s_new = dict(id=s['id'], remove="") bot.log("Removing %s statement %s" % (s['mainsnak']['property'], s['id'])) sdc_object_action = 'remove' else: s_new['id'] = s['id'] bot.log("Updating %s statement %s" % (s['mainsnak']['property'], s['id'])) sdc_object_action = 'update' sdc_edits.setdefault('claims', []) sdc_edits['claims'].append(s_new) # Do it if necessary: mapit.allowed = True if should_set_cam: set_location(tree, location_from_row(row, mapit=mapit)) if old_location == None: if new_location != None: camera_action = 'add' else: if new_location == None: camera_action = 'remove' else: camera_action = 'update' if should_set_obj: set_object_location(tree, object_location_from_row(row, mapit=mapit)) if old_object_location == None: if new_object_location != None: object_action = 'add' else: if new_object_location == None: object_action = 'remove' else: object_action = 'update' creditline = creditline_from_row(row) if (can_add_creditline(tree, creditline) and self.unmodified_on_geograph_since_upload(page, row)): add_creditline(tree, creditline) creditline_added = True minor = False else: bot.log("Cannot add credit line") newtext = str(tree) if newtext != page.text: format_params = dict(row=format_row(row)) if camera_action == 'update': format_params['camera_move'] = ( self.describe_move(old_location, new_location)) if object_action == 'update': format_params['object_move'] = ( self.describe_move(old_object_location, new_object_location)) summary = (self.summary_formats[(camera_action, object_action)] .format(**format_params)) if creditline_added: if summary == "": summary = "Add credit line with title from Geograph" else: summary += "; add credit line with title from Geograph" if mapit.used: # Requested credit where MapIt is used: # 'Please attribute us with the text “Powered by Mapit” # and a link back to the MapIt front page.' summary += ( " [powered by MapIt: http://global.mapit.mysociety.org]") bot.log("edit summary: %s" % (summary,)) # Before we save, make sure pywikibot's view of the latest # revision hasn't changed. If it has, that invalidates # our parse tree, and we need to start again. if page.latest_revision_id != revid: bot.log("page has changed (%d != %d): restarting edit" % (page.latest_revision_id, revid)) self.process_page(page) return page.text = newtext page.save(summary, minor=minor) if sdc_edits: sdc_summary = (self.summary_formats[(sdc_camera_action, sdc_object_action)] .format(**format_params)) bot.log("SDC edit summary: %s" % (sdc_summary,)) self.site._simple_request( action='wbeditentity', format='json', id='M%d' % (page.pageid,), data=json.dumps(sdc_edits), token=self.site.tokens['csrf'], summary=sdc_summary, bot=True, baserevid=revid).submit()