Exemplo n.º 1
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()
    options = {}
    for arg in local_args:
        if genFactory.handleArg(arg):
            continue
        option, sep, value = arg.partition(':')
        option = option[1:] if option.startswith('-') else None
        if option == 'summary':
            options[option] = value
        else:
            options[option] = True

    site = pywikibot.Site()

    generator = genFactory.getCombinedGenerator(preload=True)
    if generator:
        bot = IWBot(generator=generator, site=site, **options)
        bot.run()
    else:
        suggest_help(missing_generator=True)
        return False
Exemplo n.º 2
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: str
    """
    options = {}
    gen = None

    local_args = pywikibot.handle_args(args)

    # This factory is responsible for processing command line arguments
    # that are also used by other scripts and that determine on which pages
    # to work on.
    gen_factory = pagegenerators.GeneratorFactory(positional_arg_name='page')

    for arg in local_args:
        option, sep, value = arg.partition(':')
        if option == '-xml':
            filename = value or pywikibot.input(
                "Please enter the XML dump's filename:")
            gen = TableXmlDumpPageGenerator(filename)
        elif option == '-auto':
            issue_deprecation_warning(
                'The usage of "-auto"', '-always',
                1, ArgumentDeprecationWarning, since='20170205')
            options['always'] = True
        elif option in ['-always', '-quiet', '-skipwarning']:
            options[option[1:]] = True
        else:
            if option in ('-sql', '-mysqlquery'):
                if option == '-sql':
                    issue_deprecation_warning(
                        'The usage of "-sql"', '-mysqlquery',
                        1, ArgumentDeprecationWarning, since='20170205')

                query = value or """
SELECT page_namespace, page_title
FROM page JOIN text ON (page_id = old_id)
WHERE old_text LIKE '%<table%'
"""
                arg = '-mysqlquery:' + query
            gen_factory.handleArg(arg)

    if gen:
        gen = pagegenerators.NamespaceFilterPageGenerator(
            gen, gen_factory.namespaces)
    else:
        gen = gen_factory.getCombinedGenerator()

    if gen:
        if not gen_factory.nopreload:
            gen = pagegenerators.PreloadingGenerator(gen)
        bot = Table2WikiRobot(generator=gen, **options)
        bot.run()
    else:
        suggest_help(missing_generator=True)
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()
    options = {}
    for arg in local_args:
        if genFactory.handleArg(arg):
            continue
        option, sep, value = arg.partition(':')
        option = option[1:] if option.startswith('-') else None
        if option == 'summary':
            options[option] = value
        else:
            options[option] = True

    site = pywikibot.Site()

    generator = genFactory.getCombinedGenerator()
    if generator:
        generator = pagegenerators.PreloadingGenerator(generator)
        bot = IWBot(generator=generator, site=site, **options)
        bot.run()
    else:
        suggest_help(missing_generator=True)
        return False
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: str
    """
    local_args = pywikibot.handle_args(args)
    start = None
    force = False
    unknown_parameters = []

    # Parse command line arguments
    for arg in local_args:
        if arg.startswith('-start:'):
            start = arg[7:]
        elif arg == '-force':
            force = True
        else:
            unknown_parameters.append(arg)

    if isinstance(pycountry, ImportError):
        missing_dependencies = ('pycountry', )
    else:
        missing_dependencies = None

    if missing_dependencies or unknown_parameters:
        suggest_help(unknown_parameters=unknown_parameters,
                     missing_dependencies=missing_dependencies)
        return

    bot = StatesRedirectBot(start, force)
    bot.run()
Exemplo n.º 5
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    featured = False
    gen = None

    # Process global args and prepare generator args parser
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()

    for arg in local_args:
        if arg == '-featured':
            featured = True
        elif genFactory.handleArg(arg):
            pass

    mysite = pywikibot.Site()
    if mysite.sitename == 'wikipedia:nl':
        pywikibot.output(
            color_format(
                '{lightred}There is consensus on the Dutch Wikipedia that '
                'bots should not be used to fix redirects.{default}'))
        return

    if featured:
        repo = mysite.data_repository()
        if repo:
            dp = pywikibot.ItemPage(repo, featured_articles)
            try:
                ref = pywikibot.Category(mysite, dp.getSitelink(mysite))
            except pywikibot.NoPage:
                pass
            else:
                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 False
    else:
        gen = genFactory.getCombinedGenerator()
        if gen:
            gen = mysite.preloadpages(gen)
    if gen:
        bot = FixingRedirectBot(generator=gen)
        bot.run()
        return True
    else:
        suggest_help(missing_generator=True)
        return False
Exemplo n.º 6
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    featured = False
    gen = None

    # Process global args and prepare generator args parser
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()

    for arg in local_args:
        if arg == '-featured':
            featured = True
        elif genFactory.handleArg(arg):
            pass

    mysite = pywikibot.Site()
    if mysite.sitename == 'wikipedia:nl':
        pywikibot.output(color_format(
            '{lightred}There is consensus on the Dutch Wikipedia that '
            'bots should not be used to fix redirects.{default}'))
        return

    if featured:
        repo = mysite.data_repository()
        if repo:
            dp = pywikibot.ItemPage(repo, featured_articles)
            try:
                ref = pywikibot.Category(mysite, dp.getSitelink(mysite))
            except pywikibot.NoPage:
                pass
            else:
                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 False
    else:
        gen = genFactory.getCombinedGenerator()
        if gen:
            gen = mysite.preloadpages(gen)
    if gen:
        bot = FixingRedirectBot(generator=gen)
        bot.run()
        return True
    else:
        suggest_help(missing_generator=True)
        return False
Exemplo n.º 7
0
def main(*args):
    """Extracting file page information and initiate matching."""
    images = []
    other_family = ''
    other_lang = ''
    imagePageA = None
    imagePageB = None

    # Read commandline parameters.
    local_args = pywikibot.handle_args(args)

    for arg in local_args:
        if arg.startswith('-otherfamily:'):
            if len(arg) == len('-otherfamily:'):
                other_family = pywikibot.input(
                    'What family do you want to use?')
            else:
                other_family = arg[len('-otherfamily:'):]
        elif arg.startswith('-otherlang:'):
            if len(arg) == len('-otherlang:'):
                other_lang = pywikibot.input(
                    'What language do you want to use?')
            else:
                other_lang = arg[len('otherlang:'):]
        else:
            images.append(arg)

    additional_text = ('Unable to execute script because it '
                       'requires two images to work on.'
                       if len(images) != 2 else None)
    missing_dependencies = ('Pillow', ) if isinstance(Image,
                                                      ImportError) else None

    if additional_text or missing_dependencies:
        suggest_help(missing_dependencies=missing_dependencies,
                     additional_text=additional_text)
        return False

    imagePageA = pywikibot.page.FilePage(pywikibot.Site(), images[0])
    if other_lang:
        if other_family:
            imagePageB = pywikibot.page.FilePage(
                pywikibot.Site(other_lang, other_family), images[1])
        else:
            imagePageB = pywikibot.page.FilePage(pywikibot.Site(other_lang),
                                                 images[1])
    else:
        imagePageB = pywikibot.page.FilePage(pywikibot.Site(), images[1])

    match_image_pages(imagePageA, imagePageB)
Exemplo n.º 8
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    :param args: command line arguments
    :type args: str
    """
    featured = False
    options = {}
    gen = None

    # Process global args and prepare generator args parser
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()

    for arg in local_args:
        if arg == '-featured':
            featured = True
        elif arg == '-overwrite':
            options['overwrite'] = True
        elif genFactory.handle_arg(arg):
            pass

    mysite = pywikibot.Site()
    if mysite.sitename == 'wikipedia:nl':
        pywikibot.output(color_format(
            '{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 = genFactory.getCombinedGenerator(preload=True)
    if gen:
        bot = FixingRedirectBot(generator=gen, **options)
        bot.run()
    else:
        suggest_help(missing_generator=True)
Exemplo n.º 9
0
    def __init__(self, generator, **kwargs):
        """Initializer."""
        self.available_options.update({
            'enablePage': None,    # Check if someone set an enablePage or not
            'disambigPage': None,  # If no disambigPage given, not use it.
        })
        super().__init__(**kwargs)
        self.generator = generator

        # Take the configurations according to our project
        if self.opt.enablePage:
            self.opt.enablePage = pywikibot.Page(
                self.site, self.opt.enablePage)
        self.comment = i18n.twtranslate(
            self.site, 'lonelypages-comment-add-template')
        self.commentdisambig = i18n.twtranslate(
            self.site, 'lonelypages-comment-add-disambig-template')
        orphan_template = i18n.translate(self.site, _templates)
        if orphan_template is not None:
            try:
                orphan_template = OrphanTemplate(self.site, *orphan_template)
            except ValueError as e:
                orphan_template = e
        if orphan_template is None or isinstance(orphan_template, ValueError):
            err_message = 'Missing configuration for site {}'.format(self.site)
            suggest_help(
                exception=orphan_template, additional_text=err_message)
            sys.exit(err_message)
        else:
            self._settings = orphan_template
        # DisambigPage part
        if self.opt.disambigPage is not None:
            self.disambigpage = pywikibot.Page(
                self.site, self.opt.disambigPage)
            try:
                self.disambigtext = self.disambigpage.get()
            except NoPageError:
                pywikibot.output("{} doesn't exist, skip!"
                                 .format(self.disambigpage.title()))
                self.disambigtext = ''
            except IsRedirectPageError:
                pywikibot.output("{} is a redirect, don't use it!"
                                 .format(self.disambigpage.title()))
                self.opt.disambigPage = None
Exemplo n.º 10
0
    def __init__(self, generator, **kwargs):
        """Constructor."""
        self.availableOptions.update({
            'enablePage': None,    # Check if someone set an enablePage or not
            'disambigPage': None,  # If no disambigPage given, not use it.
        })
        super(LonelyPagesBot, self).__init__(**kwargs)
        self.generator = generator

        # Take the configurations according to our project
        if self.getOption('enablePage'):
            self.options['enablePage'] = pywikibot.Page(
                self.site, self.getOption('enablePage'))
        self.comment = i18n.twtranslate(
            self.site, 'lonelypages-comment-add-template')
        self.commentdisambig = i18n.twtranslate(
            self.site, 'lonelypages-comment-add-disambig-template')
        orphan_template = i18n.translate(self.site, _templates)
        if orphan_template is not None:
            try:
                orphan_template = OrphanTemplate(self.site, *orphan_template)
            except ValueError as e:
                orphan_template = e
        if orphan_template is None or isinstance(orphan_template, ValueError):
            err_message = 'Missing configuration for site %s' % self.site
            suggest_help(exception=orphan_template, additional_text=err_message)
            sys.exit(err_message)
        else:
            self._settings = orphan_template
        # DisambigPage part
        if self.getOption('disambigPage') is not None:
            self.disambigpage = pywikibot.Page(self.site, self.getOption('disambigPage'))
            try:
                self.disambigtext = self.disambigpage.get()
            except pywikibot.NoPage:
                pywikibot.output(u"%s doesn't esist, skip!" % self.disambigpage.title())
                self.disambigtext = ''
            except pywikibot.IsRedirectPage:
                pywikibot.output(u"%s is a redirect, don't use it!"
                                 % self.disambigpage.title())
                self.options['disambigPage'] = None
Exemplo n.º 11
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    generator = None
    clean = False
    create = False
    always = False
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()
    options = {}
    for arg in local_args:
        if arg == '-clean':
            clean = True
        elif arg == '-create':
            create = True
        elif arg.startswith('-always'):
            always = True
        else:
            genFactory.handleArg(arg)

    site = pywikibot.Site()

    options = {'always': always, 'create': create, 'clean': clean}
    if not generator:
        generator = genFactory.getCombinedGenerator()
    if generator:
        generator = pagegenerators.PreloadingGenerator(generator)
        bot = IWBot(generator, site, **options)
        bot.run()
    else:
        suggest_help(missing_generator=True)
        return False
Exemplo n.º 12
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    generator = None
    clean = False
    create = False
    always = False
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()
    options = {}
    for arg in local_args:
        if arg == '-clean':
            clean = True
        elif arg == '-create':
            create = True
        elif arg.startswith('-always'):
            always = True
        else:
            genFactory.handleArg(arg)

    site = pywikibot.Site()

    options = {'always': always, 'create': create, 'clean': clean}
    if not generator:
        generator = genFactory.getCombinedGenerator()
    if generator:
        generator = pagegenerators.PreloadingGenerator(generator)
        bot = IWBot(generator, site, **options)
        bot.run()
    else:
        suggest_help(missing_generator=True)
        return False
Exemplo n.º 13
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: unicode
    """
    local_args = pywikibot.handle_args(args)
    start = None
    force = False
    unknown_parameters = []

    # Parse command line arguments
    for arg in local_args:
        if arg.startswith('-start:'):
            start = arg[7:]
        elif arg == '-force':
            force = True
        else:
            unknown_parameters.append(arg)

    if isinstance(pycountry, Exception):
        additional_text = ('This script requires the python-pycountry module\n'
                           'See: https://pypi.org/project/pycountry')
        exception = pycountry
    else:
        additional_text = exception = None

    if additional_text or exception or unknown_parameters:
        suggest_help(unknown_parameters=unknown_parameters,
                     exception=exception, additional_text=additional_text)
        return

    bot = StatesRedirectBot(start, force)
    bot.run()
Exemplo n.º 14
0
def main(*args):
    """Extracting file page information and initiate matching."""
    images = []

    # Read commandline parameters.
    local_args = pywikibot.handle_args(args)
    current_site = pywikibot.Site()
    other_family = current_site.family.name
    other_lang = current_site.code

    for arg in local_args:
        option, _, value = arg.partition(':')
        if option == '-otherfamily':
            other_family = value or pywikibot.input(
                'What family do you want to use?')
        elif option == '-otherlang':
            other_lang = value or pywikibot.input(
                'What language do you want to use?')
        else:
            images.append(arg)

    additional_text = ('Unable to execute script because it '
                       'requires two images to work on.'
                       if len(images) != 2 else None)
    missing_dependencies = ('Pillow', ) if isinstance(Image,
                                                      ImportError) else None

    if suggest_help(missing_dependencies=missing_dependencies,
                    additional_text=additional_text):
        return

    other_site = pywikibot.Site(other_lang, other_family)
    imagePageA = pywikibot.page.FilePage(current_site, images[0])
    imagePageB = pywikibot.page.FilePage(other_site, images[1])

    match_image_pages(imagePageA, imagePageB)
Exemplo n.º 15
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    url = u''
    description = []
    summary = None
    keepFilename = False
    always = False
    useFilename = None
    verifyDescription = True
    aborts = set()
    ignorewarn = set()
    chunk_size = 0
    chunk_size_regex = r'^-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?$'
    chunk_size_regex = re.compile(chunk_size_regex, re.I)
    recursive = False

    # process all global bot args
    # returns a list of non-global args, i.e. args for upload.py
    for arg in pywikibot.handle_args(args):
        if arg:
            if arg == '-always':
                keepFilename = True
                always = True
                verifyDescription = False
            elif arg == '-recursive':
                recursive = True
            elif arg.startswith('-keep'):
                keepFilename = True
            elif arg.startswith('-filename:'):
                useFilename = arg[10:]
            elif arg.startswith('-summary'):
                summary = arg[9:]
            elif arg.startswith('-noverify'):
                verifyDescription = False
            elif arg.startswith('-abortonwarn'):
                if len(arg) > len('-abortonwarn:') and aborts is not True:
                    aborts.add(arg[len('-abortonwarn:'):])
                else:
                    aborts = True
            elif arg.startswith('-ignorewarn'):
                if len(arg) > len('-ignorewarn:') and ignorewarn is not True:
                    ignorewarn.add(arg[len('-ignorewarn:'):])
                else:
                    ignorewarn = True
            elif arg.startswith('-chunked'):
                match = chunk_size_regex.match(arg)
                if match:
                    if match.group(1):  # number was in there
                        base = float(match.group(1))
                        if match.group(2):  # suffix too
                            suffix = match.group(2).lower()
                            if suffix == "k":
                                suffix = 1000
                            elif suffix == "m":
                                suffix = 1000000
                            elif suffix == "ki":
                                suffix = 1 << 10
                            elif suffix == "mi":
                                suffix = 1 << 20
                            else:
                                pass  # huh?
                        else:
                            suffix = 1
                        chunk_size = math.trunc(base * suffix)
                    else:
                        chunk_size = 1 << 20  # default to 1 MiB
                else:
                    pywikibot.error('Chunk size parameter is not valid.')
            elif url == u'':
                url = arg
            else:
                description.append(arg)
    description = u' '.join(description)
    while not ("://" in url or os.path.exists(url)):
        if not url:
            error = 'No input filename given.'
        else:
            error = 'Invalid input filename given.'
            if not always:
                error += ' Try again.'
        if always:
            url = None
            break
        else:
            pywikibot.output(error)
        url = pywikibot.input(u'URL, file or directory where files are now:')
    if always and ((aborts is not True and ignorewarn is not True)
                   or not description or url is None):
        additional = ''
        missing = []
        if url is None:
            missing += ['filename']
            additional = error + ' '
        if description is None:
            missing += ['description']
        if aborts is not True and ignorewarn is not True:
            additional += ('Either -ignorewarn or -abortonwarn must be '
                           'defined for all codes. ')
        additional += 'Unable to run in -always mode'
        suggest_help(missing_parameters=missing, additional_text=additional)
        return False
    if os.path.isdir(url):
        file_list = []
        for directory_info in os.walk(url):
            if not recursive:
                # Do not visit any subdirectories
                directory_info[1][:] = []
            for dir_file in directory_info[2]:
                file_list.append(os.path.join(directory_info[0], dir_file))
        url = file_list
    else:
        url = [url]
    bot = UploadRobot(url,
                      description=description,
                      useFilename=useFilename,
                      keepFilename=keepFilename,
                      verifyDescription=verifyDescription,
                      aborts=aborts,
                      ignoreWarning=ignorewarn,
                      chunk_size=chunk_size,
                      always=always,
                      summary=summary)
    bot.run()
Exemplo n.º 16
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: str
    """
    gen = None
    xmlFilename = None
    HTTPignore = []

    # Process global args and prepare generator args parser
    local_args = pywikibot.handle_args(args)
    genFactory = pagegenerators.GeneratorFactory()

    for arg in local_args:
        if arg == '-talk':
            config.report_dead_links_on_talk = True
        elif arg == '-notalk':
            config.report_dead_links_on_talk = False
        elif arg == '-repeat':
            gen = RepeatPageGenerator()
        elif arg.startswith('-ignore:'):
            HTTPignore.append(int(arg[8:]))
        elif arg.startswith('-day:'):
            config.weblink_dead_days = int(arg[5:])
        elif arg.startswith('-xmlstart'):
            if len(arg) == 9:
                xmlStart = pywikibot.input(
                    'Please enter the dumped article to start with:')
            else:
                xmlStart = arg[10:]
        elif arg.startswith('-xml'):
            if len(arg) == 4:
                xmlFilename = i18n.input('pywikibot-enter-xml-filename')
            else:
                xmlFilename = arg[5:]
        else:
            genFactory.handle_arg(arg)

    if xmlFilename:
        try:
            xmlStart
        except NameError:
            xmlStart = None
        gen = XmlDumpPageGenerator(xmlFilename, xmlStart,
                                   genFactory.namespaces)

    if not gen:
        gen = genFactory.getCombinedGenerator()
    if gen:
        if not genFactory.nopreload:
            # fetch at least 240 pages simultaneously from the wiki, but more
            # if a high thread number is set.
            pageNumber = max(240, config.max_external_links * 2)
            gen = pagegenerators.PreloadingGenerator(gen, groupsize=pageNumber)
        gen = pagegenerators.RedirectFilterPageGenerator(gen)
        bot = WeblinkCheckerRobot(gen, HTTPignore, config.weblink_dead_days)
        try:
            bot.run()
        except ImportError:
            suggest_help(missing_dependencies=('memento_client',))
            return
        finally:
            waitTime = 0
            # Don't wait longer than 30 seconds for threads to finish.
            while countLinkCheckThreads() > 0 and waitTime < 30:
                try:
                    pywikibot.output('Waiting for remaining {0} threads to '
                                     'finish, please wait...'
                                     .format(countLinkCheckThreads()))
                    # wait 1 second
                    time.sleep(1)
                    waitTime += 1
                except KeyboardInterrupt:
                    pywikibot.output('Interrupted.')
                    break
            if countLinkCheckThreads() > 0:
                pywikibot.output('Remaining {0} threads will be killed.'
                                 .format(countLinkCheckThreads()))
                # Threads will die automatically because they are daemonic.
            if bot.history.reportThread:
                bot.history.reportThread.shutdown()
                # wait until the report thread is shut down; the user can
                # interrupt it by pressing CTRL-C.
                try:
                    while bot.history.reportThread.is_alive():
                        time.sleep(0.1)
                except KeyboardInterrupt:
                    pywikibot.output('Report thread interrupted.')
                    bot.history.reportThread.kill()
            pywikibot.output('Saving history...')
            bot.history.save()
    else:
        suggest_help(missing_generator=True)
Exemplo n.º 17
0
def main(*args) -> None:
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: str
    """
    local_args = pywikibot.handle_args(args)

    fromsite = pywikibot.Site()
    tolang = fromsite.code
    tofamily = fromsite.family.name
    prefix = ''
    overwrite = False
    target = False
    gen_args = []

    for arg in local_args:
        if arg.startswith('-tofamily'):
            tofamily = arg[len('-tofamily:'):]
        elif arg.startswith('-tolang'):
            tolang = arg[len('-tolang:'):]
        elif arg.startswith('-prefix'):
            prefix = arg[len('-prefix:'):]
        elif arg == '-overwrite':
            overwrite = True
        elif arg == '-target':
            target = True
        else:
            gen_args.append(arg)

    tosite = pywikibot.Site(tolang, tofamily)
    additional_text = ('Target site not different from source site.'
                       if fromsite == tosite else '')

    gen_factory = pagegenerators.GeneratorFactory(
        site=tosite if target else fromsite)
    unknown_args = [arg for arg in gen_args if not gen_factory.handle_arg(arg)]

    gen = gen_factory.getCombinedGenerator()

    if suggest_help(missing_generator=not gen,
                    additional_text=additional_text,
                    unknown_parameters=unknown_args):
        return

    gen_args = ' '.join(gen_args)
    pywikibot.output("""
    Page transfer configuration
    ---------------------------
    Source: {fromsite}
    Target: {tosite}

    Generator of pages to transfer: {gen_args}
    {target}
    Prefix for transferred pages: {prefix}
    """.format(fromsite=fromsite,
               tosite=tosite,
               gen_args=gen_args,
               prefix=prefix if prefix else '(none)',
               target='from target site\n' if target else ''))

    for page in gen:
        title = page.namespace().canonical_prefix() + page.title(with_ns=False)
        if target:
            # page is at target site, fetch it from source
            target_title = prefix + page.title()
            page = pywikibot.Page(fromsite, title)
        else:
            target_title = (prefix + title)
        targetpage = pywikibot.Page(tosite, target_title)
        edithistpage = pywikibot.Page(tosite, target_title + '/edithistory')

        if targetpage.exists():
            if not overwrite:
                pywikibot.warning(
                    'Skipped {0} (target page {1} exists)'.format(
                        page.title(as_link=True, force_interwiki=True),
                        targetpage.title(as_link=True)))
                continue
            if not targetpage.botMayEdit():
                pywikibot.warning(
                    'Target page {0} is not editable by bots'.format(
                        targetpage.title(as_link=True)))
                continue

        if not page.exists():
            pywikibot.warning("Page {0} doesn't exist".format(
                page.title(as_link=True)))
            continue

        pywikibot.output('Moving {0} to {1}...'.format(
            page.title(as_link=True, force_interwiki=True),
            targetpage.title(as_link=True)))

        pywikibot.log('Getting page text.')
        text = page.get(get_redirect=True)
        source_link = page.title(as_link=True, insite=targetpage.site)

        note = twtranslate(
            tosite, 'transferbot-target', {
                'source': source_link,
                'history': edithistpage.title(as_link=True,
                                              insite=targetpage.site)
            })
        text += '<noinclude>\n\n<small>{}</small></noinclude>'.format(note)

        pywikibot.log('Getting edit history.')
        historytable = page.getVersionHistoryTable()

        pywikibot.log('Putting edit history.')
        summary = twtranslate(tosite, 'transferbot-summary',
                              {'source': source_link})
        edithistpage.put(historytable, summary=summary)

        pywikibot.log('Putting page text.')
        edithist_link = ' ([[{target}/edithistory|history]])'.format(
            target=targetpage.title(
            ) if not targetpage.namespace().subpages else '')
        summary += edithist_link
        targetpage.put(text, summary=summary)
Exemplo n.º 18
0
def main(*args):
    """Process command line arguments and invoke PatrolBot."""
    # This factory is responsible for processing command line arguments
    # that are also used by other scripts and that determine on which pages
    # to work on.
    usercontribs = None
    recentchanges = False
    newpages = False
    repeat = False
    options = {}

    # Parse command line arguments
    local_args = pywikibot.handle_args(args)
    site = pywikibot.Site()
    gen_factory = pagegenerators.GeneratorFactory(site)
    for arg in local_args:
        if arg.startswith('-ask'):
            options['ask'] = True
        elif arg.startswith('-autopatroluserns'):
            options['autopatroluserns'] = True
        elif arg.startswith('-repeat'):
            repeat = True
        elif arg.startswith('-newpages'):
            newpages = True
        elif arg.startswith('-recentchanges'):
            recentchanges = True
        elif arg.startswith('-usercontribs:'):
            usercontribs = arg[14:]
        elif arg.startswith('-versionchecktime:'):
            versionchecktime = arg[len('-versionchecktime:'):]
            options['versionchecktime'] = int(versionchecktime)
        elif arg.startswith('-whitelist:'):
            options['whitelist'] = arg[len('-whitelist:'):]
        else:
            generator = gen_factory.handle_arg(arg)
            if not generator:
                if ':' in arg:
                    m = arg.split(':')
                    options[m[0]] = m[1]

    if usercontribs:
        user = pywikibot.User(site, usercontribs)
        if user.isAnonymous() or user.isRegistered():
            pywikibot.output('Processing user: {}'.format(usercontribs))
        else:
            pywikibot.warning('User {} does not exist on site {}.'.format(
                usercontribs, site))

    # default behaviour
    if not any((newpages, recentchanges, usercontribs)):
        if site.family.name == 'wikipedia':
            newpages = True
        else:
            recentchanges = True

    bot = PatrolBot(**options)

    if isinstance(mwparserfromhell, ImportError):
        suggest_help(missing_dependencies=('mwparserfromhell', ))
        return

    if newpages or usercontribs:
        pywikibot.output('Newpages:')
        gen = site.newpages
        feed = api_feed_repeater(gen,
                                 delay=60,
                                 repeat=repeat,
                                 user=usercontribs,
                                 namespaces=gen_factory.namespaces,
                                 recent_new_gen=False)
        bot.run(feed)

    if recentchanges or usercontribs:
        pywikibot.output('Recentchanges:')
        gen = site.recentchanges
        feed = api_feed_repeater(gen,
                                 delay=60,
                                 repeat=repeat,
                                 namespaces=gen_factory.namespaces,
                                 user=usercontribs)
        bot.run(feed)

    pywikibot.output('{0}/{1} patrolled'.format(bot.patrol_counter,
                                                bot.rc_item_counter))
Exemplo n.º 19
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: str
    """
    url = ''
    description = []
    summary = None
    keep_filename = False
    always = False
    use_filename = None
    filename_prefix = None
    verify_description = True
    aborts = set()
    ignorewarn = set()
    chunk_size = 0
    recursive = False
    description_file = None

    # process all global bot args
    # returns a list of non-global args, i.e. args for upload.py
    local_args = pywikibot.handle_args(args)
    for option in local_args:
        arg, _, value = option.partition(':')
        if arg == '-always':
            keep_filename = True
            always = True
            verify_description = False
        elif arg == '-recursive':
            recursive = True
        elif arg == '-keep':
            keep_filename = True
        elif arg == '-filename':
            use_filename = value
        elif arg == '-prefix':
            filename_prefix = value
        elif arg == '-summary':
            summary = value
        elif arg == '-noverify':
            verify_description = False
        elif arg == '-abortonwarn':
            if value and aborts is not True:
                aborts.add(value)
            else:
                aborts = True
        elif arg == '-ignorewarn':
            if value and ignorewarn is not True:
                ignorewarn.add(value)
            else:
                ignorewarn = True
        elif arg == '-chunked':
            match = CHUNK_SIZE_REGEX.match(option)
            chunk_size = get_chunk_size(match)
        elif arg == '-descfile':
            description_file = value
        elif not url:
            url = option
        else:
            description.append(option)

    description = ' '.join(description)

    if description_file:
        if description:
            pywikibot.error('Both a description and a -descfile were '
                            'provided. Please specify only one of those.')
            return False
        with codecs.open(description_file,
                         encoding=pywikibot.config.textfile_encoding) as f:
            description = f.read().replace('\r\n', '\n')

    while not ('://' in url or os.path.exists(url)):
        if not url:
            error = 'No input filename given.'
        else:
            error = 'Invalid input filename given.'
            if not always:
                error += ' Try again.'
        if always:
            url = None
            break
        else:
            pywikibot.output(error)
        url = pywikibot.input('URL, file or directory where files are now:')

    if always and (aborts is not True and ignorewarn is not True
                   or not description or url is None):
        additional = ''
        missing = []
        if url is None:
            missing += ['filename']
            additional = error + ' '
        if description is None:
            missing += ['description']
        if aborts is not True and ignorewarn is not True:
            additional += ('Either -ignorewarn or -abortonwarn must be '
                           'defined for all codes. ')
        additional += 'Unable to run in -always mode'
        suggest_help(missing_parameters=missing, additional_text=additional)
        return False

    if os.path.isdir(url):
        file_list = []
        for directory_info in os.walk(url):
            if not recursive:
                # Do not visit any subdirectories
                directory_info[1][:] = []
            for dir_file in directory_info[2]:
                file_list.append(os.path.join(directory_info[0], dir_file))
        url = file_list
    else:
        url = [url]

    bot = UploadRobot(url, description=description, useFilename=use_filename,
                      keepFilename=keep_filename,
                      verifyDescription=verify_description, aborts=aborts,
                      ignoreWarning=ignorewarn, chunk_size=chunk_size,
                      always=always, summary=summary,
                      filename_prefix=filename_prefix)
    bot.run()
Exemplo n.º 20
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    url = u''
    description = []
    keepFilename = False
    always = False
    useFilename = None
    verifyDescription = True
    aborts = set()
    ignorewarn = set()
    chunk_size = 0
    chunk_size_regex = r'^-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?$'
    chunk_size_regex = re.compile(chunk_size_regex, re.I)
    recursive = False

    # process all global bot args
    # returns a list of non-global args, i.e. args for upload.py
    for arg in pywikibot.handle_args(args):
        if arg:
            if arg == '-always':
                keepFilename = True
                always = True
                verifyDescription = False
            elif arg == '-recursive':
                recursive = True
            elif arg.startswith('-keep'):
                keepFilename = True
            elif arg.startswith('-filename:'):
                useFilename = arg[10:]
            elif arg.startswith('-noverify'):
                verifyDescription = False
            elif arg.startswith('-abortonwarn'):
                if len(arg) > len('-abortonwarn:') and aborts is not True:
                    aborts.add(arg[len('-abortonwarn:'):])
                else:
                    aborts = True
            elif arg.startswith('-ignorewarn'):
                if len(arg) > len('-ignorewarn:') and ignorewarn is not True:
                    ignorewarn.add(arg[len('-ignorewarn:'):])
                else:
                    ignorewarn = True
            elif arg.startswith('-chunked'):
                match = chunk_size_regex.match(arg)
                if match:
                    if match.group(1):  # number was in there
                        base = float(match.group(1))
                        if match.group(2):  # suffix too
                            suffix = match.group(2).lower()
                            if suffix == "k":
                                suffix = 1000
                            elif suffix == "m":
                                suffix = 1000000
                            elif suffix == "ki":
                                suffix = 1 << 10
                            elif suffix == "mi":
                                suffix = 1 << 20
                            else:
                                pass  # huh?
                        else:
                            suffix = 1
                        chunk_size = math.trunc(base * suffix)
                    else:
                        chunk_size = 1 << 20  # default to 1 MiB
                else:
                    pywikibot.error('Chunk size parameter is not valid.')
            elif url == u'':
                url = arg
            else:
                description.append(arg)
    description = u' '.join(description)
    while not ("://" in url or os.path.exists(url)):
        if not url:
            error = 'No input filename given.'
        else:
            error = 'Invalid input filename given.'
            if not always:
                error += ' Try again.'
        if always:
            url = None
            break
        else:
            pywikibot.output(error)
        url = pywikibot.input(u'URL, file or directory where files are now:')
    if always and ((aborts is not True and ignorewarn is not True) or
                   not description or url is None):
        additional = ''
        missing = []
        if url is None:
            missing += ['filename']
            additional = error + ' '
        if description is None:
            missing += ['description']
        if aborts is not True and ignorewarn is not True:
            additional += ('Either -ignorewarn or -abortonwarn must be '
                           'defined for all codes. ')
        additional += 'Unable to run in -always mode'
        suggest_help(missing_parameters=missing, additional_text=additional)
        return False
    if os.path.isdir(url):
        file_list = []
        for directory_info in os.walk(url):
            if not recursive:
                # Do not visit any subdirectories
                directory_info[1][:] = []
            for dir_file in directory_info[2]:
                file_list.append(os.path.join(directory_info[0], dir_file))
        url = file_list
    else:
        url = [url]
    bot = UploadRobot(url, description=description, useFilename=useFilename,
                      keepFilename=keepFilename,
                      verifyDescription=verifyDescription,
                      aborts=aborts, ignoreWarning=ignorewarn,
                      chunk_size=chunk_size, always=always)
    bot.run()
Exemplo n.º 21
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    options = {}
    gen = None

    local_args = pywikibot.handle_args(args)

    # This factory is responsible for processing command line arguments
    # that are also used by other scripts and that determine on which pages
    # to work on.
    genFactory = pagegenerators.GeneratorFactory(positional_arg_name='page')

    for arg in local_args:
        option, sep, value = arg.partition(':')
        if option == '-xml':
            filename = value or pywikibot.input(
                "Please enter the XML dump's filename:")
            gen = TableXmlDumpPageGenerator(filename)
        elif option == '-auto':
            issue_deprecation_warning(
                'The usage of "-auto"', '-always',
                1, ArgumentDeprecationWarning)
            options['always'] = True
        elif option in ['-always', '-quiet', '-skipwarning']:
            options[option[1:]] = True
        else:
            if option in ['-sql', '-mysqlquery']:
                if not (has_module('oursql') or has_module('MySQLdb')):
                    raise NotImplementedError(
                        'Neither "oursql" nor "MySQLdb" library is installed.')
                if option == '-sql':
                    issue_deprecation_warning(
                        'The usage of "-sql"', '-mysqlquery',
                        1, ArgumentDeprecationWarning)

                query = value or """
SELECT page_namespace, page_title
FROM page JOIN text ON (page_id = old_id)
WHERE old_text LIKE '%<table%'
"""
                arg = '-mysqlquery:' + query
            genFactory.handleArg(arg)

    if gen:
        gen = pagegenerators.NamespaceFilterPageGenerator(
            gen, genFactory.namespaces)
    else:
        gen = genFactory.getCombinedGenerator()

    if gen:
        if not genFactory.nopreload:
            gen = pagegenerators.PreloadingGenerator(gen)
        bot = Table2WikiRobot(generator=gen, **options)
        bot.run()
        return True
    else:
        suggest_help(missing_generator=True)
        return False
Exemplo n.º 22
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    url = u''
    description = []
    summary = None
    keepFilename = False
    always = False
    useFilename = None
    verifyDescription = True
    aborts = set()
    ignorewarn = set()
    chunk_size = 0
    chunk_size_regex = r'^-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?$'
    chunk_size_regex = re.compile(chunk_size_regex, re.I)
    recursive = False

    # process all global bot args
    # returns a list of non-global args, i.e. args for upload.py
    for arg in pywikibot.handle_args(args):
        if arg:
            if arg == '-always':
                keepFilename = True
                always = True
                verifyDescription = False
            elif arg == '-recursive':
                recursive = True
            elif arg.startswith('-keep'):
                keepFilename = True
            elif arg.startswith('-filename:'):
                useFilename = arg[10:]
            elif arg.startswith('-summary'):
                summary = arg[9:]
            elif arg.startswith('-noverify'):
                verifyDescription = False
            elif arg.startswith('-abortonwarn'):
                if len(arg) > len('-abortonwarn:') and aborts is not True:
                    aborts.add(arg[len('-abortonwarn:'):])
                else:
                    aborts = True
            elif arg.startswith('-ignorewarn'):
                if len(arg) > len('-ignorewarn:') and ignorewarn is not True:
                    ignorewarn.add(arg[len('-ignorewarn:'):])
                else:
                    ignorewarn = True
            elif arg.startswith('-chunked'):
                match = chunk_size_regex.match(arg)
                if match:
                    if match.group(1):  # number was in there
                        base = float(match.group(1))
                        if match.group(2):  # suffix too
                            suffix = match.group(2).lower()
                            if suffix == "k":
                                suffix = 1000
                            elif suffix == "m":
                                suffix = 1000000
                            elif suffix == "ki":
                                suffix = 1 << 10
                            elif suffix == "mi":
                                suffix = 1 << 20
                            else:
                                pass  # huh?
                        else:
                            suffix = 1
                        chunk_size = math.trunc(base * suffix)
                    else:
                        chunk_size = 1 << 20  # default to 1 MiB
                else:
                    pywikibot.error('Chunk size parameter is not valid.')
            elif url == u'':
                url = arg
            else:
                description.append(arg)
    description = u' '.join(description)

    # curly barckets need to double in formatted string
    description = """=={{{{int:filedesc}}}}==
{{{{Information
|description={{{{en|1=Native Israeli pronunciation of this Hebrew word}}}}
|date={0}
|source={{{{own}}}}
|author=[[User:{1}|{1}]]
|permission=
|other versions=
}}}}

=={{{{int:license-header}}}}==

{{{{self|cc-zero}}}}

[[Category:Hebrew pronunciation]]""".format(date.today(),config.usernames['commons']['commons'])


    while not ("://" in url or os.path.exists(url)):
        if not url:
            error = 'No input filename given.'
        else:
            error = 'Invalid input filename given.'
            if not always:
                error += ' Try again.'
        if always:
            url = None
            break
        else:
            pywikibot.output(error)
        url = pywikibot.input(u'URL, file or directory where files are now:')
    if always and ((aborts is not True and ignorewarn is not True) or
                   not description or url is None):
        additional = ''
        missing = []
        if url is None:
            missing += ['filename']
            additional = error + ' '
        if description is None:
            missing += ['description']
        if aborts is not True and ignorewarn is not True:
            additional += ('Either -ignorewarn or -abortonwarn must be '
                           'defined for all codes. ')
        additional += 'Unable to run in -always mode'
        suggest_help(missing_parameters=missing, additional_text=additional)
        return False
    if os.path.isdir(url):
        file_list = []
        for directory_info in os.walk(url):
            if not recursive:
                # Do not visit any subdirectories
                directory_info[1][:] = []
            for dir_file in directory_info[2]:
                file_list.append(os.path.join(directory_info[0], dir_file))
        url = file_list
    else:
        url = [url]

    bot = UploadRobot(url, description=description, useFilename=useFilename,
                      keepFilename=keepFilename,
                      verifyDescription=verifyDescription,
                      aborts=aborts, ignoreWarning=ignorewarn,
                      chunk_size=chunk_size, always=always,
                      summary="bot upload",
                      targetSite=pywikibot.Site('commons', 'commons'))
    bot.run()
Exemplo n.º 23
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: str
    """
    local_args = pywikibot.handle_args(args)

    fromsite = pywikibot.Site()
    tolang = fromsite.code
    tofamily = fromsite.family.name
    prefix = ''
    overwrite = False
    target = False
    gen_args = []

    for arg in local_args:
        if arg.startswith('-tofamily'):
            tofamily = arg[len('-tofamily:'):]
        elif arg.startswith('-tolang'):
            tolang = arg[len('-tolang:'):]
        elif arg.startswith('-prefix'):
            prefix = arg[len('-prefix:'):]
        elif arg == '-overwrite':
            overwrite = True
        elif arg == '-target':
            target = True
        else:
            gen_args.append(arg)

    tosite = pywikibot.Site(tolang, tofamily)
    additional_text = ('Target site not different from source site.'
                       if fromsite == tosite else '')

    gen_factory = pagegenerators.GeneratorFactory(
        site=tosite if target else fromsite)
    unknown_args = [arg for arg in gen_args if not gen_factory.handleArg(arg)]

    gen = gen_factory.getCombinedGenerator()

    if suggest_help(missing_generator=not gen,
                    additional_text=additional_text,
                    unknown_parameters=unknown_args):
        return

    gen_args = ' '.join(gen_args)
    pywikibot.output("""
    Page transfer configuration
    ---------------------------
    Source: {fromsite}
    Target: {tosite}

    Generator of pages to transfer: {gen_args}
    {target}
    Prefix for transferred pages: {prefix}
    """.format(fromsite=fromsite,
               tosite=tosite,
               gen_args=gen_args,
               prefix=prefix if prefix else '(none)',
               target='from target site\n' if target else ''))

    zh_items = open("zh_items.txt")
    cleaner = WikiTextCleaner()
    cleaner.build_inner_pattern()

    for page in gen:
        title = page.namespace().canonical_prefix() + page.title(with_ns=False)
        zh_title = zh_items.readline().strip()
        if target:
            # page is at target site, fetch it from source
            # target_title = prefix + page.title()
            # page = pywikibot.Page(fromsite, title)
            page = pywikibot.Page(fromsite, zh_title)
        else:
            # target_title = (prefix + title)
            target_title = zh_title
        targetpage = pywikibot.Page(tosite, target_title)
        edithistpage = pywikibot.Page(tosite, target_title + '/edithistory')

        if targetpage.exists():
            if not overwrite:
                pywikibot.warning(
                    'Skipped {0} (target page {1} exists)'.format(
                        page.title(as_link=True, force_interwiki=True),
                        targetpage.title(as_link=True)))
                continue
            if not targetpage.botMayEdit():
                pywikibot.warning(
                    'Target page {0} is not editable by bots'.format(
                        targetpage.title(as_link=True)))
                continue

        if not page.exists():
            pywikibot.warning("Page {0} doesn't exist".format(
                page.title(as_link=True)))
            continue

        pywikibot.output('Moving {0} to {1}...'.format(
            page.title(as_link=True, force_interwiki=True),
            targetpage.title(as_link=True)))

        pywikibot.log('Getting page text.')
        text = page.get(get_redirect=True)

        text_lines = text.split("\n")
        clean_text = cleaner(text_lines)
        trans_text = translate_passage(clean_text, "en2zh")

        translated_text = "此词条暂由彩云小译翻译,未经人工整理和审校,带来阅读不便,请见谅。\n\n"

        for s, c, t in zip(text_lines, clean_text, trans_text):
            translated_text += s + "\n\n"
            if c.strip():
                translated_text += c + "\n\n"
                translated_text += t + "\n\n"

        source_link = page.title(as_link=True, insite=targetpage.site)
        text = translated_text
        text += ('<noinclude>\n\n<small>This page was moved from {0}. Its '
                 'edit history can be viewed at {1}</small></noinclude>\n\n'
                 '[[Category:待整理页面]]'.format(
                     source_link,
                     edithistpage.title(as_link=True, insite=targetpage.site)))

        pywikibot.log('Getting edit history.')
        historytable = page.getVersionHistoryTable()

        pywikibot.log('Putting edit history.')
        summary = 'Moved page from {source}'.format(source=source_link)
        # edithistpage.put(historytable, summary=summary)

        pywikibot.log('Putting page text.')
        edithist_link = ' ([[{target}/edithistory|history]])'.format(
            target=targetpage.title(
            ) if not targetpage.namespace().subpages else '')
        summary += edithist_link
        targetpage.put(text, summary=summary)

    zh_items.close()
Exemplo n.º 24
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: list of unicode
    """
    url = u''
    description = []
    summary = None
    keepFilename = False
    always = False
    useFilename = None
    filename_prefix = None
    verifyDescription = True
    aborts = set()
    ignorewarn = set()
    chunk_size = 0
    chunk_size_regex = re.compile(
        r'^-chunked(?::(\d+(?:\.\d+)?)[ \t]*(k|ki|m|mi)?b?)?$', re.I)
    recursive = False

    # process all global bot args
    # returns a list of non-global args, i.e. args for upload.py
    local_args = pywikibot.handle_args(args)
    for option in local_args:
        arg, _, value = option.partition(':')
        if arg == '-always':
            keepFilename = True
            always = True
            verifyDescription = False
        elif arg == '-recursive':
            recursive = True
        elif arg == '-keep':
            keepFilename = True
        elif arg == '-filename':
            useFilename = value
        elif arg == '-prefix':
            filename_prefix = value
        elif arg == '-summary':
            summary = value
        elif arg == '-noverify':
            verifyDescription = False
        elif arg == '-abortonwarn':
            if value and aborts is not True:
                aborts.add(value)
            else:
                aborts = True
        elif arg == '-ignorewarn':
            if value and ignorewarn is not True:
                ignorewarn.add(value)
            else:
                ignorewarn = True
        elif arg == '-chunked':
            match = chunk_size_regex.match(option)
            chunk_size = get_chunk_size(match)
        elif arg and not value:
            if not url:
                url = arg
            else:
                description.append(arg)

    description = u' '.join(description)
    while not ("://" in url or os.path.exists(url)):
        if not url:
            error = 'No input filename given.'
        else:
            error = 'Invalid input filename given.'
            if not always:
                error += ' Try again.'
        if always:
            url = None
            break
        else:
            pywikibot.output(error)
        url = pywikibot.input(u'URL, file or directory where files are now:')

    if always and ((aborts is not True and ignorewarn is not True) or
                   not description or url is None):
        additional = ''
        missing = []
        if url is None:
            missing += ['filename']
            additional = error + ' '
        if description is None:
            missing += ['description']
        if aborts is not True and ignorewarn is not True:
            additional += ('Either -ignorewarn or -abortonwarn must be '
                           'defined for all codes. ')
        additional += 'Unable to run in -always mode'
        suggest_help(missing_parameters=missing, additional_text=additional)
        return False

    if os.path.isdir(url):
        file_list = []
        for directory_info in os.walk(url):
            if not recursive:
                # Do not visit any subdirectories
                directory_info[1][:] = []
            for dir_file in directory_info[2]:
                file_list.append(os.path.join(directory_info[0], dir_file))
        url = file_list
    else:
        url = [url]

    bot = UploadRobot(url, description=description, useFilename=useFilename,
                      keepFilename=keepFilename,
                      verifyDescription=verifyDescription, aborts=aborts,
                      ignoreWarning=ignorewarn, chunk_size=chunk_size,
                      always=always, summary=summary,
                      filename_prefix=filename_prefix)
    bot.run()
Exemplo n.º 25
0
def main(*args):
    """
    Process command line arguments and invoke bot.

    If args is an empty list, sys.argv is used.

    @param args: command line arguments
    @type args: str
    """
    local_args = pywikibot.handle_args(args)

    fromsite = pywikibot.Site()
    tolang = fromsite.code
    tofamily = fromsite.family.name
    prefix = ''
    overwrite = False
    gen_args = []

    gen_factory = pagegenerators.GeneratorFactory()

    for arg in local_args:
        if gen_factory.handleArg(arg):
            gen_args.append(arg)
            continue
        if arg.startswith('-tofamily'):
            tofamily = arg[len('-tofamily:'):]
        elif arg.startswith('-tolang'):
            tolang = arg[len('-tolang:'):]
        elif arg.startswith('-prefix'):
            prefix = arg[len('-prefix:'):]
        elif arg == '-overwrite':
            overwrite = True

    gen = gen_factory.getCombinedGenerator()

    tosite = pywikibot.Site(tolang, tofamily)
    additional_text = ('Target site not different from source site.'
                       if fromsite == tosite else '')

    if additional_text or not gen:
        suggest_help(missing_generator=not gen,
                     additional_text=additional_text)
        return

    gen_args = ' '.join(gen_args)
    pywikibot.output(
        """
    Page transfer configuration
    ---------------------------
    Source: %(fromsite)r
    Target: %(tosite)r

    Generator of pages to transfer: %(gen_args)s

    Prefix for transferred pages: %(prefix)s
    """ % {
            'fromsite': fromsite,
            'tosite': tosite,
            'gen_args': gen_args,
            'prefix': prefix if prefix else '(none)'
        })

    for page in gen:
        target_title = (prefix + page.namespace().canonical_prefix() +
                        page.title(with_ns=False))
        targetpage = pywikibot.Page(tosite, target_title)
        edithistpage = pywikibot.Page(tosite, target_title + '/edithistory')

        if targetpage.exists():
            if not overwrite:
                pywikibot.warning(
                    'Skipped {0} (target page {1} exists)'.format(
                        page.title(as_link=True, force_interwiki=True),
                        targetpage.title(as_link=True)))
                continue
            if not targetpage.botMayEdit():
                pywikibot.warning(
                    'Target page {0} is not editable by bots'.format(
                        targetpage.title(as_link=True)))
                continue

        if not page.exists():
            pywikibot.warning("Page {0} doesn't exist".format(
                page.title(as_link=True)))
            continue

        pywikibot.output('Moving {0} to {1}...'.format(
            page.title(as_link=True, force_interwiki=True),
            targetpage.title(as_link=True)))

        pywikibot.log('Getting page text.')
        text = page.get(get_redirect=True)
        source_link = page.title(as_link=True, insite=targetpage.site)
        text += (
            '<noinclude>\n\n<small>This page was moved from {0}. Its '
            'edit history can be viewed at {1}</small></noinclude>'.format(
                source_link,
                edithistpage.title(as_link=True, insite=targetpage.site)))

        pywikibot.log('Getting edit history.')
        historytable = page.getVersionHistoryTable()

        pywikibot.log('Putting edit history.')
        summary = 'Moved page from {source}'.format(source=source_link)
        edithistpage.put(historytable, summary=summary)

        pywikibot.log('Putting page text.')
        edithist_link = ' ([[{target}/edithistory|history]])'.format(
            target=targetpage.title(
            ) if not targetpage.namespace().subpages else '')
        summary += edithist_link
        targetpage.put(text, summary=summary)