Пример #1
0
    def run(self):
        commons = pywikibot.Site('commons', 'commons')
        comment = i18n.translate(self.site, nowCommonsMessage, fallback=True)

        for page in self.getPageGenerator():
            if self.getOption('use_hash'):
                # Page -> Has the namespace | commons image -> Not
                images_list = page  # 0 -> local image, 1 -> commons image
                page = pywikibot.Page(self.site, images_list[0])
            else:
                # If use_hash is true, we have already print this before, no need
                self.current_page = page
            try:
                localImagePage = pywikibot.FilePage(self.site, page.title())
                if localImagePage.fileIsShared():
                    pywikibot.output(u'File is already on Commons.')
                    continue
                sha1 = localImagePage.latest_file_info.sha1
                if self.getOption('use_hash'):
                    filenameOnCommons = images_list[1]
                else:
                    filenameOnCommons = self.findFilenameOnCommons(
                        localImagePage)
                if not filenameOnCommons and not self.getOption('use_hash'):
                    pywikibot.output(u'NowCommons template not found.')
                    continue
                commonsImagePage = pywikibot.FilePage(
                    commons, 'Image:%s' % filenameOnCommons)
                if localImagePage.title(withNamespace=False) == \
                 commonsImagePage.title(withNamespace=False) and self.getOption('use_hash'):
                    pywikibot.output(
                        u'The local and the commons images have the same name')
                if localImagePage.title(withNamespace=False) != \
                 commonsImagePage.title(withNamespace=False):
                    usingPages = list(localImagePage.usingPages())
                    if usingPages and usingPages != [localImagePage]:
                        pywikibot.output(
                            u'\"\03{lightred}%s\03{default}\" is still used in %i pages.'
                            % (localImagePage.title(withNamespace=False),
                               len(usingPages)))
                        if self.getOption('replace') is True:
                            pywikibot.output(
                                u'Replacing \"\03{lightred}%s\03{default}\" by \
                                    \"\03{lightgreen}%s\03{default}\".' %
                                (localImagePage.title(withNamespace=False),
                                 commonsImagePage.title(withNamespace=False)))
                            oImageRobot = image.ImageRobot(
                                pg.FileLinksGenerator(localImagePage),
                                localImagePage.title(withNamespace=False),
                                commonsImagePage.title(withNamespace=False),
                                '', self.getOption('replacealways'),
                                self.getOption('replaceloose'))
                            oImageRobot.run()
                            # If the image is used with the urlname the
                            # previous function won't work
                            if len(list(pywikibot.FilePage(self.site,
                                                            page.title()).usingPages())) > 0 and \
                                                            self.getOption('replaceloose'):
                                oImageRobot = image.ImageRobot(
                                    pg.FileLinksGenerator(localImagePage),
                                    localImagePage.title(withNamespace=False,
                                                         asUrl=True),
                                    commonsImagePage.title(
                                        withNamespace=False), '',
                                    self.getOption('replacealways'),
                                    self.getOption('replaceloose'))
                                oImageRobot.run()
                            # refresh because we want the updated list
                            usingPages = len(
                                list(
                                    pywikibot.FilePage(
                                        self.site, page.title()).usingPages()))
                            if usingPages > 0 and self.getOption('use_hash'):
                                # just an enter
                                pywikibot.input(
                                    u'There are still %s pages with this \
                                        image, confirm the manual removal from them please.'
                                    % usingPages)

                        else:
                            pywikibot.output(u'Please change them manually.')
                        continue
                    else:
                        pywikibot.output(
                            u'No page is using \"\03{lightgreen}%s\03{default}\" anymore.'
                            % localImagePage.title(withNamespace=False))
                commonsText = commonsImagePage.get()
                if self.getOption('replaceonly') is False:
                    if sha1 == commonsImagePage.latest_file_info.sha1:
                        pywikibot.output(
                            u'The image is identical to the one on Commons.')
                        if len(localImagePage.getFileVersionHistory()
                               ) > 1 and not self.getOption('use_hash'):
                            pywikibot.output(
                                u"This image has a version history. Please \
                                delete it manually after making sure that the \
                                old versions are not worth keeping."
                                "")
                            continue
                        if self.getOption('always') is False:
                            pywikibot.output(
                                u'\n\n>>>> Description on \03{lightpurple}%s\03{default} <<<<\n'
                                % page.title())
                            pywikibot.output(localImagePage.get())
                            pywikibot.output(
                                u'\n\n>>>> Description on \03{lightpurple}%s\03{default} <<<<\n'
                                % commonsImagePage.title())
                            pywikibot.output(commonsText)
                            if pywikibot.input_yn(
                                    u'Does the description on Commons contain '
                                    'all required source and license\n'
                                    'information?',
                                    default=False,
                                    automatic_quit=False):
                                localImagePage.delete(
                                    '%s [[:commons:Image:%s]]' %
                                    (comment, filenameOnCommons),
                                    prompt=False)
                        else:
                            localImagePage.delete(
                                comment +
                                ' [[:commons:Image:%s]]' % filenameOnCommons,
                                prompt=False)
                    else:
                        pywikibot.output(
                            u'The image is not identical to the one on Commons.'
                        )
            except (pywikibot.NoPage, pywikibot.IsRedirectPage) as e:
                pywikibot.output(u'%s' % e[0])
                continue
Пример #2
0
    def run(self):
        commons = pywikibot.getSite('commons', 'commons')
        comment = pywikibot.translate(self.site, nowCommonsMessage)

        for page in self.getPageGenerator():
            if use_hash:
                # Page -> Has the namespace | commons image -> Not
                images_list = page # 0 -> local image, 1 -> commons image
                page = pywikibot.Page(self.site, images_list[0])
            else:
                # If use_hash is true, we have already print this before, no need
                # 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())
            try:
                localImagePage = pywikibot.ImagePage(self.site, page.title())
                if localImagePage.fileIsOnCommons():
                    pywikibot.output(u'File is already on Commons.')
                    continue
                md5 = localImagePage.getFileMd5Sum()
                if use_hash:
                    filenameOnCommons = images_list[1]
                else:
                    filenameOnCommons = self.findFilenameOnCommons(
                        localImagePage)
                if not filenameOnCommons and not use_hash:
                    pywikibot.output(u'NowCommons template not found.')
                    continue
                commonsImagePage = pywikibot.ImagePage(commons, 'Image:%s'
                                                       % filenameOnCommons)
                if localImagePage.title(withNamespace=False) == \
                   commonsImagePage.title(withNamespace=False) and use_hash:
                    pywikibot.output(
                        u'The local and the commons images have the same name')
                if localImagePage.title(withNamespace=False) != \
                   commonsImagePage.title(withNamespace=False):
                    usingPages = list(localImagePage.usingPages())
                    if usingPages and usingPages != [localImagePage]:
                        pywikibot.output(
                            u'\"\03{lightred}%s\03{default}\" is still used in %i pages.'
                            % (localImagePage.title(withNamespace=False),
                               len(usingPages)))
                        if replace == True:
                                pywikibot.output(
                                    u'Replacing \"\03{lightred}%s\03{default}\" by \"\03{lightgreen}%s\03{default}\".'
                                    % (localImagePage.title(withNamespace=False),
                                       commonsImagePage.title(withNamespace=False)))
                                oImageRobot = image.ImageRobot(
                                    pg.FileLinksGenerator(localImagePage),
                                    localImagePage.title(withNamespace=False),
                                    commonsImagePage.title(withNamespace=False),
                                    '', replacealways, replaceloose)
                                oImageRobot.run()
                                # If the image is used with the urlname the
                                # previous function won't work
                                if len(list(pywikibot.ImagePage(self.site,
                                                                page.title()).usingPages())) > 0 and \
                                                                replaceloose:
                                    oImageRobot = image.ImageRobot(
                                        pg.FileLinksGenerator(
                                            localImagePage),
                                        self.urlname(
                                            localImagePage.title(
                                                withNamespace=False)),
                                        commonsImagePage.title(
                                            withNamespace=False),
                                        '', replacealways, replaceloose)
                                    oImageRobot.run()
                                # refresh because we want the updated list
                                usingPages = len(list(pywikibot.ImagePage(
                                    self.site, page.title()).usingPages()))
                                if usingPages > 0 and use_hash:
                                    # just an enter
                                    pywikibot.input(
                                        u'There are still %s pages with this image, confirm the manual removal from them please.'
                                        % usingPages)

                        else:
                            pywikibot.output(u'Please change them manually.')
                        continue
                    else:
                        pywikibot.output(
                            u'No page is using \"\03{lightgreen}%s\03{default}\" anymore.'
                            % localImagePage.title(withNamespace=False))
                commonsText = commonsImagePage.get()
                if replaceonly == False:
                    if md5 == commonsImagePage.getFileMd5Sum():
                        pywikibot.output(
                            u'The image is identical to the one on Commons.')
                        if len(localImagePage.getFileVersionHistory()) > 1 and not use_hash:
                            pywikibot.output(
                                u"This image has a version history. Please delete it manually after making sure that the old versions are not worth keeping.""")
                            continue
                        if autonomous == False:
                            pywikibot.output(
                                u'\n\n>>>> Description on \03{lightpurple}%s\03{default} <<<<\n'
                                % page.title())
                            pywikibot.output(localImagePage.get())
                            pywikibot.output(
                                u'\n\n>>>> Description on \03{lightpurple}%s\03{default} <<<<\n'
                                % commonsImagePage.title())
                            pywikibot.output(commonsText)
                            choice = pywikibot.inputChoice(
u'Does the description on Commons contain all required source and license\n'
                                u'information?',
                                ['yes', 'no'], ['y', 'N'], 'N')
                            if choice.lower() in ['y', 'yes']:
                                localImagePage.delete(
                                    comment + ' [[:commons:Image:%s]]'
                                    % filenameOnCommons, prompt = False)
                        else:
                            localImagePage.delete(
                                comment + ' [[:commons:Image:%s]]'
                                % filenameOnCommons, prompt = False)
                    else:
                        pywikibot.output(
                            u'The image is not identical to the one on Commons.')
            except (pywikibot.NoPage, pywikibot.IsRedirectPage), e:
                pywikibot.output(u'%s' % e[0])
                continue