def put_page(self, page, new): """ Prints diffs between orginal and new (text), puts new text for page """ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) pywikibot.showDiff(page.get(), new) if not self.acceptall: choice = pywikibot.inputChoice( u'Do you want to accept ' + u'these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice == 'a': self.acceptall = True if choice == 'y': page.put_async(new, self.msg) if self.acceptall: try: page.put(new, self.msg) 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.error(u'putting page: %s' % error.args)
def featuredArticles(site, pType): articles = [] if pType == 'good': info = good_name elif pType == 'former': info = former_name elif pType == 'list': info = lists_name else: info = featured_name try: method = info[site.lang][0] except KeyError: pywikibot.error(u'language %s doesn\'t has %s category source.' % (site.lang, pType)) return name = info[site.lang][1] # hide #-sorted items on en-wiki try: hide = info[site.lang][2] except IndexError: hide = None raw = method(site, name, hide) for p in raw: if p.namespace() == 0: # Article articles.append(p) # Article talk (like in English) elif p.namespace() == 1 and site.lang != 'el': articles.append( pywikibot.Page(p.site, p.title(withNamespace=False))) pywikibot.output( '\03{lightred}** wikipedia:%s has %i %s articles\03{default}' % (site.lang, len(articles), pType)) for p in articles: yield copy(p)
def put_page(self, page, new): """ Prints diffs between orginal and new (text), puts new text for page """ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) pywikibot.showDiff(page.get(), new) if not self.acceptall: choice = pywikibot.inputChoice(u'Do you want to accept ' + u'these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice == 'a': self.acceptall = True if choice == 'y': page.put_async(new) if self.acceptall: try: page.put(new) 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.error(u'putting page: %s' % (error.args,))
def treat(self, page): ''' Loads a page, converts all HTML tables in its text to wiki syntax, and saves the converted text. Returns True if the converted table was successfully saved, otherwise returns False. ''' pywikibot.output(u'\n>>> %s <<<' % page.title()) site = page.site() try: text = page.get() except pywikibot.NoPage: pywikibot.error(u"couldn't find %s" % page.title()) return False except pywikibot.IsRedirectPage: pywikibot.output(u'Skipping redirect %s' % page.title()) return False newText, convertedTables, warningSum = self.convertAllHTMLTables(text) # Check if there are any marked tags left markedTableTagR = re.compile("<##table##|</##table##>", re.IGNORECASE) if markedTableTagR.search(newText): pywikibot.error( u'not all marked table start or end tags processed!') return if convertedTables == 0: pywikibot.output(u"No changes were necessary.") else: if config.table2wikiAskOnlyWarnings and warningSum == 0: doUpload = True else: if config.table2wikiSkipWarnings: doUpload = True else: print( "There were %i replacement(s) that might lead to bad " "output.") % warningSum doUpload = (pywikibot.input( u'Do you want to change the page anyway? [y|N]') == "y" ) if doUpload: # get edit summary message if warningSum == 0: pywikibot.setAction( i18n.twtranslate(site.lang, 'table2wiki-no-warning')) else: pywikibot.setAction( i18n.twntranslate(site.lang, 'table2wiki-warnings', {'count': warningSum})) page.put_async(newText)
def treat(self, page): ''' Loads a page, converts all HTML tables in its text to wiki syntax, and saves the converted text. Returns True if the converted table was successfully saved, otherwise returns False. ''' pywikibot.output(u'\n>>> %s <<<' % page.title()) site = page.site() try: text = page.get() except pywikibot.NoPage: pywikibot.error(u"couldn't find %s" % page.title()) return False except pywikibot.IsRedirectPage: pywikibot.output(u'Skipping redirect %s' % page.title()) return False newText, convertedTables, warningSum = self.convertAllHTMLTables(text) # Check if there are any marked tags left markedTableTagR = re.compile("<##table##|</##table##>", re.IGNORECASE) if markedTableTagR.search(newText): pywikibot.error( u'not all marked table start or end tags processed!') return if convertedTables == 0: pywikibot.output(u"No changes were necessary.") else: if config.table2wikiAskOnlyWarnings and warningSum == 0: doUpload = True else: if config.table2wikiSkipWarnings: doUpload = True else: print("There were %i replacement(s) that might lead to bad " "output.") % warningSum doUpload = (pywikibot.input( u'Do you want to change the page anyway? [y|N]') == "y") if doUpload: # get edit summary message if warningSum == 0: pywikibot.setAction( i18n.twtranslate(site.lang, 'table2wiki-no-warning')) else: pywikibot.setAction( i18n.twntranslate(site.lang, 'table2wiki-warnings', {'count': warningSum})) page.put_async(newText)
def run(self): # TODO: change the generator for template to the included category try: genPage = pywikibot.Page(self.site, self.unblock_tpl[self.site.lang], defaultNamespace=10) except KeyError: pywikibot.error(u'Language "%s" not supported by this bot.' % self.site.lang) else: for page in genPage.getReferences(follow_redirects=False, withTemplateInclusion=True, onlyTemplateInclusion=True): if page.namespace() == 3: self.treat(page) else: pywikibot.output(u'Ignoring %s, user namespace required' % page.title(asLink=True))
def defineSign(self, force=False): if hasattr(self, '_randomSignature') and not force: return self._randomSignature signText = u'' creg = re.compile(r"^\* ?(.*?)$", re.M) if not globalvar.signFileName: signPageName = pywikibot.translate(self.site, random_sign) if not signPageName: showStatus(4) pywikibot.output( "%s doesn't allow random signature, force disable." % self.site) globalvar.randomSign = False return None signPage = pywikibot.Page(self.site, signPageName) if signPage.exists(): pywikibot.output('Loading signature list...') signText = signPage.get() else: pywikibot.output( 'The Signature list page is not exist, random signature will disable.' ) globalvar.randomSign = False else: try: f = codecs.open(pywikibot.config.datafilepath( globalvar.signFileName), 'r', encoding=config.console_encoding) except LookupError: f = codecs.open(pywikibot.config.datafilepath( globalvar.signFileName), 'r', encoding='utf-8') except IOError: pywikibot.error(u'No fileName!') raise FilenameNotSet("No signature filename specified.") signText = f.read() f.close() self._randomSignature = creg.findall(signText) return self._randomSignature
def send_mailnotification(text, subject): username = pywikibot.config.usernames[pywikibot.config.family][ pywikibot.config.mylang] pos = username.lower().find('bot') username = username[:pos] if (pos > 0) else username pywikibot.output(u'Sending mail "%s" to "%s" as notification!' % (subject, username)) # JIRA: DRTRIGON-87; output even more debug info (tip by: [email protected]) site = pywikibot.getSite() pywikibot.output(u'Bot allowed to send email: %r' % (site.isAllowed('sendemail'), )) pywikibot.output(u'Permissions: %r' % (site._rights, )) if not site.isAllowed('sendemail'): pywikibot.output(u'Try getting new token: %r' % (site.getToken(getagain=True), )) usr = userlib.User(site, username) try: if usr.sendMail(subject=subject, text=text): # 'text' should be unicode! return except: # break exception handling recursion pywikibot.exception(tb=True) pywikibot.error(u'mail to %s could not be sent!' % username) pywikibot.output(u'May be not logged in - try to send emergency email') try: import smtplib from email.mime.text import MIMEText # sender's and recipient's email addresses FROM = "*****@*****.**" % username.lower() TO = [FROM] # must be a list # Create a text/plain message msg = MIMEText(text) msg['Subject'] = "!EMERGENCY! " + subject msg['From'] = FROM msg['To'] = ", ".join(TO) # Send the mail server = smtplib.SMTP("localhost") server.sendmail(FROM, TO, msg.as_string()) server.quit() except: # break exception handling recursion pywikibot.exception(tb=True) pywikibot.error(u'emergency mail to %s could not be sent!' % TO)
def load_whitelist(self): # Check for a more recent version after 5 minutes if self.whitelist_load_ts and ( (time.time() - self.whitelist_load_ts) < 300): if pywikibot.verbose: pywikibot.output(u'Whitelist not stale yet') return whitelist_page = pywikibot.Page(pywikibot.getSite(), self.whitelist_pagename) if not self.whitelist: pywikibot.output(u'Loading %s' % self.whitelist_pagename) try: if self.whitelist_ts: # check for a more recent version h = whitelist_page.getVersionHistory(forceReload=True, revCount=1) last_edit_ts = pywikibot.parsetime2stamp(h[0][1]) if last_edit_ts == self.whitelist_ts: # As there hasn't been any changed to the whitelist # it has been effectively reloaded 'now' self.whitelist_load_ts = time.time() if pywikibot.verbose: pywikibot.output(u'Whitelist not modified') return if self.whitelist: pywikibot.output(u'Reloading whitelist') # Fetch whitelist wikitext = whitelist_page.get() # Parse whitelist self.whitelist = self.parse_page_tuples(wikitext, self.user) # Record timestamp self.whitelist_ts = whitelist_page.editTime() self.whitelist_load_ts = time.time() except Exception as e: # cascade if there isnt a whitelist to fallback on if not self.whitelist: raise pywikibot.error(u'%s' % e)
def check_setup(m): path = os.path.dirname(os.path.abspath(os.path.join(os.curdir, __file__))) mf = os.path.join(path, m) # search missing module if python_module_exists(m): return if os.path.exists(mf): return # install the missing module dist = guess_system().split(u'-') if globals()[dist[0] + '_install'](modules_needed[m][0]): return if download_install(modules_needed[m][1], m, path): return if mercurial_repo_install(modules_needed[m][2], m, path): return pywikibot.error(u'Package "%s" could not be found nor installed!' % m)
def workon(page): mysite = pywikibot.getSite() try: text = page.get() except pywikibot.IsRedirectPage: pywikibot.output(u'%s is a redirect page. Skipping' % page) return except pywikibot.NoPage: pywikibot.output(u'%s does not exist. Skipping' % page) return pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) links = page.linkedPages() if len(links): pywikibot.getall(mysite, links) else: pywikibot.output('Nothing left to do.') return for page2 in links: try: target = page2.getRedirectTarget() except pywikibot.NoPage: try: target = page2.getMovedTarget() except pywikibot.NoPage: continue target = pywikibot.Page(mysite, lastmove['new_title']) except (pywikibot.Error, pywikibot.SectionError): continue # no fix to user namespaces if target.namespace() in [0, 1] and not page2.namespace() in [0, 1]: continue text = treat(text, page2, target) if text != page.get(): comment = i18n.twtranslate(mysite, 'fixing_redirects-fixing') pywikibot.showDiff(page.get(), text) try: page.put(text, comment) except (pywikibot.Error): pywikibot.error('unable to put %s' % page)
def workon(page): mysite = pywikibot.getSite() try: text = page.get() except pywikibot.IsRedirectPage: pywikibot.output(u'%s is a redirect page. Skipping' % page) return except pywikibot.NoPage: pywikibot.output(u'%s does not exist. Skipping' % page) return pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) links = page.linkedPages() if len(links): pywikibot.getall(mysite,links) else: pywikibot.output('Nothing left to do.') return for page2 in links: try: target = page2.getRedirectTarget() except pywikibot.NoPage: try: target = page2.getMovedTarget() except pywikibot.NoPage: continue target = pywikibot.Page(mysite, lastmove['new_title']) except (pywikibot.Error, pywikibot.SectionError): continue # no fix to user namespaces if target.namespace() in [0, 1] and not page2.namespace() in [0, 1]: continue text = treat(text, page2, target) if text != page.get(): comment = i18n.twtranslate(mysite, 'fixing_redirects-fixing') pywikibot.showDiff(page.get(), text) try: page.put(text, comment) except (pywikibot.Error): pywikibot.error('unable to put %s' % page)
def send_mailnotification(text, subject): username = pywikibot.config.usernames[pywikibot.config.family][pywikibot.config.mylang] pos = username.lower().find("bot") username = username[:pos] if (pos > 0) else username pywikibot.output(u'Sending mail "%s" to "%s" as notification!' % (subject, username)) # JIRA: DRTRIGON-87; output even more debug info (tip by: [email protected]) site = pywikibot.getSite() pywikibot.output(u"Bot allowed to send email: %r" % (site.isAllowed("sendemail"),)) pywikibot.output(u"Permissions: %r" % (site._rights,)) if not site.isAllowed("sendemail"): pywikibot.output(u"Try getting new token: %r" % (site.getToken(getagain=True),)) usr = userlib.User(site, username) try: if usr.sendMail(subject=subject, text=text): # 'text' should be unicode! return except: # break exception handling recursion pywikibot.exception(tb=True) pywikibot.error(u"mail to %s could not be sent!" % username) pywikibot.output(u"May be not logged in - try to send emergency email") try: import smtplib from email.mime.text import MIMEText # sender's and recipient's email addresses FROM = "*****@*****.**" % username.lower() TO = [FROM] # must be a list # Create a text/plain message msg = MIMEText(text) msg["Subject"] = "!EMERGENCY! " + subject msg["From"] = FROM msg["To"] = ", ".join(TO) # Send the mail server = smtplib.SMTP("localhost") server.sendmail(FROM, TO, msg.as_string()) server.quit() except: # break exception handling recursion pywikibot.exception(tb=True) pywikibot.error(u"emergency mail to %s could not be sent!" % TO)
def put_page(self, page, new): """ Prints diffs between orginal and new (text), puts new text for page """ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) pywikibot.showDiff(page.get(), new) if not self.acceptall: choice = pywikibot.inputChoice( u"Do you want to accept " + u"these changes?", ["Yes", "No", "All"], ["y", "N", "a"], "N" ) if choice == "a": self.acceptall = True if choice == "y": page.put_async(new, self.msg) if self.acceptall: try: page.put(new, self.msg) 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.error(u"putting page: %s" % error.args)
def defineSign(self, force = False): if hasattr(self,'_randomSignature') and not force: return self._randomSignature signText = u'' creg = re.compile(r"^\* ?(.*?)$", re.M) if not globalvar.signFileName: signPageName = pywikibot.translate(self.site, random_sign) if not signPageName: showStatus(4) pywikibot.output("%s doesn't allow random signature, force disable." % self.site) globalvar.randomSign = False return None signPage = pywikibot.Page(self.site, signPageName ) if signPage.exists(): pywikibot.output('Loading signature list...') signText = signPage.get() else: pywikibot.output('The Signature list page is not exist, random signature will disable.') globalvar.randomSign = False else: try: f = codecs.open(pywikibot.config.datafilepath(globalvar.signFileName), 'r', encoding=config.console_encoding) except LookupError: f = codecs.open(pywikibot.config.datafilepath(globalvar.signFileName), 'r', encoding='utf-8') except IOError: pywikibot.error(u'No fileName!') raise FilenameNotSet("No signature filename specified.") signText = f.read() f.close() self._randomSignature = creg.findall(signText) return self._randomSignature
def featuredArticles(site, pType): articles = [] if pType == 'good': info = good_name elif pType == 'former': info = former_name elif pType == 'list': info = lists_name else: info = featured_name try: method = info[site.lang][0] except KeyError: pywikibot.error( u'language %s doesn\'t has %s category source.' % (site.lang, pType)) return name = info[site.lang][1] # hide #-sorted items on en-wiki try: hide = info[site.lang][2] except IndexError: hide = None raw = method(site, name, hide) for p in raw: if p.namespace() == 0: # Article articles.append(p) # Article talk (like in English) elif p.namespace() == 1 and site.lang != 'el': articles.append(pywikibot.Page(p.site, p.title(withNamespace=False))) pywikibot.output( '\03{lightred}** wikipedia:%s has %i %s articles\03{default}' % (site.lang, len(articles), pType)) for p in articles: yield copy(p)
def change_category(article, oldCat, newCat, comment=None, sortKey=None, inPlace=False): """ Remove page from oldCat and add it to newCat. @param oldCat and newCat: should be Category objects. If newCat is None, the category will be removed. @param comment: string to use as an edit summary @param sortKey: sortKey to use for the added category. Unused if newCat is None, or if inPlace=True @param inPlace: if True, change categories in place rather than rearranging them. """ cats = [] # get list of Category objects the article is in and remove duplicates for cat in article.categories(get_redirect=True): if cat not in cats: cats.append(cat) site = article.site() if not sortKey: sortKey = oldCat.sortKey if not article.canBeEdited(): pywikibot.output("Can't edit %s, skipping it..." % article.title(asLink=True)) return if oldCat not in cats: pywikibot.error(u'%s is not in category %s!' % (article.title(asLink=True), oldCat.title())) return if inPlace or article.namespace() == 10: oldtext = article.get(get_redirect=True) newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat) else: if newCat: cats[cats.index(oldCat)] = Category(site, newCat.title(), sortKey=sortKey) else: cats.pop(cats.index(oldCat)) oldtext = article.get(get_redirect=True) try: newtext = pywikibot.replaceCategoryLinks(oldtext, cats) except ValueError: # Make sure that the only way replaceCategoryLinks() can return # a ValueError is in the case of interwiki links to self. pywikibot.output(u'Skipping %s because of interwiki link to self' % article) if oldtext != newtext: try: article.put(newtext, comment) except pywikibot.EditConflict: pywikibot.output(u'Skipping %s because of edit conflict' % article.title()) except pywikibot.SpamfilterError, e: pywikibot.output(u'Skipping %s because of blacklist entry %s' % (article.title(), e.url)) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s because page is locked' % article.title())
# The page, where the bot logs to logPages = {"hu.wikipedia": u"Wikipédia:Potenciálisan vandalizmus áldozatául esett szócikkek"} # To add a new language, create or find the bad word page # similarly to the 'hu.wikipedia' one (one word per line, starting with <pre> and ending with </pre> lines), # and add to the badWordList lines below. badWordList = {"hu.wikipedia": u"User:Cenzúrabot/lista"} site = pywikibot.getSite() if ( not (site.language() + "." + site.family.name) in badWordList or not (site.language() + "." + site.family.name) in logPages ): pywikibot.error("your language isn't supported, see the source code for further details") sys.exit(1) ownWordPage = pywikibot.Page(site, badWordList[site.language() + "." + site.family.name]) try: ownWordList = ownWordPage.get(get_redirect=True) except pywikibot.NoPage: pywikibot.error("the page containing the bad word list of your language doesn't exist") sys.exit(1) ownWordList = ownWordList.split("\n") del ownWordList[0] del ownWordList[len(ownWordList) - 1] def seekbpos(str1, str2): i = 0 while i < len(str1):
def run(self): """ Starts the robot. """ # Run the generator which will yield Pages which might need to be # changed. for page in self.generator: if self.isTitleExcepted(page.title()): pywikibot.output(u"Skipping %s because the title is on the exceptions list." % page.title(asLink=True)) continue try: # Load the page's text from the wiki original_text = page.get(get_redirect=True) if not (self.articles or page.canBeEdited()): pywikibot.output(u"You can't edit page %s" % page.title(asLink=True)) continue except pywikibot.NoPage: pywikibot.output(u"Page %s not found" % page.title(asLink=True)) continue new_text = original_text while True: if self.isTextExcepted(new_text): pywikibot.output( u"Skipping %s because it contains text that is on the exceptions list." % page.title(asLink=True) ) break new_text = self.doReplacements(new_text) if new_text == original_text: pywikibot.output(u"No changes were necessary in %s" % page.title(asLink=True)) break if self.recursive: newest_text = self.doReplacements(new_text) while newest_text != new_text: new_text = newest_text newest_text = self.doReplacements(new_text) if hasattr(self, "addedCat"): cats = page.categories() if self.addedCat not in cats: cats.append(self.addedCat) new_text = pywikibot.replaceCategoryLinks(new_text, cats) # 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(original_text, new_text) if self.acceptall: break if self.exctitles: choice = pywikibot.inputChoice( u"Do you want to accept these changes?", ["Yes", "No", "no+eXcept", "Edit", "open in Browser", "All", "Quit"], ["y", "N", "x", "e", "b", "a", "q"], "N", ) else: choice = pywikibot.inputChoice( u"Do you want to accept these changes?", ["Yes", "No", "Edit", "open in Browser", "All", "Quit"], ["y", "N", "e", "b", "a", "q"], "N", ) if choice == "e": editor = editarticle.TextEditor() as_edited = editor.edit(original_text) # if user didn't press Cancel if as_edited and as_edited != new_text: new_text = as_edited continue if choice == "b": webbrowser.open("http://%s%s" % (page.site.hostname(), page.site.nice_get_address(page.title()))) i18n.input("pywikibot-enter-finished-browser") try: original_text = page.get(get_redirect=True, force=True) except pywikibot.NoPage: pywikibot.output(u"Page %s has been deleted." % page.title()) break new_text = original_text continue if choice == "q": self.writeEditCounter() self.writeExceptCounter() return if choice == "a": self.acceptall = True if choice == "x": # May happen only if self.exctitles isn't None self.exctitles.write(u"ur'^%s$',\n" % re.escape(page.title())) self.exctitles.flush() self.exceptcounter += 1 if choice == "y": if not self.articles: # Primary behaviour: working on wiki page.put_async(new_text, self.editSummary) self.editcounter += 1 # Bug: this increments even if put_async fails # This is separately in two clauses of if for # future purposes to get feedback form put_async else: # Save the title for later processing instead of editing self.editcounter += 1 self.articles.write(u"#%s\n%s" % (page.title(asLink=True, textlink=True), self.splitLine())) self.articles.flush() # For the peace of our soul :-) # choice must be 'N' break if self.acceptall and new_text != original_text: if not self.articles: # Primary behaviour: working on wiki try: page.put(new_text, self.editSummary) self.editcounter += 1 # increment only on success 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.error(u"putting page: %s" % (error.args,)) except pywikibot.LockedPage: pywikibot.output(u"Skipping %s (locked page)" % (page.title(),))
def main(): # Load the configurations in the function namespace global commento; global Template; global disambigPage; global commenttodisambig global exception enablePage = None # Check if someone set an enablePage or not limit = 50000 # All the pages! (I hope that there aren't so many lonely pages in a project..) generator = None # Check if the bot should use the default generator or not genFactory = pagegenerators.GeneratorFactory() # Load all the default generators! nwpages = False # Check variable for newpages always = False # Check variable for always disambigPage = None # If no disambigPage given, not use it. # Arguments! for arg in pywikibot.handleArgs(): if arg.startswith('-enable'): if len(arg) == 7: enablePage = pywikibot.input(u'Would you like to check if the bot should run or not?') else: enablePage = arg[8:] if arg.startswith('-disambig'): if len(arg) == 9: disambigPage = pywikibot.input(u'In which page should the bot save the disambig pages?') else: disambigPage = arg[10:] elif arg.startswith('-limit'): if len(arg) == 6: limit = int(pywikibot.input(u'How many pages do you want to check?')) else: limit = int(arg[7:]) elif arg.startswith('-newpages'): if len(arg) == 9: nwlimit = 50 # Default: 50 pages else: nwlimit = int(arg[10:]) generator = pywikibot.getSite().newpages(number = nwlimit) nwpages = True elif arg == '-always': always = True else: genFactory.handleArg(arg) # Retrive the site wikiSite = pywikibot.getSite() if not generator: generator = genFactory.getCombinedGenerator() # If the generator is not given, use the default one if not generator: generator = wikiSite.lonelypages(repeat = True, number = limit) # Take the configurations according to our project comment = pywikibot.translate(wikiSite, commento) commentdisambig = pywikibot.translate(wikiSite, commenttodisambig) template = pywikibot.translate(wikiSite, Template) exception = pywikibot.translate(wikiSite, exception) # EnablePage part if enablePage != None: # Define the Page Object enable = pywikibot.Page(wikiSite, enablePage) # Loading the page's data try: getenable = enable.get() except pywikibot.NoPage: pywikibot.output(u"%s doesn't esist, I use the page as if it was blank!" % enable.title()) getenable = '' except wikiepedia.IsRedirect: pywikibot.output(u"%s is a redirect, skip!" % enable.title()) getenable = '' # If the enable page is set to disable, turn off the bot # (useful when the bot is run on a server) if getenable != 'enable': pywikibot.output('The bot is disabled') return # DisambigPage part if disambigPage != None: disambigpage = pywikibot.Page(wikiSite, disambigPage) try: disambigtext = disambigpage.get() except pywikibot.NoPage: pywikibot.output(u"%s doesn't esist, skip!" % disambigpage.title()) disambigtext = '' except wikiepedia.IsRedirect: pywikibot.output(u"%s is a redirect, don't use it!" % disambigpage.title()) disambigPage = None # Main Loop for page in generator: if nwpages == True: page = page[0] # The newpages generator returns a tuple, not a Page object. pywikibot.output(u"Checking %s..." % page.title()) # Used to skip the first pages in test phase... #if page.title()[0] in ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q']: #continue if page.isRedirectPage(): # If redirect, skip! pywikibot.output(u'%s is a redirect! Skip...' % page.title()) continue # refs is not a list, it's a generator while resList... is a list, yes. refs = page.getReferences() refsList = list() for j in refs: if j == None: # We have to find out why the function returns that value pywikibot.error(u'1 --> Skip page') continue refsList.append(j) # This isn't possible with a generator if refsList != []: pywikibot.output(u"%s isn't orphan! Skip..." % page.title()) continue # Never understood how a list can turn in "None", but it happened :-S elif refsList == None: # We have to find out why the function returns that value pywikibot.error(u'2 --> Skip page') continue else: # Ok, no refs, no redirect... let's check if there's already the template try: oldtxt = page.get() except pywikibot.NoPage: pywikibot.output(u"%s doesn't exist! Skip..." % page.title()) continue except pywikibot.IsRedirectPage: pywikibot.output(u"%s is a redirect! Skip..." % page.title()) continue # I've used a loop in a loop. If I use continue in the second loop, it won't do anything # in the first. So let's create a variable to avoid this problem. Find = False for regexp in exception: res = re.findall(regexp, oldtxt.lower()) # Found a template! Let's skip the page! if res != []: pywikibot.output(u'Your regex has found something in %s, skipping...' % page.title()) Find = True break # Skip the page.. if Find: continue # Is the page a disambig? if page.isDisambig() and disambigPage != None: pywikibot.output(u'%s is a disambig page, report..' % page.title()) if not page.title().lower() in disambigtext.lower(): disambigtext = u"%s\n*[[%s]]" % (disambigtext, page.title()) disambigpage.put(disambigtext, commentdisambig) continue # Is the page a disambig but there's not disambigPage? Skip! elif page.isDisambig(): pywikibot.output(u'%s is a disambig page, skip...' % page.title()) continue else: # Ok, the page need the template. Let's put it there! newtxt = u"%s\n%s" % (template, oldtxt) # Adding the template in the text pywikibot.output(u"\t\t>>> %s <<<" % page.title()) # Showing the title pywikibot.showDiff(oldtxt, newtxt) # Showing the changes choice = 'y' # Default answer if not always: choice = pywikibot.inputChoice(u'Orphan page found, shall I add the template?', ['Yes', 'No', 'All'], ['y', 'n', 'a']) if choice == 'a': always = True choice = 'y' if choice == 'y': try: page.put(newtxt, comment) except pywikibot.EditConflict: pywikibot.output(u'Edit Conflict! Skip...') continue
'hu.wikipedia': u'Wikipédia:Potenciálisan vandalizmus áldozatául esett szócikkek', } # To add a new language, create or find the bad word page # similarly to the 'hu.wikipedia' one (one word per line, starting with <pre> # and ending with </pre> lines), and add to the badWordList lines below. badWordList = { 'hu.wikipedia': u'User:Cenzúrabot/lista', } site = pywikibot.getSite() if not (site.language() + '.' + site.family.name) in badWordList or \ not (site.language() + '.' + site.family.name) in logPages: pywikibot.error('your language isn\'t supported, see the source code for ' 'further details') sys.exit(1) ownWordPage = pywikibot.Page( site, badWordList[site.language() + '.' + site.family.name]) try: ownWordList = ownWordPage.get(get_redirect=True) except pywikibot.NoPage: pywikibot.error('the page containing the bad word list of your language ' 'doesn\'t exist') sys.exit(1) ownWordList = ownWordList.split('\n') del ownWordList[0] del ownWordList[len(ownWordList) - 1] def seekbpos(str1, str2):
if cat == oldCat: changesMade = True if not sortKey: sortKey = cat.sortKey if newCat: if newCat.title() not in newCatSet: newCategory = Category(site, newCat.title(), sortKey=sortKey) newCatSet.add(newCat.title()) newCatList.append(newCategory) elif cat.title() not in newCatSet: newCatSet.add(cat.title()) newCatList.append(cat) if not changesMade: pywikibot.error(u'%s is not in category %s!' % (article.title(asLink=True), oldCat.title())) else: text = article.get(get_redirect=True) try: text = pywikibot.replaceCategoryLinks(text, newCatList) except ValueError: # Make sure that the only way replaceCategoryLinks() can return # a ValueError is in the case of interwiki links to self. pywikibot.output(u'Skipping %s because of interwiki link to self' % article) try: article.put(text, comment) except pywikibot.EditConflict: pywikibot.output(u'Skipping %s because of edit conflict' % article.title()) except pywikibot.SpamfilterError, e:
def handlebadpage(self): try: self.content = self.page.get() except pywikibot.IsRedirectPage: pywikibot.output(u'Already redirected, skipping.') return except pywikibot.NoPage: pywikibot.output(u'Already deleted') return for d in pywikibot.translate(pywikibot.getSite(), done): if d in self.content: pywikibot.output( u'Found: "%s" in content, nothing necessary' % d) return print "---- Start content ----------------" pywikibot.output(u"%s" % self.content) print "---- End of content ---------------" # Loop other user answer answered = False while not answered: answer = pywikibot.input(question) if answer == 'q': sys.exit("Exiting") if answer == 'd': pywikibot.output(u'Trying to delete page [[%s]].' % self.page.title()) self.page.delete() return if answer == 'e': oldText = self.page.get() text = oldText editor = editarticle.TextEditor() text = editor.edit(self.page.get()) if oldText != text: pywikibot.showDiff(oldText, text) msg = pywikibot.input(u'Summary message:') self.page.put(text, msg) return if answer == 'b': pywikibot.output(u'Blanking page [[%s]].' % self.page.title()) try: self.page.put('', comment=pywikibot.translate( pywikibot.getSite(), blanking) % self.content) except EditConflict: print "An edit conflict occured ! Automatically retrying" handlebadpage(self) return if answer == '': print 'Page correct ! Proceeding with next pages.' return # Check user input: if answer[0] == 'u': # Answer entered as an utf8 string try: choices = answer[1:].split(',') except ValueError: # User entered wrong value pywikibot.error(u'"%s" is not valid' % answer) continue else: try: choices = answer.split(',') except ValueError: # User entered wrong value pywikibot.error(u'"%s" is not valid' % answer) continue #test input for choice in choices: try: x = int(choice) except ValueError: break else: answered = x in range(1, len(questionlist)+1) if not answered: pywikibot.error(u'"%s" is not valid' % answer) continue summary = u'' for choice in choices: answer = int(choice) # grab the template parameters tpl = pywikibot.translate(pywikibot.getSite(), templates)[questionlist[answer]] if tpl['pos'] == 'top': pywikibot.output(u'prepending %s...' % questionlist[answer]) self.content = questionlist[answer] + '\n' + self.content elif tpl['pos'] == 'bottom': pywikibot.output(u'appending %s...' % questionlist[answer]) self.content += '\n' + questionlist[answer] else: pywikibot.error( u'"pos" should be "top" or "bottom" for template ' u'%s. Contact a developer.' % questionlist[answer]) sys.exit("Exiting") summary += tpl['msg']+' ' pywikibot.output(u'Probably added %s' % questionlist[answer]) # pywikibot.output(newcontent) bug #2986247 self.page.put(self.content, comment=summary) pywikibot.output(u'with comment %s\n' % summary)
changesMade = True if not sortKey: sortKey = cat.sortKey if newCat: if newCat.title() not in newCatSet: newCategory = Category(site, newCat.title(), sortKey=sortKey) newCatSet.add(newCat.title()) newCatList.append(newCategory) elif cat.title() not in newCatSet: newCatSet.add(cat.title()) newCatList.append(cat) if not changesMade: pywikibot.error(u'%s is not in category %s!' % (article.title(asLink=True), oldCat.title())) else: text = article.get(get_redirect=True) try: text = pywikibot.replaceCategoryLinks(text, newCatList) except ValueError: # Make sure that the only way replaceCategoryLinks() can return # a ValueError is in the case of interwiki links to self. pywikibot.output(u'Skipping %s because of interwiki link to self' % article) try: article.put(text, comment) except pywikibot.EditConflict: pywikibot.output(u'Skipping %s because of edit conflict' % article.title()) except pywikibot.SpamfilterError, e:
def run(self): """ Starts the robot. """ # Run the generator which will yield Pages which might need to be # changed. for page in self.generator: if self.isTitleExcepted(page.title()): pywikibot.output( u'Skipping %s because the title is on the exceptions list.' % page.title(asLink=True)) continue try: # Load the page's text from the wiki original_text = page.get(get_redirect=True) if not (self.articles or page.canBeEdited()): pywikibot.output(u"You can't edit page %s" % page.title(asLink=True)) continue except pywikibot.NoPage: pywikibot.output(u'Page %s not found' % page.title(asLink=True)) continue new_text = original_text while True: if self.isTextExcepted(new_text): pywikibot.output( u'Skipping %s because it contains text that is on the exceptions list.' % page.title(asLink=True)) break new_text = self.doReplacements(new_text) if new_text == original_text: pywikibot.output(u'No changes were necessary in %s' % page.title(asLink=True)) break if self.recursive: newest_text = self.doReplacements(new_text) while (newest_text != new_text): new_text = newest_text newest_text = self.doReplacements(new_text) if hasattr(self, "addedCat"): cats = page.categories() if self.addedCat not in cats: cats.append(self.addedCat) new_text = pywikibot.replaceCategoryLinks( new_text, cats) # 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(original_text, new_text) if self.acceptall: break if self.exctitles: choice = pywikibot.inputChoice( u'Do you want to accept these changes?', [ 'Yes', 'No', 'no+eXcept', 'Edit', 'open in Browser', 'All', 'Quit' ], ['y', 'N', 'x', 'e', 'b', 'a', 'q'], 'N') else: choice = pywikibot.inputChoice( u'Do you want to accept these changes?', [ 'Yes', 'No', 'Edit', 'open in Browser', 'All', 'Quit' ], ['y', 'N', 'e', 'b', 'a', 'q'], 'N') if choice == 'e': editor = editarticle.TextEditor() as_edited = editor.edit(original_text) # if user didn't press Cancel if as_edited and as_edited != new_text: new_text = as_edited continue if choice == 'b': webbrowser.open("http://%s%s" % (page.site.hostname(), page.site.nice_get_address(page.title()))) i18n.input('pywikibot-enter-finished-browser') try: original_text = page.get(get_redirect=True, force=True) except pywikibot.NoPage: pywikibot.output(u'Page %s has been deleted.' % page.title()) break new_text = original_text continue if choice == 'q': self.writeEditCounter() self.writeExceptCounter() return if choice == 'a': self.acceptall = True if choice == 'x': #May happen only if self.exctitles isn't None self.exctitles.write(u"ur'^%s$',\n" % re.escape(page.title())) self.exctitles.flush() self.exceptcounter += 1 if choice == 'y': if not self.articles: # Primary behaviour: working on wiki page.put_async(new_text, self.editSummary) self.editcounter += 1 # Bug: this increments even if put_async fails # This is separately in two clauses of if for # future purposes to get feedback form put_async else: #Save the title for later processing instead of editing self.editcounter += 1 self.articles.write(u'#%s\n%s' % (page.title( asLink=True, textlink=True), self.splitLine())) self.articles.flush() # For the peace of our soul :-) # choice must be 'N' break if self.acceptall and new_text != original_text: if not self.articles: #Primary behaviour: working on wiki try: page.put(new_text, self.editSummary) self.editcounter += 1 #increment only on success 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.error(u'putting page: %s' % (error.args, )) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % (page.title(), ))
def change_category(article, oldCat, newCat, comment=None, sortKey=None, inPlace=False): """ Remove page from oldCat and add it to newCat. @param oldCat and newCat: should be Category objects. If newCat is None, the category will be removed. @param comment: string to use as an edit summary @param sortKey: sortKey to use for the added category. Unused if newCat is None, or if inPlace=True @param inPlace: if True, change categories in place rather than rearranging them. """ cats = [] # get list of Category objects the article is in and remove duplicates for cat in article.categories(get_redirect=True): if cat not in cats: cats.append(cat) site = article.site() if not sortKey: sortKey = oldCat.sortKey if not article.canBeEdited(): pywikibot.output("Can't edit %s, skipping it..." % article.title(asLink=True)) return if oldCat not in cats: pywikibot.error(u"%s is not in category %s!" % (article.title(asLink=True), oldCat.title())) return if inPlace or article.namespace() == 10: oldtext = article.get(get_redirect=True) newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat) else: if newCat: cats[cats.index(oldCat)] = Category(site, newCat.title(), sortKey=sortKey) else: cats.pop(cats.index(oldCat)) oldtext = article.get(get_redirect=True) try: newtext = pywikibot.replaceCategoryLinks(oldtext, cats) except ValueError: # Make sure that the only way replaceCategoryLinks() can return # a ValueError is in the case of interwiki links to self. pywikibot.output(u"Skipping %s because of interwiki link to self" % article) if oldtext != newtext: try: article.put(newtext, comment) except pywikibot.EditConflict: pywikibot.output(u"Skipping %s because of edit conflict" % article.title()) except pywikibot.SpamfilterError, e: pywikibot.output(u"Skipping %s because of blacklist entry %s" % (article.title(), e.url)) except pywikibot.LockedPage: pywikibot.output(u"Skipping %s because page is locked" % article.title())
logPages = { 'hu.wikipedia': u'Wikipédia:Potenciálisan vandalizmus áldozatául esett szócikkek', } # To add a new language, create or find the bad word page # similarly to the 'hu.wikipedia' one (one word per line, starting with <pre> # and ending with </pre> lines), and add to the badWordList lines below. badWordList = { 'hu.wikipedia': u'User:Cenzúrabot/lista', } site = pywikibot.getSite() if not (site.language() + '.' + site.family.name) in badWordList or \ not (site.language() + '.' + site.family.name) in logPages: pywikibot.error('your language isn\'t supported, see the source code for ' 'further details') sys.exit(1) ownWordPage = pywikibot.Page(site, badWordList[site.language() + '.' + site.family.name]) try: ownWordList = ownWordPage.get(get_redirect=True) except pywikibot.NoPage: pywikibot.error('the page containing the bad word list of your language ' 'doesn\'t exist') sys.exit(1) ownWordList = ownWordList.split('\n') del ownWordList[0] del ownWordList[len(ownWordList) - 1] def seekbpos(str1, str2):