http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11556
Revision: 11556
Author: xqt
Date: 2013-05-19 15:19:58 +0000 (Sun, 19 May 2013)
Log Message:
-----------
remove old python 2.3 stuff, PEP8 changes
Modified Paths:
--------------
trunk/pywikipedia/catlib.py
Modified: trunk/pywikipedia/catlib.py
===================================================================
--- trunk/pywikipedia/catlib.py 2013-05-19 14:41:39 UTC (rev 11555)
+++ trunk/pywikipedia/catlib.py 2013-05-19 15:19:58 UTC (rev 11556)
@@ -9,66 +9,69 @@
# (C) Russell Blau, 2005
# (C) Cyde Weys, 2005-2007
# (C) Leonardo Gregianin, 2005-2007
-# (C) Pywikipedia bot team, 2007-2012
+# (C) Pywikipedia bot team, 2007-2013
#
# Distributed under the terms of the MIT license.
#
__version__ = '$Id$'
#
-import re, time, urllib, query
-import wikipedia
-try:
- set # introduced in Python 2.4: faster and future
-except NameError:
- # fallback solution for Python 2.3
- from sets import Set as set
+import re
+import time
+import urllib
+import wikipedia as pywikibot
+import query
msg_created_for_renaming = {
- 'ar':u'روبوت: نقل من %s. المؤلفون: %s',
- 'de':u'Bot: Verschoben von %s. Autoren: %s',
- 'en':u'Robot: Moved from %s. Authors: %s',
- 'es':u'Bot: Traslado desde %s. Autores: %s',
- 'fa':u'ربات: انتقال از %s. نویسندگان: %s',
- 'fi':u'Botti siirsi luokan %s. Muokkaajat: %s',
- 'fr':u'Robot : déplacé depuis %s. Auteurs: %s',
- 'he':u'בוט: הועבר מהשם %s. כותבים: %s',
- 'hu':u'Bottal áthelyezve innen: %s. Eredeti szerzők: %s',
- 'ia':u'Robot: Transferite de %s. Autores: %s',
- 'id':u'Bot: Memindahkan dari %s. Kontributor: %s',
- 'it':u'Bot: Voce spostata da %s. Autori: %s',
+ 'ar': u'روبوت: نقل من %s. المؤلفون: %s',
+ 'da': u'Bot: flytter fra %s. Bidragsydere: %s',
+ 'de': u'Bot: Verschoben von %s. Autoren: %s',
+ 'en': u'Robot: Moved from %s. Authors: %s',
+ 'es': u'Bot: Traslado desde %s. Autores: %s',
+ 'fa': u'ربات: انتقال از %s. نویسندگان: %s',
+ 'fi': u'Botti siirsi luokan %s. Muokkaajat: %s',
+ 'fr': u'Robot : déplacé depuis %s. Auteurs: %s',
+ 'he': u'בוט: הועבר מהשם %s. כותבים: %s',
+ 'hu': u'Bottal áthelyezve innen: %s. Eredeti szerzők: %s',
+ 'ia': u'Robot: Transferite de %s. Autores: %s',
+ 'id': u'Bot: Memindahkan dari %s. Kontributor: %s',
+ 'it': u'Bot: Voce spostata da %s. Autori: %s',
'ja': u'ロボットによる: %s から移動しました。原作者は %s',
- 'ksh':u'Bot: hääjeholldt von %s. Schriiver: %s',
- 'nds':u'Kat-Bot: herschaven von %s. Schriever: %s',
- 'nl':u'Bot: hernoemd van %s. Auteurs: %s',
- 'nn':u'robot: flytta frå %s. Bidragsytarar: %s',
- 'no':u'Bot: Flytta fra %s. Bidragsytere: %s',
- 'pl':u'Robot przenosi z %s. Autorzy: %s',
- 'pt':u'Bot: Movido de %s. Autor: %s',
- 'zh':u'機器人: 已從 %s 移動。原作者是 %s',
- }
+ 'ksh': u'Bot: hääjeholldt von %s. Schriiver: %s',
+ 'nds': u'Kat-Bot: herschaven von %s. Schriever: %s',
+ 'nl': u'Bot: hernoemd van %s. Auteurs: %s',
+ 'nn': u'robot: flytta frå %s. Bidragsytarar: %s',
+ 'no': u'Bot: Flytta fra %s. Bidragsytere: %s',
+ 'pl': u'Robot przenosi z %s. Autorzy: %s',
+ 'pt': u'Bot: Movido de %s. Autor: %s',
+ 'zh': u'機器人: 已從 %s 移動。原作者是 %s',
+}
# some constants that are used internally
ARTICLE = 0
SUBCATEGORY = 1
+
def isCatTitle(title, site):
- return ':' in title and title[:title.index(':')] in site.category_namespaces()
+ return ':' in title and title[:title.index(':')
+ ] in site.category_namespaces()
+
def unique(l):
- """Given a list of hashable object, return an alphabetized unique list.
- """
- l=dict.fromkeys(l).keys()
+ """Given a list of hashable object, return an alphabetized unique list."""
+ l = dict.fromkeys(l).keys()
l.sort()
return l
-class Category(wikipedia.Page):
+class Category(pywikibot.Page):
"""Subclass of Page that has some special tricks that only work for
- category: pages"""
+ category: pages
+ """
+
def __init__(self, site, title=None, insite=None, sortKey=None,
sortKeyPrefix=None):
- wikipedia.Page.__init__(self, site=site, title=title, insite=insite,
+ pywikibot.Page.__init__(self, site=site, title=title, insite=insite,
defaultNamespace=14)
self.sortKey = sortKey
self.sortKeyPrefix = sortKeyPrefix
@@ -92,8 +95,8 @@
else:
titleWithSortKey = self.title(savetitle=True)
if not noInterwiki and (forceInterwiki
- or self.site() != wikipedia.getSite()):
- if self.site().family != wikipedia.getSite().family \
+ or self.site() != pywikibot.getSite()):
+ if self.site().family != pywikibot.getSite().family \
and self.site().family.name != self.site().lang:
return '[[%s:%s:%s]]' % (self.site().family.name,
self.site().lang,
@@ -109,8 +112,7 @@
def _getAndCacheContents(self, recurse=False, purge=False, startFrom=None,
cache=None, sortby=None, sortdir=None,
endsort=None):
- """
- Cache results of _parseCategory for a second call.
+ """Cache results of _parseCategory for a second call.
If recurse is a bool, and value is True, then recursively retrieves
contents of all subcategories without limit. If recurse is an int,
@@ -121,6 +123,7 @@
will be cached.
This should not be used outside of this module.
+
"""
if cache is None:
cache = []
@@ -174,9 +177,9 @@
def _getContentsNaive(self, recurse=False, startFrom=None, sortby=None,
sortdir=None, endsort=None):
- """
- Simple category content yielder. Naive, do not attempts to
+ """Simple category content yielder. Naive, do not attempts to
cache anything
+
"""
for tag, page in self._parseCategory(startFrom=startFrom,
sortby=sortby, sortdir=sortdir,
@@ -197,12 +200,13 @@
Set startFrom to a string which is the title of the page to start from.
Yielded results are tuples in the form (tag, page) where tag is one
- of the constants ARTICLE and SUBCATEGORY, and title is the Page or Category
- object.
+ of the constants ARTICLE and SUBCATEGORY, and title is the Page or
+ Category object.
Note that results of this method need not be unique.
This should not be used outside of this module.
+
"""
if not self.site().has_api() or self.site().versionnumber() < 11:
for tag, page in self._oldParseCategory(purge, startFrom):
@@ -224,14 +228,14 @@
if sortdir:
params['cmdir'] = sortdir
while True:
- if wikipedia.config.special_page_limit > 500:
+ if pywikibot.config.special_page_limit > 500:
params['cmlimit'] = 500
else:
- params['cmlimit'] = wikipedia.config.special_page_limit
+ params['cmlimit'] = pywikibot.config.special_page_limit
if currentPageOffset:
params.update(currentPageOffset)
- wikipedia.output('Getting [[%s]] list from %s...'
+ pywikibot.output('Getting [[%s]] list from %s...'
% (self.title(),
"%s=%s" % currentPageOffset.popitem()))
else:
@@ -245,9 +249,9 @@
endsort = endsort.upper()
params['cmendsortkey'] = endsort
msg += ' ending at %s' % endsort
- wikipedia.output(msg + u'...')
+ pywikibot.output(msg + u'...')
- wikipedia.get_throttle()
+ pywikibot.get_throttle()
data = query.GetData(params, self.site())
if 'error' in data:
raise RuntimeError("%s" % data['error'])
@@ -261,15 +265,14 @@
sortKeyPrefix = memb['sortkeyprefix']
else:
sortKeyPrefix = None
- yield SUBCATEGORY, \
- Category(self.site(), memb['title'],
- sortKey=memb['sortkey'],
- sortKeyPrefix=sortKeyPrefix)
+ yield SUBCATEGORY, Category(self.site(), memb['title'],
+ sortKey=memb['sortkey'],
+ sortKeyPrefix=sortKeyPrefix)
elif memb['ns'] == 6:
- yield ARTICLE, wikipedia.ImagePage(self.site(),
+ yield ARTICLE, pywikibot.ImagePage(self.site(),
memb['title'])
else:
- yield ARTICLE, wikipedia.Page(self.site(), memb['title'],
+ yield ARTICLE, pywikibot.Page(self.site(), memb['title'],
defaultNamespace=memb['ns'])
if count >= params['cmlimit']:
break
@@ -280,20 +283,20 @@
break
def _oldParseCategory(self, purge=False, startFrom=None):
- """
- Yields all articles and subcategories that are in this category.
+ """Yields all articles and subcategories that are in this category.
Set purge to True to instruct MediaWiki not to serve a cached version.
Set startFrom to a string which is the title of the page to start from.
Yielded results are tuples in the form (tag, page) where tag is one
- of the constants ARTICLE and SUBCATEGORY, and title is the Page or Category
- object.
+ of the constants ARTICLE and SUBCATEGORY, and title is the Page or
+ Category object.
Note that results of this method need not be unique.
This should not be used outside of this module.
+
"""
if self.site().versionnumber() < 4:
Rtitle = re.compile('title\s?=\s?\"([^\"]*)\"')
@@ -302,7 +305,8 @@
Rtitle = re.compile('/\S*(?: title\s?=\s?)?\"([^\"]*)\"')
else:
Rtitle = re.compile(
- '<li>(?:<span.*?>)?<a href=\".*?\"\s?title\s?=\s?\"([^\"]*)\"\>\+?[^\<\+]')
+ '<li>(?:<span.*?>)?<a href=\".*?\"\s?title\s?=\s?\"'
+ '([^\"]*)\"\>\+?[^\<\+]')
if self.site().versionnumber() < 8:
Rsubcat = None
Rimage = None
@@ -310,13 +314,16 @@
Rsubcat = re.compile(
'CategoryTreeLabelCategory\"\s?href=\".+?\">(.+?)</a>')
Rimage = re.compile(
- '<div class\s?=\s?\"thumb\"\sstyle=\"[^\"]*\">(?:<div style=\"[^\"]*\">)?<a href=\".*?\"(?:\sclass="image")?\stitle\s?=\s?\"([^\"]*)\"')
+ '<div class\s?=\s?\"thumb\"\sstyle=\"[^\"]*\">'
+ '(?:<div style=\"[^\"]*\">)?<a href=\".*?\"'
+ '(?:\sclass="image")?\stitle\s?=\s?\"([^\"]*)\"')
ns = self.site().category_namespaces()
# regular expression matching the "(next 200)" link
- RLinkToNextPage = re.compile('&from=(.*?)" title="');
+ RLinkToNextPage = re.compile('&from=(.*?)" title="')
if startFrom:
- currentPageOffset = urllib.quote(startFrom.encode(self.site().encoding()))
+ currentPageOffset = urllib.quote(
+ startFrom.encode(self.site().encoding()))
else:
currentPageOffset = None
while True:
@@ -325,11 +332,14 @@
path += '&action=purge'
if currentPageOffset:
path += '&from=' + currentPageOffset
- wikipedia.output('Getting [[%s]] starting at %s...'
- % (self.title(), wikipedia.url2link(currentPageOffset, self.site(), self.site())))
+ pywikibot.output('Getting [[%s]] starting at %s...'
+ % (self.title(),
+ pywikibot.url2link(currentPageOffset,
+ self.site(),
+ self.site())))
else:
- wikipedia.output('Getting [[%s]]...' % self.title())
- wikipedia.get_throttle()
+ pywikibot.output('Getting [[%s]]...' % self.title())
+ pywikibot.get_throttle()
txt = self.site().getUrl(path)
# index where subcategory listing begins
if self.site().versionnumber() >= 9:
@@ -344,9 +354,11 @@
# No pages
return
else:
- ibegin = txt.index('<!-- start content -->') # does not work for cats without text
+ # does not work for cats without text
+ ibegin = txt.index('<!-- start content -->')
# TODO: This parses category text and may think they are
# pages in category! Check for versions before 1.9
+
# index where article listing ends
if '<div class="printfooter">' in txt:
iend = txt.index('<div class="printfooter">')
@@ -364,7 +376,7 @@
elif isCatTitle(title, self.site()):
yield SUBCATEGORY, Category(self.site(), title)
else:
- yield ARTICLE, wikipedia.Page(self.site(), title)
+ yield ARTICLE, pywikibot.Page(self.site(), title)
if Rsubcat:
# For MediaWiki versions where subcats look differently
for titleWithoutNamespace in Rsubcat.findall(txt):
@@ -373,10 +385,11 @@
if Rimage:
# For MediaWiki versions where images work through galleries
for title in Rimage.findall(txt):
- # In some MediaWiki versions, the titles contain the namespace,
- # but they don't in other (newer) versions. Use the ImagePage's
- # defaultNamespace feature to get everything correctly.
- yield ARTICLE, wikipedia.ImagePage(self.site(), title)
+ # In some MediaWiki versions, the titles contain the
+ # namespace, but they don't in other (newer) versions. Use
+ # the ImagePage's defaultNamespace feature to get everything
+ # correctly.
+ yield ARTICLE, pywikibot.ImagePage(self.site(), title)
# try to find a link to the next list page
matchObj = RLinkToNextPage.search(txt)
if matchObj:
@@ -385,9 +398,8 @@
break
def subcategories(self, recurse=False, startFrom=None, cacheResults=False,
- sortby=None, sortdir=None):
- """
- Yields all subcategories of the current category.
+ sortby=None, sortdir=None):
+ """Yields all subcategories of the current category.
If recurse is True, also yields subcategories of the subcategories.
If recurse is a number, also yields subcategories of subcategories,
@@ -401,24 +413,25 @@
categories
Results a sorted (as sorted by MediaWiki), but need not be unique.
+
"""
if cacheResults:
gen = self._getAndCacheContents
else:
gen = self._getContentsNaive
- for tag, subcat in gen(recurse=recurse, startFrom=startFrom, sortby=sortby,
- sortdir=sortdir):
+ for tag, subcat in gen(recurse=recurse, startFrom=startFrom,
+ sortby=sortby, sortdir=sortdir):
if tag == SUBCATEGORY:
yield subcat
def subcategoriesList(self, recurse=False, sortby=None, sortdir=None):
- """
- Creates a list of all subcategories of the current category.
+ """Creates a list of all subcategories of the current category.
If recurse is True, also return subcategories of the subcategories.
Recurse can also be a number, as explained above.
The elements of the returned list are sorted and unique.
+
"""
subcats = []
for cat in self.subcategories(recurse, sortby=sortby, sortdir=sortdir):
@@ -426,9 +439,8 @@
return unique(subcats)
def articles(self, recurse=False, startFrom=None, cacheResults=False,
- sortby=None, sortdir=None, endsort=None):
- """
- Yields all articles of the current category.
+ sortby=None, sortdir=None, endsort=None):
+ """Yields all articles of the current category.
If recurse is True, also yields articles of the subcategories.
Recurse can be a number to restrict the depth at which subcategories
@@ -441,6 +453,7 @@
Results are unsorted (except as sorted by MediaWiki), and need not
be unique.
+
"""
if cacheResults:
gen = self._getAndCacheContents
@@ -452,14 +465,14 @@
yield page
def articlesList(self, recurse=False, sortby=None, sortdir=None):
- """
- Creates a list of all articles of the current category.
+ """Creates a list of all articles of the current category.
If recurse is True, also return articles of the subcategories.
Recurse can be a number to restrict the depth at which subcategories
are included.
The elements of the returned list are sorted and unique.
+
"""
articles = []
for article in self.articles(recurse, sortby=sortby, sortdir=sortdir):
@@ -467,20 +480,20 @@
return unique(articles)
def supercategories(self):
- """
- Yields all supercategories of the current category.
+ """Yields all supercategories of the current category.
Results are stored in the order in which they were entered, and need
not be unique.
+
"""
for supercat in self.categories():
yield supercat
def supercategoriesList(self):
- """
- Creates a list of all supercategories of the current category.
+ """Creates a list of all supercategories of the current category.
The elements of the returned list are sorted and unique.
+
"""
supercats = []
for cat in self.supercategories():
@@ -490,149 +503,161 @@
def isEmptyCategory(self):
"""Return True if category has no members (including subcategories)."""
for tag, title in self._parseCategory():
- return False
+ return
return True
def isHiddenCategory(self):
"""Return True if the category is hidden."""
text = self.get()
hidden = re.search('__HIDDENCAT__', text)
-
if hidden:
return True
- else:
- return False
def copyTo(self, catname):
- """
- Returns true if copying was successful, false if target page already
+ """Returns true if copying was successful, false if target page already
existed.
+
"""
catname = self.site().category_namespace() + ':' + catname
- targetCat = wikipedia.Page(self.site(), catname)
+ targetCat = pywikibot.Page(self.site(), catname)
if targetCat.exists():
- wikipedia.output('Target page %s already exists!' % targetCat.title())
- return False
+ pywikibot.output('Target page %s already exists!'
+ % targetCat.title())
+ return
else:
- wikipedia.output('Moving text from %s to %s.' % (self.title(), targetCat.title()))
+ pywikibot.output('Moving text from %s to %s.'
+ % (self.title(), targetCat.title()))
authors = ', '.join(self.contributingUsers())
- creationSummary = wikipedia.translate(wikipedia.getSite(), msg_created_for_renaming) % (self.title(), authors)
- #Maybe sometimes length of summary is more than 200 characters and thus will not be shown.
- #For avoidning copyright violation bot must listify authors in another place
- if len(creationSummary)>200:
- talkpage=targetCat.toggleTalkPage()
+ creationSummary = pywikibot.translate(pywikibot.getSite(),
+ msg_created_for_renaming) \
+ % (self.title(), authors)
+ # Maybe sometimes length of summary is more than 200 characters and
+ # thus will not be shown. For avoidning copyright violation bot must
+ # listify authors in another place
+ if len(creationSummary) > 200:
+ talkpage = targetCat.toggleTalkPage()
try:
- talktext=talkpage.get()
- except wikipedia.NoPage:
- talkpage.put(u"==Authors==\n%s-~~~~" % authors, u"Bot:Listifying authors")
+ talktext = talkpage.get()
+ except pywikibot.NoPage:
+ talkpage.put(u"==Authors==\n%s-~~~~" % authors,
+ u"Bot:Listifying authors")
else:
- talkpage.put(talktext+u"\n==Authors==\n%s-~~~~" % authors, u"Bot:Listifying authors")
+ talkpage.put(talktext+u"\n==Authors==\n%s-~~~~" % authors,
+ u"Bot:Listifying authors")
targetCat.put(self.get(), creationSummary)
return True
- #Like copyTo above, except this removes a list of templates (like deletion templates) that appear in
- #the old category text. It also removes all text between the two HTML comments BEGIN CFD TEMPLATE
- #and END CFD TEMPLATE. (This is to deal with CFD templates that are substituted.)
+ # Like copyTo above, except this removes a list of templates (like deletion
+ # templates) that appear in the old category text. It also removes all text
+ # between the two HTML comments BEGIN CFD TEMPLATE and END CFD TEMPLATE.
+ # (This is to deal with CFD templates that are substituted.)
def copyAndKeep(self, catname, cfdTemplates):
- """
- Returns true if copying was successful, false if target page already
+ """Returns true if copying was successful, false if target page already
existed.
+
"""
- targetCat = wikipedia.Page(self.site(), catname, defaultNamespace=14)
+ targetCat = pywikibot.Page(self.site(), catname, defaultNamespace=14)
if targetCat.exists():
- wikipedia.output('Target page %s already exists!' % targetCat.title())
- return False
+ pywikibot.output('Target page %s already exists!'
+ % targetCat.title())
+ return
else:
- wikipedia.output('Moving text from %s to %s.' % (self.title(), targetCat.title()))
+ pywikibot.output('Moving text from %s to %s.'
+ % (self.title(), targetCat.title()))
authors = ', '.join(self.contributingUsers())
- creationSummary = wikipedia.translate(wikipedia.getSite(), msg_created_for_renaming) % (self.title(), authors)
+ creationSummary = pywikibot.translate(pywikibot.getSite(),
+ msg_created_for_renaming) \
+ % (self.title(), authors)
newtext = remove_cfd_templates(cfdTemplates, self.get())
targetCat.put(newtext, creationSummary)
return True
+
def remove_cfd_templates(cfdTemplates, pageText):
for regexName in cfdTemplates:
matchcfd = re.compile(r"{{%s.*?}}" % regexName, re.IGNORECASE)
- pageText = matchcfd.sub('',pageText)
- matchcomment = re.compile(r"<!--BEGIN CFD TEMPLATE-->.*<!--END CFD TEMPLATE-->", re.IGNORECASE | re.MULTILINE | re.DOTALL)
- pageText = matchcomment.sub('',pageText)
+ pageText = matchcfd.sub('', pageText)
+ matchcomment = re.compile(
+ r"<!--BEGIN CFD TEMPLATE-->.*<!--END CFD TEMPLATE-->",
+ re.IGNORECASE | re.MULTILINE | re.DOTALL)
+ pageText = matchcomment.sub('', pageText)
pos = 0
while (pageText[pos:pos+1] == "\n"):
pos = pos + 1
pageText = pageText[pos:]
return pageText
+
def add_category(article, category, comment=None, createEmptyPages=False):
- """
- Given an article and a category, adds the article to the category.
- """
+ """Given an article and a category, adds the article to the category."""
cats = article.categories(get_redirect=True)
if not category in cats:
cats.append(category)
try:
text = article.get()
- except wikipedia.NoPage:
+ except pywikibot.NoPage:
if createEmptyPages:
text = ''
else:
raise
- text = wikipedia.replaceCategoryLinks(text, cats)
+ text = pywikibot.replaceCategoryLinks(text, cats)
try:
article.put(text, comment=comment)
- except wikipedia.EditConflict:
- wikipedia.output(u'Skipping %s because of edit conflict' % article.title())
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Skipping %s because of edit conflict'
+ % article.title())
#def Category(code, name):
# """Factory method to create category link objects from the category name"""
# # Standardized namespace
-# ns = wikipedia.getSite().category_namespaces()[0]
+# ns = pywikibot.getSite().category_namespaces()[0]
# # Prepend it
# return Category(code, "%s:%s" % (ns, name))
def change_category(article, oldCat, newCat, comment=None, sortKey=None,
inPlace=False):
- """
- Given an article which is in category oldCat, moves it to
+ """Given an article which is in category oldCat, moves it to
category newCat. Moves subcategories of oldCat as well.
oldCat and newCat should be Category objects.
If newCat is None, the category will be removed.
+
"""
cats = article.categories(get_redirect=True)
site = article.site()
changesMade = False
if not article.canBeEdited():
- wikipedia.output("Can't edit %s, skipping it..."
+ pywikibot.output("Can't edit %s, skipping it..."
% article.title(asLink=True))
- return False
+ return
if inPlace or article.namespace() == 10:
oldtext = article.get(get_redirect=True)
- newtext = wikipedia.replaceCategoryInPlace(oldtext, oldCat, newCat)
+ newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat)
if newtext == oldtext:
- wikipedia.output(
+ pywikibot.output(
u'No changes in made in page %s.' % article.title(asLink=True))
- return False
+ return
try:
article.put(newtext, comment)
return True
- except wikipedia.EditConflict:
- wikipedia.output(u'Skipping %s because of edit conflict'
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Skipping %s because of edit conflict'
% article.title(asLink=True))
- except wikipedia.LockedPage:
- wikipedia.output(u'Skipping locked page %s'
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Skipping locked page %s'
% article.title(asLink=True))
- except wikipedia.SpamfilterError, error:
- wikipedia.output(u'Changing page %s blocked by spam filter (URL=%s)'
+ except pywikibot.SpamfilterError, error:
+ pywikibot.output(u'Changing page %s blocked by spam filter (URL=%s)'
% (article.title(asLink=True), error.url))
- except wikipedia.NoUsername:
- wikipedia.output(u'Page %s not saved; sysop privileges required.'
+ except pywikibot.NoUsername:
+ pywikibot.output(u'Page %s not saved; sysop privileges required.'
% article.title(asLink=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.title(asLink=True), error.message))
- return False
+ return
# This loop will replace all occurrences of the category to be changed,
# and remove duplicates.
@@ -655,46 +680,48 @@
newCatList.append(cat)
if not changesMade:
- wikipedia.error(u'%s is not in category %s!'
+ pywikibot.error(u'%s is not in category %s!'
% (article.title(asLink=True), oldCat.title()))
else:
text = article.get(get_redirect=True)
try:
- text = wikipedia.replaceCategoryLinks(text, newCatList)
+ text = pywikibot.replaceCategoryLinks(text, newCatList)
except ValueError:
# Make sure that the only way replaceCategoryLinks() can return
# a ValueError is in the case of interwiki links to self.
- wikipedia.output(
- u'Skipping %s because of interwiki link to self' % article)
+ pywikibot.output(u'Skipping %s because of interwiki link to self'
+ % article)
try:
article.put(text, comment)
- except wikipedia.EditConflict:
- wikipedia.output(
- u'Skipping %s because of edit conflict' % article.title())
- except wikipedia.SpamfilterError, e:
- wikipedia.output(
- u'Skipping %s because of blacklist entry %s'
- % (article.title(), e.url))
- except wikipedia.LockedPage:
- wikipedia.output(
- u'Skipping %s because page is locked' % article.title())
- except wikipedia.PageNotSaved, error:
- wikipedia.output(u"Saving page %s failed: %s"
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Skipping %s because of edit conflict'
+ % article.title())
+ except pywikibot.SpamfilterError, e:
+ pywikibot.output(u'Skipping %s because of blacklist entry %s'
+ % (article.title(), e.url))
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Skipping %s because page is locked'
+ % article.title())
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u"Saving page %s failed: %s"
% (article.title(asLink=True), error.message))
-def categoryAllElementsAPI(CatName, cmlimit = 5000, categories_parsed = [], site = None):
- #action=query&list=categorymembers&cmlimit=500&cmtitle=Category:License_tags
+
+def categoryAllElementsAPI(CatName, cmlimit=5000, categories_parsed=[],
+ site=None):
+ # action=query&list=categorymembers&cmlimit=500&cmtitle=Category:License_tags
+ """ Category to load all the elements in a category using the APIs.
+ Limit: 5000 elements.
+
"""
- Category to load all the elements in a category using the APIs. Limit: 5000 elements.
- """
- wikipedia.output("Loading %s..." % CatName)
+ pywikibot.output("Loading %s..." % CatName)
params = {
- 'action' :'query',
- 'list' :'categorymembers',
- 'cmlimit' :cmlimit,
- 'cmtitle' :CatName,
- }
+ 'action': 'query',
+ 'list': 'categorymembers',
+ 'cmlimit': cmlimit,
+ 'cmtitle': CatName,
+ }
data = query.GetData(params, site)
categories_parsed.append(CatName)
@@ -702,12 +729,15 @@
members = data['query']['categorymembers']
except KeyError:
if int(cmlimit) != 500:
- wikipedia.output(u'An Error occured, trying to reload the category.')
- return categoryAllElementsAPI(CatName, cmlimit = 500)
+ pywikibot.output(
+ u'An Error occured, trying to reload the category.')
+ return categoryAllElementsAPI(CatName, cmlimit=500)
else:
- raise wikipedia.Error(data)
+ raise pywikibot.Error(data)
if len(members) == int(cmlimit):
- raise wikipedia.Error(u'The category selected has >= %s elements, limit reached.' % cmlimit)
+ raise pywikibot.Error(
+ u'The category selected has >= %s elements, limit reached.'
+ % cmlimit)
allmembers = members
results = list()
for subcat in members:
@@ -717,7 +747,9 @@
if ns == 14:
if title not in categories_parsed:
categories_parsed.append(title)
- (results_part, categories_parsed) = categoryAllElementsAPI(title, 5000, categories_parsed)
+ (results_part,
+ categories_parsed) = categoryAllElementsAPI(title, 5000,
+ categories_parsed)
allmembers.extend(results_part)
for member in allmembers:
ns = member['ns']
@@ -726,31 +758,31 @@
results.append(member)
return (results, categories_parsed)
-def categoryAllPageObjectsAPI(CatName, cmlimit = 5000, categories_parsed = [], site = None):
- """
- From a list of dictionaries, return a list of page objects.
- """
+
+def categoryAllPageObjectsAPI(CatName, cmlimit=5000, categories_parsed=[],
+ site=None):
+ """From a list of dictionaries, return a list of page objects."""
final = list()
- if site == None:
- site = wikipedia.getSite()
- for element in categoryAllElementsAPI(CatName, cmlimit, categories_parsed, site)[0]:
- final.append(wikipedia.Page(site, element['title']))
+ if site is None:
+ site = pywikibot.getSite()
+ for element in categoryAllElementsAPI(CatName, cmlimit, categories_parsed,
+ site)[0]:
+ final.append(pywikibot.Page(site, element['title']))
return final
+
def test():
- site = wikipedia.getSite()
-
+ site = pywikibot.getSite()
cat = Category(site, 'Category:Software')
-
- wikipedia.output(u'SUBCATEGORIES:')
+ pywikibot.output(u'SUBCATEGORIES:')
for subcat in cat.subcategories():
- wikipedia.output(subcat.title())
- wikipedia.output(u'\nARTICLES:')
+ pywikibot.output(subcat.title())
+ pywikibot.output(u'\nARTICLES:')
for article in cat.articles():
- wikipedia.output(article.title())
+ pywikibot.output(article.title())
-if __name__=="__main__":
+if __name__ == "__main__":
import sys
for arg in sys.argv[1:]:
- wikipedia.output(u'Ignored argument: %s' % arg)
+ pywikibot.output(u'Ignored argument: %s' % arg)
test()