Esempio n. 1
0
 def testLocalized(self):
     self.assertEqual(i18n.translate('en', self.msg_localized),
                      u'test-localized EN')
     self.assertEqual(i18n.translate('nl', self.msg_localized),
                      u'test-localized NL')
     self.assertEqual(i18n.translate('fy', self.msg_localized),
                      u'test-localized FY')
Esempio n. 2
0
 def run(self):
     template_image = i18n.translate(self.site,
                                     template_to_the_image)
     template_user = i18n.translate(self.site,
                                    template_to_the_user)
     except_text_translated = i18n.translate(self.site, except_text)
     summary = i18n.translate(self.site, comment, fallback=True)
     if not all([template_image, template_user, except_text_translated, comment]):
         raise pywikibot.Error(u'This script is not localized for %s site.'
                               % self.site)
     self.summary = summary
     generator = pagegenerators.UnusedFilesGenerator(site=self.site)
     generator = pagegenerators.PreloadingGenerator(generator)
     for image in generator:
         if not image.exists():
             pywikibot.output(u"File '%s' does not exist (see bug 69133)."
                              % image.title())
             continue
         if (except_text_translated.encode('utf-8')
                 not in image.getImagePageHtml() and
                 u'http://' not in image.text):
             if template_image in image.text:
                 pywikibot.output(u"%s done already"
                                  % image.title(asLink=True))
                 continue
             self.append_text(image, u"\n\n" + template_image)
             uploader = image.getFileVersionHistory().pop(0)['user']
             user = pywikibot.User(image.site, uploader)
             usertalkpage = user.getUserTalkPage()
             msg2uploader = template_user % {'title': image.title()}
             self.append_text(usertalkpage, msg2uploader)
Esempio n. 3
0
def main():
    always = False

    for arg in pywikibot.handleArgs():
        if arg == '-always':
            always = True

    mysite = pywikibot.Site()
    # If anything needs to be prepared, you can do it here
    template_image = i18n.translate(pywikibot.Site(),
                                    template_to_the_image)
    template_user = i18n.translate(pywikibot.Site(),
                                   template_to_the_user)
    except_text_translated = i18n.translate(pywikibot.Site(), except_text)
    if not(template_image and template_user and except_text_translated):
        pywikibot.warning(u'This script is not localized for %s site.' % mysite)
        return
    generator = pagegenerators.UnusedFilesGenerator()
    generator = pagegenerators.PreloadingGenerator(generator)
    for image in generator:
        if (except_text_translated.encode('utf-8')
                not in image.getImagePageHtml() and
                u'http://' not in image.text):
            pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                             % image.title())
            if template_image in image.text:
                pywikibot.output(u"%s done already"
                                 % image.title(asLink=True))
                continue
            appendtext(image, u"\n\n" + template_image, always)
            uploader = image.getFileVersionHistory().pop(0)['user']
            user = pywikibot.User(mysite, uploader)
            usertalkpage = user.getUserTalkPage()
            msg2uploader = template_user % {'title': image.title()}
            appendtext(usertalkpage, msg2uploader, always)
Esempio n. 4
0
    def __init__(self, generator, oldImage, newImage=None, summary='',
                 always=False, loose=False):
        """
        Arguments:
            * generator - A page generator.
            * oldImage  - The title of the old image (without namespace)
            * newImage  - The title of the new image (without namespace), or
                          None if you want to remove the image.
        """
        self.generator = generator
        self.oldImage = oldImage
        self.newImage = newImage
        self.editSummary = summary
        self.summary = summary
        self.always = always
        self.loose = loose

        # get edit summary message
        mysite = pywikibot.Site()
        if summary:
            self.editSummary = summary
        elif self.newImage:
            self.editSummary = i18n.translate(mysite, self.msg_replace,
                                              fallback=True) \
            % (self.oldImage, self.newImage)
        else:
            self.editSummary = i18n.translate(mysite, self.msg_remove,
                                              fallback=True) \
            % self.oldImage
Esempio n. 5
0
    def __init__(self, generator, old_image, new_image=None, **kwargs):
        """
        Constructor.

        @param generator: the pages to work on
        @type  generator: iterable
        @param old_image: the title of the old image (without namespace)
        @type  old_image: unicode
        @param new_image: the title of the new image (without namespace), or
                          None if you want to remove the image
        @type  new_image: unicode or None
        """
        self.availableOptions.update({
            'summary': None,
            'loose': False,
        })
        super(ImageRobot, self).__init__(**kwargs)

        self.generator = generator
        self.site = pywikibot.Site()
        self.old_image = old_image
        self.new_image = new_image

        if not self.getOption('summary'):
            if self.new_image:
                self.options['summary'] = i18n.translate(self.site, self.msg_replace,
                                                         fallback=True) \
                % (self.old_image, self.new_image)
            else:
                self.options['summary'] = i18n.translate(self.site, self.msg_remove,
                                                         fallback=True) \
                % self.old_image
Esempio n. 6
0
    def __init__(self, generator, **kwargs):
        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
        self.site = pywikibot.Site()
        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')
        self.template = i18n.translate(self.site, template)
        self.exception = i18n.translate(self.site, exception_regex)
        if self.template is None or self.exception is None:
            pywikibot.showHelp()
            sys.exit(u'Missing configuration for site %s' % self.site)
        # 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
Esempio n. 7
0
 def run(self):
     """Start the bot."""
     template_image = i18n.translate(self.site,
                                     template_to_the_image)
     template_user = i18n.translate(self.site,
                                    template_to_the_user)
     summary = i18n.translate(self.site, comment, fallback=True)
     if not all([template_image, template_user, comment]):
         raise pywikibot.Error(u'This script is not localized for %s site.'
                               % self.site)
     self.summary = summary
     generator = pagegenerators.UnusedFilesGenerator(site=self.site)
     generator = pagegenerators.PreloadingGenerator(generator)
     for image in generator:
         if not image.exists():
             pywikibot.output(u"File '%s' does not exist (see bug 69133)."
                              % image.title())
             continue
         # Use fileUrl() and fileIsShared() to confirm it is local media
         # rather than a local page with the same name as shared media.
         if (image.fileUrl() and not image.fileIsShared() and
                 u'http://' not in image.text):
             if template_image in image.text:
                 pywikibot.output(u"%s done already"
                                  % image.title(asLink=True))
                 continue
             self.append_text(image, u"\n\n" + template_image)
             uploader = image.getFileVersionHistory().pop(0)['user']
             user = pywikibot.User(image.site, uploader)
             usertalkpage = user.getUserTalkPage()
             msg2uploader = template_user % {'title': image.title()}
             self.append_text(usertalkpage, msg2uploader)
Esempio n. 8
0
 def testNoEnglish(self):
     self.assertEqual(i18n.translate('en', self.msg_no_english),
                      u'test-no-english JA')
     self.assertEqual(i18n.translate('fy', self.msg_no_english),
                      u'test-no-english JA')
     self.assertEqual(i18n.translate('nl', self.msg_no_english),
                      u'test-no-english JA')
Esempio n. 9
0
 def testSemiLocalized(self):
     self.assertEqual(i18n.translate('en', self.msg_semi_localized),
                      u'test-semi-localized EN')
     self.assertEqual(i18n.translate('nl', self.msg_semi_localized),
                      u'test-semi-localized NL')
     self.assertEqual(i18n.translate('fy', self.msg_semi_localized),
                      u'test-semi-localized NL')
Esempio n. 10
0
    def __init__(self, generator, **kwargs):
        """- generator : Page generator."""
        self.availableOptions.update({
            'ignorepdf': False,  # boolean
            'limit': None,  # int, stop after n modified pages
            'summary': None,
        })

        super(ReferencesRobot, self).__init__(**kwargs)
        self.generator = generator
        self.site = pywikibot.Site()
        self._use_fake_user_agent = config.fake_user_agent_default.get('reflinks', False)
        # Check
        manual = 'mw:Manual:Pywikibot/refLinks'
        code = None
        for alt in [self.site.code] + i18n._altlang(self.site.code):
            if alt in localized_msg:
                code = alt
                break
        if code:
            manual += '/%s' % code
        if self.getOption('summary') is None:
            self.msg = i18n.twtranslate(self.site, 'reflinks-msg', locals())
        else:
            self.msg = self.getOption('summary')

        local = i18n.translate(self.site, badtitles)
        if local:
            bad = '(' + globalbadtitles + '|' + local + ')'
        else:
            bad = globalbadtitles
        self.titleBlackList = re.compile(bad, re.I | re.S | re.X)
        self.norefbot = noreferences.NoReferencesBot(None, verbose=False)
        self.deduplicator = DuplicateReferences()

        self.site_stop_page = i18n.translate(self.site, stop_page)
        if self.site_stop_page:
            self.stop_page = pywikibot.Page(self.site, self.site_stop_page)
            if self.stop_page.exists():
                self.stop_page_rev_id = self.stop_page.latest_revision_id
            else:
                pywikibot.warning('The stop page %s does not exist'
                                  % self.stop_page.title(asLink=True))

        # Regex to grasp content-type meta HTML tag in HTML source
        self.META_CONTENT = re.compile(br'(?i)<meta[^>]*content\-type[^>]*>')
        # Extract the encoding from a charset property (from content-type !)
        self.CHARSET = re.compile(r'(?i)charset\s*=\s*(?P<enc>[^\'",;>/]*)')
        # Extract html title from page
        self.TITLE = re.compile(r'(?is)(?<=<title>).*?(?=</title>)')
        # Matches content inside <script>/<style>/HTML comments
        self.NON_HTML = re.compile(
            br'(?is)<script[^>]*>.*?</script>|<style[^>]*>.*?</style>|'
            br'<!--.*?-->|<!\[CDATA\[.*?\]\]>')

        # Authorized mime types for HTML pages
        self.MIME = re.compile(
            r'application/(?:xhtml\+xml|xml)|text/(?:ht|x)ml')
Esempio n. 11
0
 def testNonLocalized(self):
     self.assertEqual(i18n.translate('en', self.msg_non_localized),
                      u'test-non-localized EN')
     self.assertEqual(i18n.translate('fy', self.msg_non_localized),
                      u'test-non-localized EN')
     self.assertEqual(i18n.translate('nl', self.msg_non_localized),
                      u'test-non-localized EN')
     self.assertEqual(i18n.translate('ru', self.msg_non_localized),
                      u'test-non-localized EN')
Esempio n. 12
0
    def reportBadAccount(self, name=None, final=False):
        # Queue process
        if name:
            if globalvar.confirm:
                answer = pywikibot.input_choice(
                    "%s may have an unwanted username, do you want to report " "this user?" % name,
                    [("Yes", "y"), ("No", "n"), ("All", "a")],
                    "n",
                    automatic_quit=False,
                )
                if answer in ["a", "all"]:
                    answer = "y"
                    globalvar.confirm = False
            else:
                answer = "y"

            if answer.lower() in ["yes", "y"] or not globalvar.confirm:
                showStatus()
                pywikibot.output("%s is possibly an unwanted username. It will be reported." % name)
                if hasattr(self, "_BAQueue"):
                    self._BAQueue.append(name)
                else:
                    self._BAQueue = [name]

        if len(self._BAQueue) >= globalvar.dumpToLog or final:
            rep_text = ""
            # name in queue is max, put detail to report page
            pywikibot.output("Updating badname accounts to report page...")
            rep_page = pywikibot.Page(self.site, i18n.translate(self.site, report_page))
            if rep_page.exists():
                text_get = rep_page.get()
            else:
                text_get = "This is a report page for the Bad-username, please translate me. --~~~"
            pos = 0
            # The talk page includes "_" between the two names, in this way i
            # replace them to " ".
            for usrna in self._BAQueue:
                username = pywikibot.url2link(usrna, self.site, self.site)
                n = re.compile(re.escape(username), re.UNICODE)
                y = n.search(text_get, pos)
                if y:
                    pywikibot.output("%s is already in the report page." % username)
                else:
                    # Adding the log.
                    rep_text += i18n.translate(self.site, report_text) % username
                    if self.site.code == "it":
                        rep_text = "%s%s}}" % (rep_text, self.bname[username])

            com = i18n.twtranslate(self.site, "welcome-bad_username")
            if rep_text != "":
                rep_page.put(text_get + rep_text, summary=com, force=True, minorEdit=True)
                showStatus(5)
                pywikibot.output("Reported")
            self.BAQueue = list()
        else:
            return True
Esempio n. 13
0
 def testNoEnglish(self):
     """Test translate with missing English text."""
     self.assertEqual(i18n.translate('en', self.msg_no_english,
                                     fallback=True),
                      u'test-no-english JA')
     self.assertEqual(i18n.translate('fy', self.msg_no_english,
                                     fallback=True),
                      u'test-no-english JA')
     self.assertEqual(i18n.translate('nl', self.msg_no_english,
                                     fallback=True),
                      u'test-no-english JA')
Esempio n. 14
0
 def testSemiLocalized(self):
     """Test translate by fallback to an alternative language."""
     self.assertEqual(i18n.translate('en', self.msg_semi_localized,
                                     fallback=True),
                      u'test-semi-localized EN')
     self.assertEqual(i18n.translate('nl', self.msg_semi_localized,
                                     fallback=True),
                      u'test-semi-localized NL')
     self.assertEqual(i18n.translate('fy', self.msg_semi_localized,
                                     fallback=True),
                      u'test-semi-localized NL')
Esempio n. 15
0
 def testLocalized(self):
     """Test fully localized translations."""
     self.assertEqual(i18n.translate('en', self.msg_localized,
                                     fallback=True),
                      u'test-localized EN')
     self.assertEqual(i18n.translate('nl', self.msg_localized,
                                     fallback=True),
                      u'test-localized NL')
     self.assertEqual(i18n.translate('fy', self.msg_localized,
                                     fallback=True),
                      u'test-localized FY')
Esempio n. 16
0
def log(user, pages):
    talk = user.getUserTalkPage()
    titles = [page.title() for page in pages]
    notice = translate(talk.site, msg['notice']).format(titles=titles)
    text = talk.get(force=True)
    if notice in talk.text:
        pywikibot.output(u'\03{lightgreen}already logged')
        return
    header = translate()
    talk.text += u'\n\n== {} ==\n\n{} --~~~~'.format(header, notice)
    pywikibot.showDiff(text, talk.text)
    comment = translate(talk.site, msg['comment'], [bot_link(user.site)])
    talk.save(comment=comment, minor=False, botflag=True)
Esempio n. 17
0
    def __init__(self, generator, **kwargs):
        """- generator : Page generator."""
        self.availableOptions.update(
            {"ignorepdf": False, "limit": None, "summary": None}  # boolean  # int, stop after n modified pages
        )

        super(ReferencesRobot, self).__init__(**kwargs)
        self.generator = generator
        self.site = pywikibot.Site()
        # Check
        manual = "mw:Manual:Pywikibot/refLinks"
        code = None
        for alt in [self.site.code] + i18n._altlang(self.site.code):
            if alt in localized_msg:
                code = alt
                break
        if code:
            manual += "/%s" % code
        if self.getOption("summary") is None:
            self.msg = i18n.twtranslate(self.site, "reflinks-msg", locals())
        else:
            self.msg = self.getOption("summary")
        self.stopPage = pywikibot.Page(self.site, i18n.translate(self.site, stopPage))

        local = i18n.translate(self.site, badtitles)
        if local:
            bad = "(" + globalbadtitles + "|" + local + ")"
        else:
            bad = globalbadtitles
        self.titleBlackList = re.compile(bad, re.I | re.S | re.X)
        self.norefbot = noreferences.NoReferencesBot(None, verbose=False)
        self.deduplicator = DuplicateReferences()
        try:
            self.stopPageRevId = self.stopPage.latest_revision_id
        except pywikibot.NoPage:
            pywikibot.output("The stop page %s does not exist" % self.stopPage.title(asLink=True))
            raise

        # Regex to grasp content-type meta HTML tag in HTML source
        self.META_CONTENT = re.compile(br"(?i)<meta[^>]*content\-type[^>]*>")
        # Extract the encoding from a charset property (from content-type !)
        self.CHARSET = re.compile(br'(?i)charset\s*=\s*(?P<enc>[^\'",;>/]*)')
        # Extract html title from page
        self.TITLE = re.compile(r"(?is)(?<=<title>).*?(?=</title>)")
        # Matches content inside <script>/<style>/HTML comments
        self.NON_HTML = re.compile(
            br"(?is)<script[^>]*>.*?</script>|<style[^>]*>.*?</style>|<!--.*?-->|<!\[CDATA\[.*?\]\]>"
        )

        # Authorized mime types for HTML pages
        self.MIME = re.compile(r"application/(?:xhtml\+xml|xml)|text/(?:ht|x)ml")
    def setSummaryMessage(self, disambPage, new_targets=[], unlink=False,
                          dn=False):
        # make list of new targets
        targets = ''
        for page_title in new_targets:
            targets += u'[[%s]], ' % page_title
        # remove last comma
        targets = targets[:-2]

        if not targets:
            targets = i18n.twtranslate(self.mysite, 'solve_disambiguation-unknown-page')

        # first check whether user has customized the edit comment
        if (self.mysite.family.name in config.disambiguation_comment and
                self.mylang in config.disambiguation_comment[self.mysite.family.name]):
            try:
                self.comment = i18n.translate(
                    self.mysite,
                    config.disambiguation_comment[self.mysite.family.name],
                    fallback=True) % (disambPage.title(), targets)

            # Backwards compatibility, type error probably caused by too
            # many arguments for format string
            except TypeError:
                self.comment = i18n.translate(
                    self.mysite,
                    config.disambiguation_comment[self.mysite.family.name],
                    fallback=True) % disambPage.title()
        elif disambPage.isRedirectPage():
            # when working on redirects, there's another summary message
            if unlink and not new_targets:
                self.comment = i18n.twtranslate(
                    self.mysite,
                    'solve_disambiguation-redirect-removed',
                    {'from': disambPage.title()}
                )
            elif dn and not new_targets:
                self.comment = i18n.twtranslate(
                    self.mysite,
                    'solve_disambiguation-redirect-adding-dn-template',
                    {'from': disambPage.title()}
                )
            else:
                self.comment = i18n.twtranslate(self.mysite, 'solve_disambiguation-redirect-resolved', {'from': disambPage.title(), 'to': targets})
        else:
            if unlink and not new_targets:
                self.comment = i18n.twtranslate(self.mysite, 'solve_disambiguation-links-removed', {'from': disambPage.title()})
            elif dn and not new_targets:
                self.comment = i18n.twtranslate(self.mysite, 'solve_disambiguation-adding-dn-template', {'from': disambPage.title()})
            else:
                self.comment = i18n.twtranslate(self.mysite, 'solve_disambiguation-links-resolved', {'from': disambPage.title(), 'to': targets})
Esempio n. 19
0
 def testNonLocalized(self):
     """Test translate with missing localisation."""
     self.assertEqual(i18n.translate('en', self.msg_non_localized,
                                     fallback=True),
                      u'test-non-localized EN')
     self.assertEqual(i18n.translate('fy', self.msg_non_localized,
                                     fallback=True),
                      u'test-non-localized EN')
     self.assertEqual(i18n.translate('nl', self.msg_non_localized,
                                     fallback=True),
                      u'test-non-localized EN')
     self.assertEqual(i18n.translate('ru', self.msg_non_localized,
                                     fallback=True),
                      u'test-non-localized EN')
Esempio n. 20
0
    def run(self):
        for user in self.parseNewUserLog():
            #print user.name()
            if user.isBlocked():
                #showStatus(3)
                pywikibot.output(u"%s is blocked; skipping." % user.name())
                continue
            if "bot" in user.groups():
                #showStatus(3)
                pywikibot.output(u"%s is a bot; skipping." % user.name())
                continue
            if "bot" in user.name().lower():
                #showStatus(3)
                pywikibot.output(u"%s is probably a bot; skipping." % user.name())
                continue
            if user.editCount() >= globalsettings['reqEditCount']:
                #showStatus(2)
                pywikibot.output(u"%s has enough edits to be welcomed." % user.name())
                ustp =  user.getUserTalkPage()
                if ustp.exists():
                    #showStatus(3)
                    pywikibot.output(u"%s has been already welcomed; skipping." % user.name())
                    continue
                else:
                    welcome_text = i18n.translate(self.site, welcomemsg)
                    welcome_school_text = i18n.translate(self.site, welcomeschoolmsg)
                    if welcome_school_text != None:
                        discrim = i18n.translate(self.site, schooldiscriminator)
                        if discrim != None and re.match(discrim, user.name().lower()): #if the discriminator exists and is present in username
                            pywikibot.output(u"%s is a maybe a school related account; using school welcoming templatei instead." % user.name())
                            welcome_text = welcome_school_text
                    if globalsettings['randomSign'] and len(self.signList) > 0:
                        sign = choice(self.signList)
                    else:
                        sign = globalsettings['defaultSign']

                    welcome_text = (welcome_text % sign)
                    welcome_cmnt = u"Robot: " + i18n.twtranslate(self.site,'welcome-welcome')

                    #print welcome_text
                    #print welcome_cmnt
                    ustp.text = welcome_text
                    
                    try:
                        ustp.save(welcome_cmnt,minor=False)
                        pass
                    except pywikibot.EditConflict:
                        pywikibot.output(u"An edit conflict has occurred, "
                                             u"skipping %s." % user.name())
def main():
    featured = False
    gen = None

    # 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()

    for arg in pywikibot.handleArgs():
        if arg == '-featured':
            featured = True
        else:
            genFactory.handleArg(arg)

    mysite = pywikibot.getSite()
    if mysite.sitename() == 'wikipedia:nl':
        pywikibot.output(
            u'\03{lightred}There is consensus on the Dutch Wikipedia that bots should not be used to fix redirects.\03{default}')
        sys.exit()

    if featured:
        featuredList = i18n.translate(mysite, featured_articles)
        ref = pywikibot.Page(pywikibot.getSite(), featuredList)
        gen = pagegenerators.ReferringPageGenerator(ref)
        gen = pagegenerators.NamespaceFilterPageGenerator(gen, [0])
    if not gen:
        gen = genFactory.getCombinedGenerator()
    if gen:
        for page in pagegenerators.PreloadingGenerator(gen):
            workon(page)
    else:
        pywikibot.showHelp('fixing_redirects')
Esempio n. 22
0
 def changeCommonscat(self, page=None, oldtemplate=u'', oldcat=u'',
                      newtemplate=u'', newcat=u'', linktitle=u'',
                      description=u''):
     """ Change the current commonscat template and target. """
     if oldcat == '3=S' or linktitle == '3=S':
         return  # additional param on de-wiki, TODO: to be handled
     if not linktitle and (page.title().lower() in oldcat.lower() or
                           oldcat.lower() in page.title().lower()):
         linktitle = oldcat
     if linktitle and newcat != page.title(withNamespace=False):
         newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
                          % oldtemplate,
                          u'{{%s|%s|%s}}' % (newtemplate, newcat, linktitle),
                          page.get())
     elif newcat == page.title(withNamespace=False):
         newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
                          % oldtemplate,
                          u'{{%s}}' % newtemplate,
                          page.get())
     elif oldcat.strip() != newcat:  # strip trailing white space
         newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}'
                          % oldtemplate,
                          u'{{%s|%s}}' % (newtemplate, newcat),
                          page.get())
     else:  # nothing left to do
         return
     if self.summary:
         comment = self.summary
     else:
         comment = i18n.translate(page.site,
                                  msg_change,
                                  fallback=True) % {'oldcat': oldcat,
                                                    'newcat': newcat}
     self.save(newtext, page, comment)
Esempio n. 23
0
def appendtext(page, apptext, always):
    if page.isRedirectPage():
        page = page.getRedirectTarget()
    if not page.exists():
        if page.isTalkPage():
            text = u''
        else:
            raise pywikibot.NoPage(u"Page '%s' does not exist" % page.title())
    else:
        text = page.text
    # Here you can go editing. If you find you do not
    # want to edit this page, just return
    oldtext = text
    text += apptext
    if text != oldtext:
        pywikibot.showDiff(oldtext, text)
        if not always:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?', ['Yes', 'No', 'All'],
                'yNa', 'N')
            if choice == 'a':
                always = True
        if always or choice == 'y':
            page.text = text
            page.save(i18n.translate(pywikibot.Site(), comment,
                                     fallback=True))
Esempio n. 24
0
    def treat(self, page):
        """
        Load the given page, make the required changes, and save it.

        @param page: the page to treat
        @type page: pywikibot.Page
        """
        self.current_page = page
        page.get()
        wikicode = mwparserfromhell.parse(page.text)
        for el in wikicode.ifilter_tags():
            if el.tag in self.getOption('tags'):
                try:
                    result = self.process_lines(el.contents)
                except PoemError as e:
                    pywikibot.warning(e)
                    continue
                if result:
                    lines, numbering, lines_count = result
                    if lines != el.contents:
                        scheme = self.detect_numbering(numbering, lines_count)
                        if scheme:
                            el.contents = lines
                            for attr, val in scheme.items():
                                el.add(attr, val)
                            if self.getOption('preferred'):
                                # change the tag name to the preferred form
                                el.tag = self.getOption('preferred')
                        else:
                            pywikibot.warning(u'a reliable line numbering scheme could not be obtained')
        newtext = unicode(wikicode)
        summary = self.getOption('summary')
        if not summary:
            summary = i18n.translate(page.site, self.summary, fallback=True)
        self.userPut(page, page.text, newtext, comment=summary)
Esempio n. 25
0
def bot_link(site):
    group = site.mediawiki_message('group-bot')
    group = pywikibot.Page(site, group, ns=4).title()
    member = translate(site,
                       site.mediawiki_message('group-bot-member'),
                       site.user())
    return u'[[{}|{}]]'.format(group, member)
Esempio n. 26
0
    def __init__(self, **kwargs):
        """Constructor."""
        super(SandboxBot, self).__init__(**kwargs)
        if self.getOption('delay') is None:
            d = min(15, max(5, int(self.getOption('hours') * 60)))
            self.availableOptions['delay_td'] = datetime.timedelta(minutes=d)
        else:
            d = max(5, self.getOption('delay'))
            self.availableOptions['delay_td'] = datetime.timedelta(minutes=d)

        self.site = pywikibot.Site()
        if self.getOption('user'):
            localSandboxTitle = i18n.translate(self.site,
                                               user_sandboxTemplate)
            localSandbox = pywikibot.Page(self.site, localSandboxTitle)
            content.update(user_content)
            sandboxTitle[self.site.code] = [item.title() for item in
                                            localSandbox.getReferences(
                                                onlyTemplateInclusion=True)]
            if self.site.code not in user_sandboxTemplate:
                content[self.site.code] = None
                pywikibot.output(
                    u'Not properly set-up to run in user namespace!')
        if (not sandboxTitle.get(self.site.code) and not self.getOption('page')) or (not content.get(
                self.site.code) and not self.getOption('text')):
            pywikibot.output(u'This bot is not configured for the given site '
                             u'(%s), exiting.' % self.site)
            sys.exit(0)
def str2localized_duration(site, string):
    """
    Localise a shorthand duration.

    Translates a duration written in the shorthand notation (ex. "24h", "7d")
    into an expression in the local language of the wiki ("24 hours", "7 days").
    """
    mw_keys = {
        'm': 'minutes',
        'h': 'hours',
        'd': 'days',
        'w': 'weeks',
        'M': 'months',
        'y': 'years',
    }

    template = site.mediawiki_message(mw_keys[string[-1]])
    if template:
        duration = string[:-1]
        # replace plural variants
        exp = i18n.translate(site.code, template, {'$1': int(duration)})

        return exp.replace('$1', to_local_digits(duration, site.code))
    else:
        return to_local_digits(string, site.code)
def main():
    featured = False
    gen = None

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

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

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

    if featured:
        featuredList = i18n.translate(mysite, featured_articles)
        ref = pywikibot.Page(pywikibot.Site(), featuredList)
        gen = pagegenerators.ReferringPageGenerator(ref)
        gen = pagegenerators.NamespaceFilterPageGenerator(gen, [0])
    if not gen:
        gen = genFactory.getCombinedGenerator()
    if gen:
        for page in pagegenerators.PreloadingGenerator(gen):
            workon(page)
    else:
        pywikibot.showHelp('fixing_redirects')
Esempio n. 29
0
    def makelogpage(self, queue=None):
        """Make log page."""
        if queue is None:
            queue = []
        if not globalvar.makeWelcomeLog or len(queue) == 0:
            return

        text = u''
        logg = i18n.translate(self.site, logbook)
        if not logg:
            return

        target = logg + '/' + time.strftime('%Y/%m/%d',
                                            time.localtime(time.time()))
        if self.site.code == 'it':
            target = logg + '/' + time.strftime('%d/%m/%Y',
                                                time.localtime(time.time()))

        logPage = pywikibot.Page(self.site, target)
        if logPage.exists():
            text = logPage.get()
        else:
            # make new log page
            showStatus()
            pywikibot.output(
                'Log page is not exist, getting information for page creation')
            text = i18n.translate(self.site, logpage_header,
                                  fallback=i18n.DEFAULT_FALLBACK)
            text += u'\n!%s' % self.site.namespace(2)
            text += u'\n!%s' % str.capitalize(
                self.site.mediawiki_message('contribslink'))

        for result in queue:
            # Adding the log... (don't take care of the variable's name...).
            luser = pywikibot.url2link(result.name(), self.site, self.site)
            text += u'\n{{WLE|user=%s|contribs=%d}}' % (
                luser, result.editCount())
        # update log page.
        while True:
            try:
                logPage.put(text, i18n.twtranslate(self.site,
                                                   'welcome-updating'))
                return True
            except pywikibot.EditConflict:
                pywikibot.output(u'An edit conflict has occurred. Pausing for '
                                 u'10 seconds before continuing.')
                time.sleep(10)
Esempio n. 30
0
 def useHashGenerator(self):
     """Use hash generator."""
     # https://toolserver.org/~multichill/nowcommons.php?language=it&page=2&filter=
     lang = self.site.lang
     num_page = 0
     word_to_skip_translated = i18n.translate(self.site, word_to_skip)
     images_processed = list()
     while 1:
         url = ('https://toolserver.org/~multichill/nowcommons.php?'
                'language=%s&page=%s&filter=') % (lang, num_page)
         HTML_text = self.site.getUrl(url, no_hostname=True)
         reg = r'<[Aa] href="(?P<urllocal>.*?)">(?P<imagelocal>.*?)</[Aa]> +?</td><td>\n\s*?'
         reg += r'<[Aa] href="(?P<urlcommons>http[s]?://commons.wikimedia.org/.*?)" \
                >Image:(?P<imagecommons>.*?)</[Aa]> +?</td><td>'
         regex = re.compile(reg, re.UNICODE)
         found_something = False
         change_page = True
         for x in regex.finditer(HTML_text):
             found_something = True
             image_local = x.group('imagelocal')
             image_commons = x.group('imagecommons')
             if image_local in images_processed:
                 continue
             change_page = False
             images_processed.append(image_local)
             # Skip images that have something in the title (useful for it.wiki)
             image_to_skip = False
             for word in word_to_skip_translated:
                 if word.lower() in image_local.lower():
                     image_to_skip = True
             if image_to_skip:
                 continue
             url_local = x.group('urllocal')
             url_commons = x.group('urlcommons')
             pywikibot.output(color_format(
                 '\n\n>>> {lightpurple}{0}{default} <<<',
                 image_local))
             pywikibot.output(u'Local: %s\nCommons: %s\n'
                              % (url_local, url_commons))
             webbrowser.open(url_local, 0, 1)
             webbrowser.open(url_commons, 0, 1)
             if image_local.split('Image:')[1] == image_commons:
                 choice = pywikibot.input_yn(
                     u'The local and the commons images have the same name, '
                     'continue?', default=False, automatic_quit=False)
             else:
                 choice = pywikibot.input_yn(
                     u'Are the two images equal?',
                     default=False, automatic_quit=False)
             if choice:
                 yield [image_local, image_commons]
             else:
                 continue
         # The page is dinamically updated, so we may don't need to change it
         if change_page:
             num_page += 1
         # If no image found means that there aren't anymore, break.
         if not found_something:
             break
Esempio n. 31
0
 def createReferenceSection(self, oldText, index, ident='=='):
     if self.site.language() in noTitleRequired:
         newSection = u'\n%s\n' % (self.referencesText)
     else:
         newSection = u'\n%s %s %s\n%s\n' % (ident,
                                             i18n.translate(
                                                 self.site,
                                                 referencesSections)[0],
                                             ident, self.referencesText)
     return oldText[:index] + newSection + oldText[index:]
Esempio n. 32
0
 def refDead(self):
     """Dead link, tag it with a {{dead link}}."""
     tag = i18n.translate(self.site, deadLinkTag)
     if not tag:
         dead_link = self.refLink()
     else:
         if '%s' in tag:
             tag %= self.link
         dead_link = '<ref{}>{}</ref>'.format(self.name, tag)
     return dead_link
Esempio n. 33
0
 def refDead(self):
     """Dead link, tag it with a {{dead link}}."""
     tag = i18n.translate(self.site, deadLinkTag)
     if not tag:
         dead_link = self.refLink()
     elif '%s' in tag:
         dead_link = '<ref%s>%s</ref>' % (self.refname, tag % self.link)
     else:
         dead_link = '<ref%s>%s</ref>' % (self.refname, tag)
     return dead_link
Esempio n. 34
0
    def test_translate_commons(self):
        """Test localization with xdict for commons.

        Test whether the localzation is found either with the Site object
        or with the site code.
        """
        site = Site('commons', 'commons')
        for code in (site, 'commons'):
            with self.subTest(code=code):
                self.assertEqual(i18n.translate(code, self.xdict),
                                 'test-localized COMMONS')
Esempio n. 35
0
    def test_translate_fy(self):
        """Test localization fallbacks for 'fy' with xdict.

        'fy' key is defined in 'wikipedia' and  'wikisource' sub dicts.
        They should have different localizations for these two families but
        'wikisource' should have a fallback to the 'wikipedia' entry.

        Note: If the translate code is given as string, the result depends
        on the current config.family entry. Therefore there is no test with
        the code given as string.
        """
        site1 = Site('fy', 'wikipedia')
        site2 = Site('fy', 'wikibooks')
        site3 = Site('fy', 'wikisource')
        for code in (site1, site2):
            with self.subTest(code=code):
                self.assertEqual(i18n.translate(code, self.xdict),
                                 'test-localized WP-FY')
        self.assertEqual(i18n.translate(site3, self.xdict),
                         'test-localized WS-FY')
Esempio n. 36
0
    def treat(self, user) -> None:
        """Run the bot."""
        self.show_status(Msg.MATCH)
        pywikibot.output('{} has enough edits to be welcomed.'.format(
            user.username))
        ustp = user.getUserTalkPage()
        if ustp.exists():
            self.show_status(Msg.SKIP)
            pywikibot.output('{} has been already welcomed.'.format(
                user.username))
            return

        if self.badNameFilter(user.username):
            self.collect_bad_accounts(user.username)
            return

        welcome_text = self.welcome_text
        if globalvar.randomSign:
            if self.site.family.name != 'wikinews':
                welcome_text = welcome_text % choice(self.defineSign())
            if self.site.sitename != 'wiktionary:it':
                welcome_text += timeselected
        elif self.site.sitename != 'wikinews:it':
            welcome_text = welcome_text % globalvar.defaultSign

        final_text = i18n.translate(self.site, final_new_text_additions)
        if final_text:
            welcome_text += final_text
        welcome_comment = i18n.twtranslate(self.site, 'welcome-welcome')
        try:
            # append welcomed, welcome_count++
            ustp.put(welcome_text, welcome_comment, minor=False)
        except EditConflictError:
            self.show_status(Msg.WARN)
            pywikibot.output(
                'An edit conflict has occurred, skipping this user.')
        else:
            self.welcomed_users.append(user)

        welcomed_count = len(self.welcomed_users)
        if globalvar.makeWelcomeLog:
            self.show_status(Msg.DONE)
            if welcomed_count == 0:
                count = 'No users have'
            elif welcomed_count == 1:
                count = 'One user has'
            else:
                count = '{} users have'.format(welcomed_count)
            pywikibot.output(count + ' been welcomed.')

            if welcomed_count >= globalvar.dumpToLog:
                self.makelogpage()
Esempio n. 37
0
    def process_lines(self, lines):
        """
        Remove old templates from lines and obtain line numbers.

        @param lines: the lines to clean up
        @type lines: unicode
        @return: cleaned-up lines and line numbers
        @rtype: tuple containing a unicode and a list of ints
        """
        ln = 0
        numbering = []
        lines = lines.split('\n')
        tmps = i18n.translate(self.current_page.site,
                              self.line_tmps,
                              fallback=False)
        for i, line in enumerate(lines):
            if line.strip() != '':
                ln += 1
                code = mwparserfromhell.parse(line)
                for tmp in code.ifilter_templates():
                    if tmp.name.strip() not in tmps:
                        pywikibot.warning(u'unexpected template: "{}"'.format(
                            tmp.name))
                        continue
                    if ln in numbering:
                        raise PoemError(u'exceeding template: "{}"', tmp.name)
                    params = [
                        param for param in tmp.params
                        if tmp.has(param.name, True)
                    ]
                    if len(params) == 0:
                        raise PoemError(u'no parameters found in "{}"',
                                        tmp.name)
                    if len(params) > 1:
                        raise PoemError(u'multiple parameters found in "{}"',
                                        tmp.name)
                    if params[0].name.strip() != '1':
                        raise PoemError(u'unexpected parameter: "{}" in "{}"',
                                        params[0].name, tmp.name)
                    if params[0].value.strip() != unicode(ln):
                        raise PoemError(
                            u'unexpected line marker: "{}" in "{}"',
                            params[0].value, tmp.name)
                    if line.rstrip()[-len(unicode(tmp)):] != unicode(tmp):
                        raise PoemError(
                            u'the "{}" template is not at the end of the line',
                            tmp.name)
                    line = line.rstrip()[:-len(unicode(tmp))]
                    line = line.rstrip()  # possibly breaking change
                    lines[i] = line
                    numbering.append(ln)
        return '\n'.join(lines), numbering, len(lines)
Esempio n. 38
0
    def __init__(self, **kwargs) -> None:
        """Initializer."""
        super().__init__(**kwargs)
        self.check_managed_sites()
        self.bname = {}  # type: Dict[str, str]

        self.welcomed_users = []  # type: List[str]
        self.log_name = i18n.translate(self.site, logbook)

        if not self.log_name:
            globalvar.make_welcome_log = False
        if globalvar.random_sign:
            self.define_sign(True)
Esempio n. 39
0
    def test_translate_nl(self):
        """Test localization fallbacks for 'nl' with xdict.

        'nl' key is defined in 'wikipedia' sub dict. Therefore all
        localizations have a fallback to the 'wikipedia' entry.
        """
        site1 = Site('nl', 'wikipedia')
        site2 = Site('nl', 'wikibooks')
        site3 = Site('nl', 'wikisource')
        for code in (site1, site2, site3, 'nl'):
            with self.subTest(code=code):
                self.assertEqual(i18n.translate(code, self.xdict),
                                 'test-localized WP-NL')
Esempio n. 40
0
    def __init__(self, **kwargs) -> None:
        """Initializer."""
        super().__init__(**kwargs)
        self.check_managed_sites()
        self.bname = {}

        self.welcomed_users = []
        self.log_name = i18n.translate(self.site, logbook)

        if not self.log_name:
            globalvar.makeWelcomeLog = False
        if globalvar.randomSign:
            self.defineSign(True)
Esempio n. 41
0
def str2localized_duration(site, string):
    """Translate a duration written in the shorthand notation (ex. "24h", "7d")
    into an expression in the local language of the wiki ("24 hours", "7 days").
    """
    if string[-1] == 'd':
        template = site.mediawiki_message('Days')
    elif string[-1] == 'h':
        template = site.mediawiki_message('Hours')
    if template:
        exp = i18n.translate(site.code, template, int(string[:-1]))
        return exp.replace('$1', string[:-1])
    else:
        return string
Esempio n. 42
0
    def test_translate_de(self):
        """Test localization fallbacks for 'de' with xdict.

        'de' key is defined in a nested 'wikipedia' sub dict. This should
        always fall back to this nested 'wikipedia' entry.
        """
        site1 = Site('de', 'wikipedia')
        site2 = Site('de', 'wikibooks')
        site3 = Site('de', 'wikisource')
        for code in (site1, site2, site3, 'de'):
            with self.subTest(code=code):
                self.assertEqual(i18n.translate(code, self.xdict),
                                 'test-localized WP-DE')
Esempio n. 43
0
def str2localized_duration(site, string: str) -> str:
    """
    Localise a shorthand duration.

    Translates a duration written in the shorthand notation (ex. "24h", "7d")
    into an expression in the local wiki language ("24 hours", "7 days").
    """
    key, duration = checkstr(string)
    template = site.mediawiki_message(MW_KEYS[key])
    if template:
        # replace plural variants
        exp = i18n.translate(site.code, template, {'$1': int(duration)})
        return exp.replace('$1', to_local_digits(duration, site.code))
    return to_local_digits(string, site.code)
Esempio n. 44
0
    def __init__(self, **kwargs):
        """Initializer."""
        super().__init__(**kwargs)

        # handle the custom templates
        if not self.opt.filetemplate:
            self.opt.filetemplate = i18n.translate(self.site,
                                                   template_to_the_image)
        elif not re.fullmatch('{{.+}}', self.opt.filetemplate):
            self.opt.filetemplate = '{{%s}}' % self.opt.filetemplate

        if not self.opt.usertemplate:
            self.opt.usertemplate = i18n.translate(self.site,
                                                   template_to_the_user)
        elif not re.fullmatch('{{.+}}', self.opt.usertemplate):
            self.opt.usertemplate = '{{%s}}' % self.opt.usertemplate

        if not (self.opt.filetemplate and
                (self.opt.usertemplate or self.opt.nouserwarning)):
            # if no templates are given
            raise TranslationError(
                'This script is not localized for {} site;\n'
                'try using -filetemplate:<template name>.'.format(self.site))
Esempio n. 45
0
    def test_translate_en(self):
        """Test localization fallbacks for 'en' with xdict.

        'en' key is defined directly in xdict. This topmost key goes over
        site specific key. Therefore 'test-localized WS-EN' is not given
        back.
        """
        site1 = Site('en', 'wikipedia')
        site2 = Site('en', 'wikibooks')
        site3 = Site('en', 'wikisource')
        for code in (site1, site2, site3, 'en'):
            with self.subTest(code=code):
                self.assertEqual(i18n.translate(code, self.xdict),
                                 'test-localized EN')
Esempio n. 46
0
    def report_bad_account(self) -> None:
        """Report bad account."""
        rep_text = ''
        # name in queue is max, put detail to report page
        pywikibot.output('Updating badname accounts to report page...')
        rep_page = pywikibot.Page(self.site,
                                  i18n.translate(self.site, report_page))
        if rep_page.exists():
            text_get = rep_page.get()
        else:
            text_get = ('This is a report page for the Bad-username, '
                        'please translate me. --~~~')
        pos = 0
        # The talk page includes "_" between the two names, in this way
        # replace them to " ".
        for usrna in self._BAQueue:
            username = pywikibot.url2link(usrna, self.site, self.site)
            n = re.compile(re.escape(username))
            y = n.search(text_get, pos)
            if y:
                pywikibot.output(
                    '{} is already in the report page.'.format(username))
            else:
                # Adding the log.
                rep_text += i18n.translate(self.site, report_text) % username
                if self.site.code == 'it':
                    rep_text = '%s%s}}' % (rep_text, self.bname[username])

        com = i18n.twtranslate(self.site, 'welcome-bad_username')
        if rep_text != '':
            rep_page.put(text_get + rep_text,
                         summary=com,
                         force=True,
                         minor=True)
            self.show_status(Msg.DONE)
            pywikibot.output('Reported')
        self.BAQueue = []
Esempio n. 47
0
    def treat(self, page):
        """Re-directing process.

        Check if pages are in the given form Something, State, and
        if so, create a redirect from Something, ST..
        """
        for sn in self.abbrev:
            R = re.compile(r', %s$' % sn)
            if R.search(page.title()):
                pl = pywikibot.Page(self.site, page.title().replace(sn,
                                    self.abbrev[sn]))
                # A bit hacking here - the real work is done in the
                # 'except pywikibot.NoPage' part rather than the 'try'.

                try:
                    pl.get(get_redirect=True)
                    goal = pl.getRedirectTarget().title()
                    if pywikibot.Page(self.site, goal).exists():
                        pywikibot.output(
                            u"Not creating %s - redirect already exists."
                            % goal)
                    else:
                        pywikibot.warning(
                            u"%s already exists but redirects elsewhere!"
                            % goal)
                except pywikibot.IsNotRedirectPage:
                    pywikibot.warning(
                        u"Page %s already exists and is not a redirect "
                        u"Please check page!"
                        % pl.title())
                except pywikibot.NoPage:
                    change = ''
                    if page.isRedirectPage():
                        p2 = page.getRedirectTarget()
                        pywikibot.output(
                            u'Note: goal page is redirect.\nCreating redirect '
                            u'to "%s" to avoid double redirect.' % p2.title())
                    else:
                        p2 = page
                    if self.force:
                        change = 'y'
                    else:
                        change = pywikibot.input_choice(
                            u'Create redirect %s?' % pl.title(),
                            (('yes', 'y'), ('no', 'n')))
                    if change == 'y':
                        pl.set_redirect_target(
                            p2, create=True,
                            summary=i18n.translate(self.site, msg))
Esempio n. 48
0
 def treat_page(self):
     """Treat current page."""
     page = self.current_page
     code = mwparserfromhell.parse(page.text)
     edited = False  # to prevent unwanted changes
     for template in code.ifilter_templates():
         if not page.site.sametitle(template.name.strip(), 'Information'):
             continue
         desc = self.get_description(template)
         if desc is None:
             continue
         for tmp in desc.value.filter_templates(recursive=False):
             if self.process_desc_template(tmp):
                 edited = True
         desc_clean = copy.deepcopy(desc.value)
         for tmp in desc_clean.filter_templates(recursive=False):
             # TODO: emit a debug item?
             desc_clean.remove(tmp)
         value = desc_clean.strip()
         if value == '':
             pywikibot.output('Empty description')
             continue
         pywikibot.output(value)
         langs = self.detect_langs(value)
         if langs:
             pywikibot.output(
                 color_format('{lightblue}Hints from langdetect:{default}'))
             for language in langs:
                 pywikibot.output(
                     color_format(
                         '{{lightblue}}{obj.lang}: {obj.prob}{{default}}',
                         obj=language))
         lang = pywikibot.input(
             'Enter the language of the displayed text:').strip()
         if lang != '':
             tmp_page = pywikibot.Page(page.site, lang, ns=10)
             if tmp_page not in self.lang_tmps:
                 pywikibot.warning(
                     '"{lang}" is not a valid language template on {site}'.
                     format(lang=lang, site=page.site))
             new = mwparserfromhell.nodes.template.Template(lang, [value])
             self.replace_value(desc, new)
             edited = True
     if edited:
         text = str(code)
         summary = i18n.translate(page.site.lang,
                                  self.comment,
                                  fallback=True)
         self.put_current(text, summary=summary)
Esempio n. 49
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 = {}
    checkcurrent = False

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

    for arg in local_args:
        if arg.startswith('-summary'):
            if len(arg) == 8:
                options['summary'] = pywikibot.input(
                    'What summary do you want to use?')
            else:
                options['summary'] = arg[9:]
        elif arg.startswith('-checkcurrent'):
            checkcurrent = True
        elif arg == '-always':
            options['always'] = True
        else:
            genFactory.handleArg(arg)

    if checkcurrent:
        site = pywikibot.Site()
        primaryCommonscat, commonscatAlternatives = i18n.translate(
            site.code, commonscatTemplates, fallback=i18n.DEFAULT_FALLBACK)
        template_page = pywikibot.Page(site, 'Template:' + primaryCommonscat)
        generator = template_page.getReferences(namespaces=14,
                                                only_template_inclusion=True)
    else:
        generator = genFactory.getCombinedGenerator()

    if generator:
        if not genFactory.nopreload:
            generator = pagegenerators.PreloadingGenerator(generator)
        bot = CommonscatBot(generator=generator, **options)
        bot.run()
        return True
    else:
        pywikibot.bot.suggest_help(missing_generator=True)
        return False
Esempio n. 50
0
def workon(page, links):
    text = page.get()
    # Show the title of the page we're working on.
    # Highlight the title in purple.
    pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                     % page.title())
    for page2 in links:
        try:
            target = page2.getRedirectTarget()
        except (pywikibot.Error, pywikibot.SectionError):
            continue
        text = treat(text, page2, target)
    if text != page.get():
        comment = i18n.translate(page.site, msg, fallback=True)
        page.put(text, comment)
Esempio n. 51
0
    def __init__(self, generator, **kwargs):
        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
        self.site = pywikibot.Site()
        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')
        self.template = i18n.translate(self.site, template)
        self.exception = i18n.translate(self.site, exception_regex)
        if self.template is None or self.exception is None:
            pywikibot.showHelp()
            sys.exit(u'Missing configuration for site %s' % self.site)
        # 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
Esempio n. 52
0
    def changeCommonscat(self,
                         page=None,
                         oldtemplate=u'',
                         oldcat=u'',
                         newtemplate=u'',
                         newcat=u'',
                         linktitle=u'',
                         description=u''):
        """ Change the current commonscat template and target. """
        if oldcat == '3=S' or linktitle == '3=S':
            return  # additional param on de-wiki, TODO: to be handled
        if not linktitle and (page.title().lower() in oldcat.lower()
                              or oldcat.lower() in page.title().lower()):
            linktitle = oldcat
        if linktitle and newcat != page.title(withNamespace=False):
            newtext = re.sub(
                u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}' % oldtemplate,
                u'{{%s|%s|%s}}' % (newtemplate, newcat, linktitle), page.get())
        elif newcat == page.title(withNamespace=False):
            newtext = re.sub(
                u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}' % oldtemplate,
                u'{{%s}}' % newtemplate, page.get())
        elif oldcat.strip() != newcat:  # strip trailing white space
            newtext = re.sub(
                u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}' % oldtemplate,
                u'{{%s|%s}}' % (newtemplate, newcat), page.get())
        else:  # nothing left to do
            return
        if self.summary:
            comment = self.summary
        else:
            comment = i18n.translate(page.site, msg_change, fallback=True) % {
                'oldcat': oldcat,
                'newcat': newcat
            }

        try:
            self.userPut(page, page.text, newtext, comment=comment)
        except pywikibot.LockedPage:
            pywikibot.output(u"Page %s is locked; skipping." %
                             page.title(asLink=True))
        except pywikibot.EditConflict:
            pywikibot.output(u'Skipping %s because of edit conflict' %
                             (page.title()))
        except pywikibot.SpamfilterError as error:
            pywikibot.output(
                u'Cannot change %s because of spam blacklist entry %s' %
                (page.title(), error.url))
Esempio n. 53
0
    def __init__(self, **kwargs):
        """Initializer.

        :keyword site: the site to work on
        :type site: pywikibot.APISite
        """
        super().__init__(**kwargs)
        csd_cat = i18n.translate(self.site, self.csd_cat_title)
        if csd_cat is None:
            self.csd_cat = self.site.page_from_repository(self.csd_cat_item)
            if self.csd_cat is None:
                raise Error(
                    'No category for speedy deletion found for {}'.format(
                        self.site))
        else:
            self.csd_cat = pywikibot.Category(self.site, csd_cat)
        self.saved_progress = None
Esempio n. 54
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 = {}
    checkcurrent = False

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

    for arg in local_args:
        opt, _, value = arg.partition(':')
        option = opt[1:] if opt[0] == '-' else None
        if option == 'summary':
            options[option] = value or pywikibot.input(
                'What summary do you want to use?')
        elif option == 'checkcurrent':
            checkcurrent = True
        elif option == 'always':
            options[option] = True
        else:
            genFactory.handle_arg(arg)

    if checkcurrent:
        site = pywikibot.Site()
        primaryCommonscat, commonscatAlternatives = i18n.translate(
            site.code, commonscatTemplates, fallback=i18n.DEFAULT_FALLBACK)
        template_page = pywikibot.Page(site, 'Template:' + primaryCommonscat)
        generator = template_page.getReferences(namespaces=14,
                                                only_template_inclusion=True)
    else:
        generator = genFactory.getCombinedGenerator()

    if generator:
        if not genFactory.nopreload:
            generator = pagegenerators.PreloadingGenerator(generator)
        bot = CommonscatBot(generator=generator, **options)
        bot.run()
    else:
        pywikibot.bot.suggest_help(missing_generator=True)
Esempio n. 55
0
    def makelogpage(self):
        """Make log page."""
        if not globalvar.makeWelcomeLog or not self.welcomed_users:
            return

        if self.site.code == 'it':
            pattern = '%d/%m/%Y'
        else:
            pattern = '%Y/%m/%d'
        target = self.log_name + '/' + time.strftime(
            pattern, time.localtime(time.time()))

        log_page = pywikibot.Page(self.site, target)
        if log_page.exists():
            text = log_page.get()
        else:
            # make new log page
            self.show_status()
            pywikibot.output(
                'Log page is not exist, getting information for page creation')
            text = i18n.translate(self.site,
                                  logpage_header,
                                  fallback=i18n.DEFAULT_FALLBACK)
            text += '\n!' + self.site.namespace(2)
            text += '\n!' + str.capitalize(
                self.site.mediawiki_message('contribslink'))

        # Adding the log... (don't take care of the variable's name...).
        text += '\n'
        text += '\n'.join(
            '{{WLE|user=%s|contribs=%d}}' %
            (user.title(as_url=True, with_ns=False), user.editCount())
            for user in self.welcomed_users)

        # update log page.
        while True:
            try:
                log_page.put(text,
                             i18n.twtranslate(self.site, 'welcome-updating'))
            except EditConflictError:
                pywikibot.output('An edit conflict has occurred. Pausing for '
                                 '10 seconds before continuing.')
                time.sleep(10)
            else:
                break
        self.welcomed_users = []
Esempio n. 56
0
    def defineSign(self, force=False):
        """Setup signature."""
        if hasattr(self, '_randomSignature') and not force:
            return self._randomSignature

        signText = u''
        creg = re.compile(r"^\* ?(.*?)$", re.M)
        if not globalvar.signFileName:
            signPageName = i18n.translate(self.site, random_sign)
            if not signPageName:
                showStatus(4)
                pywikibot.output(
                    "%s doesn't allow random signature, force disable." %
                    self.site)
                globalvar.randomSign = False
                return

            signPage = pywikibot.Page(self.site, signPageName)
            if signPage.exists():
                pywikibot.output('Loading signature list...')
                signText = signPage.get()
            else:
                pywikibot.output(
                    'The Signature list page is not exist, random '
                    'signature will disable.')
                globalvar.randomSign = False
        else:
            try:
                f = codecs.open(pywikibot.config.datafilepath(
                    globalvar.signFileName),
                                'r',
                                encoding=config.console_encoding)
            except LookupError:
                f = codecs.open(pywikibot.config.datafilepath(
                    globalvar.signFileName),
                                'r',
                                encoding='utf-8')
            except IOError:
                pywikibot.error(u'No fileName!')
                raise FilenameNotSet("No signature filename specified.")

            signText = f.read()
            f.close()
        self._randomSignature = creg.findall(signText)
        return self._randomSignature
Esempio n. 57
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
Esempio n. 58
0
    def defineSign(self, force=False) -> List[str]:
        """Setup signature."""
        if hasattr(self, '_randomSignature') and not force:
            return self._randomSignature

        sign_text = ''
        creg = re.compile(r'^\* ?(.*?)$', re.M)
        if not globalvar.signFileName:
            sign_page_name = i18n.translate(self.site, random_sign)
            if not sign_page_name:
                self.show_status(Msg.WARN)
                pywikibot.output(
                    "{} doesn't allow random signature, force disable.".format(
                        self.site))
                globalvar.randomSign = False
                return None

            sign_page = pywikibot.Page(self.site, sign_page_name)
            if sign_page.exists():
                pywikibot.output('Loading signature list...')
                sign_text = sign_page.get()
            else:
                pywikibot.output('The signature list page does not exist, '
                                 'random signature will be disabled.')
                globalvar.randomSign = False
        else:
            try:
                f = codecs.open(pywikibot.config.datafilepath(
                    globalvar.signFileName),
                                'r',
                                encoding=config.console_encoding)
            except LookupError:
                f = codecs.open(pywikibot.config.datafilepath(
                    globalvar.signFileName),
                                'r',
                                encoding='utf-8')
            except IOError:
                pywikibot.error('No fileName!')
                raise FilenameNotSet('No signature filename specified.')

            sign_text = f.read()
            f.close()
        self._randomSignature = creg.findall(sign_text)
        return self._randomSignature
Esempio n. 59
0
    def __init__(self, always, alternatives, getAlternatives, dnSkip, generator,
                 primary, main_only, minimum=0):
        super(DisambiguationRobot, self).__init__()
        self.always = always
        self.alternatives = alternatives
        self.getAlternatives = getAlternatives
        self.dnSkip = dnSkip
        self.generator = generator
        self.primary = primary
        self.main_only = main_only
        self.minimum = minimum

        self.site = self.mysite = pywikibot.Site()
        self.mylang = self.mysite.lang
        self.comment = None

        self.dn_template_str = i18n.translate(self.mysite, dn_template)

        self.setupRegexes()
Esempio n. 60
0
    def __init__(self, generator, old_image, new_image=None, **kwargs):
        """
        Constructor.

        @param generator: the pages to work on
        @type  generator: iterable
        @param old_image: the title of the old image (without namespace)
        @type  old_image: unicode
        @param new_image: the title of the new image (without namespace), or
                          None if you want to remove the image
        @type  new_image: unicode or None
        """
        self.availableOptions.update({
            'summary': None,
            'loose': False,
        })

        Bot.__init__(self, generator=generator, **kwargs)

        self.old_image = old_image
        self.new_image = new_image

        if not self.getOption('summary'):
            self.options['summary'] = i18n.translate(
                self.site,
                self.msg_replace,
                (self.old_image,
                 self.new_image) if self.new_image else self.old_image,
                fallback=True)

        # regular expression to find the original template.
        # {{vfd}} does the same thing as {{Vfd}}, so both will be found.
        # The old syntax, {{msg:vfd}}, will also be found.
        # The group 'parameters' will either match the parameters, or an
        # empty string if there are none.

        replacements = []

        namespace = self.site.namespaces[6]
        if namespace.case == 'first-letter':
            case = re.escape(self.old_image[0].upper() +
                             self.old_image[0].lower())
            escaped = '[' + case + ']' + re.escape(self.old_image[1:])