def test_label_diff_namespace(self): """Test that it uses the old label when the new doesn't match.""" # These tests require to get the actual part which is before the title # (interwiki and namespace prefixes) which could be then compared # case insensitive. self.assertEqual( textlib.replace_links('[[Image:Foobar]]', ('File:Foobar', 'File:Foo'), self.wp_site), '[[File:Foo|Image:Foobar]]') self.assertEqual( textlib.replace_links('[[en:File:Foobar]]', ('File:Foobar', 'File:Foo'), self.wp_site), '[[File:Foo|en:File:Foobar]]')
def test_linktrails(self): """Test that the linktrails are used or applied.""" self.assertEqual( textlib.replace_links('[[Foobar]]', ('Foobar', 'Foo'), self.wp_site), '[[Foo]]bar') self.assertEqual( textlib.replace_links('[[Talk:test]]s', ('Talk:Test', 'Talk:Tests'), self.wp_site), '[[Talk:tests]]') self.assertEqual( textlib.replace_links('[[Talk:test]]s', ('Talk:Test', 'Project:Tests'), self.wp_site), '[[Project:Tests|Talk:tests]]')
def test_replace_different_case(self): """Test that it uses piped links when the case is different.""" source_text = '[[Foo|Bar]] and [[Foo|bar]]' self.assertEqual( textlib.replace_links(source_text, ('Foo', 'bar'), self.get_site('wp')), '[[Bar]] and [[bar]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', 'bar'), self.get_site('wt')), '[[bar|Bar]] and [[bar]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar'), self.get_site('wt')), '[[Bar]] and [[Bar|bar]]')
def test_unlink_some(self): """Test unlinking only some links.""" self.assertEqual( textlib.replace_links(self.text, ('World', False), self.wp_site), 'Hello World, [[how|are]] [[you#section|you]]? Are [[you]] a ' '[[bug:1337]]?') self.assertEqual( textlib.replace_links( '[[User:Namespace|Label]]\n' '[[User:Namespace#Section|Labelz]]\n' '[[Nothing]]', ('User:Namespace', False), self.wp_site), 'Label\nLabelz\n[[Nothing]]')
def test_unlink_some(self): """Test unlinking only some links.""" self.assertEqual( textlib.replace_links(self.text, ('World', False), self.wp_site), 'Hello World, [[how|are]] [[you#section|you]]? Are [[you]] a ' '[[bug:1337]]?') self.assertEqual( textlib.replace_links('[[User:Namespace|Label]]\n' '[[User:Namespace#Section|Labelz]]\n' '[[Nothing]]', ('User:Namespace', False), self.wp_site), 'Label\nLabelz\n[[Nothing]]')
def test_replace_neighbour(self): """Test that it replaces two neighbouring links.""" self.assertEqual( textlib.replace_links('[[A]][[A]][[C]]', ('A', 'B'), self.wp_site), '[[B|A]][[B|A]][[C]]')
def test_replace_file(self): """Test that it respects the namespace.""" self.assertEqual( textlib.replace_links( '[[File:Meh.png|thumb|Description of [[fancy]]]] [[Fancy]]...', ('File:Meh.png', 'File:Fancy.png'), self.wp_site), '[[File:Fancy.png|thumb|Description of [[fancy]]]] [[Fancy]]...')
def test_replacements_simplify(self): """Test a tuple as a replacement removing the need for a piped link.""" self.assertEqual( textlib.replace_links(self.text, ('how', 'are'), self.wp_site), 'Hello [[World]], [[are]] [[you#section|you]]? Are [[you]] a ' '[[bug:1337]]?')
def test_unlink_all(self): """Test unlinking.""" def callback(link, text, groups, rng): self.assertEqual(link.site, self.wp_site) return False self.assertEqual( textlib.replace_links(self.text, callback, self.wp_site), 'Hello World, are you? Are you a [[bug:1337]]?')
def test_unicode_callback(self): """Test returning unicode in the callback.""" def callback(link, text, groups, rng): self.assertEqual(link.site, self.wp_site) if link.title == 'World': # This must be a unicode instance not bytes return 'homewörlder' self.assertEqual( textlib.replace_links(self.text, callback, self.wp_site), 'Hello homewörlder, [[how|are]] [[you#section|you]]? Are [[you]] a ' '[[bug:1337]]?')
def test_replacements_function(self): """Test a dynamic function as the replacements.""" def callback(link, text, groups, rng): self.assertEqual(link.site, self.wp_site) if link.title == 'World': return pywikibot.Link('Homeworld', link.site) elif link.title.lower() == 'you': return False self.assertEqual( textlib.replace_links(self.text, callback, self.wp_site), 'Hello [[Homeworld]], [[how|are]] you? Are you a [[bug:1337]]?')
def treat_page(self): """Iterate over the linked pages and replace redirects conditionally.""" text = self.current_page.text for linked_page in self.current_page.linkedPages(): try: target = linked_page.getRedirectTarget() except (pywikibot.Error, pywikibot.SectionError): continue # TODO: Work on all links at the same time (would mean that the user # doesn't get them ordered like in links but how they appear in the page) text = textlib.replace_links(text, self._create_callback(linked_page, target), self.current_page.site) if text != self.current_page.get(): self.put_current(text)
def treat_page(self): """Iterate over the linked pages and replace redirects conditionally.""" text = self.current_page.text for linked_page in self.current_page.linkedPages(): try: target = linked_page.getRedirectTarget() except (pywikibot.Error, pywikibot.SectionError): continue # TODO: Work on all links at the same time (would mean that the user # doesn't get them ordered like in links but how they appear in the page) text = textlib.replace_links( text, self._create_callback(linked_page, target), self.current_page.site) if text != self.current_page.get(): self.put_current(text)
def test_replacements_once(self): """Test dynamic replacement.""" def callback(link, text, groups, rng): if link.title.lower() == 'you': self._count += 1 if link.section: return pywikibot.Link( '{0}#{1}'.format(self._count, link.section), link.site) else: return pywikibot.Link('{0}'.format(self._count), link.site) self._count = 0 # buffer number of found instances self.assertEqual( textlib.replace_links(self.text, callback, self.wp_site), 'Hello [[World]], [[how|are]] [[1#section]]? Are [[2]] a ' '[[bug:1337]]?') del self._count
def unlink(self, target_page): """Unlink all links linking to the target page.""" text = self.current_page.text while True: unlink_callback = self._create_callback() try: text = replace_links(text, unlink_callback, target_page.site) except EditReplacement: new_text = TextEditor().edit( unlink_callback.current_text, jumpIndex=unlink_callback.current_range[0]) # if user didn't press Cancel if new_text: text = new_text else: text = unlink_callback.current_text else: break self.put_current(text)
def test_replace_strings(self): """Test if strings can be used.""" self.assertEqual( textlib.replace_links(self.text, ('how', 'are'), self.wp_site), 'Hello [[World]], [[are]] [[you#section|you]]? Are [[you]] a ' '[[bug:1337]]?')
def test_replace_modes(self): """Test replacing with or without label and section.""" source_text = '[[Foo#bar|baz]]' self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar'), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Page(self.wp_site, 'Bar')), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Link('Bar', self.wp_site)), self.wp_site), '[[Bar]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar#snafu'), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Page(self.wp_site, 'Bar#snafu')), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Link('Bar#snafu', self.wp_site)), self.wp_site), '[[Bar#snafu]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar|foo'), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Page(self.wp_site, 'Bar|foo')), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Link('Bar|foo', self.wp_site)), self.wp_site), '[[Bar|foo]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar#snafu|foo'), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Page(self.wp_site, 'Bar#snafu|foo')), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Link('Bar#snafu|foo', self.wp_site)), self.wp_site), '[[Bar#snafu|foo]]')
def test_replace_invalid_link_text(self): """Test that it doesn't pipe a link when it's an invalid link.""" self.assertEqual( textlib.replace_links('[[Target|Foo:]]', ('Target', 'Foo'), self.wp_site), '[[Foo|Foo:]]')
def test_replace_modes(self): """Test replacing with or without label and section.""" source_text = '[[Foo#bar|baz]]' self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar'), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Page(self.wp_site, 'Bar')), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', pywikibot.Link('Bar', self.wp_site)), self.wp_site), '[[Bar]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar#snafu'), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links( source_text, ('Foo', pywikibot.Page(self.wp_site, 'Bar#snafu')), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links( source_text, ('Foo', pywikibot.Link('Bar#snafu', self.wp_site)), self.wp_site), '[[Bar#snafu]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar|foo'), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links( source_text, ('Foo', pywikibot.Page(self.wp_site, 'Bar|foo')), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links( source_text, ('Foo', pywikibot.Link('Bar|foo', self.wp_site)), self.wp_site), '[[Bar|foo]]') self.assertEqual( textlib.replace_links(source_text, ('Foo', 'Bar#snafu|foo'), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links( source_text, ('Foo', pywikibot.Page(self.wp_site, 'Bar#snafu|foo')), self.wp_site), '[[Bar#bar|baz]]') self.assertEqual( textlib.replace_links( source_text, ('Foo', pywikibot.Link('Bar#snafu|foo', self.wp_site)), self.wp_site), '[[Bar#snafu|foo]]')
for l in links_to_fix: backlinks = wikipedia_pages[l].backlinks() print 'processing backlinks for', l def replace(link, text, groups, rng): if link.title == l: if groups['label'] is not None: label = groups['label'] else: label = link.title return u'[[wikipedia:%s|%s]]' % \ (wikipedia_pages[l].wikipedia_title, label) else: return None for b in backlinks: if b.namespace() != 0: continue print 'Processing page:', b.title() if b.title() not in corrected_text: corrected_text[b.title()] = b.text corrected_text[b.title()] = replace_links(corrected_text[b.title()], replace, site=pywikibot.Site()) for page_title, text in corrected_text.items(): linking_page = pywikibot.Page(pywikibot.Link(page_title, pywikibot.Site())) linking_page.text = text print '--- corrected text for %s ---' % page_title linking_page.save(u'Fix links to Wikipedia')