예제 #1
0
    def replaceUsage(self, imagepage, filename):
        """If the image is uploaded under a different name, replace all usage."""
        if imagepage.title(withNamespace=False) != filename:
            gen = pagegenerators.FileLinksGenerator(imagepage)
            preloadingGen = pagegenerators.PreloadingGenerator(gen)

            moveSummary = i18n.twtranslate(
                imagepage.site(), 'commons-file-moved',
                {'localfile': imagepage.title(withNamespace=False),
                 'commonsfile': filename})

            imagebot = image.ImageRobot(generator=preloadingGen,
                                        oldImage=imagepage.title(withNamespace=False),
                                        newImage=filename, summary=moveSummary,
                                        always=True, loose=True)
            imagebot.run()
예제 #2
0
    def run(self):
        """Run the bot."""
        tosend = {
            'language': self.imagePage.site.lang.encode('utf-8'),
            'image': self.imagePage.title(withNamespace=False).encode('utf-8'),
            'newname': self.newname.encode('utf-8'),
            'project': self.imagePage.site.family.name.encode('utf-8'),
            'username': '',
            'commonsense': '1',
            'remove_categories': '1',
            'ignorewarnings': '1',
            'doit': 'Uitvoeren'
        }

        tosend = urlencode(tosend)
        pywikibot.output(tosend)
        CH = pageTextPost('http://tools.wmflabs.org/commonshelper/index.php',
                          tosend)
        pywikibot.output('Got CH desc.')

        tablock = CH.split('<textarea ')[1].split('>')[0]
        CH = CH.split('<textarea ' + tablock + '>')[1].split('</textarea>')[0]
        CH = CH.replace(u'&times;', u'×')
        CH = self.fixAuthor(CH)
        pywikibot.output(CH)

        # I want every picture to be tagged with the bottemplate so i can check
        # my contributions later.
        CH = ('\n\n{{BotMoveToCommons|' + self.imagePage.site.lang + '.' +
              self.imagePage.site.family.name +
              '|year={{subst:CURRENTYEAR}}|month={{subst:CURRENTMONTHNAME}}'
              '|day={{subst:CURRENTDAY}}}}' + CH)

        if self.category:
            CH = CH.replace(
                '{{subst:Unc}} <!-- Remove this line once you have '
                'added categories -->', '')
            CH += u'[[Category:' + self.category + u']]'

        bot = UploadRobot(url=self.imagePage.fileUrl(),
                          description=CH,
                          useFilename=self.newname,
                          keepFilename=True,
                          verifyDescription=False,
                          ignoreWarning=True,
                          targetSite=pywikibot.Site('commons', 'commons'))
        bot.run()

        # Should check if the image actually was uploaded
        if pywikibot.Page(pywikibot.Site('commons', 'commons'),
                          u'Image:' + self.newname).exists():
            # Get a fresh copy, force to get the page so we dont run into edit
            # conflicts
            imtxt = self.imagePage.get(force=True)

            # Remove the move to commons templates
            if self.imagePage.site.lang in moveToCommonsTemplate:
                for moveTemplate in moveToCommonsTemplate[
                        self.imagePage.site.lang]:
                    imtxt = re.sub(r'(?i)\{\{' + moveTemplate + r'[^\}]*\}\}',
                                   '', imtxt)

            # add {{NowCommons}}
            if self.imagePage.site.lang in nowCommonsTemplate:
                addTemplate = nowCommonsTemplate[
                    self.imagePage.site.lang] % self.newname
            else:
                addTemplate = nowCommonsTemplate['_default'] % self.newname

            commentText = i18n.twtranslate(
                self.imagePage.site, 'commons-file-now-available', {
                    'localfile': self.imagePage.title(withNamespace=False),
                    'commonsfile': self.newname
                })

            pywikibot.showDiff(self.imagePage.get(), imtxt + addTemplate)
            self.imagePage.put(imtxt + addTemplate, comment=commentText)

            self.gen = pagegenerators.FileLinksGenerator(self.imagePage)
            self.preloadingGen = pagegenerators.PreloadingGenerator(self.gen)

            moveSummary = i18n.twtranslate(
                self.imagePage.site, 'commons-file-moved', {
                    'localfile': self.imagePage.title(withNamespace=False),
                    'commonsfile': self.newname
                })

            # If the image is uploaded under a different name, replace all
            # instances
            if self.imagePage.title(withNamespace=False) != self.newname:
                imagebot = image.ImageRobot(
                    generator=self.preloadingGen,
                    oldImage=self.imagePage.title(withNamespace=False),
                    newImage=self.newname,
                    summary=moveSummary,
                    always=True,
                    loose=True)
                imagebot.run()

            # If the user want to delete the page and
            # the user has sysops privilege, delete the page, otherwise
            # it will be marked for deletion.
            if self.delete_after_done:
                self.imagePage.delete(moveSummary, False)
        return