Exemple #1
0
def choosecats(pagetext):
    chosen = []
    flag = False
    length = 1000
    print("Give the new categories, one per line.")
    print("Empty line: if the first, don't change. Otherwise: Ready.")
    print("-: I made a mistake, let me start over.")
    print("?: Give the text of the page with GUI.")
    print("??: Give the text of the page in console.")
    print("xx: if the first, remove all categories and add no new.")
    print("q: quit.")
    while flag == False:
        choice = pywikibot.input(u"?")
        if choice == "":
            flag = True
        elif choice == "-":
            chosen = choosecats(pagetext)
            flag = True
        elif choice == "?":
            import editarticle
            editor = editarticle.TextEditor()
            newtext = editor.edit(pagetext)
        elif choice == "??":
            pywikibot.output(pagetext[0:length])
            length = length + 500
        elif choice == "xx" and chosen == []:
            chosen = None
            flag = True
        elif choice == "q":
            print "quit..."
            sys.exit()
        else:
            chosen.append(choice)
    return chosen
Exemple #2
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.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 = editarticle.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.always = True
                elif choice == 'q':
                    self.done = True
                    return text, False
            new = match.group('label') or match.group('title')
            new += match.group('linktrail')
            return text[:match.start()] + new + text[match.end():], False
Exemple #3
0
 def process_filename(self):
     # Isolate the pure name
     filename = self.url
     
     if '/' in filename:
         filename = filename.split('/')[-1]
     
     if '\\' in filename:
         filename = filename.split('\\')[-1]
     
     if self.urlEncoding:
         filename = urllib.unquote(filename.decode(self.urlEncoding))
     
     if self.useFilename:
         filename = self.useFilename
     if not self.keepFilename:
         wikipedia.output(u"The filename on the target wiki will default to: %s" % filename)
         # ask newfn until it's valid
         ok = False
         # 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')
         while not ok:
             ok = True
             newfn = wikipedia.input(u'Enter a better name, or press enter to accept:')
             if newfn == "":
                 newfn = filename
             ext = os.path.splitext(newfn)[1].lower().strip('.')
             for c in forbidden:
                 if c in newfn:
                     print "Invalid character: %s. Please try again" % c
                     ok = False
             if ext not in allowed_formats and ok:
                 choice = wikipedia.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':
                     ok = False
         if newfn != '':
             filename = newfn
     # MediaWiki doesn't allow spaces in the file name.
     # Replace them here to avoid an extra confirmation form
     filename = filename.replace(' ', '_')
     # A proper description for the submission.
     wikipedia.output(u"The suggested description is:")
     wikipedia.output(self.description)
     if self.verifyDescription:
             newDescription = u''
             choice = wikipedia.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
Exemple #4
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':
        import editarticle
        editor = editarticle.TextEditor()
        text = editor.edit(page.get())
Exemple #5
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:
            filename = filename.split('/')[-1]

        if '\\' in filename:
            filename = filename.split('\\')[-1]

        if self.urlEncoding:
            filename = urllib.unquote(filename.decode(self.urlEncoding))

        if self.useFilename:
            filename = self.useFilename
        if not self.keepFilename:
            pywikibot.output(
                u"The filename on the target wiki will default to: %s" %
                filename)
            ok = False
            ext = os.path.splitext(filename)[1].lower().strip('.')
            # 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', u'ogv',
                               u'oga', u'tif', u'tiff')
            # ask until it's valid
            ok = True
            filename = tempfile.gettempprefix()

        # MediaWiki doesn't allow spaces in the file name.
        # Replace them here to avoid an extra confirmation form
        filename = filename.replace(' ', '_')
        # 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
Exemple #6
0
def spellcheck(page, checknames=True, knownonly=False, title=''):
    pageskip = []
    text = page
    if correct_html_codes:
        text = removeHTML(text)
    loc = 0
    while True:
        wordsearch = re.compile(r'([\s\=\<\>\_]*)([^\s\=\<\>\_]+)')
        match = wordsearch.search(text, loc)
        if not match:
            # No more words on this page
            break
        loc += len(match.group(1))
        bigword = Word(match.group(2))
        smallword = bigword.derive()
        if not Word(smallword).isCorrect(checkalternative=knownonly) and \
           (checknames or not smallword[0].isupper()):
            replacement = askAlternative(
                smallword,
                context=text[max(0, loc - 40):loc + len(match.group(2)) + 40],
                title=title)
            if replacement == edit:
                import editarticle
                editor = editarticle.TextEditor()
                # TODO: Don't know to which index to jump
                newtxt = editor.edit(text, jumpIndex=0, highlight=smallword)
                if newtxt:
                    text = newtxt
            elif replacement == endpage:
                loc = len(text)
            else:
                replacement = bigword.replace(replacement)
                text = text[:loc] + replacement + text[loc +
                                                       len(match.group(2)):]
                loc += len(replacement)
            if knownonly == 'plus' and text != page:
                knownonly = False
                loc = 0
        else:
            loc += len(match.group(2))
    if correct_html_codes:
        text = removeHTML(text)
    pageskip = []
    return text
 def run(self):
     """
     Starts the robot.
     """
     # Run the generator which will yield Pages which might need to be
     # changed.
     for page in self.generator:
         if self.isTitleExcepted(page.title()):
             pywikibot.output(
                 u'Skipping %s because the title is on the exceptions list.'
                 % page.title(asLink=True))
             continue
         try:
             # Load the page's text from the wiki
             original_text = page.get(get_redirect=True)
             if not (self.articles or page.canBeEdited()):
                 pywikibot.output(u"You can't edit page %s" %
                                  page.title(asLink=True))
                 continue
         except pywikibot.NoPage:
             pywikibot.output(u'Page %s not found' %
                              page.title(asLink=True))
             continue
         new_text = original_text
         while True:
             if self.isTextExcepted(new_text):
                 pywikibot.output(
                     u'Skipping %s because it contains text that is on the exceptions list.'
                     % page.title(asLink=True))
                 break
             new_text = self.doReplacements(new_text)
             if new_text == original_text:
                 pywikibot.output(u'No changes were necessary in %s' %
                                  page.title(asLink=True))
                 break
             if self.recursive:
                 newest_text = self.doReplacements(new_text)
                 while (newest_text != new_text):
                     new_text = newest_text
                     newest_text = self.doReplacements(new_text)
             if hasattr(self, "addedCat"):
                 cats = page.categories()
                 if self.addedCat not in cats:
                     cats.append(self.addedCat)
                     new_text = pywikibot.replaceCategoryLinks(
                         new_text, cats)
             # 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(original_text, new_text)
             if self.acceptall:
                 break
             choice = pywikibot.inputChoice(
                 u'Do you want to accept these changes?',
                 ['Yes', 'No', 'Edit', 'open in Browser', 'All', 'Quit'],
                 ['y', 'N', 'e', 'b', 'a', 'q'], 'N')
             if choice == 'e':
                 editor = editarticle.TextEditor()
                 as_edited = editor.edit(original_text)
                 # if user didn't press Cancel
                 if as_edited and as_edited != new_text:
                     new_text = as_edited
                 continue
             if choice == 'b':
                 webbrowser.open(
                     "http://%s%s" %
                     (page.site().hostname(), page.site().nice_get_address(
                         page.title())))
                 pywikibot.input("Press Enter when finished in browser.")
                 original_text = page.get(get_redirect=True, force=True)
                 new_text = original_text
                 continue
             if choice == 'q':
                 self.writeEditCounter()
                 return
             if choice == 'a':
                 self.acceptall = True
             if choice == 'y':
                 if not self.articles:
                     #Primary behaviour: working on wiki
                     page.put_async(new_text, self.editSummary)
                     self.editcounter += 1
                     #Bug: this increments even if put_async fails
                     #This is separately in two clauses of if for
                     #future purposes to get feedback form put_async
                 else:
                     #Save the title for later processing instead of editing
                     self.editcounter += 1
                     self.articles.write(
                         u'#%s\n%s' %
                         (page.title(asLink=True), self.splitLine()))
                     self.articles.flush()  # For the peace of our soul :-)
             # choice must be 'N'
             break
         if self.acceptall and new_text != original_text:
             if not self.articles:
                 #Primary behaviour: working on wiki
                 try:
                     page.put(new_text, self.editSummary)
                     self.editcounter += 1  #increment only on success
                 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.PageNotSaved, error:
                     pywikibot.output(u'Error putting page: %s' %
                                      (error.args, ))
                 except pywikibot.LockedPage:
                     pywikibot.output(u'Skipping %s (locked page)' %
                                      (page.title(), ))
Exemple #8
0
class SelflinkBot:
    def __init__(self, generator, always=False):
        self.generator = generator
        linktrail = pywikibot.getSite().linktrail()
        # The regular expression which finds links. Results consist of four groups:
        # group title is the target page title, that is, everything before | or ].
        # group section is the page section. It'll include the # to make life easier for us.
        # group label is the alternative link title, that's everything between | and ].
        # group linktrail is the link trail, that's letters after ]] which are part of the word.
        # note that the definition of 'letter' varies from language to language.
        self.linkR = re.compile(
            r'\[\[(?P<title>[^\]\|#]*)(?P<section>#[^\]\|]*)?(\|(?P<label>[^\]]*))?\]\](?P<linktrail>'
            + linktrail + ')')
        self.always = always
        self.done = False

    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

        # Check whether the link found is to the current page itself.
        if linkedPage != page:
            # 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.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 selflink?\n', [
                        'unlink', 'make bold', 'skip', 'edit', 'more context',
                        'unlink all', 'quit'
                    ], ['U', 'b', 's', 'e', 'm', 'a', 'q'], 'u')
                pywikibot.output(u'')

                if choice == 's':
                    # skip this link
                    return text, False
                elif choice == 'e':
                    editor = editarticle.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(page,
                                               text,
                                               match,
                                               context=context + 100)
                elif choice == 'a':
                    self.always = True
                elif choice == 'q':
                    self.done = True
                    return text, False
            new = match.group('label') or match.group('title')
            new += match.group('linktrail')
            if choice == 'b':
                # make bold
                return text[:match.start(
                )] + "'''" + new + "'''" + text[match.end():], False
            else:
                return text[:match.start()] + new + text[match.end():], False
Exemple #9
0
    def handlebadpage(self):
        try:
            self.content = self.page.get()
        except pywikibot.IsRedirectPage:
            pywikibot.output(u'Already redirected, skipping.')
            return
        except pywikibot.NoPage:
            pywikibot.output(u'Already deleted')
            return

        for d in pywikibot.translate(pywikibot.getSite(), done):
            if d in self.content:
                pywikibot.output(
                    u'Found: "%s" in content, nothing necessary' % d)
                return
        print "---- Start content ----------------"
        pywikibot.output(u"%s" % self.content)
        print "---- End of content ---------------"

        # Loop other user answer
        answered = False
        while not answered:
            answer = pywikibot.input(question)

            if answer == 'q':
                sys.exit("Exiting")
            if answer == 'd':
                pywikibot.output(u'Trying to delete page [[%s]].'
                                 % self.page.title())
                self.page.delete()
                return
            if answer == 'e':
                oldText = self.page.get()
                text = oldText
                editor = editarticle.TextEditor()
                text = editor.edit(self.page.get())
                if oldText != text:
                    pywikibot.showDiff(oldText, text)
                    msg = pywikibot.input(u'Summary message:')
                    self.page.put(text, msg)
                return
            if answer == 'b':
                pywikibot.output(u'Blanking page [[%s]].' % self.page.title())
                try:
                    self.page.put('',
                                  comment=pywikibot.translate(
                                      pywikibot.getSite(), blanking)
                                  % self.content)
                except EditConflict:
                    print "An edit conflict occured ! Automatically retrying"
                    handlebadpage(self)
                return
            if answer == '':
                print 'Page correct ! Proceeding with next pages.'
                return
            # Check user input:
            if answer[0] == 'u':
                # Answer entered as an utf8 string
                try:
                    choices = answer[1:].split(',')
                except ValueError:
                    # User entered wrong value
                    pywikibot.error(u'"%s" is not valid' % answer)
                    continue
            else:
                try:
                    choices = answer.split(',')
                except ValueError:
                    # User entered wrong value
                    pywikibot.error(u'"%s" is not valid' % answer)
                    continue
            #test input
            for choice in choices:
                try:
                    x = int(choice)
                except ValueError:
                    break
                else:
                    answered = x in range(1, len(questionlist)+1)
            if not answered:
                pywikibot.error(u'"%s" is not valid' % answer)
                continue
        summary = u''
        for choice in choices:
            answer = int(choice)
            # grab the template parameters
            tpl = pywikibot.translate(pywikibot.getSite(),
                                      templates)[questionlist[answer]]
            if tpl['pos'] == 'top':
                pywikibot.output(u'prepending %s...' % questionlist[answer])
                self.content = questionlist[answer] + '\n' + self.content
            elif tpl['pos'] == 'bottom':
                pywikibot.output(u'appending %s...' % questionlist[answer])
                self.content += '\n' + questionlist[answer]
            else:
                pywikibot.error(
                    u'"pos" should be "top" or "bottom" for template '
                    u'%s. Contact a developer.' % questionlist[answer])
                sys.exit("Exiting")
            summary += tpl['msg']+' '
            pywikibot.output(u'Probably added %s' % questionlist[answer])
#        pywikibot.output(newcontent) bug #2986247
        self.page.put(self.content, comment=summary)
        pywikibot.output(u'with comment %s\n' % summary)
Exemple #10
0
    def treat(self, refPage, disambPage):
        """
        Parameters:
            disambPage - The disambiguation page or redirect we don't want
                anything to link to
            refPage - A page linking to disambPage
        Returns False if the user pressed q to completely quit the program.
        Otherwise, returns True.

        """
        # TODO: break this function up into subroutines!

        dn_template_str = pywikibot.translate(self.mysite, dn_template)
        include = False
        unlink = False
        new_targets = []
        try:
            text = refPage.get(throttle=False)
            ignoreReason = self.checkContents(text)
            if ignoreReason:
                pywikibot.output(
                    '\n\nSkipping %s because it contains %s.\n\n' %
                    (refPage.title(), ignoreReason))
            else:
                include = True
        except pywikibot.IsRedirectPage:
            pywikibot.output(u'%s is a redirect to %s' %
                             (refPage.title(), disambPage.title()))
            if disambPage.isRedirectPage():
                target = self.alternatives[0]
                choice = pywikibot.inputChoice(
                    u'Do you want to make redirect %s point to %s?' %
                    (refPage.title(), target), ['yes', 'no'], ['y', 'N'], 'N')
                if choice == 'y':
                    redir_text = '#%s [[%s]]' \
                                 % (self.mysite.redirect(default=True), target)
                    try:
                        refPage.put_async(redir_text, comment=self.comment)
                    except pywikibot.PageNotSaved as error:
                        pywikibot.output(u'Page not saved: %s' % error.args)
            else:
                choice = pywikibot.inputChoice(
                    u'Do you want to work on pages linking to %s?' %
                    refPage.title(), ['yes', 'no', 'change redirect'],
                    ['y', 'N', 'c'], 'N')
                if choice == 'y':
                    gen = ReferringPageGeneratorWithIgnore(
                        refPage, self.primary)
                    preloadingGen = pagegenerators.PreloadingGenerator(gen)
                    for refPage2 in preloadingGen:
                        # run until the user selected 'quit'
                        if not self.treat(refPage2, refPage):
                            break
                elif choice == 'c':
                    text = refPage.get(throttle=False, get_redirect=True)
                    include = "redirect"
        except pywikibot.NoPage:
            pywikibot.output(
                u'Page [[%s]] does not seem to exist?! Skipping.' %
                refPage.title())
            include = False
        if include in (True, "redirect"):
            # make a backup of the original text so we can show the changes later
            original_text = text
            n = 0
            curpos = 0
            dn = False
            edited = False
            # This loop will run until we have finished the current page
            while True:
                m = self.linkR.search(text, pos=curpos)
                if not m:
                    if n == 0:
                        pywikibot.output(u"No changes necessary in %s" %
                                         refPage.title())
                        return True
                    else:
                        # stop loop and save page
                        break
                # Make sure that next time around we will not find this same hit.
                curpos = m.start() + 1
                # ignore interwiki links and links to sections of the same page
                if m.group('title') == '' or self.mysite.isInterwikiLink(
                        m.group('title')):
                    continue
                else:
                    try:
                        linkPage = pywikibot.Page(disambPage.site(),
                                                  m.group('title'))
                        # Check whether the link found is to disambPage.
                    except pywikibot.InvalidTitle:
                        continue
                    if linkPage != disambPage:
                        continue

                n += 1
                # how many bytes should be displayed around the current link
                context = 60
                #there's a {{dn}} here already
                already_dn = text[m.end():m.end() + 8].find(
                    dn_template_str[:4]) > -1
                if already_dn and self.dnSkip:
                    continue

                # This loop will run while the user doesn't choose an option
                # that will actually change the page
                while True:
                    # Show the title of the page where the link was found.
                    # Highlight the title in purple.
                    pywikibot.output(
                        u"\n\n>>> \03{lightpurple}%s\03{default} <<<" %
                        refPage.title())

                    if not self.always:
                        # at the beginning of the link, start red color.
                        # at the end of the link, reset the color to default
                        pywikibot.output(text[max(0,
                                                  m.start() -
                                                  context):m.start()] +
                                         '\03{lightred}' +
                                         text[m.start():m.end()] +
                                         '\03{default}' +
                                         text[m.end():m.end() + context])
                        if edited:
                            choice = pywikibot.input(
                                u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n"
                                u"        [t]ag template " + dn_template_str +
                                ",\n"
                                u"        [m]ore context, [l]ist, [a]dd new, x=save in this form):"
                            )
                        else:
                            choice = pywikibot.input(
                                u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n"
                                u"        [t]ag template " + dn_template_str +
                                ",\n"
                                u"        [m]ore context, show [d]isambiguation page, [l]ist, [a]dd new):"
                            )
                    else:
                        choice = self.always
                    if choice in ['a', 'A']:
                        newAlternative = pywikibot.input(u'New alternative:')
                        self.alternatives.append(newAlternative)
                        self.listAlternatives()
                    elif choice in ['e', 'E']:
                        editor = editarticle.TextEditor()
                        newText = editor.edit(text,
                                              jumpIndex=m.start(),
                                              highlight=disambPage.title())
                        # if user didn't press Cancel
                        if newText and newText != text:
                            text = newText
                            break
                    elif choice in ['d', 'D']:
                        editor = editarticle.TextEditor()
                        if disambPage.isRedirectPage():
                            disambredir = disambPage.getRedirectTarget()
                            disambigText = editor.edit(
                                disambredir.get(),
                                jumpIndex=m.start(),
                                highlight=disambredir.title())
                        else:
                            editor.edit(disambPage.get(),
                                        jumpIndex=m.start(),
                                        highlight=disambPage.title())
                    elif choice in ['l', 'L']:
                        self.listAlternatives()
                    elif choice in ['m', 'M']:
                        # show more text around the link we're working on
                        context *= 2
                    else:
                        break

                if choice in ['e', 'E']:
                    # user has edited the page and then pressed 'OK'
                    edited = True
                    curpos = 0
                    continue
                elif choice in ['n', 'N']:
                    # skip this page
                    if self.primary:
                        # If run with the -primary argument, skip this
                        # occurence next time.
                        self.primaryIgnoreManager.ignore(refPage)
                    return True
                elif choice in ['q', 'Q']:
                    # quit the program
                    return False
                elif choice in ['s', 'S']:
                    # Next link on this page
                    n -= 1
                    continue
                elif choice in ['x', 'X'] and edited:
                    # Save the page as is
                    break

                # The link looks like this:
                # [[page_title|link_text]]trailing_chars
                page_title = m.group('title')
                link_text = m.group('label')

                if not link_text:
                    # or like this: [[page_title]]trailing_chars
                    link_text = page_title
                if m.group('section') is None:
                    section = ''
                else:
                    section = m.group('section')
                trailing_chars = m.group('linktrail')
                if trailing_chars:
                    link_text += trailing_chars
                # '?', '/' for old choice
                if choice in ['t', 'T', '?', '/']:
                    # small chunk of text to search
                    search_text = text[m.end():m.end() + context]
                    # figure out where the link (and sentance) ends, put note
                    # there
                    end_of_word_match = re.search("\s", search_text)
                    if end_of_word_match:
                        position_split = end_of_word_match.start(0)
                    else:
                        position_split = 0
                    #insert dab needed template
                    text = (text[:m.end() + position_split] + dn_template_str +
                            text[m.end() + position_split:])
                    dn = True
                    continue
                elif choice in ['u', 'U']:
                    # unlink - we remove the section if there's any
                    text = text[:m.start()] + link_text + text[m.end():]
                    unlink = True
                    continue
                else:
                    if len(choice) > 0 and choice[0] == 'r':
                        # we want to throw away the original link text
                        replaceit = True
                        choice = choice[1:]
                    elif include == "redirect":
                        replaceit = True
                    else:
                        replaceit = False

                    try:
                        choice = int(choice)
                    except ValueError:
                        pywikibot.output(u"Unknown option")
                        # step back to ask the user again what to do with the
                        # current link
                        curpos -= 1
                        continue
                    if choice >= len(self.alternatives) or choice < 0:
                        pywikibot.output(
                            u"Choice out of range. Please select a number "
                            u"between 0 and %i." %
                            (len(self.alternatives) - 1))
                        # show list of possible choices
                        self.listAlternatives()
                        # step back to ask the user again what to do with the
                        # current link
                        curpos -= 1
                        continue
                    new_page_title = self.alternatives[choice]
                    repPl = pywikibot.Page(disambPage.site(), new_page_title)
                    if (new_page_title[0].isupper() or link_text[0].isupper()):
                        new_page_title = repPl.title()
                    else:
                        new_page_title = repPl.title()
                        new_page_title = (new_page_title[0].lower() +
                                          new_page_title[1:])
                    if new_page_title not in new_targets:
                        new_targets.append(new_page_title)
                    if replaceit and trailing_chars:
                        newlink = "[[%s%s]]%s" % (new_page_title, section,
                                                  trailing_chars)
                    elif replaceit or (new_page_title == link_text
                                       and not section):
                        newlink = "[[%s]]" % new_page_title
                    # check if we can create a link with trailing characters
                    # instead of a pipelink
                    elif ((len(new_page_title) <= len(link_text))
                          and (firstcap(link_text[:len(new_page_title)])
                               == firstcap(new_page_title))
                          and (re.sub(self.trailR, '',
                                      link_text[len(new_page_title):]) == '')
                          and (not section)):
                        newlink = "[[%s]]%s" \
                                  % (link_text[:len(new_page_title)],
                                     link_text[len(new_page_title):])
                    else:
                        newlink = "[[%s%s|%s]]" \
                                  % (new_page_title, section, link_text)
                    text = text[:m.start()] + newlink + text[m.end():]
                    continue

                pywikibot.output(text[max(0, m.start() - 30):m.end() + 30])
            if text == original_text:
                pywikibot.output(u'\nNo changes have been made:\n')
            else:
                pywikibot.output(u'\nThe following changes have been made:\n')
                pywikibot.showDiff(original_text, text)
                pywikibot.output(u'')
                # save the page
                self.setSummaryMessage(disambPage, new_targets, unlink, dn)
                try:
                    refPage.put_async(text, comment=self.comment)
                except pywikibot.LockedPage:
                    pywikibot.output(u'Page not saved: page is locked')
                except pywikibot.PageNotSaved as error:
                    pywikibot.output(u'Page not saved: %s' % error.args)
        return True
Exemple #11
0
def main():
    automatic = False
    namespaces = []
    msg = {
        'ar': u'إزالة الوصلات إلى موقع سبام %s',
        'de': u'Entferne in Spam-Blacklist eingetragenen Weblink auf %s',
        'en': u'Removing links to spamming site %s',
        'es': u'Removiendo enlaces a sitio publicitario %s',
        'fa': u'حذف پیوند به وبگاه هرزنگاری %s',
        'he': u'מסיר קישורים לאתר ספאם %s',
        'fr': u'Suppression du lien blacklisté %s',
        'it': u'Rimuovo link contenuto nella Spam-Blacklist %s',
        'ja': u'ロボットによる: 迷惑リンク削除 %s',
        'nl': u'Links naar gespamde site: %s verwijderd',
        'pt': u'Removendo links de spam do site %s',
        'ta': u'எரிதமாக இணைக்கப்பட்ட %s இணையத்தளம் நீக்கப்பட்டது',
        'vi': u'xóa các liên kết đến website spam %s',
        'zh': u'機器人: 移除廣告黑名單連結 %s',
    }
    spamSite = ''
    for arg in pywikibot.handleArgs():
        if arg.startswith("-automatic"):
            automatic = True
        elif arg.startswith('-namespace:'):
            try:
                namespaces.append(int(arg[len('-namespace:'):]))
            except ValueError:
                namespaces.append(arg[len('-namespace:'):])
        else:
            spamSite = arg
    if not automatic:
        pywikibot.put_throttle.setDelay(1)
    if not spamSite:
        pywikibot.showHelp('spamremove')
        pywikibot.output(u"No spam site specified.")
        sys.exit()
    mysite = pywikibot.getSite()
    pages = list(set(mysite.linksearch(spamSite)))
    if namespaces:
        pages = list(
            set(pagegenerators.NamespaceFilterPageGenerator(pages,
                                                            namespaces)))
    if len(pages) == 0:
        pywikibot.output('No page found.')
    else:
        pywikibot.getall(mysite, pages)
        for p in pages:
            text = p.get()
            if spamSite not in text:
                continue
            # 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} <<<" %
                             p.title())
            lines = text.split('\n')
            newpage = []
            lastok = ""
            for line in lines:
                if spamSite in line:
                    if lastok:
                        pywikibot.output(lastok)
                    pywikibot.output('\03{lightred}%s\03{default}' % line)
                    lastok = None
                else:
                    newpage.append(line)
                    if line.strip():
                        if lastok is None:
                            pywikibot.output(line)
                        lastok = line
            if automatic:
                answer = "y"
            else:
                answer = pywikibot.inputChoice(u'\nDelete the red lines?',
                                               ['yes', 'no', 'edit'],
                                               ['y', 'N', 'e'], 'n')
            if answer == "n":
                continue
            elif answer == "e":
                editor = editarticle.TextEditor()
                newtext = editor.edit(text,
                                      highlight=spamSite,
                                      jumpIndex=text.find(spamSite))
            else:
                newtext = "\n".join(newpage)
            if newtext != text:
                p.put(newtext, pywikibot.translate(mysite, msg) % spamSite)