Ladsgroup has uploaded a new change for review. https://gerrit.wikimedia.org/r/175427
Change subject: [T56567] Add -only and -onlytitle options to add_text.py ...................................................................... [T56567] Add -only and -onlytitle options to add_text.py It makes the bot works only when the given regex matches the title or text of the page. Change-Id: Ifd3e92ff66b9e3a998193982a886d7ed13f6b64c --- M scripts/add_text.py 1 file changed, 32 insertions(+), 4 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/27/175427/1 diff --git a/scripts/add_text.py b/scripts/add_text.py index a9489dc..8fca22d 100644 --- a/scripts/add_text.py +++ b/scripts/add_text.py @@ -38,6 +38,12 @@ -noreorder Avoid to reorder cats and interwiki +-only If used, only works in pages that given regex matches in + the text. + +-onlytitle If used, only works in pages that given regex matches in + the title. + --- Example --- 1. # This is a script to add a template to the top of the pages with @@ -117,9 +123,9 @@ ] -def add_text(page, addText, summary=None, regexSkip=None, - regexSkipUrl=None, always=False, up=False, putText=True, - oldTextGiven=None, reorderEnabled=True, create=False): +def add_text(page, addText, summary=None, regexSkip=None, regexSkipUrl=None, + always=False, up=False, putText=True, only=True, + oldTextGiven=None, reorderEnabled=True, create=False): """ Add text to a page. @@ -171,6 +177,12 @@ u'''Exception! regex (or word) used with -except is in the page. Skip! Match was: %s''' % result) return (False, False, always) + if only is not None: + if not re.search(only, text): + pywikibot.output( +u'''Exception! regex (or word) used with -only isn't in the page. Skip!''') + return (False, False, always) + # If not up, text put below if not up: newtext = text @@ -296,6 +308,8 @@ textfile = None talkPage = False reorderEnabled = True + only = None + onlytitle = None namespaces = [] # Put the text above or below the text? @@ -340,6 +354,16 @@ regexSkip = pywikibot.input(u'What text should I skip?') else: regexSkip = arg[8:] + elif arg.startswith('-onlytitle'): + if len(arg) == 10: + onlytitle = pywikibot.input(u'What titles should I get?') + else: + onlytitle = arg[11:] + elif arg.startswith('-only'): + if len(arg) == 5: + only = pywikibot.input(u'What texts should I get?') + else: + only = arg[6:] elif arg == '-up': up = True elif arg == '-noreorder': @@ -371,8 +395,12 @@ generator = pagegenerators.NamespaceFilterPageGenerator( generator, namespaces) for page in generator: + if onlytitle and not re.search(onlytitle, page.title()): + pywikibot.output( + u"The title %s doesn't match the given regex" % page.title()) + continue (text, newtext, always) = add_text(page, addText, summary, regexSkip, - regexSkipUrl, always, up, True, + regexSkipUrl, always, up, True, only, reorderEnabled=reorderEnabled, create=talkPage) -- To view, visit https://gerrit.wikimedia.org/r/175427 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ifd3e92ff66b9e3a998193982a886d7ed13f6b64c Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits