Esempio n. 1
0
def main():
    pywikibot.handleArgs()

    page = pywikibot.Page(pywikibot.Site(), cfdPage)

    # Variable declarations
    day = "None"
    mode = "None"
    summary = ""
    robot = None

    m = ReCheck()
    for line in page.text.split("\n"):
        if nobots.search(line):
            # NO BOTS!!!
            pass
        elif example.search(line):
            # Example line
            pass
        elif speedymode.search(line):
            mode = "Speedy"
            day = "None"
        elif movemode.search(line):
            mode = "Move"
            day = "None"
        elif emptymode.search(line):
            mode = "Empty"
            day = "None"
        elif deletemode.search(line):
            mode = "Delete"
            day = "None"
        elif maintenance.search(line):
            # It's probably best not to try to handle these in an automated fashion.
            mode = "None"
            day = "None"
        elif m.check(dateheader, line):
            day = m.result.group(1)
            pywikibot.output("Found day header: %s" % day)
        elif m.check(movecat, line):
            src = m.result.group(1)
            dest = m.result.group(2)
            thisDay = findDay(src, day)
            if mode == "Move" and thisDay != "None":
                summary = "Robot - Moving category " + src + " to [[:Category:" + dest + "]] per [[WP:CFD|CFD]] at " + \
                          thisDay + "."
            elif mode == "Speedy":
                summary = "Robot - Speedily moving category " + src + " to [[:Category:" + dest + \
                          "]] per [[WP:CFDS|CFDS]]."
            else:
                continue
            # If the category is redirect, we do NOT want to move articles to
            # it. The safest thing to do here is abort and wait for human
            # intervention.
            destpage = pywikibot.Page(
                pywikibot.Site(), dest, ns=14)
            if destpage.isCategoryRedirect():
                summary = 'CANCELED. Destination is redirect: ' + summary
                pywikibot.output(summary, toStdout=True)
                robot = None
            else:
                robot = category.CategoryMoveRobot(oldcat=src, newcat=dest, batch=True,
                                                   comment=summary, inplace=True, move_oldcat=True,
                                                   delete_oldcat=True, deletion_comment=True)
        elif m.check(deletecat, line):
            src = m.result.group(1)
            # I currently don't see any reason to handle these two cases separately, though
            # if are guaranteed that the category in the "Delete" case is empty, it might be
            # easier to call delete.py on it.
            thisDay = findDay(src, day)
            if (mode == "Empty" or mode == "Delete") and thisDay != "None":
                summary = "Robot - Removing category " + src + " per [[WP:CFD|CFD]] at " + thisDay + "."
            else:
                continue
            robot = category.CategoryMoveRobot(oldcat=src, batch=True, comment=summary,
                                                 deletion_comment=True, inplace=True)
        else:
            # This line does not fit any of our regular expressions, so ignore it.
            pass
        if summary != "" and robot is not None:
            pywikibot.output(summary, toStdout=True)
            # Run, robot, run!
            robot.run()
        summary = ""
        robot = None
Esempio n. 2
0
    def Run(self):
        try:
            count = 0
            lastLetter = ''
            for namespace in self.namespaces:
                self.params['gapnamespace'] = namespace
                title = None

                while True:
                    # Get data
                    self.params['gapfrom'] = self.apfrom
                    data = query.GetData(self.params, self.site)
                    try:
                        self.apfrom = data['query-continue']['allpages'][
                            'gapfrom']
                    except:
                        self.apfrom = None

                    # Process received data
                    if 'query' in data and 'pages' in data['query']:
                        firstItem = True
                        for pageID, page in data['query']['pages'].iteritems():
                            printed = False
                            title = page['title']
                            if firstItem:
                                if lastLetter != title[0]:
                                    try:
                                        print 'Processing ' + title
                                    except:
                                        print 'Processing unprintable title'
                                    lastLetter = title[0]
                                firstItem = False
                            if self.titles:
                                err = self.ProcessTitle(title)
                                if err:
                                    changed = False
                                    if self.replace:
                                        newTitle = self.PickTarget(
                                            False, title, title, err[1])
                                        if newTitle:
                                            editSummary = wikipedia.translate(
                                                self.site, self.msgRename)
                                            src = wikipedia.Page(
                                                self.site, title)
                                            if page['ns'] == 14:
                                                import category
                                                dst = wikipedia.Page(
                                                    self.site, newTitle)
                                                bot = category.CategoryMoveRobot(
                                                    src.titleWithoutNamespace(
                                                    ),
                                                    dst.titleWithoutNamespace(
                                                    ), self.autonomous,
                                                    editSummary, True)
                                                bot.run()
                                            else:
                                                src.move(newTitle, editSummary)
                                            changed = True

                                    if not changed:
                                        self.WikiLog(u"* " + err[0])
                                        printed = True

                            if self.links:
                                allLinks = None
                                if 'links' in page:
                                    allLinks = page['links']
                                if 'categories' in page:
                                    if allLinks:
                                        allLinks = allLinks + page['categories']
                                    else:
                                        allLinks = page['categories']

                                if allLinks:
                                    pageObj = None
                                    pageTxt = None
                                    msg = []

                                    for l in allLinks:
                                        ltxt = l['title']
                                        err = self.ProcessTitle(ltxt)
                                        if err:
                                            newTitle = None
                                            if self.replace:
                                                newTitle = self.PickTarget(
                                                    True, title, ltxt, err[1])
                                                if newTitle:
                                                    if pageObj is None:
                                                        pageObj = wikipedia.Page(
                                                            self.site, title)
                                                        pageTxt = pageObj.get()
                                                    msg.append(
                                                        u'[[%s]] => [[%s]]' %
                                                        (ltxt, newTitle))
                                                    #                                                    pageTxt = pageTxt.replace(ltxt, newTitle)
                                                    #                                                    pageTxt = pageTxt.replace(ltxt[0].lower() + ltxt[1:], newTitle[0].lower() + newTitle[1:])
                                                    #                                                    pageTxt = pageTxt.replace(ltxt.replace(u' ', '_'), newTitle)

                                                    frmParts = self.wordBreaker.split(
                                                        ltxt)
                                                    toParts = self.wordBreaker.split(
                                                        newTitle)
                                                    if len(frmParts) != len(
                                                            toParts):
                                                        raise ValueError(
                                                            u'Splitting parts do not match counts'
                                                        )
                                                    for i in range(
                                                            0, len(frmParts)):
                                                        if len(frmParts[i]
                                                               ) != len(
                                                                   toParts[i]):
                                                            raise ValueError(
                                                                u'Splitting parts do not match word length'
                                                            )
                                                        if len(frmParts[i]
                                                               ) > 0:
                                                            pageTxt = pageTxt.replace(
                                                                frmParts[i],
                                                                toParts[i])
                                                            pageTxt = pageTxt.replace(
                                                                frmParts[i]
                                                                [0].lower() +
                                                                frmParts[i]
                                                                [1:],
                                                                toParts[i]
                                                                [0].lower() +
                                                                toParts[i][1:])

                                            if not newTitle:
                                                if not printed:
                                                    self.WikiLog(
                                                        u"* [[:%s]]: link to %s"
                                                        % (title, err[0]))
                                                    printed = True
                                                else:
                                                    self.WikiLog(
                                                        u"** link to %s" %
                                                        err[0])

                                    if pageObj is not None:
                                        coloredMsg = u', '.join([
                                            self.ColorCodeWord(m) for m in msg
                                        ])
                                        if pageObj.get() == pageTxt:
                                            self.WikiLog(
                                                u"* Error: Text replacement failed in [[:%s]] (%s)"
                                                % (title, coloredMsg))
                                        else:
                                            wikipedia.output(
                                                u'Case Replacements: %s' %
                                                u', '.join(msg))
                                            try:
                                                pageObj.put(
                                                    pageTxt, u'%s: %s' %
                                                    (wikipedia.translate(
                                                        self.site,
                                                        self.msgLinkReplacement
                                                    ), u', '.join(msg)))
                                            except KeyboardInterrupt:
                                                raise
                                            except:
                                                self.WikiLog(
                                                    u"* Error: Could not save updated page [[:%s]] (%s)"
                                                    % (title, coloredMsg))

                            count += 1
                            if self.stopAfter > 0 and count == self.stopAfter:
                                raise "Stopping because we are done"

                    if self.apfrom is None:
                        break

                self.apfrom = u''  # Restart apfrom for other namespaces

            print "***************************** Done"

        except:
            if self.apfrom is not None:
                wikipedia.output(u'Exception at Title = %s, Next = %s' %
                                 (title, self.apfrom))
            raise
Esempio n. 3
0
    def ProcessDataBlock(self, data):
        if 'query' not in data or 'pages' not in data['query']:
            return

        firstItem = True
        for pageID, page in data['query']['pages'].items():
            printed = False
            title = page['title']
            self.currentTitle = title
            if 'missing' in page:
                continue
            if firstItem:
                if self.lastLetter != title[0]:
                    pywikibot.ui.output('Processing %s\n' % title)
                    self.lastLetter = title[0]
                firstItem = False
            if self.titles:
                err = self.ProcessTitle(title)
                if err:
                    changed = False
                    if self.replace:
                        if len(err[1]) == 1:
                            newTitle = err[1][0]
##                            choice = pywikibot.inputChoice(u'Move %s to %s?'
##                                                           % (title, newTitle),
##                                                           ['Yes', 'No'],
##                                                           ['y', 'n'])
                            editSummary = i18n.twtranslate(
                                self.site, "casechecker-rename")
                            dst = self.Page(newTitle)

                            if 'redirect' in page:
                                src = self.Page(title)
                                redir = src.getRedirectTarget()
                                redirTitle = redir.title(asLink=True,
                                                         textlink=True)

                                if not dst.exists():
                                    src.move(newTitle, editSummary,
                                             movesubpages=True)
                                    changed = True

                                replErrors = False
                                for p in src.getReferences(
                                        follow_redirects=False):
                                    if p.namespace() == 2:
                                        continue
                                    oldText = p.text
                                    newText = self.ReplaceLink(oldText, title,
                                                               newTitle)
                                    if not self.PutNewPage(
                                        p, newText, [
                                            self.MakeMoveSummary(title,
                                                                 newTitle)]):
                                        replErrors = True
                                if not replErrors:
                                    editSummary = i18n.twtranslate(
                                        self.site, "casechecker-delete-summary")
                                    newText = i18n.twtranslate(
                                        self.site,
                                        "casechecker-delete-reason", redirTitle)
                                    if newText:
                                        src.text = u'{{delete}}\n\n' + newText
                                        src.save(editSummary, minor=False)
                                        changed = True

                            elif not dst.exists():
                                src = self.Page(title)
                                if page['ns'] == 14:
                                    import category
                                    dst = self.Page(newTitle)
                                    bot = category.CategoryMoveRobot(
                                        src.title(withNamespace=False),
                                        dst.title(withNamespace=False),
                                        self.autonomous,
                                        editSummary + u' ' +
                                        self.MakeMoveSummary(title, newTitle),
                                        True)
                                    bot.run()
                                else:
                                    src.move(newTitle, editSummary,
                                             movesubpages=True)
                                changed = True

                    if not changed:
                        if len(err[1]) > 0:
                            self.AppendLineToLog(self.failedTitles, title)
                        else:
                            self.AddNoSuggestionTitle(title)

                        self.WikiLog(u"* " + err[0])
                        printed = True

            if self.links:
                allLinks = None
                if 'links' in page:
                    allLinks = page['links']
                if 'categories' in page:
                    if allLinks:
                        allLinks = allLinks + page['categories']
                    else:
                        allLinks = page['categories']

                if allLinks:
                    pageObj = None
                    pageTxt = None
                    msg = []
                    foundSuggestions = False

                    for l in allLinks:
                        ltxt = l['title']
                        err = self.ProcessTitle(ltxt)
                        if err:
                            if len(err[1]) > 0:
                                foundSuggestions = True
                            elif self.AddNoSuggestionTitle(ltxt):
                                continue

                            newTitle = None
                            if self.replace:
                                newTitle = self.PickTarget(title, ltxt, err[1])
                                if newTitle:
                                    if pageObj is None:
                                        pageObj = self.Page(title)
                                        pageTxt = pageObj.get()

                                    msg.append(self.MakeMoveSummary(ltxt,
                                                                    newTitle))

                                    pageTxt = self.ReplaceLink(pageTxt, ltxt,
                                                               newTitle)
                            if not newTitle:
                                if not printed:
                                    self.WikiLog(u"* %s: link to %s"
                                                 % (self.MakeLink(title, False),
                                                    err[0]))
                                    printed = True
                                else:
                                    self.WikiLog(u"** link to %s" % err[0])
                    if pageObj is not None:
                        if self.PutNewPage(pageObj, pageTxt, msg):
                                # done, no need to log anything
                                foundSuggestions = False

                    if foundSuggestions:
                        self.AppendLineToLog(self.failedTitles, title)
            if self.stopAfter > 0:
                self.stopAfter -= 1
                if self.stopAfter == 0:
                    raise ValueError(u'Stopping because we are done')
Esempio n. 4
0
def main():
    wikipedia.handleArgs();

    page = wikipedia.Page(wikipedia.getSite(), cfdPage)

    # Variable declarations
    day = "None"
    mode = "None"
    src = "None"
    dest = "None"
    line = ""
    summary = ""
    robot = None

    m = ReCheck()
    for line in page.get().split("\n"):
        if (nobots.search(line)):
            # NO BOTS!!!
            pass
        elif (example.search(line)):
            # Example line
            pass
        elif (speedymode.search(line)):
            mode = "Speedy"
            day = "None"
        elif (movemode.search(line)):
            mode = "Move"
            day = "None"
        elif (emptymode.search(line)):
            mode = "Empty"
            day = "None"
        elif (deletemode.search(line)):
            mode = "Delete"
            day = "None"
        elif (maintenance.search(line)):
            # It's probably best not to try to handle these in an automated fashion.
            mode = "None"
            day = "None"
        elif (m.check(dateheader, line)):
            day = m.result.group(1)
        elif (m.check(movecat, line)):
            src = m.result.group(1)
            dest = m.result.group(2)
            if (mode == "Move" and day != "None"):
                summary = "Robot - Moving category " + src + " to " + dest + " per [[WP:CFD|CFD]] at " + findDay(src, day) + "."
            elif (mode == "Speedy"):
                summary = "Robot - Speedily moving category " + src + " to " + dest + " per [[WP:CFD|CFD]]."
            else:
                continue
            # If the category is redirect, we do NOT want to move articles to
            # it. The safest thing to do here is abort and wait for human
            # intervention.
            destpage = wikipedia.Page(
                wikipedia.getSite(), dest, defaultNamespace=14)
            if destpage.isCategoryRedirect():
                summary = 'CANCELED. Destination is redirect: ' + summary
                wikipedia.output(summary, toStdout=True)
                robot = None
            else:
                robot = category.CategoryMoveRobot(oldCatTitle=src, newCatTitle=dest, batchMode=True,
                                                   editSummary=summary, inPlace=True, moveCatPage=True,
                                                   deleteEmptySourceCat=True)
        elif (m.check(deletecat, line)):
            src = m.result.group(1)
            # I currently don't see any reason to handle these two cases separately, though
            # if are guaranteed that the category in the "Delete" case is empty, it might be
            # easier to call delete.py on it.
            if ((mode == "Empty" or mode == "Delete") and day != "None"):
                summary = "Robot - Removing category " + src + " per [[WP:CFD|CFD]] at " + findDay(src, day) + "."
            else:
                continue
            robot = category.CategoryRemoveRobot(catTitle=src, batchMode=True, editSummary=summary,
                                                 useSummaryForDeletion=True, inPlace=True)
        else:
            # This line does not fit any of our regular expressions, so ignore it.
            pass
        if (summary != "" and robot != None):
            wikipedia.output(summary, toStdout=True)
            # Run, robot, run!
            robot.run()
        summary = ""
        robot = None
Esempio n. 5
0
def main(*args):
    # The location of the CFD working page.
    cfdPage = 'Wikipedia:Categories for discussion/Working'
    for arg in pywikibot.handleArgs(*args):
        # Use a non-default CFD working page if one is specified from the command-line using -page.
        if arg.startswith('-page'):
            if len(arg) == len('-page'):
                cfdPage = pywikibot.input(
                    u'Enter the CFD working page to use:')
            else:
                cfdPage = arg[len('-page:'):].replace('_', ' ')

    page = pywikibot.Page(pywikibot.getSite(), cfdPage)

    # Variable declarations
    day = "None"
    mode = "None"
    src = "None"
    dest = "None"
    line = ""
    summary = ""
    robot = None

    m = ReCheck()
    for line in page.get().split("\n"):
        if nobots.search(line):
            # NO BOTS!!!
            pass
        elif example.search(line):
            # Example line
            pass
        elif speedymode.search(line):
            mode = "Speedy"
            day = "None"
        elif movemode.search(line):
            mode = "Move"
            day = "None"
        elif emptymode.search(line):
            mode = "Empty"
            day = "None"
        elif deletemode.search(line):
            mode = "Delete"
            day = "None"
        elif maintenance.search(line):
            # It's probably best not to try to handle these in an automated
            # fashion.
            mode = "None"
            day = "None"
        elif m.check(dateheader, line):
            day = m.result.group(1)
            pywikibot.output("Found day header: %s" % day)
        elif m.check(movecat, line):
            src = m.result.group(1)
            dest = m.result.group(2)
            thisDay = findDay(src, day)
            if mode == "Move" and thisDay != "None":
                summary = (
                    "Robot - Moving category %s to [[:Category:%s]] per "
                    "[[WP:CFD|CFD]] at %s.") % (src, dest, thisDay)
            elif mode == "Speedy":
                summary = ("Robot - Speedily moving category %s to "
                           "[[:Category:%s]] per [[WP:CFDS|CFDS]].") % (src,
                                                                        dest)
            else:
                continue
            # If the category is redirect, we do NOT want to move articles to
            # it. The safest thing to do here is abort and wait for human
            # intervention.
            destpage = pywikibot.Page(pywikibot.getSite(),
                                      dest,
                                      defaultNamespace=14)
            if destpage.isCategoryRedirect():
                summary = 'CANCELED. Destination is redirect: ' + summary
                pywikibot.output(summary, toStdout=True)
                robot = None
            else:
                robot = category.CategoryMoveRobot(oldCatTitle=src,
                                                   newCatTitle=dest,
                                                   batchMode=True,
                                                   editSummary=summary,
                                                   inPlace=True,
                                                   moveCatPage=True,
                                                   deleteEmptySourceCat=True,
                                                   useSummaryForDeletion=True)
        elif m.check(deletecat, line):
            src = m.result.group(1)
            # I currently don't see any reason to handle these two cases
            # separately, though if are guaranteed that the category in the
            # "Delete" case is empty, it might be easier to call delete.py on
            # it.
            thisDay = findDay(src, day)
            if (mode == "Empty" or mode == "Delete") and thisDay != "None":
                summary = ("Robot - Removing category %s per [[WP:CFD|CFD]] "
                           "at %s.") % (src, thisDay)
            else:
                continue
            robot = category.CategoryRemoveRobot(catTitle=src,
                                                 batchMode=True,
                                                 editSummary=summary,
                                                 useSummaryForDeletion=True,
                                                 inPlace=True)
        else:
            # This line does not fit any of our regular expressions, so ignore
            # it.
            pass
        if summary != "" and robot is not None:
            pywikibot.output(summary, toStdout=True)
            # Run, robot, run!
            robot.run()
        summary = ""
        robot = None