def test_nomatch_globe(self): (self.sdc['statements']['P1259'][0]['mainsnak']['datavalue']['value'] ['globe']) = ("http://www.wikidata.org/entity/Q19907") self.assertFalse( statement_matches_template(self.sdc['statements']['P1259'][0], get_location(self.templates)))
def test_nomatch_hdg(self): (self.sdc['statements']['P1259'][0]['qualifiers']['P7787'][0] ['datavalue']['value']['amount']) = '+1' self.assertFalse( statement_matches_template(self.sdc['statements']['P1259'][0], get_location(self.templates)))
def test_nomatch_lat(self): (self.sdc['statements']['P1259'][0]['mainsnak']['datavalue']['value'] ['latitude']) += 1 self.assertFalse( statement_matches_template(self.sdc['statements']['P1259'][0], get_location(self.templates)))
def test_match_object(self): self.assertTrue( statement_matches_template(self.sdc['statements']['P625'][0], get_object_location(self.templates)))
def test_match_camera(self): self.assertTrue( statement_matches_template(self.sdc['statements']['P1259'][0], get_location(self.templates)))
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()