예제 #1
0
 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]]')
예제 #2
0
 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]]')
예제 #3
0
 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]]')
예제 #4
0
 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]]')
예제 #5
0
 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]]')
예제 #6
0
 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]]')
예제 #7
0
 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]]')
예제 #8
0
 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]]')
예제 #9
0
 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]]')
예제 #10
0
 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]]...')
예제 #11
0
 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]]?')
예제 #12
0
 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]]...')
예제 #13
0
 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]]?')
예제 #14
0
    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]]?')
예제 #15
0
 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]]?')
예제 #16
0
 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]]?')
예제 #17
0
    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]]?')
예제 #18
0
    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]]?')
예제 #19
0
    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)
예제 #20
0
    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)
예제 #21
0
 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
예제 #22
0
    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
예제 #23
0
    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)
예제 #24
0
    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)
예제 #25
0
 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]]')
예제 #26
0
 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]]?')
예제 #27
0
 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]]?')
예제 #28
0
 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]]')
예제 #29
0
 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:]]')
예제 #30
0
 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]]?')
예제 #31
0
 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:]]')
예제 #32
0
 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]]')
예제 #33
0
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')