source: ValBot/Python/uncapitalize_image_suffixes.py@ 1171

Last change on this file since 1171 was 1169, checked in by iritscen, 3 years ago

ValBot: Reorganized files. Updated docs with more helpful information.

File size: 2.4 KB
Line 
1import os
2import pywikibot
3
4from pywikibot.bot import QuitKeyboardInterrupt
5from pywikibot import pagegenerators
6
7def main(*args):
8 genFactory = pagegenerators.GeneratorFactory()
9 #allowed_suffixes = ('jpg', 'jpeg', 'png', 'gif', 'svg')
10 allowed_suffixes = []
11 images_checked = 0
12 capped_suffixes = 0
13 site = pywikibot.Site()
14
15 try:
16 allowed_suffixes = site.siteinfo.get('fileextensions', get_default=False)
17 except KeyError:
18 pywikibot.stdout('Failed to get the wiki\'s allowed image suffixes!')
19 return
20 else:
21 allowed_suffixes = [item['ext'].lower() for item in allowed_suffixes]
22 pywikibot.stdout('Wiki accepts image suffixes {0}.'.format(allowed_suffixes))
23
24 generator = site.allimages()
25 pywikibot.stdout('Looking for images with capitalized suffixes...')
26 for page in pagegenerators.PreloadingGenerator(generator, 100):
27 images_checked = images_checked + 1
28 suffix = page.title().split('.')[-1]
29 if suffix.lower() in allowed_suffixes:
30 if suffix != suffix.lower():
31 # We found a page which has an allowed suffix but which is capitalized
32 pywikibot.stdout('{0}'.format(page.title()))
33 capped_suffixes = capped_suffixes + 1
34
35 # Rename page to have lowercase suffix
36 new_page_title = os.path.splitext(page.title())[0] + '.' + suffix.lower()
37 pywikibot.stdout('Moving page to {0}...'.format(new_page_title))
38 page.move(new_page_title, reason='use lowercase file suffix', movetalk=True, noredirect=True)
39
40 # Warn the user if the page we moved had references to it
41 wiki_links = []
42 wiki_links = list(page.backlinks())
43 file_links = []
44 file_links = list(page.usingPages())
45 if len(wiki_links) or len(file_links):
46 pywikibot.stdout('However this page is referenced by:')
47 for ref in wiki_links:
48 pywikibot.stdout(' {0}'.format(ref.title()))
49 for ref in file_links:
50 pywikibot.stdout(' {0}'.format(ref.title()))
51 else:
52 pywikibot.stdout('Found disallowed suffix {0}!'.format(suffix)) # should never happen
53
54 pywikibot.stdout('Checked {0} images and found {1} with capitalized suffixes.'.format(images_checked, capped_suffixes))
55
56if __name__ == '__main__':
57 main()
Note: See TracBrowser for help on using the repository browser.