Source code for scripts.fixing_redirects

#!/usr/bin/python3
"""
Correct all redirect links in featured pages or only one page of each wiki.

Can be used with:

-always           The bot won't ask for confirmation when putting a page

-featured         Run over featured pages (for some Wikimedia wikis only)

-overwrite        Usually only the link is changed ([[Foo]] -> [[Bar|Foo]]).
                  This parameters sets the script to completly overwrite the
                  link text ([[Foo]] -> [[Bar]]).

-ignoremoves      Do not try to solve deleted pages after page move.

&params;
"""
#
# (C) Pywikibot team, 2004-2022
#
# Distributed under the terms of the MIT license.
#
import re
from concurrent.futures import ThreadPoolExecutor, as_completed
from contextlib import suppress

import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import (
    AutomaticTWSummaryBot,
    ExistingPageBot,
    SingleSiteBot,
    suggest_help,
)
from pywikibot.exceptions import (
    CircularRedirectError,
    InterwikiRedirectPageError,
    InvalidPageError,
    InvalidTitleError,
    NoMoveTargetError,
)
from pywikibot.textlib import does_text_contain_section, isDisabled
from pywikibot.tools import first_lower
from pywikibot.tools import first_upper as firstcap


# This is required for the text that is shown when you run this script
# with the parameter -help.
docuReplacements = {'&params;': pagegenerators.parameterHelp}  # noqa: N816

# Featured articles categories
FEATURED_ARTICLES = 'Q4387444'


[docs]class FixingRedirectBot(SingleSiteBot, ExistingPageBot, AutomaticTWSummaryBot): """Run over pages and resolve redirect links.""" use_redirects = False ignore_save_related_errors = True ignore_server_errors = True summary_key = 'fixing_redirects-fixing' update_options = { 'overwrite': False, 'ignoremoves': False, }
[docs] def get_target(self, page): """Get the target page for a given page.""" target = None if not page.exists(): if not self.opt.ignoremoves: with suppress(NoMoveTargetError, CircularRedirectError, InvalidTitleError): target = page.moved_target() elif page.isRedirectPage(): try: target = page.getRedirectTarget() except (CircularRedirectError, InvalidTitleError, InterwikiRedirectPageError): pass except RuntimeError as e: pywikibot.error(e) else: section = target.section() if section and not does_text_contain_section(target.text, section): pywikibot.warning( 'Section #{} not found on page {}' .format(section, target.title(as_link=True, with_section=False))) target = None if target is not None \ and target.namespace() in [2, 3] and page.namespace() not in [2, 3]: target = None return page, target
[docs] def treat_page(self) -> None: """Change all redirects from the current page to actual links.""" try: newtext = self.current_page.text except InvalidPageError as e: pywikibot.error(e) return with ThreadPoolExecutor() as executor: futures = {executor.submit(self.get_target, p) for p in self.current_page.linkedPages()} for future in as_completed(futures): page, target = future.result() if target: newtext = self.replace_links(newtext, page, target) self.put_current(newtext)
[docs]def main(*args: str) -> None: """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. :param args: command line arguments """ featured = False options = {} gen = None # Process global args and prepare generator args parser gen_factory = pagegenerators.GeneratorFactory() local_args = pywikibot.handle_args(args) local_args = gen_factory.handle_args(local_args) unknown = [] for arg in local_args: if arg == '-featured': featured = True elif arg in ('-always', '-ignoremoves', '-overwrite'): options[arg[1:]] = True else: unknown.append(arg) suggest_help(unknown_parameters=unknown) mysite = pywikibot.Site() if mysite.sitename == 'wikipedia:nl': pywikibot.output( '<<lightred>>There is consensus on the Dutch Wikipedia that ' 'bots should not be used to fix redirects.<<default>>') return if featured: ref = mysite.page_from_repository(FEATURED_ARTICLES) if ref is not None: gen = ref.articles(namespaces=0, content=True) if not gen: suggest_help( unknown_parameters=['-featured'], additional_text='Option is not available for this site.') return else: gen = gen_factory.getCombinedGenerator(preload=True) if gen: bot = FixingRedirectBot(generator=gen, **options) bot.run() else: suggest_help(missing_generator=True)
if __name__ == '__main__': main()