-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuredir.py
More file actions
123 lines (105 loc) · 4.78 KB
/
Copy pathfuredir.py
File metadata and controls
123 lines (105 loc) · 4.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import pywikibot
from pywikibot import pagegenerators
from pywikibot.bot import (
AutomaticTWSummaryBot,
ConfigParserBot,
ExistingPageBot,
SingleSiteBot,
)
import wikitextparser as wtp
# List of template names (without the "Шаблон:" prefix) that use the "Стаття" parameter.
TEMPLATE_NAMES = [
"обґрунтування добропорядного використання",
"одв",
"обґрунтування сумлінного використання",
"non-free use rationale",
"non-free image rationale",
"осв",
"non-free fair use rationale",
"non-free image data",
"non-free use rationale book cover"
]
# Aliases for the "Стаття" parameter.
ARTICLE_PARAM_ALIASES = ["Стаття", "стаття", "Article"]
class UpdateRedirectBot(
SingleSiteBot,
ConfigParserBot,
ExistingPageBot,
AutomaticTWSummaryBot,
):
summary_key = 'basic-changing'
use_redirects = False
update_options = {
'replace': False,
'summary': "Виправлення [[:Категорія:Невільні файли, у яких назва статті є перенаправленням]]",
'text': 'Test',
'top': False,
}
def treat_page(self) -> None:
page = self.current_page
print(f"Processing page: {page.title()}")
text = page.text
parsed = wtp.parse(text)
templates = parsed.templates
for template in templates:
#print(template.name)
if template.name.strip().lower() in TEMPLATE_NAMES:
#print("Processing template")
#print(template)
for alias in ARTICLE_PARAM_ALIASES:
if alias in [argument.name.strip() for argument in template.arguments]:
current_val = template.get_arg(alias).value.strip()
#print(f"current_val: {current_val}")
if current_val:
# Create a page object from the parameter value.
article_page = pywikibot.Page(self.site, current_val)
#print(f"article_page.exists(): {article_page.exists()}, article_page.isRedirectPage(): {article_page.isRedirectPage()}")
if article_page.exists() and article_page.isRedirectPage():
old_template = template.string
target_page = article_page.getRedirectTarget()
new_title = target_page.title()+"\n"
#print(f"Updating parameter '{alias}': {current_val} -> {new_title}")
template.set_arg(alias, new_title)
print(f"Saving page: {page.title()}")
text = text.replace(old_template, template.string)
self.current_page.put(text, summary=self.opt.summary)
return None
break # Stop after handling one of the aliases
print(f"No changes needed for {page.title()}.")
def main(*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
"""
options = {}
# Process global arguments to determine desired site
local_args = pywikibot.handle_args(args)
# This factory is responsible for processing command line arguments
# that are also used by other scripts and that determine on which pages
# to work on.
gen_factory = pagegenerators.GeneratorFactory()
# Process pagegenerators arguments
local_args = gen_factory.handle_args(local_args)
# Parse your own command line arguments
for arg in local_args:
arg, _, value = arg.partition(':')
option = arg[1:]
if option in ('summary', 'text'):
if not value:
pywikibot.input('Please enter a value for ' + arg)
options[option] = value
# take the remaining options as booleans.
# You will get a hint if they aren't pre-defined in your bot class
else:
options[option] = True
# The preloading option is responsible for downloading multiple
# pages from the wiki simultaneously.
gen = gen_factory.getCombinedGenerator(preload=True)
# check if further help is needed
if not pywikibot.bot.suggest_help(missing_generator=not gen):
# pass generator and private options to the bot
bot = UpdateRedirectBot(generator=gen, **options)
bot.run() # guess what it does
if __name__ == "__main__":
main()