#!/usr/bin/python3 """ This bot appends some text to all unused images and notifies uploaders. Parameters: -limit Specify number of pages to work on with "-limit:n" where n is the maximum number of articles to work on. If not used, all pages are used. -always Don't be asked every time. This script is a :py:obj:`ConfigParserBot `. The following options can be set within a settings file which is scripts.ini by default:: -nouserwarning Do not warn uploader about orphaned file. -filetemplate: Use a custom template on unused file pages. -usertemplate: Use a custom template to warn the uploader. """ # # (C) Pywikibot team, 2007-2022 # # Distributed under the terms of the MIT license. # import re import pywikibot from pywikibot import i18n, pagegenerators from pywikibot.bot import ( AutomaticTWSummaryBot, ConfigParserBot, ExistingPageBot, SingleSiteBot, ) from pywikibot.exceptions import Error, NoPageError, TranslationError from pywikibot.flow import Board template_to_the_image = { 'meta': '{{Orphan file}}', 'test': '{{Orphan file}}', 'ar': '{{صورة يتيمة}}', 'arz': '{{صوره يتيمه}}', 'en': '{{Orphan image}}', 'fa': '{{تصاویر بدون استفاده}}', 'id': '{{Berkas yatim}}', 'it': '{{immagine orfana}}', 'mk': '{{Слика сираче}}', 'te': '{{Orphan image}}', 'ur': '{{غیر مستعمل تصاویر}}', 'uz': '{{Yetim tasvir}}', 'vec': '{{Imaxine orfana}}', 'vi': '{{Hình mồ côi}}', } template_to_the_user = { 'test': '{{User:Happy5214/Unused file notice (user)|%(title)s}}', 'ar': '{{subst:تنبيه صورة يتيمة|%(title)s}}', 'arz': '{{subst:تنبيه صوره يتيمه|%(title)s}}', 'fa': '{{subst:اخطار به کاربر برای تصاویر بدون استفاده|%(title)s}}', 'ur': '{{subst:اطلاع برائے غیر مستعمل تصاویر|%(title)s}}', } class UnusedFilesBot(SingleSiteBot, AutomaticTWSummaryBot, ConfigParserBot, ExistingPageBot): """Unused files bot. .. versionchanged:: 7.0 UnusedFilesBot is a ConfigParserBot """ summary_key = 'unusedfiles-comment' update_options = { 'nouserwarning': False, # do not warn uploader 'filetemplate': '', 'usertemplate': '', } def __init__(self, **kwargs) -> None: """Initializer.""" super().__init__(**kwargs) # handle the custom templates if not self.opt.filetemplate: self.opt.filetemplate = i18n.translate(self.site, template_to_the_image) elif not re.fullmatch('{{.+}}', self.opt.filetemplate): self.opt.filetemplate = '{{%s}}' % self.opt.filetemplate if not self.opt.usertemplate: self.opt.usertemplate = i18n.translate(self.site, template_to_the_user) elif not re.fullmatch('{{.+}}', self.opt.usertemplate): self.opt.usertemplate = '{{%s}}' % self.opt.usertemplate if not (self.opt.filetemplate and (self.opt.usertemplate or self.opt.nouserwarning)): # if no templates are given raise TranslationError( 'This script is not localized for {} site;\n' 'try using -filetemplate: