def check_protection_level(operation, level, levels, default=None): """Check if the protection level is valid or ask if necessary. @return: a valid protection level @rtype: string """ if level not in levels: first_char = [] default_char = None num = 1 for level in levels: for c in level: if c not in first_char: first_char.append(c) break else: first_char.append(str(num)) num += 1 if level == default: default_char = first_char[-1] choice = pywikibot.input_choice('Choice a protection level to %s:' % operation, zip(levels, first_char), default=default_char) return levels[first_char.index(choice)] else: return level
def treat_page(self): """Capitalize redirects of the current page.""" page_t = self.current_page.title() site = self.current_page.site if self.getOption('titlecase'): page_cap = pywikibot.Page(site, page_t.title()) else: page_cap = pywikibot.Page(site, page_t.capitalize()) if page_cap.exists(): pywikibot.output(u'%s already exists, skipping...\n' % page_cap.title(asLink=True)) else: pywikibot.output(u'%s doesn\'t exist' % page_cap.title(asLink=True)) if not self.getOption('always'): choice = pywikibot.input_choice( u'Do you want to create a redirect?', [('Yes', 'y'), ('No', 'n'), ('All', 'a')], 'n') if choice == 'a': self.options['always'] = True if self.getOption('always') or choice == 'y': comment = i18n.twtranslate( site, 'capitalize_redirects-create-redirect', {'to': page_t}) page_cap.set_redirect_target(self.current_page, create=True, summary=comment)
def update_or_create_page(self, old_page, new_text): """ Reads the current text of page old_page, compare it with new_text, prompts the user, and uploads the page """ # Read the original content old_text = old_page.get() # Give the user some context if old_text != new_text: pywikibot.output(new_text) pywikibot.showDiff(old_text, new_text) # Get a decision prompt = u'Modify this page ?' # Did anything change ? if old_text == new_text: pywikibot.output(u'No changes necessary to %s' % old_page.title()); else: if not self.acceptall: choice = pywikibot.input_choice(u'Do you want to accept these changes?', [('Yes', 'Y'), ('No', 'n'), ('All', 'a')], 'N') if choice == 'a': self.acceptall = True if self.acceptall or choice == 'y': # Write out the new version old_page.put(new_text, summary)
def treat(self, page): if not page.exists(): return if page.isRedirectPage(): page = page.getRedirectTarget() page_t = page.title() self.current_page = page if self.getOption('titlecase'): page_cap = pywikibot.Page(page.site, page_t.title()) else: page_cap = pywikibot.Page(page.site, page_t.capitalize()) if page_cap.exists(): pywikibot.output(u'%s already exists, skipping...\n' % page_cap.title(asLink=True)) else: pywikibot.output(u'%s doesn\'t exist' % page_cap.title(asLink=True)) if not self.getOption('always'): choice = pywikibot.input_choice( u'Do you want to create a redirect?', [('Yes', 'y'), ('No', 'n'), ('All', 'a')], 'n') if choice == 'a': self.options['always'] = True if self.getOption('always') or choice == 'y': comment = i18n.twtranslate( page.site, 'capitalize_redirects-create-redirect', {'to': page_t}) page_cap.text = u"#%s %s" % (page.site.redirect(), page.title(asLink=True, textlink=True)) try: page_cap.save(comment) except: pywikibot.output(u"An error occurred, skipping...")
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: list of unicode """ generator = None local_args = pywikibot.handle_args(args) site = pywikibot.Site() if site.code != 'commons' or site.family.name != 'commons': pywikibot.warning('This script is primarily written for Wikimedia ' 'Commons, but has been invoked with site {0}. It ' 'might work for other sites but there is no ' 'guarantee that it does the right thing.'.format(site)) choice = pywikibot.input_choice( 'How do you want to continue?', (('Continue using {0}'.format(site), 'c'), ('Switch to Wikimedia Commons', 's'), ('Quit', 'q')), automatic_quit=False) if choice == 's': site = pywikibot.Site('commons', 'commons') elif choice == 'q': return False genFactory = pagegenerators.GeneratorFactory(site) for arg in local_args: if arg.startswith('-yesterday'): generator = uploadedYesterday(site) issue_deprecation_warning( 'The usage of "-yesterday"', '-logevents:"upload,,YYYYMMDD,YYYYMMDD"', 2, ArgumentDeprecationWarning) elif arg.startswith('-recentchanges'): generator = recentChanges(site=site, delay=120) else: genFactory.handleArg(arg) generator = genFactory.getCombinedGenerator(gen=generator) if not generator: pywikibot.bot.suggest_help(missing_generator=True) return False else: pregenerator = pagegenerators.PreloadingGenerator(generator) site.login() for page in pregenerator: pywikibot.output(page.title()) if page.exists() and (page.namespace() == 6) \ and (not page.isRedirectPage()): if isUncat(page): addUncat(page) return True
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: str """ generator = None local_args = pywikibot.handle_args(args) site = pywikibot.Site() if site.code != 'commons' or site.family.name != 'commons': pywikibot.warning( 'This script is primarily written for Wikimedia ' 'Commons, but has been invoked with site {0}. It ' 'might work for other sites but there is no ' 'guarantee that it does the right thing.'.format(site)) choice = pywikibot.input_choice( 'How do you want to continue?', (('Continue using {0}'.format(site), 'c'), ('Switch to Wikimedia Commons', 's'), ('Quit', 'q')), automatic_quit=False) if choice == 's': site = pywikibot.Site('commons', 'commons') elif choice == 'q': return False gen_factory = pagegenerators.GeneratorFactory(site) for arg in local_args: param_arg, sep, param_value = arg.partition(':') if param_value == '': param_value = None if arg.startswith('-yesterday'): generator = uploadedYesterday(site) issue_deprecation_warning('The usage of "-yesterday"', '-logevents:"upload,,YYYYMMDD,YYYYMMDD"', 2, ArgumentDeprecationWarning, since='20160305') else: gen_factory.handle_arg(arg) generator = gen_factory.getCombinedGenerator(gen=generator, preload=True) if not generator: pywikibot.bot.suggest_help(missing_generator=True) else: site.login() for page in generator: pywikibot.output(page.title()) if page.exists() and (page.namespace() == 6) \ and (not page.isRedirectPage()): if isUncat(page): addUncat(page)
def PickTarget(self, title, original, candidates): """Pick target from candidates.""" if not candidates: return None if len(candidates) == 1: return candidates[0] pagesDontExist = [] pagesRedir = {} pagesExist = [] for newTitle in candidates: dst = self.Page(newTitle) if not dst.exists(): pagesDontExist.append(newTitle) elif dst.isRedirectPage(): pagesRedir[newTitle] = dst.getRedirectTarget().title() else: pagesExist.append(newTitle) if len(pagesExist) == 1: return pagesExist[0] if not pagesExist and pagesRedir: if len(pagesRedir) == 1: return list(pagesRedir.keys())[0] t = None for v in pagesRedir.values(): if not t: t = v # first item elif t != v: break else: # all redirects point to the same target # pick the first one, doesn't matter what it is return list(pagesRedir.keys())[0] if not self.autonomous: pywikibot.output('Could not auto-decide for page {}. Which link ' 'should be chosen?'.format( self.MakeLink(title, False))) pywikibot.output('Original title: ', newline=False) self.ColorCodeWord(original + '\n', True) for count, t in enumerate(candidates, 1): if t in pagesDontExist: msg = 'missing' elif t in pagesRedir: msg = 'Redirect to ' + pagesRedir[t] else: msg = 'page exists' self.ColorCodeWord(' {}: {} ({})\n'.format(count, t, msg), True) answers = [('skip', 's')] + [(str(i), i) for i in range(1, count)] choice = pywikibot.input_choice('Which link to choose?', answers) if choice != 's': return candidates[int(choice) - 1]
def handleNextLink(self, text, match, context=100): """ Return a tuple (text, jumpToBeginning). text is the unicode string after the current link has been processed. jumpToBeginning is a boolean which specifies if the cursor position should be reset to 0. This is required after the user has edited the article. """ # ignore interwiki links and links to sections of the same page as well # as section links if not match.group('title') \ or self.pageToUnlink.site.isInterwikiLink(match.group('title')) \ or match.group('section'): return text, False linkedPage = pywikibot.Page(self.pageToUnlink.site, match.group('title')) # Check whether the link found is to the current page itself. if linkedPage != self.pageToUnlink: # not a self-link return text, False else: # at the beginning of the link, start red color. # at the end of the link, reset the color to default if self.getOption('always'): choice = 'a' else: pywikibot.output( text[max(0, match.start() - context):match.start()] + '\03{lightred}' + text[match.start():match.end()] + '\03{default}' + text[match.end():match.end() + context]) choice = pywikibot.input_choice( u'\nWhat shall be done with this link?\n', [('unlink', 'u'), ('skip', 's'), ('edit', 'e'), ('more context', 'm'), ('unlink all', 'a')], 'u') pywikibot.output(u'') if choice == 's': # skip this link return text, False elif choice == 'e': editor = TextEditor() newText = editor.edit(text, jumpIndex=match.start()) # if user didn't press Cancel if newText: return newText, True else: return text, True elif choice == 'm': # show more context by recursive self-call return self.handleNextLink(text, match, context=context + 100) elif choice == 'a': self.options['always'] = True new = match.group('label') or match.group('title') new += match.group('linktrail') return text[:match.start()] + new + text[match.end():], False
def treat(self, page): """Treat a page. @param page: The page to treat @type page: pywikibot.page.BasePage """ # Current content language of the page and site language parameters = { 'action': 'query', 'prop': 'info', 'titles': page.title(), 'meta': 'siteinfo' } r = self.site._simple_request(**parameters) langcheck = r.submit()['query'] currentlang = '' for k in langcheck['pages']: currentlang = langcheck['pages'][k]['pagelanguage'] sitelang = langcheck['general']['lang'] if self.opt.setlang == currentlang: pywikibot.output( color_format( '{lightpurple}{0}{default}: This page is already set to ' '{green}{1}{default}; skipping.', page.title(as_link=True), self.opt.setlang)) elif currentlang == sitelang or self.opt.always: self.changelang(page) elif self.opt.never: pywikibot.output( color_format( '{lightpurple}{0}{default}: This page already has a ' 'different content language {yellow}{1}{default} set; ' 'skipping.', page.title(as_link=True), currentlang)) else: pywikibot.output( color_format('\n\n>>> {lightpurple}{0}{default} <<<', page.title())) choice = pywikibot.input_choice(color_format( 'The content language for this page is already set to ' '{yellow}{0}{default}, which is different from the ' 'default ({1}). Change it to {green}{2}{default} anyway?', currentlang, sitelang, self.opt.setlang), [('Always', 'a'), ('Yes', 'y'), ('No', 'n'), ('Never', 'v')], default='Y') if choice == 'a': self.opt.always = True elif choice == 'v': self.opt.never = True if choice in 'ay': self.changelang(page) else: pywikibot.output('Skipping ...\n')
def reportBadAccount(self, name=None, final=False): # Queue process if name: if globalvar.confirm: answer = pywikibot.input_choice( "%s may have an unwanted username, do you want to report " "this user?" % name, [("Yes", "y"), ("No", "n"), ("All", "a")], "n", automatic_quit=False, ) if answer in ["a", "all"]: answer = "y" globalvar.confirm = False else: answer = "y" if answer.lower() in ["yes", "y"] or not globalvar.confirm: showStatus() pywikibot.output("%s is possibly an unwanted username. It will be reported." % name) if hasattr(self, "_BAQueue"): self._BAQueue.append(name) else: self._BAQueue = [name] if len(self._BAQueue) >= globalvar.dumpToLog or final: rep_text = "" # name in queue is max, put detail to report page pywikibot.output("Updating badname accounts to report page...") rep_page = pywikibot.Page(self.site, i18n.translate(self.site, report_page)) if rep_page.exists(): text_get = rep_page.get() else: text_get = "This is a report page for the Bad-username, please translate me. --~~~" pos = 0 # The talk page includes "_" between the two names, in this way i # replace them to " ". for usrna in self._BAQueue: username = pywikibot.url2link(usrna, self.site, self.site) n = re.compile(re.escape(username), re.UNICODE) y = n.search(text_get, pos) if y: pywikibot.output("%s is already in the report page." % username) else: # Adding the log. rep_text += i18n.translate(self.site, report_text) % username if self.site.code == "it": rep_text = "%s%s}}" % (rep_text, self.bname[username]) com = i18n.twtranslate(self.site, "welcome-bad_username") if rep_text != "": rep_page.put(text_get + rep_text, summary=com, force=True, minorEdit=True) showStatus(5) pywikibot.output("Reported") self.BAQueue = list() else: return True
def invoke_editor(self, page): """Ask for an editor and invoke it.""" choice = pywikibot.input_choice('Do you want to open the page?', [('with browser', 'b'), ('with gui', 'g'), ('no', 'n')], 'n') if choice == 'b': webbrowser.open('{}?redirect=no'.format(page.full_url())) elif choice == 'g': editor = TextEditor() editor.edit(page.text)
def _call_input_choice(self): rv = pywikibot.input_choice('question', (('answer 1', 'A'), ('answer 2', 'N'), ('answer 3', 'S')), 'A', automatic_quit=False) self.assertEqual(newstdout.getvalue(), '') self.assertIsInstance(rv, str) return rv
def _call_input_choice(self): rv = pywikibot.input_choice( "question", (("answer 1", "A"), ("answer 2", "N"), ("answer 3", "S")), "A", automatic_quit=False ) self.assertEqual(newstdout.getvalue(), "") self.assertIsInstance(rv, unicode) return rv
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: list of unicode """ generator = None local_args = pywikibot.handle_args(args) site = pywikibot.Site() if site.code != 'commons' or site.family.name != 'commons': pywikibot.warning( 'This script is primarily written for Wikimedia ' 'Commons, but has been invoked with site {0}. It ' 'might work for other sites but there is no ' 'guarantee that it does the right thing.'.format(site)) choice = pywikibot.input_choice( 'How do you want to continue?', (('Continue using {0}'.format(site), 'c'), ('Switch to Wikimedia Commons', 's'), ('Quit', 'q')), automatic_quit=False) if choice == 's': site = pywikibot.Site('commons', 'commons') elif choice == 'q': return False genFactory = pagegenerators.GeneratorFactory(site) for arg in local_args: if arg.startswith('-yesterday'): generator = uploadedYesterday(site) elif arg.startswith('-recentchanges'): generator = recentChanges(site=site, delay=120) else: genFactory.handleArg(arg) generator = genFactory.getCombinedGenerator(gen=generator) if not generator: pywikibot.bot.suggest_help(missing_generator=True) return False else: pregenerator = pagegenerators.PreloadingGenerator(generator) site.login() for page in pregenerator: pywikibot.output(page.title()) if page.exists() and (page.namespace() == 6) \ and (not page.isRedirectPage()): if isUncat(page): addUncat(page) return True
def find_alternates(filename, script_paths): """Search for similar filenames in the given script paths.""" from pywikibot import config, input_choice, output from pywikibot.bot import ShowingListOption, QuitKeyboardInterrupt from pywikibot.tools.formatter import color_format assert config.pwb_close_matches > 0, \ 'config.pwb_close_matches must be greater than 0' assert 0.0 < config.pwb_cut_off < 1.0, \ 'config.pwb_cut_off must be a float in range [0, 1]' print('ERROR: {} not found! Misspelling?'.format(filename), file=sys.stderr) scripts = {} script_paths = [['.']] + script_paths # add current directory for path in script_paths: for script_name in os.listdir(os.path.join(*path)): # remove .py for better matching name, _, suffix = script_name.rpartition('.') if suffix == 'py' and not name.startswith('__'): scripts[name] = os.path.join(*(path + [script_name])) filename = filename[:-3] similar_scripts = get_close_matches(filename, scripts, config.pwb_close_matches, config.pwb_cut_off) if not similar_scripts: return None if len(similar_scripts) == 1: script = similar_scripts[0] wait_time = config.pwb_autostart_waittime output( color_format( 'NOTE: Starting the most similar script ' '{lightyellow}{0}.py{default}\n' ' in {1} seconds; type CTRL-C to stop.', script, wait_time)) try: sleep(wait_time) # Wait a bit to let it be cancelled except KeyboardInterrupt: return None else: msg = '\nThe most similar scripts are:' alternatives = ShowingListOption(similar_scripts, pre=msg, post='') try: prefix, script = input_choice('Which script to be run:', alternatives, default='1') except QuitKeyboardInterrupt: return None print() return scripts[script]
def review_hunks(self): """Review hunks.""" help_msg = [ 'y -> accept this hunk', 'n -> do not accept this hunk', 's -> do not accept this hunk and stop reviewing', 'a -> accept this hunk and all other pending', 'r -> review later', 'h -> help', ] question = 'Accept this hunk?' answers = [('yes', 'y'), ('no', 'n'), ('stop', 's'), ('all', 'a'), ('review', 'r'), ('help', 'h')] actions = { 'y': Hunk.APPR, 'n': Hunk.NOT_APPR, 's': Hunk.NOT_APPR, 'a': Hunk.APPR, 'r': Hunk.PENDING, } pending = [h for h in self.hunks if h.reviewed == h.PENDING] while pending: hunk = pending.pop(0) pywikibot.output(hunk.header + hunk.diff_text) choice = pywikibot.input_choice(question, answers, default='r', automatic_quit=False) if choice in actions.keys(): hunk.reviewed = actions[choice] if choice == 's': while pending: hunk = pending.pop(0) hunk.reviewed = hunk.NOT_APPR break elif choice == 'a': while pending: hunk = pending.pop(0) hunk.reviewed = hunk.APPR break elif choice == 'h': pywikibot.output(u'\03{purple}%s\03{default}' % u'\n'.join(help_msg)) pending.insert(0, hunk) elif choice == 'r': pending.append(hunk) return
def PickTarget(self, title, original, candidates): if len(candidates) == 0: return if len(candidates) == 1: return candidates[0] pagesDontExist = [] pagesRedir = {} pagesExist = [] for newTitle in candidates: dst = self.Page(newTitle) if not dst.exists(): pagesDontExist.append(newTitle) elif dst.isRedirectPage(): pagesRedir[newTitle] = dst.getRedirectTarget().title() else: pagesExist.append(newTitle) if len(pagesExist) == 1: return pagesExist[0] elif len(pagesExist) == 0 and len(pagesRedir) > 0: if len(pagesRedir) == 1: return list(pagesRedir.keys())[0] t = None for v in pagesRedir.values(): if not t: t = v # first item elif t != v: break else: # all redirects point to the same target # pick the first one, doesn't matter what it is return list(pagesRedir.keys())[0] if not self.autonomous: pywikibot.output(u'Could not auto-decide for page %s. Which link ' u'should be chosen?' % self.MakeLink(title, False)) pywikibot.output(u'Original title: ', newline=False) self.ColorCodeWord(original + "\n", True) count = 1 for t in candidates: if t in pagesDontExist: msg = u'missing' elif t in pagesRedir: msg = u'Redirect to ' + pagesRedir[t] else: msg = u'page exists' self.ColorCodeWord(u' %d: %s (%s)\n' % (count, t, msg), True) count += 1 answers = [('skip', 's')] + [(str(i), i) for i in range(1, count)] choice = pywikibot.input_choice(u'Which link to choose?', answers) if choice != 's': return candidates[int(choice) - 1]
def PickTarget(self, title, original, candidates): """Pick target from candidates.""" if len(candidates) == 0: return if len(candidates) == 1: return candidates[0] pagesDontExist = [] pagesRedir = {} pagesExist = [] for newTitle in candidates: dst = self.Page(newTitle) if not dst.exists(): pagesDontExist.append(newTitle) elif dst.isRedirectPage(): pagesRedir[newTitle] = dst.getRedirectTarget().title() else: pagesExist.append(newTitle) if len(pagesExist) == 1: return pagesExist[0] elif len(pagesExist) == 0 and len(pagesRedir) > 0: if len(pagesRedir) == 1: return list(pagesRedir.keys())[0] t = None for v in pagesRedir.values(): if not t: t = v # first item elif t != v: break else: # all redirects point to the same target # pick the first one, doesn't matter what it is return list(pagesRedir.keys())[0] if not self.autonomous: pywikibot.output(u'Could not auto-decide for page %s. Which link ' u'should be chosen?' % self.MakeLink(title, False)) pywikibot.output(u'Original title: ', newline=False) self.ColorCodeWord(original + "\n", True) count = 1 for t in candidates: if t in pagesDontExist: msg = u'missing' elif t in pagesRedir: msg = u'Redirect to ' + pagesRedir[t] else: msg = u'page exists' self.ColorCodeWord(u' %d: %s (%s)\n' % (count, t, msg), True) count += 1 answers = [('skip', 's')] + [(str(i), i) for i in range(1, count)] choice = pywikibot.input_choice(u'Which link to choose?', answers) if choice != 's': return candidates[int(choice) - 1]
def find_alternates(filename, script_paths): """Search for similar filenames in the given script paths.""" from pywikibot import config, input_choice, output from pywikibot.bot import ShowingListOption, QuitKeyboardInterrupt from pywikibot.tools.formatter import color_format assert config.pwb_close_matches > 0, \ 'config.pwb_close_matches must be greater than 0' assert 0.0 < config.pwb_cut_off < 1.0, \ 'config.pwb_cut_off must be a float in range [0, 1]' print('ERROR: {} not found! Misspelling?'.format(filename), file=sys.stderr) scripts = {} for file_package in script_paths: path = file_package.split('.') for script_name in os.listdir(os.path.join(*path)): # remove .py for better matching name, _, suffix = script_name.rpartition('.') if suffix == 'py' and not name.startswith('__'): scripts[name] = os.path.join(*(path + [script_name])) filename = filename[:-3] similar_scripts = get_close_matches(filename, scripts, config.pwb_close_matches, config.pwb_cut_off) if not similar_scripts: return None if len(similar_scripts) == 1: script = similar_scripts[0] wait_time = config.pwb_autostart_waittime output(color_format( 'NOTE: Starting the most similar script ' '{lightyellow}{0}.py{default}\n' ' in {1} seconds; type CTRL-C to stop.', script, wait_time)) try: sleep(wait_time) # Wait a bit to let it be cancelled except KeyboardInterrupt: return None return scripts[script] msg = '\nThe most similar scripts are:' alternatives = ShowingListOption(similar_scripts, pre=msg, post='') try: prefix, script = input_choice('Which script to be run:', alternatives, default='1') except QuitKeyboardInterrupt: return None print() return scripts[script[0]]
def showQuest(page): """Ask for an editor and invoke it.""" quest = pywikibot.input_choice( u'Do you want to open the page?', [('with browser', 'b'), ('with gui', 'g'), ('no', 'n')], 'n', automatic_quit=False) if quest == 'b': webbrowser.open('%s?redirect=no' % page.full_url()) elif quest == 'g': from pywikibot import editor as editarticle editor = editarticle.TextEditor() editor.edit(page.text)
def getRecentEdits(userName,timestamp): recentEdits = site.usercontribs(user=userName,top_only=True,end=timestamp) print u"Number of recent edits for " + userName + " : " + str(len(list(recentEdits))) acceptAll = False for edit in recentEdits: page = pywikibot.Page(site,edit[u'title']) hist = page.getVersionHistory() previous_revision = () for hist_entry in hist: #On renvoit la revision précédant celle(s) de <userName> if hist_entry.user != userName: previous_revision = hist_entry.revid, hist_entry.user break oldVersion = page.getOldVersion(previous_revision[0]) # Affichage du diff pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) pywikibot.showDiff(page.text,oldVersion) summary = u"Révocation massive des modifications de %s (retour à la version de %s)" %(userName,previous_revision[1]) if not acceptAll: choice = pywikibot.input_choice(u'Do you want to accept these changes?', [('Yes', 'y'), ('No', 'n'), ('All', 'a')], 'N') if choice == 'a': acceptAll = True elif choice == 'n': continue if acceptAll or choice == 'y': try: page.text = oldVersion page.save(summary) except pywikibot.EditConflict: pywikibot.output(u"Skipping %s because of edit conflict" % (page.title(),)) except pywikibot.SpamfilterError, e: pywikibot.output(u"Cannot change %s because of blacklist entry %s" % (page.title(), e.url)) except pywikibot.PageNotSaved, error: pywikibot.output(u"Error putting page: %s" % (error.args,))
def treat(self, page): """Re-directing process. Check if pages are in the given form Something, State, and if so, create a redirect from Something, ST.. """ for sn in self.abbrev: R = re.compile(r', %s$' % sn) if R.search(page.title()): pl = pywikibot.Page(self.site, page.title().replace(sn, self.abbrev[sn])) # A bit hacking here - the real work is done in the # 'except pywikibot.NoPage' part rather than the 'try'. try: pl.get(get_redirect=True) goal = pl.getRedirectTarget().title() if pywikibot.Page(self.site, goal).exists(): pywikibot.output( u"Not creating %s - redirect already exists." % goal) else: pywikibot.warning( u"%s already exists but redirects elsewhere!" % goal) except pywikibot.IsNotRedirectPage: pywikibot.warning( u"Page %s already exists and is not a redirect " u"Please check page!" % pl.title()) except pywikibot.NoPage: change = '' if page.isRedirectPage(): p2 = page.getRedirectTarget() pywikibot.output( u'Note: goal page is redirect.\nCreating redirect ' u'to "%s" to avoid double redirect.' % p2.title()) else: p2 = page if self.force: change = 'y' else: change = pywikibot.input_choice( u'Create redirect %s?' % pl.title(), (('yes', 'y'), ('no', 'n'))) if change == 'y': pl.set_redirect_target(p2, create=True, summary=i18n.twtranslate( self.site, 'states_redirect-comment'))
def _call_input_choice(self): rv = pywikibot.input_choice( 'question', (('answer 1', u'A'), ('answer 2', u'N'), ('answer 3', u'S')), u'A', automatic_quit=False) self.assertEqual(newstdout.getvalue(), '') self.assertIsInstance(rv, unicode) return rv
def treat(self, page): """Re-directing process. Check if pages are in the given form Something, State, and if so, create a redirect from Something, ST.. """ for sn in self.abbrev: R = re.compile(r', %s$' % sn) if R.search(page.title()): pl = pywikibot.Page(self.site, page.title().replace(sn, self.abbrev[sn])) # A bit hacking here - the real work is done in the # 'except pywikibot.NoPage' part rather than the 'try'. try: pl.get(get_redirect=True) goal = pl.getRedirectTarget().title() if pywikibot.Page(self.site, goal).exists(): pywikibot.output( u"Not creating %s - redirect already exists." % goal) else: pywikibot.warning( u"%s already exists but redirects elsewhere!" % goal) except pywikibot.IsNotRedirectPage: pywikibot.warning( u"Page %s already exists and is not a redirect " u"Please check page!" % pl.title()) except pywikibot.NoPage: change = '' if page.isRedirectPage(): p2 = page.getRedirectTarget() pywikibot.output( u'Note: goal page is redirect.\nCreating redirect ' u'to "%s" to avoid double redirect.' % p2.title()) else: p2 = page if self.force: change = 'y' else: change = pywikibot.input_choice( u'Create redirect %s?' % pl.title(), (('yes', 'y'), ('no', 'n'))) if change == 'y': pl.set_redirect_target( p2, create=True, summary=i18n.twtranslate(self.site, 'states_redirect-comment'))
def main(): site = pywikibot.Site(user='******') site.login() while True: bot = MaintainCategoryRobot(site) bot.make_list() bot.run() choice = pywikibot.input_choice('他のカテゴリで続行しますか', [('はい', 'y'), ('いいえ', 'n')]) if choice == 'y': continue if choice == 'n': break
def review_hunks(self): """Review hunks.""" help_msg = ['y -> accept this hunk', 'n -> do not accept this hunk', 's -> do not accept this hunk and stop reviewing', 'a -> accept this hunk and all other pending', 'r -> review later', 'h -> help', ] question = 'Accept this hunk?' answers = [('yes', 'y'), ('no', 'n'), ('stop', 's'), ('all', 'a'), ('review', 'r'), ('help', 'h')] actions = {'y': Hunk.APPR, 'n': Hunk.NOT_APPR, 's': Hunk.NOT_APPR, 'a': Hunk.APPR, 'r': Hunk.PENDING, } pending = [h for h in self.hunks if h.reviewed == h.PENDING] while pending: hunk = pending.pop(0) pywikibot.output(hunk.header + hunk.diff_text) choice = pywikibot.input_choice(question, answers, default='r', automatic_quit=False) if choice in actions.keys(): hunk.reviewed = actions[choice] if choice == 's': while pending: hunk = pending.pop(0) hunk.reviewed = hunk.NOT_APPR break elif choice == 'a': while pending: hunk = pending.pop(0) hunk.reviewed = hunk.APPR break elif choice == 'h': pywikibot.output(u'\03{purple}%s\03{default}' % u'\n'.join(help_msg)) pending.insert(0, hunk) elif choice == 'r': pending.append(hunk) return
def showQuest(page): """Ask for an editor and invoke it.""" quest = pywikibot.input_choice( "Do you want to open the page?", [("with browser", "b"), ("with gui", "g"), ("no", "n")], "n", automatic_quit=False, ) if quest == "b": webbrowser.open("%s?redirect=no" % page.full_url()) elif quest == "g": from pywikibot import editor as editarticle editor = editarticle.TextEditor() editor.edit(page.text)
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: list of unicode """ operation = None argsList = [] namespaces = [] for arg in pywikibot.handle_args(args): if arg in ('-count', '-list'): operation = arg[1:] elif arg.startswith('-namespace:'): try: namespaces.append(int(arg[len('-namespace:'):])) except ValueError: namespaces.append(arg[len('-namespace:'):]) else: argsList.append(arg) if not operation: pywikibot.bot.suggest_help(missing_parameters=['operation']) return False robot = TemplateCountRobot() if not argsList: argsList = templates if 'reflist' in argsList: pywikibot.output( u'NOTE: it will take a long time to count "reflist".') choice = pywikibot.input_choice( u'Proceed anyway?', [('yes', 'y'), ('no', 'n'), ('skip', 's')], 'y', automatic_quit=False) if choice == 's': argsList.remove('reflist') elif choice == 'n': return if operation == "count": robot.countTemplates(argsList, namespaces) elif operation == "list": robot.listTemplates(argsList, namespaces)
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: str """ operation = None args_list = [] namespaces = [] for arg in pywikibot.handle_args(args): if arg in ('-count', '-list'): operation = arg[1:] elif arg.startswith('-namespace:'): try: namespaces.append(int(arg[len('-namespace:'):])) except ValueError: namespaces.append(arg[len('-namespace:'):]) else: args_list.append(arg) if not operation: pywikibot.bot.suggest_help(missing_parameters=['operation']) return False robot = TemplateCountRobot() if not args_list: args_list = templates if 'reflist' in args_list: pywikibot.output('NOTE: it will take a long time to count "reflist".') choice = pywikibot.input_choice('Proceed anyway?', [('yes', 'y'), ('no', 'n'), ('skip', 's')], 'y', automatic_quit=False) if choice == 's': args_list.remove('reflist') elif choice == 'n': return if operation == 'count': robot.countTemplates(args_list, namespaces) elif operation == 'list': robot.listTemplates(args_list, namespaces)
def showQuest(page): quest = pywikibot.input_choice( u'Do you want to open the page?', [('with browser', 'b'), ('with gui', 'g'), ('no', 'n')], 'n', automatic_quit=False) site = page.site url = '%s://%s%s?redirect=no' % (site.protocol(), site.hostname(), site.nice_get_address( page.title(asUrl=True))) if quest == 'b': webbrowser.open(url) elif quest == 'g': from pywikibot import editor as editarticle editor = editarticle.TextEditor() editor.edit(page.text)
def showQuest(page): quest = pywikibot.input_choice(u'Do you want to open the page?', [('with browser', 'b'), ('with gui', 'g'), ('no', 'n')], 'n', automatic_quit=False) site = page.site url = '%s://%s%s?redirect=no' % (site.protocol(), site.hostname(), site.nice_get_address( page.title(asUrl=True))) if quest == 'b': webbrowser.open(url) elif quest == 'g': from pywikibot import editor as editarticle editor = editarticle.TextEditor() editor.edit(page.text)
def main(): site = pywikibot.Site("es", "wikipedia") termino = 'Medallistas paralímpicos de México' categoria = 'Categoría:' + termino fichas = obtener_data('../aosbot/fichas.txt') cat = pywikibot.Category(site, categoria) gen = pg.CategorizedPageGenerator(cat, recurse=True) cont = 0 for page in gen: print("<<<<<<<<<<<<<< " + page.title()) try: item = pywikibot.ItemPage.fromPage(page) dictionary = item.get() lista = item.claims['P31'][0].getTarget() if (str(lista) == '[[wikidata:Q5]]'): page_str = page.get() tmpl_list = pywikibot.textlib.extract_templates_and_params( page_str) for tmpl in tmpl_list: for ficha in fichas: if ficha in tmpl: cont = cont + 1 break if (cont == 0): limit = page.text print(limit[0:500]) choice = pywikibot.input_choice( u'Introduce el número de la ficha que quieres introducir u otra cosa para pasar a otro artículo', [('Ficha de persona', '1'), ('Ficha de deportista', '2'), ('Ficha de actor', '3')]) if choice == '1': escribe = "{{Ficha de persona}}\n" + page.text page.put(escribe, comment="Introduciendo ficha") elif choice == '2': escribe = "{{Ficha de deportista}}\n" + page.text page.put(escribe, comment="Introduciendo ficha") elif choice == '3': escribe = "{{Ficha de actor}}\n" + page.text page.put(escribe, comment="Introduciendo ficha") else: break cont = 0 except: f = open("erroresFichas.txt", mode='a', encoding='utf-8') f.write('Error en: ' + str(page.title()) + '\n') f.close()
def step3(self): output('\n\r\03{lightyellow}Step 3\03{default}: Active admin counts') if self.getOption('skipadmins'): return output(' \03{lightyellow}skipping...\03{default}') output('\03{gray} Note: This process is done on per-wiki basis.\n\r It can be skipped using keyboard interrupt or via -skipadmins argument.') try: self.runForAll('getAdminCount', timedelta(days = self.settings['active_days'])) except KeyboardInterrupt: output('\n\r\03{lightblue}Task was finished partially.\03{default}') choice = pywikibot.input_choice('Do you want to keep partial data on save or ignore it?', [('Keep', 'k'), ('Ignore', 'i')], default='k') if choice == 'i': for wiki in self.toAdd | self.toUpdate: wiki.dumpAdminCount = False elif choice == 'q': raise pywikibot.bot.QuitKeyboardInterrupt
def treat_page(self): """Process one page.""" page = self.current_page page_text = [] for text in page.text.split('\n'): page_text += wrap(text, width=79) or [''] pywikibot.output(color_format('{blue}{}{default}', '_' * 80)) if len(page_text) > self.LINES: pywikibot.output( color_format( '{blue}The page detail is too many lines, ' 'only output first {} lines:{default}', self.LINES)) pywikibot.output('\n'.join( page_text[:min(self.LINES, len(page_text))])) pywikibot.output(color_format('{blue}{}{default}', '_' * 80)) choice = pywikibot.input_choice('Input action?', [('delete', 'd'), ('skip', 's'), ('update', 'u'), ('quit', 'q')], default='s', automatic_quit=False) # quit the bot if choice == 'q': self.quitting = True self.quit() # stop the generator and restart from current title elif choice == 'u': pywikibot.output('Updating from CSD category.') self.saved_progress = page.title() self.stop() # delete the current page elif choice == 'd': reason = self.get_reason_for_deletion(page) pywikibot.output( color_format('The chosen reason is: {lightred}{}{default}', reason)) page.delete(reason, prompt=False) # skip this page else: pywikibot.output('Skipping page {}'.format(page))
def treat(self, page): """ Check the page, and update if necessary. page -- Page to check. """ try: # 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} <<<" % page.title()) text = page.get() old_text = text text = text.replace(u':1 Star : ', u'{{BossSpeedkill|1 Star:=') text = text.replace(u':2 Star : ', u'|2 Stars:=') text = text.replace(u':3 Star : ', u'|3 Stars:=') text = text.replace(u'\n==Stages', u'}}\n==Stages') # Give the user some context pywikibot.showDiff(old_text, text) # TODO Modify to treat just whitespace as unchanged # Just comparing text with page.get() wasn't sufficient changes = False for diffline in difflib.ndiff(page.get().splitlines(), text.splitlines()): if not diffline.startswith(u' '): changes = True break if changes: if not self.acceptall: choice = pywikibot.input_choice(u'Do you want to accept these changes?', [('Yes', 'Y'), ('No', 'n'), ('All', 'a')], 'N') if choice == 'a': self.acceptall = True if self.acceptall or choice == 'y': page.put(text, summary) else: pywikibot.output('No changes were necessary in %s' % page.title()) except pywikibot.NoPage: pywikibot.output("Page %s does not exist?!" % page.title(asLink=True)) except pywikibot.IsRedirectPage: pywikibot.output("Page %s is a redirect; skipping." % page.title(asLink=True)) except pywikibot.LockedPage: pywikibot.output("Page %s is locked?!" % page.title(asLink=True))
def edit_existing(player, site, always, error_count, page_count): # Find the infobox?? page = pwb.Page(site, player.name.replace(' ', '_')) pwbpage = page.get() wtppage = wtp.parse(page.get()) infobox = [element for idx, element in enumerate(wtppage.templates) if 'Player' in element.name] infobox = process_infobox(player, infobox) newtext = wtppage.string text = pwbpage if text == newtext: print(f'skipping {player.name}') return (page_count, error_count, always) else: pwb.output(color_format( '\n\n>>> {lightpurple}{0}{default} <<<', page.title())) pwb.showDiff(text, newtext) while True: # Let's put the changes. if not always: try: choice = pwb.input_choice( 'Do you want to accept these changes?', [('Yes', 'y'), ('No', 'n'), ('All', 'a'), ('open in Browser', 'b')], 'n') except QuitKeyboardInterrupt: sys.exit('User quit bot run.') if choice == 'a': always = True elif choice == 'n': return (page_count, error_count, always) elif choice == 'b': pwb.bot.open_webbrowser(page) if always or choice == 'y': result = page.put(newtext, summary='Update player infobox', asynchronous=True) if result is None: return (page_count + 1, error_count, always) else: print(f'Error occurred! Try {player.name} again.') return (page_count, error_count + 1, always)
def userConfirm(self, question): """Obtain user response.""" if self.always: return True choice = pywikibot.input_choice(question, [('Yes', 'y'), ('No', 'N'), ('Always', 'a')], default='N') if choice == 'n': return False if choice == 'a': self.always = True return True
def replace2(self, match): link = match.group(1) trail = match.group(2) or '' target = self.from_cache(link) if not target: return match.group() left_spaces = len(link) - len(link.lstrip()) right_spaces = len(link) - len(link.rstrip()) if (link.lstrip() + trail).startswith(target): rest = (link.lstrip() + trail)[len(target):] return '[[%s%s]]%s' % (' ' * left_spaces, target, rest) if self.onlypiped is True: # todo: user_interactor return match.group() options_list = [match.group()] if not trail: options_list.append( '[[%s%s%s]]' % (left_spaces * ' ', target, right_spaces * ' ')) options_list.append('[[%s|%s%s]]' % (target, link, trail)) options = [('%d %s' % (i, opt), str(i)) for i, opt in enumerate(options_list, start=1) ] + [('Do not replace unpiped links', 'n')] pre = match.string[max(0, match.start() - 30):match.start()].rpartition('\n')[2] post = match.string[match.end():match.end() + 30].partition('\n')[0] pywikibot.output( color_format('{0}{lightred}{1}{default}{2}', pre, match.group(), post)) choice = pywikibot.input_choice('Replace this link?', options, default='1', automatic_quit=False) if choice == 'n': self.onlypiped = True choice = 1 return options_list[int(choice) - 1]
def user_confirm(self, question): """ Obtain user response if bot option 'always' not enabled. """ if self.getOption('always'): return True choice = pywikibot.input_choice(question, [('Yes', 'y'), ('No', 'N'), ('All', 'a')], default='N') if choice == 'n': return False if choice == 'q': self.quit() if choice == 'a': # Remember the choice self.options['always'] = True return True
def summary_hook(self, match, replaced): def underscores(string): if string.startswith(' '): string = '_' + string[1:] if string.endswith(' '): string = string[:-1] + '_' return string new = old = match.group() if self.needs_decision(): options = [('keep', 'k')] replacements = [] for i, repl in enumerate(self.replacements, start=1): replacement = match.expand(repl) replacements.append(replacement) options.append( ('%s %s' % (i, underscores(replacement)), str(i))) text = match.string pre = text[max(0, match.start() - 30):match.start()].rpartition('\n')[2] post = text[match.end():match.end() + 30].partition('\n')[0] pywikibot.output( color_format('{0}{lightred}{1}{default}{2}', pre, old, post)) choice = pywikibot.input_choice('Choose the best replacement', options, automatic_quit=False, default='k') if choice != 'k': new = replacements[int(choice) - 1] else: new = match.expand(self.replacements[0]) if old == new: pywikibot.warning('No replacement done in string "%s"' % old) if old != new: fragment = ' → '.join( underscores(re.sub('\n', r'\\n', i)) for i in (old, new)) if fragment.lower() not in map(methodcaller('lower'), replaced): replaced.append(fragment) return new
def treat(self, page): """Run the bot's action on each page. Bot.run() loops through everything in the page generator and applies the protections using this function. """ self.current_page = page if not self.getOption('always'): choice = pywikibot.input_choice( u'Do you want to change the protection level of %s?' % page.title(asLink=True, forceInterwiki=True), [('yes', 'y'), ('No', 'n'), ('all', 'a')], 'n') if choice == 'n': return elif choice == 'a': self.options['always'] = True applicable = page.applicable_protections() protections = dict( prot for prot in self.protections.items() if prot[0] in applicable) page.protect(reason=self.getOption('summary'), protections=protections)
def run(self) -> None: pages = [self.parent] + self.children for page in pages: original_text = page.text new_text = page.get_newtext() if original_text == new_text: break while True: pywikibot.output(color_format( '\n\n>>> {lightpurple}{0}{default} <<<', page.title())) pywikibot.showDiff(original_text, new_text) choice = pywikibot.input_choice('この変更を投稿しますか', [('はい', 'y'), ('いいえ', 'n'), ('エディタで編集する', 'e')]) if choice == 'n': break if choice == 'e': editor = editarticle.TextEditor() as_edited = editor.edit(new_text) if as_edited: new_text = as_edited continue if choice == 'y': page.text = new_text page.save( 'Botによる: [[User:YuukinBot#作業内容2|カテゴリの整備]]', asynchronous=True, callback=self._async_callback, quiet=True) self._pending_processing_titles.put(page.title(as_link=True)) while not self._pending_processed_titles.empty(): proc_title, res = self._pending_processed_titles.get() pywikibot.output('{0}{1}'.format(proc_title, 'が投稿されました' if res else 'は投稿されませんでした')) break while not all((self._pending_processing_titles.empty(), self._pending_processed_titles.empty())): proc_title, res = self._pending_processed_titles.get() pywikibot.output('{0}{1}'.format(proc_title, 'が投稿されました' if res else 'は投稿されませんでした')) pywikibot.output(f'{self.changed_pages} ページ編集しました') pywikibot.output(f'{self.parent.title(as_link=True)} 関連の整備が完了しました')
def _update_page(self, page, old_text, new_text): """ Update the specified page. page -- page to update. old_text -- original page text. new_text -- new text for the page. """ try: # 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} <<<" % page.title()) pywikibot.showDiff(old_text, new_text) # TODO Modify to treat just whitespace as unchanged # Just comparing text with page.get() wasn't sufficient changes = False for diffline in difflib.ndiff(old_text.splitlines(), new_text.splitlines()): if not diffline.startswith(u' '): changes = True break if changes: if not self.acceptall: choice = pywikibot.input_choice(u'Do you want to accept these changes?', [('Yes', 'Y'), ('No', 'n'), ('All', 'a')], 'N') if choice == 'a': self.acceptall = True if self.acceptall or choice == 'y': page.put(new_text, summary) else: pywikibot.output('No changes were necessary in %s' % page.title()) except pywikibot.NoPage: pywikibot.output("Page %s does not exist?!" % page.title(asLink=True)) except pywikibot.IsRedirectPage: pywikibot.output("Page %s is a redirect; skipping." % page.title(asLink=True)) except pywikibot.LockedPage: pywikibot.output("Page %s is locked?!" % page.title(asLink=True))
def summary_hook(self, match, replaced): def underscores(string): if string.startswith(' '): string = '_' + string[1:] if string.endswith(' '): string = string[:-1] + '_' return string new = old = match.group() if self.needsDecision(): options = [('keep', 'k')] replacements = [] for i, repl in enumerate(self.replacements, start=1): replacement = match.expand(repl) replacements.append(replacement) options.append( ('%s %s' % (i, underscores(replacement)), str(i)) ) text = match.string pre = text[max(0, match.start() - 30):match.start()].rpartition('\n')[2] post = text[match.end():match.end() + 30].partition('\n')[0] pywikibot.output(color_format('{0}{lightred}{1}{default}{2}', pre, old, post)) choice = pywikibot.input_choice('Choose the best replacement', options, automatic_quit=False, default='k') if choice != 'k': new = replacements[int(choice) - 1] else: new = match.expand(self.replacements[0]) if old == new: pywikibot.warning('No replacement done in string "%s"' % old) if old != new: fragment = ' → '.join(underscores(re.sub('\n', r'\\n', i)) for i in (old, new)) if fragment.lower() not in map(methodcaller('lower'), replaced): replaced.append(fragment) return new
def treat_page(self): """Process a single page.""" text = self.current_page.text if self.spam_external_url not in text: return lines = text.split('\n') newpage = [] lastok = '' for line in lines: if self.spam_external_url in line: if lastok: pywikibot.output(lastok) pywikibot.output(color_format('{lightred}{0}{default}', line)) lastok = None else: newpage.append(line) if line.strip(): if lastok is None: pywikibot.output(line) lastok = line if self.getOption('always'): answer = 'y' else: answer = pywikibot.input_choice('\nDelete the red lines?', [('yes', 'y'), ('no', 'n'), ('edit', 'e')], 'n', automatic_quit=False) if answer == 'n': return elif answer == 'e': editor = TextEditor() newtext = editor.edit(text, highlight=self.spam_external_url, jumpIndex=text.find(self.spam_external_url)) else: newtext = '\n'.join(newpage) if newtext != text: self.put_current(newtext, summary=self.getOption('summary'))
def user_confirm(self, question): if self.opt['always']: return True options = [('yes', 'y'), ('no', 'n'), ('all', 'a')] if self.fp_page.exists(): options.append(('false positive', 'f')) if self.own_generator: options.append(('skip rule', 's')) options += [('open in browser', 'b'), ('quit', 'q')] choice = pywikibot.input_choice(question, options, default='N', automatic_quit=False) if choice == 'n': return False if choice == 's': self.skip_rule = True return False if choice == 'b': pywikibot.bot.open_webbrowser(self.current_page) return False if choice == 'f': self.save_false_positive(self.current_page) return False if choice == 'q': self.quit() if choice == 'a': self.options['always'] = True return True
def run(self): """Start the bot's action. Loop through everything in the page generator and apply the protections. """ for page in self.generator: self.current_page = page if not self.getOption('always'): choice = pywikibot.input_choice( u'Do you want to change the protection level of %s?' % page.title(asLink=True, forceInterwiki=True), [('yes', 'y'), ('No', 'n'), ('all', 'a')], 'n', automatic_quit=False) if choice == 'n': continue elif choice == 'a': self.options['always'] = True applicable = page.applicable_protections() protections = dict( [prot for prot in self.protections if prot[0] in applicable]) page.protect(reason=self.getOption('summary'), protections=protections)
def treat_page(self): """Process a single page.""" text = self.current_page.text if self.spam_external_url not in text: return lines = text.split('\n') newpage = [] lastok = "" for line in lines: if self.spam_external_url in line: if lastok: pywikibot.output(lastok) pywikibot.output(color_format('{lightred}{0}{default}', line)) lastok = None else: newpage.append(line) if line.strip(): if lastok is None: pywikibot.output(line) lastok = line if self.getOption('always'): answer = "y" else: answer = pywikibot.input_choice( u'\nDelete the red lines?', [('yes', 'y'), ('no', 'n'), ('edit', 'e')], 'n', automatic_quit=False) if answer == "n": return elif answer == "e": editor = TextEditor() newtext = editor.edit(text, highlight=self.spam_external_url, jumpIndex=text.find(self.spam_external_url)) else: newtext = "\n".join(newpage) if newtext != text: self.put_current(newtext, summary=self.getOption('summary'))
def user_confirm(self, question): if self.getOption('always'): return True options = [('yes', 'y'), ('no', 'n'), ('all', 'a'), ('open in browser', 'b'), ('quit', 'q')] if self.own_generator: options.insert(3, ('skip rule', 's')) if self.fp_page.exists(): options.insert(3, ('false positive', 'f')) choice = pywikibot.input_choice(question, options, default='N', automatic_quit=False) if choice == 'n': return False if choice == 's': self.skip_rule = True return False if choice == 'b': pywikibot.bot.open_webbrowser(self.current_page) return False if choice == 'f': self.save_false_positive(self.current_page) return False if choice == 'q': self.quit() if choice == 'a': self.options['always'] = True return True
def treat(self, page): try: # 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} <<<" % page.title()) # TODO parameter to search for should be passed to the script text = page.get() old_text = text for param in params: text = self.add_img_param(text, param) # Give the user some context if old_text != text: pywikibot.output(text) pywikibot.showDiff(old_text, text) # TODO Modify to treat just whitespace as unchanged # Just comparing text with page.get() wasn't sufficient changes = False for diffline in difflib.ndiff(page.get().splitlines(), text.splitlines()): if not diffline.startswith(u' '): changes = True break if changes: if not self.acceptall: choice = pywikibot.input_choice(u'Do you want to accept these changes?', [('Yes', 'Y'), ('No', 'n'), ('All', 'a')], 'N') if choice == 'a': self.acceptall = True if self.acceptall or choice == 'y': page.put(text, summary) else: pywikibot.output('No changes were necessary in %s' % page.title()) except pywikibot.NoPage: pywikibot.output("Page %s does not exist?!" % page.title(asLink=True)) except pywikibot.IsRedirectPage: pywikibot.output("Page %s is a redirect; skipping." % page.title(asLink=True)) except pywikibot.LockedPage: pywikibot.output("Page %s is locked?!" % page.title(asLink=True))
def reportBadAccount(self, name=None, final=False): """Report bad account.""" # Queue process if name: if globalvar.confirm: answer = pywikibot.input_choice( u'%s may have an unwanted username, do you want to report ' u'this user?' % name, [('Yes', 'y'), ('No', 'n'), ('All', 'a')], 'n', automatic_quit=False) if answer in ['a', 'all']: answer = 'y' globalvar.confirm = False else: answer = 'y' if answer.lower() in ['yes', 'y'] or not globalvar.confirm: showStatus() pywikibot.output( u'%s is possibly an unwanted username. It will be reported.' % name) if hasattr(self, '_BAQueue'): self._BAQueue.append(name) else: self._BAQueue = [name] if len(self._BAQueue) >= globalvar.dumpToLog or final: rep_text = '' # name in queue is max, put detail to report page pywikibot.output("Updating badname accounts to report page...") rep_page = pywikibot.Page(self.site, i18n.translate(self.site, report_page)) if rep_page.exists(): text_get = rep_page.get() else: text_get = u'This is a report page for the Bad-username, please translate me. --~~~' pos = 0 # The talk page includes "_" between the two names, in this way i # replace them to " ". for usrna in self._BAQueue: username = pywikibot.url2link(usrna, self.site, self.site) n = re.compile(re.escape(username), re.UNICODE) y = n.search(text_get, pos) if y: pywikibot.output(u'%s is already in the report page.' % username) else: # Adding the log. rep_text += i18n.translate(self.site, report_text) % username if self.site.code == 'it': rep_text = "%s%s}}" % (rep_text, self.bname[username]) com = i18n.twtranslate(self.site, 'welcome-bad_username') if rep_text != '': rep_page.put(text_get + rep_text, summary=com, force=True, minorEdit=True) showStatus(5) pywikibot.output(u'Reported') self.BAQueue = list() else: return True
def treat_page(self): """Treat a single page.""" page = self.current_page pagetitle = page.title(with_ns=False) namesp = page.site.namespace(page.namespace()) if self.appendAll: newPageTitle = '{}{}{}'.format(self.pagestart, pagetitle, self.pageend) if not self.noNamespace and namesp: newPageTitle = '{}:{}'.format(namesp, newPageTitle) elif self.regexAll: newPageTitle = self.regex.sub(self.replacePattern, pagetitle) if not self.noNamespace and namesp: newPageTitle = '{}:{}'.format(namesp, newPageTitle) if self.opt.prefix: newPageTitle = '{}{}'.format(self.opt.prefix, pagetitle) if self.opt.prefix or self.appendAll or self.regexAll: if self.user_confirm( 'Change the page title to {!r}?'.format(newPageTitle)): self.moveOne(page, newPageTitle) return # else: choice = pywikibot.input_choice('What do you want to do?', [('change page name', 'c'), ('append to page name', 'a'), ('use a regular expression', 'r'), ('next page', 'n')]) if choice == 'c': newPageTitle = pywikibot.input('New page name:') self.moveOne(page, newPageTitle) elif choice == 'a': self.pagestart = pywikibot.input('Append this to the start:') self.pageend = pywikibot.input('Append this to the end:') newPageTitle = ('{}{}{}'.format(self.pagestart, pagetitle, self.pageend)) if namesp: if pywikibot.input_yn('Do you want to remove the ' 'namespace prefix "{}:"?'.format(namesp), automatic_quit=False): self.noNamespace = True else: newPageTitle = ('{}:{}'.format(namesp, newPageTitle)) choice2 = pywikibot.input_choice( 'Change the page title to {!r}?'.format(newPageTitle), [('yes', 'y'), ('no', 'n'), ('all', 'a')]) if choice2 == 'y': self.moveOne(page, newPageTitle) elif choice2 == 'a': self.appendAll = True self.moveOne(page, newPageTitle) elif choice == 'r': searchPattern = pywikibot.input('Enter the search pattern:') self.replacePattern = pywikibot.input('Enter the replace pattern:') self.regex = re.compile(searchPattern) if page.title() == page.title(with_ns=False): newPageTitle = self.regex.sub(self.replacePattern, page.title()) else: if pywikibot.input_yn('Do you want to remove the ' 'namespace prefix "{}:"?'.format(namesp), automatic_quit=False): newPageTitle = self.regex.sub(self.replacePattern, page.title(with_ns=False)) self.noNamespace = True else: newPageTitle = self.regex.sub(self.replacePattern, page.title()) choice2 = pywikibot.input_choice( 'Change the page title to {!r}?'.format(newPageTitle), [('yes', 'y'), ('no', 'n'), ('all', 'a')]) if choice2 == 'y': self.moveOne(page, newPageTitle) elif choice2 == 'a': self.regexAll = True self.moveOne(page, newPageTitle)
def main(*args): """ Process command line arguments and perform task. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: list of unicode """ always = False namespaces = [] spamSite = '' for arg in pywikibot.handle_args(args): if arg == "-always": always = True elif arg.startswith('-namespace:'): try: namespaces.append(int(arg[len('-namespace:'):])) except ValueError: namespaces.append(arg[len('-namespace:'):]) else: spamSite = arg if not spamSite: pywikibot.bot.suggest_help(missing_parameters=['spam site']) return False mysite = pywikibot.Site() pages = mysite.exturlusage(spamSite, namespaces=namespaces, content=True) summary = i18n.twtranslate(mysite, 'spamremove-remove', {'url': spamSite}) for i, p in enumerate(pages, 1): text = p.text if spamSite not in text: continue # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(color_format( '\n\n>>> {lightpurple}{0}{default} <<<', p.title())) lines = text.split('\n') newpage = [] lastok = "" for line in lines: if spamSite in line: if lastok: pywikibot.output(lastok) pywikibot.output(color_format('{lightred}{0}{default}', line)) lastok = None else: newpage.append(line) if line.strip(): if lastok is None: pywikibot.output(line) lastok = line if always: answer = "y" else: answer = pywikibot.input_choice( u'\nDelete the red lines?', [('yes', 'y'), ('no', 'n'), ('edit', 'e')], 'n', automatic_quit=False) if answer == "n": continue elif answer == "e": editor = TextEditor() newtext = editor.edit(text, highlight=spamSite, jumpIndex=text.find(spamSite)) else: newtext = "\n".join(newpage) if newtext != text: p.text = newtext p.save(summary) else: if "i" not in locals(): pywikibot.output('No page found.') elif i == 1: pywikibot.output('1 pages done.') else: pywikibot.output('%d pages done.' % i)
def add_text(page, addText, summary=None, regexSkip=None, regexSkipUrl=None, always=False, up=False, putText=True, oldTextGiven=None, reorderEnabled=True, create=False): """ Add text to a page. @rtype: tuple of (text, newtext, always) """ site = page.site if not summary: summary = i18n.twtranslate(site, 'add_text-adding', {'adding': addText[:200]}) # When a page is tagged as "really well written" it has a star in the # interwiki links. This is a list of all the templates used (in regex # format) to make the stars appear. errorCount = 0 if putText: pywikibot.output(u'Loading %s...' % page.title()) if oldTextGiven is None: try: text = page.get() except pywikibot.NoPage: if create: pywikibot.output(u"%s doesn't exist, creating it!" % page.title()) text = u'' else: pywikibot.output(u"%s doesn't exist, skip!" % page.title()) return (False, False, always) except pywikibot.IsRedirectPage: pywikibot.output(u"%s is a redirect, skip!" % page.title()) return (False, False, always) else: text = oldTextGiven # Understand if the bot has to skip the page or not # In this way you can use both -except and -excepturl if regexSkipUrl is not None: url = page.full_url() result = re.findall(regexSkipUrl, site.getUrl(url)) if result != []: pywikibot.output('Exception! regex (or word) used with -exceptUrl ' 'is in the page. Skip!\n' 'Match was: %s' % result) return (False, False, always) if regexSkip is not None: result = re.findall(regexSkip, text) if result != []: pywikibot.output('Exception! regex (or word) used with -except ' 'is in the page. Skip!\n' 'Match was: %s' % result) return (False, False, always) # If not up, text put below if not up: newtext = text # Translating the \\n into binary \n addText = addText.replace('\\n', config.line_separator) if (reorderEnabled): # Getting the categories categoriesInside = textlib.getCategoryLinks(newtext, site) # Deleting the categories newtext = textlib.removeCategoryLinks(newtext, site) # Getting the interwiki interwikiInside = textlib.getLanguageLinks(newtext, site) # Removing the interwiki newtext = textlib.removeLanguageLinks(newtext, site) # Adding the text newtext += u"%s%s" % (config.line_separator, addText) # Reputting the categories newtext = textlib.replaceCategoryLinks(newtext, categoriesInside, site, True) # Dealing the stars' issue allstars = [] starstext = textlib.removeDisabledParts(text) for star in starsList: regex = re.compile( '(\{\{(?:template:|)%s\|.*?\}\}[\s]*)' % star, re.I) found = regex.findall(starstext) if found != []: newtext = regex.sub('', newtext) allstars += found if allstars != []: newtext = newtext.strip() + config.line_separator * 2 allstars.sort() for element in allstars: newtext += '%s%s' % (element.strip(), config.LS) # Adding the interwiki newtext = textlib.replaceLanguageLinks(newtext, interwikiInside, site) else: newtext += u"%s%s" % (config.line_separator, addText) else: newtext = addText + config.line_separator + text if putText and text != newtext: pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) pywikibot.showDiff(text, newtext) # Let's put the changes. while True: # If someone load it as module, maybe it's not so useful to put the # text in the page if putText: if not always: choice = pywikibot.input_choice( u'Do you want to accept these changes?', [('Yes', 'y'), ('No', 'n'), ('All', 'a'), ('open in Browser', 'b')], 'n', automatic_quit=False) if choice == 'a': always = True elif choice == 'n': return (False, False, always) elif choice == 'b': pywikibot.bot.open_webbrowser(page) if always or choice == 'y': try: if always: page.put(newtext, summary, minorEdit=page.namespace() != 3) else: page.put_async(newtext, summary, minorEdit=page.namespace() != 3) except pywikibot.EditConflict: pywikibot.output(u'Edit conflict! skip!') return (False, False, always) except pywikibot.ServerError: errorCount += 1 if errorCount < config.max_retries: pywikibot.output(u'Server Error! Wait..') time.sleep(config.retry_wait) continue else: raise pywikibot.ServerError(u'Fifth Server Error!') except pywikibot.SpamfilterError as e: pywikibot.output( u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url)) return (False, False, always) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % page.title()) return (False, False, always) except pywikibot.PageNotSaved as error: pywikibot.output(u'Error putting page: %s' % error.args) return (False, False, always) else: # Break only if the errors are one after the other... errorCount = 0 return (True, True, always) else: return (text, newtext, always)
def main(*args): """ Process command line arguments and perform task. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: str """ # Loading the comments global categoryToCheck, project_inserted # always, define a generator to understand if the user sets one, # defining what's genFactory always = False generator = False show = False moveBlockCheck = False protectedpages = False protectType = 'edit' namespace = 0 # To prevent Infinite loops errorCount = 0 # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) genFactory = pagegenerators.GeneratorFactory() # Process local args for arg in local_args: option, sep, value = arg.partition(':') if option == '-always': always = True elif option == '-move': moveBlockCheck = True elif option == '-show': show = True elif option in ('-protectedpages', '-moveprotected'): protectedpages = True if option == '-moveprotected': protectType = 'move' 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 set for CAT in categories: cat = pywikibot.Category(site, CAT) # Define the generator gen = pagegenerators.CategorizedPageGenerator(cat) for pageCat in gen: generator.append(pageCat) pywikibot.output('Categories loaded, start!') # Main Loop if not genFactory.nopreload: generator = pagegenerators.PreloadingGenerator(generator, groupsize=60) for page in generator: pagename = page.title(as_link=True) pywikibot.output('Loading %s...' % pagename) try: text = page.text except pywikibot.NoPage: pywikibot.output("%s doesn't exist! Skipping..." % pagename) continue except pywikibot.IsRedirectPage: pywikibot.output('{} is a redirect! Skipping...'.format(pagename)) if show: showQuest(page) continue # FIXME: This check does not work : # PreloadingGenerator cannot set correctly page.editRestriction # (see bug T57322) # if not page.canBeEdited(): # pywikibot.output( # "%s is sysop-protected : this account can't edit " # "it! Skipping..." % pagename) # continue restrictions = page.protection() try: editRestr = restrictions['edit'] except KeyError: editRestr = None if not page.canBeEdited(): pywikibot.output('%s is protected: ' "this account can't edit it! Skipping..." % pagename) continue # Understand, according to the template in the page, what should be the # protection and compare it with what there really is. TemplateInThePage = understandBlock(text, TTP, TSP, TSMP, TTMP, TU) # Only to see if the text is the same or not... oldtext = text # keep track of the changes for each step (edit then move) changes = -1 if not editRestr: # page is not edit-protected # Deleting the template because the page doesn't need it. if not (TTP or TSP): raise pywikibot.Error( 'This script is not localized to use it on \n{0}. ' 'Missing "templateSemiProtection" or' '"templateTotalProtection"'.format(site.sitename)) if TU: replaceToPerform = '|'.join(TTP + TSP + TU) else: replaceToPerform = '|'.join(TTP + TSP) text, changes = re.subn( '<noinclude>(%s)</noinclude>' % replaceToPerform, '', text) if changes == 0: text, changes = re.subn('(%s)' % replaceToPerform, '', text) msg = 'The page is editable for all' if not moveBlockCheck: msg += ', deleting the template..' pywikibot.output(msg + '.') elif editRestr[0] == 'sysop': # total edit protection if (TemplateInThePage[0] == 'sysop-total' and TTP) or \ (TemplateInThePage[0] == 'unique' and TU): msg = 'The page is protected to the sysop' if not moveBlockCheck: msg += ', skipping...' pywikibot.output(msg) else: if not TNR or TU and not TNR[4] or not (TU or TNR[1]): raise pywikibot.Error( 'This script is not localized to use it on \n{0}. ' 'Missing "templateNoRegex"'.format(site.sitename)) pywikibot.output('The page is protected to the sysop, but the ' 'template seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[1], text) elif TSP or TU: # implicitly editRestr[0] = 'autoconfirmed', edit-Semi-protection if TemplateInThePage[0] == 'autoconfirmed-total' or \ TemplateInThePage[0] == 'unique': msg = 'The page is editable only for the autoconfirmed users' if not moveBlockCheck: msg += ', skipping...' pywikibot.output(msg) else: if not TNR or TU and not TNR[4] or not (TU or TNR[1]): raise pywikibot.Error( 'This script is not localized to use it on \n{0}. ' 'Missing "templateNoRegex"'.format(site.sitename)) pywikibot.output('The page is editable only for the ' 'autoconfirmed users, but the template ' 'seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[0], text) if changes == 0: # We tried to fix edit-protection templates, but it did not work. pywikibot.warning('No edit-protection template could be found') if moveBlockCheck and changes > -1: # checking move protection now try: moveRestr = restrictions['move'] except KeyError: moveRestr = False changes = -1 if not moveRestr: pywikibot.output('The page is movable for all, deleting the ' 'template...') # Deleting the template because the page doesn't need it. if TU: replaceToPerform = '|'.join(TSMP + TTMP + TU) else: replaceToPerform = '|'.join(TSMP + TTMP) text, changes = re.subn( '<noinclude>(%s)</noinclude>' % replaceToPerform, '', text) if changes == 0: text, changes = re.subn('({})'.format(replaceToPerform), '', text) elif moveRestr[0] == 'sysop': # move-total-protection if (TemplateInThePage[0] == 'sysop-move' and TTMP) or \ (TemplateInThePage[0] == 'unique' and TU): pywikibot.output('The page is protected from moving to ' 'the sysop, skipping...') if TU: # no changes needed, better to revert the old text. text = oldtext else: pywikibot.output('The page is protected from moving to ' 'the sysop, but the template seems not ' 'correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[3], text) elif TSMP or TU: # implicitly moveRestr[0] = 'autoconfirmed', # move-semi-protection if TemplateInThePage[0] == 'autoconfirmed-move' or \ TemplateInThePage[0] == 'unique': pywikibot.output('The page is movable only for the ' 'autoconfirmed users, skipping...') if TU: # no changes needed, better to revert the old text. text = oldtext else: pywikibot.output('The page is movable only for the ' 'autoconfirmed users, but the template ' 'seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[2], text) if changes == 0: # We tried to fix move-protection templates but it did not work pywikibot.warning('No move-protection template could be found') if oldtext != text: # Ok, asking if the change has to be performed and do it if yes. pywikibot.output( color_format('\n\n>>> {lightpurple}{0}{default} <<<', page.title())) pywikibot.showDiff(oldtext, text) if not always: choice = pywikibot.input_choice( 'Do you want to accept these ' 'changes?', [('Yes', 'y'), ('No', 'n'), ('All', 'a')], 'n') if choice == 'a': always = True if always or choice == 'y': while True: try: page.put(text, commentUsed, force=True) except pywikibot.EditConflict: pywikibot.output('Edit conflict! skip!') break except pywikibot.ServerError: # Sometimes there is this error that's quite annoying # because can block the whole process for nothing. errorCount += 1 if errorCount < 5: pywikibot.output('Server Error! Wait..') time.sleep(3) continue else: # Prevent Infinite Loops raise pywikibot.ServerError('Fifth Server Error!') except pywikibot.SpamfilterError as e: pywikibot.output('Cannot change %s because of ' 'blacklist entry %s' % (page.title(), e.url)) break except pywikibot.LockedPage: pywikibot.output('The page is still protected. ' 'Skipping...') break except pywikibot.PageNotSaved as error: pywikibot.output('Error putting page: %s' % (error.args, )) break else: # Break only if the errors are one after the other errorCount = 0 break