Used to correct errors in usage of [Template Team-Station](https://de.wikipedia.org/wiki/Vorlage:Team-Station).
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

teamstation.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # teamstation.py
  5. #
  6. # Copyright 2018 Jonathan Golder <jonathan@golderweb.de>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. # MA 02110-1301, USA.
  22. #
  23. #
  24. """
  25. Bot to correct errors in usage of
  26. [Template Team-Station](https://de.wikipedia.org/wiki/Vorlage:Team-Station).
  27. """
  28. import os
  29. import locale
  30. import re
  31. import pywikibot
  32. from pywikibot.bot import CurrentPageBot
  33. import mwparserfromhell as mwparser
  34. import jogobot
  35. class TeamstationBot( CurrentPageBot ): # sets 'current_page' on each treat()
  36. """
  37. Bot to correct errors in usage of [Template Team-Station]
  38. (https://de.wikipedia.org/wiki/Vorlage:Team-Station).
  39. """
  40. wrong_dash_search = r"hastemplate:Team-Station insource:/Team-Station *\| *[12][0-9]{3} *(‐|-|‑|‒|—|―|˗)/"
  41. wrong_dash_summary = "Bot: ([[Benutzer:JogoBot/Teamstation|teamstation]]) Korrigiere falschen [[Halbgeviertstrich#Bis-Strich|Bis-Strich]]"
  42. def __init__( self, genFactory, **kwargs ):
  43. """
  44. Constructor
  45. Parameters:
  46. @param genFactory GenFactory with parsed pagegenerator args to
  47. build generator
  48. @type genFactory pagegenerators.GeneratorFactory
  49. @param **kwargs Additional args
  50. @type iterable
  51. """
  52. # Copy needed args
  53. self.genFactory = genFactory
  54. # Build generator with genFactory
  55. self.build_generator()
  56. # Run super class init with builded generator
  57. super( TeamstationBot, self ).__init__(generator=self.gen)
  58. def build_generator( self ):
  59. """
  60. Builds generator
  61. """
  62. # Search articles with wrong dash symbols used in time span
  63. self.genFactory.gens.append(
  64. pywikibot.pagegenerators.SearchPageGenerator(
  65. type(self).wrong_dash_search, namespaces=[0]
  66. )
  67. )
  68. # Use this to create the generator the bot should work on
  69. self.gen = self.genFactory.getCombinedGenerator()
  70. def treat_page( self ):
  71. """
  72. Handles work on current page
  73. """
  74. # Parse article text with mwparser
  75. self.current_page.wikicode = mwparser.parse( self.current_page.text)
  76. # Normalize param 1 (jahre)
  77. self.normalize_jahre()
  78. # Convert wikicode back to string to save
  79. self.new_text = str( self.current_page.wikicode )
  80. # Save
  81. self.put_current(self.new_text, summary=type(self).wrong_dash_summary)
  82. def normalize_jahre(self):
  83. """
  84. Replace wrong dash symbols and do some space normalisations in param 1
  85. (jahre) of template Team-Station
  86. """
  87. # Iterate occurences of template Team-Station
  88. for occ in self.current_page.wikicode.ifilter_templates(
  89. matches=r"^\{\{Team-Station" ):
  90. # Short circuit wrong templates
  91. if not occ.name == "Team-Station":
  92. continue
  93. # Make sure param 1 exists
  94. if occ.has(1):
  95. # Get value of param 1
  96. jahre = occ.get(1);
  97. # Normalize rarely used "bis 2000" notation
  98. if "bis" in jahre.value:
  99. jahre.value.replace("bis", "{{0|0000}}\u2013")
  100. # Get parts of value
  101. mo = re.search(r"\s*(\{\{0\|0000\}\}|(?:1|2)\d{3})\s*(–|‐|-|‑|‒|—|―|˗)?\s*((?:1|2)\d{3})?",str(jahre.value))
  102. # Short circuit if no years found
  103. if not mo:
  104. continue
  105. jahre_n = list()
  106. # Reconstruct normalized value with correct enDash
  107. if mo.group(1):
  108. jahre_n.append(mo.group(1))
  109. else:
  110. jahre_n.append(" ")
  111. if mo.group(2):
  112. jahre_n.append("\u2013")
  113. else:
  114. jahre_n.append(" ")
  115. if mo.group(3):
  116. jahre_n.append(mo.group(3))
  117. else:
  118. jahre_n.append(" ")
  119. # Overwrite old param value with normalized value
  120. occ.add(1,"".join(jahre_n))
  121. def main(*args):
  122. """
  123. Process command line arguments and invoke bot.
  124. If args is an empty list, sys.argv is used.
  125. @param args: command line arguments
  126. @type args: list of unicode
  127. """
  128. # Make sure locale is set to 'de_DE.UTF-8' to prevent problems
  129. # with wrong month abreviations in strptime
  130. locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
  131. # Process global arguments to determine desired site
  132. local_args = pywikibot.handle_args(args)
  133. # Get the jogobot-task_slug (basename of current file without ending)
  134. task_slug = os.path.basename(__file__)[:-len(".py")]
  135. # Actually not needed since we only run semi-automaticall
  136. # Before run, we need to check wether we are currently active or not
  137. #~ if not jogobot.bot.active( task_slug ):
  138. #~ return
  139. # Parse local Args to get information about subtask
  140. ( subtask, genFactory, subtask_args ) = jogobot.bot.parse_local_args(
  141. local_args, None )
  142. # Init Bot
  143. bot = jogobot.bot.init_bot( task_slug, None, TeamstationBot, genFactory)
  144. # Run bot
  145. jogobot.bot.run_bot( task_slug, None, bot )
  146. if( __name__ == "__main__" ):
  147. main()