def __init__(self, generator, oldImage, newImage=None, summary='', always=False, loose=False): """ Arguments: * generator - A page generator. * oldImage - The title of the old image (without namespace) * newImage - The title of the new image (without namespace), or None if you want to remove the image. """ self.generator = generator self.oldImage = oldImage self.newImage = newImage self.editSummary = summary self.summary = summary self.always = always self.loose = loose # get edit summary message mysite = pywikibot.getSite() if summary: self.editSummary = summary elif self.newImage: self.editSummary = pywikibot.translate(mysite, self.msg_replace) \ % (self.oldImage, self.newImage) else: self.editSummary = pywikibot.translate(mysite, self.msg_remove) \ % self.oldImage
def treat(self): page = pywikibot.Page(self.site, self.your_page) if page.exists(): pywikibot.output( u"\nWikitable on \03{lightpurple}%s\03{default} will be " u"completed with:\n" % self.your_page ) text = page.get() newtext = self.newraw() pywikibot.output(newtext) choice = pywikibot.inputChoice(u"Do you want to add these on wikitable?", ["Yes", "No"], ["y", "N"], "N") text = text[:-3] + newtext summ = pywikibot.translate(self.site, summary_update) if choice == "y": try: page.put(u"".join(text), summ) except: pywikibot.output(u"Impossible to edit. It may be an " u"edit conflict. Skipping...") else: pywikibot.output( u"\nWikitable on \03{lightpurple}%s\03{default} will be " u"created with:\n" % self.your_page ) newtext = self.newtable() + self.newraw() pywikibot.output(newtext) summ = pywikibot.translate(self.site, summary_creation) choice = pywikibot.inputChoice(u"Do you want to accept this page creation?", ["Yes", "No"], ["y", "N"], "N") if choice == "y": try: page.put(newtext, summ) except pywikibot.LockedPage: pywikibot.output(u"Page %s is locked; skipping." % title) except pywikibot.EditConflict: pywikibot.output(u"Skipping %s because of edit conflict" % title) except pywikibot.SpamfilterError, error: pywikibot.output(u"Cannot change %s because of spam " u"blacklist entry %s" % (title, error.url))
def main(): always = False for arg in pywikibot.handleArgs(): if arg == '-always': always = True mysite = pywikibot.Site() # If anything needs to be prepared, you can do it here template_image = pywikibot.translate(pywikibot.Site(), template_to_the_image) template_user = pywikibot.translate(pywikibot.Site(), template_to_the_user) except_text_translated = pywikibot.translate(pywikibot.Site(), except_text) if not (template_image and template_user and except_text_translated): pywikibot.warning(u'This script is not localized for %s site.' % mysite) return generator = pagegenerators.UnusedFilesGenerator() generator = pagegenerators.PreloadingGenerator(generator) for image in generator: if (except_text_translated.encode('utf-8') not in image.getImagePageHtml() and u'http://' not in image.text): pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % image.title()) if template_image in image.text: pywikibot.output(u"%s done already" % image.title(asLink=True)) continue appendtext(image, u"\n\n" + template_image, always) uploader = image.getFileVersionHistory().pop(0)['user'] user = pywikibot.User(mysite, uploader) usertalkpage = user.getUserTalkPage() msg2uploader = template_user % {'title': image.title()} appendtext(usertalkpage, msg2uploader, always)
def reportBadAccount(self, name=None, final=False): #Queue process if name: if globalvar.confirm: answer = pywikibot.inputChoice( u'%s may have an unwanted username, do you want to report ' u'this user?' % name, ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if answer in ['a', 'all']: answer = 'y' globalvar.confirm = False else: answer = 'y' if answer.lower() in ['yes', 'y'] or not globalvar.confirm: showStatus() pywikibot.output( u'%s is possibly an unwanted username. It will be reported.' % name) if hasattr(self, '_BAQueue'): self._BAQueue.append(name) else: self._BAQueue = [name] if len(self._BAQueue) >= globalvar.dumpToLog or final: rep_text = '' # name in queue is max, put detail to report page pywikibot.output("Updating badname accounts to report page...") rep_page = pywikibot.Page(self.site, pywikibot.translate(self.site, report_page)) if rep_page.exists(): text_get = rep_page.get() else: text_get = u'This is a report page for the Bad-username, please translate me. --~~~' pos = 0 # The talk page includes "_" between the two names, in this way i # replace them to " ". for usrna in self._BAQueue: username = pywikibot.url2link(usrna, self.site, self.site) n = re.compile(re.escape(username), re.UNICODE) y = n.search(text_get, pos) if y: pywikibot.output(u'%s is already in the report page.' % username) else: # Adding the log. rep_text += pywikibot.translate(self.site, report_text) % username if self.site.lang == 'it': rep_text = "%s%s}}" % (rep_text, self.bname[username]) com = i18n.twtranslate(self.site, 'welcome-bad_username') if rep_text != '': rep_page.put(text_get + rep_text, comment=com, force=True, minorEdit=True) showStatus(5) pywikibot.output(u'Reported') self.BAQueue = list() else: return True
def __init__(self, generator, dry, always): """ Constructor. Parameters: * generator - The page generator that determines on which pages to work on. * dry - If True, doesn't do any real changes, but only shows what would have been changed. * always - If True, don't prompt for each redirect page. """ self.generator = generator self.dry = dry self.always = always self.lang = pywikibot.getSite().lang # Set the edit summary message self.summary = pywikibot.translate(pywikibot.getSite(), self.msg) self.templates = pywikibot.translate(pywikibot.getSite(), self.taxoboxTemplates) self.templateParameters = pywikibot.translate(pywikibot.getSite(), self.sciNameParameters) # Initialize the cache try: self.cache = pickle.load(file(self.cacheFilename, 'rb')) except: self.cache = {} if not self.lang in self.cache: self.cache[self.lang] = {}
def reportBadAccount(self, name=None, final=False): #Queue process if name: if globalvar.confirm: answer = pywikibot.inputChoice( u'%s may have an unwanted username, do you want to report ' u'this user?' % name, ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if answer in ['a', 'all']: answer = 'y' globalvar.confirm = False else: answer = 'y' if answer.lower() in ['yes', 'y'] or not globalvar.confirm: showStatus() pywikibot.output( u'%s is possibly an unwanted username. It will be reported.' % name) if hasattr(self, '_BAQueue'): self._BAQueue.append(name) else: self._BAQueue = [name] if len(self._BAQueue) >= globalvar.dumpToLog or final: rep_text = '' # name in queue is max, put detail to report page pywikibot.output("Updating badname accounts to report page...") rep_page = pywikibot.Page(self.site, pywikibot.translate(self.site, report_page)) if rep_page.exists(): text_get = rep_page.get() else: text_get = u'This is a report page for the Bad-username, please translate me. --~~~' pos = 0 # The talk page includes "_" between the two names, in this way i # replace them to " ". for usrna in self._BAQueue: username = pywikibot.url2link(usrna, self.site, self.site) n = re.compile(re.escape(username), re.UNICODE) y = n.search(text_get, pos) if y: pywikibot.output(u'%s is already in the report page.' % username) else: # Adding the log. rep_text += pywikibot.translate(self.site, report_text) % username if self.site.code == 'it': rep_text = "%s%s}}" % (rep_text, self.bname[username]) com = i18n.twtranslate(self.site, 'welcome-bad_username') if rep_text != '': rep_page.put(text_get + rep_text, comment=com, force=True, minorEdit=True) showStatus(5) pywikibot.output(u'Reported') self.BAQueue = list() else: return True
def __init__(self, generator, oldImage, newImage=None, summary='', always=False, loose=False): """ Arguments: * generator - A page generator. * oldImage - The title of the old image (without namespace) * newImage - The title of the new image (without namespace), or None if you want to remove the image. """ self.generator = generator self.oldImage = oldImage self.newImage = newImage self.editSummary = summary self.summary = summary self.always = always self.loose = loose # get edit summary message mysite = pywikibot.Site() if summary: self.editSummary = summary elif self.newImage: self.editSummary = pywikibot.translate(mysite, self.msg_replace, fallback=True) \ % (self.oldImage, self.newImage) else: self.editSummary = pywikibot.translate(mysite, self.msg_remove, fallback=True) \ % self.oldImage
def __init__(self, generator, **kwargs): """ - generator : Page generator """ self.availableOptions.update({ 'ignorepdf': False, # boolean 'limit': None, # int, stop after n modified pages 'summary': None, }) super(ReferencesRobot, self).__init__(**kwargs) self.generator = generator self.site = pywikibot.Site() # Check manual = 'mw:Manual:Pywikibot/refLinks' code = None for alt in [self.site.code] + i18n._altlang(self.site.code): if alt in localized_msg: code = alt break if code: manual += '/%s' % code if self.getOption('summary') is None: self.msg = i18n.twtranslate(self.site, 'reflinks-msg', locals()) else: self.msg = self.getOption('summary') self.stopPage = pywikibot.Page(self.site, pywikibot.translate(self.site, stopPage)) local = pywikibot.translate(self.site, badtitles) if local: bad = '(' + globalbadtitles + '|' + local + ')' else: bad = globalbadtitles self.titleBlackList = re.compile(bad, re.I | re.S | re.X) self.norefbot = noreferences.NoReferencesBot(None, verbose=False) self.deduplicator = DuplicateReferences() try: self.stopPageRevId = self.stopPage.latestRevision() except pywikibot.NoPage: pywikibot.output(u'The stop page %s does not exist' % self.stopPage.title(asLink=True)) raise # Regex to grasp content-type meta HTML tag in HTML source self.META_CONTENT = re.compile(r'(?i)<meta[^>]*content\-type[^>]*>') # Extract the encoding from a charset property (from content-type !) self.CHARSET = re.compile(r'(?i)charset\s*=\s*(?P<enc>[^\'";>/]*)') # Extract html title from page self.TITLE = re.compile(r'(?is)(?<=<title>).*?(?=</title>)') # Matches content inside <script>/<style>/HTML comments self.NON_HTML = re.compile( r'(?is)<script[^>]*>.*?</script>|<style[^>]*>.*?</style>|<!--.*?-->|<!\[CDATA\[.*?\]\]>') # Authorized mime types for HTML pages self.MIME = re.compile( r'application/(?:xhtml\+xml|xml)|text/(?:ht|x)ml')
def setSummaryMessage(self, disambPage, new_targets=[], unlink=False, dn=False): # make list of new targets targets = '' for page_title in new_targets: targets += u'[[%s]], ' % page_title # remove last comma targets = targets[:-2] if not targets: targets = i18n.twtranslate(self.mysite, 'solve_disambiguation-unknown-page') # first check whether user has customized the edit comment if (self.mysite.family.name in config.disambiguation_comment and self.mylang in config.disambiguation_comment[self.mysite.family.name]): try: self.comment = pywikibot.translate( self.mysite, config.disambiguation_comment[self.mysite.family.name] ) % (disambPage.title(), targets) # Backwards compatibility, type error probably caused by too # many arguments for format string except TypeError: self.comment = pywikibot.translate( self.mysite, config.disambiguation_comment[self.mysite.family.name] ) % disambPage.title() elif disambPage.isRedirectPage(): # when working on redirects, there's another summary message if unlink and not new_targets: self.comment = i18n.twtranslate( self.mysite, 'solve_disambiguation-redirect-removed', {'from': disambPage.title()} ) elif dn and not new_targets: self.comment = i18n.twtranslate( self.mysite, 'solve_disambiguation-redirect-adding-dn-template', {'from': disambPage.title()} ) else: self.comment = i18n.twtranslate(self.mysite, 'solve_disambiguation-redirect-resolved', {'from': disambPage.title(), 'to': targets}) else: if unlink and not new_targets: self.comment = i18n.twtranslate(self.mysite, 'solve_disambiguation-links-removed', {'from': disambPage.title()}) elif dn and not new_targets: self.comment = i18n.twtranslate(self.mysite, 'solve_disambiguation-adding-dn-template', {'from': disambPage.title()}) else: self.comment = i18n.twtranslate(self.mysite, 'solve_disambiguation-links-resolved', {'from': disambPage.title(), 'to': targets})
def run(self): while True: wait = False now = time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime()) localSandboxTitle = pywikibot.translate(self.site, sandboxTitle) if type(localSandboxTitle) is list: titles = localSandboxTitle else: titles = [localSandboxTitle,] for title in titles: sandboxPage = pywikibot.Page(self.site, title) try: text = sandboxPage.get() translatedContent = pywikibot.translate(self.site, content) translatedMsg = i18n.twtranslate(self.site, 'clean_sandbox-cleaned') subst = 'subst:' in translatedContent if text.strip() == translatedContent.strip(): pywikibot.output(u'The sandbox is still clean, no change necessary.') elif subst and sandboxPage.userName() == self.site.user(): pywikibot.output(u'The sandbox might be clean, no change necessary.') elif text.find(translatedContent.strip()) <> 0 and not subst: sandboxPage.put(translatedContent, translatedMsg) pywikibot.showDiff(text, translatedContent) pywikibot.output(u'Standard content was changed, sandbox cleaned.') else: edit_delta = datetime.datetime.utcnow() - \ pywikibot.Timestamp.fromISOformat(sandboxPage.editTime()) delta = self.getOption('delay_td') - edit_delta #Is the last edit more than 'delay' minutes ago? if delta <= datetime.timedelta(0): sandboxPage.put(translatedContent, translatedMsg) pywikibot.showDiff(text, translatedContent) else: #wait for the rest pywikibot.output(u'Sandbox edited %.1f minutes ago...' % \ (edit_delta.seconds / 60.0)) pywikibot.output(u'Sleeping for %d minutes.' % (delta.seconds/60)) time.sleep(delta.seconds) wait = True except pywikibot.EditConflict: pywikibot.output(u'*** Loading again because of edit conflict.\n') if self.getOption('no_repeat'): pywikibot.output(u'\nDone.') return elif not wait: if self.getOption('hours') < 1.0: pywikibot.output('\nSleeping %s minutes, now %s' % ((self.getOption('hours')*60), now)) else: pywikibot.output('\nSleeping %s hours, now %s' % (self.getOption('hours'), now)) time.sleep(self.getOption('hours') * 60 * 60)
def format_date(self, day, monthName, year): """ return a string with the formated date (e.g. u'31. Dezember 2013') """ l_dateformat = pywikibot.translate(self.site, dateformat, fallback=False) return l_dateformat.format(day=day, monthName=monthName, year=year)
def run(self): for page in self.generator: try: # get the page, and save it using the unmodified text. # whether or not getting a redirect throws an exception # depends on the variable self.touch_redirects. text = page.get() originalText = text for url in weblinkchecker.weblinksIn(text, withoutBracketed=True): filename = url.split('/')[-1] description = pywikibot.translate(pywikibot.getSite(), msg) % url bot = upload.UploadRobot(url, description=description) # TODO: check duplicates #filename = bot.uploadImage() #if filename: # text = text.replace(url, u'[[Image:%s]]' % filename) # # only save if there were changes #if text != originalText: # page.put(text) except pywikibot.NoPage: print "Page %s does not exist?!" % page.title(asLink=True) except pywikibot.IsRedirectPage: print "Page %s is a redirect; skipping." \ % page.title(asLink=True) except pywikibot.LockedPage: print "Page %s is locked?!" % page.title(asLink=True)
def appendtext(page, apptext, always): if page.isRedirectPage(): page = page.getRedirectTarget() if not page.exists(): if page.isTalkPage(): text = u'' else: raise pywikibot.NoPage(u"Page '%s' does not exist" % page.title()) else: text = page.text # Here you can go editing. If you find you do not # want to edit this page, just return oldtext = text text += apptext if text != oldtext: pywikibot.showDiff(oldtext, text) if not always: choice = pywikibot.inputChoice( u'Do you want to accept these changes?', ['Yes', 'No', 'All'], 'yNa', 'N') if choice == 'a': always = True if always or choice == 'y': page.text = text page.save( pywikibot.translate(pywikibot.Site(), comment, fallback=True))
def __init__(self, page, filename, summary, dry, always): self.page = pywikibot.Page( pywikibot.getSite(), page ) self.filename = filename self.summary = summary if not self.summary: self.summary = pywikibot.translate(pywikibot.getSite(), self.msg) pywikibot.setAction( self.summary )
def __init__(self, page, filename, summary, dry, always): self.page = pywikibot.Page(pywikibot.getSite(), page) self.filename = filename self.summary = summary if not self.summary: self.summary = pywikibot.translate(pywikibot.getSite(), self.msg) pywikibot.setAction(self.summary)
def changeCommonscat(self, page=None, oldtemplate=u'', oldcat=u'', newtemplate=u'', newcat=u'', linktitle=u'', description=u''): """ Change the current commonscat template and target. """ if oldcat == '3=S' or linktitle == '3=S': return # additional param on de-wiki, TODO: to be handled if not linktitle and (page.title().lower() in oldcat.lower() or oldcat.lower() in page.title().lower()): linktitle = oldcat if linktitle and newcat != page.title(withNamespace=False): newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}' % oldtemplate, u'{{%s|%s|%s}}' % (newtemplate, newcat, linktitle), page.get()) elif newcat == page.title(withNamespace=False): newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}' % oldtemplate, u'{{%s}}' % newtemplate, page.get()) elif oldcat.strip() != newcat: # strip trailing white space newtext = re.sub(u'(?i)\{\{%s\|?[^{}]*(?:\{\{.*\}\})?\}\}' % oldtemplate, u'{{%s|%s}}' % (newtemplate, newcat), page.get()) else: # nothing left to do return if self.summary: comment = self.summary else: comment = pywikibot.translate(page.site, msg_change, fallback=True) % {'oldcat': oldcat, 'newcat': newcat} self.save(newtext, page, comment)
def __init__(self, **kwargs): super(SandboxBot, self).__init__(**kwargs) if self.getOption('delay') is None: d = min(15, max(5, int(self.getOption('hours') * 60))) self.availableOptions['delay_td'] = datetime.timedelta(minutes=d) else: d = max(5, self.getOption('delay')) self.availableOptions['delay_td'] = datetime.timedelta(minutes=d) self.site = pywikibot.Site() self.site.login() if self.getOption('user'): localSandboxTitle = pywikibot.translate(self.site, user_sandboxTemplate) localSandbox = pywikibot.Page(self.site, localSandboxTitle) content.update(user_content) sandboxTitle[self.site.lang] = [ item.title() for item in localSandbox.getReferences( onlyTemplateInclusion=True) ] if self.site.lang not in user_sandboxTemplate: content[self.site.code] = None pywikibot.output( u'Not properly set-up to run in user namespace!') if (not sandboxTitle.get(self.site.code) and not self.getOption('page') ) or (not content.get(self.site.code) and not self.getOption('text')): pywikibot.output(u'This bot is not configured for the given site ' u'(%s), exiting.' % self.site) sys.exit(0)
def __init__(self): self.site = pywikibot.Site() self.site.login() self.commons = self.site.image_repository() self.localtz = tz.gettz('Europe/Berlin') startdate = datetime(2014, 4, 26, 23, 59, 59, tzinfo=self.localtz) enddate = datetime(2014, 4, 22, 23, 59, 59, tzinfo=self.localtz) self.reportPage = pywikibot.Page(self.site, redlinksPage) l_mainPageTitle = pywikibot.translate(self.site, mainPageTitle, fallback=False) self.mainPage = pywikibot.Page(self.site, l_mainPageTitle) self.mainversions = self.mainPage.getVersionHistory() NumTitle = u'Benutzer:SchirmerPower/Artikelanzahl tgl dewp (Hochrechnung)' NumPage = pywikibot.Page(self.site, NumTitle) self.numberofarticles = dict(re.findall(r'(\d{1,2}\.[\D\s]*?\d{4})\t(\d\.\d{3}\.\d{3})', NumPage.text)) day = startdate while day != enddate: self.snap(day) day -= relativedelta(days=1) #print sump.encode('utf-8') print self.reportPage.text.encode('utf-8')
def __init__(self, site=None, **kwargs): """ Initializer. @kwarg ask: If True, confirm each patrol action @kwarg whitelist: page title for whitelist (optional) @kwarg autopatroluserns: Takes user consent to automatically patrol @kwarg versionchecktime: Check versionchecktime lapse in sec """ self.available_options.update({ 'ask': False, 'whitelist': None, 'versionchecktime': 300, 'autopatroluserns': False }) super().__init__(**kwargs) self.site = site or pywikibot.Site() self.recent_gen = True self.user = None if self.opt.whitelist: self.whitelist_pagename = self.opt.whitelist else: local_whitelist_subpage_name = pywikibot.translate( self.site, self.whitelist_subpage_name, fallback=True) self.whitelist_pagename = '{}:{}/{}'.format( self.site.namespace(2), self.site.username(), local_whitelist_subpage_name) self.whitelist = None self.whitelist_ts = 0 self.whitelist_load_ts = 0 self.highest_rcid = 0 # used to track loops self.last_rcid = 0 self._load_prefix_index_aliases()
def main(*args): """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: list of unicode """ # Generate the question text questions = '\n' questionlist = {} pywikibot.handle_args(*args) site = pywikibot.Site() if (site.lang not in list(templates.keys()) and site.lang not in list(done.keys())): pywikibot.output( '\nScript is not localised for {0}. Terminating program.' ''.format(site)) else: for i, t in enumerate(pywikibot.translate(site.lang, templates)): questions += (u'%s) %s\n' % (i, t)) questionlist[i] = t bot = CleaningBot(questions, questionlist) bot.run()
def run(self): site = pywikibot.getSite() newCat = catlib.Category(pywikibot.Link('Category:' + self.newCatTitle)) # set edit summary message if not self.editSummary: self.editSummary = i18n.twtranslate(site, 'category-replacing',\ {'oldcat':self.oldCat.title(), 'newcat':newCat.title()}) # Copy the category contents to the new category page copied = False oldMovedTalk = None if self.oldCat.exists() and self.moveCatPage: copied = self.oldCat.copyAndKeep( self.newCatTitle, pywikibot.translate(site, cfd_templates), i18n.twtranslate(site, 'category-renamed') ) # Also move the talk page if copied: reason = i18n.twtranslate(site, 'category-was-moved', \ {'newcat':self.newCatTitle, 'title':self.newCatTitle}) oldTalk = self.oldCat.toggleTalkPage() if oldTalk.exists(): newTalkTitle = newCat.toggleTalkPage().title() try: talkMoved = oldTalk.move(newTalkTitle, reason) except (pywikibot.NoPage, pywikibot.PageNotSaved), e: #in order : #Source talk does not exist, or #Target talk already exists pywikibot.output(e.message) else: if talkMoved: oldMovedTalk = oldTalk
def __init__(self, **kwargs): super(SandboxBot, self).__init__(**kwargs) if self.getOption('delay') is None: d = min(15, max(5, int(self.getOption('hours') * 60))) self.availableOptions['delay_td'] = datetime.timedelta(minutes=d) else: d = max(5, self.getOption('delay')) self.availableOptions['delay_td'] = datetime.timedelta(minutes=d) self.site = pywikibot.Site() self.site.login() if self.getOption('user'): localSandboxTitle = pywikibot.translate(self.site, user_sandboxTemplate, fallback=False) localSandbox = pywikibot.Page(self.site, localSandboxTitle) content.update(user_content) sandboxTitle[self.site.lang] = [item.title() for item in localSandbox.getReferences( onlyTemplateInclusion=True)] if self.site.lang not in user_sandboxTemplate: content[self.site.code] = None pywikibot.output( u'Not properly set-up to run in user namespace!') if sandboxTitle.get(self.site.code) is None or content.get( self.site.code) is None: pywikibot.output(u'This bot is not configured for the given site ' u'(%s), exiting.' % self.site) sys.exit(0)
def appendtext(page, apptext): global always if page.isRedirectPage(): page = page.getRedirectTarget().encode("utf-8") if not page.exists(): if page.isTalkPage(): text = u'' else: raise pywikibot.NoPage(u"Page '%s' does not exist" % page.title()) else: text = page.get() # Here you can go editing. If you find you do not # want to edit this page, just return oldtext = text text += apptext if text != oldtext: pywikibot.showDiff(oldtext, text) if not always: choice = pywikibot.inputChoice( u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice == 'a': always = True if always or choice == 'y': page.put(text, pywikibot.translate(pywikibot.getSite(), comment))
def setSummaryMessage(self, disambPage, new_targets=[], unlink=False, dn=False): # TODO: setSummaryMessage() in solve_disambiguation now has parameters # new_targets and unlink. Make use of these here. comment = pywikibot.translate( self.mysite, self.msg) % disambPage.title() pywikibot.setAction(comment)
def createReferenceSection(self, oldText, index, ident='=='): if self.site.language() in noTitleRequired: newSection = u'\n%s\n' % (self.referencesText) else: newSection = u'\n%s %s %s\n%s\n' % ( ident, pywikibot.translate(self.site, referencesSections)[0], ident, self.referencesText) return oldText[:index] + newSection + oldText[index:]
def __init__(self, page, filename, summary, overwrite): self.page = pywikibot.Page( pywikibot.getSite(), page ) self.filename = filename self.summary = summary self.overwrite = overwrite if not self.summary: self.summary = pywikibot.translate(pywikibot.getSite(), self.msg) pywikibot.setAction( self.summary )
def run(self): comment = pywikibot.translate(pywikibot.getSite(), msg) pywikibot.setAction(comment) for page in self.generator: if self.done: break self.treat(page)
def makelogpage(self, queue=None): if queue is None: queue = [] if not globalvar.makeWelcomeLog or len(queue) == 0: return text = u'' logg = pywikibot.translate(self.site, logbook, fallback=False) if not logg: return target = logg + '/' + time.strftime('%Y/%m/%d', time.localtime(time.time())) if self.site.lang == 'it': target = logg + '/' + time.strftime('%d/%m/%Y', time.localtime(time.time())) logPage = pywikibot.Page(self.site, target) if logPage.exists(): text = logPage.get() else: #make new log page showStatus() pywikibot.output( 'Log page is not exist, getting information for page creation') text = pywikibot.translate(self.site, logpage_header) text += u'\n!%s' % self.site.namespace(2) text += u'\n!%s' % capitalize( self.site.mediawiki_message('contribslink')) for result in queue: # Adding the log... (don't take care of the variable's name...). luser = pywikibot.url2link(result.name(), self.site, self.site) text += u'\n{{WLE|user=%s|contribs=%d}}' % (luser, result.editCount()) #update log page. while True: try: logPage.put(text, i18n.twtranslate(self.site, 'welcome-updating')) return True except pywikibot.EditConflict: pywikibot.output(u'An edit conflict has occured. Pausing for ' u'10 seconds before continuing.') time.sleep(10)
def makelogpage(self, queue=None): if queue is None: queue = [] if not globalvar.makeWelcomeLog or len(queue) == 0: return text = u'' logg = pywikibot.translate(self.site, logbook) if not logg: return target = logg + '/' + time.strftime('%Y/%m/%d', time.localtime(time.time())) if self.site.code == 'it': target = logg + '/' + time.strftime('%d/%m/%Y', time.localtime(time.time())) logPage = pywikibot.Page(self.site, target) if logPage.exists(): text = logPage.get() else: #make new log page showStatus() pywikibot.output( 'Log page is not exist, getting information for page creation') text = pywikibot.translate(self.site, logpage_header) text += u'\n!%s' % self.site.namespace(2) text += u'\n!%s' % capitalize( self.site.mediawiki_message('contribslink')) for result in queue: # Adding the log... (don't take care of the variable's name...). luser = pywikibot.url2link(result.name(), self.site, self.site) text += u'\n{{WLE|user=%s|contribs=%d}}' % ( luser, result.editCount()) #update log page. while True: try: logPage.put(text, i18n.twtranslate(self.site, 'welcome-updating')) return True except pywikibot.EditConflict: pywikibot.output(u'An edit conflict has occured. Pausing for ' u'10 seconds before continuing.') time.sleep(10)
def useHashGenerator(self): # http://toolserver.org/~multichill/nowcommons.php?language=it&page=2&filter= lang = self.site.lang num_page = 0 word_to_skip_translated = pywikibot.translate(self.site, word_to_skip) images_processed = list() while 1: url = 'http://toolserver.org/~multichill/nowcommons.php?language=%s&page=%s&filter=' % ( lang, num_page) HTML_text = self.site.getUrl(url, no_hostname=True) reg = r'<[Aa] href="(?P<urllocal>.*?)">(?P<imagelocal>.*?)</[Aa]> +?</td><td>\n\s*?' reg += r'<[Aa] href="(?P<urlcommons>http://commons.wikimedia.org/.*?)">Image:(?P<imagecommons>.*?)</[Aa]> +?</td><td>' regex = re.compile(reg, re.UNICODE) found_something = False change_page = True for x in regex.finditer(HTML_text): found_something = True image_local = x.group('imagelocal') image_commons = x.group('imagecommons') if image_local in images_processed: continue change_page = False images_processed.append(image_local) # Skip images that have something in the title (useful for it.wiki) image_to_skip = False for word in word_to_skip_translated: if word.lower() in image_local.lower(): image_to_skip = True if image_to_skip: continue url_local = x.group('urllocal') url_commons = x.group('urlcommons') pywikibot.output( u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % image_local) pywikibot.output(u'Local: %s\nCommons: %s\n' % (url_local, url_commons)) result1 = webbrowser.open(url_local, 0, 1) result2 = webbrowser.open(url_commons, 0, 1) if image_local.split('Image:')[1] == image_commons: choice = pywikibot.inputChoice( u'The local and the commons images have the same name, continue?', ['Yes', 'No'], ['y', 'N'], 'N') else: choice = pywikibot.inputChoice( u'Are the two images equal?', ['Yes', 'No'], ['y', 'N'], 'N') if choice.lower() in ['y', 'yes']: yield [image_local, image_commons] else: continue # The page is dinamically updated, so we may don't need to change it if change_page: num_page += 1 # If no image found means that there aren't anymore, break. if not found_something: break
def setSummaryMessage(self, disambPage, new_targets=[], unlink=False, dn=False): # TODO: setSummaryMessage() in solve_disambiguation now has parameters # new_targets and unlink. Make use of these here. comment = pywikibot.translate(self.mysite, self.msg) % disambPage.title() pywikibot.setAction(comment)
def createReferenceSection(self, oldText, index, ident='=='): if self.site.language() in noTitleRequired: newSection = u'\n%s\n' % (self.referencesText) else: newSection = u'\n%s %s %s\n%s\n' % (ident, pywikibot.translate( self.site, referencesSections)[0], ident, self.referencesText) return oldText[:index] + newSection + oldText[index:]
def useHashGenerator(self): # http://toolserver.org/~multichill/nowcommons.php?language=it&page=2&filter= lang = self.site.lang num_page = 0 word_to_skip_translated = pywikibot.translate(self.site, word_to_skip) images_processed = list() while 1: url = 'http://toolserver.org/~multichill/nowcommons.php?language=%s&page=%s&filter=' % (lang, num_page) HTML_text = self.site.getUrl(url, no_hostname=True) reg = r'<[Aa] href="(?P<urllocal>.*?)">(?P<imagelocal>.*?)</[Aa]> +?</td><td>\n\s*?' reg += r'<[Aa] href="(?P<urlcommons>http://commons.wikimedia.org/.*?)">Image:(?P<imagecommons>.*?)</[Aa]> +?</td><td>' regex = re.compile(reg, re.UNICODE) found_something = False change_page = True for x in regex.finditer(HTML_text): found_something = True image_local = x.group('imagelocal') image_commons = x.group('imagecommons') if image_local in images_processed: continue change_page = False images_processed.append(image_local) # Skip images that have something in the title (useful for it.wiki) image_to_skip = False for word in word_to_skip_translated: if word.lower() in image_local.lower(): image_to_skip = True if image_to_skip: continue url_local = x.group('urllocal') url_commons = x.group('urlcommons') pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % image_local) pywikibot.output(u'Local: %s\nCommons: %s\n' % (url_local, url_commons)) result1 = webbrowser.open(url_local, 0, 1) result2 = webbrowser.open(url_commons, 0, 1) if image_local.split('Image:')[1] == image_commons: choice = pywikibot.inputChoice( u'The local and the commons images have the same name, continue?', ['Yes', 'No'], ['y', 'N'], 'N') else: choice = pywikibot.inputChoice( u'Are the two images equal?', ['Yes', 'No'], ['y', 'N'], 'N') if choice.lower() in ['y', 'yes']: yield [image_local, image_commons] else: continue # The page is dinamically updated, so we may don't need to change it if change_page: num_page += 1 # If no image found means that there aren't anymore, break. if not found_something: break
def treat(self): page = pywikibot.Page(self.site, self.your_page) if page.exists(): pywikibot.output( u'\nWikitable on \03{lightpurple}%s\03{default} will be ' u'completed with:\n' % self.your_page) text = page.get() newtext = self.newraw() pywikibot.output(newtext) choice = pywikibot.inputChoice( u'Do you want to add these on wikitable?', ['Yes', 'No'], ['y', 'N'], 'N') text = text[:-3] + newtext summ = pywikibot.translate(self.site, summary_update) if choice == 'y': try: page.put(u''.join(text), summ) except: pywikibot.output(u'Impossible to edit. It may be an ' u'edit conflict. Skipping...') else: pywikibot.output( u'\nWikitable on \03{lightpurple}%s\03{default} will be ' u'created with:\n' % self.your_page) newtext = self.newtable() + self.newraw() pywikibot.output(newtext) summ = pywikibot.translate(self.site, summary_creation) choice = pywikibot.inputChoice( u'Do you want to accept this page creation?', ['Yes', 'No'], ['y', 'N'], 'N') if choice == 'y': try: page.put(newtext, summ) except pywikibot.LockedPage: pywikibot.output(u"Page %s is locked; skipping." % title) except pywikibot.EditConflict: pywikibot.output(u'Skipping %s because of edit conflict' % title) except pywikibot.SpamfilterError, error: pywikibot.output(u'Cannot change %s because of spam ' u'blacklist entry %s' % (title, error.url))
def get_cat(self): """Specify the category page.""" if self.getOption('tiny'): self.cat = self.site.page_from_repository( self.tiny_cat_redirect_cat) else: cat_title = pywikibot.translate(self.site, self.cat_redirect_cat) if cat_title: self.cat = pywikibot.Category(pywikibot.Link(cat_title, self.site)) return self.cat is not None
def get_cat(self): """Specify the category page.""" if self.opt.tiny: self.cat = self.site.page_from_repository( self.tiny_cat_redirect_cat) else: cat_title = pywikibot.translate(self.site, self.cat_redirect_cat) if cat_title: self.cat = pywikibot.Category( pywikibot.Link(cat_title, self.site)) return self.cat is not None
def treat(self, page): if page.isRedirectPage(): page = page.getRedirectTarget() page_t = page.title() # Show the title of the page we're working on. # Highlight the title in purple. pywikibot.output(u"\n>>> \03{lightpurple}%s\03{default} <<<" % page_t) #try to see if the title contains turkish diacritics try: text = page.get() except: pywikibot.output(u"An error occurred while getting the page, skipping...") return if text.find(u"{{titlu corect") > -1: # turkish text, we should ignore it, maybe log it pywikibot.output(u"Found title marked as turkish, skipping...") return #transrule = string.maketrans("şţŞŢ", "șțȘȚ") #page_cap = pywikibot.Page(self.site, page_t.translate(transrule)) new_page_t = string.replace(page_t, u'ș', u'ş') new_page_t = string.replace(new_page_t, u'ț', u'ţ') new_page_t = string.replace(new_page_t, u'Ș', u'Ş') new_page_t = string.replace(new_page_t, u'Ț', u'Ţ') page_mod = pywikibot.Page(self.site, new_page_t) if new_page_t == page_t: pywikibot.output(u'%s does not contain diacritics, skipping...\n' % page_mod.title(asLink=True)) elif page_mod.exists() and page_mod.isRedirectPage() and page_mod.getRedirectTarget() == page: pywikibot.output(u'%s already exists, skipping...\n' % page_mod.aslink()) else: pywikibot.output(u'[[%s]] doesn\'t exist' % page_mod.title()) if not self.acceptall: choice = pywikibot.inputChoice( u'Do you want to create a redirect to %s?' % page_t, ['Yes', 'No', 'All', 'Quit'], ['y', 'N', 'a', 'q'], 'N') if choice == 'a': self.acceptall = True elif choice == 'q': self.done = True if self.acceptall or choice == 'y': comment = pywikibot.translate(self.site, msg) % page_t try: page_mod.put(u"#%s [[%s]]" % (self.site.redirect(True), page_t), comment) except Exception as e: pywikibot.output(u"An error occurred, skipping...") print type(e)
def new_month(self, date): pywikibot.output(u'new month, updating template') l_archiveTitlePrefix = pywikibot.translate(self.site, archiveTitlePrefix, fallback=False) l_templateTitle = pywikibot.translate(self.site, templateTitle, fallback=False) l_newMonthComment = pywikibot.translate(self.site, newMonthComment, fallback=False) if date.month == 12: self.nextmonthName = pywikibot.date.monthName(self.site.language(), 1) else: self.nextmonthName = pywikibot.date.monthName(self.site.language(), date.month + 1) global sump #templatePage = pywikibot.Page(self.site, l_templateTitle) if date.month == 12: # end of the year? newYearTable = u'\n\n\n{{|\n|+ \'\'\'{year}\'\'\'\n|}}' sump += newYearTable.format(year=date.year + 1) year = date.year + 1 maxDays = 31 #January else: #check for leap year, getNumberOfDaysInMonth() does not do that year = date.year if self.isleapyear(date.year) and date.month == 1: maxDays = 29 else: maxDays = pywikibot.date.getNumberOfDaysInMonth( date.month + 1) sump = sump[:-1] + u'-\n| ' + self.nextmonthName for i in range(1, maxDays + 1): sump += u'|| [[' + l_archiveTitlePrefix +\ self.format_date(i, self.nextmonthName, year) +\ u'|' + i.__str__() + u']]' sump += u'\n|}'
def get_cat_title(self): """Specify the category title.""" if self.getOption('tiny'): repo = self.site.data_repository() dp = pywikibot.ItemPage(repo, self.tiny_cat_redirect_cat) try: self.cat_title = dp.getSitelink(self.site) except pywikibot.NoPage: self.cat_title = None else: self.cat_title = pywikibot.translate(self.site, self.cat_redirect_cat, fallback=False) return self.cat_title is not None
def workon(page, links): text = page.get() # 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()) for page2 in links: try: target = page2.getRedirectTarget() except (pywikibot.Error, pywikibot.SectionError): continue text = treat(text, page2, target) if text != page.get(): comment = pywikibot.translate(mysite, msg) page.put(text, comment)
def new_month(self): pywikibot.output(u'new month, updating template') l_archiveTitlePrefix = pywikibot.translate(self.site, archiveTitlePrefix, fallback=False) l_templateTitle = pywikibot.translate(self.site, templateTitle, fallback=False) l_newMonthComment = pywikibot.translate(self.site, newMonthComment, fallback=False) templatePage = pywikibot.Page(self.site, l_templateTitle) if time.localtime().tm_mon == 12: # end of the year? newYearTable = u'\n\n\n{{|\n|+ \'\'\'{year}\'\'\'\n|}}' templatePage.text += newYearTable.format(year=self.nextyear) self.year = self.nextyear maxDays = 31 #January else: #check for leap year, getNumberOfDaysInMonth() does not do that if self.isleapyear(self.year) and time.localtime().tm_mon == 1: maxDays = 29 else: maxDays = pywikibot.date.getNumberOfDaysInMonth( time.localtime().tm_mon + 1) if self.format_date(1, self.nextmonthName, self.year) in templatePage.text: #template page is already up to date return templatePage.text = templatePage.text[:-1] + u'-\n| ' + self.nextmonthName for i in range(1, maxDays + 1): templatePage.text += u'|| [[' + l_archiveTitlePrefix +\ self.format_date(i, self.nextmonthName, self.year) +\ u'|' + i.__str__() + u']]' templatePage.text += u'\n|}' templatePage.save(comment=l_newMonthComment, botflag=True, minor=False)
def main(): global always always = False for arg in pywikibot.handleArgs(): if arg == '-always': always = True if arg == '-start': start = True mysite = pywikibot.getSite() # If anything needs to be prepared, you can do it here template_image = pywikibot.translate(pywikibot.getSite(), template_to_the_image) template_user = pywikibot.translate(pywikibot.getSite(), template_to_the_user).encode("utf-8") except_text_translated = pywikibot.translate(pywikibot.getSite(), except_text).encode("utf-8") basicgenerator = pagegenerators.UnusedFilesGenerator() generator = pagegenerators.PreloadingGenerator(basicgenerator) for page in generator: pywikibot.output("\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) if except_text_translated not in page.getImagePageHtml() and \ 'http://' not in page.get(): pywikibot.output(u'\n' + page.title()) if template_image in page.get(): pywikibot.output(u"%s done already" % page.title(asLink=True)) continue appendtext(page, u"\n\n" + template_image) uploader = page.getFileVersionHistory().pop(0)['user'] usertalkname = 'User Talk:%s' % uploader usertalkpage = pywikibot.Page(mysite, usertalkname) msg2uploader = template_user % {'title': page.title()} # msg2uploader = msg2uploader.encode("utf-8") appendtext(usertalkpage, msg2uploader)
def workon(page, links): text = page.get() # 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()) for page2 in links: try: target = page2.getRedirectTarget() except (pywikibot.Error, pywikibot.SectionError): continue text = treat(text, page2, target) if text != page.get(): comment = pywikibot.translate(mysite, msg, fallback=True) page.put(text, comment)
def __init__(self, generator, dry, always): """ Constructor. Parameters: @param generator: The page generator that determines on which pages to work. @type generator: generator. @param dry: If True, doesn't do any real changes, but only shows what would have been changed. @type dry: boolean. """ self.generator = generator self.dry = dry self.always = always # Set the edit summary message self.summary = pywikibot.translate(pywikibot.Site(), self.msg)
def main(): # This factory is responsible for processing command line arguments # that are also used by other scripts and that determine on which pages # to work on. genFactory = pagegenerators.GeneratorFactory() # The generator gives the pages that should be worked upon. gen = None # This temporary array is used to read the page title if one single # page to work on is specified by the arguments. pageTitleParts = [] # If dry is True, doesn't do any real changes, but only show # what would have been changed. dry = False # If always is True, don't confirm changes. always = False # Parse command line arguments for arg in pywikibot.handleArgs(): if arg.startswith("-dry"): dry = True elif arg.startswith("-always"): always = True elif arg.startswith("-all"): genFactory.handleArg('-namespace:0') for tmpl in pywikibot.translate(pywikibot.getSite(), SciNameBot.taxoboxTemplates): genFactory.handleArg('-transcludes:%s' % tmpl) else: # check if a standard argument like # -start:XYZ or -ref:Asdf was given. if not genFactory.handleArg(arg): pageTitleParts.append(arg) if pageTitleParts != []: # We will only work on a single page. pageTitle = ' '.join(pageTitleParts) page = pywikibot.Page(pywikibot.getSite(), pageTitle) gen = iter([page]) if not gen: gen = genFactory.getCombinedGenerator() if gen: # The preloading generator is responsible for downloading multiple # pages from the wiki simultaneously. gen = pagegenerators.PreloadingGenerator(gen) bot = SciNameBot(gen, dry, always) bot.run() else: pywikibot.showHelp()
def __init__(self, site=True, **kwargs): """ Constructor. @kwarg feed: The changes feed to work on (Newpages or Recentchanges) @kwarg ask: If True, confirm each patrol action @kwarg whitelist: page title for whitelist (optional) @kwarg autopatroluserns: Takes user consent to automatically patrol @kwarg versionchecktime: Check versionchecktime lapse in sec """ self.availableOptions.update({ 'ask': False, 'feed': None, 'whitelist': None, 'versionchecktime': 300, 'autopatroluserns': False }) super(PatrolBot, self).__init__(site, **kwargs) self.recent_gen = True self.user = None if self.getOption('whitelist'): self.whitelist_pagename = self.getOption('whitelist') else: local_whitelist_subpage_name = pywikibot.translate( self.site, self.whitelist_subpage_name, fallback=True) self.whitelist_pagename = u'%s:%s/%s' % (self.site.namespace( 2), self.site.username(), local_whitelist_subpage_name) self.whitelist = None self.whitelist_ts = 0 self.whitelist_load_ts = 0 self.highest_rcid = 0 # used to track loops self.last_rcid = 0 self.repeat_start_ts = 0 self.rc_item_counter = 0 # counts how many items have been reviewed self.patrol_counter = 0 # and how many times an action was taken for entry in self.site.siteinfo['specialpagealiases']: if entry['realname'] == 'Prefixindex': self._prefixindex_aliases = { alias.lower() for alias in entry['aliases'] } break else: raise RuntimeError('No alias for "prefixindex"')
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 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 main(*args): """Process command line arguments and run the script.""" start = '!' # Load the default parameters and start for arg in pywikibot.handle_args(): if arg.startswith('-start'): if len(arg) == 6: start = pywikibot.input( u'From what page do you want to start?') else: start = arg[7:] site = pywikibot.Site() comm = pywikibot.translate(site, comment) for pl in site.allpages(start): plname = pl.title() pywikibot.output(u'\nLoading %s...' % plname) try: oldtext = pl.get() except pywikibot.IsRedirectPage: pywikibot.output(u"%s is a redirect!" % plname) continue old = pl.interwiki() new = {} for pl2 in old: new[pl2.site] = pywikibot.Page(pl2) newtext = textlib.replaceLanguageLinks(oldtext, new, site=site) if new: if oldtext != newtext: pywikibot.showDiff(oldtext, newtext) # Submit changes try: pl.put(newtext, comment=comm) except pywikibot.LockedPage: pywikibot.output(u"%s is locked" % plname) continue else: pywikibot.output(u'No changes needed.') continue else: pywikibot.output(u'No interwiki found.') continue
def __init__(self, **kwargs): """ Constructor. @kwarg feed - The changes feed to work on (Newpages or Recentchanges) @kwarg ask - If True, confirm each patrol action @kwarg whitelist - page title for whitelist (optional) @kwarg autopatroluserns - Takes user consent to automatically patrol @kwarg versionchecktime - Check versionchecktime lapse in sec """ self.availableOptions.update({ 'ask': False, 'feed': None, 'whitelist': None, 'versionchecktime': 300, 'autopatroluserns': False }) super(PatrolBot, self).__init__(**kwargs) self.recent_gen = True self.user = None self.site = pywikibot.Site() if self.getOption('whitelist'): self.whitelist_pagename = self.getOption('whitelist') else: local_whitelist_subpage_name = pywikibot.translate( self.site, self.whitelist_subpage_name, fallback=True) self.whitelist_pagename = u'%s:%s/%s' % (self.site.namespace( 2), self.site.username(), local_whitelist_subpage_name) self.whitelist = self.getOption('whitelist') self.whitelist_ts = 0 self.whitelist_load_ts = 0 self.highest_rcid = 0 # used to track loops self.last_rcid = 0 self.repeat_start_ts = 0 self.rc_item_counter = 0 # counts how many items have been reviewed self.patrol_counter = 0 # and how many times an action was taken
def run(self): site = pywikibot.getSite() newCat = catlib.Category(pywikibot.Link('Category:' + self.newCatTitle)) newcat_contents = set(newCat.members()) # set edit summary message if not self.editSummary: self.editSummary = i18n.twtranslate(site, 'category-replacing', { 'oldcat': self.oldCat.title(), 'newcat': newCat.title() }) # Copy the category contents to the new category page copied = False oldMovedTalk = None if self.oldCat.exists() and self.moveCatPage: copied = self.oldCat.copyAndKeep( self.newCatTitle, pywikibot.translate(site, cfd_templates), i18n.twtranslate(site, 'category-renamed')) # Also move the talk page if copied: reason = i18n.twtranslate(site, 'category-was-moved', { 'newcat': self.newCatTitle, 'title': self.newCatTitle }) oldTalk = self.oldCat.toggleTalkPage() if oldTalk.exists(): newTalkTitle = newCat.toggleTalkPage().title() try: talkMoved = oldTalk.move(newTalkTitle, reason) except (pywikibot.NoPage, pywikibot.PageNotSaved), e: #in order : #Source talk does not exist, or #Target talk already exists pywikibot.output(e.message) else: if talkMoved: oldMovedTalk = oldTalk
def _movecat(self): """Private function to move the category page. Do not use this function from outside the class. """ # Some preparing pywikibot.output('Moving text from %s to %s.' % (self.oldcat.title(), self.newcat.title())) authors = ', '.join(self.oldcat.contributingUsers()) template_vars = (self.oldcat.title(), authors) comment = i18n.twtranslate(self.site, 'category-renamed') % template_vars self.newcat.text = self.oldcat.text # Replace stuff REGEX = r"<!--BEGIN CFD TEMPLATE-->.*?<!--END CFD TEMPLATE-->" match = re.compile(REGEX, re.IGNORECASE | re.MULTILINE | re.DOTALL) self.newcat.text = match.sub('', self.newcat.text) site_templates = pywikibot.translate(self.site, cfd_templates) for template_name in site_templates: match = re.compile(r"{{%s.*?}}" % template_name, re.IGNORECASE) self.newcat.text = match.sub('', self.newcat.text) # Remove leading whitespace self.newcat.text = self.newcat.text.lstrip() self.newcat.save(comment)
def main(): """ Main Function """ # Loading the comments global categoryToCheck, project_inserted # always, define a generator to understand if the user sets one, # defining what's genFactory always = False generator = False show = False moveBlockCheck = False protectedpages = False protectType = 'edit' namespace = 0 # To prevent Infinite loops errorCount = 0 # Process global args and prepare generator args parser local_args = pywikibot.handleArgs() genFactory = pagegenerators.GeneratorFactory() # Process local args for arg in local_args: if arg == '-always': always = True elif arg == '-move': moveBlockCheck = True elif arg == '-show': show = True elif arg.startswith('-protectedpages'): protectedpages = True if len(arg) > 15: namespace = int(arg[16:]) elif arg.startswith('-moveprotected'): protectedpages = True protectType = 'move' if len(arg) > 14: namespace = int(arg[15:]) else: genFactory.handleArg(arg) if config.mylang not in project_inserted: pywikibot.output(u"Your project is not supported by this script.\n" u"You have to edit the script and add it!") return site = pywikibot.Site() site.login() if protectedpages: generator = site.protectedpages(namespace=namespace, type=protectType) # Take the right templates to use, the category and the comment TSP = pywikibot.translate(site, templateSemiProtection) TTP = pywikibot.translate(site, templateTotalProtection) TSMP = pywikibot.translate(site, templateSemiMoveProtection) TTMP = pywikibot.translate(site, templateTotalMoveProtection) TNR = pywikibot.translate(site, templateNoRegex) TU = pywikibot.translate(site, templateUnique) categories = pywikibot.translate(site, categoryToCheck) commentUsed = i18n.twtranslate(site, 'blockpageschecker-summary') if not generator: generator = genFactory.getCombinedGenerator() if not generator: generator = list() pywikibot.output(u'Loading categories...') # Define the category if no other generator has been setted for CAT in categories: cat = pywikibot.Category(site, CAT) # Define the generator gen = pagegenerators.CategorizedPageGenerator(cat) for pageCat in gen: generator.append(pageCat) pywikibot.output(u'Categories loaded, start!') # Main Loop preloadingGen = pagegenerators.PreloadingGenerator(generator, step=60) for page in preloadingGen: pagename = page.title(asLink=True) pywikibot.output('Loading %s...' % pagename) try: text = page.text except pywikibot.NoPage: pywikibot.output("%s doesn't exist! Skipping..." % pagename) continue except pywikibot.IsRedirectPage: pywikibot.output("%s is a redirect! Skipping..." % pagename) if show: showQuest(page) continue """ # This check does not work : # PreloadingGenerator cannot set correctly page.editRestriction # (see bug #1949476 ) if not page.canBeEdited(): pywikibot.output("%s is sysop-protected : this account can't edit " "it! Skipping..." % pagename) continue """ restrictions = page.protection() try: editRestr = restrictions['edit'] except KeyError: editRestr = None if not page.canBeEdited(): pywikibot.output(u"%s is protected: " u"this account can't edit it! Skipping..." % pagename) continue # Understand, according to the template in the page, what should be the # protection and compare it with what there really is. TemplateInThePage = understandBlock(text, TTP, TSP, TSMP, TTMP, TU) # Only to see if the text is the same or not... oldtext = text # keep track of the changes for each step (edit then move) changes = -1 if not editRestr: # page is not edit-protected # Deleting the template because the page doesn't need it. if TU: replaceToPerform = u'|'.join(TTP + TSP + TU) else: replaceToPerform = u'|'.join(TTP + TSP) text, changes = re.subn( '<noinclude>(%s)</noinclude>' % replaceToPerform, '', text) if changes == 0: text, changes = re.subn('(%s)' % replaceToPerform, '', text) msg = u'The page is editable for all' if not moveBlockCheck: msg += u', deleting the template..' pywikibot.output(u'%s.' % msg) elif editRestr[0] == 'sysop': # total edit protection if (TemplateInThePage[0] == 'sysop-total' and TTP) or \ (TemplateInThePage[0] == 'unique' and TU): msg = 'The page is protected to the sysop' if not moveBlockCheck: msg += ', skipping...' pywikibot.output(msg) else: pywikibot.output( u'The page is protected to the sysop, but the ' u'template seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[1], text) elif TSP or TU: # implicitely editRestr[0] = 'autoconfirmed', edit-Semi-protection if TemplateInThePage[0] == 'autoconfirmed-total' or \ TemplateInThePage[0] == 'unique': msg = 'The page is editable only for the autoconfirmed users' if not moveBlockCheck: msg += ', skipping...' pywikibot.output(msg) else: pywikibot.output(u'The page is editable only for the ' u'autoconfirmed users, but the template ' u'seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[0], text) if changes == 0: # We tried to fix edit-protection templates, but it did not work. pywikibot.warning('No edit-protection template could be found') if moveBlockCheck and changes > -1: # checking move protection now try: moveRestr = restrictions['move'] except KeyError: moveRestr = False changes = -1 if not moveRestr: pywikibot.output(u'The page is movable for all, deleting the ' u'template...') # Deleting the template because the page doesn't need it. if TU: replaceToPerform = u'|'.join(TSMP + TTMP + TU) else: replaceToPerform = u'|'.join(TSMP + TTMP) text, changes = re.subn( '<noinclude>(%s)</noinclude>' % replaceToPerform, '', text) if changes == 0: text, changes = re.subn('(%s)' % replaceToPerform, '', text) elif moveRestr[0] == 'sysop': # move-total-protection if (TemplateInThePage[0] == 'sysop-move' and TTMP) or \ (TemplateInThePage[0] == 'unique' and TU): pywikibot.output(u'The page is protected from moving to ' u'the sysop, skipping...') if TU: # no changes needed, better to revert the old text. text = oldtext else: pywikibot.output(u'The page is protected from moving to ' u'the sysop, but the template seems not ' u'correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[3], text) elif TSMP or TU: # implicitely moveRestr[0] = 'autoconfirmed', # move-semi-protection if TemplateInThePage[0] == 'autoconfirmed-move' or \ TemplateInThePage[0] == 'unique': pywikibot.output(u'The page is movable only for the ' u'autoconfirmed users, skipping...') if TU: # no changes needed, better to revert the old text. text = oldtext else: pywikibot.output(u'The page is movable only for the ' u'autoconfirmed users, but the template ' u'seems not correct. Fixing...') if TU: text, changes = re.subn(TemplateInThePage[1], TNR[4], text) else: text, changes = re.subn(TemplateInThePage[1], TNR[2], text) if changes == 0: # We tried to fix move-protection templates, but it did not work pywikibot.warning('No move-protection template could be found') if oldtext != text: # Ok, asking if the change has to be performed and do it if yes. pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title()) pywikibot.showDiff(oldtext, text) if not always: choice = pywikibot.inputChoice( u'Do you want to accept these ' u'changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N') if choice == 'a': always = True if always or choice == 'y': while True: try: page.put(text, commentUsed, force=True) except pywikibot.EditConflict: pywikibot.output(u'Edit conflict! skip!') break except pywikibot.ServerError: # Sometimes there is this error that's quite annoying # because can block the whole process for nothing. errorCount += 1 if errorCount < 5: pywikibot.output(u'Server Error! Wait..') time.sleep(3) continue else: # Prevent Infinite Loops raise pywikibot.ServerError(u'Fifth Server Error!') except pywikibot.SpamfilterError as e: pywikibot.output(u'Cannot change %s because of ' u'blacklist entry %s' % (page.title(), e.url)) break except pywikibot.PageNotSaved as error: pywikibot.output(u'Error putting page: %s' % (error.args, )) break except pywikibot.LockedPage: pywikibot.output(u'The page is still protected. ' u'Skipping...') break else: # Break only if the errors are one after the other errorCount = 0 break