Revision: 7565
Author: xqt
Date: 2009-10-29 15:33:46 +0000 (Thu, 29 Oct 2009)
Log Message:
-----------
import wikipedia as pywikibot for easier comparison with rewrite branch; some fixes
Modified Paths:
--------------
trunk/pywikipedia/imageuncat.py
trunk/pywikipedia/isbn.py
trunk/pywikipedia/replace.py
trunk/pywikipedia/solve_disambiguation.py
trunk/pywikipedia/titletranslate.py
Modified: trunk/pywikipedia/imageuncat.py
===================================================================
--- trunk/pywikipedia/imageuncat.py 2009-10-29 15:29:31 UTC (rev 7564)
+++ trunk/pywikipedia/imageuncat.py 2009-10-29 15:33:46 UTC (rev 7565)
@@ -13,7 +13,8 @@
#
import os, sys, re, codecs
-import wikipedia, config, pagegenerators, query
+import wikipedia as pywikibot
+import config, pagegenerators, query
from datetime import datetime
from datetime import timedelta
@@ -1263,7 +1264,7 @@
'''
Return a pagegenerator containing all the images edited in a certain timespan.
The delay is the amount of minutes to wait and the block is the timespan to return images in.
- Should probably copied to somewhere else
+ Should probably be copied to somewhere else
'''
result = []
@@ -1299,14 +1300,15 @@
'''
Do we want to skip this page?
- If we found a category which is not in the ignore list it means that the page is categorized so skip the page.
+ If we found a category which is not in the ignore list it means
+ that the page is categorized so skip the page.
If we found a template which is in the ignore list, skip the page.
'''
- wikipedia.output(u'Working on '+ page.title())
+ pywikibot.output(u'Working on '+ page.title())
for category in page.categories():
if category not in ignoreCategories:
- wikipedia.output(u'Got category ' + category.title())
+ pywikibot.output(u'Got category ' + category.title())
return False
for templateWithTrail in page.templates():
@@ -1314,13 +1316,13 @@
template = templateWithTrail.rstrip('\n').rstrip()
if template in skipTemplates:
# Already tagged with a template, skip it
- wikipedia.output(u'Already tagged, skip it')
+ pywikibot.output(u'Already tagged, skip it')
return False
elif template in ignoreTemplates:
# template not relevant for categorization
- wikipedia.output(u'Ignore ' + template)
+ pywikibot.output(u'Ignore ' + template)
else:
- wikipedia.output(u'Not ignoring ' + template)
+ pywikibot.output(u'Not ignoring ' + template)
return False
return True
@@ -1329,13 +1331,13 @@
Add the uncat template to the page
'''
newtext = page.get() + puttext
- wikipedia.showDiff(page.get(), newtext)
+ pywikibot.showDiff(page.get(), newtext)
try:
page.put(newtext, putcomment)
- except wikipedia.EditConflict:
+ except pywikibot.EditConflict:
# Skip this page
pass
- except wikipedia.LockedPage:
+ except pywikibot.LockedPage:
# Skip this page
pass
return
@@ -1344,27 +1346,28 @@
'''
Grab a bunch of images and tag them if they are not categorized.
'''
- generator = None;
+ generator = None
genFactory = pagegenerators.GeneratorFactory()
- site = wikipedia.getSite(u'commons', u'commons')
- wikipedia.setSite(site)
- for arg in wikipedia.handleArgs():
+ site = pywikibot.getSite(u'commons', u'commons')
+ pywikibot.setSite(site)
+ for arg in pywikibot.handleArgs():
if arg.startswith('-yesterday'):
generator = uploadedYesterday(site)
elif arg.startswith('-recentchanges'):
generator = recentChanges(site=site, delay=120)
else:
genFactory.handleArg(arg)
-
if not generator:
generator = genFactory.getCombinedGenerator()
if not generator:
- wikipedia.output('You have to specify the generator you want to use for the program!')
+ pywikibot.output(
+ u'You have to specify the generator you want to use for the program!')
else:
pregenerator = pagegenerators.PreloadingGenerator(generator)
for page in pregenerator:
- if page.exists() and (page.namespace() == 6) and (not page.isRedirectPage()) :
+ if page.exists() and (page.namespace() == 6) \
+ and (not page.isRedirectPage()) :
if isUncat(page):
addUncat(page)
@@ -1372,4 +1375,4 @@
try:
main(sys.argv[1:])
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/isbn.py
===================================================================
--- trunk/pywikipedia/isbn.py 2009-10-29 15:29:31 UTC (rev 7564)
+++ trunk/pywikipedia/isbn.py 2009-10-29 15:33:46 UTC (rev 7565)
@@ -38,7 +38,8 @@
__version__='$Id$'
-import wikipedia, pagegenerators
+import wikipedia as pywikibot
+import pagegenerators
import sys, re
docuReplacements = {
@@ -1174,15 +1175,15 @@
text = page.get(get_redirect = self.touch_redirects)
# convert ISBN numbers
page.put(text)
- except wikipedia.NoPage:
+ except pywikibot.NoPage:
print "Page %s does not exist?!" % page.aslink()
- except wikipedia.IsRedirectPage:
+ except pywikibot.IsRedirectPage:
print "Page %s is a redirect; skipping." % page.aslink()
- except wikipedia.LockedPage:
+ except pywikibot.LockedPage:
print "Page %s is locked?!" % page.aslink()
-class InvalidIsbnException(wikipedia.Error):
+class InvalidIsbnException(pywikibot.Error):
"""Invalid ISBN"""
class ISBN:
@@ -1204,7 +1205,7 @@
result += prefix + '-'
rest = rest[len(prefix):]
break
-
+
# Determine the group
for groupNumber in ranges.iterkeys():
if rest.startswith(groupNumber):
@@ -1393,7 +1394,7 @@
try:
getIsbn(code)
except InvalidIsbnException, e:
- wikipedia.output(e)
+ pywikibot.output(e)
newText = oldText
if self.to13:
@@ -1402,21 +1403,21 @@
if self.format:
newText = self.isbnR.sub(_hyphenateIsbnNumber, newText)
self.save(page, newText)
- except wikipedia.NoPage:
- wikipedia.output(u"Page %s does not exist?!" % page.aslink())
- except wikipedia.IsRedirectPage:
- wikipedia.output(u"Page %s is a redirect; skipping." % page.aslink())
- except wikipedia.LockedPage:
- wikipedia.output(u"Page %s is locked?!" % page.aslink())
+ except pywikibot.NoPage:
+ pywikibot.output(u"Page %s does not exist?!" % page.aslink())
+ except pywikibot.IsRedirectPage:
+ pywikibot.output(u"Page %s is a redirect; skipping." % page.aslink())
+ except pywikibot.LockedPage:
+ pywikibot.output(u"Page %s is locked?!" % page.aslink())
def save(self, page, text):
if text != page.get():
# Show the title of the page we're working on.
# Highlight the title in purple.
- wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
- wikipedia.showDiff(page.get(), text)
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
+ pywikibot.showDiff(page.get(), text)
if not self.always:
- choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'Always yes'], ['y', 'N', 'a'], 'N')
+ choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'Always yes'], ['y', 'N', 'a'], 'N')
if choice == 'n':
return
elif choice == 'a':
@@ -1425,20 +1426,20 @@
if self.always:
try:
page.put(text)
- except wikipedia.EditConflict:
- wikipedia.output(u'Skipping %s because of edit conflict' % (page.title(),))
- except wikipedia.SpamfilterError, e:
- wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url))
- except wikipedia.LockedPage:
- wikipedia.output(u'Skipping %s (locked page)' % (page.title(),))
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Skipping %s because of edit conflict' % (page.title(),))
+ except pywikibot.SpamfilterError, e:
+ pywikibot.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url))
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Skipping %s (locked page)' % (page.title(),))
else:
# Save the page in the background. No need to catch exceptions.
page.put_async(text)
def run(self):
- comment = wikipedia.translate(wikipedia.getSite(), msg)
- wikipedia.setAction(comment)
+ comment = pywikibot.translate(pywikibot.getSite(), msg)
+ pywikibot.setAction(comment)
for page in self.generator:
self.treat(page)
@@ -1462,7 +1463,7 @@
to13 = False
format = False
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg.startswith('-namespace:'):
try:
namespaces.append(int(arg[11:]))
@@ -1479,12 +1480,12 @@
pageTitle.append(arg)
if pageTitle:
- page = wikipedia.Page(wikipedia.getSite(), ' '.join(pageTitle))
+ page = pywikibot.Page(pywikibot.getSite(), ' '.join(pageTitle))
gen = iter([page])
if not gen:
gen = genFactory.getCombinedGenerator()
if not gen:
- wikipedia.showHelp('isbn')
+ pywikibot.showHelp('isbn')
else:
if namespaces != []:
gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces)
@@ -1496,4 +1497,4 @@
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/replace.py
===================================================================
--- trunk/pywikipedia/replace.py 2009-10-29 15:29:31 UTC (rev 7564)
+++ trunk/pywikipedia/replace.py 2009-10-29 15:33:46 UTC (rev 7565)
@@ -114,15 +114,18 @@
python replace.py referer referrer -file:typos.txt -excepttext:HTTP
"""
+from __future__ import generators
#
-# (C) Daniel Herding & the Pywikipediabot Team, 2004-2008
+# (C) Daniel Herding & the Pywikipedia team, 2004-2009
#
+__version__='$Id$'
+#
# Distributed under the terms of the MIT license.
#
-from __future__ import generators
import sys, re, time
-import wikipedia, pagegenerators
+import wikipedia as pywikibot
+import pagegenerators
import editarticle
import webbrowser
@@ -136,11 +139,10 @@
'&fixes-help;': fixes.help,
}
-__version__='$Id$'
# Summary messages in different languages
# NOTE: Predefined replacement tasks might use their own dictionary, see 'fixes'
-# below.`v
+# below.
msg = {
'ar': u'%s روبوت : استبدال تلقائي للنص',
'ca': u'Robot: Reemplaçament automàtic de text %s',
@@ -202,12 +204,12 @@
self.skipping = bool(xmlStart)
self.excsInside = []
- if 'inside-tags' in self.exceptions:
+ if "inside-tags" in self.exceptions:
self.excsInside += self.exceptions['inside-tags']
- if 'inside' in self.exceptions:
+ if "inside" in self.exceptions:
self.excsInside += self.exceptions['inside']
import xmlreader
- self.site = wikipedia.getSite()
+ self.site = pywikibot.getSite()
dump = xmlreader.XmlDump(self.xmlFilename)
self.parser = dump.parse()
@@ -222,24 +224,24 @@
and not self.isTextExcepted(entry.text):
new_text = entry.text
for old, new in self.replacements:
- new_text = wikipedia.replaceExcept(new_text, old, new, self.excsInside, self.site)
+ new_text = pywikibot.replaceExcept(new_text, old, new, self.excsInside, self.site)
if new_text != entry.text:
- yield wikipedia.Page(self.site, entry.title)
+ yield pywikibot.Page(self.site, entry.title)
except KeyboardInterrupt:
try:
if not self.skipping:
- wikipedia.output(
+ pywikibot.output(
u'To resume, use "-xmlstart:%s" on the command line.'
% entry.title)
except NameError:
pass
def isTitleExcepted(self, title):
- if 'title' in self.exceptions:
+ if "title" in self.exceptions:
for exc in self.exceptions['title']:
if exc.search(title):
return True
- if 'require-title' in self.exceptions:
+ if "require-title" in self.exceptions:
for req in self.exceptions['require-title']:
if not req.search(title): # if not all requirements are met:
return True
@@ -247,7 +249,7 @@
return False
def isTextExcepted(self, text):
- if 'text-contains' in self.exceptions:
+ if "text-contains" in self.exceptions:
for exc in self.exceptions['text-contains']:
if exc.search(text):
return True
@@ -292,7 +294,7 @@
regular expressions.
inside-tags
A list of strings. These strings must be keys from the
- exceptionRegexes dictionary in wikipedia.replaceExcept().
+ exceptionRegexes dictionary in pywikibot.replaceExcept().
"""
self.generator = generator
@@ -301,22 +303,22 @@
self.acceptall = acceptall
self.allowoverlap = allowoverlap
self.recursive = recursive
+ if addedCat:
+ site = pywikibot.getSite()
+ self.addedCat = pywikibot.Page(site, addCat, defaultNamespace=14)
+ self.sleep = sleep
# Some function to set default editSummary should probably be added
self.editSummary = editSummary
- if addedCat:
- site = wikipedia.getSite()
- self.addedCat = wikipedia.Page(site, addCat, defaultNamespace=14)
- self.sleep = sleep
def isTitleExcepted(self, title):
"""
Iff one of the exceptions applies for the given title, returns True.
"""
- if 'title' in self.exceptions:
+ if "title" in self.exceptions:
for exc in self.exceptions['title']:
if exc.search(title):
return True
- if 'require-title' in self.exceptions:
+ if "require-title" in self.exceptions:
for req in self.exceptions['require-title']:
if not req.search(title):
return True
@@ -327,7 +329,7 @@
Iff one of the exceptions applies for the given page contents,
returns True.
"""
- if 'text-contains' in self.exceptions:
+ if "text-contains" in self.exceptions:
for exc in self.exceptions['text-contains']:
if exc.search(original_text):
return True
@@ -340,14 +342,14 @@
"""
new_text = original_text
exceptions = []
- if 'inside-tags' in self.exceptions:
+ if "inside-tags" in self.exceptions:
exceptions += self.exceptions['inside-tags']
- if 'inside' in self.exceptions:
+ if "inside" in self.exceptions:
exceptions += self.exceptions['inside']
for old, new in self.replacements:
if self.sleep is not None:
time.sleep(self.sleep)
- new_text = wikipedia.replaceExcept(new_text, old, new, exceptions,
+ new_text = pywikibot.replaceExcept(new_text, old, new, exceptions,
allowoverlap=self.allowoverlap)
return new_text
@@ -359,7 +361,7 @@
# changed.
for page in self.generator:
if self.isTitleExcepted(page.title()):
- wikipedia.output(
+ pywikibot.output(
u'Skipping %s because the title is on the exceptions list.'
% page.aslink())
continue
@@ -367,23 +369,23 @@
# Load the page's text from the wiki
original_text = page.get(get_redirect=True)
if not page.canBeEdited():
- wikipedia.output(u"You can't edit page %s"
+ pywikibot.output(u"You can't edit page %s"
% page.aslink())
continue
- except wikipedia.NoPage:
- wikipedia.output(u'Page %s not found' % page.aslink())
+ except pywikibot.NoPage:
+ pywikibot.output(u'Page %s not found' % page.aslink())
continue
new_text = original_text
while True:
if self.isTextExcepted(new_text):
- wikipedia.output(
+ pywikibot.output(
u'Skipping %s because it contains text that is on the exceptions list.'
% page.aslink())
break
new_text = self.doReplacements(new_text)
if new_text == original_text:
- wikipedia.output('No changes were necessary in %s'
- % page.aslink())
+ pywikibot.output(u'No changes were necessary in %s'
+ % page.aslink())
break
if self.recursive:
newest_text = self.doReplacements(new_text)
@@ -394,16 +396,16 @@
cats = page.categories()
if self.addedCat not in cats:
cats.append(self.addedCat)
- new_text = wikipedia.replaceCategoryLinks(new_text,
+ new_text = pywikibot.replaceCategoryLinks(new_text,
cats)
# Show the title of the page we're working on.
# Highlight the title in purple.
- wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
% page.title())
- wikipedia.showDiff(original_text, new_text)
+ pywikibot.showDiff(original_text, new_text)
if self.acceptall:
break
- choice = wikipedia.inputChoice(
+ choice = pywikibot.inputChoice(
u'Do you want to accept these changes?',
['Yes', 'No', 'Edit', 'open in Browser', 'All', "Quit"],
['y', 'N', 'e', 'b', 'a', 'q'], 'N')
@@ -419,7 +421,7 @@
page.site().hostname(),
page.site().nice_get_address(page.title())
))
- wikipedia.input("Press Enter when finished in browser.")
+ pywikibot.input("Press Enter when finished in browser.")
original_text = page.get(get_redirect=True, force=True)
new_text = original_text
continue
@@ -434,18 +436,18 @@
if self.acceptall and new_text != original_text:
try:
page.put(new_text, self.editSummary)
- except wikipedia.EditConflict:
- wikipedia.output(u'Skipping %s because of edit conflict'
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Skipping %s because of edit conflict'
% (page.title(),))
- except wikipedia.SpamfilterError, e:
- wikipedia.output(
+ except pywikibot.SpamfilterError, e:
+ pywikibot.output(
u'Cannot change %s because of blacklist entry %s'
% (page.title(), e.url))
- except wikipedia.PageNotSaved, error:
- wikipedia.output(u'Error putting page: %s'
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u'Error putting page: %s'
% (error.args,))
- except wikipedia.LockedPage:
- wikipedia.output(u'Skipping %s (locked page)'
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Skipping %s (locked page)'
% (page.title(),))
def prepareRegexForMySQL(pattern):
@@ -510,24 +512,24 @@
genFactory = pagegenerators.GeneratorFactory()
# Load default summary message.
# BUG WARNING: This is probably incompatible with the -lang parameter.
- editSummary = wikipedia.translate(wikipedia.getSite(), msg)
+ editSummary = pywikibot.translate(pywikibot.getSite(), msg)
# Between a regex and another (using -fix) sleep some time (not to waste
# too much CPU
sleep = None
# Read commandline parameters.
- for arg in wikipedia.handleArgs(*args):
+ for arg in pywikibot.handleArgs(*args):
if arg == '-regex':
regex = True
elif arg.startswith('-xmlstart'):
if len(arg) == 9:
- xmlStart = wikipedia.input(
+ xmlStart = pywikibot.input(
u'Please enter the dumped article to start with:')
else:
xmlStart = arg[10:]
elif arg.startswith('-xml'):
if len(arg) == 4:
- xmlFilename = wikipedia.input(
+ xmlFilename = pywikibot.input(
u'Please enter the XML dump\'s filename:')
else:
xmlFilename = arg[5:]
@@ -535,7 +537,7 @@
useSql = True
elif arg.startswith('-page'):
if len(arg) == 5:
- PageTitles.append(wikipedia.input(
+ PageTitles.append(pywikibot.input(
u'Which page do you want to change?'))
else:
PageTitles.append(arg[6:])
@@ -564,9 +566,9 @@
elif arg == '-multiline':
multiline = True
elif arg.startswith('-addcat:'):
- add_cat = arg[len('addcat:'):]
+ add_cat = arg[8:]
elif arg.startswith('-summary:'):
- editSummary = arg[len('-summary:'):]
+ editSummary = arg[9:]
summary_commandline = True
elif arg.startswith('-allowoverlap'):
allowoverlap = True
@@ -577,47 +579,47 @@
commandline_replacements.append(arg)
if (len(commandline_replacements) % 2):
- raise wikipedia.Error, 'require even number of replacements.'
+ raise pywikibot.Error, 'require even number of replacements.'
elif (len(commandline_replacements) == 2 and fix is None):
replacements.append((commandline_replacements[0],
commandline_replacements[1]))
- if summary_commandline == False:
- editSummary = wikipedia.translate(wikipedia.getSite(), msg ) % (' (-' + commandline_replacements[0] + ' +'
+ if not summary_commandline:
+ editSummary = pywikibot.translate(pywikibot.getSite(), msg ) % (' (-' + commandline_replacements[0] + ' +'
+ commandline_replacements[1] + ')')
elif (len(commandline_replacements) > 1):
if (fix is None):
for i in xrange (0, len(commandline_replacements), 2):
replacements.append((commandline_replacements[i],
commandline_replacements[i + 1]))
- if summary_commandline == False:
+ if not summary_commandline:
pairs = [( commandline_replacements[i],
commandline_replacements[i + 1] )
for i in range(0, len(commandline_replacements), 2)]
replacementsDescription = '(%s)' % ', '.join(
[('-' + pair[0] + ' +' + pair[1]) for pair in pairs])
- editSummary = wikipedia.translate(wikipedia.getSite(), msg ) % replacementsDescription
+ editSummary = pywikibot.translate(pywikibot.getSite(), msg ) % replacementsDescription
else:
- raise wikipedia.Error(
+ raise pywikibot.Error(
'Specifying -fix with replacements is undefined')
elif fix is None:
- old = wikipedia.input(u'Please enter the text that should be replaced:')
- new = wikipedia.input(u'Please enter the new text:')
+ old = pywikibot.input(u'Please enter the text that should be replaced:')
+ new = pywikibot.input(u'Please enter the new text:')
change = '(-' + old + ' +' + new
replacements.append((old, new))
while True:
- old = wikipedia.input(
+ old = pywikibot.input(
u'Please enter another text that should be replaced, or press Enter to start:')
if old == '':
change = change + ')'
break
- new = wikipedia.input(u'Please enter the new text:')
+ new = pywikibot.input(u'Please enter the new text:')
change = change + ' & -' + old + ' +' + new
replacements.append((old, new))
- if not summary_commandline == True:
- default_summary_message = wikipedia.translate(wikipedia.getSite(), msg) % change
- wikipedia.output(u'The summary message will default to: %s'
+ if not summary_commandline:
+ default_summary_message = pywikibot.translate(pywikibot.getSite(), msg) % change
+ pywikibot.output(u'The summary message will default to: %s'
% default_summary_message)
- summary_message = wikipedia.input(
+ summary_message = pywikibot.input(
u'Press Enter to use this default message, or enter a description of the\nchanges your bot will make:')
if summary_message == '':
summary_message = default_summary_message
@@ -628,16 +630,16 @@
try:
fix = fixes.fixes[fix]
except KeyError:
- wikipedia.output(u'Available predefined fixes are: %s'
+ pywikibot.output(u'Available predefined fixes are: %s'
% fixes.fixes.keys())
return
- if 'regex' in fix:
+ if "regex" in fix:
regex = fix['regex']
- if 'msg' in fix:
- editSummary = wikipedia.translate(wikipedia.getSite(), fix['msg'])
- if 'exceptions' in fix:
+ if "msg" in fix:
+ editSummary = pywikibot.translate(pywikibot.getSite(), fix['msg'])
+ if "exceptions" in fix:
exceptions = fix['exceptions']
- if 'nocase' in fix:
+ if "nocase" in fix:
caseInsensitive = fix['nocase']
replacements = fix['replacements']
@@ -692,14 +694,14 @@
LIMIT 200""" % (whereClause, exceptClause)
gen = pagegenerators.MySQLPageGenerator(query)
elif PageTitles:
- pages = [wikipedia.Page(wikipedia.getSite(), PageTitle)
+ pages = [pywikibot.Page(pywikibot.getSite(), PageTitle)
for PageTitle in PageTitles]
gen = iter(pages)
gen = genFactory.getCombinedGenerator(gen)
if not gen:
# syntax error, show help text from the top of this file
- wikipedia.showHelp('replace')
+ pywikibot.showHelp('replace')
return
if xmlFilename:
# XML parsing can be quite slow, so use smaller batches and
@@ -711,8 +713,9 @@
bot = ReplaceRobot(preloadingGen, replacements, exceptions, acceptall, allowoverlap, recursive, add_cat, sleep, editSummary)
bot.run()
+
if __name__ == "__main__":
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/solve_disambiguation.py
===================================================================
--- trunk/pywikipedia/solve_disambiguation.py 2009-10-29 15:29:31 UTC (rev 7564)
+++ trunk/pywikipedia/solve_disambiguation.py 2009-10-29 15:33:46 UTC (rev 7565)
@@ -70,6 +70,7 @@
# (C) Daniel Herding, 2004
# (C) Andre Engels, 2003-2004
# (C) WikiWichtel, 2004
+# (C) Pywikipedia team, 2003-2009
#
__version__='$Id$'
#
@@ -81,7 +82,8 @@
import re, sys, codecs
# Application specific imports
-import wikipedia, pagegenerators, editarticle
+import wikipedia as pywikibot
+import pagegenerators, editarticle
# Summary message when working on disambiguation pages
msg = {
@@ -418,23 +420,23 @@
def __iter__(self):
# TODO: start yielding before all referring pages have been found
refs = [page for page in self.disambPage.getReferences(follow_redirects = False, withTemplateInclusion = False)]
- wikipedia.output(u"Found %d references." % len(refs))
+ 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]:
for i in range(len(refs)-1, -1, -1):
if re.match(ig, refs[i].title()):
- if wikipedia.verbose:
- wikipedia.output('Ignoring page %s'
+ if pywikibot.verbose:
+ pywikibot.output('Ignoring page %s'
% refs[i].title())
del refs[i]
elif self.primaryIgnoreManager.isIgnored(refs[i]):
- #wikipedia.output('Ignoring page %s because it was skipped before' % refs[i].title())
+ #pywikibot.output('Ignoring page %s because it was skipped before' % refs[i].title())
del refs[i]
if len(refs) < self.minimum:
- wikipedia.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
- wikipedia.output(u"Will work on %d pages." % len(refs))
+ pywikibot.output(u"Will work on %d pages." % len(refs))
for ref in refs:
yield ref
@@ -449,7 +451,7 @@
self.enabled = enabled
self.ignorelist = []
- filename = wikipedia.config.datafilepath('disambiguations',
+ filename = pywikibot.config.datafilepath('disambiguations',
self.disambPage.titleForFilename() + '.txt')
try:
# The file is stored in the disambiguation/ subdir.
@@ -472,7 +474,7 @@
def ignore(self, refPage):
if self.enabled:
# Skip this occurence next time.
- filename = wikipedia.config.datafilepath('disambiguations',
+ filename = pywikibot.config.datafilepath('disambiguations',
self.disambPage.urlname() + '.txt')
try:
# Open file for appending. If none exists yet, create a new one.
@@ -516,7 +518,7 @@
self.main_only = main_only
self.minimum = minimum
- self.mysite = wikipedia.getSite()
+ self.mysite = pywikibot.getSite()
self.mylang = self.mysite.language()
self.comment = None
@@ -547,7 +549,7 @@
list = u'\n'
for i in range(len(self.alternatives)):
list += (u"%3i - %s\n" % (i, self.alternatives[i]))
- wikipedia.output(list)
+ pywikibot.output(list)
def setupRegexes(self):
# compile regular expressions
@@ -569,8 +571,8 @@
def treat(self, refPage, disambPage):
"""
Parameters:
- disambPage - The disambiguation page or redirect we don't want anything
- to link on
+ disambPage - The disambiguation page or redirect we don't want
+ anything to link to
refPage - A page linking to disambPage
Returns False if the user pressed q to completely quit the program.
Otherwise, returns True.
@@ -585,22 +587,22 @@
text=refPage.get(throttle=False)
ignoreReason = self.checkContents(text)
if ignoreReason:
- wikipedia.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 wikipedia.IsRedirectPage:
- wikipedia.output(u'%s is a redirect to %s' % (refPage.title(), disambPage.title()))
+ except pywikibot.IsRedirectPage:
+ pywikibot.output(u'%s is a redirect to %s' % (refPage.title(), disambPage.title()))
if disambPage.isRedirectPage():
target = self.alternatives[0]
- choice = wikipedia.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)
try:
refPage.put_async(redir_text,comment=self.comment)
- except wikipedia.PageNotSaved, error:
- wikipedia.output(u'Page not saved: %s' % error.args)
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u'Page not saved: %s' % error.args)
else:
- choice = wikipedia.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)
preloadingGen = pagegenerators.PreloadingGenerator(gen)
@@ -611,8 +613,8 @@
elif choice == 'c':
text=refPage.get(throttle=False,get_redirect=True)
include = "redirect"
- except wikipedia.NoPage:
- wikipedia.output(u'Page [[%s]] does not seem to exist?! Skipping.' % refPage.title())
+ except pywikibot.NoPage:
+ 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
@@ -625,7 +627,7 @@
m = self.linkR.search(text, pos = curpos)
if not m:
if n == 0:
- wikipedia.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
@@ -637,9 +639,9 @@
continue
else:
try:
- linkPage = wikipedia.Page(disambPage.site(), m.group('title'))
+ linkPage = pywikibot.Page(disambPage.site(), m.group('title'))
# Check whether the link found is to disambPage.
- except wikipedia.InvalidTitle:
+ except pywikibot.InvalidTitle:
continue
if linkPage != disambPage:
continue
@@ -652,23 +654,23 @@
while True:
# Show the title of the page where the link was found.
# Highlight the title in purple.
- wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % refPage.title())
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % refPage.title())
# at the beginning of the link, start red color.
# at the end of the link, reset the color to default
- wikipedia.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 not self.always:
if edited:
- choice = wikipedia.input(u"Option (#, r#, s=skip link, e=edit page, n=next page, u=unlink, q=quit\n"
+ choice = pywikibot.input(u"Option (#, r#, s=skip link, e=edit page, n=next page, u=unlink, q=quit\n"
" m=more context, l=list, a=add new, x=save in this form):")
else:
- choice = wikipedia.input(u"Option (#, r#, s=skip link, e=edit page, n=next page, u=unlink, q=quit\n"
+ choice = pywikibot.input(u"Option (#, r#, s=skip link, e=edit page, n=next page, u=unlink, q=quit\n"
" m=more context, d=show disambiguation page, l=list, a=add new):")
else:
choice = self.always
if choice in ['a', 'A']:
- newAlternative = wikipedia.input(u'New alternative:')
+ newAlternative = pywikibot.input(u'New alternative:')
self.alternatives.append(newAlternative)
self.listAlternatives()
elif choice in ['e', 'E']:
@@ -749,19 +751,21 @@
try:
choice=int(choice)
except ValueError:
- wikipedia.output(u"Unknown option")
- # step back to ask the user again what to do with the current link
+ pywikibot.output(u"Unknown option")
+ # step back to ask the user again what to do with the
+ # current link
curpos -= 1
continue
if choice >= len(self.alternatives) or choice < 0:
- wikipedia.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 current link
+ # step back to ask the user again what to do with the
+ # current link
curpos -= 1
continue
new_page_title = self.alternatives[choice]
- repPl = wikipedia.Page(disambPage.site(), new_page_title)
+ repPl = pywikibot.Page(disambPage.site(), new_page_title)
if (new_page_title[0].isupper()) or (link_text[0].isupper()):
new_page_title = repPl.title()
else:
@@ -781,21 +785,21 @@
text = text[:m.start()] + newlink + text[m.end():]
continue
- wikipedia.output(text[max(0,m.start()-30):m.end()+30])
+ pywikibot.output(text[max(0,m.start()-30):m.end()+30])
if text == original_text:
- wikipedia.output(u'\nNo changes have been made:\n')
+ pywikibot.output(u'\nNo changes have been made:\n')
else:
- wikipedia.output(u'\nThe following changes have been made:\n')
- wikipedia.showDiff(original_text, text)
- wikipedia.output(u'')
+ pywikibot.output(u'\nThe following changes have been made:\n')
+ pywikibot.showDiff(original_text, text)
+ pywikibot.output(u'')
# save the page
self.setSummaryMessage(disambPage, new_targets, unlink)
try:
refPage.put_async(text,comment=self.comment)
- except wikipedia.LockedPage:
- wikipedia.output(u'Page not saved: page is locked')
- except wikipedia.PageNotSaved, error:
- wikipedia.output(u'Page not saved: %s' % error.args)
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Page not saved: page is locked')
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u'Page not saved: %s' % error.args)
return True
def findAlternatives(self, disambPage):
@@ -807,11 +811,11 @@
baseTerm = template[1][1]
disambTitle = primary_topic_format[self.mylang] % baseTerm
try:
- disambPage2 = wikipedia.Page(self.mysite, disambTitle)
+ disambPage2 = pywikibot.Page(self.mysite, disambTitle)
links = disambPage2.linkedPages()
links = [correctcap(l,disambPage2.get()) for l in links]
- except wikipedia.NoPage:
- wikipedia.output(u"No page at %s, using redirect target." % disambTitle)
+ 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)) for l in links]
self.alternatives += links
@@ -819,42 +823,42 @@
try:
target = disambPage.getRedirectTarget().title()
self.alternatives.append(target)
- except wikipedia.NoPage:
- wikipedia.output(u"The specified page was not found.")
- user_input = wikipedia.input(u"""\
+ except pywikibot.NoPage:
+ pywikibot.output(u"The specified page was not found.")
+ user_input = pywikibot.input(u"""\
Please enter the name of the page where the redirect should have pointed at,
or press enter to quit:""")
if user_input == "":
sys.exit(1)
else:
self.alternatives.append(user_input)
- except wikipedia.IsNotRedirectPage:
- wikipedia.output(
+ except pywikibot.IsNotRedirectPage:
+ pywikibot.output(
u"The specified page is not a redirect. Skipping.")
return False
elif self.getAlternatives:
try:
if self.primary:
try:
- disambPage2 = wikipedia.Page(self.mysite,
+ disambPage2 = pywikibot.Page(self.mysite,
primary_topic_format[self.mylang]
% disambPage.title()
)
links = disambPage2.linkedPages()
links = [correctcap(l,disambPage2.get()) for l in links]
- except wikipedia.NoPage:
- wikipedia.output(u"Page does not exist, using the first link in page %s." % disambPage.title())
+ except pywikibot.NoPage:
+ 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]
else:
try:
links = disambPage.linkedPages()
links = [correctcap(l,disambPage.get()) for l in links]
- except wikipedia.NoPage:
- wikipedia.output(u"Page does not exist, skipping.")
+ except pywikibot.NoPage:
+ pywikibot.output(u"Page does not exist, skipping.")
return False
- except wikipedia.IsRedirectPage:
- wikipedia.output(u"Page is a redirect, skipping.")
+ except pywikibot.IsRedirectPage:
+ pywikibot.output(u"Page is a redirect, skipping.")
return False
self.alternatives += links
return True
@@ -868,32 +872,32 @@
targets = targets[:-2]
if not targets:
- targets = wikipedia.translate(self.mysite, unknown_msg)
+ targets = pywikibot.translate(self.mysite, unknown_msg)
# first check whether user has customized the edit comment
- if self.mysite.family.name in wikipedia.config.disambiguation_comment and self.mylang in wikipedia.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 = wikipedia.translate(self.mysite,
- wikipedia.config.disambiguation_comment[
+ 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 = wikipedia.translate(self.mysite,
- wikipedia.config.disambiguation_comment[
+ 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 = wikipedia.translate(self.mysite, msg_redir_unlink) % disambPage.title()
+ self.comment = pywikibot.translate(self.mysite, msg_redir_unlink) % disambPage.title()
else:
- self.comment = wikipedia.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 = wikipedia.translate(self.mysite, msg_unlink) % disambPage.title()
+ self.comment = pywikibot.translate(self.mysite, msg_unlink) % disambPage.title()
else:
- self.comment = wikipedia.translate(self.mysite, msg) % (disambPage.title(), targets)
+ self.comment = pywikibot.translate(self.mysite, msg) % (disambPage.title(), targets)
def run(self):
if self.main_only:
@@ -912,7 +916,7 @@
self.makeAlternativesUnique()
# sort possible choices
- if wikipedia.config.sort_ignore_case:
+ if pywikibot.config.sort_ignore_case:
self.alternatives.sort(lambda x,y: cmp(x.lower(), y.lower()))
else:
self.alternatives.sort()
@@ -948,7 +952,7 @@
ignoreCase = False
minimum = 0
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg.startswith('-primary:'):
primary = True
getAlternatives = False
@@ -964,12 +968,12 @@
generator = pagegenerators.TextfilePageGenerator(filename = arg[6:])
elif arg.startswith('-pos:'):
if arg[5]!=':':
- mysite = wikipedia.getSite()
- page = wikipedia.Page(mysite, arg[5:])
+ mysite = pywikibot.getSite()
+ page = pywikibot.Page(mysite, arg[5:])
if page.exists():
alternatives.append(page.title())
else:
- answer = wikipedia.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())
@@ -984,17 +988,17 @@
elif arg.startswith('-start'):
try:
if len(arg) <= len('-start:'):
- generator = pagegenerators.CategorizedPageGenerator(wikipedia.getSite().disambcategory())
+ generator = pagegenerators.CategorizedPageGenerator(pywikibot.getSite().disambcategory())
else:
- generator = pagegenerators.CategorizedPageGenerator(wikipedia.getSite().disambcategory(), start = arg[7:])
+ generator = pagegenerators.CategorizedPageGenerator(pywikibot.getSite().disambcategory(), start = arg[7:])
generator = pagegenerators.NamespaceFilterPageGenerator(generator, [0])
- except wikipedia.NoPage:
+ except pywikibot.NoPage:
print "Disambiguation category for your wiki is not known."
raise
elif arg.startswith("-"):
print "Unrecognized command line argument: %s" % arg
# show help text and exit
- wikipedia.showHelp()
+ pywikibot.showHelp()
else:
pageTitle.append(arg)
@@ -1002,14 +1006,14 @@
# connect the title's parts with spaces
if pageTitle != []:
pageTitle = ' '.join(pageTitle)
- page = wikipedia.Page(wikipedia.getSite(), pageTitle)
+ page = pywikibot.Page(pywikibot.getSite(), pageTitle)
generator = iter([page])
# if no disambiguation pages was given as an argument, and none was
# read from a file, query the user
if not generator:
- pageTitle = wikipedia.input(u'On which disambiguation page do you want to work?')
- page = wikipedia.Page(wikipedia.getSite(), pageTitle)
+ 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)
@@ -1021,4 +1025,4 @@
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/titletranslate.py
===================================================================
--- trunk/pywikipedia/titletranslate.py 2009-10-29 15:29:31 UTC (rev 7564)
+++ trunk/pywikipedia/titletranslate.py 2009-10-29 15:33:46 UTC (rev 7565)
@@ -9,7 +9,8 @@
#
import re
-import wikipedia, date
+import wikipedia as pywikibot
+import date
def translate(page, hints = None, auto = True, removebrackets = False):
"""
@@ -53,12 +54,12 @@
for newcode in codes:
if newcode in site.languages():
if newcode != site.language():
- x = wikipedia.Page(site.getSite(code=newcode), newname)
+ x = pywikibot.Page(site.getSite(code=newcode), newname)
if x not in result:
result.append(x)
else:
- if wikipedia.verbose:
- wikipedia.output(u"Ignoring unknown language code %s"%newcode)
+ if pywikibot.verbose:
+ pywikibot.output(u"Ignoring unknown language code %s"%newcode)
# Autotranslate dates into all other languages, the rest will come from existing interwiki links.
if auto:
@@ -66,7 +67,7 @@
dictName, value = date.getAutoFormat( page.site().language(), page.title() )
if dictName:
if not (dictName == 'yearsBC' and page.site().language() in date.maxyearBC and value > date.maxyearBC[page.site().language()]) or (dictName == 'yearsAD' and page.site().language() in date.maxyearAD and value > date.maxyearAD[page.site().language()]):
- wikipedia.output(u'TitleTranslate: %s was recognized as %s with value %d' % (page.title(),dictName,value))
+ 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().language():
if dictName == 'yearsBC' and entryLang in date.maxyearBC and value > date.maxyearBC[entryLang]:
@@ -75,7 +76,7 @@
pass
else:
newname = entry(value)
- x = wikipedia.Page( wikipedia.getSite(code=entryLang, fam=site.family), newname )
+ x = pywikibot.Page( pywikibot.getSite(code=entryLang, fam=site.family), newname )
if x not in result:
result.append(x) # add new page
return result
@@ -91,11 +92,11 @@
"""
result = []
- wikipedia.output( u'getting poisoned links for %s' % pl.title() )
+ pywikibot.output( u'getting poisoned links for %s' % pl.title() )
dictName, value = date.getAutoFormat( pl.site().language(), pl.title() )
if dictName is not None:
- wikipedia.output( u'date found in %s' % dictName )
+ pywikibot.output( u'date found in %s' % dictName )
# errors in year BC
if dictName in date.bcFormats:
Revision: 7564
Author: xqt
Date: 2009-10-29 15:29:31 +0000 (Thu, 29 Oct 2009)
Log Message:
-----------
+fi +uk, typo
Modified Paths:
--------------
branches/rewrite/scripts/imageuncat.py
branches/rewrite/scripts/replace.py
Modified: branches/rewrite/scripts/imageuncat.py
===================================================================
--- branches/rewrite/scripts/imageuncat.py 2009-10-29 14:08:38 UTC (rev 7563)
+++ branches/rewrite/scripts/imageuncat.py 2009-10-29 15:29:31 UTC (rev 7564)
@@ -1268,7 +1268,6 @@
Return a pagegenerator containing all the images edited in a certain timespan.
The delay is the amount of minutes to wait and the block is the timespan to return images in.
Should probably be copied to somewhere else
-
'''
rcstart = site.getcurrenttime() + timedelta(minutes=-delay-block)
rcend = site.getcurrenttime() + timedelta(minutes=-delay)
Modified: branches/rewrite/scripts/replace.py
===================================================================
--- branches/rewrite/scripts/replace.py 2009-10-29 14:08:38 UTC (rev 7563)
+++ branches/rewrite/scripts/replace.py 2009-10-29 15:29:31 UTC (rev 7564)
@@ -83,6 +83,8 @@
text. If the -regex argument is given, the first argument
will be regarded as a regular expression, and the second
argument might contain expressions like \\1 or \g<name>.
+ It is possible to introduce more than one pair of old text
+ and replacement.
Examples:
@@ -97,6 +99,9 @@
python replace.py -xml:foobar.xml "Errror" "Error" -namespace:0
+If you want to do more than one replacement at a time, use this:
+ python replace.py -xml:foobar.xml "Errror" "Error" "Faail" "Fail" -namespace:0
+
If you have a page called 'John Doe' and want to fix the format of ISBNs, use:
python replace.py -page:John_Doe -fix:isbn
@@ -144,7 +149,8 @@
'en': u'Robot: Automated text replacement %s',
'es': u'Robot: Reemplazo automático de texto %s',
'fa': u'ربات: تغییر خودکار متن %s',
- 'fr': u'Bot : Remplacement de texte automatisé %s',
+ 'fi': u'Botti korvasi automaattisesti tekstin %s',
+ 'fr': u'Robot : Remplacement de texte automatisé %s',
'he': u'בוט: החלפת טקסט אוטומטית %s',
'hu': u'Robot: Automatikus szövegcsere %s',
'ia': u'Robot: Reimplaciamento automatic de texto %s',
@@ -166,6 +172,7 @@
'ru': u'Робот: Автоматизированная замена текста %s',
'sr': u'Бот: Аутоматска замена текста %s',
'sv': u'Bot: Automatisk textersättning: %s',
+ 'uk': u'Бот: Автоматизована заміна тексту: %s',
'zh': u'機器人:執行文字代換作業 %s',
}
@@ -555,7 +562,7 @@
elif arg == '-multiline':
multiline = True
elif arg.startswith('-addcat:'):
- add_cat = arg[len('-addcat:'):]
+ add_cat = arg[8:]
elif arg.startswith('-summary:'):
edit_summary = arg[9:]
summary_commandline = True
Revision: 7562
Author: xqt
Date: 2009-10-29 12:12:59 +0000 (Thu, 29 Oct 2009)
Log Message:
-----------
cleanUpLinks: no leading space on new lines
Modified Paths:
--------------
trunk/pywikipedia/cosmetic_changes.py
Modified: trunk/pywikipedia/cosmetic_changes.py
===================================================================
--- trunk/pywikipedia/cosmetic_changes.py 2009-10-29 09:10:59 UTC (rev 7561)
+++ trunk/pywikipedia/cosmetic_changes.py 2009-10-29 12:12:59 UTC (rev 7562)
@@ -165,7 +165,8 @@
self.redirect = redirect
self.namespace = namespace
self.template = (self.namespace == 10)
-
+ self.talkpage = self.namespace >= 0 and self.namespace % 2 == 1
+
def change(self, text):
"""
Given a wiki source code text, returns the cleaned up version.
@@ -206,7 +207,7 @@
Makes sure that interwiki links are put to the correct position and
into the right order.
"""
- if pywikibot.calledModuleName() <> 'interwiki':
+ if not self.talkpage and pywikibot.calledModuleName() <> 'interwiki':
interwikiLinks = pywikibot.getLanguageLinks(text, insite = self.site)
text = pywikibot.replaceLanguageLinks(text, interwikiLinks, site = self.site, template = self.template)
return text
@@ -253,6 +254,7 @@
titleWithSection = match.group('titleWithSection')
label = match.group('label')
trailingChars = match.group('linktrail')
+ newline = match.group('newline')
if not self.site.isInterwikiLink(titleWithSection):
# The link looks like this:
@@ -331,7 +333,7 @@
# text[[ title | name ]]text -> text [[title|name]] text
# text[[ title |name]]text -> text[[title|name]]text
# text[[title| name]]text -> text [[title|name]]text
- if hadLeadingSpaces:
+ if hadLeadingSpaces and not newline:
newLink = ' ' + newLink
if hadTrailingSpaces:
newLink = newLink + ' '
@@ -346,7 +348,7 @@
# 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.
- linkR = re.compile(r'\[\[(?P<titleWithSection>[^\]\|]+)(\|(?P<label>[^\]\|]*))?\]\](?P<linktrail>' + self.site.linktrail() + ')')
+ linkR = re.compile(r'(?P<newline>[\n]*)\[\[(?P<titleWithSection>[^\]\|]+)(\|(?P<label>[^\]\|]*))?\]\](?P<linktrail>' + self.site.linktrail() + ')')
text = pywikibot.replaceExcept(text, linkR, handleOneLink, ['comment', 'math', 'nowiki', 'pre', 'startspace'])
return text
Revision: 7558
Author: russblau
Date: 2009-10-28 20:14:24 +0000 (Wed, 28 Oct 2009)
Log Message:
-----------
First implementation of API uploading, and port of scripts/upload.py
Modified Paths:
--------------
branches/rewrite/pywikibot/data/api.py
branches/rewrite/pywikibot/exceptions.py
branches/rewrite/pywikibot/site.py
branches/rewrite/scripts/upload.py
Modified: branches/rewrite/pywikibot/data/api.py
===================================================================
--- branches/rewrite/pywikibot/data/api.py 2009-10-28 18:12:26 UTC (rev 7557)
+++ branches/rewrite/pywikibot/data/api.py 2009-10-28 20:14:24 UTC (rev 7558)
@@ -16,6 +16,7 @@
except ImportError:
import simplejson as json
import logging
+import mimetypes
import re
import traceback
import time
@@ -61,14 +62,20 @@
Attributes of this object (except for the special parameters listed
below) get passed as commands to api.php, and can be get or set using
the dict interface. All attributes must be strings (or unicode). Use
- an empty string for parameters that don't require a value (e.g.,
- "action=query&...&redirects").
+ an empty string for parameters that don't require a value. For example,
+ Request(action="query", titles="Foo bar", prop="info", redirects="")
+ corresponds to the API request
+ "api.php?action=query&titles=Foo%20bar&prop=info&redirects"
This is the lowest-level interface to the API, and can be used for any
request that a particular site's API supports. See the API documentation
(http://www.mediawiki.org/wiki/API) and site-specific settings for
details on what parameters are accepted for each request type.
+ Uploading files is a special case: to upload, the parameter "mime" must
+ be true, and the parameter "file" must be set equal to a valid
+ filename on the local computer, _not_ to the content of the file.
+
Returns a dict containing the JSON data returned by the wiki. Normally,
one of the dict keys will be equal to the value of the 'action'
parameter. Errors are caught and raise an APIError exception.
@@ -95,6 +102,7 @@
@param site: The Site to which the request will be submitted. If not
supplied, uses the user's configured default Site.
+ @param mime: If true, send in "multipart/form-data" format (default False)
@param max_retries: (optional) Maximum number of times to retry after
errors, defaults to 25
@param retry_wait: (optional) Minimum time to wait after an error,
@@ -108,6 +116,7 @@
self.site = kwargs.pop("site")
except KeyError:
self.site = pywikibot.Site()
+ self.mime = kwargs.pop("mime", False)
self.max_retries = kwargs.pop("max_retries", 25)
self.retry_wait = kwargs.pop("retry_wait", 5)
self.params = {}
@@ -199,7 +208,6 @@
"protect", "block", "unblock"
)
self.site.throttle(write=write)
- mime = False # debugging
uri = self.site.scriptpath() + "/api.php"
try:
ssl = False
@@ -208,15 +216,32 @@
ssl = True
elif config.use_SSL_always:
ssl = True
- if mime:
- global container
+ if self.mime:
# construct a MIME message containing all API key/values
container = MIMEMultipart(_subtype='form-data')
for key in self.params:
- submsg = MIMENonMultipart("text", "plain")
- submsg.add_header("Content-disposition", "form-data",
- name=key)
- submsg.set_payload(self.params[key])
+ # key "file" requires special treatment in a multipart
+ # message
+ if key == "file":
+ local_filename = self.params[key]
+ filetype = mimetypes.guess_type(local_filename)[0] \
+ or 'application/octet-stream'
+ file_content = file(local_filename, "rb").read()
+ submsg = MIMENonMultipart(*filetype.split("/"))
+ submsg.add_header("Content-disposition",
+ "form-data", name=key,
+ filename=local_filename)
+ submsg.set_payload(file_content)
+ else:
+ try:
+ self.params[key].encode("ascii")
+ keytype = ("text", "plain")
+ except UnicodeError:
+ keytype = ("application", "octet-stream")
+ submsg = MIMENonMultipart(*keytype)
+ submsg.add_header("Content-disposition", "form-data",
+ name=key)
+ submsg.set_payload(self.params[key])
container.attach(submsg)
# strip the headers to get the HTTP message body
body = container.as_string()
Modified: branches/rewrite/pywikibot/exceptions.py
===================================================================
--- branches/rewrite/pywikibot/exceptions.py 2009-10-28 18:12:26 UTC (rev 7557)
+++ branches/rewrite/pywikibot/exceptions.py 2009-10-28 20:14:24 UTC (rev 7558)
@@ -116,3 +116,6 @@
class CaptchaError(Error):
"""Captcha is asked and config.solve_captcha == False."""
+class UploadWarning(Error):
+ """Upload failed with a warning message (passed as the argument)."""
+
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2009-10-28 18:12:26 UTC (rev 7557)
+++ branches/rewrite/pywikibot/site.py 2009-10-28 20:14:24 UTC (rev 7558)
@@ -2545,7 +2545,107 @@
return [image.title(withNamespace=False)
for image in self.allimages(sha1=hash_found)]
+ def upload(self, imagepage, source_filename=None, source_url=None,
+ comment=None, watch=False, ignore_warnings=False):
+ """Upload a file to the wiki.
+ Either source_filename or source_url, but not both, must be provided.
+
+ @param imagepage: an ImagePage object from which the wiki-name of the
+ file will be obtained.
+ @param source_filename: path to the file to be uploaded
+ @param source_url: URL of the file to be uploaded
+ @param comment: Edit summary; if this is not provided, then
+ imagepage.text will be used. An empty summary is not permitted.
+ @param watch: If true, add imagepage to the bot user's watchlist
+ @param ignore_warnings: if true, ignore API warnings and force
+ upload (for example, to overwrite an existing file); default False
+
+ """
+ upload_warnings = {
+ # map API warning codes to user error messages
+ # %(msg)s will be replaced by message string from API responsse
+ 'duplicate-archive':
+ "The file is a duplicate of a deleted file %(msg)s.",
+ 'was-deleted':
+ "The file %(msg)s was previously deleted.",
+ 'emptyfile':
+ "File %(msg)s is empty.",
+ 'exists':
+ "File %(msg)s already exists.",
+ 'duplicate':
+ "Uploaded file is a duplicate of %(msg)s.",
+ 'badfilename':
+ "Target filename is invalid.",
+ 'filetype-unwanted-type':
+ "File %(msg)s type is unwatched type.",
+ }
+
+ # check for required user right
+ if "upload" not in self.userinfo["rights"]:
+ raise pywikibot.Error(
+ "User '%s' does not have upload rights on site %s."
+ % (self.user(), self))
+ # check for required parameters
+ if (source_filename and source_url)\
+ or (source_filename is None and source_url is None):
+ raise ValueError(
+"APISite.upload: must provide either source_filename or source_url, not both."
+ )
+ if comment is None:
+ comment = imagepage.text
+ if not comment:
+ raise ValueError(
+"APISite.upload: cannot upload file without a summary/description."
+ )
+ token = self.token(imagepage, "edit")
+ if source_filename:
+ # upload local file
+ # make sure file actually exists
+ if not os.path.isfile(source_filename):
+ raise ValueError("File '%s' does not exist."
+ % source_filename)
+ filesize = os.path.getsize(source_filename)
+ # TODO: if file size exceeds some threshold (to be determined),
+ # upload by chunks
+ req = api.Request(site=self, action="upload", token=token,
+ filename=imagepage.title(withNamespace=False),
+ file=source_filename, comment=comment,
+ mime=True)
+ else:
+ # upload by URL
+ if "upload_by_url" not in self.userinfo["rights"]:
+ raise pywikibot.Error(
+ "User '%s' is not authorized to upload by URL on site %s."
+ % (self.user(), self))
+ req = api.Request(site=self, action="upload", token=token,
+ filename=imagepage.title(withNamespace=False),
+ url=source_url, comment=comment)
+ if watch:
+ req["watch"] = ""
+ if ignore_warnings:
+ req["ignorewarnings"] = ""
+ try:
+ result = req.submit()
+ except api.APIError, err:
+ # TODO: catch and process foreseeable errors
+ raise
+ result = result["upload"]
+ pywikibot.output(result, level=pywikibot.DEBUG)
+ if "warnings" in result:
+ warning = result["warnings"].keys()[0]
+ message = result["warnings"][warning]
+ raise pywikibot.UploadWarning(upload_warnings[warning]
+ % {'msg': message})
+ else:
+ pywikibot.output(u"Upload: unrecognized response: %s"
+ % result["result"])
+ if result["result"] == "Success":
+ pywikibot.output(u"Upload successful.")
+ imagepage._imageinfo = result["imageinfo"]
+ return
+
+
#### METHODS NOT IMPLEMENTED YET ####
class NotImplementedYet:
Modified: branches/rewrite/scripts/upload.py
===================================================================
--- branches/rewrite/scripts/upload.py 2009-10-28 18:12:26 UTC (rev 7557)
+++ branches/rewrite/scripts/upload.py 2009-10-28 20:14:24 UTC (rev 7558)
@@ -6,15 +6,16 @@
-keep Keep the filename as is
-filename Target filename
- -noverify Do not ask for verification of the upload description if one is given
+ -noverify Do not ask for verification of the upload description if one
+ is given
-If any other arguments are given, the first is the URL or filename
-to upload, and the rest is a proposed description to go with the
-upload. If none of these are given, the user is asked for the
-file or URL to upload. The bot will then upload the image to the wiki.
+If any other arguments are given, the first is the URL or filename to
+upload, and the rest is a proposed description to go with the upload. If
+none of these are given, the user is asked for the file or URL to upload.
+The bot will then upload the image to the wiki.
-The script will ask for the location of an image, if not given as a parameter,
-and for a description.
+The script will ask for the location of an image, if not given as a
+parameter, and for a description.
"""
#
# (C) Rob W.W. Hooft, Andre Engels 2003-2004
@@ -23,58 +24,26 @@
#
__version__='$Id$'
+
import os, sys, time
-import urllib, mimetypes
-import wikipedia, config, query
+import urllib
+import urlparse
+import tempfile
+import pywikibot
+from pywikibot import config
-def post_multipart(site, address, fields, files, cookies):
- """
- Post fields and files to an http host as multipart/form-data.
- fields is a sequence of (name, value) elements for regular form fields.
- files is a sequence of (name, filename, value) elements for data to be uploaded as files
- Return the server's response page.
- """
- contentType, body = encode_multipart_formdata(fields, files)
- return site.postData(address, body, contentType = contentType, cookies = cookies)
-def encode_multipart_formdata(fields, files):
- """
- fields is a sequence of (name, value) elements for regular form fields.
- files is a sequence of (name, filename, value) elements for data to be uploaded as files
- Return (content_type, body) ready for httplib.HTTP instance
- """
- boundary = '----------ThIs_Is_tHe_bouNdaRY_$'
- lines = []
- for (key, value) in fields:
- lines.append('--' + boundary)
- lines.append('Content-Disposition: form-data; name="%s"' % key)
- lines.append('')
- lines.append(value)
- for (key, filename, value) in files:
- lines.append('--' + boundary)
- lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
- lines.append('Content-Type: %s' % get_content_type(filename))
- lines.append('')
- lines.append(value)
- lines.append('--' + boundary + '--')
- lines.append('')
- body = '\r\n'.join(lines)
- content_type = 'multipart/form-data; boundary=%s' % boundary
- return content_type, body
-
-def get_content_type(filename):
- return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
-
-
class UploadRobot:
- def __init__(self, url, urlEncoding = None, description = u'', useFilename = None, keepFilename = False,
- verifyDescription = True, ignoreWarning = False, targetSite = None, uploadByUrl = False):
+ def __init__(self, url, urlEncoding=None, description=u'',
+ useFilename=None, keepFilename=False,
+ verifyDescription=True, ignoreWarning=False,
+ targetSite=None, uploadByUrl=False):
"""
ignoreWarning - Set this to True if you want to upload even if another
file would be overwritten or another mistake would be
risked.
+
"""
- self._retrieved = False
self.url = url
self.urlEncoding = urlEncoding
self.description = description
@@ -83,321 +52,203 @@
self.verifyDescription = verifyDescription
self.ignoreWarning = ignoreWarning
if config.upload_to_commons:
- self.targetSite = targetSite or wikipedia.getSite('commons', 'commons')
+ self.targetSite = targetSite or pywikibot.Site('commons', 'commons')
else:
- self.targetSite = targetSite or wikipedia.getSite()
+ self.targetSite = targetSite or pywikibot.Site()
self.targetSite.forceLogin()
self.uploadByUrl = uploadByUrl
def urlOK(self):
- '''
- Returns true iff the URL references an online site or an
- existing local file.
- '''
- return self.url != '' and ('://' in self.url or os.path.exists(self.url))
+ "Return true iff self.url looks like an URL or an existing local file."
+ return "://" in self.url or os.path.exists(self.url)
def read_file_content(self):
- if not self._retrieved or self.uploadByUrl:
- # Get file contents
- wikipedia.output(u'Reading file %s' % self.url)
- if '://' in self.url:
- resume = False
- dt = 15
+ """Return name of temp file in which remote file is saved."""
+ pywikibot.output(u'Reading file %s' % self.url)
+ resume = False
+ dt = 15
+ uo = urllib.URLopener()
+ retrieved = False
- while not self._retrieved:
- uo = wikipedia.MyURLopener()
- if resume:
- wikipedia.output(u"Resume download...")
- uo.addheader('Range', 'bytes=%s-' % rlen)
+ while not retrieved:
+ if resume:
+ pywikibot.output(u"Resume download...")
+ uo.addheader('Range', 'bytes=%s-' % rlen)
- file = uo.open(self.url)
+ infile = uo.open(self.url)
- if 'text/html' in file.info().getheader('Content-Type'):
- print "Couldn't download the image: the requested URL was not found on this server."
- return
+ if 'text/html' in infile.info().getheader('Content-Type'):
+ print \
+"Couldn't download the image: the requested URL was not found on server."
+ return
- content_len = file.info().getheader('Content-Length')
- accept_ranges = file.info().getheader('Accept-Ranges') == 'bytes'
+ content_len = infile.info().getheader('Content-Length')
+ accept_ranges = infile.info().getheader('Accept-Ranges') == 'bytes'
- if resume:
- self._contents += file.read()
- else:
- self._contents = file.read()
+ if resume:
+ _contents += infile.read()
+ else:
+ _contents = infile.read()
- file.close()
- self._retrieved = True
+ infile.close()
+ retrieved = True
- if content_len:
- rlen = len(self._contents)
- content_len = int(content_len)
- if rlen < content_len:
- self._retrieved = False
- wikipedia.output(u"Connection closed at byte %s (%s left)" % (rlen, content_len))
- if accept_ranges and rlen > 0:
- resume = True
- wikipedia.output(u"Sleeping for %d seconds..." % dt)
- time.sleep(dt)
- if dt <= 60:
- dt += 15
- elif dt < 360:
- dt += 60
- else:
- if wikipedia.verbose:
- wikipedia.output(u"WARNING: No check length to retrieved data is possible.")
+ if content_len:
+ rlen = len(_contents)
+ content_len = int(content_len)
+ if rlen < content_len:
+ retrieved = False
+ pywikibot.output(
+ u"Connection closed at byte %s (%s left)"
+ % (rlen, content_len))
+ if accept_ranges and rlen > 0:
+ resume = True
+ pywikibot.output(u"Sleeping for %d seconds..." % dt)
+ time.sleep(dt)
+ if dt <= 60:
+ dt += 15
+ elif dt < 360:
+ dt += 60
else:
- # Opening local files with MyURLopener would be possible, but we
- # don't do it because it only accepts ASCII characters in the
- # filename.
- file = open(self.url,"rb")
- self._contents = file.read()
- file.close()
-
+ pywikibot.output(
+ u"WARNING: No check length to retrieved data is possible.",
+ level=pywikibot.VERBOSE)
+ handle, tempname = tempfile.mkstemp()
+ t = os.fdopen(handle, "wb")
+ t.write(_contents)
+ t.close()
+ return tempname
+
def process_filename(self):
+ """Return base filename portion of self.url"""
# Isolate the pure name
filename = self.url
-
- if '/' in filename:
- filename = filename.split('/')[-1]
-
- if '\\' in filename:
- filename = filename.split('\\')[-1]
-
- if self.urlEncoding:
- filename = urllib.unquote(filename.decode(self.urlEncoding))
-
+ # Filename may be either a local file path or a URL
+ if "://" in filename:
+ # extract the path portion of the URL
+ filename = urlparse.urlparse(filename).path
+ filename = os.path.basename(filename)
+
if self.useFilename:
filename = self.useFilename
if not self.keepFilename:
- wikipedia.output(u"The filename on the target wiki will default to: %s" % filename)
- # ask newfn until it's valid
- ok = False
+ pywikibot.output(
+ u"The filename on the target wiki will default to: %s"
+ % filename)
# FIXME: these 2 belong somewhere else, presumably in family
forbidden = '/' # to be extended
- allowed_formats = (u'gif', u'jpg', u'jpeg', u'mid', u'midi', u'ogg', u'png', u'svg', u'xcf', u'djvu')
- while not ok:
- ok = True
- newfn = wikipedia.input(u'Enter a better name, or press enter to accept:')
+ allowed_formats = (u'gif', u'jpg', u'jpeg', u'mid', u'midi',
+ u'ogg', u'png', u'svg', u'xcf', u'djvu')
+ # ask until it's valid
+ while True:
+ newfn = pywikibot.input(
+ u'Enter a better name, or press enter to accept:')
if newfn == "":
newfn = filename
+ break
ext = os.path.splitext(newfn)[1].lower().strip('.')
- for c in forbidden:
- if c in newfn:
- print "Invalid character: %s. Please try again" % c
- ok = False
- if ext not in allowed_formats and ok:
- choice = wikipedia.inputChoice(u"File format is not one of [%s], but %s. Continue?" % (u' '.join(allowed_formats), ext), ['yes', 'no'], ['y', 'N'], 'N')
+ # are any chars in forbidden also in newfn?
+ invalid = set(forbidden) & set(newfn)
+ if invalid:
+ c = "".join(invalid)
+ print "Invalid character(s): %s. Please try again" % c
+ continue
+ if ext not in allowed_formats:
+ choice = pywikibot.inputChoice(
+ u"File format is not one of [%s], but %s. Continue?"
+ % (u' '.join(allowed_formats), ext),
+ ['yes', 'no'], ['y', 'N'], 'N')
if choice == 'n':
- ok = False
+ continue
+ break
if newfn != '':
filename = newfn
- # MediaWiki doesn't allow spaces in the file name.
- # Replace them here to avoid an extra confirmation form
- filename = filename.replace(' ', '_')
# A proper description for the submission.
- wikipedia.output(u"The suggested description is:")
- wikipedia.output(self.description)
+ pywikibot.output(u"The suggested description is:")
+ pywikibot.output(self.description)
if self.verifyDescription:
- newDescription = u''
- choice = wikipedia.inputChoice(u'Do you want to change this description?', ['Yes', 'No'], ['y', 'N'], 'n')
- if choice == 'y':
- import editarticle
- editor = editarticle.TextEditor()
- newDescription = editor.edit(self.description)
- # if user saved / didn't press Cancel
- if newDescription:
- self.description = newDescription
+ newDescription = u''
+ choice = pywikibot.inputChoice(
+ u'Do you want to change this description?',
+ ['Yes', 'No'], ['y', 'N'], 'n')
+ if choice == 'y':
+ import editarticle
+ editor = editarticle.TextEditor()
+ newDescription = editor.edit(self.description)
+ # if user saved / didn't press Cancel
+ if newDescription:
+ self.description = newDescription
return filename
-
+
def upload_image(self, debug=False):
- """Gets the image at URL self.url, and uploads it to the target wiki.
- Returns the filename which was used to upload the image.
- If the upload fails, the user is asked whether to try again or not.
- If the user chooses not to retry, returns null.
+ """Upload the image at self.url to the target wiki.
+
+ Return the filename that was used to upload the image.
+ If the upload fails, ask the user whether to try again or not.
+ If the user chooses not to retry, return null.
+
"""
- try:
- if config.use_api and self.targetSite.versionnumber() >= 16:
- x = self.targetSite.api_address()
- del x
- else:
- raise NotImplementedError
- except NotImplementedError:
- return self._uploadImageOld(debug)
-
- if not hasattr(self,'_contents'):
- self.read_file_content()
-
filename = self.process_filename()
-
- params = {
- 'action': 'upload',
- 'token': self.targetSite.getToken(),
- 'comment': self.description,
- 'filename': filename,
- #'': '',
- }
- if self.uploadByUrl:
- params['url'] = self.url
- else:
- params['file'] = self._contents
-
- if self.ignoreWarning:
- params['ignorewarnings'] = 1
-
- wikipedia.output(u'Uploading file to %s via API....' % self.targetSite)
-
- data = query.GetData(params, self.targetSite)
-
- if wikipedia.verbose:
- wikipedia.output("%s" % data)
-
- if 'error' in data: # error occured
- errCode = data['error']['code']
- wikipedia.output("%s" % data)
- else:
- data = data['upload']
- if data['result'] == u'Warning': #upload success but return warning.
- warn = data['warnings'].keys()[0]
- wikipedia.output("We got a warning message:", newline=False)
- warFn = data['warnings'][warn]
- if warn == 'duplicate-archive':
- wikipedia.output("The file is duplicate a deleted file %s." % warFn)
- elif warn == 'was-deleted':
- wikipedia.output("This file was deleted for %s." % warFn)
- elif warn == 'emptyfile':
- wikipedia.output("File %s is an empty file." % warFn)
- elif warn == 'exists':
- wikipedia.output("File %s is exists." % warFn)
- elif warn == 'duplicate':
- wikipedia.output("Uploaded file is duplicate with %s." % warFn)
- elif warn == 'badfilename':
- wikipedia.output("Target filename is invaild.")
- elif warn == 'filetype-unwanted-type':
- wikipedia.output("File %s type is unwatched type." % warFn)
- answer = wikipedia.inputChoice(u"Do you want to ignore?", ['Yes', 'No'], ['y', 'N'], 'N')
- if answer == "y":
- self.ignoreWarning = 1
- self.keepFilename = True
- return self.upload_image(debug)
- else:
- wikipedia.output("Upload aborted.")
- return
-
- elif data['result'] == u'Success': #No any warning, upload and online complete.
- wikipedia.output(u"Upload successful.")
- return filename #data['filename']
-
- def _uploadImageOld(self, debug=False):
- if not hasattr(self,'_contents'):
- self.read_file_content()
-
- filename = self.process_filename()
- # Convert the filename (currently Unicode) to the encoding used on the
- # target wiki
- encodedFilename = filename.encode(self.targetSite.encoding())
+ site = self.targetSite
+ imagepage = pywikibot.ImagePage(site, filename) # normalizes filename
+ imagepage.text = self.description
+ pywikibot.output(u'Uploading file to %s via API....' % site)
- formdata = {
- 'wpUploadDescription': self.description,
- 'wpUploadAffirm': '1',
- 'wpUpload': 'upload bestand',
- 'wpEditToken': self.targetSite.getToken(), # Get an edit token so we can do the upload
- 'wpDestFile': filename, # Set the new filename
- }
- # This somehow doesn't work.
- if self.ignoreWarning:
- formdata["wpIgnoreWarning"] = "1"
-
- if self.uploadByUrl:
- formdata["wpUploadFileURL"] = self.url
- formdata["wpSourceType"] = 'Url'
-
- # try to encode the strings to the encoding used by the target site.
- # if that's not possible (e.g. because there are non-Latin-1 characters and
- # the home Wikipedia uses Latin-1), convert all non-ASCII characters to
- # HTML entities.
- for key in formdata:
- assert isinstance(key, basestring), "ERROR: %s is not a string but %s" % (key, type(key))
- try:
- formdata[key] = formdata[key].encode(self.targetSite.encoding())
- except (UnicodeEncodeError, UnicodeDecodeError):
- formdata[key] = wikipedia.UnicodeToAsciiHtml(formdata[key]).encode(self.targetSite.encoding())
-
- # don't upload if we're in debug mode
- if not debug:
- wikipedia.output(u'Uploading file to %s...' % self.targetSite)
-
+ try:
if self.uploadByUrl:
- # Just do a post with all the fields filled out
- response, returned_html = self.targetSite.postForm(self.targetSite.upload_address(), formdata.items(), cookies = self.targetSite.cookies())
+ site.upload(imagepage, source_url=self.url,
+ ignore_warnings=self.ignoreWarning)
else:
- response, returned_html = post_multipart(self.targetSite, self.targetSite.upload_address(),
- formdata.items(), (('wpUploadFile', encodedFilename, self._contents),),
- cookies = self.targetSite.cookies())
- # There are 2 ways MediaWiki can react on success: either it gives
- # a 200 with a success message, or it gives a 302 (redirection).
- # Do we know how the "success!" HTML page should look like?
- # ATTENTION: if you changed your Wikimedia Commons account not to show
- # an English interface, this detection will fail!
- success_msg = self.targetSite.mediawiki_message('successfulupload')
- if success_msg in returned_html or response.status == 302:
- wikipedia.output(u"Upload successful.")
- # The following is not a good idea, because the server also gives a 200 when
- # something went wrong.
- #if response.status in [200, 302]:
- # wikipedia.output(u"Upload successful.")
+ if "://" in self.url:
+ temp = self.read_file_content()
+ else:
+ temp = self.url
+ site.upload(imagepage, source_filename=temp,
+ ignore_warnings=self.ignoreWarning)
- elif response.status == 301:
- wikipedia.output(u"Following redirect...")
- address = response.getheader('Location')
- wikipedia.output(u"Changed upload address to %s. Please update %s.py" % (address, self.targetSite.family.__module__))
- exec('self.targetSite.upload_address = lambda: %r' % address, locals(), globals())
+ except pywikibot.UploadWarning, warn:
+ pywikibot.output(u"We got a warning message: ", newline=False)
+ pywikibot.output(str(warn))
+ answer = pywikibot.inputChoice(u"Do you want to ignore?",
+ ['Yes', 'No'], ['y', 'N'], 'N')
+ if answer == "y":
+ self.ignoreWarning = 1
+ self.keepFilename = True
return self.upload_image(debug)
else:
- try:
- # Try to find the error message within the HTML page.
- # If we can't find it, we just dump the entire HTML page.
- returned_html = returned_html[returned_html.index('<!-- start content -->') + 22: returned_html.index('<!-- end content -->')]
- except:
- pass
- wikipedia.output(u'%s\n\n' % returned_html)
- wikipedia.output(u'%i %s' % (response.status, response.reason))
+ pywikibot.output(u"Upload aborted.")
+ return
+
+ except Exception, e:
+ pywikibot.logger.exception("Upload error: " + str(e))
- if self.targetSite.mediawiki_message('uploadwarning') in returned_html:
- answer = wikipedia.inputChoice(u"You have recevied an upload warning message. Ignore?", ['Yes', 'No'], ['y', 'N'], 'N')
- if answer == "y":
- self.ignoreWarning = 1
- self.keepFilename = True
- return self._uploadImageOld(debug)
- else:
- answer = wikipedia.inputChoice(u'Upload of %s probably failed. Above you see the HTML page which was returned by MediaWiki. Try again?' % filename, ['Yes', 'No'], ['y', 'N'], 'N')
- if answer == "y":
- return self._uploadImageOld(debug)
- else:
- return
- return filename
+ else:
+ #No warning, upload complete.
+ pywikibot.output(u"Upload successful.")
+ return filename #data['filename']
def run(self):
while not self.urlOK():
if not self.url:
- wikipedia.output(u'No input filename given')
+ pywikibot.output(u'No input filename given')
else:
- wikipedia.output(u'Invalid input filename given. Try again.')
- self.url = wikipedia.input(u'File or URL where image is now:')
+ pywikibot.output(u'Invalid input filename given. Try again.')
+ self.url = pywikibot.input(u'File or URL where image is now:')
return self.upload_image()
-def main(args):
+
+def main(*args):
url = u''
description = []
keepFilename = False
useFilename = None
verifyDescription = True
- # call wikipedia.py function to process all global wikipedia args
+ # process all global bot args
# returns a list of non-global args, i.e. args for upload.py
- args = wikipedia.handleArgs()
-
- for arg in args:
+ for arg in pywikibot.handleArgs(*args):
if arg:
if arg.startswith('-keep'):
keepFilename = True
@@ -410,11 +261,13 @@
else:
description.append(arg)
description = u' '.join(description)
- bot = UploadRobot(url, description=description, useFilename=useFilename, keepFilename=keepFilename, verifyDescription=verifyDescription)
+ bot = UploadRobot(url, description=description, useFilename=useFilename,
+ keepFilename=keepFilename,
+ verifyDescription=verifyDescription)
bot.run()
if __name__ == "__main__":
try:
- main(sys.argv[1:])
+ main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Revision: 7557
Author: xqt
Date: 2009-10-28 18:12:26 +0000 (Wed, 28 Oct 2009)
Log Message:
-----------
import wikipedia as pywikibot for easier compare with rewrite branch
Modified Paths:
--------------
trunk/pywikipedia/blockpageschecker.py
trunk/pywikipedia/category.py
trunk/pywikipedia/category_redirect.py
trunk/pywikipedia/cosmetic_changes.py
trunk/pywikipedia/editarticle.py
trunk/pywikipedia/interwiki.py
Modified: trunk/pywikipedia/blockpageschecker.py
===================================================================
--- trunk/pywikipedia/blockpageschecker.py 2009-10-28 18:10:56 UTC (rev 7556)
+++ trunk/pywikipedia/blockpageschecker.py 2009-10-28 18:12:26 UTC (rev 7557)
@@ -63,7 +63,8 @@
#
import re, webbrowser
-import wikipedia, catlib, pagegenerators, config
+import wikipedia as pywikibot
+import catlib, pagegenerators, config
# This is required for the text that is shown when you run this script
# with the parameter -help.
@@ -115,7 +116,7 @@
'en': None,
'it': [r'\{\{(?:[Tt]emplate:|)[Pp]rotetta\}\}'],
}
-
+
# Array: 0 => Semi-block, 1 => Total Block, 2 => Semi-Move, 3 => Total-Move, 4 => template-unique
templateNoRegex = {
'it':['{{Avvisobloccoparziale}}', '{{Avvisoblocco}}', None, None, '{{Protetta}}'],
@@ -171,7 +172,7 @@
for catchRegex in TU:
resultCatch = re.findall(catchRegex, text)
if resultCatch:
- return ('unique', catchRegex)
+ return ('unique', catchRegex)
if TSMP != None and TTMP != None and TTP != TTMP and TSP != TSMP:
for catchRegex in TTMP:
resultCatch = re.findall(catchRegex, text)
@@ -184,9 +185,9 @@
return ('editable', r'\A\n') # If editable means that we have no regex, won't change anything with this regex
def debugQuest(site, page):
- quest = wikipedia.input(u'Do you want to open the page on your [b]rowser, [g]ui or [n]othing?')
+ quest = pywikibot.input(u'Do you want to open the page on your [b]rowser, [g]ui or [n]othing?')
pathWiki = site.family.nicepath(site.lang)
- url = 'http://%s%s%s?&redirect=no' % (wikipedia.getSite().hostname(), pathWiki, page.urlname())
+ url = 'http://%s%s%s?&redirect=no' % (pywikibot.getSite().hostname(), pathWiki, page.urlname())
while 1:
if quest.lower() in ['b', 'B']:
webbrowser.open(url)
@@ -199,7 +200,7 @@
elif quest.lower() in ['n', 'N']:
break
else:
- wikipedia.output(u'wrong entry, type "b", "g" or "n"')
+ pywikibot.output(u'wrong entry, type "b", "g" or "n"')
continue
def main():
@@ -207,7 +208,7 @@
# Loading the comments
global categoryToCheck; global comment; global project_inserted
if config.mylang not in project_inserted:
- wikipedia.output(u"Your project is not supported by this script. You have to edit the script and add it!")
+ pywikibot.output(u"Your project is not supported by this script. You have to edit the script and add it!")
return
# always, define a generator to understand if the user sets one, defining what's genFactory
always = False; generator = False; debug = False
@@ -215,9 +216,9 @@
# To prevent Infinite loops
errorCount = 0
# Load the right site
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
# Loading the default options.
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg == '-always':
always = True
elif arg == '-move':
@@ -237,27 +238,27 @@
type = 'move')
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:])]
else:
genFactory.handleArg(arg)
# Take the right templates to use, the category and the comment
- TSP = wikipedia.translate(site, templateSemiProtection)
- TTP = wikipedia.translate(site, templateTotalProtection)
- TSMP = wikipedia.translate(site, templateSemiMoveProtection)
- TTMP = wikipedia.translate(site, templateTotalMoveProtection)
- TNR = wikipedia.translate(site, templateNoRegex)
- TU = wikipedia.translate(site, templateUnique)
+ TSP = pywikibot.translate(site, templateSemiProtection)
+ TTP = pywikibot.translate(site, templateTotalProtection)
+ TSMP = pywikibot.translate(site, templateSemiMoveProtection)
+ TTMP = pywikibot.translate(site, templateTotalMoveProtection)
+ TNR = pywikibot.translate(site, templateNoRegex)
+ TU = pywikibot.translate(site, templateUnique)
- category = wikipedia.translate(site, categoryToCheck)
- commentUsed = wikipedia.translate(site, comment)
+ category = pywikibot.translate(site, categoryToCheck)
+ commentUsed = pywikibot.translate(site, comment)
if not generator:
gen = genFactory.getCombinedGenerator()
if not generator:
generator = list()
- wikipedia.output(u'Loading categories...')
+ pywikibot.output(u'Loading categories...')
# Define the category if no other generator has been setted
for CAT in category:
cat = catlib.Category(site, CAT)
@@ -265,20 +266,20 @@
gen = pagegenerators.CategorizedPageGenerator(cat)
for pageCat in gen:
generator.append(pageCat)
- wikipedia.output(u'Categories loaded, start!')
+ pywikibot.output(u'Categories loaded, start!')
# Main Loop
preloadingGen = pagegenerators.PreloadingGenerator(generator, pageNumber = 60)
for page in preloadingGen:
pagename = page.aslink()
- wikipedia.output('Loading %s...' % pagename)
+ pywikibot.output('Loading %s...' % pagename)
try:
text = page.get()
restrictions = page.getRestrictions()
- except wikipedia.NoPage:
- wikipedia.output("%s doesn't exist! Skipping..." % pagename)
+ except pywikibot.NoPage:
+ pywikibot.output("%s doesn't exist! Skipping..." % pagename)
continue
- except wikipedia.IsRedirectPage:
- wikipedia.output("%s is a redirect! Skipping..." % pagename)
+ except pywikibot.IsRedirectPage:
+ pywikibot.output("%s is a redirect! Skipping..." % pagename)
if debug:
debugQuest(site, page)
continue
@@ -287,7 +288,7 @@
# PreloadingGenerator cannot set correctly page.editRestriction
# (see bug #1949476 )
if not page.canBeEdited():
- wikipedia.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
+ pywikibot.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
continue
"""
editRestr = restrictions['edit']
@@ -295,7 +296,7 @@
try:
config.sysopnames[site.family.name][site.lang]
except:
- wikipedia.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
+ pywikibot.output("%s is sysop-protected : this account can't edit it! Skipping..." % pagename)
continue
# Understand, according to the template in the page, what should be the protection
@@ -312,11 +313,11 @@
if TU != None:
replaceToPerform = u'|'.join(TTP + TSP + TU)
else:
- replaceToPerform = u'|'.join(TTP + TSP)
+ replaceToPerform = u'|'.join(TTP + TSP)
text, changes = re.subn('<noinclude>(%s)</noinclude>' % replaceToPerform, '', text)
if changes == 0:
- text, changes = re.subn('(%s)' % replaceToPerform, '', text)
- wikipedia.output(u'The page is editable for all, deleting the template...')
+ text, changes = re.subn('(%s)' % replaceToPerform, '', text)
+ pywikibot.output(u'The page is editable for all, deleting the template...')
elif editRestr[0] == 'sysop':
# total edit protection
@@ -324,9 +325,9 @@
msg = 'The page is protected to the sysop'
if not moveBlockCheck:
msg += ', skipping...'
- wikipedia.output(msg)
+ pywikibot.output(msg)
else:
- wikipedia.output(u'The page is protected to the sysop, but the template seems not correct. Fixing...')
+ pywikibot.output(u'The page is protected to the sysop, but the template seems not correct. Fixing...')
if TU != None:
text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
else:
@@ -338,9 +339,9 @@
msg = 'The page is editable only for the autoconfirmed users'
if not moveBlockCheck:
msg += ', skipping...'
- wikipedia.output(msg)
+ pywikibot.output(msg)
else:
- wikipedia.output(u'The page is editable only for the autoconfirmed users, but the template seems not correct. Fixing...')
+ pywikibot.output(u'The page is editable only for the autoconfirmed users, but the template seems not correct. Fixing...')
if TU != None:
text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
else:
@@ -348,7 +349,7 @@
if changes == 0:
# We tried to fix edit-protection templates, but it did not work.
- wikipedia.output('Warning : No edit-protection template could be found')
+ pywikibot.output('Warning : No edit-protection template could be found')
if moveBlockCheck:
# checking move protection now
@@ -356,7 +357,7 @@
changes = -1
if not moveRestr:
- wikipedia.output(u'The page is movable for all, deleting the template...')
+ pywikibot.output(u'The page is movable for all, deleting the template...')
# Deleting the template because the page doesn't need it.
if TU != None:
replaceToPerform = u'|'.join(TSMP + TTMP + TU)
@@ -368,9 +369,9 @@
elif moveRestr[0] == 'sysop':
# move-total-protection
if (TemplateInThePage[0] == 'sysop-move' and TTMP != None) or (TemplateInThePage[0] == 'unique' and TU != None):
- wikipedia.output(u'The page is protected from moving to the sysop, skipping...')
+ pywikibot.output(u'The page is protected from moving to the sysop, skipping...')
else:
- wikipedia.output(u'The page is protected from moving to the sysop, but the template seems not correct. Fixing...')
+ pywikibot.output(u'The page is protected from moving to the sysop, but the template seems not correct. Fixing...')
if TU != None:
text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
else:
@@ -379,9 +380,9 @@
elif TSMP != None or TU != None:
# implicitely moveRestr[0] = 'autoconfirmed', move-semi-protection
if TemplateInThePage[0] == 'autoconfirmed-move' or TemplateInThePage[0] == 'unique':
- wikipedia.output(u'The page is movable only for the autoconfirmed users, skipping...')
+ pywikibot.output(u'The page is movable only for the autoconfirmed users, skipping...')
else:
- wikipedia.output(u'The page is movable only for the autoconfirmed users, but the template seems not correct. Fixing...')
+ pywikibot.output(u'The page is movable only for the autoconfirmed users, but the template seems not correct. Fixing...')
if TU != None:
text, changes = re.subn(TemplateInThePage[1], TNR[4], text)
else:
@@ -389,43 +390,43 @@
if changes == 0:
# We tried to fix move-protection templates, but it did not work.
- wikipedia.output('Warning : No move-protection template could be found')
+ pywikibot.output('Warning : No move-protection template could be found')
if oldtext != text:
# Ok, asking if the change has to be performed and do it if yes.
- wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
- wikipedia.showDiff(oldtext, text)
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
+ pywikibot.showDiff(oldtext, text)
if not always:
- choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
+ choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
if choice == 'a':
always = True
if always or choice == 'y':
while 1:
try:
page.put(text, commentUsed, force=True)
- except wikipedia.EditConflict:
- wikipedia.output(u'Edit conflict! skip!')
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Edit conflict! skip!')
break
- except wikipedia.ServerError:
+ except pywikibot.ServerError:
# Sometimes there is this error that's quite annoying because
# can block the whole process for nothing.
errorCount += 1
if errorCount < 5:
- wikipedia.output(u'Server Error! Wait..')
+ pywikibot.output(u'Server Error! Wait..')
time.sleep(3)
continue
else:
# Prevent Infinite Loops
- raise wikipedia.ServerError(u'Fifth Server Error!')
- except wikipedia.SpamfilterError, e:
- wikipedia.output(u'Cannot change %s because of blacklist entry %s' % (page.title(), e.url))
+ 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))
break
- 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,))
break
- except wikipedia.LockedPage:
- wikipedia.output(u'The page is still protected. Skipping...')
+ except pywikibot.LockedPage:
+ pywikibot.output(u'The page is still protected. Skipping...')
break
else:
# Break only if the errors are one after the other
@@ -436,4 +437,4 @@
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/category.py
===================================================================
--- trunk/pywikipedia/category.py 2009-10-28 18:10:56 UTC (rev 7556)
+++ trunk/pywikipedia/category.py 2009-10-28 18:12:26 UTC (rev 7557)
@@ -75,13 +75,15 @@
# (C) Rob W.W. Hooft, 2004
# (C) Daniel Herding, 2004
# (C) Anreas J Schwab, 2007
+# (C) Pywikipedia team, 2008-2009
#
__version__ = '$Id$'
#
# Distributed under the terms of the MIT license.
#
import os, re, pickle, bz2
-import wikipedia, catlib, config, pagegenerators
+import wikipedia as pywikibot
+import catlib, config, pagegenerators
# This is required for the text that is shown when you run this script
# with the parameter -help.
@@ -184,7 +186,7 @@
'lt':u'robotas: Kategorija pervadinta į [[:Category:%s|%s]]',
'nds':u'Kat-Bot: Kategorie na [[:Category:%s|%s]] schaven',
'nds-nl':u'Bot: kattegerie is herneumd naor [[:Kattegerie:%s|%s]]',
- 'nl':u'Bot: categorie is hernoemd naar [[:Category:%s|%s]]',
+ 'nl':u'Bot: Categorie is hernoemd naar [[:Category:%s|%s]]',
'no':u'Robot: Kategorien ble flyttet til [[:Category:%s|%s]]',
'nn':u'robot: kategorien blei flytta til [[:Kategori:%s|%s]]',
'pt':u'Bot: Categoria [[:Category:%s|%s]] foi movida',
@@ -219,10 +221,10 @@
else:
try:
if not os.path.isabs(filename):
- filename = wikipedia.config.datafilepath(filename)
+ filename = pywikibot.config.datafilepath(filename)
f = bz2.BZ2File(filename, 'r')
- wikipedia.output(u'Reading dump from %s'
- % wikipedia.config.shortpath(filename))
+ pywikibot.output(u'Reading dump from %s'
+ % pywikibot.config.shortpath(filename))
databases = pickle.load(f)
f.close()
# keys are categories, values are 2-tuples with lists as entries.
@@ -286,9 +288,9 @@
Saves the contents of the dictionaries superclassDB and catContentDB to disk.
'''
if not os.path.isabs(filename):
- filename = wikipedia.config.datafilepath(filename)
- wikipedia.output(u'Dumping to %s, please wait...'
- % wikipedia.config.shortpath(filename))
+ filename = pywikibot.config.datafilepath(filename)
+ pywikibot.output(u'Dumping to %s, please wait...'
+ % pywikibot.config.shortpath(filename))
f = bz2.BZ2File(filename, 'w')
databases = {
'catContentDB': self.catContentDB,
@@ -328,21 +330,21 @@
# "Neumann, John von"
sorted_key = split_string[-1] + ', ' + ' '.join(split_string[:-1])
# give explicit sort key
- return wikipedia.Page(site, catlink.title() + '|' + sorted_key)
+ return pywikibot.Page(site, catlink.title() + '|' + sorted_key)
else:
- return wikipedia.Page(site, catlink.title())
+ return pywikibot.Page(site, catlink.title())
def add_category(sort_by_last_name = False, create_pages = False):
'''A robot to mass-add a category to a list of pages.'''
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
if gen:
- newcatTitle = wikipedia.input(
+ newcatTitle = pywikibot.input(
u'Category to add (do not give namespace):')
if not site.nocapitalize:
newcatTitle = newcatTitle[:1].capitalize() + newcatTitle[1:]
# set edit summary message
- editSummary = wikipedia.translate(site, msg_add) % newcatTitle
+ editSummary = pywikibot.translate(site, msg_add) % newcatTitle
cat_namespace = site.category_namespaces()[0]
@@ -352,11 +354,11 @@
answer = ''
while answer not in ('y','n','a'):
- answer = wikipedia.input(u'%s [y/n/a(ll)]:' % (page.aslink()))
+ answer = pywikibot.input(u'%s [y/n/a(ll)]:' % (page.aslink()))
if answer == 'a':
confirm = ''
while confirm not in ('y','n'):
- confirm = wikipedia.input(u"""\
+ confirm = pywikibot.input(u"""\
This should be used if and only if you are sure that your links are correct!
Are you sure? [y/n]:""")
if confirm == 'n':
@@ -365,45 +367,45 @@
if answer == 'y' or answer == 'a':
try:
text = page.get()
- except wikipedia.NoPage:
+ except pywikibot.NoPage:
if create_pages:
- wikipedia.output(u"%s doesn't exist yet. Creating."
+ pywikibot.output(u"%s doesn't exist yet. Creating."
% (page.title()))
text = ''
else:
- wikipedia.output(u"%s doesn't exist yet. Ignoring."
+ pywikibot.output(u"%s doesn't exist yet. Ignoring."
% (page.title()))
continue
- except wikipedia.IsRedirectPage, arg:
- redirTarget = wikipedia.Page(site, arg.args[0])
- wikipedia.output(
+ except pywikibot.IsRedirectPage, arg:
+ redirTarget = pywikibot.Page(site, arg.args[0])
+ pywikibot.output(
u"WARNING: %s is redirect to %s. Ignoring."
% (page.title(), redirTarget.title()))
continue
cats = page.categories()
# Show the title of the page we're working on.
# Highlight the title in purple.
- wikipedia.output(
+ pywikibot.output(
u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
% page.title())
- wikipedia.output(u"Current categories:")
+ pywikibot.output(u"Current categories:")
for cat in cats:
- wikipedia.output(u"* %s" % cat.title())
- catpl = wikipedia.Page(site,
+ pywikibot.output(u"* %s" % cat.title())
+ catpl = pywikibot.Page(site,
cat_namespace + ':' + newcatTitle)
if sort_by_last_name:
catpl = sorted_by_last_name(catpl, page)
if catpl in cats:
- wikipedia.output(u"%s is already in %s."
+ pywikibot.output(u"%s is already in %s."
% (page.title(), catpl.title()))
else:
- wikipedia.output(u'Adding %s' % catpl.aslink())
+ pywikibot.output(u'Adding %s' % catpl.aslink())
cats.append(catpl)
- text = wikipedia.replaceCategoryLinks(text, cats)
+ text = pywikibot.replaceCategoryLinks(text, cats)
try:
page.put(text, comment = editSummary)
- except wikipedia.EditConflict:
- wikipedia.output(
+ except pywikibot.EditConflict:
+ pywikibot.output(
u'Skipping %s because of edit conflict'
% (page.title()))
@@ -412,7 +414,7 @@
def __init__(self, oldCatTitle, newCatTitle, batchMode=False,
editSummary='', inPlace=False, moveCatPage=True,
deleteEmptySourceCat=True, titleRegex=None):
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
self.editSummary = editSummary
self.oldCat = catlib.Category(site, 'Category:' + oldCatTitle)
self.newCatTitle = newCatTitle
@@ -423,10 +425,10 @@
self.titleRegex = titleRegex
# set edit summary message
if not self.editSummary:
- self.editSummary = wikipedia.translate(site, msg_change)% self.oldCat.title()
+ self.editSummary = pywikibot.translate(site, msg_change)% self.oldCat.title()
def run(self):
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
newCat = catlib.Category(site, 'Category:' + self.newCatTitle)
# Copy the category contents to the new category page
@@ -435,21 +437,21 @@
if self.oldCat.exists() and self.moveCatPage:
copied = self.oldCat.copyAndKeep(
self.newCatTitle,
- wikipedia.translate(site, cfd_templates))
+ pywikibot.translate(site, cfd_templates))
# Also move the talk page
if copied:
- reason = wikipedia.translate(site, deletion_reason_move) \
+ reason = pywikibot.translate(site, deletion_reason_move) \
% (self.newCatTitle, self.newCatTitle)
oldTalk = self.oldCat.toggleTalkPage()
if oldTalk.exists():
newTalkTitle = newCat.toggleTalkPage().title()
try:
talkMoved = oldTalk.move(newTalkTitle, reason)
- except (wikipedia.NoPage, wikipedia.PageNotSaved), e:
+ except (pywikibot.NoPage, pywikibot.PageNotSaved), e:
#in order :
#Source talk does not exist, or
#Target talk already exists
- wikipedia.output(e.message)
+ pywikibot.output(e.message)
else:
if talkMoved:
oldMovedTalk = oldTalk
@@ -462,7 +464,8 @@
if not self.titleRegex or re.search(self.titleRegex,
article.title()):
catlib.change_category(article, self.oldCat, newCat,
- comment=self.editSummary, inPlace=self.inPlace)
+ comment=self.editSummary,
+ inPlace=self.inPlace)
# Move subcategories
gen = pagegenerators.SubCategoriesPageGenerator(self.oldCat,
@@ -472,19 +475,20 @@
if not self.titleRegex or re.search(self.titleRegex,
subcategory.title()):
catlib.change_category(subcategory, self.oldCat, newCat,
- comment=self.editSummary, inPlace=self.inPlace)
+ comment=self.editSummary,
+ inPlace=self.inPlace)
# Delete the old category and its moved talk page
if copied and self.deleteEmptySourceCat == True:
if self.oldCat.isEmpty():
- reason = wikipedia.translate(site, deletion_reason_move) \
+ reason = pywikibot.translate(site, deletion_reason_move) \
% (self.newCatTitle, self.newCatTitle)
confirm = not self.batchMode
self.oldCat.delete(reason, confirm, mark = True)
if oldMovedTalk is not None:
oldMovedTalk.delete(reason, confirm, mark = True)
else:
- wikipedia.output('Couldn\'t delete %s - not empty.'
+ pywikibot.output('Couldn\'t delete %s - not empty.'
% self.oldCat.title())
@@ -500,7 +504,7 @@
'he':u'בוט: יוצר רשימה מהקטגוריה %s (%d דפים)',
'kk':u'Бот: %s дегеннен (%d буын) тізімдеді',
'nds-nl':u'Bot: lieste van %s (%d pagina\'s)',
- 'nl':u'Bot: lijst van %s (%d pagina\'s)',
+ 'nl':u'Bot: Lijst van %s (%d pagina\'s)',
'sv':u'Robot: Skapar en lista från %s (%d)',
'pt':u'Bot: Listando de %s (%d entradas)',
'zh':u'機器人: 從%s提取列表(%d個項目)',
@@ -510,8 +514,8 @@
self.editSummary = editSummary
self.overwrite = overwrite
self.showImages = showImages
- self.cat = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle)
- self.list = wikipedia.Page(wikipedia.getSite(), listTitle)
+ self.cat = catlib.Category(pywikibot.getSite(), 'Category:' + catTitle)
+ self.list = pywikibot.Page(pywikibot.getSite(), listTitle)
self.subCats = subCats
self.talkPages = talkPages
self.recurse = recurse
@@ -521,7 +525,7 @@
if self.subCats:
listOfArticles += self.cat.subcategoriesList()
if not self.editSummary:
- self.editSummary = wikipedia.translate(wikipedia.getSite(), self.listify_msg) % (self.cat.title(), len(listOfArticles))
+ self.editSummary = pywikibot.translate(pywikibot.getSite(), self.listify_msg) % (self.cat.title(), len(listOfArticles))
listString = ""
for article in listOfArticles:
@@ -536,7 +540,7 @@
else:
listString = listString + "*[[:%s]]\n" % article.title()
if self.list.exists() and not self.overwrite:
- wikipedia.output(u'Page %s already exists, aborting.' % self.list.title())
+ pywikibot.output(u'Page %s already exists, aborting.' % self.list.title())
else:
self.list.put(listString, comment=self.editSummary)
@@ -561,7 +565,7 @@
'ksh':u'Bot: de Saachjropp is nu opjelööß',
'nds':u'Kat-Bot: Kategorie is nu oplööst',
'nds-nl':u'Bot: kattegerie besteet neet meer',
- 'nl':u'Bot: categorie is opgeheven',
+ 'nl':u'Bot: Categorie is opgeheven',
'no':u'Robot: Kategorien ble oppløst',
'nn':u'robot: kategorien blei løyst opp',
'pt':u'Bot: Categoria foi unida',
@@ -589,7 +593,7 @@
'lb': u'Bot: Ewech huele vun %s',
'nds':u'Kat-Bot: rut ut %s',
'nds-nl':u'Bot: vort-ehaold uut %s',
- 'nl':u'Bot: verwijderd uit %s',
+ 'nl':u'Bot: Verwijderd uit %s',
'no':u'Robot: Fjerner ifra %s',
'nn':u'robot: fjerna ifrå %s',
'pt':u'Bot: Removendo [[Categoria:%s]]',
@@ -601,19 +605,19 @@
def __init__(self, catTitle, batchMode = False, editSummary = '', useSummaryForDeletion = True, titleRegex = None, inPlace = False):
self.editSummary = editSummary
- self.cat = catlib.Category(wikipedia.getSite(), 'Category:' + catTitle)
+ self.cat = catlib.Category(pywikibot.getSite(), 'Category:' + catTitle)
# get edit summary message
self.useSummaryForDeletion = useSummaryForDeletion
self.batchMode = batchMode
self.titleRegex = titleRegex
self.inPlace = inPlace
if not self.editSummary:
- self.editSummary = wikipedia.translate(wikipedia.getSite(), self.msg_remove) % self.cat.title()
+ self.editSummary = pywikibot.translate(pywikibot.getSite(), self.msg_remove) % self.cat.title()
def run(self):
articles = self.cat.articlesList(recurse = 0)
if len(articles) == 0:
- wikipedia.output(u'There are no articles in category %s' % self.cat.title())
+ pywikibot.output(u'There are no articles in category %s' % self.cat.title())
else:
for article in articles:
if not self.titleRegex or re.search(self.titleRegex,article.title()):
@@ -621,7 +625,7 @@
# Also removes the category tag from subcategories' pages
subcategories = self.cat.subcategoriesList(recurse = 0)
if len(subcategories) == 0:
- wikipedia.output(u'There are no subcategories in category %s' % self.cat.title())
+ pywikibot.output(u'There are no subcategories in category %s' % self.cat.title())
else:
for subcategory in subcategories:
catlib.change_category(subcategory, self.cat, None, comment = self.editSummary, inPlace = self.inPlace)
@@ -630,7 +634,7 @@
if self.useSummaryForDeletion and self.editSummary:
reason = self.editSummary
else:
- reason = wikipedia.translate(wikipedia.getSite(), self.deletion_reason_remove)
+ reason = pywikibot.translate(pywikibot.getSite(), self.deletion_reason_remove)
talkPage = self.cat.toggleTalkPage()
self.cat.delete(reason, not self.batchMode)
if (talkPage.exists()):
@@ -663,7 +667,7 @@
def __init__(self, catTitle, catDB):
self.catTitle = catTitle
self.catDB = catDB
- self.editSummary = wikipedia.translate(wikipedia.getSite(), msg_change) % catTitle
+ self.editSummary = pywikibot.translate(pywikibot.getSite(), msg_change) % catTitle
def move_to_category(self, article, original_cat, current_cat):
'''
@@ -673,16 +677,16 @@
NOTE: current_cat is only used for internal recursion. You should
always use current_cat = original_cat.
'''
- wikipedia.output(u'')
+ pywikibot.output(u'')
# Show the title of the page where the link was found.
# Highlight the title in purple.
- wikipedia.output(u'Treating page \03{lightpurple}%s\03{default}, currently in \03{lightpurple}%s\03{default}' % (article.title(), current_cat.title()))
+ pywikibot.output(u'Treating page \03{lightpurple}%s\03{default}, currently in \03{lightpurple}%s\03{default}' % (article.title(), current_cat.title()))
# Determine a reasonable amount of context to print
try:
full_text = article.get(get_redirect = True)
- except wikipedia.NoPage:
- wikipedia.output(u'Page %s not found.' % article.title())
+ except pywikibot.NoPage:
+ pywikibot.output(u'Page %s not found.' % article.title())
return
try:
contextLength = full_text.index('\n\n')
@@ -694,7 +698,7 @@
if contextLength > 1000 or contextLength < 0:
contextLength = 500
print
- wikipedia.output(full_text[:contextLength])
+ pywikibot.output(full_text[:contextLength])
print
subcatlist = self.catDB.getSubcats(current_cat)
@@ -709,32 +713,32 @@
# show subcategories as possible choices (with numbers)
for i in range(len(supercatlist)):
# layout: we don't expect a cat to have more than 10 supercats
- wikipedia.output(u'u%d - Move up to %s' % (i, supercatlist[i].title()))
+ pywikibot.output(u'u%d - Move up to %s' % (i, supercatlist[i].title()))
for i in range(len(subcatlist)):
# layout: we don't expect a cat to have more than 100 subcats
- wikipedia.output(u'%2d - Move down to %s' % (i, subcatlist[i].title()))
+ pywikibot.output(u'%2d - Move down to %s' % (i, subcatlist[i].title()))
print ' j - Jump to another category'
print ' s - Skip this article'
print ' r - Remove this category tag'
print ' ? - Print first part of the page (longer and longer)'
- wikipedia.output(u'Enter - Save category as %s' % current_cat.title())
+ pywikibot.output(u'Enter - Save category as %s' % current_cat.title())
flag = False
while not flag:
print ''
- choice=wikipedia.input(u'Choice:')
+ choice = pywikibot.input(u'Choice:')
if choice in ['s', 'S']:
flag = True
elif choice == '':
- wikipedia.output(u'Saving category as %s' % current_cat.title())
+ pywikibot.output(u'Saving category as %s' % current_cat.title())
if current_cat == original_cat:
print 'No changes necessary.'
else:
catlib.change_category(article, original_cat, current_cat, comment = self.editSummary)
flag = True
elif choice in ['j', 'J']:
- newCatTitle = wikipedia.input(u'Please enter the category the article should be moved to:')
- newCat = catlib.Category(wikipedia.getSite(), 'Category:' + newCatTitle)
+ newCatTitle = pywikibot.input(u'Please enter the category the article should be moved to:')
+ newCat = catlib.Category(pywikibot.getSite(), 'Category:' + newCatTitle)
# recurse into chosen category
self.move_to_category(article, original_cat, newCat)
flag = True
@@ -745,7 +749,7 @@
elif choice == '?':
contextLength += 500
print
- wikipedia.output(full_text[:contextLength])
+ pywikibot.output(full_text[:contextLength])
print
# if categories possibly weren't visible, show them additionally
@@ -754,7 +758,7 @@
print ''
print 'Original categories: '
for cat in article.categories():
- wikipedia.output(u'* %s' % cat.title())
+ pywikibot.output(u'* %s' % cat.title())
elif choice[0] == 'u':
try:
choice=int(choice[1:])
@@ -774,15 +778,15 @@
flag = True
def run(self):
- cat = catlib.Category(wikipedia.getSite(), 'Category:' + self.catTitle)
+ cat = catlib.Category(pywikibot.getSite(), 'Category:' + self.catTitle)
articles = cat.articlesList(recurse = False)
if len(articles) == 0:
- wikipedia.output(u'There are no articles in category ' + catTitle)
+ pywikibot.output(u'There are no articles in category ' + catTitle)
else:
preloadingGen = pagegenerators.PreloadingGenerator(iter(articles))
for article in preloadingGen:
- wikipedia.output(u'\n===================================================================')
+ pywikibot.output(u'\n===================================================================')
self.move_to_category(article, cat, cat)
class CategoryTreeRobot:
@@ -803,7 +807,7 @@
self.catTitle = catTitle
self.catDB = catDB
if filename and not os.path.isabs(filename):
- filename = wikipedia.config.datafilepath(filename)
+ filename = pywikibot.config.datafilepath(filename)
self.filename = filename
# TODO: make maxDepth changeable with a parameter or config file entry
self.maxDepth = maxDepth
@@ -863,7 +867,7 @@
# create a list of wiki links to the supercategories
supercat_names.append('[[:%s|%s]]' % (supercats[i].title(), supercats[i].title().split(':', 1)[1]))
# print this list, separated with commas, using translations given in also_in_cats
- result += ' ' + wikipedia.translate(wikipedia.getSite(), also_in_cats) % ', '.join(supercat_names)
+ result += ' ' + pywikibot.translate(pywikibot.getSite(), also_in_cats) % ', '.join(supercat_names)
result += '\n'
if currentDepth < self.maxDepth:
for subcat in self.catDB.getSubcats(cat):
@@ -883,16 +887,16 @@
* catTitle - the title of the category which will be the tree's root
* maxDepth - the limit beyond which no subcategories will be listed
"""
- cat = catlib.Category(wikipedia.getSite(), 'Category:' + self.catTitle)
+ cat = catlib.Category(pywikibot.getSite(), 'Category:' + self.catTitle)
tree = self.treeview(cat)
if self.filename:
- wikipedia.output(u'Saving results in %s' % self.filename)
+ pywikibot.output(u'Saving results in %s' % self.filename)
import codecs
f = codecs.open(self.filename, 'a', 'utf-8')
f.write(tree)
f.close()
else:
- wikipedia.output(tree, toStdout = True)
+ pywikibot.output(tree, toStdout = True)
if __name__ == "__main__":
fromGiven = False
@@ -913,8 +917,8 @@
# The generator gives the pages that should be worked upon.
gen = None
- #If this is set to true then the custom edit summary given for removing
- #categories from articles will also be used as the deletion reason.
+ # If this is set to true then the custom edit summary given for removing
+ # categories from articles will also be used as the deletion reason.
useSummaryForDeletion = True
try:
catDB = CategoryDatabase()
@@ -922,7 +926,7 @@
sort_by_last_name = False
restore = False
create_pages = False
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg == 'add':
action = 'add'
elif arg == 'remove':
@@ -962,7 +966,7 @@
editSummary = arg[len('-summary:'):]
elif arg.startswith('-match'):
if len(arg) == len('-match'):
- titleRegex = wikipedia.input(u'Which regular expression should affected objects match?')
+ titleRegex = pywikibot.input(u'Which regular expression should affected objects match?')
else:
titleRegex = arg[len('-match:'):]
elif arg == '-talkpages':
@@ -988,34 +992,34 @@
add_category(sort_by_last_name, create_pages)
elif action == 'remove':
if (fromGiven == False):
- oldCatTitle = wikipedia.input(u'Please enter the name of the category that should be removed:')
+ oldCatTitle = pywikibot.input(u'Please enter the name of the category that should be removed:')
bot = CategoryRemoveRobot(oldCatTitle, batchMode, editSummary, useSummaryForDeletion, inPlace = inPlace)
bot.run()
elif action == 'move':
if (fromGiven == False):
- oldCatTitle = wikipedia.input(u'Please enter the old name of the category:')
+ oldCatTitle = pywikibot.input(u'Please enter the old name of the category:')
if (toGiven == False):
- newCatTitle = wikipedia.input(u'Please enter the new name of the category:')
+ newCatTitle = pywikibot.input(u'Please enter the new name of the category:')
bot = CategoryMoveRobot(oldCatTitle, newCatTitle, batchMode, editSummary, inPlace, titleRegex = titleRegex)
bot.run()
elif action == 'tidy':
- catTitle = wikipedia.input(u'Which category do you want to tidy up?')
+ catTitle = pywikibot.input(u'Which category do you want to tidy up?')
bot = CategoryTidyRobot(catTitle, catDB)
bot.run()
elif action == 'tree':
- catTitle = wikipedia.input(u'For which category do you want to create a tree view?')
- filename = wikipedia.input(u'Please enter the name of the file where the tree should be saved, or press enter to simply show the tree:')
+ catTitle = pywikibot.input(u'For which category do you want to create a tree view?')
+ filename = pywikibot.input(u'Please enter the name of the file where the tree should be saved, or press enter to simply show the tree:')
bot = CategoryTreeRobot(catTitle, catDB, filename)
bot.run()
elif action == 'listify':
if (fromGiven == False):
- oldCatTitle = wikipedia.input(u'Please enter the name of the category to listify:')
+ oldCatTitle = pywikibot.input(u'Please enter the name of the category to listify:')
if (toGiven == False):
- newCatTitle = wikipedia.input(u'Please enter the name of the list to create:')
+ newCatTitle = pywikibot.input(u'Please enter the name of the list to create:')
bot = CategoryListifyRobot(oldCatTitle, newCatTitle, editSummary, overwrite, showImages, subCats = True, talkPages = talkPages, recurse = recurse)
bot.run()
else:
- wikipedia.showHelp('category')
+ pywikibot.showHelp('category')
finally:
catDB.dump()
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/category_redirect.py
===================================================================
--- trunk/pywikipedia/category_redirect.py 2009-10-28 18:10:56 UTC (rev 7556)
+++ trunk/pywikipedia/category_redirect.py 2009-10-28 18:12:26 UTC (rev 7557)
@@ -12,9 +12,17 @@
are taken into account.
"""
+
+#
+# (C) Pywikipedia team, 2008-2009
+#
__version__ = '$Id$'
+#
+# Distributed under the terms of the MIT license.
+#
-import wikipedia, catlib, query, pagegenerators
+import wikipedia as pywikibot
+import catlib, query, pagegenerators
import cPickle
import math
import re
@@ -37,11 +45,11 @@
class CategoryRedirectBot(object):
def __init__(self):
self.cooldown = 7 # days
- self.site = wikipedia.getSite()
+ self.site = pywikibot.getSite()
self.catprefix = self.site.namespace(14)+":"
self.log_text = []
self.edit_requests = []
- self.log_page = wikipedia.Page(self.site,
+ self.log_page = pywikibot.Page(self.site,
u"User:%(user)s/category redirect log" %
{'user': self.site.loggedInAs()})
@@ -165,7 +173,7 @@
'zh': u"分类重定向维护机器人",
}
- self.edit_request_text = wikipedia.translate(self.site.lang,
+ self.edit_request_text = pywikibot.translate(self.site.lang,
{'en': u"""\
The following protected pages have been detected as requiring updates to \
category links:
@@ -186,10 +194,10 @@
""",
})
- self.edit_request_item = wikipedia.translate(self.site.lang,
+ self.edit_request_item = pywikibot.translate(self.site.lang,
{
- 'en': u"* %s is in %s, which is a redirect to %s",
- 'fr': u"* %s est dans %s, qui est une redirection vers %s",
+ 'en': u"* %s is in %s, which is a redirect to %s",
+ 'fr': u"* %s est dans %s, qui est une redirection vers %s",
'ksh': u"* %s es en %s, un dat es en Ömleidung op %s",
})
@@ -199,42 +207,42 @@
Moves subcategories of oldCat as well. oldCat and newCat should be
Category objects. If newCat is None, the category will be removed.
- This is a copy of portions of catlib.change_category(), with some
- changes.
+ This is a copy of portions of [old] catlib.change_category(), with
+ some changes.
"""
oldtext = article.get(get_redirect=True, force=True)
- newtext = wikipedia.replaceCategoryInPlace(oldtext, oldCat, newCat)
+ newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat)
try:
# even if no changes, still save the page, in case it needs
# an update due to changes in a transcluded template
article.put(newtext, comment)
if newtext == oldtext:
- wikipedia.output(
+ pywikibot.output(
u'No changes in made in page %s.' % article.aslink())
return False
return True
- except wikipedia.EditConflict:
- wikipedia.output(
+ except pywikibot.EditConflict:
+ pywikibot.output(
u'Skipping %s because of edit conflict' % article.aslink())
- except wikipedia.LockedPage:
- wikipedia.output(u'Skipping locked page %s' % article.aslink())
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Skipping locked page %s' % article.aslink())
self.edit_requests.append((article.aslink(),
oldCat.aslink(textlink=True),
newCat.aslink(textlink=True)))
- except wikipedia.SpamfilterError, error:
- wikipedia.output(
+ except pywikibot.SpamfilterError, error:
+ pywikibot.output(
u'Changing page %s blocked by spam filter (URL=%s)'
% (article.aslink(), error.url))
- except wikipedia.NoUsername:
- wikipedia.output(
+ except pywikibot.NoUsername:
+ pywikibot.output(
u"Page %s not saved; sysop privileges required."
% article.aslink())
self.edit_requests.append((article.aslink(textlink=True),
oldCat.aslink(textlink=True),
newCat.aslink(textlink=True)))
- except wikipedia.PageNotSaved, error:
- wikipedia.output(u"Saving page %s failed: %s"
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u"Saving page %s failed: %s"
% (article.aslink(), error.message))
return False
@@ -255,7 +263,7 @@
cmlimit="max"):
found += len(result['categorymembers'])
for item in result['categorymembers']:
- article = wikipedia.Page(self.site, item['title'])
+ article = pywikibot.Page(self.site, item['title'])
changed = self.change_category(article, oldCat, newCat,
comment=editSummary)
if changed: moved += 1
@@ -267,21 +275,21 @@
cmnamespace="10",
cmlimit="max"):
for item in result['categorymembers']:
- doc = wikipedia.Page(self.site, item['title']+"/doc")
+ doc = pywikibot.Page(self.site, item['title']+"/doc")
try:
old_text = doc.get()
- except wikipedia.Error:
+ except pywikibot.Error:
continue
changed = self.change_category(doc, oldCat, newCat,
comment=editSummary)
if changed: moved += 1
if found:
- wikipedia.output(u"%s: %s found, %s moved"
+ pywikibot.output(u"%s: %s found, %s moved"
% (oldCat.title(), found, moved))
return (found, moved)
- except wikipedia.ServerError:
- wikipedia.output(u"Server error: retrying in 5 seconds...")
+ except pywikibot.ServerError:
+ pywikibot.output(u"Server error: retrying in 5 seconds...")
time.sleep(5)
continue
except KeyboardInterrupt:
@@ -301,7 +309,7 @@
def query_results(self, **data):
"""Iterate results from API action=query, using data as parameters."""
querydata = {'action': 'query',
- 'maxlag': str(wikipedia.config.maxlag)}
+ 'maxlag': str(pywikibot.config.maxlag)}
querydata = query.CombineParams(querydata, data)
if not "action" in querydata or not querydata['action'] == 'query':
raise ValueError(
@@ -314,8 +322,8 @@
#if data.startswith(u"unknown_action"):
# e = {'code': data[:14], 'info': data[16:]}
# raise APIError(e)
- except wikipedia.ServerError:
- wikipedia.output(u"Wikimedia Server Error; retrying...")
+ except pywikibot.ServerError:
+ pywikibot.output(u"Wikimedia Server Error; retrying...")
time.sleep(5)
continue
except ValueError:
@@ -323,7 +331,7 @@
# problem. Wait a few seconds and try again
# WARNING: if the server is down, this could
# cause an infinite loop
- wikipedia.output(u"Invalid API response received; retrying...")
+ pywikibot.output(u"Invalid API response received; retrying...")
time.sleep(5)
continue
if type(result) is dict and "error" in result:
@@ -332,7 +340,7 @@
time.sleep(5)
waited += 5
if waited % 30 == 0:
- wikipedia.output(
+ pywikibot.output(
u"(Waited %i seconds due to server lag.)"
% waited)
continue
@@ -367,7 +375,7 @@
LOG_SIZE = 7 # Number of items to keep in active log
try:
log_text = self.log_page.get()
- except wikipedia.NoPage:
+ except pywikibot.NoPage:
log_text = u""
log_items = {}
header = None
@@ -408,9 +416,9 @@
l = time.localtime()
today = "%04d-%02d-%02d" % l[:3]
- edit_request_page = wikipedia.Page(self.site,
+ edit_request_page = pywikibot.Page(self.site,
u"User:%(user)s/category edit requests" % locals())
- datafile = wikipedia.config.datafilepath(
+ datafile = pywikibot.config.datafilepath(
"%s-catmovebot-data" % self.site.dbName())
try:
inp = open(datafile, "rb")
@@ -425,7 +433,7 @@
template_list = self.redir_templates[self.site.family.name
][self.site.lang]
except KeyError:
- wikipedia.output(u"No redirect templates defined for %s"
+ pywikibot.output(u"No redirect templates defined for %s"
% self.site.sitename())
return
# regex to match soft category redirects
@@ -445,13 +453,13 @@
# check for hard-redirected categories that are not already marked
# with an appropriate template
- comment = wikipedia.translate(self.site.lang, self.redir_comment)
+ comment = pywikibot.translate(self.site.lang, self.redir_comment)
for result in self.query_results(list='allpages',
apnamespace='14', # Category:
apfrom='!',
apfilterredir='redirects',
aplimit='max'):
- gen = (wikipedia.Page(self.site, page_item['title'])
+ gen = (pywikibot.Page(self.site, page_item['title'])
for page_item in result['allpages'])
# gen yields all hard redirect pages in namespace 14
for page in pagegenerators.PreloadingGenerator(gen, 120):
@@ -469,7 +477,7 @@
self.log_text.append(u"* Added {{tl|%s}} to %s"
% (template_list[0],
page.aslink(textlink=True)))
- except wikipedia.Error, e:
+ except pywikibot.Error, e:
self.log_text.append(
u"* Failed to add {{tl|%s}} to %s (%s)"
% (template_list[0],
@@ -481,9 +489,9 @@
% (page.aslink(textlink=True),
target.aslink(textlink=True)))
- wikipedia.output("Done checking hard-redirect category pages.")
+ pywikibot.output("Done checking hard-redirect category pages.")
- comment = wikipedia.translate(self.site.lang, self.move_comment)
+ comment = pywikibot.translate(self.site.lang, self.move_comment)
scan_data = {
u'action': 'query',
u'list': 'embeddedin',
@@ -502,7 +510,7 @@
gcmlimit=u'max',
prop='info|categoryinfo'):
for catdata in result['pages'].values():
- thispage = wikipedia.Page(self.site, catdata['title'])
+ thispage = pywikibot.Page(self.site, catdata['title'])
catpages.append(thispage)
if 'categoryinfo' in catdata \
and catdata['categoryinfo']['size'] != "0":
@@ -510,8 +518,8 @@
nonemptypages.append(thispage)
# preload the category pages for redirected categories
- wikipedia.output(u"")
- wikipedia.output(u"Preloading %s category redirect pages"
+ pywikibot.output(u"")
+ pywikibot.output(u"Preloading %s category redirect pages"
% len(catpages))
for cat in pagegenerators.PreloadingGenerator(catpages, 120):
cat_title = cat.titleWithoutNamespace()
@@ -521,7 +529,7 @@
continue
try:
text = cat.get(get_redirect=True)
- except wikipedia.Error:
+ except pywikibot.Error:
self.log_text.append(u"* Could not load %s; ignoring"
% cat.aslink(textlink=True))
continue
@@ -546,7 +554,7 @@
## self.log_text.append(
## u"* Removed category prefix from parameter in %s"
## % cat.aslink(textlink=True))
-## except wikipedia.Error:
+## except pywikibot.Error:
## self.log_text.append(
## u"* Unable to save changes to %s"
## % cat.aslink(textlink=True))
@@ -557,8 +565,8 @@
self.catprefix+cat_name) not in catmap:
del record[cat_name]
- wikipedia.output(u"")
- wikipedia.output(u"Checking %s destination categories" % len(destmap))
+ pywikibot.output(u"")
+ pywikibot.output(u"Checking %s destination categories" % len(destmap))
for dest in pagegenerators.PreloadingGenerator(destmap.keys(), 120):
if not dest.exists():
for d in destmap[dest]:
@@ -600,17 +608,17 @@
newtext = newtext + oldtext.strip()
try:
d.put(newtext,
- wikipedia.translate(self.site.lang,
+ pywikibot.translate(self.site.lang,
self.dbl_redir_comment),
minorEdit=True)
- except wikipedia.Error, e:
+ except pywikibot.Error, e:
self.log_text.append("** Failed: %s" % str(e))
# only scan those pages that have contents (nonemptypages)
# and that haven't been removed from catlist as broken redirects
cats_to_empty = set(catlist) & set(nonemptypages)
- wikipedia.output(u"")
- wikipedia.output(u"Moving pages out of %s redirected categories."
+ pywikibot.output(u"")
+ pywikibot.output(u"Moving pages out of %s redirected categories."
% len(cats_to_empty))
# thread_limit = int(math.log(len(cats_to_empty), 8) + 1)
# threadpool = ThreadList(limit=1) # disabling multi-threads
@@ -638,7 +646,7 @@
cPickle.dump(record, open(datafile, "wb"))
- wikipedia.setAction(wikipedia.translate(self.site.lang,
+ pywikibot.setAction(pywikibot.translate(self.site.lang,
self.maint_comment))
self.log_text.sort()
self.log_page.put(u"\n==%i-%02i-%02iT%02i:%02i:%02iZ==\n"
@@ -655,7 +663,7 @@
def main(*args):
global bot
try:
- a = wikipedia.handleArgs(*args)
+ a = pywikibot.handleArgs(*args)
if len(a) == 1:
raise RuntimeError('Unrecognized argument "%s"' % a[0])
elif a:
@@ -664,7 +672,7 @@
bot = CategoryRedirectBot()
bot.run()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
if __name__ == "__main__":
Modified: trunk/pywikipedia/cosmetic_changes.py
===================================================================
--- trunk/pywikipedia/cosmetic_changes.py 2009-10-28 18:10:56 UTC (rev 7556)
+++ trunk/pywikipedia/cosmetic_changes.py 2009-10-28 18:12:26 UTC (rev 7557)
@@ -32,7 +32,8 @@
all of them, but be careful if you do.
"""
__version__ = '$Id$'
-import wikipedia, pagegenerators, isbn
+import wikipedia as pywikibot
+import pagegenerators, isbn
import sys
import re
@@ -188,7 +189,7 @@
except isbn.InvalidIsbnException, error:
pass
if self.debug:
- wikipedia.showDiff(oldText, text)
+ pywikibot.showDiff(oldText, text)
return text
def fixSelfInterwiki(self, text):
@@ -205,9 +206,9 @@
Makes sure that interwiki links are put to the correct position and
into the right order.
"""
- if wikipedia.calledModuleName() <> 'interwiki':
- interwikiLinks = wikipedia.getLanguageLinks(text, insite = self.site)
- text = wikipedia.replaceLanguageLinks(text, interwikiLinks, site = self.site, template = self.template)
+ if pywikibot.calledModuleName() <> 'interwiki':
+ interwikiLinks = pywikibot.getLanguageLinks(text, insite = self.site)
+ text = pywikibot.replaceLanguageLinks(text, interwikiLinks, site = self.site, template = self.template)
return text
def standardizeCategories(self, text):
@@ -216,9 +217,9 @@
does not sort them.
"""
# The PyWikipediaBot is no longer allowed to touch categories on the German Wikipedia. See http://de.wikipedia.org/wiki/Hilfe_Diskussion:Personendaten/Archiv/bis_2006…
- if self.site != wikipedia.getSite('de', 'wikipedia') and not self.template:
- categories = wikipedia.getCategoryLinks(text, site = self.site)
- text = wikipedia.replaceCategoryLinks(text, categories, site = self.site)
+ if self.site != pywikibot.getSite('de', 'wikipedia') and not self.template:
+ categories = pywikibot.getCategoryLinks(text, site = self.site)
+ text = pywikibot.replaceCategoryLinks(text, categories, site = self.site)
return text
def translateAndCapitalizeNamespaces(self, text):
@@ -242,7 +243,7 @@
# skip main (article) namespace
if thisNs and namespaces:
- text = wikipedia.replaceExcept(text, r'\[\[\s*(' + '|'.join(namespaces) + ') *:(?P<nameAndLabel>.*?)\]\]', r'[[' + thisNs + ':\g<nameAndLabel>]]', exceptions)
+ text = pywikibot.replaceExcept(text, r'\[\[\s*(' + '|'.join(namespaces) + ') *:(?P<nameAndLabel>.*?)\]\]', r'[[' + thisNs + ':\g<nameAndLabel>]]', exceptions)
return text
def cleanUpLinks(self, text):
@@ -259,8 +260,8 @@
# We only work on namespace 0 because pipes and linktrails work
# differently for images and categories.
try:
- page = wikipedia.Page(self.site, titleWithSection)
- except wikipedia.InvalidTitle:
+ page = pywikibot.Page(self.site, titleWithSection)
+ except pywikibot.InvalidTitle:
return match.group()
if page.namespace() == 0:
# Replace underlines by spaces, also multiple underlines
@@ -284,7 +285,7 @@
hadTrailingSpaces = (len(titleWithSection) != titleLength)
# Convert URL-encoded characters to unicode
- titleWithSection = wikipedia.url2unicode(titleWithSection, site = self.site)
+ titleWithSection = pywikibot.url2unicode(titleWithSection, site = self.site)
if titleWithSection == '':
# just skip empty links.
@@ -347,7 +348,7 @@
# note that the definition of 'letter' varies from language to language.
linkR = re.compile(r'\[\[(?P<titleWithSection>[^\]\|]+)(\|(?P<label>[^\]\|]*))?\]\](?P<linktrail>' + self.site.linktrail() + ')')
- text = wikipedia.replaceExcept(text, linkR, handleOneLink, ['comment', 'math', 'nowiki', 'pre', 'startspace'])
+ text = pywikibot.replaceExcept(text, linkR, handleOneLink, ['comment', 'math', 'nowiki', 'pre', 'startspace'])
return text
def resolveHtmlEntities(self, text):
@@ -363,11 +364,11 @@
# ignore ' see http://eo.wikipedia.org/w/index.php?title=Liberec&diff=next&oldid=2320801
if self.site.lang == 'eo':
ignore += [39]
- text = wikipedia.html2unicode(text, ignore = ignore)
+ text = pywikibot.html2unicode(text, ignore = ignore)
return text
def validXhtml(self, text):
- text = wikipedia.replaceExcept(text, r'<br>', r'<br />', ['comment', 'math', 'nowiki', 'pre'])
+ text = pywikibot.replaceExcept(text, r'<br>', r'<br />', ['comment', 'math', 'nowiki', 'pre'])
return text
def removeUselessSpaces(self, text):
@@ -376,8 +377,8 @@
spaceAtLineEndR = re.compile(' $')
exceptions = ['comment', 'math', 'nowiki', 'pre', 'startspace', 'table', 'template']
- text = wikipedia.replaceExcept(text, multipleSpacesR, ' ', exceptions)
- text = wikipedia.replaceExcept(text, spaceAtLineEndR, '', exceptions)
+ text = pywikibot.replaceExcept(text, multipleSpacesR, ' ', exceptions)
+ text = pywikibot.replaceExcept(text, spaceAtLineEndR, '', exceptions)
return text
@@ -387,7 +388,7 @@
front of a percent sign, so it is no longer required to place it
manually.
'''
- text = wikipedia.replaceExcept(text, r'(\d) %', r'\1 %', ['timeline'])
+ text = pywikibot.replaceExcept(text, r'(\d) %', r'\1 %', ['timeline'])
return text
def cleanUpSectionHeaders(self, text):
@@ -402,7 +403,7 @@
"""
for level in range(1, 7):
equals = '=' * level
- text = wikipedia.replaceExcept(text, r'\n' + equals + ' *(?P<title>[^=]+?) *' + equals + ' *\r\n', '\n' + equals + ' \g<title> ' + equals + '\r\n', ['comment', 'math', 'nowiki', 'pre'])
+ text = pywikibot.replaceExcept(text, r'\n' + equals + ' *(?P<title>[^=]+?) *' + equals + ' *\r\n', '\n' + equals + ' \g<title> ' + equals + '\r\n', ['comment', 'math', 'nowiki', 'pre'])
return text
def putSpacesInLists(self, text):
@@ -415,76 +416,77 @@
If there are any complaints, please file a bug report.
"""
if not self.redirect:
- text = wikipedia.replaceExcept(text, r'(?m)^(?P<bullet>(\*+|#+):*)(?P<char>[^\s\*#:].+?)', '\g<bullet> \g<char>', ['comment', 'math', 'nowiki', 'pre'])
+ text = pywikibot.replaceExcept(text, r'(?m)^(?P<bullet>(\*+|#+):*)(?P<char>[^\s\*#:].+?)', '\g<bullet> \g<char>', ['comment', 'math', 'nowiki', 'pre'])
return text
#from fixes.py
def fixSyntaxSave(self, text):
exceptions = ['nowiki', 'comment', 'math', 'pre', 'source', 'startspace']
# external link in double brackets
- text = wikipedia.replaceExcept(text, r'\[\[(?P<url>https?://[^\]]+?)\]\]', r'[\g<url>]', exceptions)
+ text = pywikibot.replaceExcept(text, r'\[\[(?P<url>https?://[^\]]+?)\]\]', r'[\g<url>]', exceptions)
# external link starting with double bracket
- text = wikipedia.replaceExcept(text, r'\[\[(?P<url>https?://.+?)\]', r'[\g<url>]', exceptions)
+ text = pywikibot.replaceExcept(text, r'\[\[(?P<url>https?://.+?)\]', r'[\g<url>]', exceptions)
# external link and description separated by a dash, with
# whitespace in front of the dash, so that it is clear that
# the dash is not a legitimate part of the URL.
- text = wikipedia.replaceExcept(text, r'\[(?P<url>https?://[^\|\] \r\n]+?) +\| *(?P<label>[^\|\]]+?)\]', r'[\g<url> \g<label>]', exceptions)
+ text = pywikibot.replaceExcept(text, r'\[(?P<url>https?://[^\|\] \r\n]+?) +\| *(?P<label>[^\|\]]+?)\]', r'[\g<url> \g<label>]', exceptions)
# dash in external link, where the correct end of the URL can
# be detected from the file extension. It is very unlikely that
# this will cause mistakes.
- text = wikipedia.replaceExcept(text, r'\[(?P<url>https?://[^\|\] ]+?(\.pdf|\.html|\.htm|\.php|\.asp|\.aspx|\.jsp)) *\| *(?P<label>[^\|\]]+?)\]', r'[\g<url> \g<label>]', exceptions)
+ text = pywikibot.replaceExcept(text, r'\[(?P<url>https?://[^\|\] ]+?(\.pdf|\.html|\.htm|\.php|\.asp|\.aspx|\.jsp)) *\| *(?P<label>[^\|\]]+?)\]', r'[\g<url> \g<label>]', exceptions)
return text
def fixHtml(self, text):
# Everything case-insensitive (?i)
# Keep in mind that MediaWiki automatically converts <br> to <br />
exceptions = ['nowiki', 'comment', 'math', 'pre', 'source', 'startspace']
- text = wikipedia.replaceExcept(text, r'(?i)<b>(.*?)</b>', r"'''\1'''" , exceptions)
- text = wikipedia.replaceExcept(text, r'(?i)<strong>(.*?)</strong>', r"'''\1'''" , exceptions)
- text = wikipedia.replaceExcept(text, r'(?i)<i>(.*?)</i>', r"''\1''" , exceptions)
- text = wikipedia.replaceExcept(text, r'(?i)<em>(.*?)</em>', r"''\1''" , exceptions)
+ text = pywikibot.replaceExcept(text, r'(?i)<b>(.*?)</b>', r"'''\1'''" , exceptions)
+ text = pywikibot.replaceExcept(text, r'(?i)<strong>(.*?)</strong>', r"'''\1'''" , exceptions)
+ text = pywikibot.replaceExcept(text, r'(?i)<i>(.*?)</i>', r"''\1''" , exceptions)
+ text = pywikibot.replaceExcept(text, r'(?i)<em>(.*?)</em>', r"''\1''" , exceptions)
# horizontal line without attributes in a single line
- text = wikipedia.replaceExcept(text, r'(?i)([\r\n])<hr[ /]*>([\r\n])', r'\1----\2', exceptions)
+ text = pywikibot.replaceExcept(text, r'(?i)([\r\n])<hr[ /]*>([\r\n])', r'\1----\2', exceptions)
# horizontal line with attributes; can't be done with wiki syntax
# so we only make it XHTML compliant
- text = wikipedia.replaceExcept(text, r'(?i)<hr ([^>/]+?)>', r'<hr \1 />', exceptions)
+ text = pywikibot.replaceExcept(text, r'(?i)<hr ([^>/]+?)>', r'<hr \1 />', exceptions)
# TODO: maybe we can make the bot replace <p> tags with \r\n's.
return text
class CosmeticChangesBot:
- def __init__(self, generator, acceptall = False):
+ def __init__(self, generator, acceptall = False, comment=u'Robot: Cosmetic changes'):
self.generator = generator
self.acceptall = acceptall
+ self.comment = comment
def treat(self, page):
try:
# Show the title of the page we're working on.
# Highlight the title in purple.
- wikipedia.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
ccToolkit = CosmeticChangesToolkit(page.site(), debug = True, namespace = page.namespace())
changedText = ccToolkit.change(page.get())
if changedText != page.get():
if not self.acceptall:
- choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
+ choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No', 'All'], ['y', 'N', 'a'], 'N')
if choice == 'a':
self.acceptall = True
if self.acceptall or choice == 'y':
- page.put(changedText)
+ page.put(changedText, comment=self.comment)
else:
- wikipedia.output('No changes were necessary in %s' % page.title())
- except wikipedia.NoPage:
- wikipedia.output("Page %s does not exist?!" % page.aslink())
- except wikipedia.IsRedirectPage:
- wikipedia.output("Page %s is a redirect; skipping." % page.aslink())
- except wikipedia.LockedPage:
- wikipedia.output("Page %s is locked?!" % page.aslink())
+ pywikibot.output('No changes were necessary in %s' % page.title())
+ except pywikibot.NoPage:
+ pywikibot.output("Page %s does not exist?!" % page.aslink())
+ except pywikibot.IsRedirectPage:
+ pywikibot.output("Page %s is a redirect; skipping." % page.aslink())
+ except pywikibot.LockedPage:
+ pywikibot.output("Page %s is locked?!" % page.aslink())
def run(self):
try:
for page in self.generator:
self.treat(page)
except KeyboardInterrupt:
- wikipedia.output('\nQuitting program...')
+ pywikibot.output('\nQuitting program...')
def main():
#page generator
@@ -498,7 +500,7 @@
# to work on.
genFactory = pagegenerators.GeneratorFactory()
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg.startswith('-summary:'):
editSummary = arg[len('-summary:'):]
elif arg == '-always':
@@ -508,12 +510,11 @@
if editSummary == '':
# Load default summary message.
- editSummary = wikipedia.translate(wikipedia.getSite(), msg_standalone)
- wikipedia.setAction(editSummary)
+ editSummary = pywikibot.translate(pywikibot.getSite(), msg_standalone)
# Disabled this check. Although the point is still valid, there
# is now a warning and a prompt (see below).
- #if wikipedia.getSite() == wikipedia.getSite('nl','wikipedia'):
+ #if pywikibot.getSite() == pywikibot.getSite('nl','wikipedia'):
#print "Deze bot is op WikipediaNL niet gewenst."
#print "Het toevoegen van cosmetic changes bij andere wijzigingen is toegestaan,"
#print "maar cosmetic_changes als stand-alone bot niet."
@@ -521,22 +522,22 @@
#sys.exit()
if pageTitle:
- page = wikipedia.Page(wikipedia.getSite(), ' '.join(pageTitle))
+ page = pywikibot.Page(pywikibot.getSite(), ' '.join(pageTitle))
gen = iter([page])
if not gen:
gen = genFactory.getCombinedGenerator()
if not gen:
- wikipedia.showHelp()
+ pywikibot.showHelp()
elif not always:
- answer = wikipedia.inputChoice(warning + '\nDo you really want to continue?', ['yes', 'no'], ['y', 'N'], 'N')
-
+ answer = pywikibot.inputChoice(warning + '\nDo you really want to continue?', ['yes', 'no'], ['y', 'N'], 'N')
+
if answer == 'y':
preloadingGen = pagegenerators.PreloadingGenerator(gen)
- bot = CosmeticChangesBot(preloadingGen, acceptall=always)
+ bot = CosmeticChangesBot(preloadingGen, acceptall=always, comment=editSummary)
bot.run()
if __name__ == "__main__":
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/editarticle.py
===================================================================
--- trunk/pywikipedia/editarticle.py 2009-10-28 18:10:56 UTC (rev 7556)
+++ trunk/pywikipedia/editarticle.py 2009-10-28 18:12:26 UTC (rev 7557)
@@ -28,7 +28,7 @@
import optparse
import tempfile
-import wikipedia
+import wikipedia as pywikibot
import config
msg = {
@@ -126,7 +126,7 @@
os.unlink(tempFilename)
return self.restoreLinebreaks(newcontent)
else:
- return self.restoreLinebreaks(wikipedia.ui.editText(text, jumpIndex = jumpIndex, highlight = highlight))
+ return self.restoreLinebreaks(pywikibot.ui.editText(text, jumpIndex = jumpIndex, highlight = highlight))
class ArticleEditor:
# join lines if line starts with this ones
@@ -135,12 +135,12 @@
def __init__(self):
self.set_options()
self.setpage()
- self.site = wikipedia.getSite()
+ self.site = pywikibot.getSite()
def set_options(self):
"""Parse commandline and set options attribute"""
my_args = []
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
my_args.append(arg)
parser = optparse.OptionParser()
parser.add_option("-r", "--edit_redirect", action="store_true", default=False, help="Ignore/edit redirects")
@@ -156,9 +156,9 @@
def setpage(self):
"""Sets page and page title"""
- site = wikipedia.getSite()
- pageTitle = self.options.page or wikipedia.input(u"Page to edit:")
- self.page = wikipedia.Page(site, pageTitle)
+ site = pywikibot.getSite()
+ pageTitle = self.options.page or pywikibot.input(u"Page to edit:")
+ self.page = pywikibot.Page(site, pageTitle)
if not self.options.edit_redirect and self.page.isRedirectPage():
self.page = self.page.getRedirectTarget()
@@ -167,25 +167,25 @@
fp = open(fn, 'w')
fp.write(new)
fp.close()
- wikipedia.output(u"An edit conflict has arisen. Your edit has been saved to %s. Please try again." % fn)
+ pywikibot.output(u"An edit conflict has arisen. Your edit has been saved to %s. Please try again." % fn)
def run(self):
try:
old = self.page.get(get_redirect = self.options.edit_redirect)
- except wikipedia.NoPage:
+ except pywikibot.NoPage:
old = ""
textEditor = TextEditor()
new = textEditor.edit(old)
if new and old != new:
- wikipedia.showDiff(old, new)
- changes = wikipedia.input(u"What did you change?")
- comment = wikipedia.translate(wikipedia.getSite(), msg) % changes
+ pywikibot.showDiff(old, new)
+ changes = pywikibot.input(u"What did you change?")
+ comment = pywikibot.translate(pywikibot.getSite(), msg) % changes
try:
self.page.put(new, comment = comment, minorEdit = False, watchArticle=self.options.watch)
- except wikipedia.EditConflict:
+ except pywikibot.EditConflict:
self.handle_edit_conflict(new)
else:
- wikipedia.output(u"Nothing changed")
+ pywikibot.output(u"Nothing changed")
def main():
app = ArticleEditor()
@@ -195,5 +195,5 @@
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/interwiki.py
===================================================================
--- trunk/pywikipedia/interwiki.py 2009-10-28 18:10:56 UTC (rev 7556)
+++ trunk/pywikipedia/interwiki.py 2009-10-28 18:12:26 UTC (rev 7557)
@@ -152,7 +152,7 @@
These arguments define how much user confirmation is required:
-autonomous run automatically, do not ask any questions. If a question
- to an operator is needed, write the name of the page
+ -auto to an operator is needed, write the name of the page
to autonomous_problems.dat and continue on the next page.
(note: without ending colon)
@@ -313,7 +313,8 @@
seq2.reverse()
return seq2
-import wikipedia, config, pagegenerators, catlib
+import wikipedia as pywikibot
+import config, pagegenerators, catlib
import titletranslate, interwiki_graph
import webbrowser
@@ -337,17 +338,17 @@
def __iter__(self):
import xmlreader
- mysite = wikipedia.getSite()
+ mysite = pywikibot.getSite()
dump = xmlreader.XmlDump(self.xmlFilename)
r = re.compile(r'\d')
for entry in dump.parse():
if not r.search(entry.title):
selflinkR = re.compile(r'\[\[lmo:')
if selflinkR.search(entry.text):
- yield wikipedia.Page(mysite, entry.title)
+ yield pywikibot.Page(mysite, entry.title)
-class SaveError(wikipedia.Error):
+class SaveError(pywikibot.Error):
"""
An attempt to save a page with changed interwiki has failed.
"""
@@ -358,7 +359,7 @@
preferences or because the user chose not to change the page.
"""
-class GiveUpOnPage(wikipedia.Error):
+class GiveUpOnPage(pywikibot.Error):
"""
The user chose not to work on this page and its linked pages any more.
"""
@@ -516,7 +517,7 @@
lacklanguage = None
minlinks = 0
-class StoredPage(wikipedia.Page):
+class StoredPage(pywikibot.Page):
"""
Store the Page contents on disk to avoid sucking too much
memory when a big number of Page objects will be loaded
@@ -524,14 +525,14 @@
"""
# Please prefix the class members names by SP
- # to avoid possible name clashes with wikipedia.Page
+ # to avoid possible name clashes with pywikibot.Page
# path to the shelve
SPpath = None
# shelve
SPstore = None
- # attributes created by wikipedia.Page.__init__
+ # attributes created by pywikibot.Page.__init__
SPcopy = [ '_editrestriction',
'_site',
'_namespace',
@@ -869,7 +870,7 @@
return False
if globalvar.nobackonly:
if page == self.originPage:
- wikipedia.output("%s has a backlink from %s."%(page,linkingPage))
+ pywikibot.output(u"%s has a backlink from %s."%(page,linkingPage))
self.makeForcedStop(counter)
return False
@@ -910,31 +911,31 @@
if linkedPage.namespace() in nsmatch:
return False
if globalvar.autonomous:
- wikipedia.output(u"NOTE: Ignoring link from page %s in namespace %i to page %s in namespace %i." % (self.originPage.aslink(True), self.originPage.namespace(), linkedPage.aslink(True), linkedPage.namespace()))
+ pywikibot.output(u"NOTE: Ignoring link from page %s in namespace %i to page %s in namespace %i." % (self.originPage.aslink(True), self.originPage.namespace(), linkedPage.aslink(True), linkedPage.namespace()))
# Fill up foundIn, so that we will not write this notice
self.foundIn[linkedPage] = [linkingPage]
return True
else:
preferredPage = self.getFoundInCorrectNamespace(linkedPage.site())
if preferredPage:
- wikipedia.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.aslink(True), self.originPage.namespace(), linkedPage.aslink(True), linkedPage.namespace(), preferredPage.aslink(True)))
+ 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.aslink(True), self.originPage.namespace(), linkedPage.aslink(True), linkedPage.namespace(), preferredPage.aslink(True)))
return True
else:
- choice = wikipedia.inputChoice('WARNING: %s is in namespace %i, but %s is in namespace %i. Follow it anyway?' % (self.originPage.aslink(True), self.originPage.namespace(), linkedPage.aslink(True), linkedPage.namespace()), ['Yes', 'No', 'Add an alternative', 'give up'], ['y', 'n', 'a', 'g'])
+ choice = pywikibot.inputChoice('WARNING: %s is in namespace %i, but %s is in namespace %i. Follow it anyway?' % (self.originPage.aslink(True), self.originPage.namespace(), linkedPage.aslink(True), linkedPage.namespace()), ['Yes', 'No', 'Add an alternative', 'give up'], ['y', 'n', 'a', 'g'])
if choice != 'y':
# Fill up foundIn, so that we will not ask again
self.foundIn[linkedPage] = [linkingPage]
if choice == 'g':
self.makeForcedStop(counter)
elif choice == 'a':
- newHint = wikipedia.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 = wikipedia.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)
else:
- wikipedia.output(u"NOTE: ignoring %s and its interwiki links" % linkedPage.aslink(True))
+ pywikibot.output(u"NOTE: ignoring %s and its interwiki links" % linkedPage.aslink(True))
return True
else:
# same namespaces, no problem
@@ -943,10 +944,10 @@
def wiktionaryMismatch(self, page):
if globalvar.same=='wiktionary':
if page.title().lower() != self.originPage.title().lower():
- wikipedia.output(u"NOTE: Ignoring %s for %s in wiktionary mode" % (page.aslink(), self.originPage.aslink()))
+ pywikibot.output(u"NOTE: Ignoring %s for %s in wiktionary mode" % (page.aslink(), self.originPage.aslink()))
return True
elif page.title() != self.originPage.title() and self.originPage.site().nocapitalize and page.site().nocapitalize:
- wikipedia.output(u"NOTE: Ignoring %s for %s in wiktionary mode because both languages are uncapitalized." % (page.aslink(), self.originPage.aslink()))
+ pywikibot.output(u"NOTE: Ignoring %s for %s in wiktionary mode because both languages are uncapitalized." % (page.aslink(), self.originPage.aslink()))
return True
return False
@@ -966,32 +967,32 @@
"""
if globalvar.autonomous:
if self.originPage.isDisambig() and not page.isDisambig():
- wikipedia.output(u"NOTE: Ignoring link from disambiguation page %s to non-disambiguation %s" % (self.originPage.aslink(True), page.aslink(True)))
+ pywikibot.output(u"NOTE: Ignoring link from disambiguation page %s to non-disambiguation %s" % (self.originPage.aslink(True), page.aslink(True)))
return (True, None)
elif not self.originPage.isDisambig() and page.isDisambig():
- wikipedia.output(u"NOTE: Ignoring link from non-disambiguation page %s to disambiguation %s" % (self.originPage.aslink(True), page.aslink(True)))
+ pywikibot.output(u"NOTE: Ignoring link from non-disambiguation page %s to disambiguation %s" % (self.originPage.aslink(True), page.aslink(True)))
return (True, None)
else:
choice = 'y'
if self.originPage.isDisambig() and not page.isDisambig():
disambig = self.getFoundDisambig(page.site())
if disambig:
- wikipedia.output(u"NOTE: Ignoring non-disambiguation page %s for %s because disambiguation page %s has already been found." % (page.aslink(True), self.originPage.aslink(True), disambig.aslink(True)))
+ pywikibot.output(u"NOTE: Ignoring non-disambiguation page %s for %s because disambiguation page %s has already been found." % (page.aslink(True), self.originPage.aslink(True), disambig.aslink(True)))
return (True, None)
else:
- choice = wikipedia.inputChoice('WARNING: %s is a disambiguation page, but %s doesn\'t seem to be one. Follow it anyway?' % (self.originPage.aslink(True), page.aslink(True)), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
+ choice = pywikibot.inputChoice('WARNING: %s is a disambiguation page, but %s doesn\'t seem to be one. Follow it anyway?' % (self.originPage.aslink(True), page.aslink(True)), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
elif not self.originPage.isDisambig() and page.isDisambig():
nondisambig = self.getFoundNonDisambig(page.site())
if nondisambig:
- wikipedia.output(u"NOTE: Ignoring disambiguation page %s for %s because non-disambiguation page %s has already been found." % (page.aslink(True), self.originPage.aslink(True), nondisambig.aslink(True)))
+ pywikibot.output(u"NOTE: Ignoring disambiguation page %s for %s because non-disambiguation page %s has already been found." % (page.aslink(True), self.originPage.aslink(True), nondisambig.aslink(True)))
return (True, None)
else:
- choice = wikipedia.inputChoice('WARNING: %s doesn\'t seem to be a disambiguation page, but %s is one. Follow it anyway?' % (self.originPage.aslink(True), page.aslink(True)), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
+ choice = pywikibot.inputChoice('WARNING: %s doesn\'t seem to be a disambiguation page, but %s is one. Follow it anyway?' % (self.originPage.aslink(True), page.aslink(True)), ['Yes', 'No', 'Add an alternative', 'Give up'], ['y', 'n', 'a', 'g'])
if choice == 'n':
return (True, None)
elif choice == 'a':
- newHint = wikipedia.input(u'Give the alternative for language %s, not using a language code:' % page.site().language())
- alternativePage = wikipedia.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)
@@ -1001,18 +1002,18 @@
def isIgnored(self, page):
if page.site().language() in globalvar.neverlink:
- wikipedia.output(u"Skipping link %s to an ignored language" % page.aslink())
+ pywikibot.output(u"Skipping link %s to an ignored language" % page.aslink())
return True
if page in globalvar.ignore:
- wikipedia.output(u"Skipping link %s to an ignored page" % page.aslink())
+ pywikibot.output(u"Skipping link %s to an ignored page" % page.aslink())
return True
return False
def reportInterwikilessPage(self, page):
- wikipedia.output(u"NOTE: %s does not have any interwiki links" % self.originPage.aslink(True))
+ pywikibot.output(u"NOTE: %s does not have any interwiki links" % self.originPage.aslink(True))
if config.without_interwiki:
f = codecs.open(
- wikipedia.config.datafilepath('without_interwiki.txt'), 'a', 'utf-8')
+ pywikibot.config.datafilepath('without_interwiki.txt'), 'a', 'utf-8')
f.write("# %s \n" % page.aslink(forceInterwiki=True))
f.close()
@@ -1027,15 +1028,15 @@
newhint = None
t = globalvar.showtextlink
if t:
- wikipedia.output(self.originPage.get()[:t])
+ pywikibot.output(self.originPage.get()[:t])
# loop
while True:
- newhint = wikipedia.input(u'Give a hint (? to see pagetext):')
+ newhint = pywikibot.input(u'Give a hint (? to see pagetext):')
if newhint == '?':
t += globalvar.showtextlinkadd
- wikipedia.output(self.originPage.get()[:t])
+ pywikibot.output(self.originPage.get()[:t])
elif newhint and not ':' in newhint:
- wikipedia.output(u'Please enter a hint in the format language:pagename or type nothing if you do not have a hint.')
+ pywikibot.output(u'Please enter a hint in the format language:pagename or type nothing if you do not have a hint.')
elif not newhint:
break
else:
@@ -1066,7 +1067,7 @@
if globalvar.skipauto:
dictName, year = page.autoFormat()
if dictName is not None:
- wikipedia.output(u'WARNING: %s:%s relates to %s:%s, which is an auto entry %s(%s)' % (self.originPage.site().language(), self.originPage.title(), page.site().language(),page.title(),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.title(), page.site().language(),page.title(),dictName,year))
# Register this fact at the todo-counter.
counter.minus(page.site())
@@ -1075,7 +1076,7 @@
if not page.exists():
- wikipedia.output(u"NOTE: %s does not exist" % page.aslink(True))
+ pywikibot.output(u"NOTE: %s does not exist" % page.aslink(True))
if page == self.originPage:
# The page we are working on is the page that does not exist.
# No use in doing any work on it in that case.
@@ -1089,12 +1090,12 @@
elif page.isRedirectPage():
try:
redirectTargetPage = page.getRedirectTarget()
- except wikipedia.InvalidTitle:
+ except pywikibot.InvalidTitle:
# MW considers #redirect [[en:#foo]] as a redirect page,
# but we can't do anything useful with such pages
- wikipedia.output(u"NOTE: %s redirects to an invalid title" % page.aslink(True))
+ pywikibot.output(u"NOTE: %s redirects to an invalid title" % page.aslink(True))
continue
- wikipedia.output(u"NOTE: %s is redirect to %s" % (page.aslink(True), redirectTargetPage.aslink(True)))
+ pywikibot.output(u"NOTE: %s is redirect to %s" % (page.aslink(True), redirectTargetPage.aslink(True)))
if page == self.originPage:
if globalvar.initialredirect:
if globalvar.contentsondisk:
@@ -1110,12 +1111,12 @@
counter.minus(site, count)
self.todo = PageTree()
elif not globalvar.followredirect:
- wikipedia.output(u"NOTE: not following redirects.")
+ pywikibot.output(u"NOTE: not following redirects.")
elif page.site().family == redirectTargetPage.site().family \
and not self.skipPage(page, redirectTargetPage, counter):
if self.addIfNew(redirectTargetPage, counter, page):
if config.interwiki_shownew:
- wikipedia.output(u"%s: %s gives new redirect %s" % (self.originPage.aslink(), page.aslink(True), redirectTargetPage.aslink(True)))
+ pywikibot.output(u"%s: %s gives new redirect %s" % (self.originPage.aslink(), page.aslink(True), redirectTargetPage.aslink(True)))
continue
@@ -1127,13 +1128,13 @@
try:
iw = page.interwiki()
- except wikipedia.NoSuchSite:
- wikipedia.output(u"NOTE: site %s does not exist" % page.site())
+ except pywikibot.NoSuchSite:
+ pywikibot.output(u"NOTE: site %s does not exist" % page.site())
continue
(skip, alternativePage) = self.disambigMismatch(page, counter)
if skip:
- wikipedia.output(u"NOTE: ignoring %s and its interwiki links" % page.aslink(True))
+ pywikibot.output(u"NOTE: ignoring %s and its interwiki links" % page.aslink(True))
self.done.remove(page)
iw = ()
if alternativePage:
@@ -1161,14 +1162,14 @@
elif globalvar.autonomous and duplicate:
- wikipedia.output(u"Stopping work on %s because duplicate pages"\
+ pywikibot.output(u"Stopping work on %s because duplicate pages"\
" %s and %s are found" % (self.originPage.aslink(True),
duplicate.aslink(True),
page.aslink(True)))
self.makeForcedStop(counter)
try:
f = codecs.open(
- wikipedia.config.datafilepath('autonomous_problems.dat'),
+ pywikibot.config.datafilepath('autonomous_problems.dat'),
'a', 'utf-8')
f.write("* %s {Found more than one link for %s}" % (self.originPage.aslink(True), page.site()))
if config.interwiki_graph and config.interwiki_graph_url:
@@ -1180,11 +1181,11 @@
# except: should be avoided!!
except:
#raise
- wikipedia.output(u'File autonomous_problems.dat open or corrupted! Try again with -restore.')
+ pywikibot.output(u'File autonomous_problems.dat open or corrupted! Try again with -restore.')
sys.exit()
iw = ()
elif page.isEmpty() and not page.isCategory():
- wikipedia.output(u"NOTE: %s is empty; ignoring it and its interwiki links" % page.aslink(True))
+ pywikibot.output(u"NOTE: %s is empty; ignoring it and its interwiki links" % page.aslink(True))
# Ignore the interwiki links
self.done.remove(page)
iw = ()
@@ -1192,7 +1193,7 @@
for linkedPage in iw:
if globalvar.hintsareright:
if linkedPage.site in self.hintedsites:
- wikipedia.output(u"NOTE: %s: %s extra interwiki on hinted site ignored %s" % (self.originPage.aslink(), page.aslink(True), linkedPage.aslink(True)))
+ pywikibot.output(u"NOTE: %s: %s extra interwiki on hinted site ignored %s" % (self.originPage.aslink(), page.aslink(True), linkedPage.aslink(True)))
break
if not self.skipPage(page, linkedPage, counter):
if globalvar.followinterwiki or page == self.originPage:
@@ -1204,11 +1205,11 @@
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!
- wikipedia.output(u"NOTE: %s: %s gives duplicate interwiki on same site %s" % (self.originPage.aslink(True), page.aslink(True), linkedPage.aslink(True)))
+ pywikibot.output(u"NOTE: %s: %s gives duplicate interwiki on same site %s" % (self.originPage.aslink(True), page.aslink(True), linkedPage.aslink(True)))
break
else:
if config.interwiki_shownew:
- wikipedia.output(u"%s: %s gives new interwiki %s"% (self.originPage.aslink(), page.aslink(True), linkedPage.aslink(True)))
+ pywikibot.output(u"%s: %s gives new interwiki %s"% (self.originPage.aslink(), page.aslink(True), linkedPage.aslink(True)))
# These pages are no longer 'in progress'
self.pending = PageTree()
@@ -1223,7 +1224,7 @@
def problem(self, txt, createneed = True):
"""Report a problem with the resolution of this subject."""
- wikipedia.output(u"ERROR: %s" % txt)
+ pywikibot.output(u"ERROR: %s" % txt)
self.confirm = True
if createneed:
self.problemfound = True
@@ -1231,15 +1232,15 @@
def whereReport(self, page, indent=4):
for page2 in sorted(self.foundIn[page]):
if page2 is None:
- wikipedia.output(u" "*indent + "Given as a hint.")
+ pywikibot.output(u" "*indent + "Given as a hint.")
else:
- wikipedia.output(u" "*indent + page2.aslink(True))
+ pywikibot.output(u" "*indent + page2.aslink(True))
def assemble(self):
# No errors have been seen so far, except....
errorCount = self.problemfound
- mysite = wikipedia.getSite()
+ mysite = pywikibot.getSite()
# Build up a dictionary of all pages found, with the site as key.
# Each value will be a list of pages.
new = {}
@@ -1282,15 +1283,15 @@
# First loop over the ones that have more solutions
for site, pages in new.iteritems():
if len(pages) > 1:
- wikipedia.output(u"=" * 30)
- wikipedia.output(u"Links to %s" % site)
+ pywikibot.output(u"=" * 30)
+ pywikibot.output(u"Links to %s" % site)
i = 0
for page2 in pages:
i += 1
- wikipedia.output(u" (%d) Found link to %s in:" % (i, page2.aslink(True)))
+ pywikibot.output(u" (%d) Found link to %s in:" % (i, page2.aslink(True)))
self.whereReport(page2, indent = 8)
while True:
- answer = wikipedia.input(u"Which variant should be used [number, (n)one, (g)ive up] :")
+ answer = pywikibot.input(u"Which variant should be used [number, (n)one, (g)ive up] :")
if answer:
if answer == 'g':
return None
@@ -1312,15 +1313,15 @@
for site, pages in new.iteritems():
if len(pages) == 1:
if not acceptall:
- wikipedia.output(u"=" * 30)
+ pywikibot.output(u"=" * 30)
page2 = pages[0]
- wikipedia.output(u"Found link to %s in:" % page2.aslink(True))
+ pywikibot.output(u"Found link to %s in:" % page2.aslink(True))
self.whereReport(page2, indent = 4)
while True:
if acceptall:
answer = 'a'
else:
- answer = wikipedia.inputChoice(u'What should be done?', ['accept', 'reject', 'give up', 'accept all'], ['a', 'r', 'g', 'l'], 'a')
+ answer = pywikibot.inputChoice(u'What should be done?', ['accept', 'reject', 'give up', 'accept all'], ['a', 'r', 'g', 'l'], 'a')
if answer == 'l': # accept all
acceptall = True
answer = 'a'
@@ -1347,7 +1348,7 @@
if not self.workonme:
return
if self.forcedStop:
- wikipedia.output(u"======Aborted processing %s======" % self.originPage.aslink(True))
+ pywikibot.output(u"======Aborted processing %s======" % self.originPage.aslink(True))
return
if self.originPage.isRedirectPage():
return
@@ -1359,11 +1360,11 @@
# if len(self.done) == 1:
# # No interwiki at all
# return
- wikipedia.output(u"======Post-processing %s======" % self.originPage.aslink(True))
+ pywikibot.output(u"======Post-processing %s======" % self.originPage.aslink(True))
# Assemble list of accepted interwiki links
new = self.assemble()
if new is None: # User said give up or autonomous with problem
- wikipedia.output(u"======Aborted processing %s======" % self.originPage.aslink(True))
+ pywikibot.output(u"======Aborted processing %s======" % self.originPage.aslink(True))
return
# Make sure new contains every page link, including the page we are processing
@@ -1382,7 +1383,7 @@
frgnSiteDone = False
for siteCode in lclSite.family.languages_by_size:
- site = wikipedia.getSite(code = siteCode)
+ site = pywikibot.getSite(code = siteCode)
if (not lclSiteDone and site == lclSite) or (not frgnSiteDone and site != lclSite and site in new):
if site == lclSite:
lclSiteDone = True # even if we fail the update
@@ -1401,8 +1402,8 @@
try:
for page in new[site].interwiki():
old[page.site()] = page
- except wikipedia.NoPage:
- wikipedia.output(u"BUG>>> %s no longer exists?" % new[site].aslink(True))
+ except pywikibot.NoPage:
+ pywikibot.output(u"BUG>>> %s no longer exists?" % new[site].aslink(True))
continue
mods, mcomment, adding, removing, modifying = compareLanguages(old, new, insite = lclSite)
if (len(removing) > 0 and not globalvar.autonomous) or (len(modifying) > 0 and self.problemfound) or len(old) == 0 or (globalvar.needlimit and len(adding) + len(modifying) >= globalvar.needlimit +1):
@@ -1411,7 +1412,7 @@
updatedSites.append(site)
except SaveError:
notUpdatedSites.append(site)
- except wikipedia.NoUsername:
+ except pywikibot.NoUsername:
pass
except GiveUpOnPage:
break
@@ -1470,16 +1471,16 @@
if page.title() != page.sectionFreeTitle():
# This is not a page, but a subpage. Do not edit it.
- wikipedia.output(u"Not editing %s: not doing interwiki on subpages" % page.aslink(True))
+ pywikibot.output(u"Not editing %s: not doing interwiki on subpages" % page.aslink(True))
raise SaveError
try:
pagetext = page.get()
- except wikipedia.NoPage:
- wikipedia.output(u"Not editing %s: page does not exist" % page.aslink(True))
+ except pywikibot.NoPage:
+ pywikibot.output(u"Not editing %s: page does not exist" % page.aslink(True))
raise SaveError
# Show a message in purple.
- wikipedia.output("\03{lightpurple}Updating links on page %s.\03{default}" % page.aslink(True))
+ pywikibot.output(u"\03{lightpurple}Updating links on page %s.\03{default}" % page.aslink(True))
# clone original newPages dictionary, so that we can modify it to the local page's needs
new = dict(newPages)
@@ -1489,18 +1490,18 @@
# remove interwiki links to ignore
for iw in re.finditer('<!-- *\[\[(.*?:.*?)\]\] *-->', pagetext):
try:
- ignorepage = wikipedia.Page(page.site(), iw.groups()[0])
- except (wikipedia.NoSuchSite, wikipedia.InvalidTitle):
+ 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 (ignorepage not in interwikis):
- wikipedia.output(u"Ignoring link to %(to)s for %(from)s" % {'to': ignorepage.aslink(), 'from': page.aslink()})
+ pywikibot.output(u"Ignoring link to %(to)s for %(from)s" % {'to': ignorepage.aslink(), 'from': page.aslink()})
new.pop(ignorepage.site())
else:
- wikipedia.output(u"NOTE: Not removing interwiki from %(from)s to %(to)s (exists both commented and non-commented)" % {'to': ignorepage.aslink(), 'from': page.aslink()})
+ pywikibot.output(u"NOTE: Not removing interwiki from %(from)s to %(to)s (exists both commented and non-commented)" % {'to': ignorepage.aslink(), 'from': page.aslink()})
except KeyError:
pass
@@ -1509,7 +1510,7 @@
if pltmp != page:
s = "None"
if pltmp is not None: s = pltmp.aslink(True)
- wikipedia.output(u"BUG>>> %s is not in the list of new links! Found %s." % (page.aslink(True), s))
+ pywikibot.output(u"BUG>>> %s is not in the list of new links! Found %s." % (page.aslink(True), s))
raise SaveError
# Avoid adding an iw link back to itself
@@ -1532,28 +1533,28 @@
# temporary hard-coded special case to get rid of thousands of broken links to the Lombard Wikipedia,
# where useless bot-created articles were mass-deleted. See for example:
# http://meta.wikimedia.org/wiki/Proposals_for_closing_projects/Closure_of_Lo…
- if rmsite == wikipedia.getSite('lmo', 'wikipedia'):
- wikipedia.output('Found bad link to %s. As many lmo pages were deleted, it is assumed that it can be safely removed.' % rmPage.aslink())
+ if rmsite == pywikibot.getSite('lmo', 'wikipedia'):
+ pywikibot.output(u'Found bad link to %s. As many lmo pages were deleted, it is assumed that it can be safely removed.' % rmPage.aslink())
else:
##########
new[rmsite] = old[rmsite]
- wikipedia.output(u"WARNING: %s is either deleted or has a mismatching disambiguation state." % rmPage.aslink(True))
+ pywikibot.output(u"WARNING: %s is either deleted or has a mismatching disambiguation state." % rmPage.aslink(True))
# Re-Check what needs to get done
mods, mcomment, adding, removing, modifying = compareLanguages(old, new, insite = page.site())
if not mods:
- wikipedia.output(u'No changes needed' )
+ pywikibot.output(u'No changes needed' )
return False
- wikipedia.output(u"Changes to be made: %s" % mods)
+ pywikibot.output(u"Changes to be made: %s" % mods)
oldtext = page.get()
- newtext = wikipedia.replaceLanguageLinks(oldtext, new, site = page.site(), template = (page.namespace() == 10) )
+ newtext = pywikibot.replaceLanguageLinks(oldtext, new, site = page.site(), template = (page.namespace() == 10) )
if newtext == oldtext:
return False
if globalvar.debug:
- wikipedia.showDiff(oldtext, newtext)
+ pywikibot.showDiff(oldtext, newtext)
- # wikipedia.output(u"NOTE: Replace %s" % page.aslink())
+ # pywikibot.output(u"NOTE: Replace %s" % page.aslink())
# 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
@@ -1569,7 +1570,7 @@
# If we cannot ask, deny permission
answer = 'n'
else:
- answer = wikipedia.inputChoice(u'Submit?',
+ answer = pywikibot.inputChoice(u'Submit?',
['Yes', 'No', 'open in Browser', 'Give up'],
['y', 'n', 'b', 'g'])
if answer == 'b':
@@ -1577,51 +1578,51 @@
page.site().hostname(),
page.site().nice_get_address(page.title())
))
- wikipedia.input("Press Enter when finished in browser.")
+ pywikibot.input("Press Enter when finished in browser.")
return True
else:
# If we do not need to ask, allow
answer = 'y'
# If we got permission to submit, do so
if answer == 'y':
- # Check whether we will have to wait for wikipedia. If so, make
+ # Check whether we will have to wait for pywikibot. If so, make
# another get-query first.
if bot:
- while wikipedia.get_throttle.waittime() + 2.0 < wikipedia.put_throttle.waittime():
- wikipedia.output(u"NOTE: Performing a recursive query first to save time....")
+ while pywikibot.get_throttle.waittime() + 2.0 < pywikibot.put_throttle.waittime():
+ pywikibot.output(u"NOTE: Performing a recursive query first to save time....")
qdone = bot.oneQuery()
if not qdone:
# Nothing more to do
break
- wikipedia.output(u"NOTE: Updating live wiki...")
+ pywikibot.output(u"NOTE: Updating live wiki...")
timeout=60
while 1:
try:
status, reason, data = page.put(newtext, comment = mcomment)
- except wikipedia.LockedPage:
- wikipedia.output(u'Page %s is locked. Skipping.' % (page.title(),))
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Page %s is locked. Skipping.' % (page.title(),))
raise SaveError
- except wikipedia.EditConflict:
- wikipedia.output(u'ERROR putting page: An edit conflict occurred. Giving up.')
+ except pywikibot.EditConflict:
+ pywikibot.output(u'ERROR putting page: An edit conflict occurred. Giving up.')
raise SaveError
- except (wikipedia.SpamfilterError), error:
- wikipedia.output(u'ERROR putting page: %s blacklisted by spamfilter. Giving up.' % (error.url,))
+ except (pywikibot.SpamfilterError), error:
+ pywikibot.output(u'ERROR putting page: %s blacklisted by spamfilter. Giving up.' % (error.url,))
raise SaveError
- 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,))
raise SaveError
except (socket.error, IOError), error:
if timeout>3600:
raise
- wikipedia.output(u'ERROR putting page: %s' % (error.args,))
- wikipedia.output(u'Sleeping %i seconds before trying again.' % (timeout,))
+ pywikibot.output(u'ERROR putting page: %s' % (error.args,))
+ pywikibot.output(u'Sleeping %i seconds before trying again.' % (timeout,))
timeout *= 2
time.sleep(timeout)
- except wikipedia.ServerError:
+ except pywikibot.ServerError:
if timeout>3600:
raise
- wikipedia.output(u'ERROR putting page: ServerError.')
- wikipedia.output(u'Sleeping %i seconds before trying again.' % (timeout,))
+ pywikibot.output(u'ERROR putting page: ServerError.')
+ pywikibot.output(u'Sleeping %i seconds before trying again.' % (timeout,))
timeout *= 2
time.sleep(timeout)
else:
@@ -1629,7 +1630,7 @@
if str(status) == '302':
return True
else:
- wikipedia.output(u'%s %s' % (status, reason))
+ pywikibot.output(u'%s %s' % (status, reason))
return False
elif answer == 'g':
raise GiveUpOnPage
@@ -1653,8 +1654,8 @@
if not page.section():
try:
linkedPages = set(page.interwiki())
- except wikipedia.NoPage:
- wikipedia.output(u"WARNING: Page %s does no longer exist?!" % page.title())
+ except pywikibot.NoPage:
+ pywikibot.output(u"WARNING: Page %s does no longer exist?!" % page.title())
break
# To speed things up, create a dictionary which maps sites to pages.
# This assumes that there is only one interwiki link per language.
@@ -1665,18 +1666,18 @@
if expectedPage != page:
try:
linkedPage = linkedPagesDict[expectedPage.site()]
- wikipedia.output(u"WARNING: %s: %s does not link to %s but to %s" % (page.site().family.name, page.aslink(True), expectedPage.aslink(True), linkedPage.aslink(True)))
+ pywikibot.output(u"WARNING: %s: %s does not link to %s but to %s" % (page.site().family.name, page.aslink(True), expectedPage.aslink(True), linkedPage.aslink(True)))
except KeyError:
- wikipedia.output(u"WARNING: %s: %s does not link to %s" % (page.site().family.name, page.aslink(True), expectedPage.aslink(True)))
+ pywikibot.output(u"WARNING: %s: %s does not link to %s" % (page.site().family.name, page.aslink(True), expectedPage.aslink(True)))
# 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:
- wikipedia.output(u"WARNING: %s: %s links to incorrect %s" % (page.site().family.name, page.aslink(True), linkedPage.aslink(True)))
+ pywikibot.output(u"WARNING: %s: %s links to incorrect %s" % (page.site().family.name, page.aslink(True), linkedPage.aslink(True)))
except (socket.error, IOError):
- wikipedia.output(u'ERROR: could not report backlinks')
+ pywikibot.output(u'ERROR: could not report backlinks')
class InterwikiBot(object):
"""A class keeping track of a list of subjects, controlling which pages
@@ -1710,15 +1711,15 @@
self.generateUntil = until
def dump(self):
- site = wikipedia.getSite()
- dumpfn = wikipedia.config.datafilepath(
+ site = pywikibot.getSite()
+ dumpfn = pywikibot.config.datafilepath(
'interwiki-dumps',
'interwikidump-%s-%s.txt' % (site.family.name, site.lang))
f = codecs.open(dumpfn, 'w', 'utf-8')
for subj in self.subjects:
f.write(subj.originPage.aslink(None)+'\n')
f.close()
- wikipedia.output(u'Dump %s (%s) saved' % (site.lang, site.family.name))
+ pywikibot.output(u'Dump %s (%s) saved' % (site.lang, site.family.name))
def generateMore(self, number):
"""Generate more subjects. This is called internally when the
@@ -1726,26 +1727,26 @@
PageGenerator"""
fs = self.firstSubject()
if fs:
- wikipedia.output(u"NOTE: The first unfinished subject is " + fs.originPage.aslink(True))
- wikipedia.output(u"NOTE: Number of pages queued is %d, trying to add %d more."%(len(self.subjects), number))
+ pywikibot.output(u"NOTE: The first unfinished subject is " + fs.originPage.aslink(True))
+ pywikibot.output(u"NOTE: Number of pages queued is %d, trying to add %d more."%(len(self.subjects), number))
for i in range(number):
try:
while True:
page = self.pageGenerator.next()
if page in globalvar.skip:
- wikipedia.output(u'Skipping: %s is in the skip list' % page.title())
+ pywikibot.output(u'Skipping: %s is in the skip list' % page.title())
continue
if globalvar.skipauto:
dictName, year = page.autoFormat()
if dictName is not None:
- wikipedia.output(u'Skipping: %s is an auto entry %s(%s)' % (page.title(),dictName,year))
+ pywikibot.output(u'Skipping: %s is an auto entry %s(%s)' % (page.title(),dictName,year))
continue
if globalvar.parenthesesonly:
# Only yield pages that have ( ) in titles
if "(" not in page.title():
continue
if page.isTalkPage():
- wikipedia.output(u'Skipping: %s is a talk page' % page.title())
+ pywikibot.output(u'Skipping: %s is a talk page' % page.title())
continue
break
@@ -1781,8 +1782,8 @@
# have to wait before submitting another modification to go live. Select
# any language from counts.
oc = self.counts
- if wikipedia.getSite() in oc:
- return wikipedia.getSite()
+ if pywikibot.getSite() in oc:
+ return pywikibot.getSite()
for lang in oc:
count = self.counts[lang]
if count > max:
@@ -1793,8 +1794,8 @@
def selectQuerySite(self):
"""Select the site the next query should go out for."""
# How many home-language queries we still have?
- ###xqt: its seems this counts a negative value
- mycount = max(0, self.counts.get(wikipedia.getSite(), 0))
+ ### it seems this counts a negative value
+ mycount = max(0, self.counts.get(pywikibot.getSite(), 0))
# Do we still have enough subjects to work on for which the
# home language has been retrieved? This is rough, because
# some subjects may need to retrieve a second home-language page!
@@ -1805,17 +1806,17 @@
while timeout<3600:
try:
self.generateMore(globalvar.maxquerysize - mycount)
- except wikipedia.ServerError:
+ except pywikibot.ServerError:
# Could not extract allpages special page?
- wikipedia.output(u'ERROR: could not retrieve more pages. Will try again in %d seconds'%timeout)
+ pywikibot.output(u'ERROR: could not retrieve more pages. Will try again in %d seconds'%timeout)
time.sleep(timeout)
timeout *= 2
else:
break
# If we have a few, getting the home language is a good thing.
try:
- if self.counts[wikipedia.getSite()] > 4:
- return wikipedia.getSite()
+ if self.counts[pywikibot.getSite()] > 4:
+ return pywikibot.getSite()
except KeyError:
pass
# If getting the home language doesn't make sense, see how many
@@ -1832,7 +1833,7 @@
# First find the best language to work on
site = self.selectQuerySite()
if site is None:
- wikipedia.output(u"NOTE: Nothing left to do")
+ pywikibot.output(u"NOTE: Nothing left to do")
return False
# Now assemble a reasonable list of pages to get
subjectGroup = []
@@ -1848,7 +1849,7 @@
# We have found enough pages to fill the bandwidth.
break
if len(pageGroup) == 0:
- wikipedia.output(u"NOTE: Nothing left to do 2")
+ pywikibot.output(u"NOTE: Nothing left to do 2")
return False
# Get the content of the assembled list in one blow
gen = pagegenerators.PreloadingGenerator(iter(pageGroup))
@@ -1905,7 +1906,7 @@
modifying = sorted(site for site in oldiw & newiw if old[site] != new[site])
mcomment = u''
- mods = u''
+ mods = u""
if len(adding) + len(removing) + len(modifying) <= 3:
# Use an extended format for the string linking to all added pages.
@@ -1917,7 +1918,7 @@
colon = u': '
comma = u', '
- head, add, rem, mod = wikipedia.translate(insite.lang, msg)
+ head, add, rem, mod = pywikibot.translate(insite.lang, msg)
sep = u''
@@ -1970,10 +1971,10 @@
# to work on.
genFactory = pagegenerators.GeneratorFactory()
- for arg in wikipedia.handleArgs():
+ for arg in pywikibot.handleArgs():
if arg.startswith('-xml'):
if len(arg) == 4:
- xmlFilename = wikipedia.input(u'Please enter the XML dump\'s filename:')
+ xmlFilename = pywikibot.input(u'Please enter the XML dump\'s filename:')
else:
xmlFilename = arg[5:]
hintlessPageGen = XmlDumpLmoLinkPageGenerator(xmlFilename)
@@ -1984,7 +1985,7 @@
elif arg.startswith('-hintfile'):
hintfilename = arg[10:]
if (hintfilename is None) or (hintfilename == ''):
- hintfilename = wikipedia.input(u'Please enter the hint filename:')
+ hintfilename = pywikibot.input(u'Please enter the hint filename:')
f = codecs.open(hintfilename, 'r', config.textfile_encoding)
R = re.compile(ur'\[\[(.+?)(?:\]\]|\|)') # hint or title ends either before | or before ]]
for pageTitle in R.findall(f.read()):
@@ -2084,7 +2085,7 @@
elif arg.startswith('-neverlink:'):
globalvar.neverlink += arg[11:].split(",")
elif arg.startswith('-ignore:'):
- globalvar.ignore += [wikipedia.Page(None,p) for p in arg[8:].split(",")]
+ globalvar.ignore += [pywikibot.Page(None,p) for p in arg[8:].split(",")]
elif arg.startswith('-ignorefile:'):
ignorefile = arg[12:]
ignorePageGen = pagegenerators.TextfilePageGenerator(ignorefile)
@@ -2122,11 +2123,11 @@
# ensure that we don't try to change main page
try:
- site = wikipedia.getSite()
+ site = pywikibot.getSite()
mainpagename = site.mediawiki_message('mainpage')
- globalvar.skip.add(wikipedia.Page(site, mainpagename))
- except wikipedia.Error:
- wikipedia.output(u'Missing main page name')
+ globalvar.skip.add(pywikibot.Page(site, mainpagename))
+ except pywikibot.Error:
+ pywikibot.output(u'Missing main page name')
if newPages is not None:
if len(namespaces) == 0:
@@ -2145,8 +2146,8 @@
hintlessPageGen = pagegenerators.NewpagesPageGenerator(newPages, namespace=ns)
if optRestore or optContinue:
- site = wikipedia.getSite()
- dumpFileName = wikipedia.config.datafilepath(
+ site = pywikibot.getSite()
+ dumpFileName = pywikibot.config.datafilepath(
'interwiki-dumps',
u'interwikidump-%s-%s.txt'
% (site.family.name, site.lang))
@@ -2160,7 +2161,7 @@
nextPage = page.titleWithoutNamespace() + '!'
namespace = page.namespace()
except NameError:
- wikipedia.output(u"Dump file is empty?! Starting at the beginning.")
+ pywikibot.output(u"Dump file is empty?! Starting at the beginning.")
nextPage = "!"
namespace = 0
# old generator is used up, create a new one
@@ -2181,8 +2182,8 @@
else:
singlePageTitle = ' '.join(singlePageTitle)
if not singlePageTitle:
- singlePageTitle = wikipedia.input(u'Which page to check:')
- singlePage = wikipedia.Page(wikipedia.getSite(), singlePageTitle)
+ singlePageTitle = pywikibot.input(u'Which page to check:')
+ singlePage = pywikibot.Page(pywikibot.getSite(), singlePageTitle)
bot.add(singlePage, hints = hints)
try:
@@ -2198,4 +2199,4 @@
StoredPage.SPdeleteStore()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()