Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
    def run(self):
        comment = i18n.twtranslate(self.site, 'noreferences-add-tag')
        pywikibot.setAction(comment)

        for page in self.generator:
            # 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())
            try:
                text = page.get()
            except pywikibot.NoPage:
                pywikibot.output(u"Page %s does not exist?!" %
                                 page.title(asLink=True))
                continue
            except pywikibot.IsRedirectPage:
                pywikibot.output(u"Page %s is a redirect; skipping." %
                                 page.title(asLink=True))
                continue
            except pywikibot.LockedPage:
                pywikibot.output(u"Page %s is locked?!" %
                                 page.title(asLink=True))
                continue
            if pywikibot.getSite().sitename(
            ) == 'wikipedia:en' and page.isIpEdit():
                pywikibot.output(
                    u"Page %s is edited by IP. Possible vandalized" %
                    page.title(asLink=True))
                continue
            if self.lacksReferences(text):
                newText = self.addReferences(text)
                self.save(page, newText)
Ejemplo n.º 3
0
  def run(self):
  
    timeRE = re.compile('(\d\d):(\d\d), (\d\d?) (January|February|March|April|May|June|July|August|September|October|November|December) (\d\d\d\d) \((UTC|GMT)\)')
    userRE = re.compile('\[\[([Uu]ser|[Bb]enutzer|[Gg]ebruiker):([^\|\]]+)[^\]]*\]\]')
    linkRE = re.compile('\[\[([^\|\]:]+)[^\]]*\]\]')
    emptyRE = re.compile('^===[^=]+===\s+^{\{VICs\s+^\}\}\s*', re.MULTILINE)
    #galleryRE = re.compile('^\s*[Ii]mage:([^\|]+)')
    galleryRE = re.compile('^\s*([Ii]mage|[Ff]ile):([^\|]+)')
    viscopeRE = re.compile('^\{\{[vV]I\|(.+)\|[^\|]+\|')
    scopelistRE = re.compile('\*\s*\[\[:[Ii]mage:([^\|\]]+).*\|(.+)\]\]\s*$')

    userNote = {}
    removeCandidates = []
    tagImages = []
    scopeList = []
    numChanges = 0

    pageName = 'Commons:Valued_image_candidates'
    
    #
    # prepare a random sample for COM:VI
    #

    pywikibot.setAction( "preparing a new random sample of four valued images" )

    try:
      connection = MySQLdb.connect(host="commonswiki.labsdb", db="commonswiki_p", read_default_file="~/replica.my.cnf" )
      cursor = connection.cursor() 
      cursor.execute( "select page_title from templatelinks, page where tl_title='VI' and tl_namespace=10 and page_namespace=6 and page_id=tl_from order by RAND() limit 4" )
    except MySQLdb.OperationalError, message: 
      errorMessage = "Error %d:\n%s" % (message[ 0 ], message[ 1 ] ) 
Ejemplo n.º 4
0
 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 )
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
    def run(self):
        comment = i18n.twtranslate(self.site, 'noreferences-add-tag')
        pywikibot.setAction(comment)

        for page in self.generator:
            # 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())
            try:
                text = page.get()
            except pywikibot.NoPage:
                pywikibot.output(u"Page %s does not exist?!"
                                 % page.title(asLink=True))
                continue
            except pywikibot.IsRedirectPage:
                pywikibot.output(u"Page %s is a redirect; skipping."
                                 % page.title(asLink=True))
                continue
            except pywikibot.LockedPage:
                pywikibot.output(u"Page %s is locked?!"
                                 % page.title(asLink=True))
                continue
            if pywikibot.getSite().sitename() == 'wikipedia:en' and page.isIpEdit():
                pywikibot.output(
                    u"Page %s is edited by IP. Possible vandalized" % page.title(asLink=True))
                continue
            if self.lacksReferences(text):
                newText = self.addReferences(text)
                self.save(page, newText)
Ejemplo n.º 7
0
 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 )
Ejemplo n.º 8
0
    def run(self):
        comment = pywikibot.translate(pywikibot.getSite(), msg)
        pywikibot.setAction(comment)

        for page in self.generator:
            if self.done:
                break
            self.treat(page)
Ejemplo n.º 9
0
    def run(self):
        comment = pywikibot.translate(pywikibot.getSite(), msg)
        pywikibot.setAction(comment)

        for page in self.generator:
            if self.done:
                break
            self.treat(page)
Ejemplo n.º 10
0
def main():
    xmlFilename = None
    always = False
    ignorepdf = False
    limit = None
    namespaces = []
    generator = None

    # Process global args and prepare generator args parser
    local_args = pywikibot.handleArgs()
    genFactory = pagegenerators.GeneratorFactory()

    for arg in local_args:
        if arg.startswith('-namespace:'):
            try:
                namespaces.append(int(arg[11:]))
            except ValueError:
                namespaces.append(arg[11:])
        elif arg.startswith('-summary:'):
            pywikibot.setAction(arg[9:])
        elif arg == '-always':
            always = True
        elif arg == '-ignorepdf':
            ignorepdf = True
        elif arg.startswith('-limit:'):
            limit = int(arg[7:])
        elif arg.startswith('-xmlstart'):
            if len(arg) == 9:
                xmlStart = pywikibot.input(
                    u'Please enter the dumped article to start with:')
            else:
                xmlStart = arg[10:]
        elif arg.startswith('-xml'):
            if len(arg) == 4:
                xmlFilename = pywikibot.input(
                    u'Please enter the XML dump\'s filename:')
            else:
                xmlFilename = arg[5:]
        else:
            genFactory.handleArg(arg)

    if xmlFilename:
        try:
            xmlStart
        except NameError:
            xmlStart = None
        generator = XmlDumpPageGenerator(xmlFilename, xmlStart, namespaces)
    if not generator:
        generator = genFactory.getCombinedGenerator()
    if not generator:
        # syntax error, show help text from the top of this file
        pywikibot.showHelp('reflinks')
        return
    generator = pagegenerators.PreloadingGenerator(generator, step=50)
    generator = pagegenerators.RedirectFilterPageGenerator(generator)
    bot = ReferencesRobot(generator, always, limit, ignorepdf)
    bot.run()
Ejemplo n.º 11
0
 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)
Ejemplo n.º 12
0
    def run(self):
        # Set the edit summary message
        pywikibot.setAction(i18n.twtranslate(pywikibot.getSite(),
                                             'djvutext-creating'))

        linkingPage = pywikibot.Page(pywikibot.getSite(), self.index)
        self.prefix = linkingPage.title(withNamespace=False)
        if self.prefix[0:6] == 'Liber:':
            self.prefix = self.prefix[6:]
        pywikibot.output(u"Using prefix %s" % self.prefix)
        gen = self.PagesGenerator()

        site = pywikibot.getSite()
        self.username = config.usernames[site.family.name][site.lang]

        for pageno in gen:
            pywikibot.output("Processing page %d" % pageno)
            self.treat(pageno)
Ejemplo n.º 13
0
def upthisdrug(drug):
	global count
	source = 'https://clincalc.com/DrugStats/' + drug #drug['href']
	dname = drug.split('/')[-1]
	fig, figp, title, cotdata, potdata, soup = drugchart(source)
	if not fig:
		print Fore.MAGENTA, count, drug, "not enough data", Fore.WHITE
		return False
	desc = soup.find('meta', {'name':'description'})['content']
	keywords = soup.find('meta', {'name':'description'})['content'].split(' including:')[-1].strip()
	date = soup.find('span', id='lblLastUpdated').text
	for c in [[ "prescriptions", figp, potdata ], [ "costs", fig, cotdata ]]:
		filename = dname + " {} (DrugStats).svg".format(c[0])
		page=pywikibot.Page(site, u'File:'+filename)
		'''if page.exists() and re.search(re.escape(date), page.get()):
			print Fore.MAGENTA, count, "exists", Fore.WHITE
			if c[0]=='costs':
				return False
			else:
				continue'''
		print Fore.CYAN, count, Fore.GREEN + filename, Fore.WHITE
		dd = defaulttext
		dd += "|description = {{en|1=" + c[0].title() + " chart for ''" + title + "''."
		dd += "\nKeywords: " + keywords
		dd += "\n<br>Chart data: \n<pre style='color:darkgreen;font-family:monospace;margin-left:1em;border:1px darkgreen solid;'>\n " + re.sub('[\[\]]', '', re.sub('],',',\n',str(c[2]))) + "</pre>\n }}"
		dd += "\n|source = " + source
		dd += "\n|author = ClinCalc DrugStats" 
		dd += "\n|date = " + date
		dd += "\n|permission = Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)"
		dd += "}}\n=={{int:license-header}}==\n"
		dd += "{{Cc-by-sa-4.0|1=[https://clincalc.com/DrugStats/ ClinCalc DrugStats]}}\n\n"
		dd += u"[[Category:Charts from DrugStats]]\n[[Category:Images uploaded by Fæ]]"
		#print Fore.YELLOW + dd, Fore.WHITE
		#c[1].show()
		home = os.path.expanduser("~")
		local = home + "/Downloads/TEMP/"+filename
		c[1].write_image(local, format="svg", width=1024, height = 768)
		comment = "[[User_talk:Fæ/DrugStats|DrugStats]] chart for {} {}".format(title, date)
		pywikibot.setAction(comment)
		if len(sys.argv)<2:
			up(local, filename, dd, comment, True)
		remove(local)
	return True
Ejemplo n.º 14
0
 def populateRecentlyPromoted(self, tagImages):
   """
     Adds the newly promoted VIs in [[Commons:Valued images/Recently promoted]]
     
     Arguments :
     tagImages   list constructed in the main program
   """
   pywikibot.setAction(u'Preparing newly promoted [[COM:VI|Valued Images]] for sorting')
   recentPage = pywikibot.Page(self.site, "Commons:Valued images/Recently promoted")
     
   try:
     currentOutputText = recentPage.get(get_redirect=True)
   except pywikibot.NoPage:
     pywikibot.output(u"Page %s does not exist; skipping." % page.aslink())
     return
   except pywikibot.IsRedirectPage:
     pywikibot.output(u"Page %s is a redirect; skipping." % page.aslink())
     return
   except:
     pywikibot.output(page.aslink())
     print "An unhandled exception occured, here's the traceback!"
     traceback.print_exc()
     return
   
   endOfGal = currentOutputText.rfind(u'\n</gallery>')
   if endOfGal < 0:
     pywikibot.output(u"Gallery on page %s is malformed; skipping." % outputPage.aslink())
   else:
     newOutputText = currentOutputText[:endOfGal]
     for image in tagImages:
       newOutputText += u"\n" + image[2]
     newOutputText += currentOutputText[endOfGal:]
       
   if not self.debug:
     try:
       self.put(recentPage, newOutputText)
     except pywikibot.LockedPage:
       pywikibot.output(u"Page %s is locked; skipping." % outputPage.aslink())
     except pywikibot.EditConflict:
       pywikibot.output(u'Skipping %s because of edit conflict' % (outputPage.title()))
     except pywikibot.SpamfilterError, error:
       pywikibot.output(u'Cannot change %s because of spam blacklist entry %s' % (outputPage.title(), error.url))
Ejemplo n.º 15
0
def put_report(report, spage, action, imagecheck=True):
		report = report.decode('utf-8', 'ignore')
		page = pywikibot.Page(site, spage)
		'''try:
				html = page.get()
				if len(html)==len(report):	# There may be some real changes lost, but most of non-length changes are trivial shuffling
						return
				if imagecheck:
						imgs = set(re.findall(r"File:[^\|\]]*\(jpe?g|JPE?G|og[gv]|OG[GV]|svg|SVG|tiff?|TIFF?|gif|GIF)", html))
						rimgs = set(re.findall(r"File:[^\|\]]*\(jpe?g|JPE?G|og[gv]|OG[GV]|svg|SVG|tiff?|TIFF?|gif|GIF)", report))
						if imgs.issubset(rimgs) and rimgs.issubset(imgs):
								return
		except:
				pass'''
		cats = [c.title() for c in page.categories()]
		for cat in cats:
				if not re.search(re.escape(cat), report):
						report += '\n[[' + cat + ']]'
		pywikibot.setAction(action)
		page.put(report)
		return
Ejemplo n.º 16
0
def main():
    summary_commandline, gen, template = None, None, None
    namespaces, PageTitles, exceptions = [], [], []
    encat, newcatfile = '', ''
    autoText, autoTitle = False, False
    recentcat, newcat = False, False
    genFactory = pagegenerators.GeneratorFactory()
    for arg in pywikibot.handleArgs():
        if arg == '-autotitle':
            autoTitle = True
        elif arg == '-autotext':
            autoText = True
        elif arg.startswith('-page'):
            if len(arg) == 5:
                PageTitles.append(pywikibot.input(u'Which page do you want to chage?'))
            else:
                PageTitles.append(arg[6:])
            break
        elif arg.startswith('-except:'):
            exceptions.append(arg[8:])
        elif arg.startswith('-template:'):
            template = arg[10:]
        elif arg.startswith('-facat:'):
            encat = arg[7:].replace(u'Category:', u'').replace(u'category:', u'').replace(u'رده:', u'')
            encat = englishdictionry(u'رده:' + encat, fa_site, en_site).replace(u'Category:', u'').replace(u'category:', u'')
            break
        elif arg.startswith('-encat:'):
            encat = arg[7:].replace(u'Category:', u'').replace(u'category:', u'').replace(u'رده:', u'')
            break
        elif arg.startswith('-newcatfile:'):
            newcatfile = arg[12:]
            break
        elif arg.startswith('-recentcat'):
            arg = arg.replace(':', '')
            if len(arg) == 10:
                genfa = pagegenerators.RecentchangesPageGenerator()
            else:
                genfa = pagegenerators.RecentchangesPageGenerator(number=int(arg[10:]))
            genfa = pagegenerators.DuplicateFilterPageGenerator(genfa)
            genfa = pagegenerators.NamespaceFilterPageGenerator(genfa, [14])
            preloadingGen = pagegenerators.PreloadingGenerator(genfa, 60)
            recentcat = True
            break
        elif arg.startswith('-newcat'):
            arg = arg.replace(':', '')
            if len(arg) == 7:
                genfa = pagegenerators.NewpagesPageGenerator(step=100, namespaces=14)
            else:
                genfa = pagegenerators.NewpagesPageGenerator(step=int(arg[7:]), namespaces=14)
            preloadingGen = pagegenerators.PreloadingGenerator(genfa, 60)
            newcat = True
            break
        elif arg.startswith('-namespace:'):
            namespaces.append(int(arg[11:]))
        elif arg.startswith('-summary:'):
            pywikibot.setAction(arg[9:])
            summary_commandline = True
        else:
            generator = genFactory.handleArg(arg)
            if generator:
                gen = genFactory.getCombinedGenerator(gen)
    if encat != '':
        encatfalist, encatlists = encatlist(encat)
        if encatlists:
            for encat in encatlists:
                encat = englishdictionry(encat, en_site, fa_site)
                if encat:
                    run([encat])
        if encatfalist is not False:
            run(encatfalist)
    if PageTitles:
        pages = [pywikibot.Page(fa_site, PageTitle) for PageTitle in PageTitles]
        gen = iter(pages)
    if recentcat:
        for workpage in preloadingGen:
            workpage = workpage.title()
            cat = pywikibot.Category(fa_site, workpage)
            gent = pagegenerators.CategorizedPageGenerator(cat)
            run(gent)
        pywikibot.stopme()
        sys.exit()
    if newcat:
        for workpage in preloadingGen:
            workpage = workpage.title()
            workpage = englishdictionry(workpage, fa_site, en_site)
            if workpage is not False:
                encatfalist, encatlists = encatlist(workpage)
                if encatlists:
                    for encat in encatlists:
                        encat = englishdictionry(encat, en_site, fa_site)
                        if encat:
                            run([encat])
                if encatfalist is not False:
                    run(encatfalist)
        pywikibot.stopme()
        sys.exit()
    if newcatfile:
        text2 = codecs.open(newcatfile, 'r', 'utf8')
        text = text2.read()
        linken = re.findall(ur'\[\[.*?\]\]', text, re.S)
        if linken:
            for workpage in linken:
                workpage = workpage.split(u'|')[0].replace(u'[[', u'').replace(u']]', u'').strip()
                workpage = englishdictionry(workpage, fa_site, en_site)
                if workpage is not False:
                    encatfalist,encatlists=encatlist(workpage)
                    workpage=englishdictionry(workpage,'fa','en')
                    if encatfalist:
                        run(encatfalist)
        pywikibot.stopme()
        sys.exit()
    if not gen:
        pywikibot.stopme()
        sys.exit()
    if namespaces != []:
        gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces)
    preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber=60)
    run(preloadingGen)
Ejemplo n.º 17
0
		rowtext+="||align=right|{:,}".format(row[2])
		rowtext+="||align=center| "+re.sub(", sysop|sysop, |^sysop$","", row[3])
		if row[4]=="None":
				rowtext+="||align=center|<div style='color:silver'>"+row[4]+"</div>"
		else:
				rowtext+="||align=center|"+row[4]
		rowtext+="||align=right|{:,}".format(row[5])
		areport+=rowtext
areport+="\n|}"

hreport=u'''''Report last updated on {{subst:today}}

Total number of [[#top|users active]] in the last 30 days on Wikimedia Commons with more than 10,000 edits is '''+str(count)+''', there are [[#bots| '''+str(bcount)+''' active bots]], '''+ str(ocount) +''' [[#lostusers|recently inactive users]] and [[#admins|'''+str(acount)+''' administrators]] with low activity over two years.
'''

end=time.time()-start
endm=int(end/60)
ends=end % 60
if endm>0:
		endm=str(endm)+"m "
else:
		endm=""
endline=u"Report completed: "+time.strftime("%a, %d %b %Y %H:%M")+u" ("+endm+"{:0.1f}".format(ends)+"s runtime)."
#print '',"\n\n",endline,"\n\n",''
endline=("\n<small>"+endline+"</small>").encode('utf-8')

site=pywikibot.getSite("commons","commons")
out=pywikibot.Page(site, u"User:F\u00E6/Userlist")
pywikibot.setAction("Update report with {:,} active users".format(count))
out.put(hreport+report+'\n\n'+oreport+'\n\n'+breport+'\n\n'+areport+endline)
Ejemplo n.º 18
0
# encoding: utf-8
# $Id: wispagina.py 11 2007-06-10 15:35:37Z valhallasw $

import sys

import os
os.environ['TZ'] = 'Europe/Amsterdam'

import time, datetime
if time.localtime()[3] == 0 or (len(sys.argv) == 2 and sys.argv[1] == "-force"):
  import pywikibot as wikipedia
  import socket 
  wikipedia.setAction(u'Verwijderlijstupdater van [[Gebruiker:Valhallasw]] ([[Gebruiker:Valhallasw/toolserver/bot|changelog]]) @ %s' % socket.getfqdn())
  
  now = datetime.datetime(*time.localtime()[0:5])
  intwoweeks = now + datetime.timedelta(weeks=2)
  
  pagename = now.strftime("Wikipedia:Te beoordelen pagina's/Toegevoegd %Y%m%d")
  inhoud = now.strftime("{{subst:Te beoordelen pagina's nieuwe dag|dag=%Y%m%d}}") 
 
  P = wikipedia.Page(wikipedia.getSite(u'nl'), pagename)
  if not P.exists():          # als 'ie bestaat doe ik lekker niks ;)
    P.put(inhoud)
    
  mainpage = wikipedia.Page(wikipedia.getSite(u'nl'), "Wikipedia:Te beoordelen pagina's")
  mpInhoud = mainpage.get()
  
  if not P in mainpage.templates():
    mpInhoud = "".join(mpInhoud.split("<!-- {{"+pagename+"}} -->\n"))
    delen = mpInhoud.split("<!-- HIERVOOR -->")
    mpInhoud = delen[0] + "{{"+pagename+"}}\n<!-- HIERVOOR -->"+ delen[1]
Ejemplo n.º 19
0
 def dispatchRecentlyPromoted(self):
   """
     Takes sorted images from [[Commons:Valued images/Recently promoted]] and places them in [[Commons:Valued images by topic]]
     
     Arguments :
   """
   
   # Set the edit summary message
   pywikibot.setAction(u'Adding recently categorized [[COM:VI|valued images]] to the [[:Category:Galleries of valued images|VI galleries]]')
   pywikibot.output(u'Adding recently categorized VIs to the VI galleries')
   
   recentPage = pywikibot.Page(self.site, u'Commons:Valued images/Recently promoted')
   galleryPrefix = u'Commons:Valued images by topic/'
   
   recentOldText = ""
   recentNewText = ""
   
   try:
     recentOldText = recentPage.get(get_redirect=True)
   except pywikibot.NoPage:
     pywikibot.output(u"Page %s does not exist; skipping." % recentPage.aslink())
     return
   except pywikibot.IsRedirectPage:
     pywikibot.output(u"Page %s is a redirect; skipping." % recentPage.aslink())
   
   #The structure recording the needed moves
   moveMap = {}
   
   #Find beginning of the gallery
   inGallery = False
   for line in recentOldText.split('\n'):
     if not inGallery:
       if line == u'<gallery>':
         inGallery=True
         recentNewText += line + '\n'
         continue
       else:
         recentNewText += line + '\n'
     else:
       if line == u'</gallery>':
         inGallery=False
         recentNewText += line + '\n'
         continue
       else:
         #Here we process an image
         firstPipePosition = line.find(u'|')
         fileName = line[0:firstPipePosition]
         caption = line[firstPipePosition + 1:]
         if caption.startswith(u'{{VICbotMove|'):
           #The VI is categorized already
           firstPipe = caption.find(u'|')
           lastPipe = caption.rfind(u'|')
           endOfTemplate = caption.rfind(u'}}')
           scope = caption[firstPipe+1:lastPipe]
           subpage = caption[lastPipe+1:endOfTemplate]
           if subpage not in moveMap.keys():
             moveMap[subpage] = []
           moveMap[subpage].append((fileName, scope))
         else:
           #The VI is not categorized
           recentNewText += line + '\n'
   
   #Add pictures in galleries
   for subpage in moveMap.keys():
     galleryPage = pywikibot.Page(self.site, galleryPrefix + subpage)
     try:
       currentGalleryText = galleryPage.get(get_redirect=True)
     except pywikibot.NoPage:
       pywikibot.output(u'****************************************************')
       pywikibot.output(u"Page %s does not exist; skipping." % galleryPage.aslink())
       pywikibot.output(u"Skipped lines:")
       for pair in moveMap[subpage]:
         pywikibot.output(pair[0] + u'|' + pair[1])
       pywikibot.output(u'****************************************************')
       continue
     except pywikibot.IsRedirectPage:
       pywikibot.output(u'****************************************************')
       pywikibot.output(u"Page %s is a redirect; skipping." % galleryPage.aslink())
       pywikibot.output(u"Skipped lines:")
       for pair in moveMap[subpage]:
         pywikibot.output(pair[0] + u'|' + pair[1])
       pywikibot.output(u'****************************************************')
       continue
     endOfGal = currentGalleryText.rfind(u'\n</gallery>')
     if endOfGal < 0:
       pywikibot.output(u'****************************************************')
       pywikibot.output(u"Gallery on page %s is malformed; skipping." % galleryPage.aslink())
       pywikibot.output(u"Skipped lines:")
       for pair in moveMap[subpage]:
         pywikibot.output(pair[0] + u'|' + pair[1])
       pywikibot.output(u'****************************************************')
       continue
     newGalleryText = currentGalleryText[:endOfGal]
     for pair in moveMap[subpage]:
       newGalleryText += u'\n' + pair[0] + u'|' + pair[1]
     newGalleryText += currentGalleryText[endOfGal:]
     if not self.debug:
       try:
         self.put(galleryPage, newGalleryText)
       except pywikibot.LockedPage:
         pywikibot.output(u"Page %s is locked; skipping." % galleryPage.aslink())
       except pywikibot.EditConflict:
         pywikibot.output(u'Skipping %s because of edit conflict' % (galleryPage.title()))
       except pywikibot.SpamfilterError, error:
         pywikibot.output(u'Cannot change %s because of spam blacklist entry %s' % (galleryPage.title(), error.url))
     pywikibot.output(u"*** %s ***" % galleryPage.title())
     pywikibot.showDiff(currentGalleryText, newGalleryText)
Ejemplo n.º 20
0
def createcat(cat,txt):
		pywikibot.setAction("Create category")
		p=pywikibot.Page(site,"Category:"+cat)
		print Green,"Creating category",cat,White
		p.put(txt)
		return
Ejemplo n.º 21
0
            checkbroken = False
        elif arg.startswith('-exist'):
            checkbroken = False
        elif arg.startswith('-keepparent'):
            removeparent = False
        elif arg.startswith('-all'):
            main = False
        elif not workingcatname:
            workingcatname = arg

    if not workingcatname:
        pywikibot.bot.suggest_help(missing_parameters=['working category'])
        sys.exit(0)

    mysite = pywikibot.Site()
    pywikibot.setAction(
        i18n.twtranslate(mysite, 'makecat-create', {'cat': workingcatname}))
    workingcat = pywikibot.Category(
        mysite, u'%s:%s' % (mysite.namespaces.CATEGORY, workingcatname))
    filename = pywikibot.config.datafilepath(
        'category',
        workingcatname.encode('ascii', 'xmlcharrefreplace') + '_exclude.txt')
    try:
        f = codecs.open(filename, 'r', encoding=mysite.encoding())
        for line in f.readlines():
            # remove trailing newlines and carriage returns
            try:
                while line[-1] in ['\n', '\r']:
                    line = line[:-1]
            except IndexError:
                pass
            pl = pywikibot.Page(mysite, line)
Ejemplo n.º 22
0
    # no writing, just debugging
    #for item in tagImages :
    #	print ("Tag %s with %s" % ( item[0], item[1]) ).encode("utf-8")
    #for key in userNote.keys() :
    #	print userNote[key]
        
    #sys.exit(0)
    


    #
    # Alphabetical scope list (this is alway executed as the list might have been edited manually)
    #
    
    page = pywikibot.Page(self.site, 'Commons:Valued_images_by_scope' )
    pywikibot.setAction("Insert into and resort alphabetical VI list by scope")
    if page.exists() :
      text = page.get(get_redirect=True)
      oldtext = text
      newList = {}

      for entry in scopeList :
        scrubbed = self.scrubscope(entry[1])
        newList[ scrubbed.replace("'","").upper() ] = "*[[:File:%s|%s]]" % ( entry[0], scrubbed ) 

      for line in string.split(text, "\n") :
        match = scopelistRE.search(line)
        if match != None :
          newList[ match.group(2).replace("'","").upper() ] = line

      keys = newList.keys()
Ejemplo n.º 23
0
        elif arg.startswith('-exist'):
            checkbroken = False
        elif arg.startswith('-keepparent'):
            removeparent = False
        elif arg.startswith('-all'):
            main = False
        else:
            workingcatname.append(arg)

    if len(workingcatname) == 0:
        workingcatname = raw_input("Which page to start with? ")
    else:
        workingcatname = ' '.join(workingcatname)
    mysite = pywikibot.getSite()
    workingcatname = unicode(workingcatname, 'utf-8')
    pywikibot.setAction(i18n.twtranslate(mysite, 'makecat-create') + u' ' + workingcatname)
    workingcat = pywikibot.Category(mysite,
                                    u'%s:%s'
                                    % (mysite.category_namespace(),
                                       workingcatname))
    filename = pywikibot.config.datafilepath('category',
                                             workingcatname.encode('ascii', 'xmlcharrefreplace') + '_exclude.txt')
    try:
        f = codecs.open(filename, 'r', encoding=mysite.encoding())
        for line in f.readlines():
            # remove trailing newlines and carriage returns
            try:
                while line[-1] in ['\n', '\r']:
                    line = line[:-1]
            except IndexError:
                pass
Ejemplo n.º 24
0
init()

site = pywikibot.Site('commons', 'commons')

maincat = pywikibot.Category(site, "Category:Charts from DrugStats")
for p in maincat.articles():
    if p.namespace() != 'File:': continue
    html = p.get()
    if len(re.findall("Category:", html)) > 2:
        continue
    drug = html.split("''")[1].split(';')[0]
    subcat = pywikibot.Category(site, "Category:" + drug)
    exists = ''
    if subcat.exists():
        print Fore.GREEN + drug, Fore.WHITE
        #{{category redirect|Paracetamol}}
        schtml = subcat.get()
        if re.search('\{.ategory redirect', schtml):
            redir = schtml.split('ategory redirect|')[1].split('}')[0]
            subcat = pywikibot.Category(site, "Category:" + redir)
            print Fore.CYAN + "-> " + subcat.title(), Fore.WHITE
            drug = redir
    else:
        print Fore.MAGENTA + drug, Fore.WHITE
        exists = " (redlink)"
    if not re.search(re.escape("Category:" + drug), html):
        pywikibot.setAction("Add deduced [[:Category:" + drug + "]]" + exists)
        p.put(html + "\n[[Category:" + drug + "]]")
    else:
        print Fore.RED + "Already categorized", Fore.WHITE
Ejemplo n.º 25
0
    def put(self, title, contents):
        mysite = pywikibot.Site()

        page = pywikibot.Page(mysite, title)
        # Show the title of the page we're working on.
        # Highlight the title in purple.
        pywikibot.output(u">>> \03{lightpurple}%s\03{default} <<<"
                         % page.title())

        if self.summary:
            comment = self.summary
        else:
            comment = pywikibot.translate(mysite, self.msg, fallback=True)

        comment_top = comment + " - " + pywikibot.translate(mysite,
                                                            self.msg_top,
                                                            fallback=True)
        comment_bottom = comment + " - " + pywikibot.translate(mysite,
                                                               self.msg_bottom,
                                                               fallback=True)
        comment_force = "%s *** %s ***" % (comment,
                                           pywikibot.translate(mysite,
                                                               self.msg_force,
                                                               fallback=True))

        # Remove trailing newlines (cause troubles when creating redirects)
        contents = re.sub('^[\r\n]*', '', contents)

        if page.exists():
            if self.nocontent != u'':
                pagecontents = page.get()
                if pagecontents.find(self.nocontent) != -1 or pagecontents.find(self.nocontent.lower()) != -1:
                    pywikibot.output(u'Page has %s so it is skipped' % (self.nocontent))
                    return
            if self.append == "Top":
                pywikibot.output(u"Page %s already exists, appending on top!"
                                     % title)
                contents = contents + page.get()
                comment = comment_top
            elif self.append == "Bottom":
                pywikibot.output(u"Page %s already exists, appending on bottom!"
                                     % title)
                contents = page.get() + contents
                comment = comment_bottom
            elif self.force:
                pywikibot.output(u"Page %s already exists, ***overwriting!"
                                 % title)
                comment = comment_force
            else:
                pywikibot.output(u"Page %s already exists, not adding!" % title)
                return
        else:
            if self.autosummary:
                comment = ''
                pywikibot.setAction('')
        try:
            page.put(contents, comment=comment, minorEdit=self.minor)
        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 as error:
            pywikibot.output(
                u'Cannot change %s because of spam blacklist entry %s'
                % (title, error.url))
Ejemplo n.º 26
0
    def put(self, title, contents):
        mysite = pywikibot.getSite()

        page = pywikibot.Page(mysite, title)
        # Show the title of the page we're working on.
        # Highlight the title in purple.
        pywikibot.output(u">>> \03{lightpurple}%s\03{default} <<<"
                         % page.title())

        if self.summary:
            comment = self.summary
        else:
            comment = pywikibot.translate(mysite, self.msg)

        comment_top = comment + " - " + pywikibot.translate(mysite,
                                                            self.msg_top)
        comment_bottom = comment + " - " + pywikibot.translate(mysite,
                                                               self.msg_bottom)
        comment_force = comment + " *** " + pywikibot.translate(mysite,
                                                                self.msg_force) + " ***"

        # Remove trailing newlines (cause troubles when creating redirects)
        contents = re.sub('^[\r\n]*', '', contents)

        if page.exists():
            if self.append == "Top":
                if appendtops.find(self.nocontents) == -1 and appendtops.find(self.nocontents.lower()) == -1:
                    contents = contents + appendtops
                    pywikibot.output(u"Page %s already exists, appending on top!"
                                     % title)
                else:
                    pywikibot.output(u'Page had %s so it is skipped' % (self.nocontents))
                    return
                contents = contents + page.get()
                comment = comment_top
            elif self.append == "Bottom":
                if appendtops.find(self.nocontents) == -1 and appendtops.find(self.nocontents.lower()) == -1:
                    contents = contents + appendtops
                    pywikibot.output(u"Page %s already exists, appending on bottom!"
                                     % title)
                else:
                    pywikibot.output(u'Page had %s so it is skipped' % (self.nocontents))
                    return
                contents = page.get() + contents
                comment = comment_bottom
            elif self.force:
                pywikibot.output(u"Page %s already exists, ***overwriting!"
                                 % title)
                comment = comment_force
            else:
                pywikibot.output(u"Page %s already exists, not adding!" % title)
                return
        else:
            if self.autosummary:
                comment = ''
                pywikibot.setAction('')

        if self.dry:
            pywikibot.output("*** Dry mode ***\n" + \
                             "\03{lightpurple}title\03{default}: " + title + "\n" + \
                             "\03{lightpurple}contents\03{default}:\n" + contents + "\n" \
                             "\03{lightpurple}comment\03{default}: " + comment + "\n")
            return

        try:
            page.put(contents, comment=comment, minorEdit=self.minor)
        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 as error:
            pywikibot.output(
                u'Cannot change %s because of spam blacklist entry %s'
                % (title, error.url))
Ejemplo n.º 27
0
for searchstring in searchstrings:
	scount += 1
	print Fore.CYAN + "{}".format(scount), Fore.MAGENTA + "Searching", searchstring[0], Fore.WHITE
	pages = site.search(searchcore + searchstring[0], namespaces=6, where="text", get_redirects=False, total=None, content=False)
	count = 0
	for page in pages:
		count += 1
		print Fore.CYAN + "{}-{}".format(scount, count), Fore.GREEN + page.title()[5:-4], Fore.WHITE
		oldhtml = page.get()
		html = re.sub("\{\{PD-USGov\}\}", "{{" + searchstring[1] + "}}", oldhtml)
		escss = re.escape("{{" + searchstring[1] + "}}")
		html = re.sub(re.escape(escss +"[\n\s]*" + escss), "{{" + searchstring[1] + "}}", html)
		if len(html)!=len(oldhtml):
			comments = " ".join([r[3] for r in page.getVersionHistory()])
			if re.search("diffusion", comments):
				print Fore.RED + "Appears to have been previously diffused", Fore.WHITE
				continue
			if len(searchstring)==3 and not re.search('Category:' + searchstring[2], html):
				html += "\n[[Category:" + searchstring[2] + "]]"
			action = "[[User:Fæ/code/PD-USGov]] diffusion '{}' → {}".format(searchstring[0], "{{" + searchstring[1] + "}}")
			pywikibot.setAction(action)
			ploop = 0
			while ploop<100:
				try:
					page.put(html)
					ploop = 100
				except Exception as e:
					ploop += 1
					print Fore.CYAN, ploop, Fore.RED, str(e), Fore.WHITE
					time.sleep(10 + min(170, 10*ploop))
Ejemplo n.º 28
0
def main():
    global protectionLevels
    protectionLevels = ['sysop', 'autoconfirmed', 'none']

    pageName = ''
    summary = ''
    always = False
    doSinglePage = False
    doCategory = False
    protectSubcategories = True
    doRef = False
    doLinks = False
    doImages = False
    fileName = ''
    gen = None
    edit = ''
    move = ''
    defaultProtection = 'sysop'

    # read command line parameters
    for arg in pywikibot.handleArgs():
        if arg == '-always':
            always = True
        elif arg.startswith('-file'):
            if len(arg) == len('-file'):
                fileName = pywikibot.input(
                    u'Enter name of file to protect pages from:')
            else:
                fileName = arg[len('-file:'):]
        elif arg.startswith('-summary'):
            if len(arg) == len('-summary'):
                summary = pywikibot.input(
                    u'Enter a reason for the protection:')
            else:
                summary = arg[len('-summary:'):]
        elif arg.startswith('-cat'):
            doCategory = True
            if len(arg) == len('-cat'):
                pageName = pywikibot.input(
                    u'Enter the category to protect from:')
            else:
                pageName = arg[len('-cat:'):]
        elif arg.startswith('-nosubcats'):
            protectSubcategories = False
        elif arg.startswith('-links'):
            doLinks = True
            if len(arg) == len('-links'):
                pageName = pywikibot.input(u'Enter the page to protect from:')
            else:
                pageName = arg[len('-links:'):]
        elif arg.startswith('-ref'):
            doRef = True
            if len(arg) == len('-ref'):
                pageName = pywikibot.input(u'Enter the page to protect from:')
            else:
                pageName = arg[len('-ref:'):]
        elif arg.startswith('-page'):
            doSinglePage = True
            if len(arg) == len('-page'):
                pageName = pywikibot.input(u'Enter the page to protect:')
            else:
                pageName = arg[len('-page:'):]
        elif arg.startswith('-images'):
            doImages = True
            if len(arg) == len('-images'):
                pageName = pywikibot.input(
                    u'Enter the page with the images to protect:')
            else:
                pageName = arg[len('-images:'):]
        elif arg.startswith('-unprotect'):
            defaultProtection = 'none'
        elif arg.startswith('-edit'):
            edit = arg[len('-edit:'):]
            if edit not in protectionLevels:
                edit = choiceProtectionLevel('edit', defaultProtection)
        elif arg.startswith('-move'):
            move = arg[len('-move:'):]
            if move not in protectionLevels:
                move = choiceProtectionLevel('move', defaultProtection)
        elif arg.startswith('-create'):
            create = arg[len('-create:'):]
            if create not in protectionLevels:
                create = choiceProtectionLevel('create', defaultProtection)

    mysite = pywikibot.getSite()

    if doSinglePage:
        if not summary:
            summary = pywikibot.input(u'Enter a reason for the protection:')
        page = pywikibot.Page(mysite, pageName)
        gen = iter([page])
    elif doCategory:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-category',
                                       {'cat': pageName})
        ns = mysite.category_namespace()
        categoryPage = catlib.Category(mysite, ns + ':' + pageName)
        gen = pagegenerators.CategorizedPageGenerator(
            categoryPage, recurse=protectSubcategories)
    elif doLinks:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-links',
                                       {'page': pageName})
        linksPage = pywikibot.Page(mysite, pageName)
        gen = pagegenerators.LinkedPageGenerator(linksPage)
    elif doRef:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-ref',
                                       {'page': pageName})
        refPage = pywikibot.Page(mysite, pageName)
        gen = pagegenerators.ReferringPageGenerator(refPage)
    elif fileName:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-simple')
        gen = pagegenerators.TextfilePageGenerator(fileName)
    elif doImages:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-images',
                                       {'page': pageName})
        gen = pagegenerators.ImagesPageGenerator(
            pywikibot.Page(mysite, pageName))

    if gen:
        pywikibot.setAction(summary)
        # We are just protecting pages, so we have no need of using a preloading
        # page generator
        # to actually get the text of those pages.
        if not edit:
            edit = defaultProtection
        if not move:
            move = defaultProtection
        bot = ProtectionRobot(gen, summary, always, edit=edit, move=move)
        bot.run()
    else:
        pywikibot.showHelp(u'protect')
Ejemplo n.º 29
0
            checkbroken = False
        elif arg.startswith('-exist'):
            checkbroken = False
        elif arg.startswith('-keepparent'):
            removeparent = False
        elif arg.startswith('-all'):
            main = False
        elif not workingcatname:
            workingcatname = arg

    if not workingcatname:
        pywikibot.bot.suggest_help(missing_parameters=['working category'])
        sys.exit(0)

    mysite = pywikibot.Site()
    pywikibot.setAction(i18n.twtranslate(mysite, 'makecat-create', {'cat': workingcatname}))
    workingcat = pywikibot.Category(mysite,
                                    u'%s:%s'
                                    % (mysite.namespaces.CATEGORY,
                                       workingcatname))
    filename = pywikibot.config.datafilepath(
        'category', workingcatname.encode('ascii', 'xmlcharrefreplace') + '_exclude.txt')
    try:
        f = codecs.open(filename, 'r', encoding=mysite.encoding())
        for line in f.readlines():
            # remove trailing newlines and carriage returns
            try:
                while line[-1] in ['\n', '\r']:
                    line = line[:-1]
            except IndexError:
                pass
Ejemplo n.º 30
0
	COUNT(page_id) AS total
FROM page
INNER JOIN categorylinks AS c ON page_id=c.cl_from AND c.cl_to!="Files_from_Wellcome_Images"
INNER JOIN categorylinks AS cc on page_id=cc.cl_from AND cc.cl_to="Files_from_Wellcome_Images"
WHERE c.cl_to NOT REGEXP "Fæ|Files_from|CC-BY|Files_with|test_"
GROUP BY c.cl_to
ORDER BY COUNT(page_id) DESC
LIMIT 50;
""")

table = "{{flatlist|"
for cat, total in cursor.fetchall():
        table+="\n*{{c|"+re.sub("_"," ",cat)+"}}"+" ({:,})".format(total)
table+="\n}}"

pywikibot.setAction("Update")
page = pywikibot.Page(site, "Commons:Batch_uploading/Wellcome_Images/categories")
page.put(table)

cursor.execute("""SELECT DISTINCT rev_user_text, COUNT(page_title) AS edit_count FROM categorylinks
RIGHT JOIN page ON cl_from = page_id 
LEFT JOIN revision ON page_id = rev_page
WHERE page_namespace=6 AND cl_to = 'Files_from_Wellcome_Images' AND rev_user_text NOT REGEXP '[Bb]ot'
GROUP BY 1
HAVING edit_count>0 
ORDER BY 2 DESC;"""
)

table = []
for userx, count in cursor.fetchall():
	table.append([count, "<abbr title='{} edits to Wellcome Images'>[[User talk:".format(count) + userx + "|" +userx +"]]</abbr>"])
Ejemplo n.º 31
0
# encoding: utf-8
# $Id: wispagina.py 11 2007-06-10 15:35:37Z valhallasw $

import sys

import os

os.environ['TZ'] = 'Europe/Amsterdam'

import time, datetime
if time.localtime()[3] == 0 or (len(sys.argv) == 2
                                and sys.argv[1] == "-force"):
    import pywikibot as wikipedia
    import socket
    wikipedia.setAction(
        u'Beoordelingslijstupdater van [[Project:nlwikibots]] @ %s' %
        socket.getfqdn())

    now = datetime.datetime(*time.localtime()[0:5])
    intwoweeks = now + datetime.timedelta(weeks=2)

    pagename = now.strftime(
        "Wikipedia:Te beoordelen pagina's/Toegevoegd %Y%m%d")
    inhoud = now.strftime(
        "{{subst:Te beoordelen pagina's nieuwe dag|dag=%Y%m%d}}")

    P = wikipedia.Page(wikipedia.getSite(u'nl'), pagename)
    if not P.exists():  # als 'ie bestaat doe ik lekker niks ;)
        P.put(inhoud)

    mainpage = wikipedia.Page(wikipedia.getSite(u'nl'),
Ejemplo n.º 32
0
    def run(self):
        """Run the bot"""
        global destmap, catlist, catmap

        user = self.site.user()
        problems = []
        newredirs = []

        l = time.localtime()
        today = "%04d-%02d-%02d" % l[:3]
        edit_request_page = pywikibot.Page(
            self.site, u"User:%(user)s/category edit requests" % locals())
        datafile = pywikibot.config.datafilepath("%s-catmovebot-data" %
                                                 self.site.dbName())
        try:
            inp = open(datafile, "rb")
            record = cPickle.load(inp)
            inp.close()
        except IOError:
            record = {}
        if record:
            cPickle.dump(record, open(datafile + ".bak", "wb"), -1)

        try:
            template_list = self.site.family.category_redirect_templates[
                self.site.code]
        except KeyError:
            pywikibot.output(u"No redirect templates defined for %s" %
                             self.site.sitename())
            return
        # regex to match soft category redirects
        #  note that any templates containing optional "category:" are
        #  incorrect and will be fixed by the bot
        template_regex = re.compile(
            r"""{{\s*(?:%(prefix)s\s*:\s*)?  # optional "template:"
                     (?:%(template)s)\s*\|   # catredir template name
                     (\s*%(catns)s\s*:\s*)?  # optional "category:"
                     ([^|}]+)                # redirect target cat
                     (?:\|[^|}]*)*}}         # optional arguments 2+, ignored
             """ % {
                'prefix':
                self.site.namespace(10).lower(),
                'template':
                "|".join(item.replace(" ", "[ _]+") for item in template_list),
                'catns':
                self.site.namespace(14)
            }, re.I | re.X)

        # check for hard-redirected categories that are not already marked
        # with an appropriate template
        comment = i18n.twtranslate(self.site.lang, self.redir_comment)
        for page in pagegenerators.PreloadingGenerator(self.site.allpages(
                namespace=14, filterredir=True),
                                                       step=250):
            # generator yields all hard redirect pages in namespace 14
            if page.isCategoryRedirect():
                # this is already a soft-redirect, so skip it (for now)
                continue
            try:
                target = page.getRedirectTarget()
            except pywikibot.CircularRedirect:
                target = page
                problems.append(u"# %s is a self-linked redirect" %
                                page.title(asLink=True, textlink=True))
            except RuntimeError:
                # race condition: someone else removed the redirect while we
                # were checking for it
                continue
            if target.namespace() == 14:
                # this is a hard-redirect to a category page
                newtext = (u"{{%(template)s|%(cat)s}}" % {
                    'cat': target.title(withNamespace=False),
                    'template': template_list[0]
                })
                try:
                    page.put(newtext, comment, minorEdit=True)
                    self.log_text.append(
                        u"* Added {{tl|%s}} to %s" %
                        (template_list[0],
                         page.title(asLink=True, textlink=True)))
                except pywikibot.Error as e:
                    self.log_text.append(
                        u"* Failed to add {{tl|%s}} to %s" %
                        (template_list[0],
                         page.title(asLink=True, textlink=True)))
            else:
                problems.append(u"# %s is a hard redirect to %s" %
                                (page.title(asLink=True, textlink=True),
                                 target.title(asLink=True, textlink=True)))

        pywikibot.output("Done checking hard-redirect category pages.")

        comment = i18n.twtranslate(self.site.lang, self.move_comment)
        counts, destmap, catmap = {}, {}, {}
        catlist, nonemptypages = [], []
        redircat = pywikibot.Category(
            pywikibot.Link(
                self.cat_redirect_cat[self.site.family.name][self.site.code],
                self.site))

        # get a list of all members of the category-redirect category
        catpages = dict((c, None) for c in redircat.subcategories())

        # check the category pages for redirected categories
        pywikibot.output(u"")
        pywikibot.output(u"Checking %s category redirect pages" %
                         len(catpages))
        for cat in catpages:
            cat_title = cat.title(withNamespace=False)
            if "category redirect" in cat_title:
                self.log_text.append(u"* Ignoring %s" %
                                     cat.title(asLink=True, textlink=True))
                continue
            if hasattr(cat, "_catinfo"):
                # skip empty categories that don't return a "categoryinfo" key
                catdata = cat.categoryinfo
                if "size" in catdata and int(catdata['size']):
                    # save those categories that have contents
                    nonemptypages.append(cat)
            if cat_title not in record:
                # make sure every redirect has a record entry
                record[cat_title] = {today: None}
                try:
                    newredirs.append("*# %s -> %s" %
                                     (cat.title(asLink=True, textlink=True),
                                      cat.getCategoryRedirectTarget().title(
                                          asLink=True, textlink=True)))
                except pywikibot.Error:
                    pass
                # do a null edit on cat
                try:
                    cat.put(cat.get(get_redirect=True))
                except:
                    pass

        # delete record entries for non-existent categories
        for cat_name in record.keys():
            if pywikibot.Category(self.site,
                                  self.catprefix + cat_name) not in catpages:
                del record[cat_name]

        pywikibot.output(u"")
        pywikibot.output(u"Moving pages out of %s redirected categories." %
                         len(nonemptypages))

        for cat in pagegenerators.PreloadingGenerator(nonemptypages):
            try:
                if not cat.isCategoryRedirect():
                    self.log_text.append(u"* False positive: %s" %
                                         cat.title(asLink=True, textlink=True))
                    continue
            except pywikibot.Error:
                self.log_text.append(u"* Could not load %s; ignoring" %
                                     cat.title(asLink=True, textlink=True))
                continue
            cat_title = cat.title(withNamespace=False)
            if not self.readyToEdit(cat):
                counts[cat_title] = None
                self.log_text.append(u"* Skipping %s; in cooldown period." %
                                     cat.title(asLink=True, textlink=True))
                continue
            dest = cat.getCategoryRedirectTarget()
            if not dest.exists():
                problems.append("# %s redirects to %s" %
                                (cat.title(asLink=True, textlink=True),
                                 dest.title(asLink=True, textlink=True)))
                # do a null edit on cat to update any special redirect
                # categories this wiki might maintain
                try:
                    cat.put(cat.get(get_redirect=True))
                except:
                    pass
                continue
            if dest.isCategoryRedirect():
                double = dest.getCategoryRedirectTarget()
                if double == dest or double == cat:
                    self.log_text.append(
                        u"* Redirect loop from %s" %
                        dest.title(asLink=True, textlink=True))
                    # do a null edit on cat
                    try:
                        cat.put(cat.get(get_redirect=True))
                    except:
                        pass
                else:
                    self.log_text.append(
                        u"* Fixed double-redirect: %s -> %s -> %s" %
                        (cat.title(asLink=True, textlink=True),
                         dest.title(asLink=True, textlink=True),
                         double.title(asLink=True, textlink=True)))
                    oldtext = cat.get(get_redirect=True)
                    # remove the old redirect from the old text,
                    # leaving behind any non-redirect text
                    oldtext = template_regex.sub("", oldtext)
                    newtext = (u"{{%(redirtemp)s|%(ncat)s}}" % {
                        'redirtemp': template_list[0],
                        'ncat': double.title(withNamespace=False)
                    })
                    newtext = newtext + oldtext.strip()
                    try:
                        cat.put(newtext,
                                i18n.twtranslate(self.site.lang,
                                                 self.dbl_redir_comment),
                                minorEdit=True)
                    except pywikibot.Error as e:
                        self.log_text.append("** Failed: %s" % e)
                continue

            found, moved = self.move_contents(cat_title,
                                              dest.title(withNamespace=False),
                                              editSummary=comment)
            if found is None:
                self.log_text.append(u"* [[:%s%s]]: error in move_contents" %
                                     (self.catprefix, cat_title))
            elif found:
                record[cat_title][today] = found
                self.log_text.append(u"* [[:%s%s]]: %d found, %d moved" %
                                     (self.catprefix, cat_title, found, moved))
            counts[cat_title] = found
            # do a null edit on cat
            try:
                cat.put(cat.get(get_redirect=True))
            except:
                pass
            continue

        cPickle.dump(record, open(datafile, "wb"), -1)

        pywikibot.setAction(
            i18n.twtranslate(self.site.lang, self.maint_comment))
        self.log_text.sort()
        problems.sort()
        newredirs.sort()
        self.log_page.put(u"\n== %i-%02i-%02iT%02i:%02i:%02iZ ==\n" %
                          time.gmtime()[:6] + u"\n".join(self.log_text) +
                          u"\n* New redirects since last report:\n" +
                          u"\n".join(newredirs) + u"\n" +
                          u"\n".join(problems) + u"\n" + self.get_log_text())
        if self.edit_requests:
            edit_request_page.put(
                self.edit_request_text % {
                    'itemlist':
                    u"\n" + u"\n".join((self.edit_request_item % item)
                                       for item in self.edit_requests)
                })
Ejemplo n.º 33
0
 def test_set_action(self):
     """Test deprecated setAction function."""
     pywikibot.setAction('{0}X{0}'.format(self._old_config))
     self.assertOneDeprecation(self.INSTEAD)
     self.assertEqual(pywikibot.config.default_edit_summary,
                      '{0}X{0}'.format(self._old_config))
Ejemplo n.º 34
0
def main():
    summary_commandline, gen, template = None, None, None
    namespaces, PageTitles, exceptions = [], [], []
    encat, newcatfile = "", ""
    autoText, autoTitle = False, False
    recentcat, newcat = False, False
    genFactory = pagegenerators.GeneratorFactory()
    for arg in pywikibot.handleArgs():
        if arg == "-autotitle":
            autoTitle = True
        elif arg == "-autotext":
            autoText = True
        elif arg.startswith("-page"):
            if len(arg) == 5:
                PageTitles.append(pywikibot.input(u"Which page do you want to chage?"))
            else:
                PageTitles.append(arg[6:])
            break
        elif arg.startswith("-except:"):
            exceptions.append(arg[8:])
        elif arg.startswith("-template:"):
            template = arg[10:]
        elif arg.startswith("-facat:"):
            facat = arg.replace(u"Category:", u"").replace(u"category:", u"").replace(u"رده:", u"")
            encat = (
                englishdictionry(u"رده:" + facat[7:], fa_site, en_site)
                .replace(u"Category:", u"")
                .replace(u"category:", u"")
            )
            break
        elif arg.startswith("-encat:"):
            encat = arg[7:].replace(u"Category:", u"").replace(u"category:", u"").replace(u"رده:", u"")
            break
        elif arg.startswith("-newcatfile:"):
            newcatfile = arg[12:]
            break
        elif arg.startswith("-recentcat"):
            arg = arg.replace(":", "")
            if len(arg) == 10:
                genfa = pagegenerators.RecentchangesPageGenerator()
            else:
                genfa = pagegenerators.RecentchangesPageGenerator(number=int(arg[10:]))
            genfa = pagegenerators.DuplicateFilterPageGenerator(genfa)
            genfa = pagegenerators.NamespaceFilterPageGenerator(genfa, [14])
            preloadingGen = pagegenerators.PreloadingGenerator(genfa, 60)
            recentcat = True
            break
        elif arg.startswith("-newcat"):
            arg = arg.replace(":", "")
            if len(arg) == 7:
                genfa = pagegenerators.NewpagesPageGenerator(step=100, namespaces=14)
            else:
                genfa = pagegenerators.NewpagesPageGenerator(step=int(arg[7:]), namespaces=14)
            preloadingGen = pagegenerators.PreloadingGenerator(genfa, 60)
            newcat = True
            break
        elif arg.startswith("-namespace:"):
            namespaces.append(int(arg[11:]))
        elif arg.startswith("-summary:"):
            pywikibot.setAction(arg[9:])
            summary_commandline = True
        else:
            generator = genFactory.handleArg(arg)
            if generator:
                gen = genFactory.getCombinedGenerator(gen)
    if encat != "":
        encatfalist, encatlists = encatlist(encat)
        if encatlists:
            for encat in encatlists:
                encat = englishdictionry(encat, en_site, fa_site)
                if encat:
                    run([encat])
        if encatfalist is not False:
            run(encatfalist)
    if PageTitles:
        pages = [pywikibot.Page(fa_site, PageTitle) for PageTitle in PageTitles]
        gen = iter(pages)
    if recentcat:
        for workpage in preloadingGen:
            workpage = workpage.title()
            cat = pywikibot.Category(fa_site, workpage)
            gent = pagegenerators.CategorizedPageGenerator(cat)
            run(gent)
        pywikibot.stopme()
        sys.exit()
    if newcat:
        for workpage in preloadingGen:
            workpage = workpage.title()
            workpage = englishdictionry(workpage, fa_site, en_site)
            if workpage is not False:
                encatfalist, encatlists = encatlist(workpage)
                if encatlists:
                    for encat in encatlists:
                        encat = englishdictionry(encat, en_site, fa_site)
                        if encat:
                            run([encat])
                if encatfalist is not False:
                    run(encatfalist)
        pywikibot.stopme()
        sys.exit()
    if newcatfile:
        text2 = codecs.open(newcatfile, "r", "utf8")
        text = text2.read()
        linken = re.findall(ur"\[\[.*?\]\]", text, re.S)
        if linken:
            for workpage in linken:
                pywikibot.output(u"\03{lightblue}Working on --- Link " + workpage + u" at th newcatfile\03{default}")
                workpage = workpage.split(u"|")[0].replace(u"[[", u"").replace(u"]]", u"").strip()
                workpage = englishdictionry(workpage, fa_site, en_site)
                if workpage is not False:
                    encatfalist, encatlists = encatlist(workpage)
                    workpage = englishdictionry(workpage, fa_site, en_site)
                    if encatlists:
                        run(encatlists)
                    if encatfalist is not False:
                        run(encatfalist)
        pywikibot.stopme()
        sys.exit()
    if not gen:
        pywikibot.stopme()
        sys.exit()
    if namespaces != []:
        gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces)
    preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber=60)
    run(preloadingGen)
Ejemplo n.º 35
0
 def run(self):
     pywikibot.setAction( self.summary )
     for page in self.generator:
         self.treat(page)
Ejemplo n.º 36
0
	COUNT(page_id) AS total
FROM page
INNER JOIN categorylinks AS c ON page_id=c.cl_from AND c.cl_to!="Files_from_Wellcome_Images"
INNER JOIN categorylinks AS cc on page_id=cc.cl_from AND cc.cl_to="Files_from_Wellcome_Images"
WHERE c.cl_to NOT REGEXP "Fæ|Files_from|CC-BY|Files_with|test_"
GROUP BY c.cl_to
ORDER BY COUNT(page_id) DESC
LIMIT 50;
""")

table = "{{flatlist|"
for cat, total in cursor.fetchall():
    table += "\n*{{c|" + re.sub("_", " ", cat) + "}}" + " ({:,})".format(total)
table += "\n}}"

pywikibot.setAction("Update")
page = pywikibot.Page(site,
                      "Commons:Batch_uploading/Wellcome_Images/categories")
page.put(table)

cursor.execute(
    """SELECT DISTINCT rev_user_text, COUNT(page_title) AS edit_count FROM categorylinks
RIGHT JOIN page ON cl_from = page_id 
LEFT JOIN revision ON page_id = rev_page
WHERE page_namespace=6 AND cl_to = 'Files_from_Wellcome_Images' AND rev_user_text NOT REGEXP '[Bb]ot'
GROUP BY 1
HAVING edit_count>0 
ORDER BY 2 DESC;""")

table = []
for userx, count in cursor.fetchall():
Ejemplo n.º 37
0
    for page in pages:
        count += 1
        print Fore.CYAN + "{}-{}".format(
            scount, count), Fore.GREEN + page.title()[5:-4], Fore.WHITE
        oldhtml = page.get()
        html = re.sub("\{\{PD-USGov\}\}", "{{" + searchstring[1] + "}}",
                      oldhtml)
        escss = re.escape("{{" + searchstring[1] + "}}")
        html = re.sub(re.escape(escss + "[\n\s]*" + escss),
                      "{{" + searchstring[1] + "}}", html)
        if len(html) != len(oldhtml):
            comments = " ".join([r[3] for r in page.getVersionHistory()])
            if re.search("PD-USGov diffusion", comments):
                print Fore.RED + "Appears to have been previously diffused", Fore.WHITE
                continue
            if len(searchstring) == 3 and not re.search(
                    'Category:' + searchstring[2], html):
                html += "\n[[Category:" + searchstring[2] + "]]"
            action = "PD-USGov diffusion matching '{}' → {}".format(
                searchstring[0], "{{" + searchstring[1] + "}}")
            pywikibot.setAction(action)
            ploop = 0
            while ploop < 100:
                try:
                    page.put(html)
                    ploop = 100
                except Exception as e:
                    ploop += 1
                    print Fore.CYAN, ploop, Fore.RED, str(e), Fore.WHITE
                    sleep(10 + min(170, 10 * ploop))
Ejemplo n.º 38
0
    def run(self):
        """Run the bot"""
        global destmap, catlist, catmap

        user = self.site.user()
        problems = []
        newredirs = []

        l = time.localtime()
        today = "%04d-%02d-%02d" % l[:3]
        edit_request_page = pywikibot.Page(
            self.site, u"User:%(user)s/category edit requests" % locals())
        datafile = pywikibot.config.datafilepath("%s-catmovebot-data"
                                                 % self.site.dbName())
        try:
            inp = open(datafile, "rb")
            record = cPickle.load(inp)
            inp.close()
        except IOError:
            record = {}
        if record:
            cPickle.dump(record, open(datafile + ".bak", "wb"), -1)

        try:
            template_list = self.site.family.category_redirect_templates[
                self.site.code]
        except KeyError:
            pywikibot.output(u"No redirect templates defined for %s"
                             % self.site.sitename())
            return
        # regex to match soft category redirects
        #  note that any templates containing optional "category:" are
        #  incorrect and will be fixed by the bot
        template_regex = re.compile(
            r"""{{\s*(?:%(prefix)s\s*:\s*)?  # optional "template:"
                     (?:%(template)s)\s*\|   # catredir template name
                     (\s*%(catns)s\s*:\s*)?  # optional "category:"
                     ([^|}]+)                # redirect target cat
                     (?:\|[^|}]*)*}}         # optional arguments 2+, ignored
             """ % {'prefix': self.site.namespace(10).lower(),
                    'template': "|".join(item.replace(" ", "[ _]+")
                                         for item in template_list),
                    'catns': self.site.namespace(14)},
            re.I | re.X)

        # check for hard-redirected categories that are not already marked
        # with an appropriate template
        comment = i18n.twtranslate(self.site.lang, self.redir_comment)
        for page in pagegenerators.PreloadingGenerator(
                self.site.allpages(namespace=14, filterredir=True), step=250):
            # generator yields all hard redirect pages in namespace 14
            if page.isCategoryRedirect():
                # this is already a soft-redirect, so skip it (for now)
                continue
            try:
                target = page.getRedirectTarget()
            except pywikibot.CircularRedirect:
                target = page
                problems.append(u"# %s is a self-linked redirect"
                                % page.title(asLink=True, textlink=True))
            except RuntimeError:
                # race condition: someone else removed the redirect while we
                # were checking for it
                continue
            if target.namespace() == 14:
                # this is a hard-redirect to a category page
                newtext = (u"{{%(template)s|%(cat)s}}"
                           % {'cat': target.title(withNamespace=False),
                              'template': template_list[0]})
                try:
                    page.put(newtext, comment, minorEdit=True)
                    self.log_text.append(u"* Added {{tl|%s}} to %s"
                                         % (template_list[0],
                                            page.title(asLink=True,
                                                       textlink=True)))
                except pywikibot.Error as e:
                    self.log_text.append(u"* Failed to add {{tl|%s}} to %s"
                                         % (template_list[0],
                                            page.title(asLink=True,
                                                       textlink=True)))
            else:
                problems.append(u"# %s is a hard redirect to %s"
                                % (page.title(asLink=True, textlink=True),
                                   target.title(asLink=True, textlink=True)))

        pywikibot.output("Done checking hard-redirect category pages.")

        comment = i18n.twtranslate(self.site.lang, self.move_comment)
        counts, destmap, catmap = {}, {}, {}
        catlist, nonemptypages = [], []
        redircat = pywikibot.Category(
            pywikibot.Link(self.cat_redirect_cat
                           [self.site.family.name][self.site.code], self.site))

        # get a list of all members of the category-redirect category
        catpages = dict((c, None)
                        for c in self.site.categorymembers(redircat,
                                                           namespaces=[14]))

        # check the category pages for redirected categories
        pywikibot.output(u"")
        pywikibot.output(u"Checking %s category redirect pages"
                         % len(catpages))
        for cat in catpages:
            cat_title = cat.title(withNamespace=False)
            if "category redirect" in cat_title:
                self.log_text.append(u"* Ignoring %s"
                                     % cat.title(asLink=True, textlink=True))
                continue
            if hasattr(cat, "_catinfo"):
                # skip empty categories that don't return a "categoryinfo" key
                catdata = cat.categoryinfo
                if "size" in catdata and int(catdata['size']):
                    # save those categories that have contents
                    nonemptypages.append(cat)
            if cat_title not in record:
                # make sure every redirect has a record entry
                record[cat_title] = {today: None}
                try:
                    newredirs.append("*# %s -> %s"
                                     % (cat.title(asLink=True, textlink=True),
                                        cat.getCategoryRedirectTarget().title(
                                            asLink=True, textlink=True)))
                except pywikibot.Error:
                    pass
                # do a null edit on cat
                try:
                    cat.put(cat.get(get_redirect=True))
                except:
                    pass

        # delete record entries for non-existent categories
        for cat_name in record.keys():
            if pywikibot.Category(self.site,
                                  self.catprefix + cat_name) not in catpages:
                del record[cat_name]

        pywikibot.output(u"")
        pywikibot.output(u"Moving pages out of %s redirected categories."
                         % len(nonemptypages))

        for cat in pagegenerators.PreloadingGenerator(nonemptypages):
            try:
                if not cat.isCategoryRedirect():
                    self.log_text.append(u"* False positive: %s"
                                         % cat.title(asLink=True,
                                                     textlink=True))
                    continue
            except pywikibot.Error:
                self.log_text.append(u"* Could not load %s; ignoring"
                                     % cat.title(asLink=True, textlink=True))
                continue
            cat_title = cat.title(withNamespace=False)
            if not self.readyToEdit(cat):
                counts[cat_title] = None
                self.log_text.append(u"* Skipping %s; in cooldown period."
                                     % cat.title(asLink=True, textlink=True))
                continue
            dest = cat.getCategoryRedirectTarget()
            if not dest.exists():
                problems.append("# %s redirects to %s"
                                % (cat.title(asLink=True, textlink=True),
                                   dest.title(asLink=True, textlink=True)))
                # do a null edit on cat to update any special redirect
                # categories this wiki might maintain
                try:
                    cat.put(cat.get(get_redirect=True))
                except:
                    pass
                continue
            if dest.isCategoryRedirect():
                double = dest.getCategoryRedirectTarget()
                if double == dest or double == cat:
                    self.log_text.append(u"* Redirect loop from %s"
                                         % dest.title(asLink=True,
                                                      textlink=True))
                    # do a null edit on cat
                    try:
                        cat.put(cat.get(get_redirect=True))
                    except:
                        pass
                else:
                    self.log_text.append(
                        u"* Fixed double-redirect: %s -> %s -> %s"
                        % (cat.title(asLink=True, textlink=True),
                           dest.title(asLink=True, textlink=True),
                           double.title(asLink=True, textlink=True)))
                    oldtext = cat.get(get_redirect=True)
                    # remove the old redirect from the old text,
                    # leaving behind any non-redirect text
                    oldtext = template_regex.sub("", oldtext)
                    newtext = (u"{{%(redirtemp)s|%(ncat)s}}"
                               % {'redirtemp': template_list[0],
                                  'ncat': double.title(withNamespace=False)})
                    newtext = newtext + oldtext.strip()
                    try:
                        cat.put(newtext,
                                i18n.twtranslate(self.site.lang,
                                                 self.dbl_redir_comment),
                                minorEdit=True)
                    except pywikibot.Error as e:
                        self.log_text.append("** Failed: %s" % e)
                continue

            found, moved = self.move_contents(cat_title,
                                              dest.title(withNamespace=False),
                                              editSummary=comment)
            if found is None:
                self.log_text.append(
                    u"* [[:%s%s]]: error in move_contents"
                    % (self.catprefix, cat_title))
            elif found:
                record[cat_title][today] = found
                self.log_text.append(
                    u"* [[:%s%s]]: %d found, %d moved"
                    % (self.catprefix, cat_title, found, moved))
            counts[cat_title] = found
            # do a null edit on cat
            try:
                cat.put(cat.get(get_redirect=True))
            except:
                pass
            continue

        cPickle.dump(record, open(datafile, "wb"), -1)

        pywikibot.setAction(i18n.twtranslate(self.site.lang,
                                             self.maint_comment))
        self.log_text.sort()
        problems.sort()
        newredirs.sort()
        self.log_page.put(u"\n== %i-%02i-%02iT%02i:%02i:%02iZ ==\n"
                          % time.gmtime()[:6]
                          + u"\n".join(self.log_text)
                          + u"\n* New redirects since last report:\n"
                          + u"\n".join(newredirs)
                          + u"\n" + u"\n".join(problems)
                          + u"\n" + self.get_log_text())
        if self.edit_requests:
            edit_request_page.put(self.edit_request_text
                                  % {'itemlist': u"\n" + u"\n".join(
                                      (self.edit_request_item % item)
                                      for item in self.edit_requests)})
Ejemplo n.º 39
0
    def featuredWithInterwiki(self, fromsite, template_on_top, task,
                              quiet, dry=False):
        tosite = self.site
        if not fromsite.lang in self.cache:
            self.cache[fromsite.lang] = {}
        if not tosite.lang in self.cache[fromsite.lang]:
            self.cache[fromsite.lang][tosite.lang] = {}
        cc = self.cache[fromsite.lang][tosite.lang]
        if self.getOption('nocache') is True or \
           fromsite.code in self.getOption('nocache'):
            cc = {}
        templatelist = self.getTemplateList(tosite.code, task)
        findtemplate = '(' + '|'.join(templatelist) + ')'
        re_Link_FA = re.compile(ur"\{\{%s\|%s\}\}"
                                % (findtemplate.replace(u' ', u'[ _]'),
                                   fromsite.code), re.IGNORECASE)
        gen = self.featuredArticles(fromsite, task, cc)
        gen = PreloadingGenerator(gen)
        pairs = []
        for a in gen:
            if a.isRedirectPage():
                a = a.getRedirectTarget()

            if not a.exists():
                pywikibot.output(u"source page doesn't exist: %s"
                                 % a.title())
                continue

            atrans = self.findTranslated(a, tosite, quiet)
            if not atrans:
                continue

            if task != 'former':
                text = atrans.get()
                m = re_Link_FA.search(text)
                if m:
                    pywikibot.output(u"(already done)")
                else:
                    # insert just before interwiki
                    if (not interactive or
                        pywikibot.input(
                            u'Connecting %s -> %s. Proceed? [Y/N]'
                            % (a.title(), atrans.title())) in ['Y', 'y']):
                        site = pywikibot.getSite()
                        comment = pywikibot.setAction(
                            i18n.twtranslate(site, 'featured-' + task,
                                             {'page': unicode(a)}))

                        # Moving {{Link FA|xx}} to top of interwikis
                        if template_on_top:
                            # Getting the interwiki
                            iw = pywikibot.getLanguageLinks(text, site)
                            # Removing the interwiki
                            text = pywikibot.removeLanguageLinks(text, site)
                            text += u"\r\n{{%s|%s}}\r\n" % (templatelist[0],
                                                            fromsite.code)
                            # Adding the interwiki
                            text = pywikibot.replaceLanguageLinks(text,
                                                                  iw, site)

                        # Placing {{Link FA|xx}} right next to
                        # corresponding interwiki
                        else:
                            text = (text[:m.end()] +
                                    (u" {{%s|%s}}" % (templatelist[0],
                                                      fromsite.code)) +
                                    text[m.end():])
                        if not dry:
                            try:
                                atrans.put(text, comment)
                            except pywikibot.LockedPage:
                                pywikibot.output(u'Page %s is locked!'
                                                 % atrans.title())
                            except pywikibot.PageNotSaved, e:
                                pywikibot.output(u"Page not saved")
                cc[a.title()] = atrans.title()
            else:
                text = atrans.get()
                m = re_Link_FA.search(text)
                if m:
                    # insert just before interwiki
                    if (not interactive or
                        pywikibot.input(
                            u'Connecting %s -> %s. Proceed? [Y/N]'
                            % (a.title(), atrans.title())) in ['Y', 'y']):
                        site = pywikibot.getSite()
                        comment = pywikibot.setAction(
                            i18n.twtranslate(site, 'featured-former',
                                             {'page': unicode(a)}))
                        text = re.sub(re_Link_FA, '', text)
                        if not dry:
                            try:
                                atrans.put(text, comment)
                            except pywikibot.LockedPage:
                                pywikibot.output(u'Page %s is locked!'
                                                 % atrans.title())
                            except pywikibot.PageNotSaved, e:
                                pywikibot.output(u"Page not saved")
                else:
Ejemplo n.º 40
0
breport+="\n|}"
oreport+="\n|}"
areport = ""

hreport=u'''{{User:Revibot/list}}
Report last updated on {{REVISIONYEAR}}-{{REVISIONMONTH}}-{{REVISIONDAY}}

Total number of [[#top|users active]] in the last 30 days on Korean Wikipedia with more than 10,000 edits is ''' + "{:,d}".format(count) + ''', there are [[#bots|''' + str(bcount) + ''' active bots]] and ''' + str(ocount) +''' [[#lostusers|recently inactive users]].<!--and [[#admins|? administrators]] with low activity over two years.-->
'''


end=time.time()-start
endh=int(end/3600)
if endh>0:
	endh = str(endh) + " h "
else:
	endh = ""
endm=int(end/60) % 60
ends=end % 60
if endm>0:
		endm=str(endm)+" m "
else:
		endm=""
endline=u"Report completed: "+time.strftime("%a, %d %b %Y %H:%M")+u" ("+endh + endm+"{:0.1f}".format(ends)+" s runtime)."
endline += "\n\nStatus: "+status
endline=("\n<small>"+endline+"</small>").encode('utf-8')

site=pywikibot.Site()
out=pywikibot.Page(site, u"User:Revibot/Userlist")
pywikibot.setAction("Update report with {:,} active users".format(count))
out.put(hreport+report+'\n\n'+oreport+'\n\n'+breport+'\n\n'+areport+endline)
Ejemplo n.º 41
0
def createcat(cat, txt):
    pywikibot.setAction("Create category")
    p = pywikibot.Page(site, "Category:" + cat)
    print Green, "Creating category", cat, White
    p.put(txt)
    return
Ejemplo n.º 42
0
            elif found:
                record[cat_title][today] = found
                self.log_text.append(
                    u"* [[:%s%s]]: %d found, %d moved"
                    % (self.catprefix, cat_title, found, moved))
            counts[cat_title] = found
            # do a null edit on cat
            try:
                cat.put(cat.get(get_redirect=True))
            except:
                pass
            continue

        cPickle.dump(record, open(datafile, "wb"), -1)

        pywikibot.setAction(i18n.twtranslate(self.site.lang,
                                             self.maint_comment))
        self.log_text.sort()
        problems.sort()
        newredirs.sort()
        self.log_page.put(u"\n==%i-%02i-%02iT%02i:%02i:%02iZ==\n"
                            % time.gmtime()[:6]
                          + u"\n".join(self.log_text)
                          + u"\n* New redirects since last report:\n"
                          + u"\n".join(newredirs)
                          + u"\n" + u"\n".join(problems)
                          + u"\n" + self.get_log_text())
        if self.edit_requests:
            edit_request_page.put(self.edit_request_text
                                 % {'itemlist':
                                    u"\n" + u"\n".join(
                                        (self.edit_request_item % item)
Ejemplo n.º 43
0
    def put(self, title, contents):
        mysite = pywikibot.getSite()

        page = pywikibot.Page(mysite, title)
        # Show the title of the page we're working on.
        # Highlight the title in purple.
        pywikibot.output(u">>> \03{lightpurple}%s\03{default} <<<" %
                         page.title())

        if self.summary:
            comment = self.summary
        else:
            comment = i18n.twtranslate(mysite, 'pagefromfile-msg')

        comment_top = comment + " - " + i18n.twtranslate(
            mysite, 'pagefromfile-msg_top')
        comment_bottom = comment + " - " + i18n.twtranslate(
            mysite, 'pagefromfile-msg_bottom')
        comment_force = "%s *** %s ***" % (
            comment, i18n.twtranslate(mysite, 'pagefromfile-msg_force'))

        # Remove trailing newlines (cause troubles when creating redirects)
        contents = re.sub('^[\r\n]*', '', contents)

        if page.exists():
            if self.nocontent != u'':
                pagecontents = page.get()
                if pagecontents.find(
                        self.nocontent) != -1 or pagecontents.find(
                            self.nocontent.lower()) != -1:
                    pywikibot.output(u'Page has %s so it is skipped' %
                                     (self.nocontent))
                    return
            if self.append == "Top":
                pywikibot.output(u"Page %s already exists, appending on top!" %
                                 title)
                contents = contents + page.get()
                comment = comment_top
            elif self.append == "Bottom":
                pywikibot.output(
                    u"Page %s already exists, appending on bottom!" % title)
                contents = page.get() + contents
                comment = comment_bottom
            elif self.force:
                pywikibot.output(u"Page %s already exists, ***overwriting!" %
                                 title)
                comment = comment_force
            else:
                pywikibot.output(u"Page %s already exists, not adding!" %
                                 title)
                return
        else:
            if self.autosummary:
                comment = ''
                pywikibot.setAction('')

        if self.dry:
            pywikibot.output(u"*** Dry mode ***\n"
                             u"\03{lightpurple}title\03{default}: " + title +
                             "\n"
                             u"\03{lightpurple}contents\03{default}:\n" +
                             contents + "\n"
                             u"\03{lightpurple}comment\03{default}: " +
                             comment + "\n")
            return

        try:
            page.put(contents, comment=comment, minorEdit=self.minor)
        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 blacklist entry %s' %
                (title, error.url))
Ejemplo n.º 44
0
def main():
    genFactory = pagegenerators.GeneratorFactory()
    pageName = ''
    singlePage = ''
    summary = ''
    always = False
    doSinglePage = False
    doCategory = False
    deleteSubcategories = True
    doRef = False
    doLinks = False
    doImages = False
    undelete = False
    fileName = ''
    generator = None

    # read command line parameters
    for arg in pywikibot.handleArgs():
        if arg == '-always':
            always = True
        elif arg.startswith('-summary'):
            if len(arg) == len('-summary'):
                summary = pywikibot.input(u'Enter a reason for the deletion:')
            else:
                summary = arg[len('-summary:'):]
        elif arg.startswith('-nosubcats'):
            deleteSubcategories = False
        elif arg.startswith('-images'):
            doImages = True
            if len(arg) == len('-images'):
                pageName = pywikibot.input(
                    u'Enter the page with the images to delete:')
            else:
                pageName = arg[len('-images'):]
        elif arg.startswith('-undelete'):
            undelete = True
        else:
            genFactory.handleArg(arg)
        if not summary:
            if arg.startswith('-category'):
                summary = i18n.twtranslate(mysite, 'delete-from-category', {'page': pageName})
            elif arg.startswith('-links'):
                summary = i18n.twtranslate(mysite, 'delete-linked-pages', {'page': pageName})
            elif arg.startswith('-ref'):
                summary = i18n.twtranslate(mysite, 'delete-referring-pages', {'page': pageName})
            elif arg.startswith('-file'):
                summary = i18n.twtranslate(mysite, 'delete-from-file')
    mysite = pywikibot.getSite()
    if doImages:
        if not summary:
            summary = i18n.twtranslate(mysite, 'delete-images',
                                       {'page': pageName})
        page = pywikibot.Page(mysite, pageName)
        generator = pagegenerators.ImagesPageGenerator(page)
    if not summary:
        summary = pywikibot.input(u'Enter a reason for the %sdeletion:'
                                  % ['', 'un'][undelete])
    if not generator:
        generator = genFactory.getCombinedGenerator()
    if not generator:
        # syntax error, show help text from the top of this file
        pywikibot.showHelp('delete')
        return
    if generator:
        pywikibot.setAction(summary)
        # We are just deleting pages, so we have no need of using a preloading
        # page generator to actually get the text of those pages.
        bot = DeletionRobot(generator, summary, always, undelete)
        bot.run()
Ejemplo n.º 45
0
def main():
    global protectionLevels
    protectionLevels = ['sysop', 'autoconfirmed', 'none']

    pageName = ''
    summary = ''
    always = False
    doSinglePage = False
    doCategory = False
    protectSubcategories = True
    doRef = False
    doLinks = False
    doImages = False
    fileName = ''
    gen = None
    edit = ''
    move = ''
    defaultProtection = 'sysop'

    # read command line parameters
    for arg in pywikibot.handleArgs():
        if arg == '-always':
            always = True
        elif arg.startswith('-file'):
            if len(arg) == len('-file'):
                fileName = pywikibot.input(
                    u'Enter name of file to protect pages from:')
            else:
                fileName = arg[len('-file:'):]
        elif arg.startswith('-summary'):
            if len(arg) == len('-summary'):
                summary = pywikibot.input(
                    u'Enter a reason for the protection:')
            else:
                summary = arg[len('-summary:'):]
        elif arg.startswith('-cat'):
            doCategory = True
            if len(arg) == len('-cat'):
                pageName = pywikibot.input(
                    u'Enter the category to protect from:')
            else:
                pageName = arg[len('-cat:'):]
        elif arg.startswith('-nosubcats'):
            protectSubcategories = False
        elif arg.startswith('-links'):
            doLinks = True
            if len(arg) == len('-links'):
                pageName = pywikibot.input(u'Enter the page to protect from:')
            else:
                pageName = arg[len('-links:'):]
        elif arg.startswith('-ref'):
            doRef = True
            if len(arg) == len('-ref'):
                pageName = pywikibot.input(u'Enter the page to protect from:')
            else:
                pageName = arg[len('-ref:'):]
        elif arg.startswith('-page'):
            doSinglePage = True
            if len(arg) == len('-page'):
                pageName = pywikibot.input(u'Enter the page to protect:')
            else:
                pageName = arg[len('-page:'):]
        elif arg.startswith('-images'):
            doImages = True
            if len(arg) == len('-images'):
                pageName = pywikibot.input(u'Enter the page with the images to protect:')
            else:
                pageName = arg[len('-images:'):]
        elif arg.startswith('-unprotect'):
            defaultProtection = 'none'
        elif arg.startswith('-edit'):
            edit = arg[len('-edit:'):]
            if edit not in protectionLevels:
                edit = choiceProtectionLevel('edit', defaultProtection)
        elif arg.startswith('-move'):
            move = arg[len('-move:'):]
            if move not in protectionLevels:
                move = choiceProtectionLevel('move', defaultProtection)
        elif arg.startswith('-create'):
            create = arg[len('-create:'):]
            if create not in protectionLevels:
                create = choiceProtectionLevel('create', defaultProtection)

    mysite = pywikibot.getSite()

    if doSinglePage:
        if not summary:
            summary = pywikibot.input(u'Enter a reason for the protection:')
        page = pywikibot.Page(mysite, pageName)
        gen = iter([page])
    elif doCategory:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-category',
                                       {'cat': pageName})
        ns = mysite.category_namespace()
        categoryPage = pywikibot.Category(mysite, ns + ':' + pageName)
        gen = pagegenerators.CategorizedPageGenerator(
            categoryPage, recurse=protectSubcategories)
    elif doLinks:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-links',
                                       {'page': pageName})
        linksPage = pywikibot.Page(mysite, pageName)
        gen = pagegenerators.LinkedPageGenerator(linksPage)
    elif doRef:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-ref',
                                       {'page': pageName})
        refPage = pywikibot.Page(mysite, pageName)
        gen = pagegenerators.ReferringPageGenerator(refPage)
    elif fileName:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-simple')
        gen = pagegenerators.TextfilePageGenerator(fileName)
    elif doImages:
        if not summary:
            summary = i18n.twtranslate(mysite, 'protect-images',
                                       {'page': pageName})
        gen = pagegenerators.ImagesPageGenerator(pywikibot.Page(mysite,
                                                                pageName))

    if gen:
        pywikibot.setAction(summary)
        # We are just protecting pages, so we have no need of using a preloading
        # page generator
        # to actually get the text of those pages.
        if not edit:
            edit = defaultProtection
        if not move:
            move = defaultProtection
        bot = ProtectionRobot(gen, summary, always, edit=edit, move=move)
        bot.run()
    else:
        pywikibot.showHelp(u'protect')
Ejemplo n.º 46
0
        # Location not applicable
        if nolocRE.search(text) != None:
            taglist[name] = False
            print "Location not applicable"
            continue

        # already contains a Location
        if string.find(text, '{{Location') >= 0:

            # already contains a generated Location
            if string.find(text, 'source:exif') >= 0:
                print "HMM, %s looks already processed" % name

                if oldtext != text:
                    pywikibot.setAction("removed gps exif request template")
                    try:
                        page.put(text)
                    except:
                        print "failed to save page"
                        continue
                    print "removed a superfluous gps exif request template"

                taglist[name] = False
                continue

            # extract location to compare to exif
            lat_in_dec = 0
            lon_in_dec = 0

            #{{Location dec|47.5059|-122.0343|type:forest_region:US}}
Ejemplo n.º 47
0
def main():
    summary_commandline, gen, template = None, None, None
    namespaces, PageTitles, exceptions = [], [], []
    encat, newcatfile = '', ''
    autoText, autoTitle = False, False
    recentcat, newcat = False, False
    genFactory = pagegenerators.GeneratorFactory()
    for arg in pywikibot.handleArgs():
        if arg == '-autotitle':
            autoTitle = True
        elif arg == '-autotext':
            autoText = True
        elif arg.startswith('-page'):
            if len(arg) == 5:
                PageTitles.append(
                    pywikibot.input(u'Which page do you want to chage?'))
            else:
                PageTitles.append(arg[6:])
            break
        elif arg.startswith('-except:'):
            exceptions.append(arg[8:])
        elif arg.startswith('-template:'):
            template = arg[10:]
        elif arg.startswith('-facat:'):
            encat = arg[7:].replace(u'Category:',
                                    u'').replace(u'category:',
                                                 u'').replace(u'رده:', u'')
            encat = englishdictionry(u'رده:' + encat, fa_site,
                                     en_site).replace(u'Category:',
                                                      u'').replace(
                                                          u'category:', u'')
            break
        elif arg.startswith('-encat:'):
            encat = arg[7:].replace(u'Category:',
                                    u'').replace(u'category:',
                                                 u'').replace(u'رده:', u'')
            break
        elif arg.startswith('-newcatfile:'):
            newcatfile = arg[12:]
            break
        elif arg.startswith('-recentcat'):
            arg = arg.replace(':', '')
            if len(arg) == 10:
                genfa = pagegenerators.RecentchangesPageGenerator()
            else:
                genfa = pagegenerators.RecentchangesPageGenerator(
                    number=int(arg[10:]))
            genfa = pagegenerators.DuplicateFilterPageGenerator(genfa)
            genfa = pagegenerators.NamespaceFilterPageGenerator(genfa, [14])
            preloadingGen = pagegenerators.PreloadingGenerator(genfa, 60)
            recentcat = True
            break
        elif arg.startswith('-newcat'):
            arg = arg.replace(':', '')
            if len(arg) == 7:
                genfa = pagegenerators.NewpagesPageGenerator(step=100,
                                                             namespaces=14)
            else:
                genfa = pagegenerators.NewpagesPageGenerator(step=int(arg[7:]),
                                                             namespaces=14)
            preloadingGen = pagegenerators.PreloadingGenerator(genfa, 60)
            newcat = True
            break
        elif arg.startswith('-namespace:'):
            namespaces.append(int(arg[11:]))
        elif arg.startswith('-summary:'):
            pywikibot.setAction(arg[9:])
            summary_commandline = True
        else:
            generator = genFactory.handleArg(arg)
            if generator:
                gen = genFactory.getCombinedGenerator(gen)
    if encat != '':
        encatfalist, encatlists = encatlist(encat)
        if encatlists:
            for encat in encatlists:
                encat = englishdictionry(encat, en_site, fa_site)
                if encat:
                    run([encat])
        if encatfalist is not False:
            run(encatfalist)
    if PageTitles:
        pages = [
            pywikibot.Page(fa_site, PageTitle) for PageTitle in PageTitles
        ]
        gen = iter(pages)
    if recentcat:
        for workpage in preloadingGen:
            workpage = workpage.title()
            cat = pywikibot.Category(fa_site, workpage)
            gent = pagegenerators.CategorizedPageGenerator(cat)
            run(gent)
        pywikibot.stopme()
        sys.exit()
    if newcat:
        for workpage in preloadingGen:
            workpage = workpage.title()
            workpage = englishdictionry(workpage, fa_site, en_site)
            if workpage is not False:
                encatfalist, encatlists = encatlist(workpage)
                if encatlists:
                    for encat in encatlists:
                        encat = englishdictionry(encat, en_site, fa_site)
                        if encat:
                            run([encat])
                if encatfalist is not False:
                    run(encatfalist)
        pywikibot.stopme()
        sys.exit()
    if newcatfile:
        text2 = codecs.open(newcatfile, 'r', 'utf8')
        text = text2.read()
        linken = re.findall(ur'\[\[.*?\]\]', text, re.S)
        if linken:
            for workpage in linken:
                workpage = workpage.split(u'|')[0].replace(u'[[', u'').replace(
                    u']]', u'').strip()
                workpage = englishdictionry(workpage, fa_site, en_site)
                if workpage is not False:
                    encatfalist, encatlists = encatlist(workpage)
                    if encatlists:
                        run(encatlists)
                    if encatfalist is not False:
                        run(encatfalist)
        pywikibot.stopme()
        sys.exit()
    if not gen:
        pywikibot.stopme()
        sys.exit()
    if namespaces != []:
        gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces)
    preloadingGen = pagegenerators.PreloadingGenerator(gen, pageNumber=60)
    run(preloadingGen)
 def test_set_action(self):
     """Test deprecated setAction function."""
     pywikibot.setAction('{0}X{0}'.format(self._old_config))
     self.assertOneDeprecation(self.INSTEAD)
     self.assertEqual(pywikibot.config.default_edit_summary,
                      '{0}X{0}'.format(self._old_config))
Ejemplo n.º 49
0
         pair = [image, tim]
         break
 if pair == []:
     continue
 count += 1
 html = ""
 ccount = 0
 changesdone = False
 for cat in image.categories():
     if cat.isHiddenCategory() or re.search('categories', cat.title()):
         continue
     if cat not in tim.categories():
         ccount += 1
         html += "\n[[" + cat.title() + "]]"
 if html != "":
     pywikibot.setAction("Add {} categories from jpeg".format(ccount))
     changesdone = True
     ploop = True
     while ploop:
         try:
             tim.put(tim.get() + html)
             ploop = False
         except Exception as e:
             print Fore.RED + str(e), Fore.WHITE
             sleep(10)
 html = ""
 ccount = 0
 for cat in tim.categories():
     if cat.isHiddenCategory() or re.search('categories', cat.title()):
         continue
     if cat not in image.categories():