def delete_1_broken_redirect(self, redir_name, reason): self.check_page_broken() self.check_page() redir_page = pywikibot.Page(self.site, redir_name) # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % redir_page.title()) try: targetPage = redir_page.getRedirectTarget() except pywikibot.IsNotRedirectPage: pywikibot.output(u'%s is not a redirect.' % redir_page.title()) except pywikibot.NoPage: pywikibot.output(u'%s doesn\'t exist.' % redir_page.title()) else: try: targetPage.get() except pywikibot.NoPage: if self.prompt( u'Redirect target %s does not exist. Do you want to delete %s?' % (targetPage.title(asLink=True), redir_page.title(asLink=True))): try: redir_page.delete(reason, prompt = False) except pywikibot.NoUsername: if i18n.twhas_key( targetPage.site.lang, 'redirect-broken-redirect-template') and \ i18n.twhas_key(targetPage.site.lang, 'redirect-remove-broken'): pywikibot.output( u"No sysop in user-config.py, put page to speedy deletion.") content = redir_page.get(get_redirect=True) ### TODO: Add bot's signature if needed ### Not supported via TW yet content = i18n.twtranslate( targetPage.site.lang, 'redirect-broken-redirect-template' ) + "\n" + content redir_page.put(content, reason) except pywikibot.IsRedirectPage: pywikibot.output( u'Redirect target %s is also a redirect! Won\'t delete anything.' % targetPage.title(asLink=True)) else: #we successfully get the target page, meaning that #it exists and is not a redirect: no reason to touch it. pywikibot.output( u'Redirect target %s does exist! Won\'t delete anything.' % targetPage.title(asLink=True)) pywikibot.output(u'')
def delete_1_broken_redirect(self, redir_name, reason): redir_page = pywikibot.Page(self.site, redir_name) # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % redir_page.title()) try: targetPage = redir_page.getRedirectTarget() except pywikibot.IsNotRedirectPage: pywikibot.output(u'%s is not a redirect.' % redir_page.title()) except pywikibot.NoPage: pywikibot.output(u'%s doesn\'t exist.' % redir_page.title()) else: try: targetPage.get() except pywikibot.NoPage: if self.prompt(u'Redirect target %s does not exist. ' u'Do you want to delete %s?' % (targetPage.title(asLink=True), redir_page.title(asLink=True))): try: redir_page.delete(reason, prompt=False) except pywikibot.NoUsername: if ((i18n.twhas_key( targetPage.site.lang, 'redirect-broken-redirect-template') and i18n.twhas_key(targetPage.site.lang, 'redirect-remove-broken')) or \ targetPage.site.lang == '-'): pywikibot.output(u"No sysop in user-config.py, " u"put page to speedy deletion.") content = redir_page.get(get_redirect=True) ### TODO: Add bot's signature if needed ### Not supported via TW yet content = i18n.twtranslate( targetPage.site.lang, 'redirect-broken-redirect-template' ) + "\n" + content redir_page.put(content, reason) except pywikibot.IsRedirectPage: pywikibot.output(u"Redirect target %s is also a redirect! " u"Won't delete anything." % targetPage.title(asLink=True)) else: # we successfully get the target page, meaning that # it exists and is not a redirect: no reason to touch it. pywikibot.output( u'Redirect target %s does exist! Won\'t delete anything.' % targetPage.title(asLink=True)) pywikibot.output(u'')
def get_sd_template(self, site=None) -> Optional[str]: """Look for speedy deletion template and return it. @param site: site for which the template has to be given @type site: pywikibot.BaseSite @return: A valid speedy deletion template. """ title = None if site: sd = self.opt.sdtemplate if not sd and i18n.twhas_key(site, 'redirect-broken-redirect-template'): sd = i18n.twtranslate(site, 'redirect-broken-redirect-template') # check whether template exists for this site if sd: template = extract_templates_and_params_regex_simple(sd) if template: title = template[0][0] page = pywikibot.Page(site, title, ns=10) if page.exists(): return sd pywikibot.warning('No speedy deletion template {}available.'.format( '"{}" '.format(title) if title else '')) return None
def get_sd_template(self): """Look for speedy deletion template and return it. @return: A valid speedy deletion template. @rtype: str or None """ if self.getOption('delete') and not self.site.logged_in(sysop=True): sd = self.getOption('sdtemplate') if not sd and i18n.twhas_key(self.site, 'redirect-broken-redirect-template'): sd = i18n.twtranslate(self.site, 'redirect-broken-redirect-template') # TODO: Add bot's signature if needed (Bug: T131517) # check whether template exists for this site title = None if sd: template = extract_templates_and_params_regex_simple(sd) if template: title = template[0][0] page = pywikibot.Page(self.site, title, ns=10) if page.exists(): return sd pywikibot.warning( 'No speedy deletion template {0}available.' ''.format('"{0}" '.format(title) if title else '')) return None
def test_template(self): """Test validity of template.""" lang = site.lang if lang not in keys: return if not i18n.twhas_key(lang, package): return msg = i18n.twtranslate(lang, package, fallback=False) # check whether the message contains a template templates = extract_templates_and_params_regex_simple(msg) self.assertIsInstance(templates, list) self.assertIsNotEmpty(templates) # known problems if (package == PACKAGES[0] and site.code in ['simple', 'test2'] or package == PACKAGES[1] and site.code == 'test'): raise unittest.SkipTest( "{site} wiki has '{site.lang}' language code but " "missing template for package '{package}'. Must be " 'solved by the corresponding script.'.format( site=site, package=package)) # check whether template exists title = templates[0][0] page = pywikibot.Page(site, title, ns=10) self.assertTrue(page.exists(), msg='Invalid L10N in package "{package}"\n' 'template "{title}" does not exist for lang ' '"{site.lang}" on site "{site}"'.format( package=package, title=title, site=site))
def delete_redirect(self, page, summary_key): """Delete the redirect page.""" assert page.site == self.site, ( 'target page is on different site {0}'.format(page.site)) reason = i18n.twtranslate(self.site, summary_key) if page.site.logged_in(sysop=True): page.delete(reason, prompt=False) elif i18n.twhas_key(page.site, 'redirect-broken-redirect-template'): pywikibot.output(u"No sysop in user-config.py, " u"put page to speedy deletion.") try: content = page.get(get_redirect=True) except pywikibot.SectionError: content_page = pywikibot.Page(page.site, page.title(withSection=False)) content = content_page.get(get_redirect=True) # TODO: Add bot's signature if needed (Bug: T131517) content = i18n.twtranslate( page.site, 'redirect-broken-redirect-template') + '\n' + content try: page.put(content, reason) except pywikibot.PageSaveRelatedError as e: pywikibot.error(e) else: pywikibot.output( u'No speedy deletion template available')
def delete_redirect(self, page, summary_key): """Delete the redirect page.""" assert page.site == self.site, ( 'target page is on different site {0}'.format(page.site)) reason = i18n.twtranslate(self.site, summary_key) if page.site.logged_in(sysop=True): page.delete(reason, prompt=False) elif i18n.twhas_key(page.site, 'redirect-broken-redirect-template'): pywikibot.output(u"No sysop in user-config.py, " u"put page to speedy deletion.") try: content = page.get(get_redirect=True) except pywikibot.SectionError: content_page = pywikibot.Page(page.site, page.title(withSection=False)) content = content_page.get(get_redirect=True) # TODO: Add bot's signature if needed (Bug: T131517) content = i18n.twtranslate( page.site, 'redirect-broken-redirect-template') + '\n' + content try: page.put(content, reason) except pywikibot.PageSaveRelatedError as e: pywikibot.error(e) else: pywikibot.output(u'No speedy deletion template available')
def has_valid_template(self, twtitle): """"Check whether a template from translatewiki.net does exist on real wiki. We assume we are always working on self.site @param twtitle - a sting which is the i18n key """ if self._valid_template is None: self._valid_template = False if i18n.twhas_key(self.site, twtitle): template_msg = i18n.twtranslate(self.site, twtitle) template = re.findall(u'.*?{{(.*?)[|}]', template_msg) if template: title = template[0] page = pywikibot.Page(self.site, title, ns=10) self._valid_template = page.exists() return self._valid_template
def fix_1_double_redirect(self, redir_name): if isinstance(redir_name, basestring): redir = pywikibot.Page(self.site, redir_name) else: redir = redir_name # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % redir.title()) newRedir = redir redirList = [] # bookkeeping to detect loops while True: redirList.append( u'%s:%s' % (newRedir.site.lang, newRedir.title(withSection=False))) try: targetPage = newRedir.getRedirectTarget() except pywikibot.IsNotRedirectPage: if len(redirList) == 1: pywikibot.output(u'Skipping: Page %s is not a redirect.' % redir.title(asLink=True)) break # do nothing elif len(redirList) == 2: pywikibot.output( u'Skipping: Redirect target %s is not a redirect.' % newRedir.title(asLink=True)) break # do nothing else: pass # target found except pywikibot.SectionError: pywikibot.warning( u"Redirect target section %s doesn't exist." % newRedir.title(asLink=True)) except (pywikibot.CircularRedirect, pywikibot.InterwikiRedirectPage) as e: pywikibot.exception(e) pywikibot.output(u"Skipping %s." % newRedir) break except pywikibot.BadTitle as e: # str(e) is in the format 'BadTitle: [[Foo]]' pywikibot.warning( u'Redirect target %s is not a valid page title.' % str(e)[10:]) break except pywikibot.NoPage: if len(redirList) == 1: pywikibot.output(u'Skipping: Page %s does not exist.' % redir.title(asLink=True)) break else: if self.getOption('always'): pywikibot.output( u"Skipping: Redirect target %s doesn't exist." % newRedir.title(asLink=True)) break # skip if automatic else: pywikibot.warning( u"Redirect target %s doesn't exist." % newRedir.title(asLink=True)) except pywikibot.ServerError: pywikibot.output(u'Skipping due to server error: ' u'No textarea found') break else: pywikibot.output(u' Links to: %s.' % targetPage.title(asLink=True)) try: mw_msg = targetPage.site.mediawiki_message( 'wikieditor-toolbar-tool-redirect-example') except KeyError: pass else: if targetPage.title() == mw_msg: pywikibot.output( u"Skipping toolbar example: Redirect source is " u"potentially vandalized.") break # watch out for redirect loops if redirList.count(u'%s:%s' % (targetPage.site.lang, targetPage.title(withSection=False))): pywikibot.warning( u'Redirect target %s forms a redirect loop.' % targetPage.title(asLink=True)) break # FIXME: doesn't work. edits twice! try: content = targetPage.get(get_redirect=True) except pywikibot.SectionError: content_page = pywikibot.Page( targetPage.site, targetPage.title(withSection=False)) content = content_page.get(get_redirect=True) if i18n.twhas_key( targetPage.site, 'redirect-broken-redirect-template') and \ i18n.twhas_key(targetPage.site, 'redirect-remove-loop'): pywikibot.output(u"Tagging redirect for deletion") # Delete the two redirects content = i18n.twtranslate( targetPage.site, 'redirect-broken-redirect-template' ) + "\n" + content summ = i18n.twtranslate(targetPage.site, 'redirect-remove-loop') targetPage.put(content, summ) redir.put(content, summ) break else: # redirect target found if targetPage.isStaticRedirect(): pywikibot.output( u" Redirect target is STATICREDIRECT.") pass else: newRedir = targetPage continue try: oldText = redir.get(get_redirect=True) except pywikibot.BadTitle: pywikibot.output(u"Bad Title Error") break if self.is_repo and redir.namespace() == self.repo.item_namespace: redir = pywikibot.ItemPage(self.repo, redir.title()) targetPage = pywikibot.ItemPage(self.repo, targetPage.title()) pywikibot.output('Fixing double item redirect') redir.set_redirect_target(targetPage) break redir.set_redirect_target(targetPage, keep_section=True, save=False) summary = i18n.twtranslate(self.site, 'redirect-fix-double', {'to': targetPage.title(asLink=True)}) pywikibot.showDiff(oldText, redir.text) if self.user_confirm(u'Do you want to accept the changes?'): try: redir.save(summary) except pywikibot.LockedPage: pywikibot.output(u'%s is locked.' % redir.title()) except pywikibot.SpamfilterError as error: pywikibot.output( u"Saving page [[%s]] prevented by spam filter: %s" % (redir.title(), error.url)) except pywikibot.PageNotSaved as error: pywikibot.output(u"Saving page [[%s]] failed: %s" % (redir.title(), error)) except pywikibot.NoUsername: pywikibot.output( u"Page [[%s]] not saved; sysop privileges required." % redir.title()) except pywikibot.Error as error: pywikibot.output( u"Unexpected error occurred trying to save [[%s]]: %s" % (redir.title(), error)) break
def delete_1_broken_redirect(self, redir_name): if isinstance(redir_name, basestring): redir_page = pywikibot.Page(self.site, redir_name) else: redir_page = redir_name # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % redir_page.title()) try: targetPage = redir_page.getRedirectTarget() except pywikibot.IsNotRedirectPage: pywikibot.output(u'%s is not a redirect.' % redir_page.title()) except pywikibot.NoPage: pywikibot.output(u'%s doesn\'t exist.' % redir_page.title()) except pywikibot.InvalidTitle: pywikibot.exception() else: try: targetPage.get() except pywikibot.BadTitle as e: pywikibot.warning( u'Redirect target %s is not a valid page title.' % str(e)[10:]) pass except pywikibot.InvalidTitle: pywikibot.exception() pass except pywikibot.NoPage: movedTarget = None try: movedTarget = targetPage.moved_target() except pywikibot.NoMoveTarget: pass if movedTarget: if not movedTarget.exists(): # FIXME: Test to another move pywikibot.output(u'Target page %s does not exist' % (movedTarget)) elif redir_name == movedTarget.title(): pywikibot.output(u'Target page forms a redirect loop') else: pywikibot.output(u'%s has been moved to %s' % (redir_page, movedTarget)) reason = i18n.twtranslate( self.site, 'redirect-fix-broken-moved', {'to': movedTarget.title(asLink=True)}) content = redir_page.get(get_redirect=True) redir_page.set_redirect_target(movedTarget, keep_section=True, save=False) pywikibot.showDiff(content, redir_page.text) pywikibot.output(u'Summary - %s' % reason) if self.user_confirm( u'Redirect target %s has been moved to %s.\n' u'Do you want to fix %s?' % (targetPage, movedTarget, redir_page)): try: redir_page.save(reason) except pywikibot.NoUsername: pywikibot.output( u"Page [[%s]] not saved; " u"sysop privileges required." % redir_page.title()) pass except pywikibot.LockedPage: pywikibot.output(u'%s is locked.' % redir_page.title()) pass elif self.getOption('delete') and self.user_confirm( u'Redirect target %s does not exist.\n' u'Do you want to delete %s?' % (targetPage.title(asLink=True), redir_page.title(asLink=True))): reason = i18n.twtranslate(self.site, 'redirect-remove-broken') if self.site.logged_in(sysop=True): redir_page.delete(reason, prompt=False) else: assert targetPage.site == self.site, ( u'target page is on different site %s' % targetPage.site) if i18n.twhas_key(self.site, 'redirect-broken-redirect-template'): pywikibot.output(u"No sysop in user-config.py, " u"put page to speedy deletion.") content = redir_page.get(get_redirect=True) # TODO: Add bot's signature if needed # Not supported via TW yet content = i18n.twtranslate( targetPage.site, 'redirect-broken-redirect-template' ) + "\n" + content try: redir_page.put(content, reason) except pywikibot.PageSaveRelatedError as e: pywikibot.error(e) else: pywikibot.output( u'No speedy deletion template available') else: pywikibot.output( u'Cannot fix or delete the broken redirect') except pywikibot.IsRedirectPage: pywikibot.output(u"Redirect target %s is also a redirect! " u"Won't delete anything." % targetPage.title(asLink=True)) else: # we successfully get the target page, meaning that # it exists and is not a redirect: no reason to touch it. pywikibot.output( u'Redirect target %s does exist! Won\'t delete anything.' % targetPage.title(asLink=True))
def fix_1_double_redirect(self, redir_name): if isinstance(redir_name, basestring): redir = pywikibot.Page(self.site, redir_name) else: redir = redir_name # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % redir.title()) newRedir = redir redirList = [] # bookkeeping to detect loops while True: redirList.append(u'%s:%s' % (newRedir.site.lang, newRedir.title(withSection=False))) try: targetPage = newRedir.getRedirectTarget() except pywikibot.IsNotRedirectPage: if len(redirList) == 1: pywikibot.output(u'Skipping: Page %s is not a redirect.' % redir.title(asLink=True)) break # do nothing elif len(redirList) == 2: pywikibot.output( u'Skipping: Redirect target %s is not a redirect.' % newRedir.title(asLink=True)) break # do nothing else: pass # target found except pywikibot.SectionError: pywikibot.warning( u"Redirect target section %s doesn't exist." % newRedir.title(asLink=True)) except (pywikibot.CircularRedirect, pywikibot.InterwikiRedirectPage) as e: pywikibot.exception(e) pywikibot.output(u"Skipping %s." % newRedir) break except pywikibot.BadTitle as e: # str(e) is in the format 'BadTitle: [[Foo]]' pywikibot.warning( u'Redirect target %s is not a valid page title.' % str(e)[10:]) break except pywikibot.NoPage: if len(redirList) == 1: pywikibot.output(u'Skipping: Page %s does not exist.' % redir.title(asLink=True)) break else: if self.getOption('always'): pywikibot.output( u"Skipping: Redirect target %s doesn't exist." % newRedir.title(asLink=True)) break # skip if automatic else: pywikibot.warning( u"Redirect target %s doesn't exist." % newRedir.title(asLink=True)) except pywikibot.ServerError: pywikibot.output(u'Skipping due to server error: ' u'No textarea found') break else: pywikibot.output( u' Links to: %s.' % targetPage.title(asLink=True)) try: mw_msg = targetPage.site.mediawiki_message( 'wikieditor-toolbar-tool-redirect-example') except KeyError: pass else: if targetPage.title() == mw_msg: pywikibot.output( u"Skipping toolbar example: Redirect source is " u"potentially vandalized.") break # watch out for redirect loops if redirList.count(u'%s:%s' % (targetPage.site.lang, targetPage.title(withSection=False))): pywikibot.warning( u'Redirect target %s forms a redirect loop.' % targetPage.title(asLink=True)) break # FIXME: doesn't work. edits twice! try: content = targetPage.get(get_redirect=True) except pywikibot.SectionError: content_page = pywikibot.Page( targetPage.site, targetPage.title(withSection=False)) content = content_page.get(get_redirect=True) if i18n.twhas_key( targetPage.site, 'redirect-broken-redirect-template') and \ i18n.twhas_key(targetPage.site, 'redirect-remove-loop'): pywikibot.output(u"Tagging redirect for deletion") # Delete the two redirects content = i18n.twtranslate( targetPage.site, 'redirect-broken-redirect-template' ) + "\n" + content summ = i18n.twtranslate(targetPage.site, 'redirect-remove-loop') targetPage.put(content, summ) redir.put(content, summ) break else: # redirect target found if targetPage.isStaticRedirect(): pywikibot.output( u" Redirect target is STATICREDIRECT.") pass else: newRedir = targetPage continue try: oldText = redir.get(get_redirect=True) except pywikibot.BadTitle: pywikibot.output(u"Bad Title Error") break redir.set_redirect_target(targetPage, keep_section=True, save=False) summary = i18n.twtranslate(self.site, 'redirect-fix-double', {'to': targetPage.title(asLink=True)} ) pywikibot.showDiff(oldText, redir.text) if self.user_confirm(u'Do you want to accept the changes?'): try: redir.save(summary) except pywikibot.LockedPage: pywikibot.output(u'%s is locked.' % redir.title()) except pywikibot.SpamfilterError as error: pywikibot.output( u"Saving page [[%s]] prevented by spam filter: %s" % (redir.title(), error.url)) except pywikibot.PageNotSaved as error: pywikibot.output(u"Saving page [[%s]] failed: %s" % (redir.title(), error)) except pywikibot.NoUsername: pywikibot.output( u"Page [[%s]] not saved; sysop privileges required." % redir.title()) except pywikibot.Error as error: pywikibot.output( u"Unexpected error occurred trying to save [[%s]]: %s" % (redir.title(), error)) break
def delete_1_broken_redirect(self, redir_name): if isinstance(redir_name, basestring): redir_page = pywikibot.Page(self.site, redir_name) else: redir_page = redir_name # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % redir_page.title()) try: targetPage = redir_page.getRedirectTarget() except pywikibot.IsNotRedirectPage: pywikibot.output(u'%s is not a redirect.' % redir_page.title()) except pywikibot.NoPage: pywikibot.output(u'%s doesn\'t exist.' % redir_page.title()) else: try: targetPage.get() except pywikibot.BadTitle as e: pywikibot.warning( u'Redirect target %s is not a valid page title.' % str(e)[10:]) pass except pywikibot.InvalidTitle: pywikibot.exception() pass except pywikibot.NoPage: movedTarget = None try: movedTarget = targetPage.moved_target() except pywikibot.NoMoveTarget: pass if movedTarget: if not movedTarget.exists(): # FIXME: Test to another move pywikibot.output(u'Target page %s does not exist' % (movedTarget)) elif redir_name == movedTarget.title(): pywikibot.output(u'Target page forms a redirect loop') else: pywikibot.output(u'%s has been moved to %s' % (redir_page, movedTarget)) reason = i18n.twtranslate(self.site, 'redirect-fix-broken-moved', {'to': movedTarget.title( asLink=True)}) content = redir_page.get(get_redirect=True) redir_page.set_redirect_target( movedTarget, keep_section=True, save=False) pywikibot.showDiff(content, redir_page.text) pywikibot.output(u'Summary - %s' % reason) if self.user_confirm( u'Redirect target %s has been moved to %s.\n' u'Do you want to fix %s?' % (targetPage, movedTarget, redir_page)): try: redir_page.save(reason) except pywikibot.NoUsername: pywikibot.output(u"Page [[%s]] not saved; " u"sysop privileges required." % redir_page.title()) pass except pywikibot.LockedPage: pywikibot.output(u'%s is locked.' % redir_page.title()) pass elif self.getOption('delete') and self.user_confirm( u'Redirect target %s does not exist.\n' u'Do you want to delete %s?' % (targetPage.title(asLink=True), redir_page.title(asLink=True))): reason = i18n.twtranslate(self.site, 'redirect-remove-broken') if self.site.logged_in(sysop=True): redir_page.delete(reason, prompt=False) else: assert targetPage.site == self.site, ( u'target page is on different site %s' % targetPage.site) if i18n.twhas_key(self.site, 'redirect-broken-redirect-template'): pywikibot.output(u"No sysop in user-config.py, " u"put page to speedy deletion.") content = redir_page.get(get_redirect=True) # TODO: Add bot's signature if needed # Not supported via TW yet content = i18n.twtranslate( targetPage.site, 'redirect-broken-redirect-template' ) + "\n" + content try: redir_page.put(content, reason) except pywikibot.PageSaveRelatedError as e: pywikibot.error(e) else: pywikibot.output( u'No speedy deletion template available') else: pywikibot.output(u'Cannot fix or delete the broken redirect') except pywikibot.IsRedirectPage: pywikibot.output(u"Redirect target %s is also a redirect! " u"Won't delete anything." % targetPage.title(asLink=True)) else: # we successfully get the target page, meaning that # it exists and is not a redirect: no reason to touch it. pywikibot.output( u'Redirect target %s does exist! Won\'t delete anything.' % targetPage.title(asLink=True))