def do_cat(self, d): cat = pywikibot.Category( self.site, 'Category:Transport infrastructure completed in %(year)s' % d) if cat.exists(): subcats = list(cat.subcategories()) else: subcats = [] good_to_go = False shouldhave = [] for c in ALL_CATS: real = pywikibot.Category(self.site, c % d) if real.exists(): good_to_go = True if not (real in subcats): shouldhave.append(real) if not good_to_go: return if not cat.exists(): self.edit( cat, TRANSPORT_TEXT % d, 'BOT: Creating Transport infrastructure by year category') self.output('\n* creating [[:%s]]' % cat.title()) for i in shouldhave: t = i.get() n = pywikibot.replaceCategoryLinks(t, [cat], addOnly=True) self.edit(i, n, 'BOT: Adding [[:%s]]' % cat.title()) self.output('\n*adding [[:%s]] to [[:%s]]' % (cat.title(), i.title()))
def treat(self, page): text = self.load(page) if text is None: return cats = [c for c in page.categories()] # 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.output(u"Current categories:") for cat in cats: pywikibot.output(u"* %s" % cat.title()) catpl = pywikibot.Page(self.site, self.newcatTitle, defaultNamespace=14) if catpl in cats: pywikibot.output(u"%s is already in %s." % (page.title(), catpl.title())) else: if self.sort: catpl = self.sorted_by_last_name(catpl, page) pywikibot.output(u'Adding %s' % catpl.title(asLink=True)) cats.append(catpl) text = pywikibot.replaceCategoryLinks(text, cats) if not self.save(text, page, self.editSummary): pywikibot.output(u'Page %s not saved.' % page.title(asLink=True))
def run(self): for merge in self.merges: source_cat = pywikibot.Category(site, merge[0]) target_cat1 = pywikibot.Category(site, merge[1]) target_cat2 = pywikibot.Category(site, merge[2]) if not (source_cat.exists() and target_cat1.exists() and target_cat2.exists()): continue for page in source_cat.articles(): if page.namespace() != 0: continue text = page.get() page_cats = pywikibot.getCategoryLinks(text) if source_cat in page_cats: page_cats.remove(source_cat) if not target_cat1 in page_cats: page_cats.append(target_cat1) if not target_cat2 in page_cats: page_cats.append(target_cat2) new_text = pywikibot.replaceCategoryLinks(text, page_cats) if text == new_text: continue pywikibot.showDiff(text, new_text) page.put( new_text, "[[Wikipedia:Bots|Bot]]: Merging from %s to %s and %s per CFD outcome" % (source_cat.title(asLink=True), target_cat1.title(asLink=True), target_cat2.title(asLink=True))) break
def run(self): for merge in self.merges: source_cat = pywikibot.Category(site, merge[0]) target_cat1 = pywikibot.Category(site, merge[1]) target_cat2 = pywikibot.Category(site, merge[2]) if not (source_cat.exists() and target_cat1.exists() and target_cat2.exists()): continue for page in source_cat.articles(): if page.namespace() != 0: continue text = page.get() page_cats = pywikibot.getCategoryLinks(text) if source_cat in page_cats: page_cats.remove(source_cat) if not target_cat1 in page_cats: page_cats.append(target_cat1) if not target_cat2 in page_cats: page_cats.append(target_cat2) new_text = pywikibot.replaceCategoryLinks(text, page_cats) if text == new_text: continue pywikibot.showDiff(text, new_text) page.put( new_text, "[[Wikipedia:Bots|Bot]]: Merging from %s to %s and %s per CFD outcome" % ( source_cat.title(asLink=True), target_cat1.title(asLink=True), target_cat2.title(asLink=True) ) ) break
def treat(self, page): text = self.load(page) if text is None: return cats = [c for c in page.categories()] # 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.output(u"Current categories:") for cat in cats: pywikibot.output(u"* %s" % cat.title()) newcatTitle = self.newcatTitle if not page.site.nocapitalize: newcatTitle = newcatTitle[:1].upper() + newcatTitle[1:] catpl = pywikibot.Page(page.site, newcatTitle, ns=14) if catpl in cats: pywikibot.output(u"%s is already in %s." % (page.title(), catpl.title())) else: if self.sort: catpl = self.sorted_by_last_name(catpl, page) pywikibot.output(u'Adding %s' % catpl.title(asLink=True)) cats.append(catpl) text = pywikibot.replaceCategoryLinks(text, cats) if not self.save(text, page, newcatTitle): pywikibot.output(u'Page %s not saved.' % page.title(asLink=True))
def do_cat(self, d): cat = pywikibot.Category(self.site, 'Category:Transport infrastructure completed in %(year)s' % d) if cat.exists(): subcats = list(cat.subcategories()) else: subcats = [] good_to_go = False shouldhave = [] for c in ALL_CATS: real = pywikibot.Category(self.site, c % d) if real.exists(): good_to_go = True if not (real in subcats): shouldhave.append(real) if not good_to_go: return if not cat.exists(): self.edit(cat, TRANSPORT_TEXT % d, 'BOT: Creating Transport infrastructure by year category') self.output('\n* creating [[:%s]]' % cat.title()) for i in shouldhave: t= i.get() n=pywikibot.replaceCategoryLinks(t, [cat],addOnly=True) self.edit(i, n, 'BOT: Adding [[:%s]]' % cat.title()) self.output('\n*adding [[:%s]] to [[:%s]]' % (cat.title(), i.title()))
def categories(self): for page in self.generator: try: pywikibot.output(u'\n>>>> %s <<<<' % page.title()) commons = pywikibot.Site().image_repository() commonsCategory = pywikibot.Category( commons, 'Category:%s' % page.title()) try: getcommonscat = commonsCategory.text commonsCategoryTitle = commonsCategory.title() categoryname = commonsCategoryTitle.split('Category:', 1)[1] if page.title() == categoryname: oldText = page.get() text = oldText # for commonscat template findTemplate = re.compile(ur'\{\{[Cc]ommons') s = findTemplate.search(text) findTemplate2 = re.compile(ur'\{\{[Ss]isterlinks') s2 = findTemplate2.search(text) if s or s2: pywikibot.output(u'** Already done.') else: text = pywikibot.replaceCategoryLinks( text + u'{{commonscat|%s}}' % categoryname, list(page.categories())) if oldText != text: pywikibot.showDiff(oldText, text) if not self.acceptall: choice = pywikibot.inputChoice( u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice == 'a': self.acceptall = True if self.acceptall or choice == 'y': try: msg = i18n.twtranslate( pywikibot.Site(), 'commons_link-cat-template-added') page.put(text, msg) except pywikibot.EditConflict: pywikibot.output( u'Skipping %s because of edit ' u'conflict' % (page.title())) except pywikibot.NoPage: pywikibot.output(u'Category does not exist in Commons!') except pywikibot.NoPage: pywikibot.output(u'Page %s does not exist?!' % page.title()) except pywikibot.IsRedirectPage: pywikibot.output(u'Page %s is a redirect; skipping.' % page.title()) except pywikibot.LockedPage: pywikibot.output(u'Page %s is locked?!' % page.title())
def make_categories(page, list, site=None): if site is None: site = pywikibot.getSite() pllist = [] for p in list: cattitle = "%s:%s" % (site.category_namespace(), p) pllist.append(pywikibot.Page(site, cattitle)) page.put_async(pywikibot.replaceCategoryLinks(page.get(), pllist), comment=i18n.twtranslate(site.lang, 'catall-changing'))
def make_categories(page, list, site=None): if site is None: site = pywikibot.Site() pllist = [] for p in list: cattitle = "%s:%s" % (site.category_namespace(), p) pllist.append(pywikibot.Page(site, cattitle)) page.put_async(pywikibot.replaceCategoryLinks(page.get(), pllist), comment=i18n.twtranslate(site.code, 'catall-changing'))
def categories(self): for page in self.generator: try: pywikibot.output(u'\n>>>> %s <<<<' % page.title()) commons = pywikibot.getSite().image_repository() commonsCategory = pywikibot.Category(commons, 'Category:%s' % page.title()) try: getcommonscat = commonsCategory.get(get_redirect=True) commonsCategoryTitle = commonsCategory.title() categoryname = commonsCategoryTitle.split('Category:', 1)[1] if page.title() == categoryname: oldText = page.get() text = oldText # for commonscat template findTemplate = re.compile(ur'\{\{[Cc]ommons') s = findTemplate.search(text) findTemplate2 = re.compile(ur'\{\{[Ss]isterlinks') s2 = findTemplate2.search(text) if s or s2: pywikibot.output(u'** Already done.') else: text = pywikibot.replaceCategoryLinks( text + u'{{commonscat|%s}}' % categoryname, list(page.categories())) if oldText != text: pywikibot.showDiff(oldText, text) if not self.acceptall: choice = pywikibot.inputChoice( u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice == 'a': self.acceptall = True if self.acceptall or choice == 'y': try: msg = i18n.twtranslate( pywikibot.getSite(), 'commons_link-cat-template-added') page.put(text, msg) except pywikibot.EditConflict: pywikibot.output( u'Skipping %s because of edit ' u'conflict' % (page.title())) except pywikibot.NoPage: pywikibot.output(u'Category does not exist in Commons!') except pywikibot.NoPage: pywikibot.output(u'Page %s does not exist?!' % page.title()) except pywikibot.IsRedirectPage: pywikibot.output(u'Page %s is a redirect; skipping.' % page.title()) except pywikibot.LockedPage: pywikibot.output(u'Page %s is locked?!' % page.title())
def putAfterTemplate(oldtext, template, toadd, loose=True): ''' Try to put text after template. If the template is not found return False if loose is set to False If loose is set to True: Remove interwiki's, categories, add template, restore categories, restore interwiki's. Based on cc-by-sa-3.0 code by Dschwen ''' newtext = u'' templatePosition = oldtext.find(u'{{%s' % (template,)) if templatePosition >= 0: previousChar = u'' currentChar = u'' templatePosition += 2 curly = 1 square = 0 while templatePosition < len(oldtext): currentChar = oldtext[templatePosition] if currentChar == u'[' and previousChar == u'[': square += 1 previousChar = u'' if currentChar == u']' and previousChar == u']': square -= 1 previousChar = u'' if currentChar == u'{' and previousChar == u'{': curly += 1 previousChar = u'' if currentChar == u'}' and previousChar == u'}': curly -= 1 previousChar = u'' previousChar = currentChar templatePosition += 1 if curly == 0 and square <= 0: # Found end of template break newtext = oldtext[:templatePosition] + \ u'\n' + toadd + oldtext[templatePosition:] else: if loose: newtext = oldtext cats = pywikibot.getCategoryLinks(newtext) ll = pywikibot.getLanguageLinks(newtext) newtext = pywikibot.removeLanguageLinks(newtext) newtext = pywikibot.removeCategoryLinks(newtext) newtext += u'\n' + toadd newtext = pywikibot.replaceCategoryLinks(newtext, cats) newtext = pywikibot.replaceLanguageLinks(newtext, ll) return newtext
def addRemoveRepoCats(article, repos, allRepoCats, comment=None): # Create list of repos to be removed notRepos = [] if not article.canBeEdited(): pywikibot.output("Can't edit %s, skipping it..." % article.aslink()) return False cats = article.categories(get_redirect=True) site = article.site changesMade = False newCatList = [] newCatSet = set() repoCatList = [] #remove all repos for cat in cats: if cat in allRepoCats: changesMade = True continue if cat.title() not in newCatSet: newCatSet.add(cat.title()) newCatList.append(cat) #add relevant repos for i in range(len(repos)): repo = repos[i] newCatList.append(catlib.Category(site, 'Category:' + repoCats[repo])) changesMade = True if not changesMade: pywikibot.output(u'No changes necessary to %s!' % article.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='Addon-Bot repo category update', watchArticle=None, minorEdit=True) 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())
def standardizePageFooter(self, text): """ Makes sure that categories are put to the correct position, but does not sort them. """ # The PyWikipediaBot is no longer allowed to touch categories on the German Wikipedia. See http://de.wikipedia.org/wiki/Hilfe_Diskussion:Personendaten/Archiv/bis_2006#Position_der_Personendaten_am_.22Artikelende.22 if self.site != pywikibot.getSite('de', 'wikipedia') and not self.template: categories = pywikibot.getCategoryLinks(text, site = self.site) text = pywikibot.replaceCategoryLinks(text, categories, site = self.site) return text
def addRemoveRepoCats(article, repos, allRepoCats, comment=None): # Create list of repos to be removed notRepos = [] if not article.canBeEdited(): pywikibot.output("Can't edit %s, skipping it..." % article.aslink()) return False cats = article.categories(get_redirect=True) site = article.site changesMade = False newCatList = [] newCatSet = set() repoCatList = [] #remove all repos for cat in cats: if cat in allRepoCats: changesMade = True continue if cat.title() not in newCatSet: newCatSet.add(cat.title()) newCatList.append(cat) #add relevant repos for i in range(len(repos)): repo = repos[i] newCatList.append(catlib.Category(site, 'Category:'+ repoCats[repo])) changesMade = True if not changesMade: pywikibot.output(u'No changes necessary to %s!' % article.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='Addon-Bot repo category update', watchArticle = None, minorEdit = True) 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())
def do_page(page, cat): text = page.get() if cat.title().lower() in text.lower(): print 'Skipping '+page.title(asLink=True) return newtext = pywikibot.replaceCategoryLinks(text, [cat], site=site, addOnly=True) pywikibot.showDiff(text, newtext) print 'Saving '+page.title(asLink=True) try: page.put(newtext, 'Robot: Adding [[:%s]]' % cat.title()) except pywikibot.exceptions.LockedPage: pass
def filterSubCategory(subcat, category): ''' Filter category from all articles and files in subcat ''' articleGen = pagegenerators.PreloadingGenerator(pagegenerators.CategorizedPageGenerator(subcat)) for article in articleGen: pywikibot.output(u'Working on %s' % (article.title(),)) articleCategories = article.categories() if category in articleCategories: articleCategories.remove(category) text = article.get() newtext = pywikibot.replaceCategoryLinks(text, articleCategories) pywikibot.showDiff(text, newtext) comment = u'Removing [[%s]]: Is already in the subcategory [[%s]]' % (category.title(), subcat.title()) article.put(newtext, comment)
def filterSubCategory(subcat, category): """ Filter category from all articles and files in subcat """ articleGen = pagegenerators.PreloadingGenerator( pagegenerators.CategorizedPageGenerator(subcat)) for article in articleGen: pywikibot.output(u'Working on %s' % (article.title(), )) articleCategories = article.categories() if category in articleCategories: articleCategories.remove(category) text = article.get() newtext = pywikibot.replaceCategoryLinks(text, articleCategories) pywikibot.showDiff(text, newtext) comment = ( u'Removing [[%s]]: Is already in the subcategory [[%s]]' % (category.title(), subcat.title())) article.put(newtext, comment)
def subthread(pagep, catinp, cat, page): if not page: pywikibot.output(pagep.title() + " not found") return pywikibot.output("thread %s >>> adding..." % page.title()) try: if page.namespace() not in wp.conf.nstl: oldcat = set(page.categories(fromtext=True)) newcat = (oldcat - set([catinp])) | set([cat]) if oldcat != newcat: pywikibot.output("Change!") text = pywikibot.replaceCategoryLinks(page.get(), list(newcat), site) page.put(text, u"autoCategory") else: pywikibot.output("Nothing changes!") except: wp.error()
def include(pl, checklinks=True, realinclude=True, linkterm=None): cl = checklinks if linkterm: actualworkingcat = pywikibot.Category(mysite, workingcat.title(), sortKey=linkterm) else: actualworkingcat = workingcat if realinclude: try: text = pl.get() except pywikibot.NoPage: pass except pywikibot.IsRedirectPage: cl = True pass else: cats = [x for x in pl.categories()] if workingcat not in cats: cats = [x for x in pl.categories()] for c in cats: if c in parentcats: if removeparent: pl.change_category(actualworkingcat) break else: pl.put( pywikibot.replaceCategoryLinks( text, cats + [actualworkingcat])) if cl: if checkforward: for page2 in pl.linkedPages(): if needcheck(page2): tocheck.append(page2) checked[page2] = page2 if checkbackward: for refPage in pl.getReferences(): if needcheck(refPage): tocheck.append(refPage) checked[refPage] = refPage
def include(pl, checklinks=True, realinclude=True, linkterm=None): cl = checklinks if linkterm: actualworkingcat = pywikibot.Category(mysite, workingcat.title(), sortKey=linkterm) else: actualworkingcat = workingcat if realinclude: try: text = pl.get() except pywikibot.NoPage: pass except pywikibot.IsRedirectPage: cl = True pass else: cats = [x for x in pl.categories()] if workingcat not in cats: cats = [x for x in pl.categories()] for c in cats: if c in parentcats: if removeparent: pl.change_category(actualworkingcat) break else: pl.put(pywikibot.replaceCategoryLinks( text, cats + [actualworkingcat])) if cl: if checkforward: for page2 in pl.linkedPages(): if needcheck(page2): tocheck.append(page2) checked[page2] = page2 if checkbackward: for refPage in pl.getReferences(): if needcheck(refPage): tocheck.append(refPage) checked[refPage] = refPage
def standardizePageFooter(self, text): """ Makes sure that interwiki links, categories and star templates are put to the correct position and into the right order. This combines the old instances standardizeInterwiki and standardizeCategories The page footer has the following section in that sequence: 1. categories 2. ## TODO: template beyond categories ## 3. additional information depending on local site policy 4. stars templates for featured and good articles 5. interwiki links """ starsList = [ u"bueno", u"bom interwiki", u"cyswllt[ _]erthygl[ _]ddethol", u"dolen[ _]ed", u"destacado", u"destaca[tu]", u"enllaç[ _]ad", u"enllaz[ _]ad", u"leam[ _]vdc", u"legătură[ _]a[bcf]", u"liamm[ _]pub", u"lien[ _]adq", u"lien[ _]ba", u"liên[ _]kết[ _]bài[ _]chất[ _]lượng[ _]tốt", u"liên[ _]kết[ _]chọn[ _]lọc", u"ligam[ _]adq", u"ligoelstara", u"ligoleginda", u"link[ _][afgu]a", u"link[ _]adq", u"link[ _]f[lm]", u"link[ _]km", u"link[ _]sm", u"linkfa", u"na[ _]lotura", u"nasc[ _]ar", u"tengill[ _][úg]g", u"ua", u"yüm yg", u"רא", u"وصلة مقالة جيدة", u"وصلة مقالة مختارة", ] categories = None interwikiLinks = None allstars = [] hasCommentLine = False # The PyWikipediaBot is no longer allowed to touch categories on the # German Wikipedia. See # http://de.wikipedia.org/wiki/Hilfe_Diskussion:Personendaten/Archiv/1#Position_der_Personendaten_am_.22Artikelende.22 # ignoring nn-wiki of cause of the comment line above iw section if ( not self.template and not "{{Personendaten" in text and not "{{SORTIERUNG" in text and not "{{DEFAULTSORT" in text and not self.site.lang in ("et", "it", "bg", "ru") ): categories = pywikibot.getCategoryLinks(text, site=self.site) if not self.talkpage: # and pywikibot.calledModuleName() <> 'interwiki': subpage = False if self.template: loc = None try: tmpl, loc = moved_links[self.site.lang] del tmpl except KeyError: pass if loc != None and loc in self.title: subpage = True interwikiLinks = pywikibot.getLanguageLinks(text, insite=self.site, template_subpage=subpage) # Removing the interwiki text = pywikibot.removeLanguageLinks(text, site=self.site) # Removing the stars' issue starstext = pywikibot.removeDisabledParts(text) for star in starsList: regex = re.compile("(\{\{(?:template:|)%s\|.*?\}\}[\s]*)" % star, re.I) found = regex.findall(starstext) if found != []: text = regex.sub("", text) allstars += found # nn got a message between the categories and the iw's # and they want to keep it there, first remove it if self.site.lang in msg_interwiki: iw_msg = msg_interwiki[self.site.lang] if isinstance(iw_msg, tuple): iw_reg = iw_msg[1] iw_msg = iw_msg[0] else: iw_reg = u"(%s)" % iw_msg regex = re.compile(iw_reg) found = regex.findall(text) if found: hasCommentLine = True text = regex.sub("", text) # Adding categories if categories: ##Sorting categories in alphabetic order. beta test only on Persian Wikipedia, TODO fix bug for sorting # if self.site.language() == 'fa': # categories.sort() ##Taking main cats to top # for name in categories: # if re.search(u"(.+?)\|(.{,1}?)",name.title()) or name.title()==name.title().split(":")[0]+title: # categories.remove(name) # categories.insert(0, name) text = pywikibot.replaceCategoryLinks(text, categories, site=self.site) # Put the iw message back if not self.talkpage and ( (interwikiLinks or hasCommentLine) and self.site.language() == "nn" or (interwikiLinks and hasCommentLine) and self.site.language() == "fr" ): text += config.line_separator * 2 + iw_msg # Adding stars templates if allstars: text = text.strip() + self.site.family.interwiki_text_separator allstars.sort() for element in allstars: text += "%s%s" % (element.strip(), config.line_separator) pywikibot.log(u"%s" % element.strip()) # Adding the interwiki if interwikiLinks: text = pywikibot.replaceLanguageLinks( text, interwikiLinks, site=self.site, template=self.template, template_subpage=subpage ) return text
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 ' u'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 add_text(page=None, addText=None, summary=None, regexSkip=None, regexSkipUrl=None, always=False, up=False, putText=True, oldTextGiven=None, reorderEnabled=True, create=False): # 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. starsList = [ u'bueno', u'bom interwiki', u'cyswllt[ _]erthygl[ _]ddethol', u'dolen[ _]ed', u'destacado', u'destaca[tu]', u'enllaç[ _]ad', u'enllaz[ _]ad', u'leam[ _]vdc', u'legătură[ _]a[bcf]', u'liamm[ _]pub', u'lien[ _]adq', u'lien[ _]ba', u'liên[ _]kết[ _]bài[ _]chất[ _]lượng[ _]tốt', u'liên[ _]kết[ _]chọn[ _]lọc', u'ligam[ _]adq', u'ligoelstara', u'ligoleginda', u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km', u'link[ _]sm', u'linkfa', u'na[ _]lotura', u'nasc[ _]ar', u'tengill[ _][úg]g', u'ua', u'yüm yg', u'רא', u'وصلة مقالة جيدة', u'وصلة مقالة مختارة', ] errorCount = 0 site = pywikibot.getSite() pathWiki = site.family.nicepath(site.lang) site = pywikibot.getSite() 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 # If not up, text put below if not up: newtext = text # Translating the \\n into binary \n addText = addText.replace('\\n', '\n') if (reorderEnabled): # Getting the categories categoriesInside = pywikibot.getCategoryLinks(newtext, site) # Deleting the categories newtext = pywikibot.removeCategoryLinks(newtext, site) # Getting the interwiki interwikiInside = pywikibot.getLanguageLinks(newtext, site) # Removing the interwiki newtext = pywikibot.removeLanguageLinks(newtext, site) # Adding the text newtext += u"\n%s" % addText # Reputting the categories newtext = pywikibot.replaceCategoryLinks(newtext, categoriesInside, site, True) # Dealing the stars' issue allstars = [] starstext = pywikibot.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() + '\r\n\r\n' allstars.sort() for element in allstars: newtext += '%s\r\n' % element.strip() # Adding the interwiki newtext = pywikibot.replaceLanguageLinks(newtext, interwikiInside, site) else: newtext += u"\n%s" % addText else: newtext = addText + '\n' + 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 always or choice == 'y': try: pass if always: page.put(newtext, summary, minorEdit=False) else: page.put_async(newtext, summary, minorEdit=False) except pywikibot.EditConflict: pywikibot.output(u'Edit conflict! skip!') return (False, False, always) except pywikibot.ServerError: errorCount += 1 if errorCount < 5: pywikibot.output(u'Server Error! Wait..') time.sleep(5) continue else: raise pywikibot.ServerError(u'Fifth Server Error!') except pywikibot.SpamfilterError, e: pywikibot.output( u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url)) return (False, False, always) except pywikibot.PageNotSaved, error: pywikibot.output(u'Error putting page: %s' % error.args) return (False, False, always) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % page.title()) return (False, False, always) else: # Break only if the errors are one after the other... errorCount = 0 return (True, True, always)
def add_text(page=None, addText=None, summary=None, regexSkip=None, regexSkipUrl=None, always=False, up=False, putText=True, oldTextGiven=None, reorderEnabled=True, create=False): # 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. starsList = [ u'bueno', u'bom interwiki', u'cyswllt[ _]erthygl[ _]ddethol', u'dolen[ _]ed', u'destacado', u'destaca[tu]', u'enllaç[ _]ad', u'enllaz[ _]ad', u'leam[ _]vdc', u'legătură[ _]a[bcf]', u'liamm[ _]pub', u'lien[ _]adq', u'lien[ _]ba', u'liên[ _]kết[ _]bài[ _]chất[ _]lượng[ _]tốt', u'liên[ _]kết[ _]chọn[ _]lọc', u'ligam[ _]adq', u'ligoelstara', u'ligoleginda', u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km', u'link[ _]sm', u'linkfa', u'na[ _]lotura', u'nasc[ _]ar', u'tengill[ _][úg]g', u'ua', u'yüm yg', u'רא', u'وصلة مقالة جيدة', u'وصلة مقالة مختارة', ] errorCount = 0 site = pywikibot.getSite() pathWiki = site.family.nicepath(site.lang) site = pywikibot.getSite() 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 # If not up, text put below if not up: newtext = text # Translating the \\n into binary \n addText = addText.replace('\\n', '\n') if (reorderEnabled): # Getting the categories categoriesInside = pywikibot.getCategoryLinks(newtext, site) # Deleting the categories newtext = pywikibot.removeCategoryLinks(newtext, site) # Getting the interwiki interwikiInside = pywikibot.getLanguageLinks(newtext, site) # Removing the interwiki newtext = pywikibot.removeLanguageLinks(newtext, site) # Adding the text newtext += u"\n%s" % addText # Reputting the categories newtext = pywikibot.replaceCategoryLinks(newtext, categoriesInside, site, True) # Dealing the stars' issue allstars = [] starstext = pywikibot.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() + '\r\n\r\n' allstars.sort() for element in allstars: newtext += '%s\r\n' % element.strip() # Adding the interwiki newtext = pywikibot.replaceLanguageLinks(newtext, interwikiInside, site) else: newtext += u"\n%s" % addText else: newtext = addText + '\n' + 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 always or choice == 'y': try: pass if always: page.put(newtext, summary, minorEdit=False) else: page.put_async(newtext, summary, minorEdit=False) except pywikibot.EditConflict: pywikibot.output(u'Edit conflict! skip!') return (False, False, always) except pywikibot.ServerError: errorCount += 1 if errorCount < 5: pywikibot.output(u'Server Error! Wait..') time.sleep(5) continue else: raise pywikibot.ServerError(u'Fifth Server Error!') except pywikibot.SpamfilterError, e: pywikibot.output( u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url)) return (False, False, always) except pywikibot.PageNotSaved, error: pywikibot.output(u'Error putting page: %s' % error.args) return (False, False, always) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % page.title()) return (False, False, always) else: # Break only if the errors are one after the other... errorCount = 0 return (True, True, always)
def add_text(page=None, addText=None, summary=None, regexSkip=None, regexSkipUrl=None, always=False, up=False, putText=True, oldTextGiven=None, reorderEnabled=True, create=False): if not addText: raise NoEnoughData('You have to specify what text you want to add!') 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 = site.nice_get_address(page.title(asUrl=True)) result = re.findall(regexSkipUrl, site.getUrl(url)) if result != []: pywikibot.output( u'''Exception! regex (or word) used with -exceptUrl is in the page. Skip! Match was: %s''' % result) return (False, False, always) if regexSkip is not None: result = re.findall(regexSkip, text) if result != []: pywikibot.output( u'''Exception! regex (or word) used with -except is in the page. Skip! 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 = pywikibot.getCategoryLinks(newtext, site) # Deleting the categories newtext = pywikibot.removeCategoryLinks(newtext, site) # Getting the interwiki interwikiInside = pywikibot.getLanguageLinks(newtext, site) # Removing the interwiki newtext = pywikibot.removeLanguageLinks(newtext, site) # Adding the text newtext += u"%s%s" % (config.line_separator, addText) # Reputting the categories newtext = pywikibot.replaceCategoryLinks(newtext, categoriesInside, site, True) # Dealing the stars' issue allstars = [] starstext = pywikibot.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 = pywikibot.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.inputChoice( u'Do you want to accept these changes?', ['Yes', 'No', 'All', 'open in Browser'], ['y', 'n', 'a', 'b'], 'n') if choice == 'a': always = True elif choice == 'n': return (False, False, always) elif choice == 'b': webbrowser.open( "http://%s%s" % (site.hostname(), site.nice_get_address(page.title(asUrl=True)))) pywikibot.input("Press Enter when finished in browser.") 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.PageNotSaved as error: pywikibot.output(u'Error putting page: %s' % error.args) return (False, False, always) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % page.title()) 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 standardizePageFooter(self, text): """ Makes sure that interwiki links, categories and star templates are put to the correct position and into the right order. This combines the old instances standardizeInterwiki and standardizeCategories The page footer has the following section in that sequence: 1. categories 2. ## TODO: template beyond categories ## 3. additional information depending on local site policy 4. stars templates for featured and good articles 5. interwiki links """ starsList = [ u'bueno', u'bom interwiki', u'cyswllt[ _]erthygl[ _]ddethol', u'dolen[ _]ed', u'destacado', u'destaca[tu]', u'enllaç[ _]ad', u'enllaz[ _]ad', u'leam[ _]vdc', u'legătură[ _]a[bcf]', u'liamm[ _]pub', u'lien[ _]adq', u'lien[ _]ba', u'liên[ _]kết[ _]bài[ _]chất[ _]lượng[ _]tốt', u'liên[ _]kết[ _]chọn[ _]lọc', u'ligam[ _]adq', u'ligazón[ _]a[bd]', u'ligoelstara', u'ligoleginda', u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km', u'link[ _]sm', u'linkfa', u'na[ _]lotura', u'nasc[ _]ar', u'tengill[ _][úg]g', u'ua', u'yüm yg', u'רא', u'وصلة مقالة جيدة', u'وصلة مقالة مختارة', ] categories = None interwikiLinks = None allstars = [] # The PyWikipediaBot is no longer allowed to touch categories on the # German Wikipedia. See # https://de.wikipedia.org/wiki/Hilfe_Diskussion:Personendaten/Archiv/1#Position_der_Personendaten_am_.22Artikelende.22 # ignoring nn-wiki of cause of the comment line above iw section if not self.template and '{{Personendaten' not in text and \ '{{SORTIERUNG' not in text and '{{DEFAULTSORT' not in text and \ self.site.lang not in ('et', 'it', 'bg', 'ru'): categories = pywikibot.getCategoryLinks(text, site=self.site) if not self.talkpage: # and pywikibot.calledModuleName() <> 'interwiki': subpage = False if self.template: loc = None try: tmpl, loc = moved_links[self.site.lang] del tmpl except KeyError: pass if loc is not None and loc in self.title: subpage = True interwikiLinks = pywikibot.getLanguageLinks( text, insite=self.site, template_subpage=subpage) # Removing the interwiki text = pywikibot.removeLanguageLinks(text, site=self.site) # Removing the stars' issue starstext = pywikibot.removeDisabledParts(text) for star in starsList: regex = re.compile('(\{\{(?:template:|)%s\|.*?\}\}[\s]*)' % star, re.I) found = regex.findall(starstext) if found != []: text = regex.sub('', text) allstars += found # Adding categories if categories: ##Sorting categories in alphabetic order. beta test only on Persian Wikipedia, TODO fix bug for sorting #if self.site.language() == 'fa': # categories.sort() ##Taking main cats to top # for name in categories: # if re.search(u"(.+?)\|(.{,1}?)",name.title()) or name.title()==name.title().split(":")[0]+title: # categories.remove(name) # categories.insert(0, name) text = pywikibot.replaceCategoryLinks(text, categories, site=self.site) # Adding stars templates if allstars: text = text.strip() + self.site.family.interwiki_text_separator allstars.sort() for element in allstars: text += '%s%s' % (element.strip(), config.line_separator) pywikibot.log(u'%s' % element.strip()) # Adding the interwiki if interwikiLinks: text = pywikibot.replaceLanguageLinks(text, interwikiLinks, site=self.site, template=self.template, template_subpage=subpage) return text
def run(self): """ Starts the bot. """ # 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 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 ' u'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(nofollow_redirects=True) 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 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(asUrl=True)))) 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': return if choice == 'a': self.acceptall = True if choice == 'y': page.put_async(new_text, self.summary) # choice must be 'N' break if self.acceptall and new_text != original_text: try: page.put(new_text, self.summary) except pywikibot.EditConflict: pywikibot.output(u'Skipping %s because of edit conflict' % (page.title(), )) except pywikibot.SpamfilterError as e: pywikibot.output( u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url)) except pywikibot.PageNotSaved as error: pywikibot.output(u'Error putting page: %s' % (error.args, )) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % (page.title(), ))
def standardizePageFooter(self, text): """ Makes sure that interwiki links, categories and star templates are put to the correct position and into the right order. This combines the old instances standardizeInterwiki and standardizeCategories The page footer has the following section in that sequence: 1. categories 2. ## TODO: template beyond categories ## 3. additional information depending on local site policy 4. stars templates for featured and good articles 5. interwiki links """ starsList = [ u'bueno', u'bom interwiki', u'cyswllt[ _]erthygl[ _]ddethol', u'dolen[ _]ed', u'destacado', u'destaca[tu]', u'enllaç[ _]ad', u'enllaz[ _]ad', u'leam[ _]vdc', u'legătură[ _]a[bcf]', u'liamm[ _]pub', u'lien[ _]adq', u'lien[ _]ba', u'liên[ _]kết[ _]bài[ _]chất[ _]lượng[ _]tốt', u'liên[ _]kết[ _]chọn[ _]lọc', u'ligam[ _]adq', u'ligoelstara', u'ligoleginda', u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km', u'link[ _]sm', u'linkfa', u'na[ _]lotura', u'nasc[ _]ar', u'tengill[ _][úg]g', u'ua', u'yüm yg', u'רא', u'وصلة مقالة جيدة', u'وصلة مقالة مختارة', ] categories = None interwikiLinks = None allstars = [] # The PyWikipediaBot is no longer allowed to touch categories on the # German Wikipedia. See # http://de.wikipedia.org/wiki/Hilfe_Diskussion:Personendaten/Archiv/1#Position_der_Personendaten_am_.22Artikelende.22 # ignoring nn-wiki of cause of the comment line above iw section if not self.template and not '{{Personendaten' in text and \ not '{{SORTIERUNG' in text and not '{{DEFAULTSORT' in text and \ not self.site.lang in ('et', 'it', 'bg', 'ru'): categories = pywikibot.getCategoryLinks(text, site=self.site) if not self.talkpage: # and pywikibot.calledModuleName() <> 'interwiki': subpage = False if self.template: loc = None try: tmpl, loc = moved_links[self.site.lang] del tmpl except KeyError: pass if loc is not None and loc in self.title: subpage = True interwikiLinks = pywikibot.getLanguageLinks( text, insite=self.site, template_subpage=subpage) # Removing the interwiki text = pywikibot.removeLanguageLinks(text, site=self.site) # Removing the stars' issue starstext = pywikibot.removeDisabledParts(text) for star in starsList: regex = re.compile('(\{\{(?:template:|)%s\|.*?\}\}[\s]*)' % star, re.I) found = regex.findall(starstext) if found != []: text = regex.sub('', text) allstars += found # Adding categories if categories: ##Sorting categories in alphabetic order. beta test only on Persian Wikipedia, TODO fix bug for sorting #if self.site.language() == 'fa': # categories.sort() ##Taking main cats to top # for name in categories: # if re.search(u"(.+?)\|(.{,1}?)",name.title()) or name.title()==name.title().split(":")[0]+title: # categories.remove(name) # categories.insert(0, name) text = pywikibot.replaceCategoryLinks(text, categories, site=self.site) # Adding stars templates if allstars: text = text.strip() + self.site.family.interwiki_text_separator allstars.sort() for element in allstars: text += '%s%s' % (element.strip(), config.line_separator) pywikibot.log(u'%s' %element.strip()) # Adding the interwiki if interwikiLinks: text = pywikibot.replaceLanguageLinks(text, interwikiLinks, site=self.site, template=self.template, template_subpage=subpage) return text
def doall(title, titlep): global sitep pywikibot.output("autoCategory: %s, %s" % (title, titlep)) page = wp.Page(title) if not titlep: pagep = page.getLang(pywikibot.Site("en")) else: pagep = wp.Page(titlep) sitep = pagep.site if page.exists(): missingcats = set(page.categories(fromtext=True)) oldtext = page.get() else: missingcats = set() oldtext = "" oldcats = set(missingcats) catsp = list(pagep.categories(fromtext=True)) cats = site.getLang(catsp) for i, catp in enumerate(catsp): if not cats[i]: missingcats.add(wp.Category(catp.title())) if page.namespace() not in wp.conf.nstl: text = pywikibot.replaceCategoryLinks(oldtext, list(missingcats), site) else: return NotImplementedError if (oldcats != missingcats) or (text == ""): pywikibot.output(">>> add untranslated categories") page.put(text or dummytext, u"เพิ่มหมวดหมู่") data = pywikibot.ItemPage.fromPage(page) datap = pywikibot.ItemPage.fromPage(pagep) if (not data.exists()) and (not datap.exists()): importiw(pagep, page) elif not datap.exists(): addItem(data, pagep) elif not data.exists(): addItem(datap, page) elif data.getID() != datap.getID(): return False pool = lthread.ThreadPool(10) if page.isCategory(): doCategory(pool, page, pagep) for i, catp in enumerate(catsp): pywikibot.output(catp.title()) if not lre.pats["maintaincat"].search(catp.title()) and cats[i]: page.add_category([cats[i]]) # doCategory(pool, cats[i], catp) pool.wait_completion() text = lre.pats["dummy"].sub("", page.get()) if page.get() != text: page.put(text, u"ลบข้อความ dummy") return True
def add_text(page=None, addText=None, summary=None, regexSkip=None, regexSkipUrl=None, always=False, up=False, putText=True, oldTextGiven=None, reorderEnabled=True, create=False): if not addText: raise NoEnoughData('You have to specify what text you want to add!') if not summary: summary = i18n.twtranslate(pywikibot.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 site = pywikibot.Site() pathWiki = site.family.nicepath(site.code) 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 = '%s%s' % (pathWiki, page.title(asUrl=True)) result = re.findall(regexSkipUrl, site.getUrl(url)) if result != []: pywikibot.output( u'''Exception! regex (or word) used with -exceptUrl is in the page. Skip! Match was: %s''' % result) return (False, False, always) if regexSkip is not None: result = re.findall(regexSkip, text) if result != []: pywikibot.output( u'''Exception! regex (or word) used with -except is in the page. Skip! 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 = pywikibot.getCategoryLinks(newtext, site) # Deleting the categories newtext = pywikibot.removeCategoryLinks(newtext, site) # Getting the interwiki interwikiInside = pywikibot.getLanguageLinks(newtext, site) # Removing the interwiki newtext = pywikibot.removeLanguageLinks(newtext, site) # Adding the text newtext += u"%s%s" % (config.line_separator, addText) # Reputting the categories newtext = pywikibot.replaceCategoryLinks(newtext, categoriesInside, site, True) # Dealing the stars' issue allstars = [] starstext = pywikibot.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 = pywikibot.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.inputChoice( u'Do you want to accept these changes?', ['Yes', 'No', 'All', 'open in Browser'], ['y', 'n', 'a', 'b'], 'n') if choice == 'a': always = True elif choice == 'n': return (False, False, always) elif choice == 'b': webbrowser.open("http://%s%s" % ( page.site.hostname(), page.site.nice_get_address(page.title()) )) pywikibot.input("Press Enter when finished in browser.") 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.PageNotSaved as error: pywikibot.output(u'Error putting page: %s' % error.args) return (False, False, always) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % page.title()) 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 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 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(nofollow_redirects=True) 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 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': return if choice == 'a': self.acceptall = True if choice == 'y': page.put_async(new_text, self.summary) # choice must be 'N' break if self.acceptall and new_text != original_text: try: page.put(new_text, self.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,)) except pywikibot.LockedPage: pywikibot.output(u'Skipping %s (locked page)' % (page.title(),))
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 ' u'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(),))