def main():
    pywikibot.warning("this script should not be run manually/directly, but automatically by maintainer.py")
    if len(sys.argv) == 1:
        pywikibot.output("Usage: censure.py <article title>")
        sys.exit(1)
    del sys.argv[0]
    checkPage(" ".join(sys.argv).decode("utf-8"))
Example #2
0
 def load(self, page):
     """
     Loads the given page, does some changes, and saves it.
     """
     try:
         # Load the page
         text = page.get()
     except pywikibot.NoPage:
         if self.create:
             pywikibot.output(u"Page %s doesn't exist yet; creating."
                              % (page.title(asLink=True)))
             text = ''
         else:
             pywikibot.output(u"Page %s does not exist; skipping."
                              % page.title(asLink=True))
     except pywikibot.IsRedirectPage:
         redirTarget = page.getRedirectTarget()
         if self.follow_redirects:
             text = redirTarget.get()
         else:
             pywikibot.warning(u"Page %s is a redirect to %s; skipping."
                               % (page.title(asLink=True),
                                  redirTarget.title(asLink=True)))
     else:
         return text
Example #3
0
 def load(self, page):
     """
     Loads the given page, does some changes, and saves it.
     """
     try:
         # Load the page
         text = page.get()
     except pywikibot.NoPage:
         if self.create:
             pywikibot.output(u"Page %s doesn't exist yet; creating." %
                              (page.title(asLink=True)))
             text = ''
         else:
             pywikibot.output(u"Page %s does not exist; skipping." %
                              page.title(asLink=True))
     except pywikibot.IsRedirectPage:
         redirTarget = page.getRedirectTarget()
         if self.follow_redirects:
             text = redirTarget.get()
         else:
             pywikibot.warning(
                 u"Page %s is a redirect to %s; skipping." %
                 (page.title(asLink=True), redirTarget.title(asLink=True)))
     else:
         return text
Example #4
0
def main():
    pywikibot.warning('this script should not be run manually/directly, but automatically by maintainer.py')
    if len(sys.argv) == 1:
        pywikibot.output("Usage: censure.py <article title>")
        sys.exit(1)
    del sys.argv[0]
    checkPage(' '.join(sys.argv).decode('utf-8'))
Example #5
0
def getOpenStreetMap(latitude, longitude):
    '''
    Get the result from http://nominatim.openstreetmap.org/reverse
    and put it in a list of tuples to play around with
    '''
    result = []
    gotInfo = False
    parameters = urllib.urlencode({'lat' : latitude, 'lon' : longitude, 'accept-language' : 'en'})
    while(not gotInfo):
	try:
	    page = urllib.urlopen("http://nominatim.openstreetmap.org/reverse?format=xml&%s" % parameters)
	    et = xml.etree.ElementTree.parse(page)
	    gotInfo=True
	except IOError:
	    pywikibot.output(u'Got an IOError, let\'s try again')
	    time.sleep(30)
	except socket.timeout:
	    pywikibot.output(u'Got a timeout, let\'s try again')
	    time.sleep(30)
    validParts = [u'hamlet', u'village', u'city', u'county', u'country']
    invalidParts = [u'path', u'road', u'suburb', u'state', u'country_code']
    addressparts = et.find('addressparts')
    #xml.etree.ElementTree.dump(et)

    for addresspart in addressparts.getchildren():
	if addresspart.tag in validParts:
	    result.append(addresspart.text)
	elif addresspart.tag in invalidParts:
	    pywikibot.output(u'Dropping %s, %s' % (addresspart.tag, addresspart.text))
	else:
	    pywikibot.warning(u'%s, %s is not in addressparts lists' % (addresspart.tag, addresspart.text))
    #print result
    return result
 def do_check(self, page_title, params=None):
     # Create two threads as follows
     # (simple 'thread' for more sophisticated code use 'threading')
     pywikibot.output(u"CHECK: %s" % page_title)
     try:
         thread.start_new_thread( main_subster, (self.refs[page_title], params) )
     except:
         pywikibot.warning(u"unable to start thread")
Example #7
0
    def __init__(self):
        '''Constructor of SubsterBot(), initialize needed vars.'''

        pywikibot.output(
            u'\03{lightgreen}* Initialization of bot:\03{default}')

        basic.AutoBasicBot.__init__(self)

        # modification of timezone to be in sync with wiki
        os.environ['TZ'] = 'Europe/Amsterdam'
        if hasattr(time, "tzset"):
            time.tzset()
            pywikibot.output(u'Setting process TimeZone (TZ): %s' %
                             str(time.tzname))  # ('CET', 'CEST')
        else:
            # e.g. windows doesn't have that attribute
            pywikibot.warning(
                u'This operating system has NO SUPPORT for setting TimeZone by '
                u'code! Before running this script, please set the TimeZone '
                u'manually to one approriate for use with the Wikipedia '
                u'language and region you intend to.')

        # init constants
        self._bot_config = bot_config
        # convert e.g. namespaces to corret language
        self._bot_config['TemplateName'] = pywikibot.Page(
            self.site, self._bot_config['TemplateName']).title()
        self._template_regex = re.compile(
            '\{\{' + self._bot_config['TemplateName'] + '(.*?)\}\}', re.S)
        # TODO: implement proper error handling template/output for wikidata
        #       see: https://bugzilla.wikimedia.org/show_bug.cgi?id=60225
        #       see: https://www.wikidata.org/wiki/Template:Exchange_Rate_Data
        #if self.site.is_data_repository():
        #    self._bot_config['VerboseMessage'] = self._bot_config['data_VerboseMessage']

        # init constants
        self._userListPage = pywikibot.Page(self.site,
                                            self._bot_config['TemplateName'])
        self._ConfCSSpostprocPage = pywikibot.Page(
            self.site, self._bot_config['ConfCSSpostproc'])
        self._ConfCSSconfigPage = pywikibot.Page(
            self.site, self._bot_config['ConfCSSconfig'])
        self.pagegen = pagegenerators.ReferringPageGenerator(
            self._userListPage, onlyTemplateInclusion=True)
        self._code = self._ConfCSSpostprocPage.get()
        pywikibot.output(
            u'Imported postproc %s rev %s from %s' %
            ((self._ConfCSSpostprocPage.title(asLink=True), ) +
             self._ConfCSSpostprocPage.getVersionHistory(revCount=1)[0][:2]))
        self._flagenable = {}
        if self._ConfCSSconfigPage.exists():
            exec(self._ConfCSSconfigPage.get()
                 )  # with variable: bot_config_wiki
            self._flagenable = bot_config_wiki['flagenable']
            pywikibot.output(
                u'Imported config %s rev %s from %s' %
                ((self._ConfCSSconfigPage.title(asLink=True), ) +
                 self._ConfCSSconfigPage.getVersionHistory(revCount=1)[0][:2]))
Example #8
0
 def do_check(self, page_title, params=None):
     # Create two threads as follows
     # (simple 'thread' for more sophisticated code use 'threading')
     pywikibot.output(u"CHECK: %s" % page_title)
     try:
         thread.start_new_thread(main_subster,
                                 (self.refs[page_title], params))
     except:
         pywikibot.warning(u"unable to start thread")
Example #9
0
    def parse(self):
        """Return a generator that will yield XmlEntry objects"""
        print 'Reading XML dump...'
        if not 'iterparse' in globals():
            pywikibot.warning(
                u'''cElementTree not found. Using slower fallback solution.
Consider installing the python-celementtree package.''')
            return self.regex_parse()
        else:
            return self.new_parse()
Example #10
0
    def parse(self):
        """Return a generator that will yield XmlEntry objects"""
        print 'Reading XML dump...'
        if 'iterparse' not in globals():
            pywikibot.warning(
u'''cElementTree not found. Using slower fallback solution.
Consider installing the python-celementtree package.''')
            return self.regex_parse()
        else:
            return self.new_parse()
Example #11
0
    def __init__(self):
        '''Constructor of SubsterBot(), initialize needed vars.'''

        pywikibot.output(u'\03{lightgreen}* Initialization of bot:\03{default}')

        basic.AutoBasicBot.__init__(self)

        # modification of timezone to be in sync with wiki
        os.environ['TZ'] = 'Europe/Amsterdam'
        if hasattr(time, "tzset"):
            time.tzset()
            pywikibot.output(u'Setting process TimeZone (TZ): %s'
                             % str(time.tzname))    # ('CET', 'CEST')
        else:
            # e.g. windows doesn't have that attribute
            pywikibot.warning(
                u'This operating system has NO SUPPORT for setting TimeZone by '
                u'code! Before running this script, please set the TimeZone '
                u'manually to one approriate for use with the Wikipedia '
                u'language and region you intend to.')

        # init constants
        self._bot_config = bot_config
        # convert e.g. namespaces to corret language
        self._bot_config['TemplateName'] = pywikibot.Page(
            self.site, self._bot_config['TemplateName']).title()
        self._template_regex = re.compile(
            '\{\{' + self._bot_config['TemplateName'] + '(.*?)\}\}', re.S)
        # TODO: implement proper error handling template/output for wikidata
        #       see: https://bugzilla.wikimedia.org/show_bug.cgi?id=60225
        #       see: https://www.wikidata.org/wiki/Template:Exchange_Rate_Data
        #if self.site.is_data_repository():
        #    self._bot_config['VerboseMessage'] = self._bot_config['data_VerboseMessage']

        # init constants
        self._userListPage = pywikibot.Page(self.site,
                                            self._bot_config['TemplateName'])
        self._ConfCSSpostprocPage = pywikibot.Page(
            self.site, self._bot_config['ConfCSSpostproc'])
        self._ConfCSSconfigPage = pywikibot.Page(
            self.site, self._bot_config['ConfCSSconfig'])
        self.pagegen = pagegenerators.ReferringPageGenerator(
            self._userListPage, onlyTemplateInclusion=True)
        self._code = self._ConfCSSpostprocPage.get()
        pywikibot.output(u'Imported postproc %s rev %s from %s'
                         % ((self._ConfCSSpostprocPage.title(asLink=True), )
                            + self._ConfCSSpostprocPage.getVersionHistory(revCount=1)[0][:2]))
        self._flagenable = {}
        if self._ConfCSSconfigPage.exists():
            exec(self._ConfCSSconfigPage.get())  # with variable: bot_config_wiki
            self._flagenable = bot_config_wiki['flagenable']
            pywikibot.output(u'Imported config %s rev %s from %s'
                             % ((self._ConfCSSconfigPage.title(asLink=True), )
                                + self._ConfCSSconfigPage.getVersionHistory(revCount=1)[0][:2]))
def main():

    pywikibot.warning(u"This script will set preferences on all " u"configured accounts!")
    pywikibot.output(
        u"You have %s accounts configured." % sum([len(family) for family in config.usernames.itervalues()])
    )

    if pywikibot.inputChoice(u"Do you wish to continue?", ["no", "yes"], ["n", "y"], "n") == "n":
        return

    if (
        pywikibot.inputChoice(
            u"Do you already know which preference you wish " u"to set?", ["no", "yes"], ["n", "y"], "y"
        )
        == "n"
    ):
        site = pywikibot.getSite()
        pywikibot.output(u"Getting list of available preferences from %s." % site)
        prefs = Preferences(site)

        pywikibot.output(u"-" * 73)
        pywikibot.output(u"| Name                | Value                    |")
        pywikibot.output(u"-" * 73)
        pref_data = prefs.items()
        pref_data.sort()
        for key, value in pref_data:
            pywikibot.output(table_cell(key, 4) + table_cell(value, 5) + "|")
        pywikibot.output(u"-" * 73)
    pywikibot.output(u"")
    pywikibot.output(u"(For checkboxes: An empty string evaluates to False; " u"all others to True)")
    pywikibot.output(u"")

    while True:
        keys, values = [], []
        while True:
            try:
                keys.append(pywikibot.input(u"Which preference do you wish to set?"))
            except KeyboardInterrupt:
                return
            values.append(pywikibot.input(u"To what value do you wish to set '%s'?" % keys[-1]))
            if pywikibot.inputChoice(u"Set more preferences?", ["no", "yes"], ["n", "y"], "n") == "n":
                break

        if (
            pywikibot.inputChoice(
                u"Set %s?" % u", ".join(u"%s:%s" % (key, value) for key, value in zip(keys, values)),
                ["yes", "no"],
                ["y", "n"],
                "n",
            )
            == "y"
        ):
            set_all(keys, values, verbose=True)
            pywikibot.output(u"Preferences have been set on all wikis.")
Example #13
0
def main():

    pywikibot.warning(u'This script will set preferences on all '
                      u'configured accounts!')
    pywikibot.output(
        u'You have %s accounts configured.' %
        sum([len(family) for family in config.usernames.itervalues()]))

    if pywikibot.inputChoice(u'Do you wish to continue?', ['no', 'yes'],
                             ['n', 'y'], 'n') == 'n':
        return

    if pywikibot.inputChoice(
            u'Do you already know which preference you wish '
            u'to set?', ['no', 'yes'], ['n', 'y'], 'y') == 'n':
        site = pywikibot.getSite()
        pywikibot.output(u'Getting list of available preferences from %s.' %
                         site)
        prefs = Preferences(site)

        pywikibot.output(u'-' * 73)
        pywikibot.output(u'| Name                | Value                    |')
        pywikibot.output(u'-' * 73)
        pref_data = prefs.items()
        pref_data.sort()
        for key, value in pref_data:
            pywikibot.output(table_cell(key, 4) + table_cell(value, 5) + '|')
        pywikibot.output(u'-' * 73)
    pywikibot.output(u'')
    pywikibot.output(u'(For checkboxes: An empty string evaluates to False; '
                     u'all others to True)')
    pywikibot.output(u'')

    while True:
        keys, values = [], []
        while True:
            try:
                keys.append(
                    pywikibot.input(u'Which preference do you wish to set?'))
            except KeyboardInterrupt:
                return
            values.append(
                pywikibot.input(u"To what value do you wish to set '%s'?" %
                                keys[-1]))
            if pywikibot.inputChoice(u"Set more preferences?", ['no', 'yes'],
                                     ['n', 'y'], 'n') == 'n':
                break

        if pywikibot.inputChoice(
                u"Set %s?" % u', '.join(u'%s:%s' % (key, value)
                                        for key, value in zip(keys, values)),
            ['yes', 'no'], ['y', 'n'], 'n') == 'y':
            set_all(keys, values, verbose=True)
            pywikibot.output(u"Preferences have been set on all wikis.")
Example #14
0
def main():

    pywikibot.warning(u'This script will set preferences on all '
                     u'configured accounts!')
    pywikibot.output(u'You have %s accounts configured.'
                     % sum([len(family)
                            for family in config.usernames.itervalues()]))

    if pywikibot.inputChoice(u'Do you wish to continue?',
                             ['no', 'yes'], ['n', 'y'], 'n') == 'n':
        return

    if pywikibot.inputChoice(u'Do you already know which preference you wish '
                             u'to set?', ['no', 'yes'], ['n', 'y'], 'y') == 'n':
        site = pywikibot.getSite()
        pywikibot.output(u'Getting list of available preferences from %s.'
                         % site)
        prefs = Preferences(site)

        pywikibot.output(u'-' * 73)
        pywikibot.output(u'| Name                | Value                    |')
        pywikibot.output(u'-' * 73)
        pref_data = prefs.items()
        pref_data.sort()
        for key, value in pref_data:
            pywikibot.output(table_cell(key, 4) + table_cell(value, 5) + '|')
        pywikibot.output(u'-' * 73)
    pywikibot.output(u'')
    pywikibot.output(u'(For checkboxes: An empty string evaluates to False; '
                     u'all others to True)')
    pywikibot.output(u'')

    while True:
        keys, values = [], []
        while True:
            try:
                keys.append(pywikibot.input(
                    u'Which preference do you wish to set?'))
            except KeyboardInterrupt:
                return
            values.append(pywikibot.input(
                u"To what value do you wish to set '%s'?" % keys[-1]))
            if pywikibot.inputChoice(u"Set more preferences?",
                                     ['no', 'yes'], ['n', 'y'], 'n') == 'n':
                break

        if pywikibot.inputChoice(
                u"Set %s?"
                % u', '.join(u'%s:%s' % (key, value)
                             for key, value in zip(keys, values)),
                ['yes', 'no'], ['y', 'n'], 'n') == 'y':
            set_all(keys, values, verbose=True)
            pywikibot.output(u"Preferences have been set on all wikis.")
 def handleNextLink(self, page, text, match, context=100):
     """
     Returns a tuple (text, jumpToBeginning).
     text is the unicode string after the current link has been processed.
     jumpToBeginning is a boolean which specifies if the cursor position
     should be reset to 0. This is required after the user has edited the
     article.
     """
     # ignore interwiki links and links to sections of the same page as well
     # as section links
     if not match.group("title") or page.site().isInterwikiLink(match.group("title")) or match.group("section"):
         return text, False
     try:
         linkedPage = pywikibot.Page(page.site(), match.group("title"))
     except pywikibot.InvalidTitle, err:
         pywikibot.warning(u"%s" % err)
         return text, False
Example #16
0
 def fix_1_double_redirect(self,  redir_name):
     redir = pywikibot.Page(self.site, redir_name)
     # 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} <<<"
                      % redir.title())
     newRedir = redir
     redirList = []  # bookkeeping to detect loops
     while True:
         redirList.append(u'%s:%s' % (newRedir.site.lang,
                                      newRedir.sectionFreeTitle()))
         try:
             targetPage = newRedir.getRedirectTarget()
         except pywikibot.IsNotRedirectPage:
             if len(redirList) == 1:
                 pywikibot.output(u'Skipping: Page %s is not a redirect.'
                                  % redir.title(asLink=True))
                 break  # do nothing
             elif len(redirList) == 2:
                 pywikibot.output(
                     u'Skipping: Redirect target %s is not a redirect.'
                     % newRedir.title(asLink=True))
                 break  # do nothing
             else:
                 pass  # target found
         except pywikibot.SectionError:
             pywikibot.warning(
                 u"Redirect target section %s doesn't exist."
                 % newRedir.title(asLink=True))
         except pywikibot.BadTitle as e:
             # str(e) is in the format 'BadTitle: [[Foo]]'
             pywikibot.warning(
                 u'Redirect target %s is not a valid page title.'
                 % str(e)[10:])
         # sometimes this error occures. Invalid Title starting with a '#'
         except pywikibot.InvalidTitle, err:
             pywikibot.warning(u'%s' % err)
             break
         except pywikibot.NoPage:
             if len(redirList) == 1:
                 pywikibot.output(u'Skipping: Page %s does not exist.'
                                  % redir.title(asLink=True))
                 break
             else:
                 if self.always:
                     pywikibot.output(
                         u"Skipping: Redirect target %s doesn't exist."
                         % newRedir.title(asLink=True))
                     break  # skip if automatic
                 else:
                     pywikibot.warning(
                         u"Redirect target %s doesn't exist."
                         % newRedir.title(asLink=True))
Example #17
0
 def fix_1_double_redirect(self, redir_name):
     redir = pywikibot.Page(self.site, redir_name)
     # 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} <<<" %
                      redir.title())
     newRedir = redir
     redirList = []  # bookkeeping to detect loops
     while True:
         redirList.append(u'%s:%s' %
                          (newRedir.site.lang, newRedir.sectionFreeTitle()))
         try:
             targetPage = newRedir.getRedirectTarget()
         except pywikibot.IsNotRedirectPage:
             if len(redirList) == 1:
                 pywikibot.output(u'Skipping: Page %s is not a redirect.' %
                                  redir.title(asLink=True))
                 break  # do nothing
             elif len(redirList) == 2:
                 pywikibot.output(
                     u'Skipping: Redirect target %s is not a redirect.' %
                     newRedir.title(asLink=True))
                 break  # do nothing
             else:
                 pass  # target found
         except pywikibot.SectionError:
             pywikibot.warning(
                 u"Redirect target section %s doesn't exist." %
                 newRedir.title(asLink=True))
         except pywikibot.BadTitle as e:
             # str(e) is in the format 'BadTitle: [[Foo]]'
             pywikibot.warning(
                 u'Redirect target %s is not a valid page title.' %
                 str(e)[10:])
         # sometimes this error occures. Invalid Title starting with a '#'
         except pywikibot.InvalidTitle, err:
             pywikibot.warning(u'%s' % err)
             break
         except pywikibot.NoPage:
             if len(redirList) == 1:
                 pywikibot.output(u'Skipping: Page %s does not exist.' %
                                  redir.title(asLink=True))
                 break
             else:
                 if self.always:
                     pywikibot.output(
                         u"Skipping: Redirect target %s doesn't exist." %
                         newRedir.title(asLink=True))
                     break  # skip if automatic
                 else:
                     pywikibot.warning(
                         u"Redirect target %s doesn't exist." %
                         newRedir.title(asLink=True))
Example #18
0
 def handleNextLink(self, page, text, match, context=100):
     """
     Returns a tuple (text, jumpToBeginning).
     text is the unicode string after the current link has been processed.
     jumpToBeginning is a boolean which specifies if the cursor position
     should be reset to 0. This is required after the user has edited the
     article.
     """
     # ignore interwiki links and links to sections of the same page as well
     # as section links
     if not match.group('title') \
        or page.site().isInterwikiLink(match.group('title')) \
        or match.group('section'):
         return text, False
     try:
         linkedPage = pywikibot.Page(page.site(), match.group('title'))
     except pywikibot.InvalidTitle, err:
         pywikibot.warning(u'%s' % err)
         return text, False
Example #19
0
def set_all(keys, values, verbose=False):
    log = open('preferences.txt', 'a')
    log.write('PREFERENCES\t%s\n' % time.gmtime())
    log.write('KEYS\t%s\n' % keys)
    log.write('VALUES\t%s\n' % values)

    for family in config.usernames:
        for lang in config.usernames[family]:
            try:
                set_for(lang, family, keys, values, verbose)
            except (SystemExit, KeyboardInterrupt):
                return
            except:
                pywikibot.exception(tb=True)
                pywikibot.warning(u'An exception occured!')
                log.write('FAILED\t%s\t%s\n' % (family, lang))
            else:
                log.write('SUCCESS\t%s\t%s\n' % (family, lang))
    log.close()
Example #20
0
def set_all(keys, values, verbose=False):
    log = open('preferences.txt', 'a')
    log.write('PREFERENCES\t%s\n' % time.gmtime())
    log.write('KEYS\t%s\n' % keys)
    log.write('VALUES\t%s\n' % values)

    for family in config.usernames:
        for lang in config.usernames[family]:
            try:
                set_for(lang, family, keys, values, verbose)
            except (SystemExit, KeyboardInterrupt):
                return
            except:
                pywikibot.exception(tb=True)
                pywikibot.warning(u'An exception occured!')
                log.write('FAILED\t%s\t%s\n' % (family, lang))
            else:
                log.write('SUCCESS\t%s\t%s\n' % (family, lang))
    log.close()
Example #21
0
def getOpenStreetMap(latitude, longitude):
    '''
    Get the result from http://nominatim.openstreetmap.org/reverse
    and put it in a list of tuples to play around with
    '''
    result = []
    gotInfo = False
    parameters = urllib.urlencode({
        'lat': latitude,
        'lon': longitude,
        'accept-language': 'en'
    })
    while (not gotInfo):
        try:
            page = urllib.urlopen(
                "http://nominatim.openstreetmap.org/reverse?format=xml&%s" %
                parameters)
            et = xml.etree.ElementTree.parse(page)
            gotInfo = True
        except IOError:
            pywikibot.output(u'Got an IOError, let\'s try again')
            time.sleep(30)
        except socket.timeout:
            pywikibot.output(u'Got a timeout, let\'s try again')
            time.sleep(30)
    validParts = [u'hamlet', u'village', u'city', u'county', u'country']
    invalidParts = [u'path', u'road', u'suburb', u'state', u'country_code']
    addressparts = et.find('addressparts')
    #xml.etree.ElementTree.dump(et)

    for addresspart in addressparts.getchildren():
        if addresspart.tag in validParts:
            result.append(addresspart.text)
        elif addresspart.tag in invalidParts:
            pywikibot.output(u'Dropping %s, %s' %
                             (addresspart.tag, addresspart.text))
        else:
            pywikibot.warning(u'%s, %s is not in addressparts lists' %
                              (addresspart.tag, addresspart.text))
    #print result
    return result
Example #22
0
def main(args):
    pywikibot.warnnig(u'This is an experimental bot')
    pywikibot.warning(u'It will only work on self published work images')
    pywikibot.warning(u'This bot is still full of bugs')
    pywikibot.warning(u'Use at your own risk!')

    generator = None
    autonomous = False
    checkTemplate = True

    # Load a lot of default generators
    genFactory = pagegenerators.GeneratorFactory()

    for arg in pywikibot.handleArgs():
        if arg == '-nochecktemplate':
            checkTemplate = False
        elif arg == '-autonomous':
            autonomous = True
        else:
            genFactory.handleArg(arg)

    if not supportedSite():
        pywikibot.output(u'Sorry, this site is not supported (yet).')
        return False

    generator = genFactory.getCombinedGenerator()
    if not generator:
        raise add_text.NoEnoughData(
            'You have to specify the generator you want to use for the script!'
        )

    pregenerator = pagegenerators.PreloadingGenerator(generator)

    prefetchQueue = Queue(maxsize=50)
    uploadQueue = Queue(maxsize=200)

    imageFetcherThread = imageFetcher(pregenerator, prefetchQueue)
    userInteractionThread = userInteraction(prefetchQueue, uploadQueue)
    uploaderThread = uploader(uploadQueue)

    imageFetcherThread.daemon = False
    userInteractionThread.daemon = False
    uploaderThread.daemon = False

    if autonomous:
        pywikibot.output(
            u'Bot is running in autonomous mode. There will be no user interaction.'
        )
        userInteractionThread.setAutonomous()

    if not checkTemplate:
        pywikibot.output(
            u'No check template will be added to the uploaded files.')
        uploaderThread.nochecktemplate()

    fetchDone = imageFetcherThread.start()
    userDone = userInteractionThread.start()
    uploadDone = uploaderThread.start()
Example #23
0
def main(args):
    pywikibot.warnnig(u'This is an experimental bot')
    pywikibot.warning(u'It will only work on self published work images')
    pywikibot.warning(u'This bot is still full of bugs')
    pywikibot.warning(u'Use at your own risk!')

    generator = None
    autonomous = False
    checkTemplate = True

    # Load a lot of default generators
    genFactory = pagegenerators.GeneratorFactory()

    for arg in pywikibot.handleArgs():
        if arg == '-nochecktemplate':
            checkTemplate = False
        elif arg == '-autonomous':
            autonomous = True
        else:
            genFactory.handleArg(arg)

    if not supportedSite():
        pywikibot.output(u'Sorry, this site is not supported (yet).')
        return False

    generator = genFactory.getCombinedGenerator()
    if not generator:
        raise add_text.NoEnoughData(
            'You have to specify the generator you want to use for the script!')

    pregenerator = pagegenerators.PreloadingGenerator(generator)

    prefetchQueue = Queue(maxsize=50)
    uploadQueue = Queue(maxsize=200)

    imageFetcherThread = imageFetcher(pregenerator, prefetchQueue)
    userInteractionThread = userInteraction(prefetchQueue, uploadQueue)
    uploaderThread = uploader(uploadQueue)

    imageFetcherThread.daemon = False
    userInteractionThread.daemon = False
    uploaderThread.daemon = False

    if autonomous:
        pywikibot.output(u'Bot is running in autonomous mode. There will be no '
                         u'user interaction.')
        userInteractionThread.setAutonomous()

    if not checkTemplate:
        pywikibot.output(u'No check template will be added to the uploaded '
                         u'files.')
        uploaderThread.nochecktemplate()

    fetchDone = imageFetcherThread.start()
    userDone = userInteractionThread.start()
    uploadDone = uploaderThread.start()
Example #24
0
                    globalvar.queryLimit = int(arg[7:])
            elif arg.startswith('-numberlog'):
                if len(arg) == 10:
                    globalvar.dumpToLog = int(pywikibot.input(u'After how many welcomed users would you like to update the welcome log?'))
                else:
                    globalvar.dumpToLog = int(arg[11:])
            elif arg == '-quiet':
                globalvar.quiet = True
            elif arg == '-quick':
                globalvar.quick = True

        # Filename and pywikipedia path
        # file where is stored the random signature index
        filename = pywikibot.config.datafilepath('welcome-%s-%s.data' % (pywikibot.default_family, pywikibot.default_code))
        if globalvar.offset and globalvar.timeoffset:
            pywikibot.warning('both -offset and -timeoffset were provided, ignoring -offset')
            globalvar.offset = 0
        bot = WelcomeBot()
        try:
            bot.run()
        except KeyboardInterrupt:
            if bot.welcomed_users:
                showStatus()
                pywikibot.output("Put welcomed users before quit...")
                bot.makelogpage(bot.welcomed_users)
            pywikibot.output("\nQuitting...")
    finally:
        # If there is the savedata, the script must save the number_user.
        if globalvar.randomSign and globalvar.saveSignIndex and bot.welcomed_users:
            import cPickle
            f = file(filename, 'w')
Example #25
0
    def read_file_content(self):
        """Return name of temp file in which remote file is saved."""
        if not self._retrieved or self.uploadByUrl:
            # Get file contents
            pywikibot.output(u'Reading file %s' % self.url)
            if '://' in self.url:
                resume = False
                dt = 15

                while not self._retrieved:
                    uo = pywikibot.MyURLopener
                    headers = [('User-agent', pywikibot.useragent)]

                    if resume:
                        pywikibot.output(u"Resume download...")
                        headers.append(('Range', 'bytes=%s-' % rlen))
                    uo.addheaders = headers

                    file = uo.open(self.url)

                    if 'text/html' in file.info().getheader('Content-Type'):
                        print("Couldn't download the image: the requested URL "
                              "was not found on this server.")
                        return

                    content_len = file.info().getheader('Content-Length')
                    accept_ranges = file.info().getheader(
                        'Accept-Ranges') == 'bytes'

                    if resume:
                        self._contents += file.read()
                    else:
                        self._contents = file.read()

                    file.close()
                    self._retrieved = True

                    if content_len:
                        rlen = len(self._contents)
                        content_len = int(content_len)
                        if rlen < content_len:
                            self._retrieved = False
                            pywikibot.output(
                                u"Connection closed at byte %s (%s left)"
                                % (rlen, content_len))
                            if accept_ranges and rlen > 0:
                                resume = True
                            pywikibot.output(u"Sleeping for %d seconds..." % dt)
                            time.sleep(dt)
                            if dt <= 60:
                                dt += 15
                            elif dt < 360:
                                dt += 60
                    else:
                        if pywikibot.verbose:
                            pywikibot.warning(u"No check length to retrieved "
                                              u"data is possible.")
            else:
                # Opening local files with MyURLopener would be possible, but we
                # don't do it because it only accepts ASCII characters in the
                # filename.
                file = open(self.url, "rb")
                self._contents = file.read()
                file.close()
Example #26
0
        self.site = site

    def run(self):
        for (page, date, length, loggedIn, username, comment) in \
                self.site.newpages(100, repeat=True):
            handler = PageHandler(page, date, length, loggedIn, username,
                                  comment)
            handler.run()

# Generate the question text
i = 0
questions = '\n'
questionlist = {}
for t in pywikibot.translate(pywikibot.getSite(), templates):
    i += 1
    questions += (u'%s) %s\n' % (i, t))
    questionlist[i] = t
question = questions + question

# MAIN
if __name__ == "__main__":
    try:
        for arg in pywikibot.handleArgs():
            pywikibot.warning(
                u'argument "%s" not understood; ignoring.' % arg)
        bot = CleaningBot()
        bot.run()
    except:
        pywikibot.stopme()
        raise
Example #27
0
             break  # skip if automatic
         else:
             pywikibot.warning(
                 u"Redirect target %s doesn't exist."
                 % newRedir.title(asLink=True))
 except pywikibot.ServerError:
     pywikibot.output(u'Skipping due to server error: '
                      u'No textarea found')
     break
 else:
     pywikibot.output(
         u'   Links to: %s.'
         % targetPage.title(asLink=True))
     if targetPage.site != self.site:
         pywikibot.warning(
             u'redirect target (%s) is on a different site.'
             % targetPage.title(asLink=True))
         if self.always:
             break  # skip if automatic
     mw_msg = targetPage.site.mediawiki_message(
         'wikieditor-toolbar-tool-redirect-example')
     if targetPage.title() == mw_msg:
         pywikibot.output(
             u"Skipping toolbar example: Redirect source is "
             u"potentially vandalized.")
         break
     # watch out for redirect loops
     if redirList.count(u'%s:%s'
                        % (targetPage.site.lang,
                           targetPage.sectionFreeTitle())):
         pywikibot.warning(
Example #28
0
def main():
    """ Main Function """
    # Loading the comments
    global categoryToCheck, project_inserted
    # always, define a generator to understand if the user sets one,
    # defining what's genFactory
    always = False
    generator = False
    show = False
    moveBlockCheck = False
    protectedpages = False
    protectType = 'edit'
    namespace = 0
    genFactory = pagegenerators.GeneratorFactory()
    # To prevent Infinite loops
    errorCount = 0
    # Loading the default options.
    for arg in pywikibot.handleArgs():
        if arg == '-always':
            always = True
        elif arg == '-move':
            moveBlockCheck = True
        elif arg == '-show':
            show = True
        elif arg.startswith('-protectedpages'):
            protectedpages = True
            if len(arg) > 15:
                namespace = int(arg[16:])
        elif arg.startswith('-moveprotected'):
            protectedpages = True
            protectType = 'move'
            if len(arg) > 14:
                namespace = int(arg[15:])
        else:
            genFactory.handleArg(arg)

    if config.mylang not in project_inserted:
        pywikibot.output(u"Your project is not supported by this script.\n"
                         u"You have to edit the script and add it!")
        return
    site = pywikibot.getSite()
    if protectedpages:
        generator = site.protectedpages(namespace=namespace, type=protectType)
    # Take the right templates to use, the category and the comment
    TSP = pywikibot.translate(site, templateSemiProtection)
    TTP = pywikibot.translate(site, templateTotalProtection)
    TSMP = pywikibot.translate(site, templateSemiMoveProtection)
    TTMP = pywikibot.translate(site, templateTotalMoveProtection)
    TNR = pywikibot.translate(site, templateNoRegex)
    TU = pywikibot.translate(site, templateUnique)

    category = pywikibot.translate(site, categoryToCheck)
    commentUsed = i18n.twtranslate(site, 'blockpageschecker-summary')
    if not generator:
        generator = genFactory.getCombinedGenerator()
    if not generator:
        generator = list()
        pywikibot.output(u'Loading categories...')
        # Define the category if no other generator has been setted
        for CAT in category:
            cat = catlib.Category(site, CAT)
            # Define the generator
            gen = pagegenerators.CategorizedPageGenerator(cat)
            for pageCat in gen:
                generator.append(pageCat)
        pywikibot.output(u'Categories loaded, start!')
    # Main Loop
    preloadingGen = pagegenerators.PreloadingGenerator(generator, pageNumber=60)
    for page in preloadingGen:
        pagename = page.title(asLink=True)
        pywikibot.output('Loading %s...' % pagename)
        try:
            text = page.get()
            restrictions = page.getRestrictions()
        except pywikibot.NoPage:
            pywikibot.output("%s doesn't exist! Skipping..." % pagename)
            continue
        except pywikibot.IsRedirectPage:
            pywikibot.output("%s is a redirect! Skipping..." % pagename)
            if show:
                showQuest(site, page)
            continue
        """
        # This check does not work :
        # PreloadingGenerator cannot set correctly page.editRestriction
        # (see bug #1949476 )
        if not page.canBeEdited():
            pywikibot.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
            continue
        """
        if 'edit' in restrictions.keys():
            editRestr = restrictions['edit']
        else:
            editRestr = None
        if editRestr and editRestr[0] == 'sysop':
            try:
                config.sysopnames[site.family.name][site.lang]
            except:
                pywikibot.output(u"%s is sysop-protected: "
                                 u"this account can't edit it! Skipping..."
                                 % pagename)
                continue

        # Understand, according to the template in the page, what should be the
        # protection and compare it with what there really is.
        TemplateInThePage = understandBlock(text, TTP, TSP, TSMP, TTMP, TU)
        # Only to see if the text is the same or not...
        oldtext = text
        # keep track of the changes for each step (edit then move)
        changes = -1

        if not editRestr:
            # page is not edit-protected
            # Deleting the template because the page doesn't need it.
            if TU:
                replaceToPerform = u'|'.join(TTP + TSP + TU)
            else:
                replaceToPerform = u'|'.join(TTP + TSP)
            text, changes = re.subn('<noinclude>(%s)</noinclude>'
                                    % replaceToPerform, '', text)
            if changes == 0:
                text, changes = re.subn('(%s)' % replaceToPerform, '', text)
            msg = u'The page is editable for all'
            if not moveBlockCheck:
                msg += u', deleting the template..'
            pywikibot.output(u'%s.' % msg)

        elif editRestr[0] == 'sysop':
            # total edit protection
            if (TemplateInThePage[0] == 'sysop-total' and TTP) or \
               (TemplateInThePage[0] == 'unique' and TU):
                msg = 'The page is protected to the sysop'
                if not moveBlockCheck:
                    msg += ', skipping...'
                pywikibot.output(msg)
            else:
                pywikibot.output(u'The page is protected to the sysop, but the '
                                 u'template seems not correct. Fixing...')
                if TU:
                    text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
                else:
                    text, changes = re.subn(TemplateInThePage[1], TNR[1], text)

        elif TSP or TU:
            # implicitely editRestr[0] = 'autoconfirmed', edit-Semi-protection
            if TemplateInThePage[0] == 'autoconfirmed-total' or \
               TemplateInThePage[0] == 'unique':
                msg = 'The page is editable only for the autoconfirmed users'
                if not moveBlockCheck:
                    msg += ', skipping...'
                pywikibot.output(msg)
            else:
                pywikibot.output(u'The page is editable only for the '
                                 u'autoconfirmed users, but the template '
                                 u'seems not correct. Fixing...')
                if TU:
                    text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
                else:
                    text, changes = re.subn(TemplateInThePage[1], TNR[0], text)

        if changes == 0:
            # We tried to fix edit-protection templates, but it did not work.
            pywikibot.warning('No edit-protection template could be found')

        if moveBlockCheck and changes > -1:
            # checking move protection now
            try:
                moveRestr = restrictions['move']
            except KeyError:
                moveRestr = False
            changes = -1

            if not moveRestr:
                pywikibot.output(u'The page is movable for all, deleting the '
                                 u'template...')
                # Deleting the template because the page doesn't need it.
                if TU:
                    replaceToPerform = u'|'.join(TSMP + TTMP + TU)
                else:
                    replaceToPerform = u'|'.join(TSMP + TTMP)
                text, changes = re.subn('<noinclude>(%s)</noinclude>'
                                        % replaceToPerform, '', text)
                if changes == 0:
                    text, changes = re.subn('(%s)' % replaceToPerform, '', text)
            elif moveRestr[0] == 'sysop':
                # move-total-protection
                if (TemplateInThePage[0] == 'sysop-move' and TTMP) or \
                   (TemplateInThePage[0] == 'unique' and TU):
                    pywikibot.output(u'The page is protected from moving to '
                                     u'the sysop, skipping...')
                    if TU:
                        # no changes needed, better to revert the old text.
                        text = oldtext
                else:
                    pywikibot.output(u'The page is protected from moving to '
                                     u'the sysop, but the template seems not '
                                     u'correct. Fixing...')
                    if TU:
                        text, changes = re.subn(TemplateInThePage[1], TNR[4],
                                                text)
                    else:
                        text, changes = re.subn(TemplateInThePage[1], TNR[3],
                                                text)

            elif TSMP or TU:
                # implicitely moveRestr[0] = 'autoconfirmed',
                # move-semi-protection
                if TemplateInThePage[0] == 'autoconfirmed-move' or \
                   TemplateInThePage[0] == 'unique':
                    pywikibot.output(u'The page is movable only for the '
                                     u'autoconfirmed users, skipping...')
                    if TU:
                        # no changes needed, better to revert the old text.
                        text = oldtext
                else:
                    pywikibot.output(u'The page is movable only for the '
                                     u'autoconfirmed users, but the template '
                                     u'seems not correct. Fixing...')
                    if TU:
                        text, changes = re.subn(TemplateInThePage[1], TNR[4],
                                                text)
                    else:
                        text, changes = re.subn(TemplateInThePage[1], TNR[2],
                                                text)

            if changes == 0:
                # We tried to fix move-protection templates, but it did not work
                pywikibot.warning('No move-protection template could be found')

        if oldtext != text:
            # Ok, asking if the change has to be performed and do it if yes.
            pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                             % page.title())
            pywikibot.showDiff(oldtext, text)
            if not always:
                choice = pywikibot.inputChoice(u'Do you want to accept these '
                                               u'changes?',
                                               ['Yes', 'No', 'All'],
                                               ['y', 'N', 'a'], 'N')
                if choice == 'a':
                    always = True
            if always or choice == 'y':
                while 1:
                    try:
                        page.put(text, commentUsed, force=True)
                    except pywikibot.EditConflict:
                        pywikibot.output(u'Edit conflict! skip!')
                        break
                    except pywikibot.ServerError:
                        # Sometimes there is this error that's quite annoying
                        # because can block the whole process for nothing.
                        errorCount += 1
                        if errorCount < 5:
                            pywikibot.output(u'Server Error! Wait..')
                            time.sleep(3)
                            continue
                        else:
                            # Prevent Infinite Loops
                            raise pywikibot.ServerError(u'Fifth Server Error!')
                    except pywikibot.SpamfilterError, e:
                        pywikibot.output(u'Cannot change %s because of '
                                         u'blacklist entry %s'
                                         % (page.title(), e.url))
                        break
                    except pywikibot.PageNotSaved, error:
                        pywikibot.output(u'Error putting page: %s'
                                         % (error.args,))
                        break
                    except pywikibot.LockedPage:
                        pywikibot.output(u'The page is still protected. '
                                         u'Skipping...')
                        break
                    else:
                        # Break only if the errors are one after the other
                        errorCount = 0
                        break
Example #29
0
        sys.path.append(os.path.split(sys.argv[0])[0])
        execfile(sys.argv[0])

        exitcode = ERROR_SGE_ok
        pywikibot.output(u'')
        pywikibot.output(u'DONE')
    except:
        pywikibot.exception(tb=True)
        error = traceback.format_exc()
        if pywikibot.logger.isEnabledFor(pywikibot.DEBUG):
            exitcode = ERROR_SGE_ok  # print traceback of re-raised errors by skipping sys.exit()
            raise
        else:
            send_mailnotification(error, u'Bot ERROR')
        pywikibot.output(u'')
        pywikibot.warning(u'DONE with Exception(s) occured in Bot')
    finally:
        site = pywikibot.getSite()
        name = str('%s-%s-%s' % (bot_name, site.family.name, site.lang))
        d = shelve.open(pywikibot.config.datafilepath('cache', 'state_bots'))
        d[name] = {
            'error': str(bool(error)),
            'traceback': str(error.encode('utf-8')),
            'timestamp': str(pywikibot.Timestamp.now().isoformat(' ')),
        }
        d.close()

        pywikibot.stopme()
        (sys.stdout, sys.stderr) = (sys.__stdout__, sys.__stderr__)

        # use exitcode to control SGE (restart or stop with sending mail)
Example #30
0
class RedirectRobot:
    def __init__(self, action, generator, always=False, number=None):
        self.site = pywikibot.getSite()
        self.action = action
        self.generator = generator
        self.always = always
        self.number = number
        self.exiting = False

    def prompt(self, question):
        if not self.always:
            choice = pywikibot.inputChoice(question,
                                           ['Yes', 'No', 'All', 'Quit'],
                                           ['y', 'N', 'a', 'q'], 'N')
            if choice == 'n':
                return False
            elif choice == 'q':
                self.exiting = True
                return False
            elif choice == 'a':
                self.always = True
        return True

    def delete_broken_redirects(self):
        # get reason for deletion text
        reason = i18n.twtranslate(self.site, 'redirect-remove-broken')
        for redir_name in self.generator.retrieve_broken_redirects():
            self.delete_1_broken_redirect(redir_name, reason)
            if self.exiting:
                break

    def delete_1_broken_redirect(self, redir_name, reason):
        redir_page = pywikibot.Page(self.site, redir_name)
        # 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} <<<" %
                         redir_page.title())
        try:
            targetPage = redir_page.getRedirectTarget()
        except pywikibot.IsNotRedirectPage:
            pywikibot.output(u'%s is not a redirect.' % redir_page.title())
        except pywikibot.NoPage:
            pywikibot.output(u'%s doesn\'t exist.' % redir_page.title())
        else:
            try:
                targetPage.get()
            except pywikibot.NoPage:
                if self.prompt(u'Redirect target %s does not exist. '
                               u'Do you want to delete %s?' %
                               (targetPage.title(asLink=True),
                                redir_page.title(asLink=True))):
                    try:
                        redir_page.delete(reason, prompt=False)
                    except pywikibot.NoUsername:
                        if (i18n.twhas_key(
                            targetPage.site.lang,
                            'redirect-broken-redirect-template') and
                            i18n.twhas_key(targetPage.site.lang,
                                           'redirect-remove-broken')) or \
                                           targetPage.site.lang == '-':
                            pywikibot.output(u"No sysop in user-config.py, "
                                             u"put page to speedy deletion.")
                            content = redir_page.get(get_redirect=True)
                            ### TODO: Add bot's signature if needed
                            ###       Not supported via TW yet
                            content = i18n.twtranslate(
                                targetPage.site.lang,
                                'redirect-broken-redirect-template'
                            ) + "\n" + content
                            redir_page.put(content, reason)
            except pywikibot.IsRedirectPage:
                pywikibot.output(u"Redirect target %s is also a redirect! "
                                 u"Won't delete anything." %
                                 targetPage.title(asLink=True))
            else:
                #we successfully get the target page, meaning that
                #it exists and is not a redirect: no reason to touch it.
                pywikibot.output(
                    u'Redirect target %s does exist! Won\'t delete anything.' %
                    targetPage.title(asLink=True))
        pywikibot.output(u'')

    def fix_double_redirects(self):
        for redir_name in self.generator.retrieve_double_redirects():
            self.fix_1_double_redirect(redir_name)
            if self.exiting:
                break

    def fix_1_double_redirect(self, redir_name):
        redir = pywikibot.Page(self.site, redir_name)
        # 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} <<<" %
                         redir.title())
        newRedir = redir
        redirList = []  # bookkeeping to detect loops
        while True:
            redirList.append(u'%s:%s' %
                             (newRedir.site.lang, newRedir.sectionFreeTitle()))
            try:
                targetPage = newRedir.getRedirectTarget()
            except pywikibot.IsNotRedirectPage:
                if len(redirList) == 1:
                    pywikibot.output(u'Skipping: Page %s is not a redirect.' %
                                     redir.title(asLink=True))
                    break  # do nothing
                elif len(redirList) == 2:
                    pywikibot.output(
                        u'Skipping: Redirect target %s is not a redirect.' %
                        newRedir.title(asLink=True))
                    break  # do nothing
                else:
                    pass  # target found
            except pywikibot.SectionError:
                pywikibot.warning(
                    u"Redirect target section %s doesn't exist." %
                    newRedir.title(asLink=True))
            except pywikibot.BadTitle, e:
                # str(e) is in the format 'BadTitle: [[Foo]]'
                pywikibot.warning(
                    u'Redirect target %s is not a valid page title.' %
                    str(e)[10:])
            # sometimes this error occures. Invalid Title starting with a '#'
            except pywikibot.InvalidTitle, err:
                pywikibot.warning(u'%s' % err)
                break
            except pywikibot.NoPage:
                if len(redirList) == 1:
                    pywikibot.output(u'Skipping: Page %s does not exist.' %
                                     redir.title(asLink=True))
                    break
                else:
                    if self.always:
                        pywikibot.output(
                            u"Skipping: Redirect target %s doesn't exist." %
                            newRedir.title(asLink=True))
                        break  # skip if automatic
                    else:
                        pywikibot.warning(
                            u"Redirect target %s doesn't exist." %
                            newRedir.title(asLink=True))
Example #31
0
def main():
    password = None
    sysop = False
    logall = False
    forceLogin = False
    verbose = False
    clean = False
    testonly = False

    for arg in pywikibot.handleArgs():
        if arg.startswith("-pass"):
            if len(arg) == 5:
                password = pywikibot.input(u'Password for all accounts '
                                           u'(no characters will be shown):',
                                           password=True)
            else:
                password = arg[6:]
        elif arg == "-clean":
            clean = True
        elif arg == "-sysop":
            sysop = True
        elif arg == "-all":
            logall = True
        elif arg == "-force":
            forceLogin = True
        elif arg == "-test":
            testonly = True
        else:
            pywikibot.showHelp('login')
            return

    if pywikibot.verbose > 1:
        pywikibot.warning(u"""
Using -v -v on login.py might leak private data. When sharing, please double
check your password is not readable and log out your bots session.""")
        verbose = True  # only use this verbose when running from login.py
    if logall:
        if sysop:
            namedict = config.sysopnames
        else:
            namedict = config.usernames

        for familyName in namedict.iterkeys():
            for lang in namedict[familyName].iterkeys():
                if testonly:
                    show(pywikibot.getSite(lang, familyName), sysop)
                else:
                    try:
                        site = pywikibot.getSite(lang, familyName)
                        loginMan = LoginManager(password, sysop=sysop,
                                                site=site, verbose=verbose)
                        if clean:
                            loginMan.logout()
                        else:
                            if not forceLogin and site.loggedInAs(sysop=sysop):
                                pywikibot.output(u'Already logged in on %s'
                                                 % site)
                            else:
                                loginMan.login()
                    except pywikibot.NoSuchSite:
                        pywikibot.output(lang + u'.' + familyName +
                                         u' is not a valid site, please remove '
                                         u'it from your config')

    elif testonly:
        show(pywikibot.getSite(), sysop)
    elif clean:
        try:
            site = pywikibot.getSite()
            lgm = LoginManager(site=site)
            lgm.logout()
        except pywikibot.NoSuchSite:
            pass
    else:
        loginMan = LoginManager(password, sysop=sysop, verbose=verbose)
        loginMan.login()
Example #32
0
def lowlevel_warning(text):
    if has_logger():
        pywikibot.warning(text)
    else:
        print "WARNING:", text
Example #33
0
def main():
    gen = None
    prefix = None
    oldName = None
    newName = None
    noredirect = False
    always = False
    skipredirects = False
    summary = None
    fromToPairs = []

    # 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.startswith('-pairs'):
            if len(arg) == len('-pairs'):
                filename = pywikibot.input(
                    u'Enter the name of the file containing pairs:')
            else:
                filename = arg[len('-pairs:'):]
            oldName1 = None
            for page in pagegenerators.TextfilePageGenerator(filename):
                if oldName1:
                    fromToPairs.append([oldName1, page.title()])
                    oldName1 = None
                else:
                    oldName1 = page.title()
            if oldName1:
                pywikibot.warning(
                    u'file %s contains odd number of links' % filename)
        elif arg == '-noredirect':
            noredirect = True
        elif arg == '-always':
            always = True
        elif arg == '-skipredirects':
            skipredirects = True
        elif arg.startswith('-from:'):
            if oldName:
                pywikibot.warning(u'-from:%s without -to:' % oldName)
            oldName = arg[len('-from:'):]
        elif arg.startswith('-to:'):
            if oldName:
                fromToPairs.append([oldName, arg[len('-to:'):]])
                oldName = None
            else:
                pywikibot.warning(u'%s without -from' % arg)
        elif arg.startswith('-prefix'):
            if len(arg) == len('-prefix'):
                prefix = pywikibot.input(u'Enter the prefix:')
            else:
                prefix = arg[8:]
        elif arg.startswith('-summary'):
            if len(arg) == len('-summary'):
                summary = pywikibot.input(u'Enter the summary:')
            else:
                summary = arg[9:]
        else:
            genFactory.handleArg(arg)

    if oldName:
        pywikibot.warning(u'-from:%s without -to:' % oldName)
    for pair in fromToPairs:
        page = pywikibot.Page(pywikibot.getSite(), pair[0])
        bot = MovePagesBot(None, prefix, noredirect, always, skipredirects,
                           summary)
        bot.moveOne(page, pair[1])

    if not gen:
        gen = genFactory.getCombinedGenerator()
    if gen:
        preloadingGen = pagegenerators.PreloadingGenerator(gen)
        bot = MovePagesBot(preloadingGen, prefix, noredirect, always,
                           skipredirects, summary)
        bot.run()
    elif not fromToPairs:
        pywikibot.showHelp()
Example #34
0
def main():
    password = None
    sysop = False
    logall = False
    forceLogin = False
    verbose = False
    clean = False
    testonly = False

    for arg in pywikibot.handleArgs():
        if arg.startswith("-pass"):
            if len(arg) == 5:
                password = pywikibot.input(
                    u'Password for all accounts '
                    u'(no characters will be shown):',
                    password=True)
            else:
                password = arg[6:]
        elif arg == "-clean":
            clean = True
        elif arg == "-sysop":
            sysop = True
        elif arg == "-all":
            logall = True
        elif arg == "-force":
            forceLogin = True
        elif arg == "-test":
            testonly = True
        else:
            pywikibot.showHelp('login')
            return

    if pywikibot.verbose > 1:
        pywikibot.warning(u"""
Using -v -v on login.py might leak private data. When sharing, please double
check your password is not readable and log out your bots session.""")
        verbose = True  # only use this verbose when running from login.py
    if logall:
        if sysop:
            namedict = config.sysopnames
        else:
            namedict = config.usernames

        for familyName in namedict.iterkeys():
            for lang in namedict[familyName].iterkeys():
                if testonly:
                    show(pywikibot.getSite(lang, familyName), sysop)
                else:
                    try:
                        site = pywikibot.getSite(lang, familyName)
                        loginMan = LoginManager(password,
                                                sysop=sysop,
                                                site=site,
                                                verbose=verbose)
                        if clean:
                            loginMan.logout()
                        else:
                            if not forceLogin and site.loggedInAs(sysop=sysop):
                                pywikibot.output(u'Already logged in on %s' %
                                                 site)
                            else:
                                loginMan.login()
                    except pywikibot.NoSuchSite:
                        pywikibot.output(
                            lang + u'.' + familyName +
                            u' is not a valid site, please remove '
                            u'it from your config')

    elif testonly:
        show(pywikibot.getSite(), sysop)
    elif clean:
        try:
            site = pywikibot.getSite()
            lgm = LoginManager(site=site)
            lgm.logout()
        except pywikibot.NoSuchSite:
            pass
    else:
        loginMan = LoginManager(password, sysop=sysop, verbose=verbose)
        loginMan.login()
Example #35
0
def main():
    pywikibot.warning('this script can not be run manually/directly, but '
                      'automatically by maintainer.py')
Example #36
0
def main():
    start = '0'
    force = False
    msg = {'en':'Creating state abbreviation redirect',
           'ar':u'إنشاء تحويلة اختصار الولاية',
           'fa':u'ایجاد تغییرمسیر برای نام اختصاری ایالت',
           'he':u'יוצר הפניה מראשי התיבות של המדינה',
           }

    abbrev = {
        'Alabama': 'AL',
        'Alaska': 'AK',
        'Arizona': 'AZ',
        'Arkansas': 'AR',
        'California': 'CA',
        'Colorado': 'CO',
        'Delaware': 'DE',
        'Florida': 'FL',
        'Georgia': 'GA',
        'Hawaii': 'HI',
        'Idaho': 'ID',
        'Illinois': 'IL',
        'Indiana': 'IN',
        'Iowa': 'IA',
        'Kansas': 'KS',
        'Kentucky': 'KY',
        'Louisiana': 'LA',
        'Maine': 'ME',
        'Maryland': 'MD',
        'Massachusetts': 'MA',
        'Michigan': 'MI',
        'Minnesota': 'MN',
        'Mississippi': 'MS',
        'Missouri': 'MO',
        'Montana': 'MT',
        'North Carolina': 'NC',
        'North Dakota': 'ND',
        'Nebraska': 'NE',
        'Nevada': 'NV',
        'New Hampshire': 'NH',
        'New Jersey': 'NJ',
        'New Mexico': 'NM',
        'New York': 'NY',
        'Ohio': 'OH',
        'Oklahoma': 'OK',
        'Oregon': 'OR',
        'Pennsylvania': 'PA',
        'Rhode Island': 'RI',
        'South Carolina': 'SC',
        'South Dakota': 'SD',
        'Tennessee': 'TN',
        'Texas': 'TX',
        'Utah': 'UT',
        'Vermont': 'VT',
        'Virginia': 'VA',
        'Washington': 'WA',
        'West Virginia': 'WV',
        'Wisconsin': 'WI',
        'Wyoming': 'WY'
    }

    for arg in pywikibot.handleArgs():
        if arg.startswith('-start:'):
            start = arg[7:]
        elif arg == '-force':
            force = True
        else:
            pywikibot.warning(
                u'argument "%s" not understood; ignoring.' % arg)

    mysite = pywikibot.getSite()
    for p in mysite.allpages(start = start):
        for sn in abbrev:
            R=re.compile('[^[]]*' + '\%2C_' + sn)
            for res in R.findall(p.title()):
                pl=pywikibot.Page(mysite, p.title().replace(sn,abbrev[sn]))
                # A bit hacking here - the real work is done in the
                # 'except pywikibot.NoPage' part rather than the 'try'.
                try:
                    goal = pl.getRedirectTarget().title()
                    if pywikibot.Page(mysite, goal):
                        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. Please check page!"
                        % goal)
                except pywikibot.NoPage:
                    change=''
                    if p.isRedirectPage():
                        p2 = p.getRedirectTarget()
                        wikipeda.ouput(
                            u'Note: goal page is redirect. Creating redirect to "%s" to avoid double redirect.'
                            % p2.title().replace("%2C",",").replace("_"," "))
                    else:
                        p2 = p
                    if force:
                        change='y'
                    else:
                        while not change in ['y','n']:
                            pywikibot.output(
                                u"Create redirect %s" %
                                pl.title().replace("%2C",",").replace("_"," "))
                            change = raw_input("(y/n)? ")
                    if change=='y':
                        text = '#REDIRECT [['+p2.title().replace("%2C",",").replace("_"," ")+']]'
                        pl.put(text, comment=pywikibot.translate(mysite, msg),
                               minorEdit = '0')
Example #37
0
                 newRedir.title(asLink=True))
             break  # skip if automatic
         else:
             pywikibot.warning(
                 u"Redirect target %s doesn't exist." %
                 newRedir.title(asLink=True))
 except pywikibot.ServerError:
     pywikibot.output(u'Skipping due to server error: '
                      u'No textarea found')
     break
 else:
     pywikibot.output(u'   Links to: %s.' %
                      targetPage.title(asLink=True))
     if targetPage.site != self.site:
         pywikibot.warning(
             u'redirect target (%s) is on a different site.' %
             targetPage.title(asLink=True))
         if self.always:
             break  # skip if automatic
     try:
         mw_msg = targetPage.site.mediawiki_message(
             'wikieditor-toolbar-tool-redirect-example')
     except KeyError:
         pass
     else:
         if targetPage.title() == mw_msg:
             pywikibot.output(
                 u"Skipping toolbar example: Redirect source is "
                 u"potentially vandalized.")
             break
     # watch out for redirect loops
Example #38
0
        sys.path.append(os.path.split(sys.argv[0])[0])
        execfile(sys.argv[0])

        exitcode = ERROR_SGE_ok
        pywikibot.output(u"")
        pywikibot.output(u"DONE")
    except:
        pywikibot.exception(tb=True)
        error = traceback.format_exc()
        if pywikibot.logger.isEnabledFor(pywikibot.DEBUG):
            exitcode = ERROR_SGE_ok  # print traceback of re-raised errors by skipping sys.exit()
            raise
        else:
            send_mailnotification(error, u"Bot ERROR")
        pywikibot.output(u"")
        pywikibot.warning(u"DONE with Exception(s) occured in Bot")
    finally:
        site = pywikibot.getSite()
        name = str("%s-%s-%s" % (bot_name, site.family.name, site.lang))
        d = shelve.open(pywikibot.config.datafilepath("cache", "state_bots"))
        d[name] = {
            "error": str(bool(error)),
            "traceback": str(error.encode("utf-8")),
            "timestamp": str(pywikibot.Timestamp.now().isoformat(" ")),
        }
        d.close()

        pywikibot.stopme()
        (sys.stdout, sys.stderr) = (sys.__stdout__, sys.__stderr__)

        # use exitcode to control SGE (restart or stop with sending mail)
def main():
    pywikibot.warning('this script can not be run manually/directly, but automatically by maintainer.py')
Example #40
0
def main():
    gen = None
    prefix = None
    oldName = None
    newName = None
    noredirect = False
    always = False
    skipredirects = False
    summary = None
    fromToPairs = []

    # 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.startswith('-pairs'):
            if len(arg) == len('-pairs'):
                filename = pywikibot.input(
                    u'Enter the name of the file containing pairs:')
            else:
                filename = arg[len('-pairs:'):]
            oldName1 = None
            for page in pagegenerators.TextfilePageGenerator(filename):
                if oldName1:
                    fromToPairs.append([oldName1, page.title()])
                    oldName1 = None
                else:
                    oldName1 = page.title()
            if oldName1:
                pywikibot.warning(u'file %s contains odd number of links' %
                                  filename)
        elif arg == '-noredirect':
            noredirect = True
        elif arg == '-always':
            always = True
        elif arg == '-skipredirects':
            skipredirects = True
        elif arg.startswith('-from:'):
            if oldName:
                pywikibot.warning(u'-from:%s without -to:' % oldName)
            oldName = arg[len('-from:'):]
        elif arg.startswith('-to:'):
            if oldName:
                fromToPairs.append([oldName, arg[len('-to:'):]])
                oldName = None
            else:
                pywikibot.warning(u'%s without -from' % arg)
        elif arg.startswith('-prefix'):
            if len(arg) == len('-prefix'):
                prefix = pywikibot.input(u'Enter the prefix:')
            else:
                prefix = arg[8:]
        elif arg.startswith('-summary'):
            if len(arg) == len('-summary'):
                summary = pywikibot.input(u'Enter the summary:')
            else:
                summary = arg[9:]
        else:
            genFactory.handleArg(arg)

    if oldName:
        pywikibot.warning(u'-from:%s without -to:' % oldName)
    for pair in fromToPairs:
        page = pywikibot.Page(pywikibot.getSite(), pair[0])
        bot = MovePagesBot(None, prefix, noredirect, always, skipredirects,
                           summary)
        bot.moveOne(page, pair[1])

    if not gen:
        gen = genFactory.getCombinedGenerator()
    if gen:
        preloadingGen = pagegenerators.PreloadingGenerator(gen)
        bot = MovePagesBot(preloadingGen, prefix, noredirect, always,
                           skipredirects, summary)
        bot.run()
    elif not fromToPairs:
        pywikibot.showHelp()
Example #41
0
    def badNameFilter(self, name, force = False):
        if not globalvar.filtBadName:
            return False

        #initialize blacklist
        if not hasattr(self, '_blacklist') or force:
            elenco = [
                ' ano', ' anus', 'anal ', 'babies', 'baldracca', 'balle', 'bastardo',
                'bestiali', 'bestiale', 'bastarda', 'b.i.t.c.h.', 'bitch', 'boobie',
                'bordello', 'breast', 'cacata', 'cacca', 'cachapera', 'cagata',
                'cane', 'cazz', 'cazzo', 'cazzata', 'chiavare', 'chiavata', 'chick',
                'christ ', 'cristo', 'clitoride', 'coione', 'cojdioonear', 'cojones',
                'cojo', 'coglione', 'coglioni', 'cornuto', 'cula', 'culatone',
                'culattone', 'culo', 'deficiente', 'deficente', 'dio', 'die ',
                'died ', 'ditalino', 'ejackulate', 'enculer', 'eroticunt', 'fanculo',
                'f******o', 'fica ', 'ficken', 'figa', 'sfiga', 'fottere', 'fotter',
                'fottuto', 'f**k', 'f.u.c.k.', "funkyass",
                'gay', 'hentai.com', 'horne', 'horney', 'virgin', 'hotties', 'idiot',
                '@alice.it', 'incest', 'jesus', 'gesu', 'gesù', 'kazzo', 'kill',
                'leccaculo', 'lesbian', 'lesbica', 'lesbo', 'masturbazione',
                'masturbare', 'masturbo', 'merda', 'merdata', 'merdoso', 'mignotta',
                'minchia', 'minkia', 'minchione', 'mona', 'nudo', 'nuda', 'nudi',
                'oral', 'sex', 'orgasmso', 'porc', 'pompa', 'pompino', 'porno',
                'puttana', 'puzza', 'puzzone', "racchia", 'sborone', 'sborrone',
                'sborata', 'sborolata', 'sboro', 'scopata', 'scopare', 'scroto',
                'scrotum', 'sega', 'sesso', 'shit', 'shiz', 's.h.i.t.', 'sadomaso',
                'sodomist', 'stronzata', 'stronzo', 'succhiamelo', 'succhiacazzi',
                'testicol', 'troia', 'universetoday.net', 'vaffanculo', 'v****a',
                'vibrator', "vacca", 'yiddiot', "zoccola",
            ]
            elenco_others = ['@', ".com", ".sex", ".org", ".uk", ".en", ".it", "admin",
                "administrator", "amministratore", '@yahoo.com', '@alice.com', "amministratrice",
                "burocrate", "checkuser", "developer", "http://", "jimbo", "mediawiki",
                "on wheals", "on wheal", "on wheel", "planante", "razinger", "sysop", "troll",
                "vandal", " v.f. ", "v. fighter", "vandal f.", "vandal fighter", 'wales jimmy',
                "wheels", "wales", "www.",
            ]

            #blacklist from wikipage
            badword_page = pywikibot.Page(self.site, pywikibot.translate(self.site, bad_pag) )
            list_loaded = list()
            if badword_page.exists():
                pywikibot.output(u'\nLoading the bad words list from %s...' % self.site )
                list_loaded = load_word_function(badword_page.get())
            else:
                showStatus(4)
                pywikibot.output(u'The bad word page doesn\'t exist!')
            self._blacklist = elenco + elenco_others + list_loaded
            del elenco, elenco_others, list_loaded

        if not hasattr(self, '_whitelist') or force:
            #initialize whitelist
            whitelist_default = ['emiliano']
            wtlpg = pywikibot.translate(self.site, whitelist_pg)
            list_white = list()
            if wtlpg:
                whitelist_page = pywikibot.Page(self.site, wtlpg)
                if whitelist_page.exists():
                    pywikibot.output(u'\nLoading the whitelist from %s...' % self.site )
                    list_white = load_word_function(whitelist_page.get())
                else:
                    showStatus(4)
                    pywikibot.output(u"The whitelist's page doesn't exist!")
            else:
                showStatus(4)
                pywikibot.warning(u"The whitelist hasn't been setted!")

            # Join the whitelist words.
            self._whitelist = list_white + whitelist_default
            del list_white, whitelist_default

        try:
            for wname in self._whitelist:
                if wname.lower() in str(name).lower():
                    name = name.lower().replace(wname.lower(), '')
                    for bname in self._blacklist:
                        self.bname[name] = bname
                        return bname.lower() in name.lower()
        except UnicodeEncodeError:
            pass
        try:
            for bname in self._blacklist:
                if bname.lower() in str(name).lower(): #bad name positive
                    self.bname[name] = bname
                    return True
        except UnicodeEncodeError:
            pass
        return False
Example #42
0
                            u'After how many welcomed users would you like to update the welcome log?'
                        ))
                else:
                    globalvar.dumpToLog = int(arg[11:])
            elif arg == '-quiet':
                globalvar.quiet = True
            elif arg == '-quick':
                globalvar.quick = True

        # Filename and pywikipedia path
        # file where is stored the random signature index
        filename = pywikibot.config.datafilepath(
            'welcome-%s-%s.data' %
            (pywikibot.default_family, pywikibot.default_code))
        if globalvar.offset and globalvar.timeoffset:
            pywikibot.warning(
                'both -offset and -timeoffset were provided, ignoring -offset')
            globalvar.offset = 0
        bot = WelcomeBot()
        try:
            bot.run()
        except KeyboardInterrupt:
            if bot.welcomed_users:
                showStatus()
                pywikibot.output("Put welcomed users before quit...")
                bot.makelogpage(bot.welcomed_users)
            pywikibot.output("\nQuitting...")
    finally:
        # If there is the savedata, the script must save the number_user.
        if globalvar.randomSign and globalvar.saveSignIndex and bot.welcomed_users:
            import cPickle
            f = file(filename, 'w')
Example #43
0
    def badNameFilter(self, name, force=False):
        if not globalvar.filtBadName:
            return False

        #initialize blacklist
        if not hasattr(self, '_blacklist') or force:
            elenco = [
                ' ano',
                ' anus',
                'anal ',
                'babies',
                'baldracca',
                'balle',
                'bastardo',
                'bestiali',
                'bestiale',
                'bastarda',
                'b.i.t.c.h.',
                'bitch',
                'boobie',
                'bordello',
                'breast',
                'cacata',
                'cacca',
                'cachapera',
                'cagata',
                'cane',
                'cazz',
                'cazzo',
                'cazzata',
                'chiavare',
                'chiavata',
                'chick',
                'christ ',
                'cristo',
                'clitoride',
                'coione',
                'cojdioonear',
                'cojones',
                'cojo',
                'coglione',
                'coglioni',
                'cornuto',
                'cula',
                'culatone',
                'culattone',
                'culo',
                'deficiente',
                'deficente',
                'dio',
                'die ',
                'died ',
                'ditalino',
                'ejackulate',
                'enculer',
                'eroticunt',
                'fanculo',
                'f******o',
                'fica ',
                'ficken',
                'figa',
                'sfiga',
                'fottere',
                'fotter',
                'fottuto',
                'f**k',
                'f.u.c.k.',
                "funkyass",
                'gay',
                'hentai.com',
                'horne',
                'horney',
                'virgin',
                'hotties',
                'idiot',
                '@alice.it',
                'incest',
                'jesus',
                'gesu',
                'gesù',
                'kazzo',
                'kill',
                'leccaculo',
                'lesbian',
                'lesbica',
                'lesbo',
                'masturbazione',
                'masturbare',
                'masturbo',
                'merda',
                'merdata',
                'merdoso',
                'mignotta',
                'minchia',
                'minkia',
                'minchione',
                'mona',
                'nudo',
                'nuda',
                'nudi',
                'oral',
                'sex',
                'orgasmso',
                'porc',
                'pompa',
                'pompino',
                'porno',
                'puttana',
                'puzza',
                'puzzone',
                "racchia",
                'sborone',
                'sborrone',
                'sborata',
                'sborolata',
                'sboro',
                'scopata',
                'scopare',
                'scroto',
                'scrotum',
                'sega',
                'sesso',
                'shit',
                'shiz',
                's.h.i.t.',
                'sadomaso',
                'sodomist',
                'stronzata',
                'stronzo',
                'succhiamelo',
                'succhiacazzi',
                'testicol',
                'troia',
                'universetoday.net',
                'vaffanculo',
                'v****a',
                'vibrator',
                "vacca",
                'yiddiot',
                "zoccola",
            ]
            elenco_others = [
                '@',
                ".com",
                ".sex",
                ".org",
                ".uk",
                ".en",
                ".it",
                "admin",
                "administrator",
                "amministratore",
                '@yahoo.com',
                '@alice.com',
                "amministratrice",
                "burocrate",
                "checkuser",
                "developer",
                "http://",
                "jimbo",
                "mediawiki",
                "on wheals",
                "on wheal",
                "on wheel",
                "planante",
                "razinger",
                "sysop",
                "troll",
                "vandal",
                " v.f. ",
                "v. fighter",
                "vandal f.",
                "vandal fighter",
                'wales jimmy',
                "wheels",
                "wales",
                "www.",
            ]

            #blacklist from wikipage
            badword_page = pywikibot.Page(
                self.site, pywikibot.translate(self.site, bad_pag))
            list_loaded = list()
            if badword_page.exists():
                pywikibot.output(u'\nLoading the bad words list from %s...' %
                                 self.site)
                list_loaded = load_word_function(badword_page.get())
            else:
                showStatus(4)
                pywikibot.output(u'The bad word page doesn\'t exist!')
            self._blacklist = elenco + elenco_others + list_loaded
            del elenco, elenco_others, list_loaded

        if not hasattr(self, '_whitelist') or force:
            #initialize whitelist
            whitelist_default = ['emiliano']
            wtlpg = pywikibot.translate(self.site, whitelist_pg)
            list_white = list()
            if wtlpg:
                whitelist_page = pywikibot.Page(self.site, wtlpg)
                if whitelist_page.exists():
                    pywikibot.output(u'\nLoading the whitelist from %s...' %
                                     self.site)
                    list_white = load_word_function(whitelist_page.get())
                else:
                    showStatus(4)
                    pywikibot.output(u"The whitelist's page doesn't exist!")
            else:
                showStatus(4)
                pywikibot.warning(u"The whitelist hasn't been setted!")

            # Join the whitelist words.
            self._whitelist = list_white + whitelist_default
            del list_white, whitelist_default

        try:
            for wname in self._whitelist:
                if wname.lower() in str(name).lower():
                    name = name.lower().replace(wname.lower(), '')
                    for bname in self._blacklist:
                        self.bname[name] = bname
                        return bname.lower() in name.lower()
        except UnicodeEncodeError:
            pass
        try:
            for bname in self._blacklist:
                if bname.lower() in str(name).lower():  #bad name positive
                    self.bname[name] = bname
                    return True
        except UnicodeEncodeError:
            pass
        return False
Example #44
0
def main():
    start = '0'
    force = False
    msg = {'en': 'Creating state abbreviation redirect',
           'ar': u'إنشاء تحويلة اختصار الولاية',
           'fa': u'ایجاد تغییرمسیر برای نام اختصاری ایالت',
           'he': u'יוצר הפניה מראשי התיבות של המדינה',
           }

    abbrev = {
        'Alabama': 'AL',
        'Alaska': 'AK',
        'Arizona': 'AZ',
        'Arkansas': 'AR',
        'California': 'CA',
        'Colorado': 'CO',
        'Delaware': 'DE',
        'Florida': 'FL',
        'Georgia': 'GA',
        'Hawaii': 'HI',
        'Idaho': 'ID',
        'Illinois': 'IL',
        'Indiana': 'IN',
        'Iowa': 'IA',
        'Kansas': 'KS',
        'Kentucky': 'KY',
        'Louisiana': 'LA',
        'Maine': 'ME',
        'Maryland': 'MD',
        'Massachusetts': 'MA',
        'Michigan': 'MI',
        'Minnesota': 'MN',
        'Mississippi': 'MS',
        'Missouri': 'MO',
        'Montana': 'MT',
        'North Carolina': 'NC',
        'North Dakota': 'ND',
        'Nebraska': 'NE',
        'Nevada': 'NV',
        'New Hampshire': 'NH',
        'New Jersey': 'NJ',
        'New Mexico': 'NM',
        'New York': 'NY',
        'Ohio': 'OH',
        'Oklahoma': 'OK',
        'Oregon': 'OR',
        'Pennsylvania': 'PA',
        'Rhode Island': 'RI',
        'South Carolina': 'SC',
        'South Dakota': 'SD',
        'Tennessee': 'TN',
        'Texas': 'TX',
        'Utah': 'UT',
        'Vermont': 'VT',
        'Virginia': 'VA',
        'Washington': 'WA',
        'West Virginia': 'WV',
        'Wisconsin': 'WI',
        'Wyoming': 'WY'
    }

    for arg in pywikibot.handleArgs():
        if arg.startswith('-start:'):
            start = arg[7:]
        elif arg == '-force':
            force = True
        else:
            pywikibot.warning(
                u'argument "%s" not understood; ignoring.' % arg)

    mysite = pywikibot.getSite()
    for p in mysite.allpages(start=start):
        for sn in abbrev:
            R = re.compile('[^[]]*' + '\%2C_' + sn)
            for res in R.findall(p.title()):
                pl = pywikibot.Page(mysite, p.title().replace(sn, abbrev[sn]))
                # A bit hacking here - the real work is done in the
                # 'except pywikibot.NoPage' part rather than the 'try'.
                try:
                    goal = pl.getRedirectTarget().title()
                    if pywikibot.Page(mysite, goal):
                        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. Please check page!"
                        % goal)
                except pywikibot.NoPage:
                    change = ''
                    if p.isRedirectPage():
                        p2 = p.getRedirectTarget()
                        pywikibot.ouput(
                            u'Note: goal page is redirect. Creating redirect '
                            u'to "%s" to avoid double redirect.'
                            % p2.title().replace("%2C", ",").replace("_", " "))
                    else:
                        p2 = p
                    if force:
                        change = 'y'
                    else:
                        while change not in ['y', 'n']:
                            pywikibot.output(
                                u"Create redirect %s" %
                                pl.title().replace("%2C",
                                                   ",").replace("_", " "))
                            change = raw_input("(y/n)? ")
                    if change == 'y':
                        text = '#REDIRECT [[%s]]' % p2.title().replace(
                            "%2C", ",").replace("_", " ")
                        pl.put(text, comment=pywikibot.translate(mysite, msg))
Example #45
0
        self.site = site

    def run(self):
        for (page, date, length, loggedIn, username, comment) in \
                self.site.newpages(100, repeat=True):
            handler = PageHandler(page, date, length, loggedIn, username,
                                  comment)
            handler.run()

# Generate the question text
i = 0
questions = '\n'
questionlist = {}
for t in pywikibot.translate(pywikibot.getSite(), templates):
    i += 1
    questions += (u'%s) %s\n' % (i, t))
    questionlist[i] = t
question = questions + question

# MAIN
if __name__ == "__main__":
    try:
        for arg in pywikibot.handleArgs():
            pywikibot.warning(
                u'argument "%s" not understood; ignoring.' % arg)
        bot = CleaningBot()
        bot.run()
    except:
        pywikibot.stopme()
        raise
def lowlevel_warning(text):
    if has_logger():
        pywikibot.warning(text)
    else:
        print "WARNING:", text
Example #47
0
def main():
    """ Main Function """
    # Loading the comments
    global categoryToCheck, project_inserted
    # always, define a generator to understand if the user sets one,
    # defining what's genFactory
    always = False
    generator = False
    show = False
    moveBlockCheck = False
    protectedpages = False
    protectType = 'edit'
    namespace = 0
    genFactory = pagegenerators.GeneratorFactory()
    # To prevent Infinite loops
    errorCount = 0
    # Loading the default options.
    for arg in pywikibot.handleArgs():
        if arg == '-always':
            always = True
        elif arg == '-move':
            moveBlockCheck = True
        elif arg == '-show':
            show = True
        elif arg.startswith('-protectedpages'):
            protectedpages = True
            if len(arg) > 15:
                namespace = int(arg[16:])
        elif arg.startswith('-moveprotected'):
            protectedpages = True
            protectType = 'move'
            if len(arg) > 14:
                namespace = int(arg[15:])
        else:
            genFactory.handleArg(arg)

    if config.mylang not in project_inserted:
        pywikibot.output(u"Your project is not supported by this script.\n"
                         u"You have to edit the script and add it!")
        return
    site = pywikibot.getSite()
    if protectedpages:
        generator = site.protectedpages(namespace=namespace, type=protectType)
    # Take the right templates to use, the category and the comment
    TSP = pywikibot.translate(site, templateSemiProtection)
    TTP = pywikibot.translate(site, templateTotalProtection)
    TSMP = pywikibot.translate(site, templateSemiMoveProtection)
    TTMP = pywikibot.translate(site, templateTotalMoveProtection)
    TNR = pywikibot.translate(site, templateNoRegex)
    TU = pywikibot.translate(site, templateUnique)

    category = pywikibot.translate(site, categoryToCheck)
    commentUsed = i18n.twtranslate(site, 'blockpageschecker-summary')
    if not generator:
        generator = genFactory.getCombinedGenerator()
    if not generator:
        generator = list()
        pywikibot.output(u'Loading categories...')
        # Define the category if no other generator has been setted
        for CAT in category:
            cat = catlib.Category(site, CAT)
            # Define the generator
            gen = pagegenerators.CategorizedPageGenerator(cat)
            for pageCat in gen:
                generator.append(pageCat)
        pywikibot.output(u'Categories loaded, start!')
    # Main Loop
    preloadingGen = pagegenerators.PreloadingGenerator(generator,
                                                       pageNumber=60)
    for page in preloadingGen:
        pagename = page.title(asLink=True)
        pywikibot.output('Loading %s...' % pagename)
        try:
            text = page.get()
            restrictions = page.getRestrictions()
        except pywikibot.NoPage:
            pywikibot.output("%s doesn't exist! Skipping..." % pagename)
            continue
        except pywikibot.IsRedirectPage:
            pywikibot.output("%s is a redirect! Skipping..." % pagename)
            if show:
                showQuest(site, page)
            continue
        """
        # This check does not work :
        # PreloadingGenerator cannot set correctly page.editRestriction
        # (see bug #1949476 )
        if not page.canBeEdited():
            pywikibot.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
            continue
        """
        if 'edit' in restrictions.keys():
            editRestr = restrictions['edit']
        else:
            editRestr = None
        if editRestr and editRestr[0] == 'sysop':
            try:
                config.sysopnames[site.family.name][site.lang]
            except:
                pywikibot.output(u"%s is sysop-protected: "
                                 u"this account can't edit it! Skipping..." %
                                 pagename)
                continue

        # Understand, according to the template in the page, what should be the
        # protection and compare it with what there really is.
        TemplateInThePage = understandBlock(text, TTP, TSP, TSMP, TTMP, TU)
        # Only to see if the text is the same or not...
        oldtext = text
        # keep track of the changes for each step (edit then move)
        changes = -1

        if not editRestr:
            # page is not edit-protected
            # Deleting the template because the page doesn't need it.
            if TU:
                replaceToPerform = u'|'.join(TTP + TSP + TU)
            else:
                replaceToPerform = u'|'.join(TTP + TSP)
            text, changes = re.subn(
                '<noinclude>(%s)</noinclude>' % replaceToPerform, '', text)
            if changes == 0:
                text, changes = re.subn('(%s)' % replaceToPerform, '', text)
            msg = u'The page is editable for all'
            if not moveBlockCheck:
                msg += u', deleting the template..'
            pywikibot.output(u'%s.' % msg)

        elif editRestr[0] == 'sysop':
            # total edit protection
            if (TemplateInThePage[0] == 'sysop-total' and TTP) or \
               (TemplateInThePage[0] == 'unique' and TU):
                msg = 'The page is protected to the sysop'
                if not moveBlockCheck:
                    msg += ', skipping...'
                pywikibot.output(msg)
            else:
                pywikibot.output(
                    u'The page is protected to the sysop, but the '
                    u'template seems not correct. Fixing...')
                if TU:
                    text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
                else:
                    text, changes = re.subn(TemplateInThePage[1], TNR[1], text)

        elif TSP or TU:
            # implicitely editRestr[0] = 'autoconfirmed', edit-Semi-protection
            if TemplateInThePage[0] == 'autoconfirmed-total' or \
               TemplateInThePage[0] == 'unique':
                msg = 'The page is editable only for the autoconfirmed users'
                if not moveBlockCheck:
                    msg += ', skipping...'
                pywikibot.output(msg)
            else:
                pywikibot.output(u'The page is editable only for the '
                                 u'autoconfirmed users, but the template '
                                 u'seems not correct. Fixing...')
                if TU:
                    text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
                else:
                    text, changes = re.subn(TemplateInThePage[1], TNR[0], text)

        if changes == 0:
            # We tried to fix edit-protection templates, but it did not work.
            pywikibot.warning('No edit-protection template could be found')

        if moveBlockCheck and changes > -1:
            # checking move protection now
            try:
                moveRestr = restrictions['move']
            except KeyError:
                moveRestr = False
            changes = -1

            if not moveRestr:
                pywikibot.output(u'The page is movable for all, deleting the '
                                 u'template...')
                # Deleting the template because the page doesn't need it.
                if TU:
                    replaceToPerform = u'|'.join(TSMP + TTMP + TU)
                else:
                    replaceToPerform = u'|'.join(TSMP + TTMP)
                text, changes = re.subn(
                    '<noinclude>(%s)</noinclude>' % replaceToPerform, '', text)
                if changes == 0:
                    text, changes = re.subn('(%s)' % replaceToPerform, '',
                                            text)
            elif moveRestr[0] == 'sysop':
                # move-total-protection
                if (TemplateInThePage[0] == 'sysop-move' and TTMP) or \
                   (TemplateInThePage[0] == 'unique' and TU):
                    pywikibot.output(u'The page is protected from moving to '
                                     u'the sysop, skipping...')
                    if TU:
                        # no changes needed, better to revert the old text.
                        text = oldtext
                else:
                    pywikibot.output(u'The page is protected from moving to '
                                     u'the sysop, but the template seems not '
                                     u'correct. Fixing...')
                    if TU:
                        text, changes = re.subn(TemplateInThePage[1], TNR[4],
                                                text)
                    else:
                        text, changes = re.subn(TemplateInThePage[1], TNR[3],
                                                text)

            elif TSMP or TU:
                # implicitely moveRestr[0] = 'autoconfirmed',
                # move-semi-protection
                if TemplateInThePage[0] == 'autoconfirmed-move' or \
                   TemplateInThePage[0] == 'unique':
                    pywikibot.output(u'The page is movable only for the '
                                     u'autoconfirmed users, skipping...')
                    if TU:
                        # no changes needed, better to revert the old text.
                        text = oldtext
                else:
                    pywikibot.output(u'The page is movable only for the '
                                     u'autoconfirmed users, but the template '
                                     u'seems not correct. Fixing...')
                    if TU:
                        text, changes = re.subn(TemplateInThePage[1], TNR[4],
                                                text)
                    else:
                        text, changes = re.subn(TemplateInThePage[1], TNR[2],
                                                text)

            if changes == 0:
                # We tried to fix move-protection templates, but it did not work
                pywikibot.warning('No move-protection template could be found')

        if oldtext != text:
            # Ok, asking if the change has to be performed and do it if yes.
            pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" %
                             page.title())
            pywikibot.showDiff(oldtext, text)
            if not always:
                choice = pywikibot.inputChoice(
                    u'Do you want to accept these '
                    u'changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                if choice == 'a':
                    always = True
            if always or choice == 'y':
                while 1:
                    try:
                        page.put(text, commentUsed, force=True)
                    except pywikibot.EditConflict:
                        pywikibot.output(u'Edit conflict! skip!')
                        break
                    except pywikibot.ServerError:
                        # Sometimes there is this error that's quite annoying
                        # because can block the whole process for nothing.
                        errorCount += 1
                        if errorCount < 5:
                            pywikibot.output(u'Server Error! Wait..')
                            time.sleep(3)
                            continue
                        else:
                            # Prevent Infinite Loops
                            raise pywikibot.ServerError(u'Fifth Server Error!')
                    except pywikibot.SpamfilterError, e:
                        pywikibot.output(u'Cannot change %s because of '
                                         u'blacklist entry %s' %
                                         (page.title(), e.url))
                        break
                    except pywikibot.PageNotSaved, error:
                        pywikibot.output(u'Error putting page: %s' %
                                         (error.args, ))
                        break
                    except pywikibot.LockedPage:
                        pywikibot.output(u'The page is still protected. '
                                         u'Skipping...')
                        break
                    else:
                        # Break only if the errors are one after the other
                        errorCount = 0
                        break