Revision: 8240
Author: xqt
Date: 2010-06-03 07:19:33 +0000 (Thu, 03 Jun 2010)
Log Message:
-----------
import wikipedia as pywikibot for easier merging to rewrite
Modified Paths:
--------------
trunk/pywikipedia/add_text.py
trunk/pywikipedia/archivebot.py
trunk/pywikipedia/articlenos.py
trunk/pywikipedia/basic.py
trunk/pywikipedia/casechecker.py
trunk/pywikipedia/catall.py
trunk/pywikipedia/commonscat.py
Modified: trunk/pywikipedia/add_text.py
===================================================================
--- trunk/pywikipedia/add_text.py 2010-06-03 05:30:45 UTC (rev 8239)
+++ trunk/pywikipedia/add_text.py 2010-06-03 07:19:33 UTC (rev 8240)
@@ -63,7 +63,8 @@
"""
#
-# (C) Filnik, 2007-2008
+# (C) Filnik, 2007-2009
+# (C) Pywikipedia bot team, 2007-2010
#
# Distributed under the terms of the MIT license.
#
@@ -71,7 +72,7 @@
#
import re, pagegenerators, urllib2, urllib
-import wikipedia
+import wikipedia as pywikibot
import codecs, config
# This is required for the text that is shown when you run this script
@@ -104,10 +105,10 @@
nn_iw_msg = u'<!--interwiki (no, sv, da first; then other languages alphabetically by name)-->'
-class NoEnoughData(wikipedia.Error):
+class NoEnoughData(pywikibot.Error):
""" Error class for when the user doesn't specified all the data needed """
-class NothingFound(wikipedia.Error):
+class NothingFound(pywikibot.Error):
""" An exception indicating that a regex has return [] instead of results."""
# Useful for the untagged function
@@ -115,13 +116,13 @@
""" Function to load HTML text of a URL """
try:
request = urllib2.Request(url)
- request.add_header("User-Agent", wikipedia.useragent)
+ request.add_header("User-Agent", pywikibot.useragent)
response = urllib2.urlopen(request)
text = response.read()
response.close()
# When you load to many users, urllib2 can give this error.
except urllib2.HTTPError:
- wikipedia.output(u"Server error. Pausing for 10 seconds... " + time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime()) )
+ pywikibot.output(u"Server error. Pausing for 10 seconds... " + time.strftime("%d %b %Y %H:%M:%S (UTC)", time.gmtime()) )
response.close()
time.sleep(10)
return pageText(url)
@@ -146,7 +147,7 @@
'Nothing found! Try to use the tool by yourself to be sure that it works!')
else:
for result in results:
- yield wikipedia.Page(wikipedia.getSite(), result)
+ yield pywikibot.Page(pywikibot.getSite(), result)
def add_text(page = None, addText = None, summary = None, regexSkip = None,
regexSkipUrl = None, always = False, up = False, putText = True,
@@ -154,7 +155,7 @@
if not addText:
raise NoEnoughData('You have to specify what text you want to add!')
if not summary:
- summary = wikipedia.translate(wikipedia.getSite(), msg) % addText[:200]
+ summary = pywikibot.translate(pywikibot.getSite(), msg) % addText[:200]
# When a page is tagged as "really well written" it has a star in the
# interwiki links. This is a list of all the templates used (in regex
@@ -188,25 +189,25 @@
]
errorCount = 0
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
# /wiki/ is not always the right path in non-wiki projects
pathWiki = site.family.nicepath(site.lang)
if putText:
- wikipedia.output(u'Loading %s...' % page.title())
+ pywikibot.output(u'Loading %s...' % page.title())
if oldTextGiven == None:
try:
text = page.get()
- except wikipedia.NoPage:
+ except pywikibot.NoPage:
if create:
- wikipedia.output(u"%s doesn't exist, creating it!"
+ pywikibot.output(u"%s doesn't exist, creating it!"
% page.title())
text = u''
else:
- wikipedia.output(u"%s doesn't exist, skip!" % page.title())
+ pywikibot.output(u"%s doesn't exist, skip!" % page.title())
return (False, False, always) # continue
- except wikipedia.IsRedirectPage:
- wikipedia.output(u"%s is a redirect, skip!" % page.title())
+ except pywikibot.IsRedirectPage:
+ pywikibot.output(u"%s is a redirect, skip!" % page.title())
return (False, False, always) # continue
else:
text = oldTextGiven
@@ -216,27 +217,28 @@
url = '%s%s' % (pathWiki, page.urlname())
result = re.findall(regexSkipUrl, site.getUrl(url))
if result != []:
- wikipedia.output(
+ pywikibot.output(
u'Exception! regex (or word) used with -exceptUrl is in the page. Skip!')
return (False, False, always) # continue
if regexSkip != None:
result = re.findall(regexSkip, text)
if result != []:
- wikipedia.output(
+ pywikibot.output(
u'Exception! regex (or word) used with -except is in the page. Skip!')
return (False, False, always) # continue
# If not up, text put below
if not up:
newtext = text
# Getting the categories
- categoriesInside = wikipedia.getCategoryLinks(newtext, site)
+ categoriesInside = pywikibot.getCategoryLinks(newtext, site)
# Deleting the categories
- newtext = wikipedia.removeCategoryLinks(newtext, site)
+ newtext = pywikibot.removeCategoryLinks(newtext, site)
# Getting the interwiki
- interwikiInside = wikipedia.getLanguageLinks(newtext, site)
+ interwikiInside = pywikibot.getLanguageLinks(newtext, site)
# Removing the interwiki
- newtext = wikipedia.removeLanguageLinks(newtext, site)
- #nn got a message between the categories and the iw's and they want to keep it there, first remove it
+ newtext = pywikibot.removeLanguageLinks(newtext, site)
+ # nn got a message between the categories and the iw's and they want to
+ # keep it there, first remove it
if (site.language()==u'nn'):
newtext = newtext.replace(nn_iw_msg, '')
# Translating the \\n into binary \n
@@ -244,13 +246,14 @@
# Adding the text
newtext += u"\n%s" % addText
# Reputting the categories
- newtext = wikipedia.replaceCategoryLinks(newtext, categoriesInside, site, True)
+ newtext = pywikibot.replaceCategoryLinks(newtext,
+ categoriesInside, site, True)
#Put the nn iw message back
if (site.language()==u'nn'):
newtext = newtext + u'\n' + nn_iw_msg
# Dealing the stars' issue
allstars = []
- starstext = wikipedia.removeDisabledParts(text)
+ starstext = pywikibot.removeDisabledParts(text)
for star in starsList:
regex = re.compile('(\{\{(?:template:|)%s\|.*?\}\}[\s]*)' % star,
re.I)
@@ -264,21 +267,21 @@
for element in allstars:
newtext += '%s\r\n' % element.strip()
# Adding the interwiki
- newtext = wikipedia.replaceLanguageLinks(newtext, interwikiInside, site)
+ newtext = pywikibot.replaceLanguageLinks(newtext, interwikiInside, site)
# If instead the text must be added above...
else:
newtext = addText + '\n' + text
if putText and text != newtext:
- wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
% page.title())
- wikipedia.showDiff(text, newtext)
+ pywikibot.showDiff(text, newtext)
# Let's put the changes.
while True:
# If someone load it as module, maybe it's not so useful to put the
# text in the page
if putText:
if not always:
- choice = wikipedia.inputChoice(
+ choice = pywikibot.inputChoice(
u'Do you want to accept these changes?',
['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
if choice == 'a':
@@ -291,27 +294,27 @@
page.put(newtext, summary)
else:
page.put_async(newtext, summary)
- except wikipedia.EditConflict:
- wikipedia.output(u'Edit conflict! skip!')
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Edit conflict! skip!')
return (False, False, always)
- except wikipedia.ServerError:
+ except pywikibot.ServerError:
errorCount += 1
if errorCount < 5:
- wikipedia.output(u'Server Error! Wait..')
+ pywikibot.output(u'Server Error! Wait..')
time.sleep(5)
continue
else:
- raise wikipedia.ServerError(u'Fifth Server Error!')
- except wikipedia.SpamfilterError, e:
- wikipedia.output(
+ raise pywikibot.ServerError(u'Fifth Server Error!')
+ except pywikibot.SpamfilterError, e:
+ pywikibot.output(
u'Cannot change %s because of blacklist entry %s'
% (page.title(), e.url))
return (False, False, always)
- except wikipedia.PageNotSaved, error:
- wikipedia.output(u'Error putting page: %s' % error.args)
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u'Error putting page: %s' % error.args)
return (False, False, always)
- except wikipedia.LockedPage:
- wikipedia.output(u'Skipping %s (locked page)'
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Skipping %s (locked page)'
% page.title())
return (False, False, always)
else:
@@ -333,44 +336,44 @@
# Put the text above or below the text?
up = False
# Loading the arguments
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg.startswith('-textfile'):
if len(arg) == 9:
- textfile = wikipedia.input(
+ textfile = pywikibot.input(
u'Which textfile do you want to add?')
else:
textfile = arg[10:]
elif arg.startswith('-text'):
if len(arg) == 5:
- addText = wikipedia.input(u'What text do you want to add?')
+ addText = pywikibot.input(u'What text do you want to add?')
else:
addText = arg[6:]
elif arg.startswith('-summary'):
if len(arg) == 8:
- summary = wikipedia.input(u'What summary do you want to use?')
+ summary = pywikibot.input(u'What summary do you want to use?')
else:
summary = arg[9:]
elif arg.startswith('-page'):
if len(arg) == 5:
- generator = [wikipedia.Page(
- wikipedia.getSite(),
- wikipedia.input(u'What page do you want to use?')
+ generator = [pywikibot.Page(
+ pywikibot.getSite(),
+ pywikibot.input(u'What page do you want to use?')
)]
else:
- generator = [wikipedia.Page(wikipedia.getSite(), arg[6:])]
+ generator = [pywikibot.Page(pywikibot.getSite(), arg[6:])]
elif arg.startswith('-excepturl'):
if len(arg) == 10:
- regexSkipUrl = wikipedia.input(u'What text should I skip?')
+ regexSkipUrl = pywikibot.input(u'What text should I skip?')
else:
regexSkipUrl = arg[11:]
elif arg.startswith('-except'):
if len(arg) == 7:
- regexSkip = wikipedia.input(u'What text should I skip?')
+ regexSkip = pywikibot.input(u'What text should I skip?')
else:
regexSkip = arg[8:]
elif arg.startswith('-untagged'):
if len(arg) == 9:
- untaggedProject = wikipedia.input(
+ untaggedProject = pywikibot.input(
u'What project do you want to use?')
else:
untaggedProject = arg[10:]
@@ -395,7 +398,7 @@
'You have to specify the generator you want to use for the script!')
if talkPage:
generator = pagegenerators.PageWithTalkPageGenerator(generator)
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
for namespace in site.namespaces():
index = site.getNamespaceIndex(namespace)
if index%2==1 and index>0:
@@ -412,4 +415,4 @@
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/archivebot.py
===================================================================
--- trunk/pywikipedia/archivebot.py 2010-06-03 05:30:45 UTC (rev 8239)
+++ trunk/pywikipedia/archivebot.py 2010-06-03 07:19:33 UTC (rev 8240)
@@ -67,14 +67,16 @@
-L LANG, --lang=LANG set the language code to work on
"""
#
-# (C) Misza13, 2006-2007
+# (C) Misza13, 2006-2010
+# (C) Pywikipedia bot team, 2007-2010
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
-import wikipedia, pagegenerators, query
-Site = wikipedia.getSite()
+import wikipedia as pywikibot
+import pagegenerators, query
+Site = pywikibot.getSite()
import os, re, time, locale, traceback, string, urllib
@@ -85,7 +87,6 @@
import md5
new_hash = md5.md5
-
language = Site.language()
messages = {
'_default': {
@@ -138,7 +139,6 @@
'ArchiveSummary': u'Arkiverer %(count)d trådar frå [[%(from)s]].',
'OlderThanSummary': u'eldre enn',
},
- # fix below
'pt': {
'ArchiveFull': u'(ARQUIVO COMPLETO)',
'InitialArchiveHeader': u'{{talkarchive}}',
@@ -168,25 +168,20 @@
lang = '_default'
return messages[lang][key]
-
-class MalformedConfigError(wikipedia.Error):
+class MalformedConfigError(pywikibot.Error):
"""There is an error in the configuration template."""
-
-class MissingConfigError(wikipedia.Error):
+class MissingConfigError(pywikibot.Error):
"""The config is missing in the header (either it's in one of the threads
or transcluded from another page)."""
-
class AlgorithmError(MalformedConfigError):
"""Invalid specification of archiving algorithm."""
-
-class ArchiveSecurityError(wikipedia.Error):
+class ArchiveSecurityError(pywikibot.Error):
"""Archive is not a subpage of page being archived and key not specified
(or incorrect)."""
-
def str2time(str):
"""Accepts a string defining a time period:
7d - 7 days
@@ -199,7 +194,6 @@
else:
return int(str)
-
def str2size(str):
"""Accepts a string defining a size:
1337 - 1337 bytes
@@ -218,23 +212,22 @@
else:
return (int(str[:-1])*1024,'B')
-
def int2month(num):
"""Returns the locale's full name of month 'num' (1-12)."""
if hasattr(locale, 'nl_langinfo'):
return locale.nl_langinfo(locale.MON_1+num-1).decode('utf-8')
- Months = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
+ Months = ['january', 'february', 'march', 'april', 'may', 'june',
+ 'july', 'august', 'september', 'october', 'november', 'december']
return Site.mediawiki_message(Months[num-1])
-
def int2month_short(num):
"""Returns the locale's abbreviated name of month 'num' (1-12)."""
if hasattr(locale, 'nl_langinfo'):
return locale.nl_langinfo(locale.ABMON_1+num-1).replace('\xa0', '').decode('utf-8')
- Months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
+ Months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
+ 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
return Site.mediawiki_message(Months[num-1])
-
def txt2timestamp(txt, format):
"""Attempts to convert the timestamp 'txt' according to given 'format'.
On success, returns the time tuple; on failure, returns None."""
@@ -248,7 +241,6 @@
pass
return None
-
def generateTransclusions(Site, template, namespaces=[], eicontinue=''):
qdata = {
'action' : 'query',
@@ -261,18 +253,18 @@
if eicontinue:
qdata['eicontinue'] = eicontinue
- wikipedia.output(u'Fetching template transclusions...')
+ pywikibot.output(u'Fetching template transclusions...')
response, result = query.GetData(qdata, Site, back_response = True)
for page_d in result['query']['embeddedin']:
- yield wikipedia.Page(Site, page_d['title'])
+ yield pywikibot.Page(Site, page_d['title'])
if 'query-continue' in result:
eicontinue = result['query-continue']['embeddedin']['eicontinue']
- for page in generateTransclusions(Site, template, namespaces, eicontinue):
+ for page in generateTransclusions(Site, template, namespaces,
+ eicontinue):
yield page
-
class DiscussionThread(object):
"""An object representing a discussion thread on a page, that is something of the form:
@@ -288,7 +280,8 @@
self.timestamp = None
def __repr__(self):
- return '%s("%s",%d bytes)' % (self.__class__.__name__,self.title,len(self.content))
+ return '%s("%s",%d bytes)' \
+ % (self.__class__.__name__,self.title,len(self.content))
def feedLine(self, line):
if not self.content and not line:
@@ -318,7 +311,7 @@
pat = re.compile(r'(\d\d):(\d\d), (\d\d?)\. (\S+)\.? (\d\d\d\d) \(UTC\)')
TM = pat.search(line)
if TM:
-# wikipedia.output(TM)
+# pywikibot.output(TM)
TIME = txt2timestamp(TM.group(0),"%d. %b %Y kl. %H:%M (%Z)")
if not TIME:
TIME = txt2timestamp(TM.group(0), "%Y. %B %d., %H:%M (%Z)")
@@ -344,9 +337,9 @@
TIME = txt2timestamp(TM.group(0),"%H:%M, %d. %b. %Y (%Z)")
if TIME:
self.timestamp = max(self.timestamp,time.mktime(TIME))
-# wikipedia.output(u'Time to be parsed: %s' % TM.group(0))
-# wikipedia.output(u'Parsed time: %s' % TIME)
-# wikipedia.output(u'Newest timestamp in thread: %s' % TIME)
+# pywikibot.output(u'Time to be parsed: %s' % TM.group(0))
+# pywikibot.output(u'Parsed time: %s' % TIME)
+# pywikibot.output(u'Newest timestamp in thread: %s' % TIME)
def size(self):
return len(self.title) + len(self.content) + 12
@@ -367,23 +360,23 @@
return message('OlderThanSummary') + ' ' + reT.group(1)
return ''
-
class DiscussionPage(object):
- """A class that represents a single discussion page as well as an archive page.
- Feed threads to it and run an update() afterwards."""
- #TODO: Make it a subclass of wikipedia.Page
+ """A class that represents a single discussion page as well as an archive
+ page. Feed threads to it and run an update() afterwards."""
+ #TODO: Make it a subclass of pywikibot.Page
def __init__(self, title, archiver, vars=None):
self.title = title
self.threads = []
- self.Page = wikipedia.Page(Site,self.title)
+ self.Page = pywikibot.Page(Site,self.title)
self.full = False
self.archiver = archiver
self.vars = vars
try:
self.loadPage()
- except wikipedia.NoPage:
- self.header = archiver.get('archiveheader',message('InitialArchiveHeader'))
+ except pywikibot.NoPage:
+ self.header = archiver.get('archiveheader',
+ message('InitialArchiveHeader'))
if self.vars:
self.header = self.header % self.vars
@@ -427,7 +420,7 @@
def update(self, summary, sortThreads = False):
if sortThreads:
- wikipedia.output(u'Sorting threads...')
+ pywikibot.output(u'Sorting threads...')
self.threads.sort(key = lambda t: t.timestamp)
newtext = re.sub('\n*$','\n\n',self.header) #Fix trailing newlines
for t in self.threads:
@@ -436,10 +429,9 @@
summary += ' ' + message('ArchiveFull')
self.Page.put(newtext, minorEdit=True, comment=summary)
-
class PageArchiver(object):
"""A class that encapsulates all archiving methods.
- __init__ expects a wikipedia.Page object.
+ __init__ expects a pywikibot.Page object.
Execute by running the .run() method."""
algo = 'none'
@@ -472,10 +464,14 @@
self.attributes[attr] = [value, out]
def saveables(self):
- return [a for a in self.attributes if self.attributes[a][1] and a != 'maxage']
+ return [a for a in self.attributes if self.attributes[a][1]
+ and a != 'maxage']
def attr2text(self):
- return '{{%s\n%s\n}}' % (self.tpl,'\n'.join(['|%s = %s'%(a,self.get(a)) for a in self.saveables()]))
+ return '{{%s\n%s\n}}' \
+ % (self.tpl,
+ '\n'.join(['|%s = %s'%(a,self.get(a))
+ for a in self.saveables()]))
def key_ok(self):
s = new_hash()
@@ -485,7 +481,7 @@
def loadConfig(self):
hdrlines = self.Page.header.split('\n')
-# wikipedia.output(u'Looking for: %s' % self.tpl)
+# pywikibot.output(u'Looking for: %s' % self.tpl)
mode = 0
for line in hdrlines:
if mode == 0 and re.search('{{'+self.tpl,line):
@@ -507,11 +503,14 @@
def feedArchive(self, archive, thread, maxArchiveSize, vars=None):
"""Feed the thread to one of the archives.
If it doesn't exist yet, create it.
- If archive name is an empty string (or None), discard the thread (/dev/null).
+ If archive name is an empty string (or None),
+ discard the thread (/dev/null).
Also checks for security violations."""
if not archive:
return
- if not self.force and not self.Page.title+'/' == archive[:len(self.Page.title)+1] and not self.key_ok():
+ if not self.force \
+ and not self.Page.title+'/' == archive[:len(self.Page.title)+1] \
+ and not self.key_ok():
raise ArchiveSecurityError
if not archive in self.archives:
self.archives[archive] = DiscussionPage(archive,self,vars)
@@ -525,10 +524,12 @@
T = time.mktime(time.gmtime())
whys = []
for t in oldthreads:
- if len(oldthreads) - self.archivedThreads <= int(self.get('minthreadsleft',5)):
+ if len(oldthreads) - self.archivedThreads \
+ <= int(self.get('minthreadsleft',5)):
self.Page.threads.append(t)
continue #Because there's too little threads left.
- #TODO: Make an option so that unstamped (unsigned) posts get archived.
+ # TODO: Make an option so that unstamped (unsigned) posts get
+ # archived.
why = t.shouldBeArchived(self)
if why:
archive = self.get('archive')
@@ -556,7 +557,9 @@
return
whys = self.analyzePage()
if self.archivedThreads < int(self.get('minthreadstoarchive',2)):
- return #We might not want to archive a measly few threads (lowers edit frequency)
+ # We might not want to archive a measly few threads
+ # (lowers edit frequency)
+ return
if whys:
#Save the archives first (so that bugs don't cause a loss of data)
for a in sorted(self.archives.keys()):
@@ -568,14 +571,14 @@
rx = re.compile('{{'+self.tpl+'\n.*?\n}}',re.DOTALL)
self.Page.header = rx.sub(self.attr2text(),self.Page.header)
self.commentParams['count'] = self.archivedThreads
- self.commentParams['archives'] = ', '.join(['[['+a.title+']]' for a in self.archives.values()])
+ self.commentParams['archives'] \
+ = ', '.join(['[['+a.title+']]' for a in self.archives.values()])
if not self.commentParams['archives']:
self.commentParams['archives'] = '/dev/null'
self.commentParams['why'] = ', '.join(whys)
comment = self.pageSummary % self.commentParams
self.Page.update(comment)
-
def main():
global Site, language
from optparse import OptionParser
@@ -601,7 +604,8 @@
(options, args) = parser.parse_args()
if options.locale:
- locale.setlocale(locale.LC_TIME,options.locale) #Required for english month names
+ #Required for english month names
+ locale.setlocale(locale.LC_TIME,options.locale)
if options.timezone:
os.environ['TZ'] = options.timezone
@@ -615,7 +619,7 @@
s = new_hash()
s.update(options.salt+'\n')
s.update(options.calc+'\n')
- wikipedia.output(u'key = ' + s.hexdigest())
+ pywikibot.output(u'key = ' + s.hexdigest())
return
if options.salt:
@@ -629,14 +633,14 @@
force = False
if options.lang:
- Site = wikipedia.getSite(options.lang)
+ Site = pywikibot.getSite(options.lang)
language = Site.language()
- if wikipedia.debug: print Site
+ if pywikibot.debug: print Site
for a in args:
pagelist = []
if not options.filename and not options.pagename:
- #for pg in wikipedia.Page(Site,a).getReferences(follow_redirects=False,onlyTemplateInclusion=True):
+ #for pg in pywikibot.Page(Site,a).getReferences(follow_redirects=False,onlyTemplateInclusion=True):
if not options.namespace == None:
ns = [str(options.namespace)]
else:
@@ -645,26 +649,28 @@
pagelist.append(pg)
if options.filename:
for pg in file(options.filename,'r').readlines():
- pagelist.append(wikipedia.Page(Site,pg))
+ pagelist.append(pywikibot.Page(Site,pg))
if options.pagename:
- pagelist.append(wikipedia.Page(Site,options.pagename))
+ pagelist.append(pywikibot.Page(Site,options.pagename))
pagelist = sorted(pagelist)
#if not options.namespace == None:
# pagelist = [pg for pg in pagelist if pg.namespace()==options.namespace]
for pg in pagelist:
- try: #Catching exceptions, so that errors in one page do not bail out the entire process
+ # Catching exceptions, so that errors in one page do not bail out
+ # the entire process
+ try:
Archiver = PageArchiver(pg, a, salt, force)
Archiver.run()
time.sleep(10)
except:
- wikipedia.output(u'Error occured while processing page %s' % pg.aslink(True))
+ pywikibot.output(u'Error occured while processing page %s'
+ % pg.aslink(True))
traceback.print_exc()
-
if __name__ == '__main__':
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/articlenos.py
===================================================================
--- trunk/pywikipedia/articlenos.py 2010-06-03 05:30:45 UTC (rev 8239)
+++ trunk/pywikipedia/articlenos.py 2010-06-03 07:19:33 UTC (rev 8240)
@@ -1,10 +1,12 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
-A script that displays the ordinal number of the new articles being created visible on the Recent Changes list.
-The script doesn't make any edits, no bot account needed.
+A script that displays the ordinal number of the new articles being created
+visible on the Recent Changes list. The script doesn't make any edits, no bot
+account needed.
-Note: the script requires the Python IRC library http://python-irclib.sourceforge.net/
+Note: the script requires the Python IRC library
+http://python-irclib.sourceforge.net/
"""
# Author: Balasyum
@@ -13,8 +15,9 @@
__version__ = '$Id$'
from ircbot import SingleServerIRCBot
-from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr
-import wikipedia
+from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad
+from irclib import ip_quad_to_numstr
+import wikipedia as pywikibot
import re
class ArtNoDisp(SingleServerIRCBot):
@@ -22,11 +25,13 @@
SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
self.site = site
- self.other_ns = re.compile(u'14\[\[07(' + u'|'.join(site.namespaces()) + u')')
+ self.other_ns = re.compile(
+ u'14\[\[07(' + u'|'.join(site.namespaces()) + u')')
self.api_url = self.site.api_address()
self.api_url += 'action=query&meta=siteinfo&siprop=statistics&format=xml'
self.api_found = re.compile(r'articles="(.*?)"')
- self.re_edit = re.compile(r'^C14\[\[^C07(?P<page>.+?)^C14\]\]^C4 (?P<flags>.*?)^C10 ^C02(?P<url>.+?)^C ^C5\*^C ^C03(?P<user>.+?)^C ^C5\*^C \(?^B?(?P<bytes>[+-]?\d+?)^B?\) ^C10(?P<summary>.*)^C'.replace('^B', '\002').replace('^C', '\003').replace('^U', '\037'))
+ self.re_edit = re.compile(
+ r'^C14\[\[^C07(?P<page>.+?)^C14\]\]^C4 (?P<flags>.*?)^C10 ^C02(?P<url>.+?)^C ^C5\*^C ^C03(?P<user>.+?)^C ^C5\*^C \(?^B?(?P<bytes>[+-]?\d+?)^B?\) ^C10(?P<summary>.*)^C'.replace('^B', '\002').replace('^C', '\003').replace('^U', '\037'))
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
@@ -52,12 +57,12 @@
name = msg[8:msg.find(u'14',9)]
text = self.site.getUrl(self.api_url)
entry = self.api_found.findall(text)
- page = wikipedia.Page(self.site, name)
+ page = pywikibot.Page(self.site, name)
try:
text = page.get()
- except wikipedia.NoPage:
+ except pywikibot.NoPage:
return
- except wikipedia.IsRedirectPage:
+ except pywikibot.IsRedirectPage:
return
print entry[0], name
@@ -74,7 +79,7 @@
pass
def main():
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
site.forceLogin()
chan = '#' + site.language() + '.' + site.family.name
bot = ArtNoDisp(site, chan, site.loggedInAs(), "irc.wikimedia.org")
Modified: trunk/pywikipedia/basic.py
===================================================================
--- trunk/pywikipedia/basic.py 2010-06-03 05:30:45 UTC (rev 8239)
+++ trunk/pywikipedia/basic.py 2010-06-03 07:19:33 UTC (rev 8240)
@@ -15,7 +15,14 @@
All other parameters will be regarded as part of the title of a single page,
and the bot will only work on that single page.
"""
+#
+# (C) Pywikipedia bot team, 2006-2010
+#
+# Distributed under the terms of the MIT license.
+#
__version__ = '$Id$'
+#
+
import wikipedia as pywikibot
import pagegenerators
@@ -69,10 +76,12 @@
# Load the page
text = page.get()
except pywikibot.NoPage:
- pywikibot.output(u"Page %s does not exist; skipping." % page.aslink())
+ 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())
+ pywikibot.output(u"Page %s is a redirect; skipping."
+ % page.aslink())
return
################################################################
@@ -87,7 +96,8 @@
if text != page.get():
# Show the title of the page we're working on.
# Highlight the title in purple.
- pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
+ % page.title())
# show what was changed
pywikibot.showDiff(page.get(), text)
if not self.dry:
@@ -97,11 +107,16 @@
# Save the page
page.put(text, comment=self.summary)
except pywikibot.LockedPage:
- pywikibot.output(u"Page %s is locked; skipping." % page.aslink())
+ pywikibot.output(u"Page %s is locked; skipping."
+ % page.aslink())
except pywikibot.EditConflict:
- pywikibot.output(u'Skipping %s because of edit conflict' % (page.title()))
+ pywikibot.output(
+ u'Skipping %s because of edit conflict'
+ % (page.title()))
except pywikibot.SpamfilterError, error:
- pywikibot.output(u'Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
+ pywikibot.output(
+u'Cannot change %s because of spam blacklist entry %s'
+ % (page.title(), error.url))
def main():
Modified: trunk/pywikipedia/casechecker.py
===================================================================
--- trunk/pywikipedia/casechecker.py 2010-06-03 05:30:45 UTC (rev 8239)
+++ trunk/pywikipedia/casechecker.py 2010-06-03 07:19:33 UTC (rev 8240)
@@ -16,12 +16,17 @@
for cc in xuniqueCombinations(items[i+1:],n-1):
yield [items[i]]+cc
# End of permutation code
-
+#
+# (C) Pywikipedia bot team, 2006-2010
+#
+# Distributed under the terms of the MIT license.
+#
__version__ = '$Id$'
#
# Windows Concose colors
-# This code makes this script Windows ONLY!!! Feel free to adapt it to another platform
+# This code makes this script Windows ONLY!!!
+# Feel free to adapt it to another platform
#
# Adapted from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496901
#
@@ -48,7 +53,8 @@
def SetColor(color):
if std_out_handle:
try:
- return ctypes.windll.kernel32.SetConsoleTextAttribute(std_out_handle, color)
+ return ctypes.windll.kernel32.SetConsoleTextAttribute(
+ std_out_handle, color)
except:
pass
@@ -59,7 +65,8 @@
# end of console code
import os
-import sys, query, wikipedia, re, codecs
+import sys, query, re, codecs
+import wikipedia as pywikibot
class CaseChecker( object ):
@@ -74,7 +81,8 @@
'ru': u'[[ВП:КЛ]]',
}
- # These words are always in one language, even though they could be typed in both
+ # These words are always in one language, even though they could be typed
+ # in both
alwaysInLocal = [ u'СССР', u'Как', u'как' ]
alwaysInLatin = [ u'II', u'III' ]
@@ -85,12 +93,16 @@
localSuspects = u'АВЕКМНОРСТХІЁЇаеорсухіёї'
latinSuspects = u'ABEKMHOPCTXIËÏaeopcyxiëï'
- localKeyboard = u'йцукенгшщзфывапролдячсмить' # possibly try to fix one character mistypes in an alternative keyboard layout
+ # possibly try to fix one character mistypes in an alternative keyboard
+ # layout
+ localKeyboard = u'йцукенгшщзфывапролдячсмить'
latinKeyboard = u'qwertyuiopasdfghjklzxcvbnm'
romanNumChars = u'IVXLMC'
- romannumSuffixes = localLowerLtr # all letters that may be used as suffixes after roman numbers: "Iый"
- romanNumSfxPtrn = re.compile(u'^[' + romanNumChars + ']+[' + localLowerLtr + ']+$')
+ # all letters that may be used as suffixes after roman numbers: "Iый"
+ romannumSuffixes = localLowerLtr
+ romanNumSfxPtrn = re.compile(
+ u'^[' + romanNumChars + ']+[' + localLowerLtr + ']+$')
whitelists = {
'ru': u'ВП:КЛ/Whitelist'
@@ -118,12 +130,12 @@
def __init__(self):
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg.startswith('-from'):
if arg.startswith('-from:'):
self.apfrom = arg[6:]
else:
- self.apfrom = wikipedia.input(u'Which page to start from: ')
+ self.apfrom = pywikibot.input(u'Which page to start from: ')
elif arg.startswith('-reqsize:'):
self.aplimit = int(arg[9:])
elif arg == '-links':
@@ -142,13 +154,14 @@
elif arg.startswith('-wikilog:'):
self.wikilogfile = arg[9:]
else:
- wikipedia.output(u'Unknown argument %s.' % arg)
- wikipedia.showHelp()
+ pywikibot.output(u'Unknown argument %s.' % arg)
+ pywikibot.showHelp()
sys.exit()
if self.namespaces == []:
if self.apfrom == u'':
- self.namespaces = [14, 10, 12, 0] # 0 should be after templates ns
+ # 0 should be after templates ns
+ self.namespaces = [14, 10, 12, 0]
else:
self.namespaces = [0]
@@ -161,7 +174,7 @@
self.params['prop'] = 'links|categories'
- self.site = wikipedia.getSite()
+ self.site = pywikibot.getSite()
if len(self.localSuspects) != len(self.latinSuspects):
raise ValueError(u'Suspects must be the same size')
@@ -169,7 +182,7 @@
raise ValueError(u'Keyboard info must be the same size')
if not os.path.isabs(self.wikilogfile):
- self.wikilogfile = wikipedia.config.datafilepath(self.wikilogfile)
+ self.wikilogfile = pywikibot.config.datafilepath(self.wikilogfile)
try:
self.wikilog = codecs.open(self.wikilogfile, 'a', 'utf-8')
except IOError:
@@ -177,29 +190,35 @@
self.lclToLatDict = dict([(ord(self.localSuspects[i]),
self.latinSuspects[i])
- for i in range(len(self.localSuspects))])
+ for i in range(len(self.localSuspects))])
self.latToLclDict = dict([(ord(self.latinSuspects[i]),
self.localSuspects[i])
- for i in range(len(self.localSuspects))])
+ for i in range(len(self.localSuspects))])
if self.localKeyboard is not None:
- self.lclToLatKeybDict = dict([(ord(self.localKeyboard[i]),
- self.latinKeyboard[i])
- for i in range(len(self.localKeyboard))])
- self.latToLclKeybDict = dict([(ord(self.latinKeyboard[i]),
- self.localKeyboard[i])
- for i in range(len(self.localKeyboard))])
+ self.lclToLatKeybDict = dict(
+ [(ord(self.localKeyboard[i]),
+ self.latinKeyboard[i])
+ for i in range(len(self.localKeyboard))])
+ self.latToLclKeybDict = dict(
+ [(ord(self.latinKeyboard[i]),
+ self.localKeyboard[i])
+ for i in range(len(self.localKeyboard))])
else:
self.lclToLatKeybDict = {}
self.latToLclKeybDict = {}
- badPtrnStr = u'([%s][%s]|[%s][%s])' % (self.latLtr, self.localLtr, self.localLtr, self.latLtr)
- self.badWordPtrn = re.compile(u'[%s%s]*%s[%s%s]*' % (self.latLtr, self.localLtr, badPtrnStr, self.latLtr, self.localLtr) )
+ badPtrnStr = u'([%s][%s]|[%s][%s])' \
+ % (self.latLtr, self.localLtr, self.localLtr, self.latLtr)
+ self.badWordPtrn = re.compile(u'[%s%s]*%s[%s%s]*'
+ % (self.latLtr, self.localLtr,
+ badPtrnStr, self.latLtr,
+ self.localLtr))
# Get whitelist
if self.site.lang in self.whitelists:
wlpage = self.whitelists[self.site.lang]
- wikipedia.output(u'Loading whitelist from %s' % wlpage)
+ pywikibot.output(u'Loading whitelist from %s' % wlpage)
wlparams = {
'action' : 'query',
'prop' : 'links',
@@ -208,18 +227,22 @@
'indexpageids' : '',
}
- data = query.GetData(wlparams, wikipedia.getSite(self.site.lang), encodeTitle=False)
+ data = query.GetData(wlparams, pywikibot.getSite(self.site.lang),
+ encodeTitle=False)
if len(data['query']['pageids']) == 1:
pageid = data['query']['pageids'][0]
links = data['query']['pages'][pageid]['links']
self.knownWords = set( [n['title'] for n in links] )
else:
raise "The number of pageids is not 1"
- wikipedia.output(u'Loaded whitelist with %i items' % len(self.knownWords))
- if wikipedia.verbose and len(self.knownWords) > 0:
- wikipedia.output(u'Whitelist: [[%s]]' % u']], [['.join(self.knownWords))
+ pywikibot.output(u'Loaded whitelist with %i items'
+ % len(self.knownWords))
+ if pywikibot.verbose and len(self.knownWords) > 0:
+ pywikibot.output(
+ u'Whitelist: [[%s]]' % u']], [['.join(self.knownWords))
else:
- wikipedia.output(u'Whitelist is not known for language %s' % self.site.lang)
+ pywikibot.output(u'Whitelist is not known for language %s'
+ % self.site.lang)
self.knownWords = set()
def Run(self):
@@ -235,7 +258,8 @@
self.params['gapfrom'] = self.apfrom
data = query.GetData(self.params, self.site)
try:
- self.apfrom = data['query-continue']['allpages']['gapfrom']
+ self.apfrom = data['query-continue'] \
+ ['allpages']['gapfrom']
except:
self.apfrom = None
@@ -248,7 +272,7 @@
if firstItem:
if lastLetter != title[0]:
try:
- print 'Processing ' + title
+ print 'Processing %s' % title
except:
print 'Processing unprintable title'
lastLetter = title[0]
@@ -258,14 +282,24 @@
if err:
changed = False
if self.replace:
- newTitle = self.PickTarget(False, title, title, err[1])
+ newTitle = self.PickTarget(False,
+ title,
+ title,
+ err[1])
if newTitle:
- editSummary = wikipedia.translate(self.site, self.msgRename)
- src = wikipedia.Page(self.site, title)
+ editSummary = pywikibot.translate(
+ self.site, self.msgRename)
+ src = pywikibot.Page(self.site,
+ title)
if page['ns'] == 14:
import category
- dst = wikipedia.Page(self.site, newTitle)
- bot = category.CategoryMoveRobot(src.titleWithoutNamespace(), dst.titleWithoutNamespace(), self.autonomous, editSummary, True)
+ dst = pywikibot.Page(self.site,
+ newTitle)
+ bot = category.CategoryMoveRobot(
+ src.titleWithoutNamespace(),
+ dst.titleWithoutNamespace(),
+ self.autonomous,
+ editSummary, True)
bot.run()
else:
src.move(newTitle, editSummary)
@@ -296,47 +330,76 @@
if err:
newTitle = None
if self.replace:
- newTitle = self.PickTarget(True, title, ltxt, err[1])
+ newTitle = self.PickTarget(
+ True, title, ltxt, err[1])
if newTitle:
if pageObj is None:
- pageObj = wikipedia.Page(self.site, title)
+ pageObj = pywikibot.Page(
+ self.site, title)
pageTxt = pageObj.get()
- msg.append(u'[[%s]] => [[%s]]' % (ltxt, newTitle))
+ msg.append(
+ u'[[%s]] => [[%s]]'
+ % (ltxt, newTitle))
# pageTxt = pageTxt.replace(ltxt, newTitle)
# pageTxt = pageTxt.replace(ltxt[0].lower() + ltxt[1:], newTitle[0].lower() + newTitle[1:])
# pageTxt = pageTxt.replace(ltxt.replace(u' ', '_'), newTitle)
- frmParts = self.wordBreaker.split(ltxt)
- toParts = self.wordBreaker.split(newTitle)
- if len(frmParts) != len(toParts):
- raise ValueError(u'Splitting parts do not match counts')
- for i in range(0, len(frmParts)):
- if len(frmParts[i]) != len(toParts[i]):
- raise ValueError(u'Splitting parts do not match word length')
+ frmParts = self.wordBreaker.split(
+ ltxt)
+ toParts = self.wordBreaker.split(
+ newTitle)
+ if len(frmParts) != len(
+ toParts):
+ raise ValueError(
+u'Splitting parts do not match counts')
+ for i in range(0,
+ len(frmParts)):
+ if len(frmParts[i]) != len(
+ toParts[i]):
+ raise ValueError(
+u'Splitting parts do not match word length')
if len(frmParts[i]) > 0:
- pageTxt = pageTxt.replace(frmParts[i], toParts[i])
- pageTxt = pageTxt.replace(frmParts[i][0].lower() + frmParts[i][1:], toParts[i][0].lower() + toParts[i][1:])
+ pageTxt = pageTxt.replace(
+ frmParts[i], toParts[i])
+ pageTxt = pageTxt.replace(
+ frmParts[i][0].lower() + frmParts[i][1:],
+ toParts[i][0].lower() + toParts[i][1:])
if not newTitle:
if not printed:
- self.WikiLog(u"* [[:%s]]: link to %s" % (title, err[0]))
+ self.WikiLog(
+u"* [[:%s]]: link to %s" % (title, err[0]))
printed = True
else:
- self.WikiLog(u"** link to %s" % err[0])
+ self.WikiLog(
+ u"** link to %s"
+ % err[0])
if pageObj is not None:
- coloredMsg = u', '.join([self.ColorCodeWord(m) for m in msg])
+ coloredMsg = u', '.join(
+ [self.ColorCodeWord(m)
+ for m in msg])
if pageObj.get() == pageTxt:
- self.WikiLog(u"* Error: Text replacement failed in [[:%s]] (%s)" % (title, coloredMsg))
+ self.WikiLog(
+u"* Error: Text replacement failed in [[:%s]] (%s)" % (title, coloredMsg))
else:
- wikipedia.output(u'Case Replacements: %s' % u', '.join(msg))
+ pywikibot.output(
+ u'Case Replacements: %s'
+ % u', '.join(msg))
try:
- pageObj.put(pageTxt, u'%s: %s' % (wikipedia.translate(self.site, self.msgLinkReplacement), u', '.join(msg)))
+ pageObj.put(
+ pageTxt,
+ u'%s: %s'
+ % (pywikibot.translate(
+ self.site,
+ self.msgLinkReplacement),
+ u', '.join(msg)))
except KeyboardInterrupt:
raise
except:
- self.WikiLog(u"* Error: Could not save updated page [[:%s]] (%s)" % (title, coloredMsg))
+ self.WikiLog(
+u"* Error: Could not save updated page [[:%s]] (%s)" % (title, coloredMsg))
count += 1
@@ -352,11 +415,11 @@
except:
if self.apfrom is not None:
- wikipedia.output(u'Exception at Title = %s, Next = %s' % (title, self.apfrom))
+ pywikibot.output(u'Exception at Title = %s, Next = %s' % (title, self.apfrom))
raise
def WikiLog(self, text):
- wikipedia.output(text)
+ pywikibot.output(text)
self.wikilog.write(text + u'\n')
self.wikilog.flush()
@@ -385,7 +448,8 @@
mapLat = {}
found = True
- # See if it would make sense to treat the whole word as either cyrilic or latin
+ # See if it would make sense to treat the whole word as either
+ # cyrilic or latin
mightBeLat = mightBeLcl = True
for l in badWord:
if l in self.localLtr:
@@ -409,7 +473,8 @@
mapLat[badWord] = badWord.translate(self.lclToLatDict)
if mightBeLcl and mightBeLat:
ambigBadWords.add(badWord)
- ambigBadWordsCount += 1 # Cannot do len(ambigBadWords) because they might be duplicates
+ # Cannot do len(ambigBadWords) because they might be duplicates
+ ambigBadWordsCount += 1
count += 1
if not found:
@@ -422,7 +487,8 @@
# We cannot auto-translate - offer a list of suggested words
suggestions = mapLcl.values() + mapLat.values()
if len(suggestions) > 0:
- infoText += u", word suggestions: " + u', '.join([self.ColorCodeWord(t) for t in suggestions])
+ infoText += u", word suggestions: " + u', '.join(
+ [self.ColorCodeWord(t) for t in suggestions])
else:
infoText += u", no suggestions"
else:
@@ -437,12 +503,14 @@
possibleAlternatives.append(title)
infoText += u", convert to " + self.MakeLink(title)
else:
- # Try to pick 0, 1, 2, ..., len(ambiguous words) unique combinations
- # from the bad words list, and convert just the picked words to cyrilic,
- # whereas making all other words as latin character.
+ # Try to pick 0, 1, 2, ..., len(ambiguous words) unique
+ # combinations from the bad words list, and convert just the
+ # picked words to cyrilic, whereas making all other words as
+ # latin character.
for itemCntToPick in range(0, len(ambigBadWords)+1):
title2 = title
- for uc in xuniqueCombinations(list(ambigBadWords), itemCntToPick):
+ for uc in xuniqueCombinations(list(ambigBadWords),
+ itemCntToPick):
wordsToLat = ambigBadWords.copy()
for bw in uc:
title2 = title2.replace(bw, mapLcl[bw])
@@ -452,7 +520,8 @@
possibleAlternatives.append(title2)
if len(possibleAlternatives) > 0:
- infoText += u", can be converted to " + u', '.join([self.MakeLink(t) for t in possibleAlternatives])
+ infoText += u", can be converted to " + u', '.join(
+ [self.MakeLink(t) for t in possibleAlternatives])
else:
infoText += u", no suggestions"
@@ -471,7 +540,7 @@
pagesExist = []
for newTitle in candidates:
- dst = wikipedia.Page(self.site, newTitle)
+ dst = pywikibot.Page(self.site, newTitle)
if not dst.exists():
pagesDontExist.append(newTitle)
elif dst.isRedirectPage():
@@ -496,28 +565,31 @@
return pagesRedir.keys()[0]
if not self.autonomous:
- wikipedia.output(u'Could not auto-decide for page [[%s]]. Which link should be chosen?' % title)
- wikipedia.output(u'Original title: ', newline=False)
+ pywikibot.output(u'Could not auto-decide for page [[%s]]. Which link should be chosen?' % title)
+ pywikibot.output(u'Original title: ', newline=False)
self.ColorCodeWord(original + "\n", True)
count = 1
for t in candidates:
if t in pagesDontExist: msg = u'missing'
elif t in pagesRedir: msg = u'Redirect to ' + pagesRedir[t]
else: msg = u'page exists'
- self.ColorCodeWord(u' %d: %s (%s)\n' % (count, t, msg), True)
+ self.ColorCodeWord(u' %d: %s (%s)\n'
+ % (count, t, msg), True)
count += 1
answers = [str(i) for i in range(0, count)]
- choice = int(wikipedia.inputChoice(u'Which link to choose? (0 to skip)', answers, [a[0] for a in answers]))
+ choice = int(pywikibot.inputChoice(
+ u'Which link to choose? (0 to skip)',
+ answers, [a[0] for a in answers]))
if choice > 0:
return candidates[choice-1]
else:
if len(candidates) == 1:
newTitle = candidates[0]
- dst = wikipedia.Page(self.site, newTitle)
+ dst = pywikibot.Page(self.site, newTitle)
if not dst.exists():
- # choice = wikipedia.inputChoice(u'Move %s to %s?' % (title, newTitle), ['Yes', 'No'], ['y', 'n'])
+ # choice = pywikibot.inputChoice(u'Move %s to %s?' % (title, newTitle), ['Yes', 'No'], ['y', 'n'])
return newTitle
return None
@@ -544,7 +616,7 @@
if toScreen: SetColor(FOREGROUND_RED)
else: res += self.suffixClr + self.latClrFnt
lastIsCyr = False
- if toScreen: wikipedia.output(l, newline=False)
+ if toScreen: pywikibot.output(l, newline=False)
else: res += l
if toScreen: SetColor(FOREGROUND_WHITE)
@@ -559,4 +631,4 @@
bot = CaseChecker()
bot.Run()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/catall.py
===================================================================
--- trunk/pywikipedia/catall.py 2010-06-03 05:30:45 UTC (rev 8239)
+++ trunk/pywikipedia/catall.py 2010-06-03 07:19:33 UTC (rev 8240)
@@ -10,13 +10,15 @@
"""
#
# (C) Rob W.W. Hooft, Andre Engels, 2004
+# (C) Pywikipedia bot team, 2004-2010
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
-import wikipedia, sys
+import wikipedia as pywikibot
+import sys
msg={
'ar':u'بوت: تغيير التصنيفات',
@@ -50,7 +52,7 @@
print ("xx: if the first, remove all categories and add no new.")
print ("q: quit.")
while flag == False:
- choice=wikipedia.input(u"?")
+ choice=pywikibot.input(u"?")
if choice=="":
flag=True
elif choice=="-":
@@ -61,7 +63,7 @@
editor = editarticle.TextEditor()
newtext = editor.edit(pagetext)
elif choice =="??":
- wikipedia.output(pagetext[0:length])
+ pywikibot.output(pagetext[0:length])
length = length+500
elif choice=="xx" and chosen==[]:
chosen = None
@@ -75,18 +77,18 @@
def make_categories(page, list, site = None):
if site is None:
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
pllist=[]
for p in list:
cattitle="%s:%s" % (site.category_namespace(), p)
- pllist.append(wikipedia.Page(site,cattitle))
- page.put_async(wikipedia.replaceCategoryLinks(page.get(), pllist),
- comment=wikipedia.translate(site.lang, msg))
+ pllist.append(pywikibot.Page(site,cattitle))
+ page.put_async(pywikibot.replaceCategoryLinks(page.get(), pllist),
+ comment=pywikibot.translate(site.lang, msg))
docorrections=True
start=[]
-for arg in wikipedia.handleArgs():
+for arg in pywikibot.handleArgs():
if arg == '-onlynew':
docorrections=False
else:
@@ -97,7 +99,7 @@
else:
start=' '.join(start)
-mysite = wikipedia.getSite()
+mysite = pywikibot.getSite()
try:
for p in mysite.allpages(start = start):
@@ -105,7 +107,7 @@
text=p.get()
cats=p.categories()
if cats == []:
- wikipedia.output(u"========== %s ==========" % p.title())
+ pywikibot.output(u"========== %s ==========" % p.title())
print "No categories"
print "----------------------------------------"
newcats=choosecats(text)
@@ -113,16 +115,16 @@
make_categories(p, newcats, mysite)
else:
if docorrections:
- wikipedia.output(u"========== %s ==========" % p.title())
+ pywikibot.output(u"========== %s ==========" % p.title())
for c in cats:
- wikipedia.output(c.title())
+ pywikibot.output(c.title())
print "----------------------------------------"
newcats=choosecats(text)
if newcats is None:
make_categories(p, [], mysite)
elif newcats != []:
make_categories(p, newcats, mysite)
- except wikipedia.IsRedirectPage:
- wikipedia.output(u'%s is a redirect' % p.title())
+ except pywikibot.IsRedirectPage:
+ pywikibot.output(u'%s is a redirect' % p.title())
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/commonscat.py
===================================================================
--- trunk/pywikipedia/commonscat.py 2010-06-03 05:30:45 UTC (rev 8239)
+++ trunk/pywikipedia/commonscat.py 2010-06-03 07:19:33 UTC (rev 8240)
@@ -51,8 +51,10 @@
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
+#
-import wikipedia, config, pagegenerators, add_text, re
+import wikipedia as pywikibot
+import config, pagegenerators, add_text, re
docuReplacements = {
'¶ms;': pagegenerators.parameterHelp
@@ -63,44 +65,54 @@
commonscatTemplates = {
'_default': (u'Commonscat', []),
'af' : (u'CommonsKategorie', [u'commonscat']),
- 'ar' : (u'تصنيف كومنز', [u'Commonscat', u'تصنيف كومونز', u'Commons cat', u'CommonsCat']),
+ 'ar' : (u'تصنيف كومنز', [u'Commonscat', u'تصنيف كومونز',
+ u'Commons cat', u'CommonsCat']),
'az' : (u'CommonsKat', []),
'bn' : (u'কমন্সক্যাট', [u'Commonscat']),
'crh' : (u'CommonsKat', [u'Commonscat']),
'cs' : (u'Commonscat', [u'Commons cat']),
'da' : (u'Commonscat', [u'Commons cat', u'Commonskat', u'Commonscat2']),
'de' : (u'Commonscat', [u'CommonsCat',]),
- 'en' : (u'Commons category', [u'Commoncat', u'Commons2', u'Cms-catlist-up', u'Catlst commons', u'Commonscategory', u'Commonscat', u'Commons cat']),
- 'es' : (u'Commonscat', [u'Ccat', u'Commons cat', u'Categoría Commons', u'Commonscat-inline']),
+ 'en' : (u'Commons category', [u'Commoncat', u'Commons2', u'Cms-catlist-up',
+ u'Catlst commons', u'Commonscategory',
+ u'Commonscat', u'Commons cat']),
+ 'es' : (u'Commonscat', [u'Ccat', u'Commons cat', u'Categoría Commons',
+ u'Commonscat-inline']),
'eu' : (u'Commonskat', [u'Commonscat']),
- 'fa' : (u'انبار-رده', [u'Commonscat', u'Commons cat', u'انبار رده', u'Commons category']),
- 'fr' : (u'Commonscat', [u'CommonsCat', u'Commons cat', u'Commons category']),
+ 'fa' : (u'انبار-رده', [u'Commonscat', u'Commons cat',
+ u'انبار رده', u'Commons category']),
+ 'fr' : (u'Commonscat', [u'CommonsCat', u'Commons cat',
+ u'Commons category']),
'frp' : (u'Commonscat', [u'CommonsCat']),
'ga' : (u'Catcómhaoin', [u'Commonscat']),
'hi' : (u'Commonscat', [u'Commons2', u'Commons cat', u'Commons category']),
'hu' : (u'Közvagyonkat', []),
'hy' : (u'Commons cat', [u'Commonscat']),
- 'id' : (u'Commonscat', [u'Commons cat', u'Commons2', u'CommonsCat', u'Commons category']),
+ 'id' : (u'Commonscat', [u'Commons cat', u'Commons2',
+ u'CommonsCat', u'Commons category']),
'ja' : (u'Commonscat', [u'Commons cat', u'Commons category']),
'jv' : (u'Commonscat', [u'Commons cat']),
'kaa' : (u'Commons cat', [u'Commonscat']),
'kk' : (u'Commonscat', [u'Commons2']),
'ko' : (u'Commonscat', [u'Commons cat', u'공용분류']),
'la' : (u'CommuniaCat', []),
- 'mk' : (u'Ризница-врска', [u'Commonscat', u'Commons cat', u'CommonsCat', u'Commons2', u'Commons category']),
+ 'mk' : (u'Ризница-врска', [u'Commonscat', u'Commons cat', u'CommonsCat',
+ u'Commons2', u'Commons category']),
'ml' : (u'Commonscat', [u'Commons cat', u'Commons2']),
'nn' : (u'Commonscat', [u'Commons cat']),
'os' : (u'Commonscat', [u'Commons cat']),
'pt' : (u'Commonscat', [u'Commons cat']),
'ro' : (u'Commonscat', [u'Commons cat']),
'ru' : (u'Commonscat', [u'Викисклад-кат']),
- 'sl' : (u'Kategorija v Zbirki', [u'Commonscat', u'Kategorija v zbirki', u'Commons cat', u'Katzbirke']),
+ 'sl' : (u'Kategorija v Zbirki', [u'Commonscat', u'Kategorija v zbirki',
+ u'Commons cat', u'Katzbirke']),
'sv' : (u'Commonscat', [u'Commonscat-rad', u'Commonskat', u'Commons cat']),
'sw' : (u'Commonscat', [u'Commons2', u'Commons cat']),
'te' : (u'Commonscat', [u'Commons cat']),
'tr' : (u'CommonsKat', [u'Commonscat', u'Commons cat']),
'uk' : (u'Commonscat', [u'Commons cat', u'Category', u'Commonscat-inline']),
- 'vi' : (u'Commonscat', [u'Commons2', u'Commons cat', u'Commons category', u'Commons+cat']),
+ 'vi' : (u'Commonscat', [u'Commons2', u'Commons cat', u'Commons category',
+ u'Commons+cat']),
'zh' : (u'Commonscat', [u'Commons cat']),
'zh-classical' : (u'共享類', [u'Commonscat']),
'zh-yue' : (u'同享類', [u'Commonscat', u'共享類 ', u'Commons cat']),
@@ -110,10 +122,18 @@
'af' : [u'commons'],
'ar' : [u'تحويلة تصنيف', u'كومنز', u'كومونز', u'Commons'],
'cs' : [u'Commons', u'Sestřičky', u'Sisterlinks'],
- 'da' : [u'Commons', u'Commons left', u'Commons2', u'Commonsbilleder', u'Commonscat left', u'Commonscat2', u'GalleriCommons', u'Søsterlinks'],
+ 'da' : [u'Commons', u'Commons left', u'Commons2', u'Commonsbilleder',
+ u'Commonscat left', u'Commonscat2', u'GalleriCommons',
+ u'Søsterlinks'],
'de' : [u'Commons'],
- 'en' : [u'Category redirect', u'Commons', u'Commonscat1A', u'Commoncats', u'Commonscat4Ra', u'Sisterlinks', u'Sisterlinkswp', u'Tracking category', u'Template category', u'Wikipedia category'],
- 'eo' : [u'Commons', (u'Projekto/box', 'commons='), (u'Projekto', 'commons='), (u'Projektoj', 'commons='), (u'Projektoj', 'commonscat=')],
+ 'en' : [u'Category redirect', u'Commons', u'Commonscat1A', u'Commoncats',
+ u'Commonscat4Ra', u'Sisterlinks', u'Sisterlinkswp',
+ u'Tracking category', u'Template category', u'Wikipedia category'],
+ 'eo' : [u'Commons',
+ (u'Projekto/box', 'commons='),
+ (u'Projekto', 'commons='),
+ (u'Projektoj', 'commons='),
+ (u'Projektoj', 'commonscat=')],
'es' : [u'Commons', u'IprCommonscat'],
'eu' : [u'Commons'],
'fa' : [u'Commons', u'ویکیانبار'],
@@ -124,7 +144,8 @@
'it' : [(u'Ip', 'commons='), (u'Interprogetto', 'commons=')],
'ja' : [u'CommonscatS', u'SisterlinksN', u'Interwikicat'],
'nds-nl' : [u'Commons'],
- 'nl' : [u'Commons', u'Commonsklein', u'Commonscatklein', u'Catbeg', u'Catsjab', u'Catwiki'],
+ 'nl' : [u'Commons', u'Commonsklein', u'Commonscatklein', u'Catbeg',
+ u'Catsjab', u'Catwiki'],
'om' : [u'Commons'],
'pt' : [u'Correlatos'],
'ru' : [u'Навигация'],
@@ -153,16 +174,21 @@
return True
else:
for (inPageTemplate, param) in templatesWithParams:
- if inPageTemplate == template[0] and template[1] in param[0]:
+ if inPageTemplate == template[0] \
+ and template[1] in param[0]:
return True
return False
def updateInterwiki (wikipediaPage = None, commonsPage = None):
'''
- Update the interwiki's at commons from a wikipedia page. The bot just replaces the interwiki links at the commons page with the interwiki's from the wikipedia page.
- This should probably be more intelligent. We could use add all the interwiki's and remove duplicates. Or only remove language links if multiple language links to the same language exist.
+ Update the interwiki's at commons from a wikipedia page. The bot just
+ replaces the interwiki links at the commons page with the interwiki's from
+ the wikipedia page. This should probably be more intelligent. We could use
+ add all the interwiki's and remove duplicates. Or only remove language links
+ if multiple language links to the same language exist.
- This function is disabled for the moment untill i figure out what the best way is to update the interwiki's.
+ This function is disabled for the moment untill i figure out what the best
+ way is to update the interwiki's.
'''
interwikis = {}
comment= u''
@@ -173,75 +199,96 @@
interwikis[interwikiPage.site()]=interwikiPage
oldtext = commonsPage.get()
# The commonssite object doesnt work with interwiki's
- newtext = wikipedia.replaceLanguageLinks(oldtext, interwikis, wikipedia.getSite(u'nl'))
- comment = u'Updating interwiki\'s from [[' + wikipediaPage.site().language() + u':' + wikipediaPage.title() + u']]'
+ newtext = pywikibot.replaceLanguageLinks(oldtext, interwikis,
+ pywikibot.getSite(u'nl'))
+ comment = u'Updating interwiki\'s from [[' + \
+ wikipediaPage.site().language() + \
+ u':' + wikipediaPage.title() + u']]'
if newtext != oldtext:
#This doesnt seem to work. Newtext has some trailing whitespace
- wikipedia.showDiff(oldtext, newtext)
+ pywikibot.showDiff(oldtext, newtext)
commonsPage.put(newtext=newtext, comment=comment)
def addCommonscat (page = None, summary = None, always = False):
'''
Take a page. Go to all the interwiki page looking for a commonscat template.
- When all the interwiki's links are checked and a proper category is found add it to the page.
+ When all the interwiki's links are checked and a proper category is found
+ add it to the page.
'''
- wikipedia.output(u'Working on ' + page.title());
+ pywikibot.output(u'Working on ' + page.title());
#Get the right templates for this page
- primaryCommonscat, commonscatAlternatives=getCommonscatTemplate(page.site().language())
+ primaryCommonscat, commonscatAlternatives = getCommonscatTemplate(
+ page.site().language())
commonscatLink = getCommonscatLink (page)
if commonscatLink:
- wikipedia.output(u'Commonscat template is already on ' + page.title());
+ pywikibot.output(u'Commonscat template is already on %s'
+ % page.title())
(currentCommonscatTemplate, currentCommonscatTarget) = commonscatLink
checkedCommonscatTarget = checkCommonscatLink(currentCommonscatTarget)
if (currentCommonscatTarget==checkedCommonscatTarget):
#The current commonscat link is good
- wikipedia.output(u'Commonscat link at ' + page.title() + u' to Category:' + currentCommonscatTarget + u' is ok');
+ pywikibot.output(u'Commonscat link at %s to Category:%s is ok'
+ % (page.title() , currentCommonscatTarget));
return (True, always)
elif checkedCommonscatTarget!=u'':
#We have a new Commonscat link, replace the old one
- changeCommonscat (page, currentCommonscatTemplate, currentCommonscatTarget, primaryCommonscat, checkedCommonscatTarget)
+ changeCommonscat(page, currentCommonscatTemplate,
+ currentCommonscatTarget, primaryCommonscat,
+ checkedCommonscatTarget)
return (True, always)
else:
#Commonscat link is wrong
commonscatLink = findCommonscatLink(page)
if (commonscatLink!=u''):
- changeCommonscat (page, currentCommonscatTemplate, currentCommonscatTarget, primaryCommonscat, commonscatLink)
+ changeCommonscat (page, currentCommonscatTemplate,
+ currentCommonscatTarget, primaryCommonscat,
+ commonscatLink)
#else
#Should i remove the commonscat link?
elif skipPage(page):
- wikipedia.output("Found a template in the skip list. Skipping " + page.title());
+ pywikibot.output("Found a template in the skip list. Skipping %s"
+ % page.title());
else:
commonscatLink = findCommonscatLink(page)
if (commonscatLink!=u''):
textToAdd = u'{{' + primaryCommonscat + u'|' + commonscatLink + u'}}'
- (success, status, always) = add_text.add_text(page, textToAdd, summary, None, None, always);
+ (success, status, always) = add_text.add_text(page, textToAdd,
+ summary, None, None,
+ always);
return (True, always);
return (True, always);
-def changeCommonscat (page = None, oldtemplate = u'', oldcat = u'', newtemplate = u'', newcat = u''):
+def changeCommonscat (page=None, oldtemplate=u'', oldcat=u'', newtemplate=u'',
+ newcat=u''):
'''
Change the current commonscat template and target.
'''
- newtext = re.sub(u'(?i)\{\{' + oldtemplate + u'\|?[^}]*\}\}', u'{{' + newtemplate + u'|' + newcat + u'}}', page.get())
+ newtext = re.sub(u'(?i)\{\{' + oldtemplate + u'\|?[^}]*\}\}',
+ u'{{' + newtemplate + u'|' + newcat + u'}}',
+ page.get())
comment = u'Changing commonscat link from [[:Commons:Category:' + oldcat + u'|' + oldcat + u']] to [[:Commons:Category:' + newcat + u'|' + newcat + u']]'
- wikipedia.showDiff(page.get(), newtext)
+ pywikibot.showDiff(page.get(), newtext)
page.put(newtext, comment)
def findCommonscatLink (page=None):
for ipage in page.interwiki():
try:
- if(ipage.exists() and not ipage.isRedirectPage() and not ipage.isDisambig()):
+ if(ipage.exists() and not ipage.isRedirectPage()
+ and not ipage.isDisambig()):
commonscatLink = getCommonscatLink (ipage)
if commonscatLink:
(currentCommonscatTemplate, possibleCommonscat) = commonscatLink
checkedCommonscat = checkCommonscatLink(possibleCommonscat)
if (checkedCommonscat!= u''):
- wikipedia.output("Found link for " + page.title() + " at [[" + ipage.site().language() + ":" + ipage.title() + "]] to " + checkedCommonscat + ".")
+ pywikibot.output(
+ u"Found link for %s at [[%s:%s]] to %s."
+ % (page.title(), ipage.site().language(),
+ ipage.title(), checkedCommonscat))
return checkedCommonscat
- except wikipedia.BadTitle:
+ except pywikibot.BadTitle:
#The interwiki was incorrect
return u''
return u''
@@ -251,13 +298,15 @@
'''
Go through the page and return a tuple of (<templatename>, <target>)
'''
- primaryCommonscat, commonscatAlternatives=getCommonscatTemplate(wikipediaPage.site().language())
+ primaryCommonscat, commonscatAlternatives = getCommonscatTemplate(
+ wikipediaPage.site().language())
commonscatTemplate =u''
commonscatTarget = u''
#See if commonscat is present
for template in wikipediaPage.templatesWithParams():
- if template[0]==primaryCommonscat or template[0] in commonscatAlternatives:
+ if template[0]==primaryCommonscat \
+ or template[0] in commonscatAlternatives:
commonscatTemplate = template[0]
if (len(template[1]) > 0):
commonscatTarget = template[1][0]
@@ -273,33 +322,38 @@
If the page is a redirect this function tries to follow it.
If the page doesnt exists the function will return an empty string
'''
- if wikipedia.verbose:
- wikipedia.output("getCommonscat: " + name );
+ if pywikibot.verbose:
+ pywikibot.output("getCommonscat: " + name )
try:
- #This can throw a wikipedia.BadTitle
- commonsPage = wikipedia.Page(wikipedia.getSite("commons", "commons"), "Category:" + name);
+ #This can throw a pywikibot.BadTitle
+ commonsPage = pywikibot.Page(pywikibot.getSite("commons", "commons"),
+ "Category:" + name)
if not commonsPage.exists():
- if wikipedia.verbose:
- wikipedia.output("getCommonscat: The category doesnt exist.");
+ if pywikibot.verbose:
+ pywikibot.output(u"getCommonscat: The category doesnt exist.")
return u''
elif commonsPage.isRedirectPage():
- if wikipedia.verbose:
- wikipedia.output("getCommonscat: The category is a redirect");
- return checkCommonscatLink(commonsPage.getRedirectTarget().titleWithoutNamespace());
+ if pywikibot.verbose:
+ pywikibot.output(u"getCommonscat: The category is a redirect")
+ return checkCommonscatLink(
+ commonsPage.getRedirectTarget().titleWithoutNamespace())
elif "Category redirect" in commonsPage.templates():
- if wikipedia.verbose:
- wikipedia.output("getCommonscat: The category is a category redirect");
+ if pywikibot.verbose:
+ pywikibot.output(
+ u"getCommonscat: The category is a category redirect")
for template in commonsPage.templatesWithParams():
- if ((template[0]=="Category redirect") and (len(template[1]) > 0)):
+ if ((template[0]=="Category redirect")
+ and (len(template[1]) > 0)):
return checkCommonscatLink(template[1][0])
elif commonsPage.isDisambig():
- if wikipedia.verbose:
- wikipedia.output("getCommonscat: The category is disambiguation");
+ if pywikibot.verbose:
+ pywikibot.output(
+ u"getCommonscat: The category is disambiguation")
return u''
else:
return commonsPage.titleWithoutNamespace()
- except wikipedia.BadTitle:
+ except pywikibot.BadTitle:
#Funky title so not correct
return u''
@@ -314,16 +368,21 @@
# Load a lot of default generators
genFactory = pagegenerators.GeneratorFactory()
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg.startswith('-summary'):
if len(arg) == 8:
- summary = wikipedia.input(u'What summary do you want to use?')
+ summary = pywikibot.input(u'What summary do you want to use?')
else:
summary = arg[9:]
elif arg.startswith('-checkcurrent'):
checkcurrent = True
- primaryCommonscat, commonscatAlternatives = getCommonscatTemplate(wikipedia.getSite().language())
- generator = pagegenerators.NamespaceFilterPageGenerator(pagegenerators.ReferringPageGenerator(wikipedia.Page(wikipedia.getSite(), u'Template:' + primaryCommonscat), onlyTemplateInclusion=True), ns)
+ primaryCommonscat, commonscatAlternatives = getCommonscatTemplate(
+ pywikibot.getSite().language())
+ generator = pagegenerators.NamespaceFilterPageGenerator(
+ pagegenerators.ReferringPageGenerator(
+ pywikibot.Page(pywikibot.getSite(),
+ u'Template:' + primaryCommonscat),
+ onlyTemplateInclusion=True), ns)
elif arg == '-always':
always = True
@@ -333,19 +392,23 @@
if not generator:
generator = genFactory.getCombinedGenerator()
if not generator:
- raise add_text.NoEnoughData('You have to specify the generator you want to use for the script!')
+ raise add_text.NoEnoughData(
+u'You have to specify the generator you want to use for the script!')
pregenerator = pagegenerators.PreloadingGenerator(generator)
for page in pregenerator:
if not page.exists():
- wikipedia.output(u'Page %s does not exist. Skipping.' % page.aslink())
+ pywikibot.output(u'Page %s does not exist. Skipping.'
+ % page.aslink())
elif page.isRedirectPage():
- wikipedia.output(u'Page %s is a redirect. Skipping.' % page.aslink())
+ pywikibot.output(u'Page %s is a redirect. Skipping.' % page.aslink())
elif page.isCategoryRedirect():
- wikipedia.output(u'Page %s is a category redirect. Skipping.' % page.aslink())
+ pywikibot.output(u'Page %s is a category redirect. Skipping.'
+ % page.aslink())
elif page.isDisambig():
- wikipedia.output(u'Page %s is a disambiguation. Skipping.' % page.aslink())
+ pywikibot.output(u'Page %s is a disambiguation. Skipping.'
+ % page.aslink())
else:
(status, always) = addCommonscat(page, summary, always)
@@ -353,4 +416,4 @@
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Revision: 8239
Author: xqt
Date: 2010-06-03 05:30:45 +0000 (Thu, 03 Jun 2010)
Log Message:
-----------
solve_disambiguation.py: update from rewrite r7986, adds a command to tag the ambiguous link with the Disambiguation Needed template (patch bug #2919950)
Modified Paths:
--------------
branches/rewrite/scripts/solve_disambiguation.py
trunk/pywikipedia/solve_disambiguation.py
Modified: branches/rewrite/scripts/solve_disambiguation.py
===================================================================
--- branches/rewrite/scripts/solve_disambiguation.py 2010-06-02 13:50:48 UTC (rev 8238)
+++ branches/rewrite/scripts/solve_disambiguation.py 2010-06-03 05:30:45 UTC (rev 8239)
@@ -86,8 +86,9 @@
# Application specific imports
import pywikibot
-from pywikibot import config, pagegenerators
from scripts import editarticle
+from pywikibot import pagegenerators
+from pywikibot import config
# Summary message when working on disambiguation pages
msg = {
@@ -501,8 +502,8 @@
self.ignorelist = []
filename = config.datafilepath(
- 'disambiguations',
- self.disambPage.title(as_filename=True) + '.txt')
+ 'disambiguations',
+ self.disambPage.title(as_filename=True) + '.txt')
try:
# The file is stored in the disambiguation/ subdir.
# Create if necessary.
@@ -525,8 +526,8 @@
if self.enabled:
# Skip this occurence next time.
filename = config.datafilepath(
- 'disambiguations',
- self.disambPage.title(asUrl=True) + '.txt')
+ 'disambiguations',
+ self.disambPage.title(asUrl=True) + '.txt')
try:
# Open file for appending. If none exists yet, create a new one.
f = codecs.open(filename, 'a', 'utf-8')
@@ -650,7 +651,7 @@
text=refPage.get(throttle=False)
ignoreReason = self.checkContents(text)
if ignoreReason:
- pywikibot.output('\n\nSkipping %s because it contains %s.\n'
+ pywikibot.output('\n\nSkipping %s because it contains %s.\n\n'
% (refPage.title(), ignoreReason))
else:
include = True
@@ -663,8 +664,8 @@
u'Do you want to make redirect %s point to %s?'
% (refPage.title(), target), ['yes', 'no'], ['y', 'N'], 'N')
if choice == 'y':
- redir_text = '#%s [[%s]]' % (
- self.mysite.redirect(default=True), target)
+ redir_text = '#%s [[%s]]' \
+ % (self.mysite.redirect(default=True), target)
try:
refPage.put_async(redir_text,comment=self.comment)
except pywikibot.PageNotSaved, error:
@@ -672,8 +673,8 @@
else:
choice = pywikibot.inputChoice(
u'Do you want to work on pages linking to %s?'
- % refPage.title(), ['yes', 'no', 'change redirect'],
- ['y', 'N', 'c'], 'N')
+ % refPage.title(), ['yes', 'no', 'change redirect'],
+ ['y', 'N', 'c'], 'N')
if choice == 'y':
gen = ReferringPageGeneratorWithIgnore(refPage,
self.primary)
@@ -718,7 +719,7 @@
# ignore interwiki links
if foundlink.site != disambPage.site:
continue
- # check whether the link found is to disambPage
+ # Check whether the link found is to disambPage.
try:
if foundlink.canonical_title() != disambPage.title():
continue
@@ -742,7 +743,7 @@
# Highlight the title in purple.
pywikibot.output(
u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
- % refPage.title())
+ % refPage.title())
if not self.always:
# at the beginning of the link, start red color.
@@ -756,12 +757,12 @@
if edited:
choice = pywikibot.input(
u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n"
-u" ?=tag with " + dn_template_str + ",\n"
+u" [t]ag template " + dn_template_str + ",\n"
u" [m]ore context, [l]ist, [a]dd new, x=save in this form):")
else:
choice = pywikibot.input(
u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n"
-u" ?=tag with " + dn_template_str + ",\n"
+u" [t]ag template " + dn_template_str + ",\n"
u" [m]ore context, show [d]isambiguation page, [l]ist, [a]dd new):")
else:
choice = self.always
@@ -789,7 +790,7 @@
disambigText = editor.edit(
disambPage.get(),
jumpIndex=m.start(),
- highlight = disambPage.title())
+ highlight=disambPage.title())
elif choice in ['l', 'L']:
self.listAlternatives()
elif choice in ['m', 'M']:
@@ -835,8 +836,8 @@
trailing_chars = m.group('linktrail')
if trailing_chars:
link_text += trailing_chars
-
- if choice in ['?', '/']:
+ # '?', '/' for old choice
+ if choice in ['t', 'T', '?', '/']:
#small chunk of text to search
search_text = text[m.end() : m.end() + context]
#figure out where the link (and sentance) ends, put note there
@@ -887,7 +888,7 @@
repPl = pywikibot.Page(pywikibot.Link(new_page_title,
disambPage.site))
if (new_page_title[0].isupper()
- or link_text[0].isupper()):
+ or link_text[0].isupper()):
new_page_title = repPl.title()
else:
new_page_title = repPl.title()
@@ -905,11 +906,11 @@
# check if we can create a link with trailing characters
# instead of a pipelink
elif len(new_page_title) <= len(link_text) \
- and firstcap(link_text[:len(new_page_title)]) \
- == firstcap(new_page_title) \
- and re.sub(self.trailR, '',
- link_text[len(new_page_title):]) == '' \
- and not section:
+ and firstcap(link_text[:len(new_page_title)]) \
+ == firstcap(new_page_title) \
+ and re.sub(self.trailR, '',
+ link_text[len(new_page_title):]) == '' \
+ and not section:
newlink = "[[%s]]%s" \
% (link_text[:len(new_page_title)],
link_text[len(new_page_title):])
@@ -939,26 +940,26 @@
def findAlternatives(self, disambPage):
if disambPage.isRedirectPage() and not self.primary:
if (disambPage.site.lang in self.primary_redir_template
- and self.primary_redir_template[disambPage.site.lang]
- in disambPage.templates(get_redirect = True)):
+ and self.primary_redir_template[disambPage.site.lang]
+ in disambPage.templates(get_redirect = True)):
baseTerm = disambPage.title()
for template in disambPage.templatesWithParams(
- get_redirect=True):
+ get_redirect=True):
if template[0] == self.primary_redir_template[
- disambPage.site.lang] \
- and len(template[1]) > 0:
+ disambPage.site.lang] \
+ and len(template[1]) > 0:
baseTerm = template[1][1]
disambTitle = primary_topic_format[self.mylang] % baseTerm
try:
disambPage2 = pywikibot.Page(
- pywikibot.Link(disambTitle, self.mysite))
+ pywikibot.Link(disambTitle, self.mysite))
links = disambPage2.linkedPages()
links = [correctcap(l,disambPage2.get()) for l in links]
except pywikibot.NoPage:
pywikibot.output(u"No page at %s, using redirect target."
% disambTitle)
links = disambPage.linkedPages()[:1]
- links = [correctcap(l, disambPage.get(get_redirect = True))
+ links = [correctcap(l, disambPage.get(get_redirect=True))
for l in links]
self.alternatives += links
else:
@@ -983,16 +984,16 @@
if self.primary:
try:
disambPage2 = pywikibot.Page(
- pywikibot.Link(
- primary_topic_format[self.mylang]
- % disambPage.title(),
- self.mysite))
+ pywikibot.Link(
+ primary_topic_format[self.mylang]
+ % disambPage.title(),
+ self.mysite))
links = disambPage2.linkedPages()
links = [correctcap(l, disambPage2.get())
for l in links]
except pywikibot.NoPage:
pywikibot.output(
- u"Page does not exist, using the first link in page %s."
+u"Page does not exist, using the first link in page %s."
% disambPage.title())
links = disambPage.linkedPages()[:1]
links = [correctcap(l, disambPage.get())
@@ -1000,7 +1001,7 @@
else:
try:
links = disambPage.linkedPages()
- links = [correctcap(l ,disambPage.get())
+ links = [correctcap(l, disambPage.get())
for l in links]
except pywikibot.NoPage:
pywikibot.output(u"Page does not exist, skipping.")
@@ -1025,44 +1026,44 @@
# first check whether user has customized the edit comment
if (self.mysite.family.name in config.disambiguation_comment
- and self.mylang in config.disambiguation_comment
+ and self.mylang in config.disambiguation_comment
[self.mysite.family.name]):
try:
self.comment = pywikibot.translate(
- self.mysite,
- config.disambiguation_comment
- [self.mysite.family.name]
- ) % (disambPage.title(), targets)
+ self.mysite,
+ config.disambiguation_comment[
+ self.mysite.family.name]
+ ) % (disambPage.title(), targets)
# Backwards compatibility, type error probably caused by too
# many arguments for format string
except TypeError:
self.comment = pywikibot.translate(
- self.mysite,
- config.disambiguation_comment
- [self.mysite.family.name]
- ) % disambPage.title()
+ self.mysite,
+ config.disambiguation_comment[
+ self.mysite.family.name]
+ ) % disambPage.title()
elif disambPage.isRedirectPage():
# when working on redirects, there's another summary message
if unlink and not new_targets:
self.comment = pywikibot.translate(self.mysite,
msg_redir_unlink) \
- % disambPage.title()
+ % disambPage.title()
elif dn and not new_targets:
self.comment = pywikibot.translate(self.mysite, msg_redir_dn) \
- % disambPage.title()
+ % disambPage.title()
else:
self.comment = pywikibot.translate(self.mysite, msg_redir) \
- % (disambPage.title(), targets)
+ % (disambPage.title(), targets)
else:
if unlink and not new_targets:
self.comment = pywikibot.translate(self.mysite, msg_unlink) \
- % disambPage.title()
+ % disambPage.title()
elif dn and not new_targets:
self.comment = pywikibot.translate(self.mysite, msg_dn) \
- % disambPage.title()
+ % disambPage.title()
else:
self.comment = pywikibot.translate(self.mysite, msg) \
- % (disambPage.title(), targets)
+ % (disambPage.title(), targets)
def run(self):
if self.main_only:
@@ -1133,10 +1134,10 @@
elif arg.startswith('-file'):
if len(arg) == 5:
generator = pagegenerators.TextfilePageGenerator(
- filename = None)
+ filename=None)
else:
generator = pagegenerators.TextfilePageGenerator(
- filename = arg[6:])
+ filename=arg[6:])
elif arg.startswith('-pos:'):
if arg[5]!=':':
mysite = pywikibot.getSite()
@@ -1167,7 +1168,7 @@
else:
generator = pagegenerators.CategorizedPageGenerator(
pywikibot.getSite().disambcategory(),
- start = arg[7:])
+ start=arg[7:])
generator = pagegenerators.NamespaceFilterPageGenerator(
generator, [0])
except pywikibot.NoPage:
Modified: trunk/pywikipedia/solve_disambiguation.py
===================================================================
--- trunk/pywikipedia/solve_disambiguation.py 2010-06-02 13:50:48 UTC (rev 8238)
+++ trunk/pywikipedia/solve_disambiguation.py 2010-06-03 05:30:45 UTC (rev 8239)
@@ -27,6 +27,9 @@
-just only use the alternatives given on the command line, do not
read the page for other possibilities
+ -dnskip Skip links already marked with a disambiguation-needed
+ template (e.g., {{dn}})
+
-primary "primary topic" disambiguation (Begriffsklärung nach Modell 2).
That's titles where one topic is much more important, the
disambiguation page is saved somewhere else, and the important
@@ -55,7 +58,7 @@
wiki that is defined (to the bot) as the category containing
disambiguation pages, starting at XY. If only '-start' or
'-start:' is given, it starts at the beginning.
-
+
-min:XX (XX being a number) only work on disambiguation pages for which
at least XX are to be worked on.
@@ -83,7 +86,8 @@
# Application specific imports
import wikipedia as pywikibot
-import pagegenerators, editarticle
+import editarticle
+import pagegenerators
# Summary message when working on disambiguation pages
msg = {
@@ -190,6 +194,21 @@
'uk': u'Виправлення посилання на багатозначність за допомогою бота: %s вилучено',
}
+# Disambiguation Needed template
+dn_template = {
+ 'en' : u'{{dn}}',
+ }
+
+# Summary message when adding Disambiguation Needed template
+msg_dn = {
+ 'en' : u'Robot-assisted disambiguation: %s - Marked as needing expert attention',
+ }
+
+# Summary message when adding Disambiguation Needed template to a redirect link
+msg_redir_dn = {
+ 'en' : u'Robot-assisted disambiguation: %s - Marked as needing expert attention',
+ }
+
# Summary message to (unknown)
unknown_msg = {
'ar' : u'(غير معروف)',
@@ -426,7 +445,8 @@
return string[0].upper()+string[1:]
def correctcap(link, text):
- # If text links to a page with title link uncapitalized, uncapitalize link, otherwise capitalize it
+ # If text links to a page with title link uncapitalized, uncapitalize link,
+ # otherwise capitalize it
linkupper = link.title()
linklower = linkupper[0].lower() + linkupper[1:]
if "[[%s]]"%linklower in text or "[[%s|"%linklower in text:
@@ -440,13 +460,18 @@
self.minimum = minimum
def __iter__(self):
- generator = pagegenerators.ReferringPageGenerator(self.disambPage, followRedirects = False, withTemplateInclusion = False)
- generator = pagegenerators.PageTitleFilterPageGenerator(generator, ignore_title)
+ generator = pagegenerators.ReferringPageGenerator(
+ self.disambPage,
+ followRedirects=False,
+ withTemplateInclusion=False)
+ generator = pagegenerators.PageTitleFilterPageGenerator(generator,
+ ignore_title)
refs = [page for page in generator]
if len(refs) < self.minimum:
- pywikibot.output(u"Found only %d pages to work on; skipping." % len(refs))
+ pywikibot.output(u"Found only %d pages to work on; skipping."
+ % len(refs))
return
pywikibot.output(u"Will work on %d pages." % len(refs))
for ref in refs:
@@ -463,8 +488,9 @@
self.enabled = enabled
self.ignorelist = []
- filename = pywikibot.config.datafilepath('disambiguations',
- self.disambPage.titleForFilename() + '.txt')
+ filename = pywikibot.config.datafilepath(
+ 'disambiguations',
+ self.disambPage.titleForFilename() + '.txt')
try:
# The file is stored in the disambiguation/ subdir.
# Create if necessary.
@@ -486,11 +512,11 @@
def ignore(self, refPage):
if self.enabled:
# Skip this occurence next time.
- filename = pywikibot.config.datafilepath('disambiguations',
- self.disambPage.urlname() + '.txt')
+ filename = pywikibot.config.datafilepath(
+ 'disambiguations',
+ self.disambPage.urlname() + '.txt')
try:
# Open file for appending. If none exists yet, create a new one.
- # The file is stored in the disambiguation/ subdir. Create if necessary.
f = codecs.open(filename, 'a', 'utf-8')
f.write(refPage.urlname() + '\n')
f.close()
@@ -515,16 +541,18 @@
u'{{[Pp]rocessing}}',
),
}
-
+
primary_redir_template = {
# Page.templates() format, first letter uppercase
'hu': u'Egyért-redir',
}
-
- def __init__(self, always, alternatives, getAlternatives, generator, primary, main_only, minimum = 0):
+
+ def __init__(self, always, alternatives, getAlternatives, dnSkip, generator,
+ primary, main_only, minimum = 0):
self.always = always
self.alternatives = alternatives
self.getAlternatives = getAlternatives
+ self.dnSkip = dnSkip
self.generator = generator
self.primary = primary
self.main_only = main_only
@@ -572,11 +600,16 @@
linktrail = self.mysite.linktrail()
self.trailR = re.compile(linktrail)
- # The regular expression which finds links. Results consist of four groups:
- # group title is the target page title, that is, everything before | or ].
- # group section is the page section. It'll include the # to make life easier for us.
- # group label is the alternative link title, that's everything between | and ].
- # group linktrail is the link trail, that's letters after ]] which are part of the word.
+ # The regular expression which finds links. Results consist of four
+ # groups:
+ # group title is the target page title, that is, everything before
+ # | or ].
+ # group section is the page section. It'll include the # to make life
+ # easier for us.
+ # group label is the alternative link title, that's everything
+ # between | and ].
+ # group linktrail is the link trail, that's letters after ]] which
+ # are part of the word.
# note that the definition of 'letter' varies from language to language.
self.linkR = re.compile(r'\[\[(?P<title>[^\]\|#]*)(?P<section>#[^\]\|]*)?(\|(?P<label>[^\]]*))?\]\](?P<linktrail>' + linktrail + ')')
@@ -588,10 +621,11 @@
refPage - A page linking to disambPage
Returns False if the user pressed q to completely quit the program.
Otherwise, returns True.
-
+
"""
# TODO: break this function up into subroutines!
+ dn_template_str = pywikibot.translate(self.mysite, dn_template)
include = False
unlink = False
new_targets = []
@@ -599,24 +633,33 @@
text=refPage.get(throttle=False)
ignoreReason = self.checkContents(text)
if ignoreReason:
- pywikibot.output('\n\nSkipping %s because it contains %s.\n\n' % (refPage.title(), ignoreReason))
+ pywikibot.output('\n\nSkipping %s because it contains %s.\n\n'
+ % (refPage.title(), ignoreReason))
else:
include = True
except pywikibot.IsRedirectPage:
- pywikibot.output(u'%s is a redirect to %s' % (refPage.title(), disambPage.title()))
+ pywikibot.output(u'%s is a redirect to %s'
+ % (refPage.title(), disambPage.title()))
if disambPage.isRedirectPage():
target = self.alternatives[0]
- choice = pywikibot.inputChoice(u'Do you want to make redirect %s point to %s?' % (refPage.title(), target), ['yes', 'no'], ['y', 'N'], 'N')
+ choice = pywikibot.inputChoice(
+ u'Do you want to make redirect %s point to %s?'
+ % (refPage.title(), target), ['yes', 'no'], ['y', 'N'], 'N')
if choice == 'y':
- redir_text = '#%s [[%s]]' % (self.mysite.redirect(default=True), target)
+ redir_text = '#%s [[%s]]' \
+ % (self.mysite.redirect(default=True), target)
try:
refPage.put_async(redir_text,comment=self.comment)
except pywikibot.PageNotSaved, error:
pywikibot.output(u'Page not saved: %s' % error.args)
else:
- choice = pywikibot.inputChoice(u'Do you want to work on pages linking to %s?' % refPage.title(), ['yes', 'no', 'change redirect'], ['y', 'N', 'c'], 'N')
+ choice = pywikibot.inputChoice(
+ u'Do you want to work on pages linking to %s?'
+ % refPage.title(), ['yes', 'no', 'change redirect'],
+ ['y', 'N', 'c'], 'N')
if choice == 'y':
- gen = ReferringPageGeneratorWithIgnore(refPage, self.primary)
+ gen = ReferringPageGeneratorWithIgnore(refPage,
+ self.primary)
preloadingGen = pagegenerators.PreloadingGenerator(gen)
for refPage2 in preloadingGen:
# run until the user selected 'quit'
@@ -626,20 +669,24 @@
text=refPage.get(throttle=False,get_redirect=True)
include = "redirect"
except pywikibot.NoPage:
- pywikibot.output(u'Page [[%s]] does not seem to exist?! Skipping.' % refPage.title())
+ pywikibot.output(
+ u'Page [[%s]] does not seem to exist?! Skipping.'
+ % refPage.title())
include = False
if include in (True, "redirect"):
# make a backup of the original text so we can show the changes later
original_text = text
n = 0
curpos = 0
+ dn = False
edited = False
# This loop will run until we have finished the current page
while True:
m = self.linkR.search(text, pos = curpos)
if not m:
if n == 0:
- pywikibot.output(u"No changes necessary in %s" % refPage.title())
+ pywikibot.output(u"No changes necessary in %s"
+ % refPage.title())
return True
else:
# stop loop and save page
@@ -661,23 +708,39 @@
n += 1
# how many bytes should be displayed around the current link
context = 60
+ #there's a {{dn}} here already
+ already_dn = text[m.end() : m.end() + 8].find(dn_template_str[:4]) > -1
+ if already_dn and self.dnSkip:
+ continue
+
# This loop will run while the user doesn't choose an option
# that will actually change the page
while True:
# Show the title of the page where the link was found.
# Highlight the title in purple.
- pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % refPage.title())
+ pywikibot.output(
+ u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
+ % refPage.title())
if not self.always:
# at the beginning of the link, start red color.
# at the end of the link, reset the color to default
- pywikibot.output(text[max(0, m.start() - context) : m.start()] + '\03{lightred}' + text[m.start() : m.end()] + '\03{default}' + text[m.end() : m.end() + context])
+ pywikibot.output(text[max(0, m.start() - context)
+ : m.start()]
+ + '\03{lightred}'
+ + text[m.start() : m.end()]
+ + '\03{default}'
+ + text[m.end() : m.end() + context])
if edited:
- choice = pywikibot.input(u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit\n"
- " [m]ore context, [l]ist, [a]dd new, x=save in this form):")
+ choice = pywikibot.input(
+u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n"
+u" [t]ag template " + dn_template_str + ",\n"
+u" [m]ore context, [l]ist, [a]dd new, x=save in this form):")
else:
- choice = pywikibot.input(u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit\n"
- " [m]ore context, show [d]isambiguation page, [l]ist, [a]dd new):")
+ choice = pywikibot.input(
+u"Option (#, r#, [s]kip link, [e]dit page, [n]ext page, [u]nlink, [q]uit,\n"
+u" [t]ag template " + dn_template_str + ",\n"
+u" [m]ore context, show [d]isambiguation page, [l]ist, [a]dd new):")
else:
choice = self.always
if choice in ['a', 'A']:
@@ -686,7 +749,8 @@
self.listAlternatives()
elif choice in ['e', 'E']:
editor = editarticle.TextEditor()
- newText = editor.edit(text, jumpIndex = m.start(), highlight = disambPage.title())
+ newText = editor.edit(text, jumpIndex=m.start(),
+ highlight=disambPage.title())
# if user didn't press Cancel
if newText and newText != text:
text = newText
@@ -695,9 +759,15 @@
editor = editarticle.TextEditor()
if disambPage.isRedirectPage():
disambredir = disambPage.getRedirectTarget()
- disambigText = editor.edit(disambredir.get(), jumpIndex = m.start(), highlight = disambredir.title())
+ disambigText = editor.edit(
+ disambredir.get(),
+ jumpIndex=m.start(),
+ highlight=disambredir.title())
else:
- disambigText = editor.edit(disambPage.get(), jumpIndex = m.start(), highlight = disambPage.title())
+ disambigText = editor.edit(
+ disambPage.get(),
+ jumpIndex=m.start(),
+ highlight=disambPage.title())
elif choice in ['l', 'L']:
self.listAlternatives()
elif choice in ['m', 'M']:
@@ -743,8 +813,22 @@
trailing_chars = m.group('linktrail')
if trailing_chars:
link_text += trailing_chars
-
- if choice in ['u', 'U']:
+ # '?', '/' for old choice
+ if choice in ['t', 'T', '?', '/']:
+ #small chunk of text to search
+ search_text = text[m.end() : m.end() + context]
+ #figure out where the link (and sentance) ends, put note there
+ end_of_word_match = re.search("\s", search_text)
+ if end_of_word_match:
+ position_split = end_of_word_match.start(0)
+ else:
+ position_split = 0
+ #insert dab needed template
+ text = text[:m.end() + position_split] + dn_template_str \
+ + text[m.end() + position_split:]
+ dn = True
+ continue
+ elif choice in ['u', 'U']:
# unlink - we remove the section if there's any
text = text[:m.start()] + link_text + text[m.end():]
unlink = True
@@ -768,7 +852,9 @@
curpos -= 1
continue
if choice >= len(self.alternatives) or choice < 0:
- pywikibot.output(u"Choice out of range. Please select a number between 0 and %i." % (len(self.alternatives) - 1))
+ pywikibot.output(
+u"Choice out of range. Please select a number between 0 and %i."
+ % (len(self.alternatives) - 1))
# show list of possible choices
self.listAlternatives()
# step back to ask the user again what to do with the
@@ -777,22 +863,36 @@
continue
new_page_title = self.alternatives[choice]
repPl = pywikibot.Page(disambPage.site(), new_page_title)
- if (new_page_title[0].isupper()) or (link_text[0].isupper()):
+ if (new_page_title[0].isupper()
+ or link_text[0].isupper()):
new_page_title = repPl.title()
else:
new_page_title = repPl.title()
- new_page_title = new_page_title[0].lower() + new_page_title[1:]
+ new_page_title = new_page_title[0].lower() \
+ + new_page_title[1:]
if new_page_title not in new_targets:
new_targets.append(new_page_title)
if replaceit and trailing_chars:
- newlink = "[[%s%s]]%s" % (new_page_title, section, trailing_chars)
- elif replaceit or (new_page_title == link_text and not section):
+ newlink = "[[%s%s]]%s" % (new_page_title,
+ section,
+ trailing_chars)
+ elif replaceit or (new_page_title == link_text
+ and not section):
newlink = "[[%s]]" % new_page_title
- # check if we can create a link with trailing characters instead of a pipelink
- elif len(new_page_title) <= len(link_text) and firstcap(link_text[:len(new_page_title)]) == firstcap(new_page_title) and re.sub(self.trailR, '', link_text[len(new_page_title):]) == '' and not section:
- newlink = "[[%s]]%s" % (link_text[:len(new_page_title)], link_text[len(new_page_title):])
+ # check if we can create a link with trailing characters
+ # instead of a pipelink
+ elif len(new_page_title) <= len(link_text) \
+ and firstcap(link_text[:len(new_page_title)]) \
+ == firstcap(new_page_title) \
+ and re.sub(self.trailR, '',
+ link_text[len(new_page_title):]) == '' \
+ and not section:
+ newlink = "[[%s]]%s" \
+ % (link_text[:len(new_page_title)],
+ link_text[len(new_page_title):])
else:
- newlink = "[[%s%s|%s]]" % (new_page_title, section, link_text)
+ newlink = "[[%s%s|%s]]" \
+ % (new_page_title, section, link_text)
text = text[:m.start()] + newlink + text[m.end():]
continue
@@ -804,7 +904,7 @@
pywikibot.showDiff(original_text, text)
pywikibot.output(u'')
# save the page
- self.setSummaryMessage(disambPage, new_targets, unlink)
+ self.setSummaryMessage(disambPage, new_targets, unlink, dn)
try:
refPage.put_async(text,comment=self.comment)
except pywikibot.LockedPage:
@@ -815,20 +915,28 @@
def findAlternatives(self, disambPage):
if disambPage.isRedirectPage() and not self.primary:
- if disambPage.site().lang in self.primary_redir_template and self.primary_redir_template[disambPage.site().lang] in disambPage.templates(get_redirect = True):
+ if (disambPage.site().lang in self.primary_redir_template
+ and self.primary_redir_template[disambPage.site().lang]
+ in disambPage.templates(get_redirect = True)):
baseTerm = disambPage.title()
- for template in disambPage.templatesWithParams(get_redirect = True):
- if template[0] == self.primary_redir_template[disambPage.site().lang] and len(template[1]) > 0:
+ for template in disambPage.templatesWithParams(
+ get_redirect=True):
+ if template[0] == self.primary_redir_template[
+ disambPage.site().lang] \
+ and len(template[1]) > 0:
baseTerm = template[1][1]
disambTitle = primary_topic_format[self.mylang] % baseTerm
try:
- disambPage2 = pywikibot.Page(self.mysite, disambTitle)
+ disambPage2 = pywikibot.Page(
+ self.mysite, disambTitle)
links = disambPage2.linkedPages()
links = [correctcap(l,disambPage2.get()) for l in links]
except pywikibot.NoPage:
- pywikibot.output(u"No page at %s, using redirect target." % disambTitle)
+ pywikibot.output(u"No page at %s, using redirect target."
+ % disambTitle)
links = disambPage.linkedPages()[:1]
- links = [correctcap(l,disambPage.get(get_redirect = True)) for l in links]
+ links = [correctcap(l, disambPage.get(get_redirect=True))
+ for l in links]
self.alternatives += links
else:
try:
@@ -851,20 +959,25 @@
try:
if self.primary:
try:
- disambPage2 = pywikibot.Page(self.mysite,
- primary_topic_format[self.mylang]
- % disambPage.title()
- )
+ disambPage2 = pywikibot.Page(
+ self.mysite,
+ primary_topic_format[self.mylang]
+ % disambPage.title())
links = disambPage2.linkedPages()
- links = [correctcap(l,disambPage2.get()) for l in links]
+ links = [correctcap(l, disambPage2.get())
+ for l in links]
except pywikibot.NoPage:
- pywikibot.output(u"Page does not exist, using the first link in page %s." % disambPage.title())
+ pywikibot.output(
+u"Page does not exist, using the first link in page %s."
+ % disambPage.title())
links = disambPage.linkedPages()[:1]
- links = [correctcap(l,disambPage.get()) for l in links]
+ links = [correctcap(l, disambPage.get())
+ for l in links]
else:
try:
links = disambPage.linkedPages()
- links = [correctcap(l,disambPage.get()) for l in links]
+ links = [correctcap(l, disambPage.get())
+ for l in links]
except pywikibot.NoPage:
pywikibot.output(u"Page does not exist, skipping.")
return False
@@ -874,7 +987,8 @@
self.alternatives += links
return True
- def setSummaryMessage(self, disambPage, new_targets = [], unlink = False):
+ def setSummaryMessage(self, disambPage, new_targets=[], unlink=False,
+ dn=False):
# make list of new targets
targets = ''
for page_title in new_targets:
@@ -886,29 +1000,45 @@
targets = pywikibot.translate(self.mysite, unknown_msg)
# first check whether user has customized the edit comment
- if self.mysite.family.name in pywikibot.config.disambiguation_comment and self.mylang in pywikibot.config.disambiguation_comment[self.mysite.family.name]:
+ if (self.mysite.family.name in pywikibot.config.disambiguation_comment
+ and self.mylang in pywikibot.config.disambiguation_comment
+ [self.mysite.family.name]):
try:
- self.comment = pywikibot.translate(self.mysite,
- pywikibot.config.disambiguation_comment[
- self.mysite.family.name]
- ) % (disambPage.title(), targets)
- #Backwards compatibility, type error probably caused by too many arguments for format string
+ self.comment = pywikibot.translate(
+ self.mysite,
+ pywikibot.config.disambiguation_comment[
+ self.mysite.family.name]
+ ) % (disambPage.title(), targets)
+ # Backwards compatibility, type error probably caused by too
+ # many arguments for format string
except TypeError:
- self.comment = pywikibot.translate(self.mysite,
- pywikibot.config.disambiguation_comment[
- self.mysite.family.name]
- ) % disambPage.title()
+ self.comment = pywikibot.translate(
+ self.mysite,
+ pywikibot.config.disambiguation_comment[
+ self.mysite.family.name]
+ ) % disambPage.title()
elif disambPage.isRedirectPage():
# when working on redirects, there's another summary message
if unlink and not new_targets:
- self.comment = pywikibot.translate(self.mysite, msg_redir_unlink) % disambPage.title()
+ self.comment = pywikibot.translate(self.mysite,
+ msg_redir_unlink) \
+ % disambPage.title()
+ elif dn and not new_targets:
+ self.comment = pywikibot.translate(self.mysite, msg_redir_dn) \
+ % disambPage.title()
else:
- self.comment = pywikibot.translate(self.mysite, msg_redir) % (disambPage.title(), targets)
+ self.comment = pywikibot.translate(self.mysite, msg_redir) \
+ % (disambPage.title(), targets)
else:
if unlink and not new_targets:
- self.comment = pywikibot.translate(self.mysite, msg_unlink) % disambPage.title()
+ self.comment = pywikibot.translate(self.mysite, msg_unlink) \
+ % disambPage.title()
+ elif dn and not new_targets:
+ self.comment = pywikibot.translate(self.mysite, msg_dn) \
+ % disambPage.title()
else:
- self.comment = pywikibot.translate(self.mysite, msg) % (disambPage.title(), targets)
+ self.comment = pywikibot.translate(self.mysite, msg) \
+ % (disambPage.title(), targets)
def run(self):
if self.main_only:
@@ -920,7 +1050,8 @@
u'%s:' % namespace for namespace in self.mysite.namespaces()]
for disambPage in self.generator:
- self.primaryIgnoreManager = PrimaryIgnoreManager(disambPage, enabled=self.primary)
+ self.primaryIgnoreManager = PrimaryIgnoreManager(
+ disambPage, enabled=self.primary)
if not self.findAlternatives(disambPage):
continue
@@ -933,7 +1064,8 @@
self.alternatives.sort()
self.listAlternatives()
- gen = ReferringPageGeneratorWithIgnore(disambPage, self.primary, minimum = self.minimum)
+ gen = ReferringPageGeneratorWithIgnore(disambPage, self.primary,
+ minimum = self.minimum)
preloadingGen = pagegenerators.PreloadingGenerator(gen)
for refPage in preloadingGen:
if not self.primaryIgnoreManager.isIgnored(refPage):
@@ -944,12 +1076,13 @@
# clear alternatives before working on next disambiguation page
self.alternatives = []
-def main():
+def main(*args):
# the option that's always selected when the bot wonders what to do with
# a link. If it's None, the user is prompted (default behaviour).
always = None
alternatives = []
getAlternatives = True
+ dnSkip = False
# if the -file argument is used, page titles are dumped in this array.
# otherwise it will only contain one page.
generator = None
@@ -963,7 +1096,7 @@
ignoreCase = False
minimum = 0
- for arg in pywikibot.handleArgs():
+ for arg in pywikibot.handleArgs(*args):
if arg.startswith('-primary:'):
primary = True
getAlternatives = False
@@ -974,9 +1107,11 @@
always = arg[8:]
elif arg.startswith('-file'):
if len(arg) == 5:
- generator = pagegenerators.TextfilePageGenerator(filename = None)
+ generator = pagegenerators.TextfilePageGenerator(
+ filename=None)
else:
- generator = pagegenerators.TextfilePageGenerator(filename = arg[6:])
+ generator = pagegenerators.TextfilePageGenerator(
+ filename=arg[6:])
elif arg.startswith('-pos:'):
if arg[5]!=':':
mysite = pywikibot.getSite()
@@ -984,7 +1119,8 @@
if page.exists():
alternatives.append(page.title())
else:
- answer = pywikibot.inputChoice(u'Possibility %s does not actually exist. Use it anyway?'
+ answer = pywikibot.inputChoice(
+ u'Possibility %s does not actually exist. Use it anyway?'
% page.title(), ['yes', 'no'], ['y', 'N'], 'N')
if answer == 'y':
alternatives.append(page.title())
@@ -992,6 +1128,8 @@
alternatives.append(arg[5:])
elif arg == '-just':
getAlternatives = False
+ elif arg == '-dnskip':
+ dnSkip = True
elif arg == '-main':
main_only = True
elif arg.startswith('-min:'):
@@ -999,10 +1137,14 @@
elif arg.startswith('-start'):
try:
if len(arg) <= len('-start:'):
- generator = pagegenerators.CategorizedPageGenerator(pywikibot.getSite().disambcategory())
+ generator = pagegenerators.CategorizedPageGenerator(
+ pywikibot.getSite().disambcategory())
else:
- generator = pagegenerators.CategorizedPageGenerator(pywikibot.getSite().disambcategory(), start = arg[7:])
- generator = pagegenerators.NamespaceFilterPageGenerator(generator, [0])
+ generator = pagegenerators.CategorizedPageGenerator(
+ pywikibot.getSite().disambcategory(),
+ start=arg[7:])
+ generator = pagegenerators.NamespaceFilterPageGenerator(
+ generator, [0])
except pywikibot.NoPage:
print "Disambiguation category for your wiki is not known."
raise
@@ -1020,18 +1162,20 @@
page = pywikibot.Page(pywikibot.getSite(), pageTitle)
generator = iter([page])
- # if no disambiguation pages was given as an argument, and none was
+ # if no disambiguation page was given as an argument, and none was
# read from a file, query the user
if not generator:
- pageTitle = pywikibot.input(u'On which disambiguation page do you want to work?')
+ pageTitle = pywikibot.input(
+ u'On which disambiguation page do you want to work?')
page = pywikibot.Page(pywikibot.getSite(), pageTitle)
generator = iter([page])
- bot = DisambiguationRobot(always, alternatives, getAlternatives, generator, primary, main_only, minimum = minimum)
+ bot = DisambiguationRobot(always, alternatives, getAlternatives, dnSkip,
+ generator, primary, main_only,
+ minimum=minimum)
bot.run()
-
if __name__ == "__main__":
try:
main()
Revision: 8238
Author: xqt
Date: 2010-06-02 13:50:48 +0000 (Wed, 02 Jun 2010)
Log Message:
-----------
Adding @property decorator to Page.site() (bug #3010404) patch by stanlekub. Thanks.
Modified Paths:
--------------
branches/rewrite/pywikibot/data/api.py
branches/rewrite/pywikibot/page.py
branches/rewrite/pywikibot/pagegenerators.py
branches/rewrite/scripts/category.py
branches/rewrite/scripts/cosmetic_changes.py
branches/rewrite/scripts/interwiki.py
branches/rewrite/scripts/interwiki_graph.py
branches/rewrite/scripts/redirect.py
branches/rewrite/scripts/replace.py
branches/rewrite/scripts/solve_disambiguation.py
branches/rewrite/scripts/titletranslate.py
branches/rewrite/tests/api_tests.py
branches/rewrite/tests/page_tests.py
Modified: branches/rewrite/pywikibot/data/api.py
===================================================================
--- branches/rewrite/pywikibot/data/api.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/pywikibot/data/api.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -820,7 +820,7 @@
page._catinfo = pagedict["categoryinfo"]
if "templates" in pagedict:
- page._templates = [ pywikibot.Page(page.site(), tl['title'])
+ page._templates = [ pywikibot.Page(page.site, tl['title'])
for tl in pagedict['templates'] ]
if "langlinks" in pagedict:
@@ -828,7 +828,7 @@
for ll in pagedict["langlinks"]:
link = pywikibot.Link.langlinkUnsafe(ll['lang'],
ll['*'],
- source=page.site())
+ source=page.site)
links.append(link)
page._langlinks = links
Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/pywikibot/page.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -102,7 +102,7 @@
self.__dict__ = source.__dict__
if title:
# overwrite title
- self._link = Link(title, source=source.site(), defaultNamespace=ns)
+ self._link = Link(title, source=source.site, defaultNamespace=ns)
## if ":" in title:
## prefix = title[ :title.index(":")]
## self._ns = self._site.ns_index(prefix)
@@ -111,7 +111,7 @@
## else:
## title = title[title.index(":")+1 : ].strip(" _")
## self._title = "%s:%s" % (
-## self.site().namespace(self._ns),
+## self.site.namespace(self._ns),
## self._title)
## else:
## self._ns = 0
@@ -127,7 +127,7 @@
## self._title = source.title
## # reassemble the canonical title from components
## if self._ns:
-## self._title = "%s:%s" % (self.site().namespace(self._ns),
+## self._title = "%s:%s" % (self.site.namespace(self._ns),
## self._title)
else:
raise pywikibot.Error(
@@ -140,6 +140,7 @@
## # Always capitalize the first letter
## self._title = self._title[:1].upper() + self._title[1:]
+ @property
def site(self):
"""Return the Site object for the wiki on which this Page resides."""
return self._link.site
@@ -176,17 +177,17 @@
title = title + "#" + self._link.section
if asLink:
if forceInterwiki or (allowInterwiki and
- (self.site().family.name != config.family
- or self.site().code != config.mylang)):
- if self.site().family.name != config.family \
- and self.site().family.name != self.site().code:
- return u'[[%s:%s:%s]]' % (self.site().family.name,
- self.site().code,
+ (self.site.family.name != config.family
+ or self.site.code != config.mylang)):
+ if self.site.family.name != config.family \
+ and self.site.family.name != self.site.code:
+ return u'[[%s:%s:%s]]' % (self.site.family.name,
+ self.site.code,
title)
else:
# use this form for sites like commons, where the
# code is the same as the family name
- return u'[[%s:%s]]' % (self.site().code,
+ return u'[[%s:%s]]' % (self.site.code,
title)
elif textlink and (self.isImage() or self.isCategory()):
return u'[[:%s]]' % title
@@ -199,7 +200,7 @@
if underscore or asUrl:
title = title.replace(u' ', u'_')
if asUrl:
- encodedTitle = title.encode(self.site().encoding())
+ encodedTitle = title.encode(self.site.encoding())
title = urllib.quote(encodedTitle)
if as_filename:
# Replace characters that are not possible in file names on some
@@ -246,8 +247,8 @@
if not isinstance(other, Page):
# especially, return -1 if other is None
return -1
- if self.site() != other.site():
- return cmp(self.site(), other.site())
+ if self.site != other.site:
+ return cmp(self.site, other.site)
if self.namespace() != other.namespace():
return cmp(self.namespace(), other.namespace())
return cmp(self._link.title, other._link.title)
@@ -271,7 +272,7 @@
if not hasattr(self, '_autoFormat'):
from pywikibot import date
self._autoFormat = date.getAutoFormat(
- self.site().code,
+ self.site.code,
self.title(withNamespace=False)
)
return self._autoFormat
@@ -335,7 +336,7 @@
or not self._revid in self._revisions \
or self._revisions[self._revid].text is None:
try:
- self.site().loadrevisions(self, getText=True, sysop=sysop)
+ self.site.loadrevisions(self, getText=True, sysop=sysop)
except (pywikibot.NoPage, pywikibot.SectionError), e:
self._getexception = e
raise
@@ -357,7 +358,7 @@
"""
if force or not oldid in self._revisions \
or self._revisions[oldid].text is None:
- self.site().loadrevisions(self, getText=True, revids=oldid,
+ self.site.loadrevisions(self, getText=True, revids=oldid,
sysop=sysop)
# TODO: what about redirects, errors?
return self._revisions[oldid].text
@@ -365,16 +366,16 @@
def permalink(self):
"""Return the permalink URL for current revision of this page."""
return "%s://%s/%sindex.php?title=%s&oldid=%s" \
- % (self.site().protocol(),
- self.site().hostname(),
- self.site().scriptpath(),
+ % (self.site.protocol(),
+ self.site.hostname(),
+ self.site.scriptpath(),
self.title(asUrl=True),
self.latestRevision())
def latestRevision(self):
"""Return the current revision id for this page."""
if not hasattr(self, '_revid'):
- self.site().loadrevisions(self)
+ self.site.loadrevisions(self)
return self._revid
def _textgetter(self):
@@ -404,7 +405,7 @@
req = pywikibot.data.api.Request(action="expandtemplates",
text=self.text,
title=self.title(withSection=False),
- site=self.site())
+ site=self.site)
result = req.submit()
return result["expandtemplates"]["*"]
@@ -433,11 +434,11 @@
found.
"""
- return self.site().page_exists(self)
+ return self.site.page_exists(self)
def isRedirectPage(self):
"""Return True if this is a redirect, False if not or not existing."""
- return self.site().page_isredirect(self)
+ return self.site.page_isredirect(self)
def isCategoryRedirect(self):
"""Return True if this is a category redirect page, False otherwise."""
@@ -445,12 +446,12 @@
if not self.isCategory():
return False
if not hasattr(self, "_catredirect"):
- catredirs = self.site().category_redirects()
+ catredirs = self.site.category_redirects()
for (template, args) in self.templatesWithParams():
if template.title(withNamespace=False) in catredirs:
# Get target (first template argument)
try:
- self._catredirect = self.site().namespace(14) \
+ self._catredirect = self.site.namespace(14) \
+ ":" + args[0].strip()
break
except IndexError:
@@ -466,7 +467,7 @@
def getCategoryRedirectTarget(self):
"""If this is a category redirect, return the target category title."""
if self.isCategoryRedirect():
- return Category(Link(self._catredirect, self.site()))
+ return Category(Link(self._catredirect, self.site))
raise pywikibot.IsNotRedirectPage(self.title())
def isEmpty(self):
@@ -477,8 +478,8 @@
"""
txt = self.get()
- txt = pywikibot.removeLanguageLinks(txt, site = self.site())
- txt = pywikibot.removeCategoryLinks(txt, site = self.site())
+ txt = pywikibot.removeLanguageLinks(txt, site = self.site)
+ txt = pywikibot.removeCategoryLinks(txt, site = self.site)
if len(txt) < 4:
return True
else:
@@ -504,14 +505,14 @@
return None
if self.isTalkPage():
if self.namespace() == 1:
- return Page(self.site(), self.title(withNamespace=False))
+ return Page(self.site, self.title(withNamespace=False))
else:
- return Page(self.site(),
- self.site().namespace(ns - 1) + ':'
+ return Page(self.site,
+ self.site.namespace(ns - 1) + ':'
+ self.title(withNamespace=False))
else:
- return Page(self.site(),
- self.site().namespace(ns + 1) + ':'
+ return Page(self.site,
+ self.site.namespace(ns + 1) + ':'
+ self.title(withNamespace=False))
def isCategory(self):
@@ -535,22 +536,22 @@
"""
if not hasattr(self, "_isDisambig"):
- if not hasattr(self.site(), "_disambigtemplates"):
- self.site()._disambigtemplates = \
- self.site().family.disambig(self.site().code)
- if self.site()._disambigtemplates is None:
+ if not hasattr(self.site, "_disambigtemplates"):
+ self.site._disambigtemplates = \
+ self.site.family.disambig(self.site.code)
+ if self.site._disambigtemplates is None:
try:
- disambigpages = Page(self.site(),
+ disambigpages = Page(self.site,
"MediaWiki:Disambiguationspage")
- self.site()._disambigtemplates = [
+ self.site._disambigtemplates = [
link.title(withNamespace=False)
for link in disambigpages.linkedPages()
if link.namespace() == 10
]
except pywikibot.NoPage:
- self.site()._disambigtemplates = ['Disambig']
+ self.site._disambigtemplates = ['Disambig']
for t in self.templates():
- if t.title(withNamespace=False) in self.site()._disambigtemplates:
+ if t.title(withNamespace=False) in self.site._disambigtemplates:
self._isDisambig = True
break
else:
@@ -582,7 +583,7 @@
# to implement those methods in the site interface and then combine
# the results for this method, or to implement this method and then
# split up the results for the others.
- return self.site().pagereferences(
+ return self.site.pagereferences(
self,
followRedirects=follow_redirects,
filterRedirects=redirectsOnly,
@@ -604,7 +605,7 @@
@param total: iterate no more than this number of pages in total
"""
- return self.site().pagebacklinks(self,
+ return self.site.pagebacklinks(self,
followRedirects=followRedirects,
filterRedirects=filterRedirects,
namespaces=namespaces, step=step,
@@ -621,7 +622,7 @@
@param total: iterate no more than this number of pages in total
"""
- return self.site().page_embeddedin(self,
+ return self.site.page_embeddedin(self,
filterRedirects=filter_redirects,
namespaces=namespaces,
step=step, total=total)
@@ -634,7 +635,7 @@
- page is protected, and bot has a sysop account for this site.
"""
- return self.site().page_can_be_edited(self)
+ return self.site.page_can_be_edited(self)
def botMayEdit(self):
"""Return True if this page allows bots to edit it.
@@ -652,7 +653,7 @@
""" # TODO: move this to Site object?
if config.ignore_bot_templates: #Check the "master ignore switch"
return True
- username = self.site().user()
+ username = self.site.user()
try:
templates = self.templatesWithParams();
except (pywikibot.NoPage,
@@ -729,7 +730,7 @@
err = None
link = self.title(asLink=True)
try:
- done = self.site().editpage(self, summary=comment, minor=minor,
+ done = self.site.editpage(self, summary=comment, minor=minor,
watch=watchval)
if not done:
pywikibot.warning(u"Page %s not saved" % link)
@@ -787,7 +788,7 @@
@return: True if successful, False otherwise.
"""
- return self.site().watchpage(self, unwatch)
+ return self.site.watchpage(self, unwatch)
def linkedPages(self, namespaces=None, step=None, total=None):
"""Iterate Pages that this Page links to.
@@ -803,7 +804,7 @@
@return: a generator that yields Page objects.
"""
- return self.site().pagelinks(self, namespaces=namespaces, step=step,
+ return self.site.pagelinks(self, namespaces=namespaces, step=step,
total=total)
def interwiki(self, expand=True):
@@ -824,15 +825,15 @@
for linkmatch in pywikibot.link_regex.finditer(
pywikibot.removeDisabledParts(text)):
linktitle = linkmatch.group("title")
- link = Link(linktitle, self.site())
+ link = Link(linktitle, self.site)
# only yield links that are to a different site and that
# are not language links
try:
- if link.site != self.site():
+ if link.site != self.site:
if linktitle.lstrip().startswith(":"):
# initial ":" indicates not a language link
yield link
- elif link.site.family != self.site().family:
+ elif link.site.family != self.site.family:
# link to a different family is not a language link
yield link
except pywikibot.Error:
@@ -863,7 +864,7 @@
# method is called. If we do this, we'll have to think
# about what will happen if the generator is not completely
# iterated upon.
- return self.site().pagelanglinks(self, step=step, total=total)
+ return self.site.pagelanglinks(self, step=step, total=total)
def templates(self):
"""Return a list of Page objects for templates used on this Page.
@@ -892,7 +893,7 @@
"""
if hasattr(self, '_templates'):
return iter(self._templates)
- return self.site().pagetemplates(self, step=step, total=total)
+ return self.site.pagetemplates(self, step=step, total=total)
@deprecate_arg("followRedirects", None)
@deprecate_arg("loose", None)
@@ -904,7 +905,7 @@
@return: a generator that yields ImagePage objects.
"""
- return self.site().pageimages(self, step=step, total=total)
+ return self.site.pageimages(self, step=step, total=total)
def templatesWithParams(self):
"""Iterate templates used on this Page.
@@ -922,7 +923,7 @@
# element into a list in the format used by old scripts
result = []
for template in templates:
- link = pywikibot.Link(template[0], self.site(),
+ link = pywikibot.Link(template[0], self.site,
defaultNamespace=10)
try:
if link.canonical_title() not in titles:
@@ -942,7 +943,7 @@
positional.append(args[key])
for name in named:
positional.append("%s=%s" % (name, named[name]))
- result.append((pywikibot.Page(link, self.site()), positional))
+ result.append((pywikibot.Page(link, self.site), positional))
return result
@deprecate_arg("nofollow_redirects", None)
@@ -956,7 +957,7 @@
@return: a generator that yields Category objects.
"""
- return self.site().pagecategories(self, withSortKey=withSortKey,
+ return self.site.pagecategories(self, withSortKey=withSortKey,
step=step, total=total)
def extlinks(self, step=None, total=None):
@@ -967,7 +968,7 @@
@return: a generator that yields unicode objects containing URLs.
"""
- return self.site().page_extlinks(self, step=step, total=total)
+ return self.site.page_extlinks(self, step=step, total=total)
def getRedirectTarget(self):
"""Return a Page object for the target this Page redirects to.
@@ -976,7 +977,7 @@
exception. This method also can raise a NoPage exception.
"""
- return self.site().getredirtarget(self)
+ return self.site.getredirtarget(self)
# BREAKING CHANGE: in old framework, default value for getVersionHistory
# returned no more than 500 revisions; now, it iterates
@@ -998,7 +999,7 @@
@param total: iterate no more than this number of revisions in total
"""
- self.site().loadrevisions(self, getText=False, rvdir=reverseOrder,
+ self.site.loadrevisions(self, getText=False, rvdir=reverseOrder,
step=step, total=total)
return [ ( self._revisions[rev].revid,
self._revisions[rev].timestamp,
@@ -1034,7 +1035,7 @@
edit date/time, user name and content
"""
- return self.site().loadrevisions(self, getText=True,
+ return self.site.loadrevisions(self, getText=True,
rvdir=reverseOrder,
step=step, total=total)
@@ -1070,7 +1071,7 @@
reason = pywikibot.input(u'Please enter a reason for the move:')
# TODO: implement "safe" parameter (Is this necessary ?)
# TODO: implement "sysop" parameter
- return self.site().movepage(self, newtitle, reason,
+ return self.site.movepage(self, newtitle, reason,
movetalk=movetalkpage,
noredirect=deleteAndMove)
@@ -1089,7 +1090,7 @@
pywikibot.output(u'Deleting %s.' % (self.title(asLink=True)))
reason = pywikibot.input(u'Please enter a reason for the deletion:')
answer = u'y'
- if prompt and not hasattr(self.site(), '_noDeletePrompt'):
+ if prompt and not hasattr(self.site, '_noDeletePrompt'):
answer = pywikibot.inputChoice(u'Do you want to delete %s?'
% self.title(asLink = True, forceInterwiki = True),
['Yes', 'No', 'All'],
@@ -1097,10 +1098,10 @@
'N')
if answer in ['a', 'A']:
answer = 'y'
- self.site()._noDeletePrompt = True
+ self.site._noDeletePrompt = True
if answer in ['y', 'Y']:
try:
- return self.site().deletepage(self, reason)
+ return self.site.deletepage(self, reason)
except pywikibot.NoUsername, e:
if mark:
raise NotImplementedError(
@@ -1121,7 +1122,7 @@
"""
if not hasattr(self, "_deletedRevs"):
self._deletedRevs = {}
- for item in self.site().deletedrevs(self, step=step, total=total):
+ for item in self.site.deletedrevs(self, step=step, total=total):
for rev in item.get("revisions", []):
self._deletedRevs[rev['timestamp']] = rev
yield rev['timestamp']
@@ -1140,7 +1141,7 @@
(not retrieveText)
or "content" in self._deletedRevs["timestamp"]):
return self._deletedRevs["timestamp"]
- for item in self.site().deletedrevs(self, start=timestamp,
+ for item in self.site.deletedrevs(self, start=timestamp,
get_text=retrieveText, total=1):
# should only be one item with one revision
if item['title'] == self.title:
@@ -1187,7 +1188,7 @@
% (self.title(asLink=True)))
comment = pywikibot.input(
u'Please enter a reason for the undeletion:')
- return self.site().undelete(self, comment)
+ return self.site.undelete(self, comment)
@deprecate_arg("throttle", None)
def protect(self, edit='sysop', move='sysop', unprotect=False,
@@ -1216,16 +1217,16 @@
if unprotect:
edit = move = ""
answer = 'y'
- if prompt and not hasattr(self.site(), '_noProtectPrompt'):
+ if prompt and not hasattr(self.site, '_noProtectPrompt'):
answer = pywikibot.inputChoice(
u'Do you want to change the protection level of %s?'
% self.title(asLink=True, forceInterwiki = True),
['Yes', 'No', 'All'], ['Y', 'N', 'A'], 'N')
if answer in ['a', 'A']:
answer = 'y'
- self.site()._noProtectPrompt = True
+ self.site._noProtectPrompt = True
if answer in ['y', 'Y']:
- return self.site().protect(self, edit, move, reason)
+ return self.site.protect(self, edit, move, reason)
def change_category(self, oldCat, newCat, comment=None, sortKey=None,
inPlace=True):
@@ -1241,7 +1242,7 @@
"""
#TODO: is inPlace necessary?
- site = self.site()
+ site = self.site
changesMade = False
if not self.canBeEdited():
@@ -1337,7 +1338,7 @@
if not self.isCategory():
return None # should this raise an exception??
try:
- return self.site().categoryinfo(self)
+ return self.site.categoryinfo(self)
except NotImplementedError:
return None
@@ -1346,7 +1347,7 @@
@deprecated("Site.encoding()")
def encoding(self):
"""DEPRECATED: use Site.encoding() instead"""
- return self.site().encoding()
+ return self.site.encoding()
@deprecated("Page.title(withNamespace=False)")
def titleWithoutNamespace(self, underscore=False):
@@ -1427,15 +1428,15 @@
if not hasattr(self, '_imagePageHtml'):
from pywikibot.comms import http
path = "%s/index.php?title=%s" \
- % (self.site().scriptpath(), self.title(asUrl=True))
- self._imagePageHtml = http.request(self.site(), path)
+ % (self.site.scriptpath(), self.title(asUrl=True))
+ self._imagePageHtml = http.request(self.site, path)
return self._imagePageHtml
def fileUrl(self):
"""Return the URL for the image described on this page."""
# TODO add scaling option?
if not hasattr(self, '_imageinfo'):
- self._imageinfo = self.site().getimageinfo(self) #FIXME
+ self._imageinfo = self.site.getimageinfo(self) #FIXME
return self._imageinfo['url']
def fileIsOnCommons(self):
@@ -1446,7 +1447,7 @@
def fileIsShared(self):
"""Return True if image is stored on any known shared repository."""
# as of now, the only known repositories are commons and wikitravel
- if 'wikitravel_shared' in self.site().shared_image_repository():
+ if 'wikitravel_shared' in self.site.shared_image_repository():
return self.fileUrl().startswith(
u'http://wikitravel.org/upload/shared/')
return self.fileIsOnCommons()
@@ -1466,7 +1467,7 @@
def getFileSHA1Sum(self):
"""Return image file's SHA1 checksum."""
if not hasattr(self, '_imageinfo'):
- self._imageinfo = self.site().getimageinfo(self) #FIXME
+ self._imageinfo = self.site.getimageinfo(self) #FIXME
return self._imageinfo['sha1']
def getFileVersionHistory(self):
@@ -1477,7 +1478,7 @@
"""
#TODO; return value may need to change
- return self.site().getimageinfo(self, history=True) #FIXME
+ return self.site.getimageinfo(self, history=True) #FIXME
def getFileVersionHistoryTable(self):
"""Return the version history in the form of a wiki table."""
@@ -1496,7 +1497,7 @@
@param total: iterate no more than this number of pages in total
"""
- return self.site().imageusage(self, step=step, total=total)
+ return self.site.imageusage(self, step=step, total=total)
class Category(Page):
@@ -1553,9 +1554,9 @@
recurse = recurse - 1
if not hasattr(self, "_subcats"):
self._subcats = []
- for member in self.site().categorymembers(self, namespaces=[14],
+ for member in self.site.categorymembers(self, namespaces=[14],
step=step, total=total):
- subcat = Category(self.site(), member.title())
+ subcat = Category(self.site, member.title())
self._subcats.append(subcat)
yield subcat
if total is not None:
@@ -1601,9 +1602,9 @@
total (at all levels)
"""
- namespaces = [x for x in self.site().namespaces()
+ namespaces = [x for x in self.site.namespaces()
if x>=0 and x!=14]
- for member in self.site().categorymembers(self,
+ for member in self.site.categorymembers(self,
namespaces=namespaces,
step=step, total=total):
yield member
@@ -1625,7 +1626,7 @@
def members(self, recurse=False, namespaces=None, step=None, total=None):
"""Yield all category contents (subcats, pages, and files)."""
- for member in self.site().categorymembers(self, namespaces,
+ for member in self.site.categorymembers(self, namespaces,
step=step, total=total):
yield member
if total is not None:
@@ -1646,7 +1647,7 @@
def isEmptyCategory(self):
"""Return True if category has no members (including subcategories)."""
- for member in self.site().categorymembers(self, total=1):
+ for member in self.site.categorymembers(self, total=1):
return False
return True
@@ -1668,8 +1669,8 @@
# move to category.py? (Although it doesn't seem to be used there,
# either)
if not isinstance(cat, Category):
- cat = self.site().category_namespace() + ':' + cat
- targetCat = Category(self.site(), cat)
+ cat = self.site.category_namespace() + ':' + cat
+ targetCat = Category(self.site, cat)
else:
targetCat = cat
if targetCat.exists():
@@ -1709,8 +1710,8 @@
"""
# I don't see why we need this as part of the framework either
# move to scripts/category.py?
- catname = self.site().category_namespace() + ':' + catname
- targetCat = Category(self.site(), catname)
+ catname = self.site.category_namespace() + ':' + catname
+ targetCat = Category(self.site, catname)
if targetCat.exists():
pywikibot.warning(u'Target page %s already exists!'
% targetCat.title())
@@ -1801,7 +1802,7 @@
del self._userprops
if not hasattr(self, '_userprops'):
usrequest = pywikibot.data.api.Request(
- site=self.site(),
+ site=self.site,
action='query',
list='users',
usprop='blockinfo|groups|editcount|registration|emailable',
@@ -1883,7 +1884,7 @@
raise AutoblockUser("This is an autoblock ID, you can only use to unblock it.")
if subpage:
subpage = u'/' + subpage
- return Page(Link(self.title() + subpage, self.site()))
+ return Page(Link(self.title() + subpage, self.site))
def getUserTalkPage(self, subpage=u''):
""" Return a pywikibot.Page object corresponding to this user's main
@@ -1900,7 +1901,7 @@
if subpage:
subpage = u'/' + subpage
return Page(Link(self.title(withNamespace=False) + subpage,
- self.site(), defaultNamespace=3))
+ self.site, defaultNamespace=3))
def sendMail(self, subject, text, ccme = False):
""" Send an email to this user via mediawiki's email interface.
@@ -1919,13 +1920,13 @@
if not self.isEmailable():
raise UserActionRefuse('This user is not mailable')
- if not self.site().has_right('sendemail'):
+ if not self.site.has_right('sendemail'):
raise UserActionRefuse('You don\'t have permission to send mail')
params = {
'action': 'emailuser',
'target': self.username,
- 'token': self.site().token(self, 'email'),
+ 'token': self.site.token(self, 'email'),
'subject': subject,
'text': text,
}
@@ -1972,11 +1973,11 @@
@param namespaces: only iterate links in these namespaces
@type namespaces: list
"""
- for contrib in self.site().usercontribs(user=self.username,
+ for contrib in self.site.usercontribs(user=self.username,
namespaces=namespaces, total=total):
ts = pywikibot.Timestamp.fromISOformat(contrib['timestamp'])
ts = int(ts.strftime("%Y%m%d%H%M%S"))
- yield Page(Link(contrib['title'], self.site(),
+ yield Page(Link(contrib['title'], self.site,
defaultNamespace=contrib['ns'])), \
contrib['revid'], ts, contrib['comment']
@@ -1990,7 +1991,7 @@
@param total: limit result to this number of pages
@type total: int
"""
- for item in self.site().logevents(logtype='upload', user=self.username,
+ for item in self.site.logevents(logtype='upload', user=self.username,
total=total):
yield item.title(), str(item.timestamp()), item.comment(), False
@@ -2361,7 +2362,7 @@
"""
link = Link.__new__(Link)
- link._site = page.site()
+ link._site = page.site
link._section = page.section()
link._namespace = page.namespace()
link._title = page.title(withNamespace=False,
Modified: branches/rewrite/pywikibot/pagegenerators.py
===================================================================
--- branches/rewrite/pywikibot/pagegenerators.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/pywikibot/pagegenerators.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -708,8 +708,8 @@
seenPages = {}
for page in generator:
if page not in seenPages:
- _page = u"%s:%s:%s" % (page.site().family.name,
- page.site().code,
+ _page = u"%s:%s:%s" % (page.site.family.name,
+ page.site.code,
page.title())
seenPages[_page] = True
yield page
@@ -760,7 +760,7 @@
sites = {}
# build a list of pages for each site found in the iterator
for page in generator:
- site = page.site()
+ site = page.site
sites.setdefault(site, []).append(page)
if len(sites[site]) >= step:
group = sites[site]
@@ -789,7 +789,7 @@
site = pywikibot.Site()
for page in site.unusedfiles(number=number, repeat=repeat,
extension=extension):
- yield pywikibot.ImagePage(page.site(), page.title())
+ yield pywikibot.ImagePage(page.site, page.title())
def WithoutInterwikiPageGenerator(number=100, repeat=False, site=None):
if site is None:
@@ -1007,7 +1007,7 @@
title = url[len(base):]
page = pywikibot.Page(pywikibot.Link(title, self.site))
# Google contains links in the format http://de.wikipedia.org/wiki/en:Foobar
- if page.site() == self.site:
+ if page.site == self.site:
yield page
def MySQLPageGenerator(query, site = None):
Modified: branches/rewrite/scripts/category.py
===================================================================
--- branches/rewrite/scripts/category.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/scripts/category.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -343,7 +343,7 @@
'''
page_name = pagelink.title()
- site = pagelink.site()
+ site = pagelink.site
# regular expression that matches a name followed by a space and
# disambiguation brackets. Group 1 is the name without the rest.
bracketsR = re.compile('(.*) \(.+?\)')
Modified: branches/rewrite/scripts/cosmetic_changes.py
===================================================================
--- branches/rewrite/scripts/cosmetic_changes.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/scripts/cosmetic_changes.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -469,7 +469,7 @@
# 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())
- ccToolkit = CosmeticChangesToolkit(page.site(), debug = True, namespace = page.namespace())
+ ccToolkit = CosmeticChangesToolkit(page.site, debug = True, namespace = page.namespace())
changedText = ccToolkit.change(page.get())
if changedText != page.get():
if not self.acceptall:
Modified: branches/rewrite/scripts/interwiki.py
===================================================================
--- branches/rewrite/scripts/interwiki.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/scripts/interwiki.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -843,7 +843,7 @@
return self.size
def add(self, page):
- site = page.site()
+ site = page.site
if not site in self.tree:
self.tree[site] = []
self.tree[site].append(page)
@@ -851,7 +851,7 @@
def remove(self, page):
try:
- self.tree[page.site()].remove(page)
+ self.tree[page.site].remove(page)
self.size -= 1
except ValueError:
pass
@@ -916,7 +916,7 @@
Site:
Code becomes:
- todo <- {originPage.site():[originPage]}
+ todo <- {originPage.site:[originPage]}
done <- []
while todo != {}:
site <- electSite()
@@ -1101,7 +1101,7 @@
page = StoredPage(page)
self.foundIn[page] = [linkingPage]
self.todo.add(page)
- counter.plus(page.site())
+ counter.plus(page.site)
return True
def skipPage(self, page, target, counter):
@@ -1122,9 +1122,9 @@
return False
elif self.originPage.namespace() != linkedPage.namespace():
# Allow for a mapping between different namespaces
- crossFrom = self.originPage.site().family.crossnamespace.get(self.originPage.namespace(), {})
- crossTo = crossFrom.get(self.originPage.site().language(), crossFrom.get('_default', {}))
- nsmatch = crossTo.get(linkedPage.site().language(), crossTo.get('_default', []))
+ crossFrom = self.originPage.site.family.crossnamespace.get(self.originPage.namespace(), {})
+ crossTo = crossFrom.get(self.originPage.site.language(), crossFrom.get('_default', {}))
+ nsmatch = crossTo.get(linkedPage.site.language(), crossTo.get('_default', []))
if linkedPage.namespace() in nsmatch:
return False
if globalvar.autonomous:
@@ -1133,7 +1133,7 @@
self.foundIn[linkedPage] = [linkingPage]
return True
else:
- preferredPage = self.getFoundInCorrectNamespace(linkedPage.site())
+ preferredPage = self.getFoundInCorrectNamespace(linkedPage.site)
if preferredPage:
pywikibot.output(u"NOTE: Ignoring link from page %s in namespace %i to page %s in namespace %i because page %s in the correct namespace has already been found." % (self.originPage, self.originPage.namespace(), linkedPage, linkedPage.namespace(), preferredPage))
return True
@@ -1145,9 +1145,9 @@
if choice == 'g':
self.makeForcedStop(counter)
elif choice == 'a':
- newHint = pywikibot.input(u'Give the alternative for language %s, not using a language code:' % linkedPage.site().language())
+ newHint = pywikibot.input(u'Give the alternative for language %s, not using a language code:' % linkedPage.site.language())
if newHint:
- alternativePage = pywikibot.Page(linkedPage.site(), newHint)
+ alternativePage = pywikibot.Page(linkedPage.site, newHint)
if alternativePage:
# add the page that was entered by the user
self.addIfNew(alternativePage, counter, None)
@@ -1163,7 +1163,7 @@
if page.title().lower() != self.originPage.title().lower():
pywikibot.output(u"NOTE: Ignoring %s for %s in wiktionary mode" % (page, self.originPage))
return True
- elif page.title() != self.originPage.title() and self.originPage.site().nocapitalize and page.site().nocapitalize:
+ elif page.title() != self.originPage.title() and self.originPage.site.nocapitalize and page.site.nocapitalize:
pywikibot.output(u"NOTE: Ignoring %s for %s in wiktionary mode because both languages are uncapitalized." % (page, self.originPage))
return True
return False
@@ -1192,14 +1192,14 @@
else:
choice = 'y'
if self.originPage.isDisambig() and not page.isDisambig():
- disambig = self.getFoundDisambig(page.site())
+ disambig = self.getFoundDisambig(page.site)
if disambig:
pywikibot.output(u"NOTE: Ignoring non-disambiguation page %s for %s because disambiguation page %s has already been found." % (page, self.originPage, disambig))
return (True, None)
else:
choice = pywikibot.inputChoice(u'WARNING: %s is a disambiguation page, but %s doesn\'t seem to be one. Follow it anyway?' % (self.originPage, page), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
elif not self.originPage.isDisambig() and page.isDisambig():
- nondisambig = self.getFoundNonDisambig(page.site())
+ nondisambig = self.getFoundNonDisambig(page.site)
if nondisambig:
pywikibot.output(u"NOTE: Ignoring disambiguation page %s for %s because non-disambiguation page %s has already been found." % (page, self.originPage, nondisambig))
return (True, None)
@@ -1208,8 +1208,8 @@
if choice == 'n':
return (True, None)
elif choice == 'a':
- newHint = pywikibot.input(u'Give the alternative for language %s, not using a language code:' % page.site().language())
- alternativePage = pywikibot.Page(page.site(), newHint)
+ newHint = pywikibot.input(u'Give the alternative for language %s, not using a language code:' % page.site.language())
+ alternativePage = pywikibot.Page(page.site, newHint)
return (True, alternativePage)
elif choice == 'g':
self.makeForcedStop(counter)
@@ -1218,7 +1218,7 @@
return (False, None)
def isIgnored(self, page):
- if page.site().language() in globalvar.neverlink:
+ if page.site.language() in globalvar.neverlink:
pywikibot.output(u"Skipping link %s to an ignored language" % page)
return True
if page in globalvar.ignore:
@@ -1286,14 +1286,14 @@
if globalvar.skipauto:
dictName, year = page.autoFormat()
if dictName is not None:
- pywikibot.output(u'WARNING: %s:%s relates to %s:%s, which is an auto entry %s(%s)' % (self.originPage.site().language(), self.originPage, page.site().language(),page,dictName,year))
+ pywikibot.output(u'WARNING: %s:%s relates to %s:%s, which is an auto entry %s(%s)' % (self.originPage.site.language(), self.originPage, page.site.language(),page,dictName,year))
# Abort processing if the bot is running in autonomous mode.
if globalvar.autonomous:
self.makeForcedStop(counter)
# Register this fact at the todo-counter.
- counter.minus(page.site())
+ counter.minus(page.site)
# Now check whether any interwiki links should be added to the
# todo list.
@@ -1351,7 +1351,7 @@
elif not globalvar.followredirect:
if not globalvar.quiet:
pywikibot.output(u"NOTE: not following %sredirects." % redir)
- elif page.site().family == redirectTargetPage.site().family \
+ elif page.site.family == redirectTargetPage.site.family \
and not self.skipPage(page, redirectTargetPage, counter):
if self.addIfNew(redirectTargetPage, counter, page):
if config.interwiki_shownew:
@@ -1381,7 +1381,7 @@
iw = page.langlinks()
except pywikibot.NoSuchSite:
if not globalvar.quiet:
- pywikibot.output(u"NOTE: site %s does not exist" % page.site())
+ pywikibot.output(u"NOTE: site %s does not exist" % page.site)
continue
(skip, alternativePage) = self.disambigMismatch(page, counter)
@@ -1394,7 +1394,7 @@
self.addIfNew(alternativePage, counter, None)
duplicate = None
- for p in self.done.filter(page.site()):
+ for p in self.done.filter(page.site):
if p != page and p.exists() and not p.isRedirectPage() and not p.isCategoryRedirect():
duplicate = p
break
@@ -1405,7 +1405,7 @@
# Ignore the interwiki links.
iw = ()
if globalvar.lacklanguage:
- if globalvar.lacklanguage in [link.site().language() for link in iw]:
+ if globalvar.lacklanguage in [link.site.language() for link in iw]:
iw = ()
self.workonme = False
if len(iw) < globalvar.minlinks:
@@ -1422,7 +1422,7 @@
f = codecs.open(
pywikibot.config.datafilepath('autonomous_problems.dat'),
'a', 'utf-8')
- f.write(u"* %s {Found more than one link for %s}" % (self.originPage, page.site()))
+ f.write(u"* %s {Found more than one link for %s}" % (self.originPage, page.site))
if config.interwiki_graph and config.interwiki_graph_url:
filename = interwiki_graph.getFilename(self.originPage, extension = config.interwiki_graph_formats[0])
f.write(u" [%s%s graph]" % (config.interwiki_graph_url, filename))
@@ -1453,9 +1453,9 @@
if self.addIfNew(linkedPage, counter, page):
# It is new. Also verify whether it is the second on the
# same site
- lpsite=linkedPage.site()
+ lpsite=linkedPage.site
for prevPage in self.foundIn:
- if prevPage != linkedPage and prevPage.site() == lpsite:
+ if prevPage != linkedPage and prevPage.site == lpsite:
# Still, this could be "no problem" as either may be a
# redirect to the other. No way to find out quickly!
pywikibot.output(u"NOTE: %s: %s gives duplicate interwiki on same site %s" % (self.originPage, page, linkedPage))
@@ -1499,8 +1499,8 @@
new = {}
for page in self.done:
if page.exists() and not page.isRedirectPage() and not page.isCategoryRedirect():
- site = page.site()
- if site == self.originPage.site():
+ site = page.site
+ if site == self.originPage.site:
if page != self.originPage:
self.problem(u"Found link to %s" % page )
self.whereReport(page)
@@ -1624,8 +1624,8 @@
# Make sure new contains every page link, including the page we are processing
# replaceLinks will skip the site it's working on.
- if self.originPage.site() not in new:
- new[self.originPage.site()] = self.originPage
+ if self.originPage.site not in new:
+ new[self.originPage.site] = self.originPage
#self.replaceLinks(self.originPage, new, True, bot)
@@ -1634,7 +1634,7 @@
# Process all languages here
globalvar.always = False
if globalvar.limittwo:
- lclSite = self.originPage.site()
+ lclSite = self.originPage.site
lclSiteDone = False
frgnSiteDone = False
@@ -1658,7 +1658,7 @@
try:
for link in new[site].iterlanglinks():
page = pywikibot.Page(link)
- old[page.site()] = page
+ old[page.site] = page
except pywikibot.NoPage:
pywikibot.output(u"BUG>>> %s no longer exists?" % new[site])
continue
@@ -1747,23 +1747,23 @@
# remove interwiki links to ignore
for iw in re.finditer('<!-- *\[\[(.*?:.*?)\]\] *-->', pagetext):
try:
- ignorepage = pywikibot.Page(page.site(), iw.groups()[0])
+ ignorepage = pywikibot.Page(page.site, iw.groups()[0])
except (pywikibot.NoSuchSite, pywikibot.InvalidTitle):
continue
try:
- if (new[ignorepage.site()] == ignorepage) and (ignorepage.site() != page.site()):
+ if (new[ignorepage.site] == ignorepage) and (ignorepage.site != page.site):
if (ignorepage not in interwikis):
pywikibot.output(u"Ignoring link to %(to)s for %(from)s" % {'to': ignorepage, 'from': page})
- new.pop(ignorepage.site())
+ new.pop(ignorepage.site)
else:
pywikibot.output(u"NOTE: Not removing interwiki from %(from)s to %(to)s (exists both commented and non-commented)" % {'to': ignorepage, 'from': page})
except KeyError:
pass
# sanity check - the page we are fixing must be the only one for that site.
- pltmp = new[page.site()]
+ pltmp = new[page.site]
if pltmp != page:
s = u"None"
if pltmp is not None: s = pltmp
@@ -1771,25 +1771,25 @@
raise SaveError(u'BUG: sanity check failed')
# Avoid adding an iw link back to itself
- del new[page.site()]
+ del new[page.site]
# Put interwiki links into a map
old={}
for page2 in interwikis:
- old[page2.site()] = page2
+ old[page2.site] = page2
# Check what needs to get done
- mods, mcomment, adding, removing, modifying = compareLanguages(old, new, insite = page.site())
+ mods, mcomment, adding, removing, modifying = compareLanguages(old, new, insite = page.site)
# When running in autonomous mode without -force switch, make sure we don't remove any items, but allow addition of the new ones
if globalvar.autonomous and not globalvar.force and len(removing) > 0:
for rmsite in removing:
- if rmsite != page.site(): # Sometimes sites have an erroneous link to itself as an interwiki
+ if rmsite != page.site: # Sometimes sites have an erroneous link to itself as an interwiki
rmPage = old[rmsite]
new[rmsite] = old[rmsite] #put it to new means don't delete it
pywikibot.output(u"WARNING: %s is either deleted or has a mismatching disambiguation state." % rmPage)
# Re-Check what needs to get done
- mods, mcomment, adding, removing, modifying = compareLanguages(old, new, insite = page.site())
+ mods, mcomment, adding, removing, modifying = compareLanguages(old, new, insite = page.site)
if not mods:
pywikibot.output(u'No changes needed' )
@@ -1799,7 +1799,7 @@
oldtext = page.get()
template = (page.namespace() == 10)
newtext = pywikibot.replaceLanguageLinks(oldtext, new,
- site = page.site(),
+ site = page.site,
template = template)
# This is for now. Later there should be different funktions for each kind
if not botMayEdit(page):
@@ -1815,7 +1815,7 @@
# pywikibot.output(u"NOTE: Replace %s" % page)
# Determine whether we need permission to submit
ask = False
- if removing and removing != [page.site()]: # Allow for special case of a self-pointing interwiki link
+ if removing and removing != [page.site]: # Allow for special case of a self-pointing interwiki link
self.problem(u'Found incorrect link to %s in %s'% (",".join([x.lang for x in removing]), page), createneed = False)
ask = True
if globalvar.force:
@@ -1833,8 +1833,8 @@
['y', 'n', 'b', 'g', 'a'])
if answer == 'b':
webbrowser.open("http://%s%s" % (
- page.site().hostname(),
- page.site().nice_get_address(page.title())
+ page.site.hostname(),
+ page.site.nice_get_address(page.title())
))
pywikibot.input(u"Press Enter when finished in browser.")
return True
@@ -1931,21 +1931,21 @@
# This assumes that there is only one interwiki link per language.
linkedPagesDict = {}
for linkedPage in linkedPages:
- linkedPagesDict[linkedPage.site()] = linkedPage
+ linkedPagesDict[linkedPage.site] = linkedPage
for expectedPage in expectedPages - linkedPages:
if expectedPage != page:
try:
- linkedPage = linkedPagesDict[expectedPage.site()]
- pywikibot.output(u"WARNING: %s: %s does not link to %s but to %s" % (page.site().family.name, page, expectedPage, linkedPage))
+ linkedPage = linkedPagesDict[expectedPage.site]
+ pywikibot.output(u"WARNING: %s: %s does not link to %s but to %s" % (page.site.family.name, page, expectedPage, linkedPage))
except KeyError:
- pywikibot.output(u"WARNING: %s: %s does not link to %s" % (page.site().family.name, page, expectedPage))
+ pywikibot.output(u"WARNING: %s: %s does not link to %s" % (page.site.family.name, page, expectedPage))
# Check for superfluous links
for linkedPage in linkedPages:
if linkedPage not in expectedPages:
# Check whether there is an alternative page on that language.
# In this case, it was already reported above.
- if linkedPage.site() not in expectedSites:
- pywikibot.output(u"WARNING: %s: %s links to incorrect %s" % (page.site().family.name, page, linkedPage))
+ if linkedPage.site not in expectedSites:
+ pywikibot.output(u"WARNING: %s: %s links to incorrect %s" % (page.site.family.name, page, linkedPage))
except (socket.error, IOError):
pywikibot.output(u'ERROR: could not report backlinks')
@@ -2033,7 +2033,7 @@
if page.namespace() == 10:
loc = None
try:
- tmpl, loc = moved_links[page.site().lang]
+ tmpl, loc = moved_links[page.site.lang]
del tmpl
except KeyError:
pass
@@ -2044,7 +2044,7 @@
if self.generateUntil:
until = self.generateUntil
- if page.site().lang not in page.site().family.nocapitalize:
+ if page.site.lang not in page.site.family.nocapitalize:
until = until[0].upper()+until[1:]
if page.titleWithoutNamespace() > until:
raise StopIteration
@@ -2230,13 +2230,13 @@
def botMayEdit (page):
tmpl = []
try:
- tmpl, loc = moved_links[page.site().lang]
+ tmpl, loc = moved_links[page.site.lang]
except KeyError:
pass
if type(tmpl) != list:
tmpl = [tmpl]
try:
- tmpl += ignoreTemplates[page.site().lang]
+ tmpl += ignoreTemplates[page.site.lang]
except KeyError:
pass
tmpl += ignoreTemplates['_default']
@@ -2255,7 +2255,7 @@
for page, pagelist in hints.iteritems():
# The WarnfileReader gives us a list of pagelinks, but titletranslate.py expects a list of strings, so we convert it back.
# TODO: This is a quite ugly hack, in the future we should maybe make titletranslate expect a list of pagelinks.
- hintStrings = ['%s:%s' % (hintedPage.site().language(), hintedPage.title()) for hintedPage in pagelist]
+ hintStrings = ['%s:%s' % (hintedPage.site.language(), hintedPage.title()) for hintedPage in pagelist]
bot.add(page, hints = hintStrings)
def main():
Modified: branches/rewrite/scripts/interwiki_graph.py
===================================================================
--- branches/rewrite/scripts/interwiki_graph.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/scripts/interwiki_graph.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -53,14 +53,14 @@
self.subject = subject
def getLabel(self, page):
- return (u'"\"%s:%s\""' % (page.site().language(),
+ return (u'"\"%s:%s\""' % (page.site.language(),
page.title())).encode('utf-8')
def addNode(self, page):
node = pydot.Node(self.getLabel(page), shape = 'rectangle')
node.set_URL("\"http://%s%s\""
- % (page.site().hostname(),
- page.site().get_address(page.urlname())))
+ % (page.site.hostname(),
+ page.site.get_address(page.urlname())))
node.set_style('filled')
node.set_fillcolor('white')
node.set_fontsize('11')
@@ -74,7 +74,7 @@
node.set_color('green')
node.set_style('filled,bold')
# if we found more than one valid page for this language:
- if len(filter(lambda p: p.site() == page.site() and p.exists() \
+ if len(filter(lambda p: p.site == page.site and p.exists() \
and not p.isRedirectPage(),
self.subject.foundIn.keys())) > 1:
# mark conflict by octagonal node
@@ -102,7 +102,7 @@
# opposite edge.
else:
# add edge
- if refPage.site() == page.site():
+ if refPage.site == page.site:
edge.set_color('blue')
elif not page.exists():
# mark dead links
@@ -231,8 +231,8 @@
def getFilename(page, extension = None):
- filename = '%s-%s-%s' % (page.site().family.name,
- page.site().language(),
+ filename = '%s-%s-%s' % (page.site.family.name,
+ page.site.language(),
page.titleForFilename())
if extension:
filename += '.%s' % extension
Modified: branches/rewrite/scripts/redirect.py
===================================================================
--- branches/rewrite/scripts/redirect.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/scripts/redirect.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -515,16 +515,16 @@
try:
redir_page.delete(reason, prompt = False)
except pywikibot.NoUsername:
- if targetPage.site().lang in sd_template \
- and targetPage.site().lang in reason_broken:
+ if targetPage.site.lang in sd_template \
+ and targetPage.site.lang in reason_broken:
pywikibot.output(
u"No sysop in user-config.py, put page to speedy deletion.")
content = redir_page.get(get_redirect=True)
content = pywikibot.translate(
- targetPage.site().lang,
+ targetPage.site.lang,
sd_template)+"\n"+content
summary = pywikibot.translate(
- targetPage.site().lang,
+ targetPage.site.lang,
reason_broken)
redir_page.put(content, summary)
@@ -555,7 +555,7 @@
newRedir = redir
redirList = [] # bookkeeping to detect loops
while True:
- redirList.append(u'%s:%s' % (newRedir.site().lang,
+ redirList.append(u'%s:%s' % (newRedir.site.lang,
newRedir.title(withSection=False)))
try:
targetPage = newRedir.getRedirectTarget()
@@ -604,11 +604,11 @@
pywikibot.output(
u' Links to: %s.'
% targetPage.title(asLink=True))
- if targetPage.site().sitename() == 'wikipedia:en' \
+ if targetPage.site.sitename() == 'wikipedia:en' \
and targetPage.title() == 'Target page name':
pywikibot.output(u"Skipping: Redirect source is vandalized.")
break
- if targetPage.site() != self.site:
+ if targetPage.site != self.site:
pywikibot.output(
u'Warning: redirect target (%s) is on a different site.'
% (targetPage.title(asLink=True)))
@@ -616,7 +616,7 @@
break # skip if automatic
# watch out for redirect loops
if redirList.count(u'%s:%s'
- % (targetPage.site().lang,
+ % (targetPage.site.lang,
targetPage.title(withSection=False))
) > 0:
pywikibot.output(
@@ -627,17 +627,17 @@
content = targetPage.get(get_redirect=True)
except pywikibot.SectionError:
content = pywikibot.Page(
- targetPage.site(),
+ targetPage.site,
targetPage.title(withSection=False)
).get(get_redirect=True)
- if targetPage.site().lang in sd_template \
- and targetPage.site().lang in sd_tagging_sum:
+ if targetPage.site.lang in sd_template \
+ and targetPage.site.lang in sd_tagging_sum:
pywikibot.output(u"Tagging redirect for deletion")
# Delete the two redirects
content = pywikibot.translate(
- targetPage.site().lang,
+ targetPage.site.lang,
sd_template)+"\n"+content
- summ = pywikibot.translate(targetPage.site().lang,
+ summ = pywikibot.translate(targetPage.site.lang,
sd_tagging_sum)
targetPage.put(content, summ)
redir.put(content, summ)
Modified: branches/rewrite/scripts/replace.py
===================================================================
--- branches/rewrite/scripts/replace.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/scripts/replace.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -416,8 +416,8 @@
continue
if choice == 'b':
webbrowser.open("http://%s%s" % (
- page.site().hostname(),
- page.site().nice_get_address(page.title())
+ page.site.hostname(),
+ page.site.nice_get_address(page.title())
))
pywikibot.input("Press Enter when finished in browser.")
original_text = page.get(get_redirect=True, force=True)
Modified: branches/rewrite/scripts/solve_disambiguation.py
===================================================================
--- branches/rewrite/scripts/solve_disambiguation.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/scripts/solve_disambiguation.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -445,10 +445,11 @@
return string[0].upper()+string[1:]
def correctcap(link, text):
- # If text links to a page with title link uncapitalized, uncapitalize link, otherwise capitalize it
+ # If text links to a page with title link uncapitalized, uncapitalize link,
+ # otherwise capitalize it
linkupper = link.title()
linklower = linkupper[0].lower() + linkupper[1:]
- if text.find("[[%s]]"%linklower) > -1 or text.find("[[%s|"%linklower) > -1:
+ if "[[%s]]"%linklower in text or "[[%s|"%linklower in text:
return linklower
else:
return linkupper
@@ -468,11 +469,11 @@
withTemplateInclusion=False)]
pywikibot.output(u"Found %d references." % len(refs))
# Remove ignorables
- if self.disambPage.site().family.name in ignore_title \
- and self.disambPage.site().lang \
- in ignore_title[self.disambPage.site().family.name]:
- for ig in ignore_title[self.disambPage.site().family.name
- ][self.disambPage.site().lang]:
+ if self.disambPage.site.family.name in ignore_title \
+ and self.disambPage.site.lang \
+ in ignore_title[self.disambPage.site.family.name]:
+ for ig in ignore_title[self.disambPage.site.family.name
+ ][self.disambPage.site.lang]:
for i in range(len(refs)-1, -1, -1):
if re.match(ig, refs[i].title()):
pywikibot.log(u'Ignoring page %s'
@@ -711,11 +712,11 @@
curpos = m.start() + 1
try:
foundlink = pywikibot.Link(m.group('title'),
- disambPage.site())
+ disambPage.site)
except pywikibot.Error:
continue
# ignore interwiki links
- if foundlink.site != disambPage.site():
+ if foundlink.site != disambPage.site:
continue
# check whether the link found is to disambPage
try:
@@ -884,7 +885,7 @@
continue
new_page_title = self.alternatives[choice]
repPl = pywikibot.Page(pywikibot.Link(new_page_title,
- disambPage.site()))
+ disambPage.site))
if (new_page_title[0].isupper()
or link_text[0].isupper()):
new_page_title = repPl.title()
@@ -937,14 +938,14 @@
def findAlternatives(self, disambPage):
if disambPage.isRedirectPage() and not self.primary:
- if (disambPage.site().lang in self.primary_redir_template
- and self.primary_redir_template[disambPage.site().lang]
+ if (disambPage.site.lang in self.primary_redir_template
+ and self.primary_redir_template[disambPage.site.lang]
in disambPage.templates(get_redirect = True)):
baseTerm = disambPage.title()
for template in disambPage.templatesWithParams(
get_redirect=True):
if template[0] == self.primary_redir_template[
- disambPage.site().lang] \
+ disambPage.site.lang] \
and len(template[1]) > 0:
baseTerm = template[1][1]
disambTitle = primary_topic_format[self.mylang] % baseTerm
Modified: branches/rewrite/scripts/titletranslate.py
===================================================================
--- branches/rewrite/scripts/titletranslate.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/scripts/titletranslate.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -19,7 +19,7 @@
Does some magic stuff. Returns a list of Links.
"""
result = []
- site = page.site()
+ site = page.site
if hints:
for h in hints:
if h.find(':') == -1:
@@ -64,12 +64,12 @@
# Autotranslate dates into all other languages, the rest will come from existing interwiki links.
if auto:
# search inside all dictionaries for this link
- dictName, value = date.getAutoFormat( page.site().code, page.title() )
+ dictName, value = date.getAutoFormat( page.site.code, page.title() )
if dictName:
- if not (dictName == 'yearsBC' and date.maxyearBC.has_key(page.site().code) and value > date.maxyearBC[page.site().code]) or (dictName == 'yearsAD' and date.maxyearAD.has_key(page.site().code) and value > date.maxyearAD[page.site().code]):
+ if not (dictName == 'yearsBC' and date.maxyearBC.has_key(page.site.code) and value > date.maxyearBC[page.site.code]) or (dictName == 'yearsAD' and date.maxyearAD.has_key(page.site.code) and value > date.maxyearAD[page.site.code]):
pywikibot.output(u'TitleTranslate: %s was recognized as %s with value %d' % (page.title(),dictName,value))
for entryLang, entry in date.formats[dictName].iteritems():
- if entryLang != page.site().code:
+ if entryLang != page.site.code:
if dictName == 'yearsBC' and date.maxyearBC.has_key(entryLang) and value > date.maxyearBC[entryLang]:
pass
elif dictName == 'yearsAD' and date.maxyearAD.has_key(entryLang) and value > date.maxyearAD[entryLang]:
@@ -94,7 +94,7 @@
pywikibot.output( u'getting poisoned links for %s' % pl.title() )
- dictName, value = date.getAutoFormat( pl.site().code, pl.title() )
+ dictName, value = date.getAutoFormat( pl.site.code, pl.title() )
if dictName is not None:
pywikibot.output( u'date found in %s' % dictName )
Modified: branches/rewrite/tests/api_tests.py
===================================================================
--- branches/rewrite/tests/api_tests.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/tests/api_tests.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -68,7 +68,7 @@
self.assertEqual(len(results), 4)
for page in results:
self.assertEqual(type(page), pywikibot.Page)
- self.assertEqual(page.site(), mysite)
+ self.assertEqual(page.site, mysite)
self.assert_(page.title() in titles)
Modified: branches/rewrite/tests/page_tests.py
===================================================================
--- branches/rewrite/tests/page_tests.py 2010-06-02 13:00:08 UTC (rev 8237)
+++ branches/rewrite/tests/page_tests.py 2010-06-02 13:50:48 UTC (rev 8238)
@@ -114,7 +114,7 @@
def testSite(self):
"""Test site() method"""
- self.assertEqual(mainpage.site(), site)
+ self.assertEqual(mainpage.site, site)
self.assertEqual(mainpage.encoding(), site.encoding())
def testNamespace(self):