Exemple #1
0
    def change_category(self,
                        article,
                        oldCat,
                        newCat,
                        comment=None,
                        sortKey=None):
        """Given an article in category oldCat, moves it to category newCat.
        Moves subcategories of oldCat as well. oldCat and newCat should be
        Category objects. If newCat is None, the category will be removed.

        This is a copy of portions of [old] catlib.change_category(), with
        some changes.

        """
        oldtext = article.get(get_redirect=True, force=True)
        if newCat in article.categories() or newCat == article:
            newtext = pywikibot.replaceCategoryInPlace(oldtext,
                                                       oldCat,
                                                       None,
                                                       site=self.site)
        else:
            newtext = pywikibot.replaceCategoryInPlace(oldtext,
                                                       oldCat,
                                                       newCat,
                                                       site=self.site)
        try:
            # even if no changes, still save the page, in case it needs
            # an update due to changes in a transcluded template
            article.put(newtext, comment)
            if newtext == oldtext:
                pywikibot.output(u'No changes made in page %s.' %
                                 article.title(asLink=True))
                return False
            return True
        except pywikibot.EditConflict:
            pywikibot.output(u'Skipping %s because of edit conflict' %
                             article.title(asLink=True))
        except pywikibot.LockedPage:
            pywikibot.output(u'Skipping locked page %s' %
                             article.title(asLink=True))
            self.edit_requests.append({
                'title':
                article.title(asLink=True, textlink=True),
                'oldcat':
                oldCat.title(asLink=True, textlink=True),
                'newcat':
                newCat.title(asLink=True, textlink=True)
            })
        except pywikibot.SpamfilterError, error:
            pywikibot.output(
                u'Changing page %s blocked by spam filter (URL=%s)' %
                (article.title(asLink=True), error.url))
    def change_category(self, article, oldCat, newCat, comment=None,
                        sortKey=None):
        """Given an article in category oldCat, moves it to category newCat.
        Moves subcategories of oldCat as well. oldCat and newCat should be
        Category objects. If newCat is None, the category will be removed.

        This is a copy of portions of [old] catlib.change_category(), with
        some changes.

        """
        oldtext = article.get(get_redirect=True, force=True)
        newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat)
        try:
            # even if no changes, still save the page, in case it needs
            # an update due to changes in a transcluded template
            article.put(newtext, comment)
            if newtext == oldtext:
                pywikibot.output(
                    u'No changes in made in page %s.' % article.aslink())
                return False
            return True
        except pywikibot.EditConflict:
            pywikibot.output(
                u'Skipping %s because of edit conflict' % article.aslink())
        except pywikibot.LockedPage:
            pywikibot.output(u'Skipping locked page %s' % article.aslink())
            self.edit_requests.append((article.aslink(),
                                       oldCat.aslink(textlink=True),
                                       newCat.aslink(textlink=True)))
        except pywikibot.SpamfilterError, error:
            pywikibot.output(
                u'Changing page %s blocked by spam filter (URL=%s)'
                             % (article.aslink(), error.url))
Exemple #3
0
def change_category(article, oldCat, newCat, comment=None, sortKey=None, inPlace=False):
    """
    Given an article which is in category oldCat, moves it to
    category newCat. Moves subcategories of oldCat as well.
    oldCat and newCat should be Category objects.
    If newCat is None, the category will be removed.
    """
    cats = article.categories(get_redirect=True)
    site = article.site()
    changesMade = False

    if not article.canBeEdited():
        wikipedia.output("Can't edit %s, skipping it..." % article.title(asLink=True))
        return False
    if inPlace or article.namespace() == 10:
        oldtext = article.get(get_redirect=True)
        newtext = wikipedia.replaceCategoryInPlace(oldtext, oldCat, newCat)
        if newtext == oldtext:
            wikipedia.output(u"No changes in made in page %s." % article.title(asLink=True))
            return False
        try:
            article.put(newtext, comment)
            return True
        except wikipedia.EditConflict:
            wikipedia.output(u"Skipping %s because of edit conflict" % article.title(asLink=True))
        except wikipedia.LockedPage:
            wikipedia.output(u"Skipping locked page %s" % article.title(asLink=True))
        except wikipedia.SpamfilterError, error:
            wikipedia.output(
                u"Changing page %s blocked by spam filter (URL=%s)" % (article.title(asLink=True), error.url)
            )
        except wikipedia.NoUsername:
            wikipedia.output(u"Page %s not saved; sysop privileges required." % article.title(asLink=True))
Exemple #4
0
def main():
  gen = pagegenerators.ImageGenerator(pagegenerators.NamespaceFilterPageGenerator(pagegenerators.CategorizedPageGenerator(source_cat),[6]))
  for page in gen:
    image = page
    page = wikipedia.Page(site, page.title())
    if not (image.getFileVersionHistory()[0][1] == u"Mattes"):
      continue
    cats = page.categories()
    text = ""
    if not oldcat in cats:
      continue
    g = False
    s = False
    if list(set(cats) & set(germany_cats)):
      g = True
    if list(set(cats) & set(switzerland_cats)):
      s = True
    if g or s:
      text = page.get()
      oldtext = text
      if g:
        if not re.search(ur"\{\{FoP-Germany\}\}", text, re.I):
          text = u"{{FoP-Germany}}\n" + text
          summary = ur"Bot: Added {{[[Template:FoP-Germany|FoP-Germany]]}}"
      if s:
        if not re.search(ur"\{\{FoP-Switzerland\}\}", text, re.I):
          text = u"{{FoP-Switzerland}}\n" + text
          summary = ur"Bot: Added {{[[Template:FoP-Switzerland|FoP-Switzerland]]}}"
      oldtext2 = text
      text = wikipedia.replaceCategoryInPlace(text, oldcat, None)
      if text != oldtext2:
        summary += ur"; removed [[:Category:FOP]]"
Exemple #5
0
def change_category(article,
                    oldCat,
                    newCat,
                    comment=None,
                    sortKey=None,
                    inPlace=False):
    """Given an article which is in category oldCat, moves it to
    category newCat. Moves subcategories of oldCat as well.
    oldCat and newCat should be Category objects.
    If newCat is None, the category will be removed.

    """
    cats = article.categories(get_redirect=True)
    site = article.site()
    changesMade = False

    if not article.canBeEdited():
        pywikibot.output("Can't edit %s, skipping it..." %
                         article.title(asLink=True))
        return
    if inPlace or article.namespace() == 10:
        oldtext = article.get(get_redirect=True)
        newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat)
        if newtext == oldtext:
            pywikibot.output(u'No changes in made in page %s.' %
                             article.title(asLink=True))
            return
        try:
            article.put(newtext, comment)
            return True
        except pywikibot.EditConflict:
            pywikibot.output(u'Skipping %s because of edit conflict' %
                             article.title(asLink=True))
        except pywikibot.LockedPage:
            pywikibot.output(u'Skipping locked page %s' %
                             article.title(asLink=True))
        except pywikibot.SpamfilterError, error:
            pywikibot.output(
                u'Changing page %s blocked by spam filter (URL=%s)' %
                (article.title(asLink=True), error.url))
        except pywikibot.NoUsername:
            pywikibot.output(u'Page %s not saved; sysop privileges required.' %
                             article.title(asLink=True))
Exemple #6
0
def change_category(article, oldCat, newCat, comment=None, sortKey=None, inPlace=False):
    """
    Remove page from oldCat and add it to newCat.

    @param oldCat and newCat: should be Category objects.
        If newCat is None, the category will be removed.

    @param comment: string to use as an edit summary

    @param sortKey: sortKey to use for the added category.
        Unused if newCat is None, or if inPlace=True

    @param inPlace: if True, change categories in place rather than
                  rearranging them.

    """
    cats = []

    # get list of Category objects the article is in and remove duplicates
    for cat in article.categories(get_redirect=True):
        if cat not in cats:
            cats.append(cat)

    site = article.site()

    if not sortKey:
        sortKey = oldCat.sortKey

    if not article.canBeEdited():
        pywikibot.output("Can't edit %s, skipping it..." % article.title(asLink=True))
        return

    if oldCat not in cats:
        pywikibot.error(u"%s is not in category %s!" % (article.title(asLink=True), oldCat.title()))
        return

    if inPlace or article.namespace() == 10:
        oldtext = article.get(get_redirect=True)
        newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat)
    else:
        if newCat:
            cats[cats.index(oldCat)] = Category(site, newCat.title(), sortKey=sortKey)
        else:
            cats.pop(cats.index(oldCat))
        oldtext = article.get(get_redirect=True)
        try:
            newtext = pywikibot.replaceCategoryLinks(oldtext, cats)
        except ValueError:
            # Make sure that the only way replaceCategoryLinks() can return
            # a ValueError is in the case of interwiki links to self.
            pywikibot.output(u"Skipping %s because of interwiki link to self" % article)

    if oldtext != newtext:
        try:
            article.put(newtext, comment)
        except pywikibot.EditConflict:
            pywikibot.output(u"Skipping %s because of edit conflict" % article.title())
        except pywikibot.SpamfilterError, e:
            pywikibot.output(u"Skipping %s because of blacklist entry %s" % (article.title(), e.url))
        except pywikibot.LockedPage:
            pywikibot.output(u"Skipping %s because page is locked" % article.title())
Exemple #7
0
def change_category(article,
                    oldCat,
                    newCat,
                    comment=None,
                    sortKey=None,
                    inPlace=False):
    """
    Remove page from oldCat and add it to newCat.

    @param oldCat and newCat: should be Category objects.
        If newCat is None, the category will be removed.

    @param comment: string to use as an edit summary

    @param sortKey: sortKey to use for the added category.
        Unused if newCat is None, or if inPlace=True

    @param inPlace: if True, change categories in place rather than
                  rearranging them.

    """
    cats = []

    # get list of Category objects the article is in and remove duplicates
    for cat in article.categories(get_redirect=True):
        if cat not in cats:
            cats.append(cat)

    site = article.site()

    if not sortKey:
        sortKey = oldCat.sortKey

    if not article.canBeEdited():
        pywikibot.output("Can't edit %s, skipping it..." %
                         article.title(asLink=True))
        return

    if oldCat not in cats:
        pywikibot.error(u'%s is not in category %s!' %
                        (article.title(asLink=True), oldCat.title()))
        return

    if inPlace or article.namespace() == 10:
        oldtext = article.get(get_redirect=True)
        newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat)
    else:
        if newCat:
            cats[cats.index(oldCat)] = Category(site,
                                                newCat.title(),
                                                sortKey=sortKey)
        else:
            cats.pop(cats.index(oldCat))
        oldtext = article.get(get_redirect=True)
        try:
            newtext = pywikibot.replaceCategoryLinks(oldtext, cats)
        except ValueError:
            # Make sure that the only way replaceCategoryLinks() can return
            # a ValueError is in the case of interwiki links to self.
            pywikibot.output(u'Skipping %s because of interwiki link to self' %
                             article)

    if oldtext != newtext:
        try:
            article.put(newtext, comment)
        except pywikibot.EditConflict:
            pywikibot.output(u'Skipping %s because of edit conflict' %
                             article.title())
        except pywikibot.SpamfilterError, e:
            pywikibot.output(u'Skipping %s because of blacklist entry %s' %
                             (article.title(), e.url))
        except pywikibot.LockedPage:
            pywikibot.output(u'Skipping %s because page is locked' %
                             article.title())