Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUpClass(cls):
"""Verify that the translations are available."""
if not i18n.messages_available():
raise unittest.SkipTest("i18n messages package '%s' not available."
% i18n._messages_package_name)
super(TWNBotTestCase, cls).setUpClass()
@param category: category name with start parameter
@type category: str
@rtype: tuple
"""
if category and category.startswith('-'):
categoryname = category.partition(':')[2]
issue_deprecation_warning(
'The usage of "{0}" as actual parameter of '
'pagegenerators.getCategory'.format(category),
categoryname, 2, ArgumentDeprecationWarning, since='20141019')
else:
categoryname = category
if not categoryname:
categoryname = i18n.input(
'pywikibot-enter-category-name',
fallback_prompt='Please enter the category name:')
categoryname = categoryname.replace('#', '|')
categoryname, sep, startfrom = categoryname.partition('|')
if not startfrom:
startfrom = None
# Insert Category: before category name to avoid parsing problems in
# Link.parse() when categoryname contains ":";
# Part before ":" might be interpreted as an interwiki prefix
prefix = categoryname.split(':', 1)[0] # whole word if ":" not present
if prefix not in self.site.namespaces[14]:
categoryname = '{0}:{1}'.format(
self.site.namespace(14), categoryname)
cat = pywikibot.Category(pywikibot.Link(categoryname,
def setSummaryMessage(self, disambPage, new_targets=[], unlink_counter=0,
dn=False):
"""Setup i18n summary message."""
# make list of new targets
comma = self.mysite.mediawiki_message('comma-separator')
targets = comma.join('[[{0}]]'.format(page_title)
for page_title in new_targets)
if not targets:
targets = i18n.twtranslate(self.mysite,
'solve_disambiguation-unknown-page')
# first check whether user has customized the edit comment
if (self.mysite.family.name in config.disambiguation_comment and
self.mylang in config.disambiguation_comment[
self.mysite.family.name]):
try:
self.comment = i18n.translate(
self.mysite,
config.disambiguation_comment[self.mysite.family.name],
fallback=True) % (disambPage.title(), targets)
# Backwards compatibility, type error probably caused by too
# many arguments for format string
except TypeError:
self.comment = i18n.translate(
if value:
namespace = int(value)
else:
genFactory.handleArg(arg)
if config.mylang not in project_inserted:
pywikibot.output('Your project is not supported by this script.\n'
'You have to edit the script and add it!')
return
site = pywikibot.Site()
if protectedpages:
generator = site.protectedpages(namespace=namespace, type=protectType)
# Take the right templates to use, the category and the comment
TSP = i18n.translate(site, templateSemiProtection)
TTP = i18n.translate(site, templateTotalProtection)
TSMP = i18n.translate(site, templateSemiMoveProtection)
TTMP = i18n.translate(site, templateTotalMoveProtection)
TNR = i18n.translate(site, templateNoRegex)
TU = i18n.translate(site, templateUnique)
categories = i18n.translate(site, categoryToCheck)
commentUsed = i18n.twtranslate(site, 'blockpageschecker-summary')
if not generator:
generator = genFactory.getCombinedGenerator()
if not generator:
generator = []
pywikibot.output('Loading categories...')
# Define the category if no other generator has been setted
for CAT in categories:
cat = pywikibot.Category(site, CAT)
# save the content retrieved from generator
contents = page.text
# delete page's text to get it from live wiki
del page.text
if self.getOption('summary'):
comment = self.getOption('summary')
else:
comment = i18n.twtranslate(self.site, 'pagefromfile-msg')
comment_top = comment + ' - ' + i18n.twtranslate(
self.site, 'pagefromfile-msg_top')
comment_bottom = comment + ' - ' + i18n.twtranslate(
self.site, 'pagefromfile-msg_bottom')
comment_force = '{0} *** {1} ***'.format(
comment, i18n.twtranslate(self.site, 'pagefromfile-msg_force'))
if page.exists():
if not self.getOption('redirect') and page.isRedirectPage():
pywikibot.output('Page {0} is redirect, skipping!'
.format(title))
return
pagecontents = page.text
nocontent = self.getOption('nocontent')
if nocontent and (
nocontent in pagecontents or
nocontent.lower() in pagecontents):
pywikibot.output('Page has {0} so it is skipped'
.format(nocontent))
return
if self.getOption('append'):
separator = self.getOption('append')[1]
config.report_dead_links_on_talk = False
elif arg == '-repeat':
gen = RepeatPageGenerator()
elif arg.startswith('-ignore:'):
HTTPignore.append(int(arg[8:]))
elif arg.startswith('-day:'):
config.weblink_dead_days = int(arg[5:])
elif arg.startswith('-xmlstart'):
if len(arg) == 9:
xmlStart = pywikibot.input(
'Please enter the dumped article to start with:')
else:
xmlStart = arg[10:]
elif arg.startswith('-xml'):
if len(arg) == 4:
xmlFilename = i18n.input('pywikibot-enter-xml-filename')
else:
xmlFilename = arg[5:]
else:
genFactory.handleArg(arg)
if xmlFilename:
try:
xmlStart
except NameError:
xmlStart = None
gen = XmlDumpPageGenerator(xmlFilename, xmlStart,
genFactory.namespaces)
if not gen:
gen = genFactory.getCombinedGenerator()
if gen:
if self.site.code == 'it':
pattern = '%d/%m/%Y'
else:
pattern = '%Y/%m/%d'
target = self.log_name + '/' + time.strftime(
pattern, time.localtime(time.time()))
log_page = pywikibot.Page(self.site, target)
if log_page.exists():
text = log_page.get()
else:
# make new log page
showStatus()
pywikibot.output(
'Log page is not exist, getting information for page creation')
text = i18n.translate(self.site, logpage_header,
fallback=i18n.DEFAULT_FALLBACK)
text += '\n!' + self.site.namespace(2)
text += '\n!' + str.capitalize(
self.site.mediawiki_message('contribslink'))
# Adding the log... (don't take care of the variable's name...).
text += '\n'
text += '\n'.join(
'{{WLE|user=%s|contribs=%d}}' % (
user.title(as_url=True, with_ns=False), user.editCount())
for user in queue)
# update log page.
while True:
try:
log_page.put(text, i18n.twtranslate(self.site,
def run(self):
"""Run the bot."""
ok = False
try:
header = self.header
r = comms.http.fetch(
self.url, headers=header,
use_fake_user_agent=self._use_fake_user_agent)
except requests.exceptions.InvalidURL:
message = i18n.twtranslate(self.page.site,
'weblinkchecker-badurl_msg',
{'URL': self.url})
except Exception:
pywikibot.output('Exception while processing URL {0} in page {1}'
.format(self.url, self.page.title()))
raise
if (r.status == requests.codes.ok
and str(r.status) not in self.HTTPignore):
ok = True
else:
message = '{0}'.format(r.status)
if ok:
if self.history.setLinkAlive(self.url):
pywikibot.output('*Link to {0} in [[{1}]] is back alive.'
.format(self.url, self.page.title()))
else:
else:
#make new log page
showStatus()
pywikibot.output('Log page is not exist, getting information for page creation')
text = pywikibot.translate(self.site, logpage_header)
text += u'\n!%s' % self.site.namespace(2)
text += u'\n!%s' % capitalize(self.site.mediawiki_message('contribslink'))
for result in queue:
# Adding the log... (don't take care of the variable's name...).
luser = pywikibot.url2link(result.name(), self.site, self.site)
text += u'\n{{WLE|user=%s|contribs=%d}}' % (luser, result.editCount())
#update log page.
while True:
try:
logPage.put(text, i18n.twtranslate(self.site,
'welcome-updating'))
return True
except pywikibot.EditConflict:
pywikibot.output(u'An edit conflict has occured. Pausing for 10 seconds before continuing.')
time.sleep(10)
def addReferences(self, oldText):
"""
Add a references tag into an existing section where it fits into.
If there is no such section, creates a new section containing
the references tag. Also repair malformed references tags.
Set the edit summary accordingly.
@param oldText: page text to be modified
@type oldText: str
@return: The modified pagetext
@rtype: str
"""
# Do we have a malformed tag which could be repaired?
# Set the edit summary for this case
self.comment = i18n.twtranslate(self.site, 'noreferences-fix-tag')
# Repair two opening tags or a opening and an empty tag
pattern = re.compile(r'< *references *>(.*?)'
r'< */?\s*references */? *>', re.DOTALL)
if pattern.search(oldText):
pywikibot.output('Repairing references tag')
return re.sub(pattern, r'\1', oldText)
# Repair single unclosed references tag
pattern = re.compile(r'< *references *>')
if pattern.search(oldText):
pywikibot.output('Repairing references tag')
return re.sub(pattern, '', oldText)
# Is there an existing section where we can add the references tag?
# Set the edit summary for this case
self.comment = i18n.twtranslate(self.site, 'noreferences-add-tag')