def treat(self):
     page = pywikibot.Page(self.site, self.your_page)
     if page.exists():
         pywikibot.output(
             u"\nWikitable on \03{lightpurple}%s\03{default} will be " u"completed with:\n" % self.your_page
         )
         text = page.get()
         newtext = self.newraw()
         pywikibot.output(newtext)
         choice = pywikibot.inputChoice(u"Do you want to add these on wikitable?", ["Yes", "No"], ["y", "N"], "N")
         text = text[:-3] + newtext
         summ = pywikibot.translate(self.site, summary_update)
         if choice == "y":
             try:
                 page.put(u"".join(text), summ)
             except:
                 pywikibot.output(u"Impossible to edit. It may be an " u"edit conflict. Skipping...")
     else:
         pywikibot.output(
             u"\nWikitable on \03{lightpurple}%s\03{default} will be " u"created with:\n" % self.your_page
         )
         newtext = self.newtable() + self.newraw()
         pywikibot.output(newtext)
         summ = pywikibot.translate(self.site, summary_creation)
         choice = pywikibot.inputChoice(u"Do you want to accept this page creation?", ["Yes", "No"], ["y", "N"], "N")
         if choice == "y":
             try:
                 page.put(newtext, summ)
             except pywikibot.LockedPage:
                 pywikibot.output(u"Page %s is locked; skipping." % title)
             except pywikibot.EditConflict:
                 pywikibot.output(u"Skipping %s because of edit conflict" % title)
             except pywikibot.SpamfilterError, error:
                 pywikibot.output(u"Cannot change %s because of spam " u"blacklist entry %s" % (title, error.url))
Esempio n. 2
0
    def process_filename(self):
        """Return base filename portion of self.url"""
        # Isolate the pure name
        filename = self.url
        # Filename may be either a local file path or a URL
        if "://" in filename:
            # extract the path portion of the URL
            filename = urlparse.urlparse(filename).path
        filename = os.path.basename(filename)

        if self.useFilename:
            filename = self.useFilename
        if not self.keepFilename:
            pywikibot.output(
                u"The filename on the target wiki will default to: %s"
                % filename)
            # FIXME: these 2 belong somewhere else, presumably in family
            forbidden = '/' # to be extended
            allowed_formats = (u'gif', u'jpg', u'jpeg', u'mid', u'midi',
                               u'ogg', u'png', u'svg', u'xcf', u'djvu')
            # ask until it's valid
            while True:
                newfn = pywikibot.input(
                            u'Enter a better name, or press enter to accept:')
                if newfn == "":
                    newfn = filename
                    break
                ext = os.path.splitext(newfn)[1].lower().strip('.')
                # are any chars in forbidden also in newfn?
                invalid = set(forbidden) & set(newfn)
                if invalid:
                    c = "".join(invalid)
                    print "Invalid character(s): %s. Please try again" % c
                    continue
                if ext not in allowed_formats:
                    choice = pywikibot.inputChoice(
                        u"File format is not one of [%s], but %s. Continue?"
                         % (u' '.join(allowed_formats), ext),
                            ['yes', 'no'], ['y', 'N'], 'N')
                    if choice == 'n':
                        continue
                break
            if newfn != '':
                filename = newfn
        # A proper description for the submission.
        pywikibot.output(u"The suggested description is:")
        pywikibot.output(self.description)
        if self.verifyDescription:
            newDescription = u''
            choice = pywikibot.inputChoice(
                u'Do you want to change this description?',
                ['Yes', 'No'], ['y', 'N'], 'n')
            if choice == 'y':
                import editarticle
                editor = editarticle.TextEditor()
                newDescription = editor.edit(self.description)
            # if user saved / didn't press Cancel
            if newDescription:
                self.description = newDescription
        return filename
	def creerPage(self, text, page, comment, minorEdit=True, botflag=True):
		# only save if something was changed
		if text != None:
			arretdurgence()
			# Show the title of the page we're working on.
			# Highlight the title in purple.
			pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
			# show what was changed
			#pywikibot.showDiff(page.get(), text)
			pywikibot.output(u'%s' % text)
			pywikibot.output(u'Comment: %s' %comment)
			if not self.dry:
				#choice = pywikibot.inputChoice(u'Êtes-vous sûr de vouloir créer la page ?', ['Yes', 'No'], ['y', 'N'], 'N')
				choice = 'y'
				if choice == 'y':
					try:
						arretdurgence()
						if arret == "Oui":
							pywikibot.inputChoice(u"Demande d'arrêt d'urgence",['vu'],['v'],'')
						# Save the page
						page.put(text, comment=comment,
								 minorEdit=minorEdit, botflag=botflag)
					except pywikibot.LockedPage:
						pywikibot.output(u"Page %s is locked; skipping."
										 % page.title(asLink=True))
					except pywikibot.EditConflict:
						pywikibot.output(
							u'Skipping %s because of edit conflict'
							% (page.title()))
					except pywikibot.SpamfilterError, error:
						pywikibot.output(
u'Cannot change %s because of spam blacklist entry %s'
							% (page.title(), error.url))
					else:
						return True
def arretdurgence():
    global arret
    arretpage = pywikibot.Page(pywikibot.Site('fr', 'wikipedia'),
                               u"Discussion utilisateur:ZéroBot")
    arret = arretpage.get()
    if arret != u"{{/Stop}}":
        pywikibot.inputChoice(u"Demande d'arrêt d'urgence", ['vu'], ['v'], '')
Esempio n. 5
0
    def useHashGenerator(self):
        # https://toolserver.org/~multichill/nowcommons.php?language=it&page=2&filter=
        lang = self.site.lang
        num_page = 0
        word_to_skip_translated = i18n.translate(self.site, word_to_skip)
        images_processed = list()
        while 1:
            url = ('https://toolserver.org/~multichill/nowcommons.php?'
                   'language=%s&page=%s&filter=') % (lang, num_page)
            HTML_text = self.site.getUrl(url, no_hostname=True)
            reg = r'<[Aa] href="(?P<urllocal>.*?)">(?P<imagelocal>.*?)</[Aa]> +?</td><td>\n\s*?'
            reg += r'<[Aa] href="(?P<urlcommons>http[s]?://commons.wikimedia.org/.*?)" \
                   >Image:(?P<imagecommons>.*?)</[Aa]> +?</td><td>'

            regex = re.compile(reg, re.UNICODE)
            found_something = False
            change_page = True
            for x in regex.finditer(HTML_text):
                found_something = True
                image_local = x.group('imagelocal')
                image_commons = x.group('imagecommons')
                if image_local in images_processed:
                    continue
                change_page = False
                images_processed.append(image_local)
                # Skip images that have something in the title (useful for it.wiki)
                image_to_skip = False
                for word in word_to_skip_translated:
                    if word.lower() in image_local.lower():
                        image_to_skip = True
                if image_to_skip:
                    continue
                url_local = x.group('urllocal')
                url_commons = x.group('urlcommons')
                pywikibot.output(
                    u"\n\n>>> \03{lightpurple}%s\03{default} <<<" %
                    image_local)
                pywikibot.output(u'Local: %s\nCommons: %s\n' %
                                 (url_local, url_commons))
                result1 = webbrowser.open(url_local, 0, 1)
                result2 = webbrowser.open(url_commons, 0, 1)
                if image_local.split('Image:')[1] == image_commons:
                    choice = pywikibot.inputChoice(
                        u'The local and the commons images have the same name, continue?',
                        ['Yes', 'No'], ['y', 'N'], 'N')
                else:
                    choice = pywikibot.inputChoice(
                        u'Are the two images equal?', ['Yes', 'No'],
                        ['y', 'N'], 'N')
                if choice == 'y':
                    yield [image_local, image_commons]
                else:
                    continue
            # The page is dinamically updated, so we may don't need to change it
            if change_page:
                num_page += 1
            # If no image found means that there aren't anymore, break.
            if not found_something:
                break
Esempio n. 6
0
 def useHashGenerator(self):
     # http://toolserver.org/~multichill/nowcommons.php?language=it&page=2&filter=
     lang = self.site.lang
     num_page = 0
     word_to_skip_translated = i18n.translate(self.site, word_to_skip)
     images_processed = list()
     while 1:
         url = ('http://toolserver.org/~multichill/nowcommons.php?'
                'language=%s&page=%s&filter=') % (lang, num_page)
         HTML_text = self.site.getUrl(url, no_hostname=True)
         reg = r'<[Aa] href="(?P<urllocal>.*?)">(?P<imagelocal>.*?)</[Aa]> +?</td><td>\n\s*?'
         reg += r'<[Aa] href="(?P<urlcommons>http://commons.wikimedia.org/.*?)" \
                >Image:(?P<imagecommons>.*?)</[Aa]> +?</td><td>'
         regex = re.compile(reg, re.UNICODE)
         found_something = False
         change_page = True
         for x in regex.finditer(HTML_text):
             found_something = True
             image_local = x.group('imagelocal')
             image_commons = x.group('imagecommons')
             if image_local in images_processed:
                 continue
             change_page = False
             images_processed.append(image_local)
             # Skip images that have something in the title (useful for it.wiki)
             image_to_skip = False
             for word in word_to_skip_translated:
                 if word.lower() in image_local.lower():
                     image_to_skip = True
             if image_to_skip:
                 continue
             url_local = x.group('urllocal')
             url_commons = x.group('urlcommons')
             pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                              % image_local)
             pywikibot.output(u'Local: %s\nCommons: %s\n'
                              % (url_local, url_commons))
             result1 = webbrowser.open(url_local, 0, 1)
             result2 = webbrowser.open(url_commons, 0, 1)
             if image_local.split('Image:')[1] == image_commons:
                 choice = pywikibot.inputChoice(
                     u'The local and the commons images have the same name, continue?',
                     ['Yes', 'No'], ['y', 'N'], 'N')
             else:
                 choice = pywikibot.inputChoice(
                     u'Are the two images equal?',
                     ['Yes', 'No'], ['y', 'N'], 'N')
             if choice.lower() in ['y', 'yes']:
                 yield [image_local, image_commons]
             else:
                 continue
         # The page is dinamically updated, so we may don't need to change it
         if change_page:
             num_page += 1
         # If no image found means that there aren't anymore, break.
         if not found_something:
             break
Esempio n. 7
0
def arretdurgence():
    """
	Fonction d'arrêt d'urgence : recupère le contenu de la page
	de discussion du bot et arrête le script en demandant une confirmation
	lorsque la page contient autre chose que le modèle {{/Stop}}
	(i.e. lorsqu'un message a été laissé sur la page de discussion)
	"""
    global arret
    arretpage = pywikibot.Page(pywikibot.Site('fr', 'wikipedia'),
                               u"Discussion utilisateur:ZéroBot")
    arret = arretpage.get()
    if arret != u"{{/Stop}}":
        pywikibot.inputChoice(
            u"\03{lightred}Demande d'arrêt d'urgence\03{default}", ['vu'],
            ['v'], '')
Esempio n. 8
0
    def save(self, text, page, newcatTitle, minorEdit=True, botflag=True):
        # only save if something was changed
        if text != page.get():
            # show what was changed
            pywikibot.showDiff(page.get(), text)
            comment = self.editSummary
            if not comment:
                comment = i18n.twtranslate(page.site, 'category-adding',
                                           {'newcat': newcatTitle})
            pywikibot.output(u'Comment: %s' % comment)
            if not self.dry:
                if not self.always:
                    confirm = 'y'
                    while True:
                        choice = pywikibot.inputChoice(
                            u'Do you want to accept these changes?',
                            ['Yes', 'No', 'Always'], ['y', 'N', 'a'], 'N')
                        if choice == 'a':
                            confirm = pywikibot.inputChoice(
                                u"""\
This should be used if and only if you are sure that your links are correct!
Are you sure?""", ['Yes', 'No'], ['y', 'n'], 'n')
                            if confirm == 'y':
                                self.always = True
                                break
                        else:
                            break
                if self.always or choice == 'y':
                    try:
                        # Save the page
                        page.put(text,
                                 comment=comment,
                                 minorEdit=minorEdit,
                                 botflag=botflag)
                    except pywikibot.LockedPage:
                        pywikibot.output(u"Page %s is locked; skipping." %
                                         page.title(asLink=True))
                    except pywikibot.EditConflict:
                        pywikibot.output(
                            u'Skipping %s because of edit conflict' %
                            (page.title()))
                    except pywikibot.SpamfilterError as error:
                        pywikibot.output(
                            u'Cannot change %s because of spam blacklist entry '
                            u'%s' % (page.title(), error.url))
                    else:
                        return True
        return False
Esempio n. 9
0
    def reportBadAccount(self, name=None, final=False):
        #Queue process
        if name:
            if globalvar.confirm:
                answer = pywikibot.inputChoice(
                    u'%s may have an unwanted username, do you want to report '
                    u'this user?' % name,
                    ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                if answer in ['a', 'all']:
                    answer = 'y'
                    globalvar.confirm = False
            else:
                answer = 'y'

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

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

            com = i18n.twtranslate(self.site, 'welcome-bad_username')
            if rep_text != '':
                rep_page.put(text_get + rep_text, comment=com, force=True,
                             minorEdit=True)
                showStatus(5)
                pywikibot.output(u'Reported')
            self.BAQueue = list()
        else:
            return True
Esempio n. 10
0
   def save(self, text, page, comment=None, minorEdit=True,
             botflag=True):
      # only save if something was changed
      saveres=False
      if self.text != text:
         if not self.dry:
            if self.manual:
               # Show the title of the page we're working on.
               # Highlight the title in purple.
               pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                                % page.title())
               # show what was changed
               pywikibot.showDiff(page.get(), text)
               logger.info(u'Comment: %s' %comment)
   
               choice = pywikibot.inputChoice(
                       u'Do you want to accept these changes?',
                       ['Yes', 'No'], ['y', 'N'], 'N')
   
            if (not self.manual) or (self.manual and choice == 'y'):
               logger.debug("Saving ...")
               saveres=self._save(page,text,comment,minorEdit,botflag)
            else:
               logger.debug("Changes discarded - doing nothing")
      else:
         logger.debug("Nothing changed - doing nothing")

      if saveres:
         logger.info("Changes saved")
      else:
         logger.info("Nothing done")

      return saveres
 def treat(self, page):
     try:
         # Show the title of the page we're working on.
         # Highlight the title in purple.
         pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
         ccToolkit = CosmeticChangesToolkit(
             page.site, debug=True, namespace=page.namespace(), pageTitle=page.title()
         )
         changedText = ccToolkit.change(page.get())
         if changedText.strip() != page.get().strip():
             if not self.acceptall:
                 choice = pywikibot.inputChoice(
                     u"Do you want to accept these changes?", ["Yes", "No", "All", "Quit"], ["y", "N", "a", "q"], "N"
                 )
                 if choice == "a":
                     self.acceptall = True
                 elif choice == "q":
                     self.done = True
                     return
             if self.acceptall or choice == "y":
                 page.text = changedText
                 page.save(comment=self.comment, async=self.async)
         else:
             pywikibot.output("No changes were necessary in %s" % page.title())
     except pywikibot.NoPage:
         pywikibot.output("Page %s does not exist?!" % page.title(asLink=True))
     except pywikibot.IsRedirectPage:
         pywikibot.output("Page %s is a redirect; skipping." % page.title(asLink=True))
     except pywikibot.LockedPage:
         pywikibot.output("Page %s is locked?!" % page.title(asLink=True))
     except pywikibot.EditConflict:
         pywikibot.output("An edit conflict has occured at %s." % page.title(asLink=True))
Esempio n. 12
0
def delete_item(item, other, msg=None, by=site.user(), rfd=False, allow_sitelinks=False, askmerge=False):
    item.get(force=True)
    other.get(force=True)
    if allow_sitelinks is not True and len(item.sitelinks) > 0:
        error_merge_msg(item, other)
        return False
    if compare(item, other, 'sitelinks') is False or compare(item, other, 'labels') is False or compare(item, other, 'descriptions') is False:
        if askmerge and pywikibot.inputChoice(u'force merging?', ['Yes', 'No'], ['Y', 'N'], 'N').strip().lower() in ['yes', 'y']:
            merge_items((other, item), force_lower=False, taxon_mode=False)
        return False
    if by is None:
        by = item.site.data_repository().user()
    for key in item.aliases:
        for alias in item.aliases[key]:
            if alias.strip() != '' and ((key not in other.aliases) or (alias not in other.aliases[key])):
                error_merge_msg(item, other)
                return False
    for prop in item.claims:
        if (prop not in other.claims) or len(list(set([claim.getTarget() for claim in item.claims[prop]])-set([claim.getTarget() for claim in other.claims[prop]]))):
            error_merge_msg(item, other)
            return False
    if rfd:
        rfd_page = pywikibot.Page(site, 'Requests for deletions', ns=4)
        rfd_page.get(force=True)
        if msg is None:
            msg = u'Merged with {other}{by}'.format(other=other.getID(), by=(u' by [[User:{0}|{0}]]'.format(by) if by != site.user() else ''))
        rfd_page.text += u'\n\n{{{{subst:Request for deletion|itemid={qid}|reason={msg}}}}} --~~~~'.format(qid=item.getID(), msg=msg)
        page.save(comment=u'[[Wikidata:Bots|Bot]]: nominating [[{qid}]] for deletion'.format(qid=item.getID()), minor=False, botflag=True)
        pywikibot.output(u'\03{{lightgreen}}{item} successfully nominated for deletion'.format(item=item))
        return True
    else:
        item.delete(reason=(msg if msg else u'Merged with [[{qid}]] by [[User:{by}|{by}]]'.format(qid=other.getID(), by=by)))
        pywikibot.output(u'\03{{lightgreen}}{item} successfully deleted'.format(item=item))
        return True
Esempio n. 13
0
    def save(self, page, newText):
        """
        Saves the page to the wiki, if the user accepts the changes made.
        """
        pywikibot.showDiff(page.get(), newText)
        if not self.always:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?',
                ['Yes', 'No', 'Always yes'], 'yNa', 'Y')
            if choice == 'n':
                return
            elif choice == 'a':
                self.always = True

        page.text = newText
        if self.always:
            try:
                page.save(self.comment)
            except pywikibot.EditConflict:
                pywikibot.output(u'Skipping %s because of edit conflict'
                                 % (page.title(),))
            except pywikibot.SpamfilterError as e:
                pywikibot.output(
                    u'Cannot change %s because of blacklist entry %s'
                    % (page.title(), e.url))
            except pywikibot.LockedPage:
                pywikibot.output(u'Skipping %s (locked page)' % (page.title(),))
        else:
            # Save the page in the background. No need to catch exceptions.
            page.save(self.comment, async=True)
Esempio n. 14
0
    def handleNextLink(self, 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 self.pageToUnlink.site.isInterwikiLink(match.group('title')) \
           or match.group('section'):
            return text, False
        linkedPage = pywikibot.Page(self.pageToUnlink.site,
                                    match.group('title'))
        # Check whether the link found is to the current page itself.
        if linkedPage != self.pageToUnlink:
            # not a self-link
            return text, False
        else:
            # at the beginning of the link, start red color.
            # at the end of the link, reset the color to default
            if self.getOption('always'):
                choice = 'a'
            else:
                pywikibot.output(
                    text[max(0, match.start() - context):match.start()]
                    + '\03{lightred}' + text[match.start():match.end()]
                    + '\03{default}' + text[match.end():match.end() + context])
                choice = pywikibot.inputChoice(
                    u'\nWhat shall be done with this link?\n',
                    ['unlink', 'skip', 'edit', 'more context',
                     'unlink all', 'quit'],
                    ['U', 's', 'e', 'm', 'a', 'q'], 'u')
                pywikibot.output(u'')

                if choice == 's':
                    # skip this link
                    return text, False
                elif choice == 'e':
                    editor = TextEditor()
                    newText = editor.edit(text, jumpIndex=match.start())
                    # if user didn't press Cancel
                    if newText:
                        return newText, True
                    else:
                        return text, True
                elif choice == 'm':
                    # show more context by recursive self-call
                    return self.handleNextLink(text, match,
                                               context=context + 100)
                elif choice == 'a':
                    self.options['always'] = True
                elif choice == 'q':
                    self.quit()
            new = match.group('label') or match.group('title')
            new += match.group('linktrail')
            return text[:match.start()] + new + text[match.end():], False
Esempio n. 15
0
    def reportBadAccount(self, name=None, final=False):
        #Queue process
        if name:
            if globalvar.confirm:
                answer = pywikibot.inputChoice(
                    u'%s may have an unwanted username, do you want to report '
                    u'this user?' % name,
                    ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
                if answer in ['a', 'all']:
                    answer = 'y'
                    globalvar.confirm = False
            else:
                answer = 'y'

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

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

            com = i18n.twtranslate(self.site, 'welcome-bad_username')
            if rep_text != '':
                rep_page.put(text_get + rep_text, comment=com, force=True,
                             minorEdit=True)
                showStatus(5)
                pywikibot.output(u'Reported')
            self.BAQueue = list()
        else:
            return True
Esempio n. 16
0
def appendtext(page, apptext, always):
    if page.isRedirectPage():
        page = page.getRedirectTarget()
    if not page.exists():
        if page.isTalkPage():
            text = u''
        else:
            raise pywikibot.NoPage(u"Page '%s' does not exist" % page.title())
    else:
        text = page.text
    # Here you can go editing. If you find you do not
    # want to edit this page, just return
    oldtext = text
    text += apptext
    if text != oldtext:
        pywikibot.showDiff(oldtext, text)
        if not always:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?', ['Yes', 'No', 'All'],
                'yNa', 'N')
            if choice == 'a':
                always = True
        if always or choice == 'y':
            page.text = text
            page.save(i18n.translate(pywikibot.Site(), comment, fallback=True))
Esempio n. 17
0
    def save(self, page, newText):
        """
        Saves the page to the wiki, if the user accepts the changes made.
        """
        pywikibot.showDiff(page.get(), newText)
        if not self.always:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?',
                ['Yes', 'No', 'Always yes'], ['y', 'N', 'a'], 'Y')
            if choice == 'n':
                return
            elif choice == 'a':
                self.always = True

        if self.always:
            try:
                page.put(newText)
            except pywikibot.EditConflict:
                pywikibot.output(u'Skipping %s because of edit conflict'
                                 % (page.title(),))
            except pywikibot.SpamfilterError, e:
                pywikibot.output(
                    u'Cannot change %s because of blacklist entry %s'
                    % (page.title(), e.url))
            except pywikibot.LockedPage:
                pywikibot.output(u'Skipping %s (locked page)' % (page.title(),))
Esempio n. 18
0
    def userPut(self, page, oldtext, newtext):
        """
        Print differences, ask user for confirmation,
        and puts the page if needed.

        Option used:
            * 'always'
        """
        if oldtext == newtext:
            pywikibot.output(u'No changes were needed on %s' %
                             page.title(asLink=True))
            return

        pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" %
                         page.title())
        pywikibot.showDiff(oldtext, newtext)

        choice = 'a'
        if not self.getOption('always'):
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?', ['Yes', 'No', 'All'],
                ['y', 'N', 'a'], 'N')
            if choice == 'a':
                # Remember the choice
                self.options['always'] = True

        if choice != 'n':
            page.put(newtext, async=(choice == 'a'))
Esempio n. 19
0
 def save(self, text, page, comment=None, minorEdit=True, botflag=True):
     # only save if something was changed
     if text != page.get():
         # Show the title of the page we're working on.
         # Highlight the title in purple.
         pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" %
                          page.title())
         # show what was changed
         pywikibot.showDiff(page.get(), text)
         pywikibot.output(u'Comment: %s' % comment)
         if not self.dry:
             choice = pywikibot.inputChoice(
                 u'Do you want to accept these changes?', ['Yes', 'No'],
                 ['y', 'N'], 'N')
             if choice == 'y':
                 try:
                     page.text = text
                     # Save the page
                     page.save(comment=comment or self.comment,
                               minor=minorEdit,
                               botflag=botflag)
                 except pywikibot.LockedPage:
                     pywikibot.output(u"Page %s is locked; skipping." %
                                      page.title(asLink=True))
                 except pywikibot.EditConflict:
                     pywikibot.output(
                         u'Skipping %s because of edit conflict' %
                         (page.title()))
                 except pywikibot.SpamfilterError as error:
                     pywikibot.output(
                         u'Cannot change %s because of spam blacklist entry %s'
                         % (page.title(), error.url))
                 else:
                     return True
     return False
Esempio n. 20
0
    def save(self, page, newText):
        """
        Saves the page to the wiki, if the user accepts the changes made.
        """
        pywikibot.showDiff(page.get(), newText)
        if not self.always:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?',
                ['Yes', 'No', 'Always yes'], ['y', 'N', 'a'], 'Y')
            if choice == 'n':
                return
            elif choice == 'a':
                self.always = True

        if self.always:
            try:
                page.put(newText)
            except pywikibot.EditConflict:
                pywikibot.output(u'Skipping %s because of edit conflict' %
                                 (page.title(), ))
            except pywikibot.SpamfilterError, e:
                pywikibot.output(
                    u'Cannot change %s because of blacklist entry %s' %
                    (page.title(), e.url))
            except pywikibot.LockedPage:
                pywikibot.output(u'Skipping %s (locked page)' %
                                 (page.title(), ))
Esempio n. 21
0
def appendtext(page, apptext, always):
    if page.isRedirectPage():
        page = page.getRedirectTarget()
    if not page.exists():
        if page.isTalkPage():
            text = u''
        else:
            raise pywikibot.NoPage(u"Page '%s' does not exist" % page.title())
    else:
        text = page.text
    # Here you can go editing. If you find you do not
    # want to edit this page, just return
    oldtext = text
    text += apptext
    if text != oldtext:
        pywikibot.showDiff(oldtext, text)
        if not always:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?', ['Yes', 'No', 'All'],
                'yNa', 'N')
            if choice == 'a':
                always = True
        if always or choice == 'y':
            page.text = text
            page.save(i18n.translate(pywikibot.Site(), comment,
                                     fallback=True))
Esempio n. 22
0
 def get_dest(self):
     self.dest = u'%s-core.%s' % tuple(self.source.rsplit(u'.', 1))
     if not self.warnonly and pywikibot.inputChoice(
             u'Destination file is %s.' % self.dest, ['Yes', 'No'],
         ['y', 'n'], 'y') == 'n':
         pywikibot.output('Quitting...')
         exit()
Esempio n. 23
0
 def get_dest(self):
     self.dest = u'%s-core.%s' % tuple(self.source.rsplit(u'.', 1))
     if not self.warnonly and pywikibot.inputChoice(
             u'Destination file is %s.' % self.dest,
             ['Yes', 'No'], ['y', 'n'], 'y') == 'n':
         pywikibot.output('Quitting...')
         exit()
Esempio n. 24
0
 def save(self, text, page, comment=None, minorEdit=True,
          botflag=True):
     # only save if something was changed
     if text != page.get():
         # Show the title of the page we're working on.
         # Highlight the title in purple.
         pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                          % page.title())
         # show what was changed
         pywikibot.showDiff(page.get(), text)
         pywikibot.output(u'Comment: %s' % comment)
         if not self.dry:
             choice = pywikibot.inputChoice(
                 u'Do you want to accept these changes?',
                 ['Yes', 'No'], ['y', 'N'], 'N')
             if choice == 'y':
                 try:
                     page.text = text
                     # Save the page
                     page.save(comment=comment or self.comment,
                               minor=minorEdit, botflag=botflag)
                 except pywikibot.LockedPage:
                     pywikibot.output(u"Page %s is locked; skipping."
                                      % page.title(asLink=True))
                 except pywikibot.EditConflict:
                     pywikibot.output(
                         u'Skipping %s because of edit conflict'
                         % (page.title()))
                 except pywikibot.SpamfilterError as error:
                     pywikibot.output(
                         u'Cannot change %s because of spam blacklist entry %s'
                         % (page.title(), error.url))
                 else:
                     return True
     return False
Esempio n. 25
0
    def wait_completion(self):
        """Pending threads finish."""
        def remaining():
            remainingItems = self.pool.qsize() - 1
                # -1 because we added a None element to stop the queue
            remainingSeconds = ltime.td(seconds=remainingItems)
            return (remainingItems, remainingSeconds)

        self.pool.put((None, [], {}))

        if self.pool.qsize() > 1:
            pywikibot.output(u'Waiting for %i items to be put. Estimated time remaining: %s'
                   % remaining())

        while(self.thread.isAlive()):
            try:
                self.thread.join(1)
            except KeyboardInterrupt:
                answer = pywikibot.inputChoice(u"""\
    There are %i items remaining in the queue. Estimated time remaining: %s
    Really exit?"""
                                         % remaining(),
                                     ['yes', 'no'], ['y', 'N'], 'N')
                if answer == 'y':
                    return
Esempio n. 26
0
    def save(self, page, text):
        if text != page.get():
            # Show the title of the page we're working on.
            # Highlight the title in purple.
            pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" %
                             page.title())
            pywikibot.showDiff(page.get(), text)
            if not self.always:
                choice = pywikibot.inputChoice(
                    u'Do you want to accept these changes?',
                    ['Yes', 'No', 'Always yes'], ['y', 'N', 'a'], 'N')
                if choice == 'n':
                    return
                elif choice == 'a':
                    self.always = True

            if self.always:
                try:
                    page.put(text, comment=self.comment)
                except pywikibot.EditConflict:
                    pywikibot.output(u'Skipping %s because of edit conflict' %
                                     (page.title(), ))
                except pywikibot.SpamfilterError, e:
                    pywikibot.output(
                        u'Cannot change %s because of blacklist entry %s' %
                        (page.title(), e.url))
                except pywikibot.LockedPage:
                    pywikibot.output(u'Skipping %s (locked page)' %
                                     (page.title(), ))
Esempio n. 27
0
    def userPut(self, page, oldtext, newtext):
        """
        Print differences, ask user for confirmation,
        and puts the page if needed.

        Option used:
            * 'always'
        """
        if oldtext == newtext:
            pywikibot.output(u'No changes were needed on %s'
                             % page.title(asLink=True))
            return

        pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                         % page.title())
        pywikibot.showDiff(oldtext, newtext)

        choice = 'a'
        if not self.getOption('always'):
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?',
                ['Yes', 'No', 'All'],
                ['y', 'N', 'a'],
                'N'
            )
            if choice == 'a':
                # Remember the choice
                self.options['always'] = True

        if choice != 'n':
            page.put(newtext, async=(choice == 'a'))
Esempio n. 28
0
    def put_page(self, page, new):
        """ Print diffs between orginal and new (text), put new text for page

        """
        pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" %
                         page.title())
        pywikibot.showDiff(page.get(), new)
        if not self.acceptall:
            choice = pywikibot.inputChoice(
                u'Do you want to accept ' + u'these changes?',
                ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
            if choice == 'a':
                self.acceptall = True
            if choice == 'y':
                page.text = new
                page.save(self.msg, async=True)
        if self.acceptall:
            try:
                page.text = new
                page.save(self.msg)
            except pywikibot.EditConflict:
                pywikibot.output(u'Skipping %s because of edit conflict' %
                                 (page.title(), ))
            except pywikibot.SpamfilterError as e:
                pywikibot.output(
                    u'Cannot change %s because of blacklist entry %s' %
                    (page.title(), e.url))
            except pywikibot.PageNotSaved as error:
                pywikibot.error(u'putting page: %s' % (error.args, ))
            except pywikibot.LockedPage:
                pywikibot.output(u'Skipping %s (locked page)' %
                                 (page.title(), ))
            except pywikibot.ServerError as e:
                pywikibot.output(u'Server Error : %s' % e)
Esempio n. 29
0
    def save(self, page, text):
        if text != page.get():
            # Show the title of the page we're working on.
            # Highlight the title in purple.
            pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                             % page.title())
            pywikibot.showDiff(page.get(), text)
            if not self.always:
                choice = pywikibot.inputChoice(
                    u'Do you want to accept these changes?',
                    ['Yes', 'No', 'Always yes'], ['y', 'N', 'a'], 'N')
                if choice == 'n':
                    return
                elif choice == 'a':
                    self.always = True

            if self.always:
                try:
                    page.put(text, comment=self.comment)
                except pywikibot.EditConflict:
                    pywikibot.output(u'Skipping %s because of edit conflict'
                                     % (page.title(),))
                except pywikibot.SpamfilterError, e:
                    pywikibot.output(
                        u'Cannot change %s because of blacklist entry %s'
                        % (page.title(), e.url))
                except pywikibot.LockedPage:
                    pywikibot.output(u'Skipping %s (locked page)'
                                     % (page.title(),))
Esempio n. 30
0
def check_protection_level(operation, level, levels, default=None):
    """Check if the protection level is valid or asks if necessary.

    @return a valid protection level
    @rtype string
    """
    if level not in levels:
        first_char = []
        default_char = None
        num = 1
        for level in levels:
            for c in level:
                if c not in first_char:
                    first_char.append(c)
                    break
            else:
                first_char.append(unicode(num))
                num += 1
            if level == default:
                default_char = first_char[-1]
        choice = pywikibot.inputChoice('Choice a protection level to %s:'
                                       % operation, levels, first_char,
                                       default=default_char)

        return levels[first_char.index(choice)]
    else:
        return level
Esempio n. 31
0
    def put_page(self, page, new):
        """ Print diffs between orginal and new (text), put new text for page

        """
        pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
                         % page.title())
        pywikibot.showDiff(page.get(), new)
        if not self.acceptall:
            choice = pywikibot.inputChoice(u'Do you want to accept ' +
                                           u'these changes?',
                                           ['Yes', 'No', 'All'],
                                           ['y', 'N', 'a'], 'N')
            if choice == 'a':
                self.acceptall = True
            if choice == 'y':
                page.text = new
                page.save(self.msg, async=True)
        if self.acceptall:
            try:
                page.text = new
                page.save(self.msg)
            except pywikibot.EditConflict:
                pywikibot.output(u'Skipping %s because of edit conflict'
                                  % (page.title(),))
            except pywikibot.SpamfilterError as e:
                pywikibot.output(
                    u'Cannot change %s because of blacklist entry %s'
                    % (page.title(), e.url))
            except pywikibot.PageNotSaved as error:
                pywikibot.error(u'putting page: %s' % (error.args,))
            except pywikibot.LockedPage:
                pywikibot.output(u'Skipping %s (locked page)'
                                  % (page.title(),))
            except pywikibot.ServerError as e:
                pywikibot.output(u'Server Error : %s' % e)
Esempio n. 32
0
def check_protection_level(operation, level, levels, default=None):
    """Check if the protection level is valid or asks if necessary.

    @return a valid protection level
    @rtype string
    """
    if level not in levels:
        first_char = []
        default_char = None
        num = 1
        for level in levels:
            for c in level:
                if c not in first_char:
                    first_char.append(c)
                    break
            else:
                first_char.append(unicode(num))
                num += 1
            if level == default:
                default_char = first_char[-1]
        choice = pywikibot.inputChoice('Choice a protection level to %s:' %
                                       operation,
                                       levels,
                                       first_char,
                                       default=default_char)

        return levels[first_char.index(choice)]
    else:
        return level
Esempio n. 33
0
    def save(self, page, newText):
        """
        Saves the page to the wiki, if the user accepts the changes made.
        """
        pywikibot.showDiff(page.get(), newText)
        if not self.always:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?',
                ['Yes', 'No', 'Always yes'], 'yNa', 'Y')
            if choice == 'n':
                return
            elif choice == 'a':
                self.always = True

        page.text = newText
        if self.always:
            try:
                page.save(self.comment)
            except pywikibot.EditConflict:
                pywikibot.output(u'Skipping %s because of edit conflict' %
                                 (page.title(), ))
            except pywikibot.SpamfilterError as e:
                pywikibot.output(
                    u'Cannot change %s because of blacklist entry %s' %
                    (page.title(), e.url))
            except pywikibot.LockedPage:
                pywikibot.output(u'Skipping %s (locked page)' %
                                 (page.title(), ))
        else:
            # Save the page in the background. No need to catch exceptions.
            page.save(self.comment, async=True)
Esempio n. 34
0
def main():
    always = dryrun = False

    # Handles command-line arguments for pywikibot.
    for arg in pywikibot.handleArgs():
        if arg == '-dry':  # Performs a dry run (does not edit site)
            dryrun = True
        if arg == '-always':  # Does not ask for confirmation
            always = True

    # pywikibot/families/eagle_family.py
    site = pywikibot.Site('en', 'eagle').data_repository()

    f = open(DATA_FILE, 'r')
    reader = csv.reader(f, delimiter="\t")
    for row in reader:
        id = row[0]
        pywikibot.output("\n>>>>> " + id + " <<<<<\n")

        try:
            translationFr = row[1]
            image = row[2]
        except IndexError:
            pywikibot.output('ERROR: Data not found for id ' + id +
                             '. Ignoring.')

        if translationFr:
            pywikibot.output('Translation FR: ' + translationFr)
        else:
            pywikibot.output('WARNING: no translation!')

        if image:
            pywikibot.output('Image: ' + image)
        else:
            pywikibot.output('WARNING: no image!')

        pywikibot.output('')  # newline

        if not always:
            choice = pywikibot.inputChoice(u"Proceed?", ['Yes', 'No', 'All'],
                                           ['y', 'N', 'a'], 'N')
        else:
            choice = 'y'
        if choice in ['A', 'a']:
            always = True
            choice = 'y'
        if not dryrun and choice in ['Y', 'y']:
            page = pywikibot.ItemPage(site)
            page.editEntity({'labels': {'fr': id}})
            page.get()

            addClaimToItem(site, page, 'P33', id)  # Petrae ID
            if translationFr:
                addClaimToItem(site, page, 'P15', translationFr)

            # P10 wants Wikimedia Commons image link!
            # addClaimToItem(site, page, 'P10', image)

    f.close()
Esempio n. 35
0
def insert_alphabetically(page, regex, language_code, new, comment, suffix="\n"):
  """
  Inserts the "new" into the list given in site. Each entry of the list, is matched by
  "regex". This regex must have a group named "code" OR a placeholder named "code" ($code).

  This code is then checked against language_code, and if the current code is after the
  language_code it adds the "new" prior to that. The new entry needs a placeholde named code
  ($code).

  On both "regex" and "new" is string.Template.substitute applied. Any trailing $-signs are
  automatically escaped if not done so.
  """
  if re.search(r"[^$](\$\$)*\$$", regex):
    regex += '$'
  oldtext = page.text
  compiled = re.compile(Template(regex).substitute(code=r'(?P<code>[a-z]{2}(-[a-z]{2})?)', newline=r'(?P<newline>\n)?'), re.M)
  print compiled.pattern
  matches = list(compiled.finditer(oldtext))
  if len(matches) == 0:
    print("No matches found.")
    return False
  else:
    insert_at = 0
    got_to_end = False
    for match in matches:
      if match.group('code') == language_code:
        print("Language is already available")
        return False
      elif match.group('code') > language_code:
        insert_at = match.start()
        break
    else:
      insert_at = match.end()
      got_to_end = True
    try:
      if match.start('newline') < 0:
        suffix = ""
      else:
        suffix = "\n"
    except IndexError:
      pass
    print "here we are {}".format(insert_at)
    print oldtext[insert_at]
    if got_to_end and len(oldtext) > insert_at + len(suffix):
      insert_at += len(suffix)
    if insert_at > 0 and len(oldtext) == insert_at - len(suffix) + 1:
      print "+= {}".format(len(suffix))
      oldtext += suffix
    page.text = oldtext[:insert_at]
    if callable(new):
      page.text += new(language_code, match)
    else:
      page.text += Template(new).substitute(code=language_code) + suffix
    page.text += oldtext[insert_at:]
    print("Changes in {}:".format(page.title()))     
    pywikibot.showDiff(oldtext, page.text)
    if pywikibot.inputChoice("Save changes?", ['Yes', 'No'], ['y', 'n'], 'n') == 'y':
      page.save(comment=comment)
    return True
Esempio n. 36
0
 def get_dest(self):
     """Get the destination file and confirm it."""
     self.dest = self.source + '.py'
     if self.confirm and pywikibot.inputChoice(
             u'Destination file is %s.' % self.dest,
             answers=['yes', 'no'], hotkeys='yn', default='n') == 'n':
         pywikibot.output('Quitting...')
         exit()
Esempio n. 37
0
    def save(self, text, page, newcatTitle, minorEdit=True, botflag=True):
        # only save if something was changed
        if text != page.get():
            # show what was changed
            pywikibot.showDiff(page.get(), text)
            comment = self.editSummary
            if not comment:
                comment = i18n.twtranslate(page.site, 'category-adding',
                                           {'newcat': newcatTitle})
            pywikibot.output(u'Comment: %s' % comment)
            if not self.dry:
                if not self.always:
                    confirm = 'y'
                    while True:
                        choice = pywikibot.inputChoice(
                            u'Do you want to accept these changes?',
                            ['Yes', 'No', 'Always'], ['y', 'N', 'a'], 'N')
                        if choice == 'a':
                            confirm = pywikibot.inputChoice(u"""\
This should be used if and only if you are sure that your links are correct!
Are you sure?""", ['Yes', 'No'], ['y', 'n'], 'n')
                            if confirm == 'y':
                                self.always = True
                                break
                        else:
                            break
                if self.always or choice == 'y':
                    try:
                        # Save the page
                        page.put(text, comment=comment,
                                 minorEdit=minorEdit, botflag=botflag)
                    except pywikibot.LockedPage:
                        pywikibot.output(u"Page %s is locked; skipping."
                                         % page.title(asLink=True))
                    except pywikibot.EditConflict:
                        pywikibot.output(
                            u'Skipping %s because of edit conflict'
                            % (page.title()))
                    except pywikibot.SpamfilterError as error:
                        pywikibot.output(
                            u'Cannot change %s because of spam blacklist entry '
                            u'%s' % (page.title(), error.url))
                    else:
                        return True
        return False
Esempio n. 38
0
    def categories(self):
        for page in self.generator:
            try:
                pywikibot.output(u'\n>>>> %s <<<<' % page.title())
                commons = pywikibot.Site().image_repository()
                commonsCategory = pywikibot.Category(
                    commons, 'Category:%s' % page.title())
                try:
                    getcommonscat = commonsCategory.text
                    commonsCategoryTitle = commonsCategory.title()
                    categoryname = commonsCategoryTitle.split('Category:',
                                                              1)[1]
                    if page.title() == categoryname:
                        oldText = page.get()
                        text = oldText

                        # for commonscat template
                        findTemplate = re.compile(ur'\{\{[Cc]ommons')
                        s = findTemplate.search(text)
                        findTemplate2 = re.compile(ur'\{\{[Ss]isterlinks')
                        s2 = findTemplate2.search(text)
                        if s or s2:
                            pywikibot.output(u'** Already done.')
                        else:
                            text = pywikibot.replaceCategoryLinks(
                                text + u'{{commonscat|%s}}' % categoryname,
                                list(page.categories()))
                            if oldText != text:
                                pywikibot.showDiff(oldText, text)
                                if not self.acceptall:
                                    choice = pywikibot.inputChoice(
                                        u'Do you want to accept these changes?',
                                        ['Yes', 'No', 'All'], ['y', 'N', 'a'],
                                        'N')
                                    if choice == 'a':
                                        self.acceptall = True
                                if self.acceptall or choice == 'y':
                                    try:
                                        msg = i18n.twtranslate(
                                            pywikibot.Site(),
                                            'commons_link-cat-template-added')
                                        page.put(text, msg)
                                    except pywikibot.EditConflict:
                                        pywikibot.output(
                                            u'Skipping %s because of edit '
                                            u'conflict' % (page.title()))

                except pywikibot.NoPage:
                    pywikibot.output(u'Category does not exist in Commons!')

            except pywikibot.NoPage:
                pywikibot.output(u'Page %s does not exist?!' % page.title())
            except pywikibot.IsRedirectPage:
                pywikibot.output(u'Page %s is a redirect; skipping.' %
                                 page.title())
            except pywikibot.LockedPage:
                pywikibot.output(u'Page %s is locked?!' % page.title())
Esempio n. 39
0
def main():
    always = dryrun = False

    # Handles command-line arguments for pywikibot.
    for arg in pywikibot.handleArgs():
        if arg == "-dry":  # Performs a dry run (does not edit site)
            dryrun = True
        if arg == "-always":  # Does not ask for confirmation
            always = True

            # pywikibot/families/eagle_family.py
    site = pywikibot.Site("en", "eagle").data_repository()

    f = open(DATA_FILE, "r")
    reader = csv.reader(f, delimiter="\t")
    for row in reader:
        id = row[0]
        pywikibot.output("\n>>>>> " + id + " <<<<<\n")

        try:
            translationFr = row[1]
            image = row[2]
        except IndexError:
            pywikibot.output("ERROR: Data not found for id " + id + ". Ignoring.")

        if translationFr:
            pywikibot.output("Translation FR: " + translationFr)
        else:
            pywikibot.output("WARNING: no translation!")

        if image:
            pywikibot.output("Image: " + image)
        else:
            pywikibot.output("WARNING: no image!")

        pywikibot.output("")  # newline

        if not always:
            choice = pywikibot.inputChoice(u"Proceed?", ["Yes", "No", "All"], ["y", "N", "a"], "N")
        else:
            choice = "y"
        if choice in ["A", "a"]:
            always = True
            choice = "y"
        if not dryrun and choice in ["Y", "y"]:
            page = pywikibot.ItemPage(site)
            page.editEntity({"labels": {"fr": id}})
            page.get()

            addClaimToItem(site, page, "P33", id)  # Petrae ID
            if translationFr:
                addClaimToItem(site, page, "P15", translationFr)

                # P10 wants Wikimedia Commons image link!
                # addClaimToItem(site, page, 'P10', image)

    f.close()
Esempio n. 40
0
    def upload_image(self, debug=False):
        """Upload the image at self.url to the target wiki.

        Return the filename that was used to upload the image.
        If the upload fails, ask the user whether to try again or not.
        If the user chooses not to retry, return null.

        """
        filename = self.process_filename()

        site = self.targetSite
        imagepage = pywikibot.FilePage(site, filename)  # normalizes filename
        imagepage.text = self.description

        pywikibot.output(u'Uploading file to %s via API....' % site)

        try:
            if self.uploadByUrl:
                site.upload(imagepage, source_url=self.url,
                            ignore_warnings=self.ignoreWarning)
            else:
                if "://" in self.url:
                    temp = self.read_file_content()
                else:
                    temp = self.url
                site.upload(imagepage, source_filename=temp,
                            ignore_warnings=self.ignoreWarning,
                            chunk_size=self.chunk_size)

        except pywikibot.data.api.UploadWarning as warn:
            pywikibot.output(
                u'We got a warning message: {0}'.format(warn.message))
            if self.abort_on_warn(warn.code):
                answer = "N"
            else:
                answer = pywikibot.inputChoice(u"Do you want to ignore?",
                                               ['Yes', 'No'], ['y', 'N'], 'N')
            if answer == "y":
                self.ignoreWarning = 1
                self.keepFilename = True
                return self.upload_image(debug)
            else:
                pywikibot.output(u"Upload aborted.")
                return
        except pywikibot.data.api.APIError as error:
            if error.code == u'uploaddisabled':
                pywikibot.error("Upload error: Local file uploads are disabled on %s."
                                  % site)
            else:
                pywikibot.error("Upload error: ", exc_info=True)
        except Exception:
            pywikibot.error("Upload error: ", exc_info=True)

        else:
            # No warning, upload complete.
            pywikibot.output(u"Upload successful.")
            return filename  # data['filename']
Esempio n. 41
0
    def categories(self):
        for page in self.generator:
            try:
                pywikibot.output(u'\n>>>> %s <<<<' % page.title())
                commons = pywikibot.getSite().image_repository()
                commonsCategory = pywikibot.Category(commons,
                                                     'Category:%s' % page.title())
                try:
                    getcommonscat = commonsCategory.get(get_redirect=True)
                    commonsCategoryTitle = commonsCategory.title()
                    categoryname = commonsCategoryTitle.split('Category:', 1)[1]
                    if page.title() == categoryname:
                        oldText = page.get()
                        text = oldText

                        # for commonscat template
                        findTemplate = re.compile(ur'\{\{[Cc]ommons')
                        s = findTemplate.search(text)
                        findTemplate2 = re.compile(ur'\{\{[Ss]isterlinks')
                        s2 = findTemplate2.search(text)
                        if s or s2:
                            pywikibot.output(u'** Already done.')
                        else:
                            text = pywikibot.replaceCategoryLinks(
                                text + u'{{commonscat|%s}}' % categoryname,
                                list(page.categories()))
                            if oldText != text:
                                pywikibot.showDiff(oldText, text)
                                if not self.acceptall:
                                    choice = pywikibot.inputChoice(
                                        u'Do you want to accept these changes?',
                                        ['Yes', 'No', 'All'], ['y', 'N', 'a'],
                                        'N')
                                    if choice == 'a':
                                        self.acceptall = True
                                if self.acceptall or choice == 'y':
                                    try:
                                        msg = i18n.twtranslate(
                                            pywikibot.getSite(), 'commons_link-cat-template-added')
                                        page.put(text, msg)
                                    except pywikibot.EditConflict:
                                        pywikibot.output(
                                            u'Skipping %s because of edit '
                                            u'conflict'
                                            % (page.title()))

                except pywikibot.NoPage:
                    pywikibot.output(u'Category does not exist in Commons!')

            except pywikibot.NoPage:
                pywikibot.output(u'Page %s does not exist?!' % page.title())
            except pywikibot.IsRedirectPage:
                pywikibot.output(u'Page %s is a redirect; skipping.'
                                 % page.title())
            except pywikibot.LockedPage:
                pywikibot.output(u'Page %s is locked?!' % page.title())
Esempio n. 42
0
    def PickTarget(self, title, original, candidates):
        if len(candidates) == 0:
            return
        if len(candidates) == 1:
            return candidates[0]

        pagesDontExist = []
        pagesRedir = {}
        pagesExist = []

        for newTitle in candidates:
            dst = self.Page(newTitle)
            if not dst.exists():
                pagesDontExist.append(newTitle)
            elif dst.isRedirectPage():
                pagesRedir[newTitle] = dst.getRedirectTarget().title()
            else:
                pagesExist.append(newTitle)
        if len(pagesExist) == 1:
            return pagesExist[0]
        elif len(pagesExist) == 0 and len(pagesRedir) > 0:
            if len(pagesRedir) == 1:
                return pagesRedir.keys()[0]
            t = None
            for k, v in pagesRedir.iteritems():
                if not t:
                    t = v  # first item
                elif t != v:
                    break
            else:
                # all redirects point to the same target
                # pick the first one, doesn't matter what it is
                return pagesRedir.keys()[0]

        if not self.autonomous:
            pywikibot.output(u'Could not auto-decide for page %s. Which link '
                             u'should be chosen?' %
                             self.MakeLink(title, False))
            pywikibot.output(u'Original title: ', newline=False)
            self.ColorCodeWord(original + "\n", True)
            count = 1
            for t in candidates:
                if t in pagesDontExist:
                    msg = u'missing'
                elif t in pagesRedir:
                    msg = u'Redirect to ' + pagesRedir[t]
                else:
                    msg = u'page exists'
                self.ColorCodeWord(u'  %d: %s (%s)\n' % (count, t, msg), True)
                count += 1
            answers = [str(i) for i in xrange(0, count)]
            choice = int(
                pywikibot.inputChoice(u'Which link to choose? (0 to skip)',
                                      answers, [a[0] for a in answers]))
            if choice > 0:
                return candidates[choice - 1]
Esempio n. 43
0
def put(page, text, comment):
	global always
	if always:
		answer = 'a'
	else:
		answer = pywikibot.inputChoice("Are you sure you want to add the code to %s?" % page.title(), ["Yes", "No", "Always"], ["y", "n", "a"])
	if answer == 'a':
		always = True
	if answer != 'n':
		page.put(text, comment=comment)
Esempio n. 44
0
def choiceProtectionLevel(operation, default):
    default = default[0]
    firstChar = map(lambda level: level[0], protectionLevels)
    choiceChar = pywikibot.inputChoice('Choice a protection level to %s:'
                                       % operation,
                                       protectionLevels, firstChar,
                                       default=default)
    for level in protectionLevels:
        if level.startswith(choiceChar):
            return level
Esempio n. 45
0
 def get_dest(self):
     """Get the destination file and confirm it."""
     self.dest = self.source + '.py'
     if self.confirm and pywikibot.inputChoice(
             u'Destination file is %s.' % self.dest,
             answers=['yes', 'no'],
             hotkeys='yn',
             default='n') == 'n':
         pywikibot.output('Quitting...')
         exit()
Esempio n. 46
0
    def PickTarget(self, title, original, candidates):
        if len(candidates) == 0:
            return
        if len(candidates) == 1:
            return candidates[0]

        pagesDontExist = []
        pagesRedir = {}
        pagesExist = []

        for newTitle in candidates:
            dst = self.Page(newTitle)
            if not dst.exists():
                pagesDontExist.append(newTitle)
            elif dst.isRedirectPage():
                pagesRedir[newTitle] = dst.getRedirectTarget().title()
            else:
                pagesExist.append(newTitle)
        if len(pagesExist) == 1:
            return pagesExist[0]
        elif len(pagesExist) == 0 and len(pagesRedir) > 0:
            if len(pagesRedir) == 1:
                return pagesRedir.keys()[0]
            t = None
            for k, v in pagesRedir.iteritems():
                if not t:
                    t = v  # first item
                elif t != v:
                    break
            else:
                # all redirects point to the same target
                # pick the first one, doesn't matter what it is
                return pagesRedir.keys()[0]

        if not self.autonomous:
            pywikibot.output(u'Could not auto-decide for page %s. Which link '
                             u'should be chosen?' % self.MakeLink(title, False))
            pywikibot.output(u'Original title: ', newline=False)
            self.ColorCodeWord(original + "\n", True)
            count = 1
            for t in candidates:
                if t in pagesDontExist:
                    msg = u'missing'
                elif t in pagesRedir:
                    msg = u'Redirect to ' + pagesRedir[t]
                else:
                    msg = u'page exists'
                self.ColorCodeWord(u'  %d: %s (%s)\n' % (count, t, msg), True)
                count += 1
            answers = [str(i) for i in xrange(0, count)]
            choice = int(pywikibot.inputChoice(
                u'Which link to choose? (0 to skip)',
                answers, [a[0] for a in answers]))
            if choice > 0:
                return candidates[choice - 1]
Esempio n. 47
0
    def review_hunks(self):
        "Review hunks."

        help_msg = [
            'y -> accept this hunk',
            'n -> do not accept this hunk',
            's -> do not accept this hunk and stop reviewing',
            'a -> accept this hunk and all other pending',
            'r -> review later',
            'h -> help',
        ]

        question = 'Accept this hunk?'
        answers = ['yes', 'no', 'stop', 'all', 'review', 'help']
        hotkeys = ['y', 'n', 's', 'a', 'r', 'h']
        actions = {
            'y': Hunk.APPR,
            'n': Hunk.NOT_APPR,
            's': Hunk.NOT_APPR,
            'a': Hunk.APPR,
            'r': Hunk.PENDING,
        }

        pending = [h for h in self.hunks if h.reviewed == h.PENDING]

        while pending:

            hunk = pending.pop(0)

            pywikibot.output(hunk.header + hunk.diff_text)
            choice = pywikibot.inputChoice(question,
                                           answers,
                                           hotkeys,
                                           default='r')

            if choice in actions.keys():
                hunk.reviewed = actions[choice]
            if choice == 's':
                while pending:
                    hunk = pending.pop(0)
                    hunk.reviewed = hunk.NOT_APPR
                break
            elif choice == 'a':
                while pending:
                    hunk = pending.pop(0)
                    hunk.reviewed = hunk.APPR
                break
            elif choice == 'h':
                pywikibot.output(u'\03{purple}%s\03{default}' %
                                 u'\n'.join(help_msg))
                pending.insert(0, hunk)
            elif choice == 'r':
                pending.append(hunk)

        return
Esempio n. 48
0
    def run(self):
        """Start the robot's action."""
        # regular expression to find the original template.
        # {{vfd}} does the same thing as {{Vfd}}, so both will be found.
        # The old syntax, {{msg:vfd}}, will also be found.
        # The group 'parameters' will either match the parameters, or an
        # empty string if there are none.

        replacements = []
        exceptions = {}
        site = pywikibot.Site()
        for old, new in self.templates.items():
            namespaces = list(site.namespace(10, all=True))
            if not site.nocapitalize:
                pattern = '[' + \
                          re.escape(old[0].upper()) + \
                          re.escape(old[0].lower()) + \
                          ']' + re.escape(old[1:])
            else:
                pattern = re.escape(old)
            pattern = re.sub(r'_|\\ ', r'[_ ]', pattern)
            templateRegex = re.compile(r'\{\{ *(' + ':|'.join(namespaces) + \
                                       r':|[mM][sS][gG]:)?' + pattern + \
                                       r'(?P<parameters>\s*\|.+?|) *}}',
                                       re.DOTALL)

            if self.subst and self.remove:
                replacements.append(
                    (templateRegex, '{{subst:%s\g<parameters>}}' % new))
                exceptions['inside-tags'] = ['ref', 'gallery']
            elif self.subst:
                replacements.append(
                    (templateRegex, '{{subst:%s\g<parameters>}}' % old))
                exceptions['inside-tags'] = ['ref', 'gallery']
            elif self.remove:
                replacements.append((templateRegex, ''))
            else:
                template = pywikibot.Page(site, new, ns=10)
                if not template.exists():
                    pywikibot.warning(u'Template "%s" does not exist.' % new)
                    choice = pywikibot.inputChoice(
                        u'Do you want to proceed anyway?', ['Yes', 'No'],
                        ['y', 'N'], 'N')
                    if choice == 'n':
                        continue
                replacements.append(
                    (templateRegex, '{{%s\g<parameters>}}' % new))

        replaceBot = replace.ReplaceRobot(self.generator,
                                          replacements,
                                          exceptions,
                                          acceptall=self.acceptAll,
                                          addedCat=self.addedCat,
                                          summary=self.editSummary)
        replaceBot.run()
Esempio n. 49
0
 def treat(self):
     page = pywikibot.Page(self.site, self.your_page)
     if page.exists():
         pywikibot.output(
             u'\nWikitable on \03{lightpurple}%s\03{default} will be '
             u'completed with:\n' % self.your_page)
         text = page.get()
         newtext = self.newraw()
         pywikibot.output(newtext)
         choice = pywikibot.inputChoice(
             u'Do you want to add these on wikitable?', ['Yes', 'No'],
             ['y', 'N'], 'N')
         text = text[:-3] + newtext
         summ = pywikibot.translate(self.site, summary_update)
         if choice == 'y':
             try:
                 page.put(u''.join(text), summ)
             except:
                 pywikibot.output(u'Impossible to edit. It may be an '
                                  u'edit conflict. Skipping...')
     else:
         pywikibot.output(
             u'\nWikitable on \03{lightpurple}%s\03{default} will be '
             u'created with:\n' % self.your_page)
         newtext = self.newtable() + self.newraw()
         pywikibot.output(newtext)
         summ = pywikibot.translate(self.site, summary_creation)
         choice = pywikibot.inputChoice(
             u'Do you want to accept this page creation?', ['Yes', 'No'],
             ['y', 'N'], 'N')
         if choice == 'y':
             try:
                 page.put(newtext, summ)
             except pywikibot.LockedPage:
                 pywikibot.output(u"Page %s is locked; skipping." % title)
             except pywikibot.EditConflict:
                 pywikibot.output(u'Skipping %s because of edit conflict' %
                                  title)
             except pywikibot.SpamfilterError, error:
                 pywikibot.output(u'Cannot change %s because of spam '
                                  u'blacklist entry %s' %
                                  (title, error.url))
Esempio n. 50
0
        def testInputChoiceIncorrectAnswer(self):
            newstdin.write("X\nN\n")
            newstdin.seek(0)

            returned = pywikibot.inputChoice("question", ["answer 1", "answer 2", "answer 3"], ["A", "N", "S"], "A")

            self.assertEqual(newstdout.getvalue(), "")
            self.assertEqual(newstderr.getvalue(), "question ([A]nswer 1, a[N]swer 2, an[S]wer 3) " * 2)

            self.assertIsInstance(returned, unicode)
            self.assertEqual(returned, "n")
Esempio n. 51
0
def choiceProtectionLevel(operation, default):
    default = default[0]
    firstChar = map(lambda level: level[0], protectionLevels)
    choiceChar = pywikibot.inputChoice('Choice a protection level to %s:' %
                                       operation,
                                       protectionLevels,
                                       firstChar,
                                       default=default)
    for level in protectionLevels:
        if level.startswith(choiceChar):
            return level
Esempio n. 52
0
    def run(self):
        """Start the robot's action."""
        # regular expression to find the original template.
        # {{vfd}} does the same thing as {{Vfd}}, so both will be found.
        # The old syntax, {{msg:vfd}}, will also be found.
        # The group 'parameters' will either match the parameters, or an
        # empty string if there are none.

        replacements = []
        exceptions = {}
        site = pywikibot.Site()
        for old, new in self.templates.items():
            namespaces = list(site.namespace(10, all=True))
            if not site.nocapitalize:
                pattern = '[' + \
                          re.escape(old[0].upper()) + \
                          re.escape(old[0].lower()) + \
                          ']' + re.escape(old[1:])
            else:
                pattern = re.escape(old)
            pattern = re.sub(r'_|\\ ', r'[_ ]', pattern)
            templateRegex = re.compile(r'\{\{ *(' + ':|'.join(namespaces) + \
                                       r':|[mM][sS][gG]:)?' + pattern + \
                                       r'(?P<parameters>\s*\|.+?|) *}}',
                                       re.DOTALL)

            if self.subst and self.remove:
                replacements.append((templateRegex,
                                     '{{subst:%s\g<parameters>}}' % new))
                exceptions['inside-tags'] = ['ref', 'gallery']
            elif self.subst:
                replacements.append((templateRegex,
                                     '{{subst:%s\g<parameters>}}' % old))
                exceptions['inside-tags'] = ['ref', 'gallery']
            elif self.remove:
                replacements.append((templateRegex, ''))
            else:
                template = pywikibot.Page(site, new, ns=10)
                if not template.exists():
                    pywikibot.warning(u'Template "%s" does not exist.' % new)
                    choice = pywikibot.inputChoice(
                        u'Do you want to proceed anyway?',
                        ['Yes', 'No'], ['y', 'N'], 'N')
                    if choice == 'n':
                        continue
                replacements.append((templateRegex,
                                     '{{%s\g<parameters>}}' % new))

        replaceBot = replace.ReplaceRobot(self.generator, replacements,
                                          exceptions, acceptall=self.acceptAll,
                                          addedCat=self.addedCat,
                                          summary=self.editSummary)
        replaceBot.run()
Esempio n. 53
0
    def addZeroSection(self):
        global acceptAll
        editSummary = u'оформлення'
        pywikibot.output(u'Working on ' + self.page.title(asLink=True) +
                         u'...')
        text = self.page.get()

        if len(self.removeTopPart(text).strip()) == 0:
            pywikibot.output(u'No lead text in ' +
                             self.page.title(asLink=True) + u'.')
            return

        params = {
            u'action': u'query',
            u'prop': u'revisions',
            u'titles': self.page.title(),
            u'rvprop': u'content',
            u'rvlimit': 1,
            u'rvsection': 0
        }

        params[u'site'] = self.page.site
        result = pywikibot.data.api.Request(**params).submit()
        leadText = result[u'query'][u'pages'].values(
        )[0][u'revisions'][0][u'*']

        leadTextWithoutTemplates = self.removeTopPart(leadText).strip()
        if len(leadTextWithoutTemplates) == 0:
            pywikibot.output(u'No lead text in ' +
                             self.page.title(asLink=True) + u'.')
            return

        newLeadText = leadText.replace(leadTextWithoutTemplates,
                                       u'\n== ... ==\n' + leadText).strip()
        newText = text.replace(leadText, newLeadText)
        newText = self.cosmeticChanges(newText)

        pywikibot.output(u'Changes for ' + self.page.title(asLink=True) + u':')
        pywikibot.showDiff(oldtext=text, newtext=newText)

        choice = 'y'
        if not acceptAll:
            choice = pywikibot.inputChoice(
                u'Do you want to accept these changes?',
                ['Yes', 'No', 'All', 'Quit'], ['y', 'N', 'a', 'q'], 'N')
            if choice == 'q':
                exit()
            if choice == 'a':
                acceptAll = True
                choice = 'y'
        if choice == 'y':
            self.page.put(newText, editSummary)
            pass
Esempio n. 54
0
    def save(self, text, page, comment, minorEdit=True, botflag=True):
        # only save if something was changed
        if text != page.get():
            arretdurgence()

            # Show the title of the page we're working on.
            # Highlight the title in purple.
            pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" %
                             page.title())
            # show what was changed
            #pywikibot.showDiff(page.get(), text)
            pywikibot.output(text)
            pywikibot.output(u'Comment: %s' % comment)
            if not self.dry:
                #choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No'], ['y', 'N'], 'N')
                choice = 'y'
                if choice == 'y':
                    try:
                        arretdurgence()
                        if arret == "Oui":
                            pywikibot.inputChoice(u"Demande d'arrêt d'urgence",
                                                  ['vu'], ['v'], '')
                        # Save the page
                        page.put(text,
                                 comment=comment,
                                 minorEdit=minorEdit,
                                 botflag=botflag)
                    except pywikibot.LockedPage:
                        pywikibot.output(u"Page %s is locked; skipping." %
                                         page.title(asLink=True))
                    except pywikibot.EditConflict:
                        pywikibot.output(
                            u'Skipping %s because of edit conflict' %
                            (page.title()))
                    except pywikibot.SpamblacklistError, error:
                        pywikibot.output(
                            u'Cannot change %s because of spam blacklist entry %s'
                            % (page.title(), error.url))
                    else:
                        return True
Esempio n. 55
0
def showQuest(site, page):
    quest = pywikibot.inputChoice(u'Do you want to open the page?',
                                  ['with browser', 'with gui', 'no'],
                                  ['b', 'g', 'n'], 'n')
    pathWiki = site.family.nicepath(site.lang)
    url = 'http://%s%s%s?&redirect=no' % (pywikibot.getSite().hostname(),
                                          pathWiki, page.urlname())
    if quest == 'b':
        webbrowser.open(url)
    elif quest == 'g':
        from pywikibot import editor as editarticle
        editor = editarticle.TextEditor()
        text = editor.edit(page.get())
Esempio n. 56
0
 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
Esempio n. 57
0
 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
Esempio n. 58
0
def showQuest(page):
    quest = pywikibot.inputChoice(u'Do you want to open the page?',
                                  ['with browser', 'with gui', 'no'],
                                  ['b', 'g', 'n'], 'n')
    site = page.site
    url = '%s://%s%s?redirect=no' % (site.protocol(), site.hostname(),
                                     site.nice_get_address(
                                         page.title(asUrl=True)))
    if quest == 'b':
        webbrowser.open(url)
    elif quest == 'g':
        from pywikibot import editor as editarticle
        editor = editarticle.TextEditor()
        editor.edit(page.text)