#!/usr/bin/env python3"""This script can be used to change one image to another or remove an image.Syntax: python pwb.py image image_name [new_image_name]If only one command-line parameter is provided then that image will beremoved; if two are provided, then the first image will be replaced bythe second one on all pages.Command line options:-summary: Provide a custom edit summary. If the summary includes spaces, surround it with single quotes, such as: ``-summary:'My edit summary'``-always Don't prompt to make changes, just do them.-loose Do loose replacements. This will replace all occurrences of the name of the image (and not just explicit image syntax). This should work to catch all instances of the image, including where it is used as a template parameter or in image galleries. However, it can also make more mistakes. This only works with image replacement, not image removal.Examples--------The image "FlagrantCopyvio.jpg" is about to be deleted, so let's firstremove it from everything that displays it: python pwb.py image FlagrantCopyvio.jpgThe image "Flag.svg" has been uploaded, making the old "Flag.jpg"obsolete: python pwb.py image Flag.jpg Flag.svg"""## (C) Pywikibot team, 2013-2024## Distributed under the terms of the MIT license.#from__future__importannotationsimportreimportpywikibotfrompywikibotimporti18n,pagegeneratorsfrompywikibot.botimportSingleSiteBotfrompywikibot.textlibimportcase_escape,ignore_casetry:fromscripts.replaceimportReplaceRobotasReplaceBotexceptModuleNotFoundError:frompywikibot_scripts.replaceimportReplaceRobotasReplaceBot
[docs]classImageRobot(ReplaceBot):"""This bot will replace or remove all occurrences of an old image."""def__init__(self,generator,old_image:str,new_image:str='',**kwargs)->None:"""Initializer. :param generator: the pages to work on :type generator: iterable :param old_image: the title of the old image (without namespace) :param new_image: the title of the new image (without namespace), or None if you want to remove the image """self.available_options.update({'summary':None,'loose':False,})SingleSiteBot.__init__(self,**kwargs)self.old_image=old_imageself.new_image=new_imageparam={'old':self.old_image,'new':self.new_image,'file':self.old_image,}summary=self.opt.summaryori18n.twtranslate(self.site,'image-replace'ifself.new_imageelse'image-remove',param)namespace=self.site.namespaces[6]escaped=case_escape(namespace.case,self.old_image,underscore=True)ifnotself.opt.looseornotself.new_image:image_regex=re.compile(r'\[\[ *(?:{})\s*:\s*{} *(?P<parameters>\|'r'(?:[^\[\]]|\[\[[^\]]+\]\]|\[[^\]]+\])*|) *\]\]'.format('|'.join(ignore_case(s)forsinnamespace),escaped))else:image_regex=re.compile(r''+escaped)replacements=[]ifnotself.opt.looseandself.new_image:replacements.append((image_regex,f'[[{self.site.namespaces.FILE.custom_name}:{self.new_image}''\\g<parameters>]]'))else:replacements.append((image_regex,self.new_image))super().__init__(generator,replacements,always=self.opt.always,site=self.site,summary=summary)
[docs]defmain(*args:str)->None:"""Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. :param args: command line arguments """old_image=''new_image=''options={}forargumentinpywikibot.handle_args(args):arg,_,value=argument.partition(':')ifargin('-always','-loose'):options[arg[1:]]=Trueelifarg=='-summary':options[arg[1:]]=valueorpywikibot.input('Choose an edit summary: ')elifold_image:new_image=argelse:old_image=argifold_image:site=pywikibot.Site()old_imagepage=pywikibot.FilePage(site,old_image)gen=old_imagepage.using_pages()preloading_gen=pagegenerators.PreloadingGenerator(gen)bot=ImageRobot(preloading_gen,old_image,new_image,site=site,**options)bot.run()else:pywikibot.bot.suggest_help(missing_parameters=['old image'])