Exemple #1
0
 def PutNewPage(self, pageObj, pageTxt, msg):
     title = pageObj.title(asLink=True, textlink=True)
     coloredMsg = u', '.join([self.ColorCodeWord(m) for m in msg])
     if pageObj.get(get_redirect=True) == pageTxt:
         self.WikiLog(u"* Error: Text replacement failed in %s (%s)" %
                      (self.MakeLink(title, False), coloredMsg))
     else:
         pywikibot.output(u'Case Replacements: %s' % u', '.join(msg))
         try:
             pageObj.put(
                 pageTxt, u'%s: %s' %
                 (i18n.twtranslate(self.site,
                                   "casechecker-replacement-summary"),
                  self.site.mediawiki_message(u"comma-separator").join(msg))
             )
             return True
         except KeyboardInterrupt:
             raise
         except:
             self.WikiLog(u"* Error: Could not save updated page %s (%s)" %
                          (self.MakeLink(title, False), coloredMsg))
     return False
Exemple #2
0
 def PutNewPage(self, pageObj, pageTxt, msg):
     title = pageObj.title(asLink=True, textlink=True)
     coloredMsg = u', '.join([self.ColorCodeWord(m) for m in msg])
     if pageObj.get(get_redirect=True) == pageTxt:
         self.WikiLog(u"* Error: Text replacement failed in %s (%s)"
                      % (self.MakeLink(title, False), coloredMsg))
     else:
         pywikibot.output(u'Case Replacements: %s' % u', '.join(msg))
         try:
             pageObj.put(
                 pageTxt,
                 u'%s: %s'
                 % (i18n.twtranslate(
                     self.site,
                     "casechecker-replacement-summary"),
                     self.site.mediawiki_message(u"Comma-separator").join(msg)))
             return True
         except KeyboardInterrupt:
             raise
         except:
             self.WikiLog(u"* Error: Could not save updated page %s (%s)"
                          % (self.MakeLink(title, False), coloredMsg))
     return False
Exemple #3
0
 def MakeMoveSummary(self, fromTitle, toTitle):
     return i18n.twtranslate(self.site, "casechecker-replacement-linklist") % {'source': fromTitle, 'target': toTitle}
Exemple #4
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'].iteritems():
            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]
                            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.get(get_redirect=True)
                                    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,
                                        fallback=False)
                                    if newText:
                                        src.put(newText, editSummary,
                                                minorEdit=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')