Revision: 7722
Author: russblau
Date: 2009-11-30 21:32:22 +0000 (Mon, 30 Nov 2009)
Log Message:
-----------
Make sure all generator calls have an explicit "site" parameter
Modified Paths:
--------------
branches/rewrite/pywikibot/site.py
Modified: branches/rewrite/pywikibot/site.py
===================================================================
--- branches/rewrite/pywikibot/site.py 2009-11-30 19:00:26 UTC (rev 7721)
+++ branches/rewrite/pywikibot/site.py 2009-11-30 21:32:22 UTC (rev 7722)
@@ -810,7 +810,8 @@
def mediawiki_message(self, key):
"""Return the MediaWiki message text for key "key" """
if not key in self._msgcache:
- msg_query = api.QueryGenerator(meta="allmessages", amfilter=key)
+ msg_query = api.QueryGenerator(site=self, meta="allmessages",
+ amfilter=key)
for msg in msg_query:
if msg['name'] == key and not 'missing' in msg:
self._msgcache[key] = msg['*']
@@ -1097,7 +1098,7 @@
props += '|templates'
if langlinks:
props += '|langlinks'
- rvgen = api.PropertyGenerator(props)
+ rvgen = api.PropertyGenerator(props, site=self)
rvgen.set_maximum_items(-1) # suppress use of "rvlimit" parameter
if len(pageids) == len(sublist):
# only use pageids if all pages have them
Revision: 7719
Author: xqt
Date: 2009-11-30 14:51:16 +0000 (Mon, 30 Nov 2009)
Log Message:
-----------
basic.py: actualized from rewrite branch; comments length max 80 chars.
Modified Paths:
--------------
trunk/pywikipedia/basic.py
trunk/pywikipedia/interwiki.py
trunk/pywikipedia/pagegenerators.py
trunk/pywikipedia/redirect.py
Modified: trunk/pywikipedia/basic.py
===================================================================
--- trunk/pywikipedia/basic.py 2009-11-30 11:56:24 UTC (rev 7718)
+++ trunk/pywikipedia/basic.py 2009-11-30 14:51:16 UTC (rev 7719)
@@ -9,14 +9,14 @@
¶ms;
- -debug If given, doesn't do any real changes, but only shows
- what would have been changed.
+-dry If given, doesn't do any real changes, but only shows
+ what would have been changed.
All other parameters will be regarded as part of the title of a single page,
and the bot will only work on that single page.
"""
__version__ = '$Id$'
-import wikipedia
+import wikipedia as pywikibot
import pagegenerators
# This is required for the text that is shown when you run this script
@@ -43,20 +43,20 @@
'zh': u'機器人:編輯.....',
}
- def __init__(self, generator, debug):
+ def __init__(self, generator, dry):
"""
Constructor. Parameters:
* generator - The page generator that determines on which pages
to work on.
- * debug - If True, doesn't do any real changes, but only shows
+ * dry - If True, doesn't do any real changes, but only shows
what would have been changed.
"""
self.generator = generator
- self.debug = debug
+ self.dry = dry
+ # Set the edit summary message
+ self.summary = pywikibot.translate(pywikibot.getSite(), self.msg)
def run(self):
- # Set the edit summary message
- wikipedia.setAction(wikipedia.translate(wikipedia.getSite(), self.msg))
for page in self.generator:
self.treat(page)
@@ -67,11 +67,11 @@
try:
# Load the page
text = page.get()
- except wikipedia.NoPage:
- wikipedia.output(u"Page %s does not exist; skipping." % page.aslink())
+ except pywikibot.NoPage:
+ pywikibot.output(u"Page %s does not exist; skipping." % page.aslink())
return
- except wikipedia.IsRedirectPage:
- wikipedia.output(u"Page %s is a redirect; skipping." % page.aslink())
+ except pywikibot.IsRedirectPage:
+ pywikibot.output(u"Page %s is a redirect; skipping." % page.aslink())
return
################################################################
@@ -86,21 +86,21 @@
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())
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
# show what was changed
- wikipedia.showDiff(page.get(), text)
- if not self.debug:
- choice = wikipedia.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No'], ['y', 'N'], 'N')
+ pywikibot.showDiff(page.get(), text)
+ if not self.dry:
+ choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No'], ['y', 'N'], 'N')
if choice == 'y':
try:
# Save the page
- page.put(text)
- except wikipedia.LockedPage:
- wikipedia.output(u"Page %s is locked; skipping." % page.aslink())
- except wikipedia.EditConflict:
- wikipedia.output(u'Skipping %s because of edit conflict' % (page.title()))
- except wikipedia.SpamfilterError, error:
- wikipedia.output(u'Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
+ page.put(text, comment=self.summary)
+ except pywikibot.LockedPage:
+ pywikibot.output(u"Page %s is locked; skipping." % page.aslink())
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Skipping %s because of edit conflict' % (page.title()))
+ except pywikibot.SpamfilterError, error:
+ pywikibot.output(u'Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
def main():
@@ -113,14 +113,14 @@
# This temporary array is used to read the page title if one single
# page to work on is specified by the arguments.
pageTitleParts = []
- # If debug is True, doesn't do any real changes, but only show
+ # If dry is True, doesn't do any real changes, but only show
# what would have been changed.
- debug = False
+ dry = False
# Parse command line arguments
- for arg in wikipedia.handleArgs():
- if arg.startswith("-debug"):
- debug = True
+ for arg in pywikibot.handleArgs():
+ if arg.startswith("-dry"):
+ dry = True
else:
# check if a standard argument like
# -start:XYZ or -ref:Asdf was given.
@@ -130,7 +130,7 @@
if pageTitleParts != []:
# We will only work on a single page.
pageTitle = ' '.join(pageTitleParts)
- page = wikipedia.Page(wikipedia.getSite(), pageTitle)
+ page = pywikibot.Page(pywikibot.getSite(), pageTitle)
gen = iter([page])
if not gen:
@@ -139,13 +139,13 @@
# The preloading generator is responsible for downloading multiple
# pages from the wiki simultaneously.
gen = pagegenerators.PreloadingGenerator(gen)
- bot = BasicBot(gen, debug)
+ bot = BasicBot(gen, dry)
bot.run()
else:
- wikipedia.showHelp()
+ pywikibot.showHelp()
if __name__ == "__main__":
try:
main()
finally:
- wikipedia.stopme()
+ pywikibot.stopme()
Modified: trunk/pywikipedia/interwiki.py
===================================================================
--- trunk/pywikipedia/interwiki.py 2009-11-30 11:56:24 UTC (rev 7718)
+++ trunk/pywikipedia/interwiki.py 2009-11-30 14:51:16 UTC (rev 7719)
@@ -74,21 +74,22 @@
that amount of pages and then stop. This is only useful in
combination with -start. The default is not to stop.
- -until: used as -until:title, specifies that the robot should process
- pages in wiki default sort order up to, and including, "title"
- and then stop. This is only useful in combination with -start.
- The default is not to stop.
+ -until: used as -until:title, specifies that the robot should
+ process pages in wiki default sort order up to, and
+ including, "title" and then stop. This is only useful in
+ combination with -start. The default is not to stop.
Note: do not specify a namespace, even if -start has one.
- -bracket only work on pages that have (in the home language) parenthesis
- in their title. All other pages are skipped.
+ -bracket only work on pages that have (in the home language)
+ parenthesis in their title. All other pages are skipped.
(note: without ending colon)
-skipfile: used as -skipfile:filename, skip all links mentioned in
the given file. This does not work with -number!
-skipauto use to skip all pages that can be translated automatically,
- like dates, centuries, months, etc. (note: without ending colon)
+ like dates, centuries, months, etc.
+ (note: without ending colon)
-lack: used as -lack:xx with xx a language code: only work on pages
without links to language xx. You can also add a number nn
@@ -102,7 +103,8 @@
useful if you specify a single page to work on. If no
text is given after the second ':', the name of the page
itself is used as the title for the hint, unless the
- -hintnobracket command line option (see there) is also selected.
+ -hintnobracket command line option (see there) is also
+ selected.
There are some special hints, trying a number of languages
at once:
@@ -222,15 +224,15 @@
The following arguments are only important for users who have accounts for
multiple languages, and specify on which sites the bot should modify pages:
- -localonly only work on the local wiki, not on other wikis in the family
- I have a login at. (note: without ending colon)
+ -localonly only work on the local wiki, not on other wikis in the
+ family I have a login at. (note: without ending colon)
-limittwo only update two pages - one in the local wiki (if logged-in)
and one in the top available one.
For example, if the local page has links to de and fr,
this option will make sure that only local and de: (larger)
- site is updated. This option is useful to quickly set two way
- links without updating all of wiki's sites.
+ site is updated. This option is useful to quickly set two
+ way links without updating all of wiki's sites.
(note: without ending colon)
-whenneeded works like limittwo, but other languages are changed in the
Modified: trunk/pywikipedia/pagegenerators.py
===================================================================
--- trunk/pywikipedia/pagegenerators.py 2009-11-30 11:56:24 UTC (rev 7718)
+++ trunk/pywikipedia/pagegenerators.py 2009-11-30 14:51:16 UTC (rev 7719)
@@ -135,9 +135,10 @@
-gorandom Specifies that the robot should starting at the random pages
returned by [[Special:Random]].
--recentchanges Work on new and edited pages returned by [[Special:Recentchanges]].
- Can also be given as "-recentchanges:n" where n is the number
- of pages to be returned, else 100 pages are returned.
+-recentchanges Work on new and edited pages returned by
+ [[Special:Recentchanges]]. Can also be given as
+ "-recentchanges:n" where n is the number of pages to be
+ returned, else 100 pages are returned.
-redirectonly Work on redirect pages only, not their target pages.
The robot goes alphabetically through all redirect pages
Modified: trunk/pywikipedia/redirect.py
===================================================================
--- trunk/pywikipedia/redirect.py 2009-11-30 11:56:24 UTC (rev 7718)
+++ trunk/pywikipedia/redirect.py 2009-11-30 14:51:16 UTC (rev 7719)
@@ -19,18 +19,18 @@
-xml Retrieve information from a local XML dump
(http://download.wikimedia.org). Argument can also be given as
"-xml:filename.xml". Cannot be used with -api or -moves.
- If neither of -xml -api -moves is given, info will be loaded from
- a special page of the live wiki.
+ If neither of -xml -api -moves is given, info will be loaded
+ from a special page of the live wiki.
-api Retrieve information from the wiki via MediaWikis application
program interface (API). Cannot be used with -xml or -moves.
- If neither of -xml -api -moves is given, info will be loaded from
- a special page of the live wiki.
+ If neither of -xml -api -moves is given, info will be loaded
+ from a special page of the live wiki.
-moves Use the page move log to find double-redirect candidates. Only
- works with action "double", does not work with either -xml, or -api.
- If neither of -xml -api -moves is given, info will be loaded from
- a special page of the live wiki.
+ works with action "double", does not work with either -xml, or
+ -api. If neither of -xml -api -moves is given, info will be
+ loaded from a special page of the live wiki.
-namespace:n Namespace to process. Works only with an XML dump, or the API
interface. Can be given multiple times, for several namespaces.
Revision: 7716
Author: siebrand
Date: 2009-11-30 11:44:07 +0000 (Mon, 30 Nov 2009)
Log Message:
-----------
[ #2895284 ] Port of the User() class. Contributed by stanlekub. Superfluous whitespace removed by siebrand. Untested.
Comments from patch submit:
[..] this creates a class User(), which is a subclass of Page, with additional methods.
The patch adds:
* the class User() in page.py
* 2 new exceptions in exceptions.py
* an import statement for User() in __init__.py
* a minimal usrlib.py file is also provided for backwards-compatibility, in the same manner it was done for catlib.
This implementation should be fully compatible with the "old" one... with all of its defaults included. There is for example a strange discrepancy between the format of timestamps as returned by .contributions() and .loadedImages().
The methods .block() and .unblock() aren't implemented yet, and the 'autoblock' thingy should probably be tweeked a little more.
Comments on this are really welcomed [..]
Modified Paths:
--------------
branches/rewrite/pywikibot/__init__.py
branches/rewrite/pywikibot/exceptions.py
branches/rewrite/pywikibot/page.py
Added Paths:
-----------
branches/rewrite/pywikibot/userlib.py
Modified: branches/rewrite/pywikibot/__init__.py
===================================================================
--- branches/rewrite/pywikibot/__init__.py 2009-11-30 11:36:45 UTC (rev 7715)
+++ branches/rewrite/pywikibot/__init__.py 2009-11-30 11:44:07 UTC (rev 7716)
@@ -140,7 +140,7 @@
"""
logger = logging.getLogger("pywiki.wiki")
-
+
if code is None:
code = config.mylang
if fam is None:
@@ -172,7 +172,7 @@
getSite = Site # alias for backwards-compability
-from page import Page, ImagePage, Category, Link
+from page import Page, ImagePage, Category, Link, User
link_regex = re.compile(r'\[\[(?P<title>[^\]|[#<>{}]*)(\|.*?)?\]\]')
@@ -209,7 +209,7 @@
Output a string showing the differences between oldtext and newtext.
The differences are highlighted (only on compatible systems) to show which
changes were made.
-
+
"""
# This is probably not portable to non-terminal interfaces....
# For information on difflib, see http://pydoc.org/2.3/difflib.html
Modified: branches/rewrite/pywikibot/exceptions.py
===================================================================
--- branches/rewrite/pywikibot/exceptions.py 2009-11-30 11:36:45 UTC (rev 7715)
+++ branches/rewrite/pywikibot/exceptions.py 2009-11-30 11:44:07 UTC (rev 7716)
@@ -29,7 +29,7 @@
return self.unicode
class PageRelatedError(Error):
- """Abstract Exception, used when the Exception concerns a particular
+ """Abstract Exception, used when the Exception concerns a particular
Page, and when a generic message can be written once for all"""
# Preformated UNICODE message where the page title will be inserted
# Override this in subclasses.
@@ -68,7 +68,7 @@
class CircularRedirect(Error):
"""Page is a circular redirect
-
+
Exception argument is the redirect target; this may be the same title
as this page or a different title (in which case the target page directly
or indirectly redirects back to this one)
@@ -119,3 +119,11 @@
class UploadWarning(Error):
"""Upload failed with a warning message (passed as the argument)."""
+class AutoblockUser(Error):
+ """
+ The class AutoblockUserError is an exception that is raised whenever
+ an action is requested on a virtual autoblock user that's not available
+ for him (i.e. roughly everything except unblock).
+ """
+class UserActionRefuse(Error):
+ pass
Modified: branches/rewrite/pywikibot/page.py
===================================================================
--- branches/rewrite/pywikibot/page.py 2009-11-30 11:36:45 UTC (rev 7715)
+++ branches/rewrite/pywikibot/page.py 2009-11-30 11:44:07 UTC (rev 7716)
@@ -1753,6 +1753,243 @@
return sorted(list(set(self.categories())))
+class User(Page):
+ """A class that represents a Wiki user.
+ """
+
+ @deprecate_arg("insite", None)
+ def __init__(self, source, title=u''):
+ """All parameters are the same as for Page() constructor.
+ """
+ if len(title) > 1 and title[0] == u'#':
+ self.is_autoblock = True
+ title = title[1:]
+ else:
+ self.is_autoblock = False
+ Page.__init__(self, source, title, ns=2)
+ if self.namespace() != 2:
+ raise ValueError(u"'%s' is not in the user namespace!"
+ % title)
+ if self.is_autoblock:
+ # This user is probably being queried for purpose of lifting
+ # an autoblock.
+ pywikibot.output("This is an autoblock ID, "
+ "you can only use to unblock it.")
+
+ @property
+ def username(self):
+ """ Convenience method that returns the title of the page with
+ namespace prefix omitted, aka the username, as a Unicode string.
+ """
+ if self.is_autoblock:
+ return u'#' + self.title(withNamespace=False)
+ else:
+ return self.title(withNamespace=False)
+
+ def getprops(self, force=False):
+ """ Return a Dictionnary that contains user's properties. Use cached
+ values if already called before, otherwise fetch data from the API.
+
+ @param force: if True, forces reloading the data from API
+ @type force: bool
+ """
+ if force:
+ del self._userprops
+ if not hasattr(self, '_userprops'):
+ usrequest = pywikibot.data.api.Request(
+ site=self.site(),
+ action='query',
+ list='users',
+ usprop='blockinfo|groups|editcount|registration|emailable',
+ ususers=self.username,
+ )
+ usdata = usrequest.submit()
+ assert 'query' in usdata, \
+ "API users response lacks 'query' key"
+ assert 'users' in usdata['query'], \
+ "API users response lacks 'users' key"
+ if u'missing' in usdata['query']['users'][0] or \
+ u'invalid' in usdata['query']['users'][0]:
+ raise pywikibot.Error(u'No such user or invaild username (%s)'\
+ % self.username)
+ self._userprops = usdata['query']['users'][0]
+ return self._userprops
+
+ def registrationTime(self, force=False):
+ """ Return registration time for this user, as a Unicode string in
+ ISO8601 format, or None if the date is unknown.
+
+ @param force: if True, forces reloading the data from API
+ @type force: bool
+ """
+ if 'registration' in self.getprops(force):
+ return self.getprops()['registration']
+
+ def editCount(self, force=False):
+ """ Return edit count for this user as int.
+
+ @param force: if True, forces reloading the data from API
+ @type force: bool
+ """
+ if 'editcount' in self.getprops(force):
+ return self.getprops()['editcount']
+ else:
+ return 0
+
+ def isBlocked(self, force=False):
+ """ Return True if this user is currently blocked, False otherwise.
+
+ @param force: if True, forces reloading the data from API
+ @type force: bool
+ """
+ return 'blockedby' in self.getprops(force)
+
+ def isEmailable(self, force=False):
+ """ Return True if emails can be send to this user through mediawiki,
+ False otherwise.
+
+ @param force: if True, forces reloading the data from API
+ @type force: bool
+ """
+ return 'emailable' in self.getprops(force)
+
+ def groups(self, force=False):
+ """ Return a list of groups to wich this user belongs. The return value
+ is guaranteed to be a list object, possibly empty.
+
+ @param force: if True, forces reloading the data from API
+ @type force: bool
+ """
+ if 'groups' in self.getprops(force):
+ return self.getprops()['groups']
+ else:
+ return []
+
+ def getUserPage(self, subpage=u''):
+ """ Return a pywikibot.Page object corresponding to this user's main
+ page, or a subpage of it if subpage is set.
+
+ @param subpage: subpage part to be appended to the main
+ page title (optional)
+ @type subpage: unicode
+ """
+ if self.is_autoblock:
+ #This user is probably being queried for purpose of lifting
+ #an autoblock, so has no user pages per se.
+ raise AutoblockUser("This is an autoblock ID, you can only use to unblock it.")
+ if subpage:
+ subpage = u'/' + subpage
+ return Page(Link(self.title() + subpage, self.site()))
+
+ def getUserTalkPage(self, subpage=u''):
+ """ Return a pywikibot.Page object corresponding to this user's main
+ talk page, or a subpage of it if subpage is set.
+
+ @param subpage: subpage part to be appended to the main
+ talk page title (optional)
+ @type subpage: unicode
+ """
+ if self.is_autoblock:
+ #This user is probably being queried for purpose of lifting
+ #an autoblock, so has no user talk pages per se.
+ raise AutoblockUser("This is an autoblock ID, you can only use to unblock it.")
+ if subpage:
+ subpage = u'/' + subpage
+ return Page(Link(self.title(withNamespace=False) + subpage,
+ self.site(), defaultNamespace=3))
+
+ def sendMail(self, subject, text, ccme = False):
+ """ Send an email to this user via mediawiki's email interface.
+ Return True on success, False otherwise.
+ This method can raise an UserActionRefuse exception in case this user
+ doesn't allow sending email to him or the currently logged in bot
+ doesn't have the right to send emails.
+
+ @param subject: the subject header of the mail
+ @type subject: unicode
+ @param text: mail body
+ @type text: unicode
+ @param ccme: if True, sends a copy of this email to the bot
+ @type ccme: bool
+ """
+ if not self.isEmailable():
+ raise UserActionRefuse('This user is not mailable')
+
+ if not self.site().has_right('sendemail'):
+ raise UserActionRefuse('You don\'t have permission to send mail')
+
+ params = {
+ 'action': 'emailuser',
+ 'target': self.username,
+ 'token': self.site().token(self, 'email'),
+ 'subject': subject,
+ 'text': text,
+ }
+ if ccme:
+ params['ccme'] = 1
+ mailrequest = pywikibot.data.api.Request(**params)
+ maildata = mailrequest.submit()
+
+ if 'error' in maildata:
+ code = maildata['error']['code']
+ if code == u'usermaildisabled ':
+ pywikibot.output(u'User mail has been disabled')
+ elif 'emailuser' in maildata:
+ if maildata['emailuser']['result'] == u'Success':
+ pywikibot.output(u'Email sent.')
+ return True
+ return False
+
+ @deprecated("contributions")
+ @deprecate_arg("limit", "total") # To be consistent with rest of framework
+ def editedPages(self, total=500):
+ """ Deprecated function that wraps 'contributions' for backwards
+ compatibility. Yields pywikibot.Page objects that this user has
+ edited, with an upper bound of 'total'. Pages returned are not
+ guaranteed to be unique.
+
+ @param total: limit result to this number of pages.
+ @type total: int.
+ """
+ for item in self.contributions(total=total):
+ yield item[0]
+
+ @deprecate_arg("limit", "total") # To be consistent with rest of framework
+ @deprecate_arg("namespace", "namespaces")
+ def contributions(self, total=500, namespaces=[]):
+ """ Yield tuples describing this user edits.
+ Each tuple is composed of a pywikibot.Page object,
+ the revision id (int), the edit timestamp (as int in mediawiki's
+ internal format), and the comment (unicode).
+ Pages returned are not guaranteed to be unique.
+
+ @param total: limit result to this number of pages
+ @type total: int
+ @param namespaces: only iterate links in these namespaces
+ @type namespaces: list
+ """
+ for contrib in self.site().usercontribs(user=self.username,
+ namespaces=namespaces, total=total):
+ ts = pywikibot.Timestamp.fromISOformat(contrib['timestamp'])
+ ts = int(ts.strftime("%Y%m%d%H%M%S"))
+ yield Page(Link(contrib['title'], self.site(),
+ defaultNamespace=contrib['ns'])), \
+ contrib['revid'], ts, contrib['comment']
+
+ @deprecate_arg("number", "total")
+ def uploadedImages(self, total=10):
+ """ Yield tuples describing files uploaded by this user.
+ Each tuple is composed of a pywikibot.Page, the timestamp (str in
+ ISO8601 format), comment (unicode) and a bool (always False...).
+ Pages returned are not guaranteed to be unique.
+
+ @param total: limit result to this number of pages
+ @type total: int
+ """
+ for item in self.site().logevents(logtype='upload', user=self.username,
+ total=total):
+ yield item.title(), str(item.timestamp()), item.comment(), False
+
class Revision(object):
"""A structure holding information about a single revision of a Page."""
def __init__(self, revid, timestamp, user, anon=False, comment=u"",
@@ -2261,4 +2498,3 @@
pass
# Couldn't convert, raise the original exception
raise firstException
-
Added: branches/rewrite/pywikibot/userlib.py
===================================================================
--- branches/rewrite/pywikibot/userlib.py (rev 0)
+++ branches/rewrite/pywikibot/userlib.py 2009-11-30 11:44:07 UTC (rev 7716)
@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+"""
+WARNING: THIS MODULE EXISTS SOLELY TO PROVIDE BACKWARDS-COMPATIBILITY.
+
+Do not use in new scripts; use the source to find the appropriate
+function/method instead.
+
+"""
+#
+# (C) Pywikipedia bot team, 2008
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+
+
+from pywikibot import User
Property changes on: branches/rewrite/pywikibot/userlib.py
___________________________________________________________________
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Revision: 7715
Author: siebrand
Date: 2009-11-30 11:36:45 +0000 (Mon, 30 Nov 2009)
Log Message:
-----------
[ #2894485 ] Basic.py adapted for rewrite branch. Contributed by stanlekub.
Added Paths:
-----------
branches/rewrite/scripts/basic.py
Copied: branches/rewrite/scripts/basic.py (from rev 7713, trunk/pywikipedia/basic.py)
===================================================================
--- branches/rewrite/scripts/basic.py (rev 0)
+++ branches/rewrite/scripts/basic.py 2009-11-30 11:36:45 UTC (rev 7715)
@@ -0,0 +1,153 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+This is not a complete bot; rather, it is a template from which simple
+bots can be made. You can rename it to mybot.py, then edit it in
+whatever way you want.
+
+The following parameters are supported:
+
+¶ms;
+
+-dry If given, doesn't do any real changes, but only shows
+ what would have been changed.
+
+All other parameters will be regarded as part of the title of a single page,
+and the bot will only work on that single page.
+"""
+__version__ = '$Id$'
+import pywikibot
+from pywikibot import pagegenerators
+
+# This is required for the text that is shown when you run this script
+# with the parameter -help.
+docuReplacements = {
+ '¶ms;': pagegenerators.parameterHelp
+}
+
+class BasicBot:
+ # Edit summary message that should be used.
+ # NOTE: Put a good description here, and add translations, if possible!
+ msg = {
+ 'ar': u'روبوت: تغيير ...',
+ 'cs': u'Robot změnil ...',
+ 'de': u'Bot: Ändere ...',
+ 'en': u'Robot: Changing ...',
+ 'fr': u'Robot: Changé ...',
+ 'ja':u'ロボットによる:編集',
+ 'ksh': u'Bot: Ännern ...',
+ 'nds': u'Bot: Änderung ...',
+ 'nl': u'Bot: wijziging ...',
+ 'pt': u'Bot: alterando...',
+ 'sv': u'Bot: Ändrar ...',
+ 'zh': u'機器人:編輯.....',
+ }
+
+ def __init__(self, generator, dry):
+ """
+ Constructor. Parameters:
+ @param generator: The page generator that determines on which pages
+ to work on.
+ @type generator: generator.
+ @param dry: If True, doesn't do any real changes, but only shows
+ what would have been changed.
+ @type dry: boolean.
+ """
+ self.generator = generator
+ self.dry = dry
+ self.summary = pywikibot.translate(pywikibot.getSite(), self.msg)
+
+ def run(self):
+ for page in self.generator:
+ self.treat(page)
+
+ def treat(self, page):
+ """
+ Loads the given page, does some changes, and saves it.
+ """
+ try:
+ # Load the page
+ text = page.get()
+ except pywikibot.NoPage:
+ pywikibot.output(u"Page %s does not exist; skipping." % page.title(asLink=True))
+ return
+ except pywikibot.IsRedirectPage:
+ pywikibot.output(u"Page %s is a redirect; skipping." % page.title(asLink=True))
+ return
+
+ ################################################################
+ # NOTE: Here you can modify the text in whatever way you want. #
+ ################################################################
+
+ # If you find out that you do not want to edit this page, just return.
+ # Example: This puts the text 'Test' at the beginning of the page.
+ text = 'Test ' + text
+
+ # only save if something was changed
+ if text != page.get():
+ # Show the title of the page we're working on.
+ # Highlight the title in purple.
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<" % page.title())
+ # show what was changed
+ pywikibot.showDiff(page.get(), text)
+ if not self.dry:
+ choice = pywikibot.inputChoice(u'Do you want to accept these changes?', ['Yes', 'No'], ['y', 'N'], 'N')
+ if choice == 'y':
+ try:
+ page.text = text
+ # Save the page
+ page.save(comment=self.summary)
+ except pywikibot.LockedPage:
+ pywikibot.output(u"Page %s is locked; skipping." % page.title(asLink=True))
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Skipping %s because of edit conflict' % (page.title()))
+ except pywikibot.SpamfilterError, error:
+ pywikibot.output(u'Cannot change %s because of spam blacklist entry %s' % (page.title(), error.url))
+
+
+def main():
+ # This factory is responsible for processing command line arguments
+ # that are also used by other scripts and that determine on which pages
+ # to work on.
+ genFactory = pagegenerators.GeneratorFactory()
+ # The generator gives the pages that should be worked upon.
+ gen = None
+ # This temporary array is used to read the page title if one single
+ # page to work on is specified by the arguments.
+ pageTitleParts = []
+ # If dry is True, doesn't do any real changes, but only show
+ # what would have been changed.
+ dry = False
+
+ # Parse command line arguments
+ for arg in pywikibot.handleArgs():
+ if arg.startswith("-dry"):
+ dry = True
+ else:
+ # check if a standard argument like
+ # -start:XYZ or -ref:Asdf was given.
+ if not genFactory.handleArg(arg):
+ pageTitleParts.append(arg)
+
+ if pageTitleParts != []:
+ # We will only work on a single page.
+ pageTitle = ' '.join(pageTitleParts)
+ page = pywikibot.Page(pywikibot.Link(pageTitle, pywikibot.getSite()))
+ gen = iter([page])
+
+ if not gen:
+ gen = genFactory.getCombinedGenerator()
+ if gen:
+ # The preloading generator is responsible for downloading multiple
+ # pages from the wiki simultaneously.
+ gen = pagegenerators.PreloadingGenerator(gen)
+ bot = BasicBot(gen, dry)
+ bot.run()
+ else:
+ pywikibot.showHelp()
+
+if __name__ == "__main__":
+ try:
+ main()
+ finally:
+ pywikibot.stopme()
Revision: 7714
Author: siebrand
Date: 2009-11-30 11:31:55 +0000 (Mon, 30 Nov 2009)
Log Message:
-----------
Fix syntax error introduced in r7712
Modified Paths:
--------------
trunk/pywikipedia/interwiki.py
Modified: trunk/pywikipedia/interwiki.py
===================================================================
--- trunk/pywikipedia/interwiki.py 2009-11-30 10:27:21 UTC (rev 7713)
+++ trunk/pywikipedia/interwiki.py 2009-11-30 11:31:55 UTC (rev 7714)
@@ -1934,7 +1934,7 @@
else:
break
# If we have a few, getting the home language is a good thing.
- if not globalvar.restoreAll
+ if not globalvar.restoreAll:
try:
if self.counts[pywikibot.getSite()] > 4:
return pywikibot.getSite()
Revision: 7713
Author: xqt
Date: 2009-11-30 10:27:21 +0000 (Mon, 30 Nov 2009)
Log Message:
-----------
Additional output with verbose option
Modified Paths:
--------------
trunk/pywikipedia/interwiki.py
Modified: trunk/pywikipedia/interwiki.py
===================================================================
--- trunk/pywikipedia/interwiki.py 2009-11-30 10:12:57 UTC (rev 7712)
+++ trunk/pywikipedia/interwiki.py 2009-11-30 10:27:21 UTC (rev 7713)
@@ -1234,7 +1234,7 @@
elif page.site().family == redirectTargetPage.site().family \
and not self.skipPage(page, redirectTargetPage, counter):
if self.addIfNew(redirectTargetPage, counter, page):
- if config.interwiki_shownew:
+ if config.interwiki_shownew or pywikibot.verbose:
pywikibot.output(u"%s: %s gives new redirect %s" % (self.originPage.aslink(), page.aslink(True), redirectTargetPage.aslink(True)))
continue
@@ -1327,7 +1327,7 @@
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:
+ if config.interwiki_shownew or pywikibot.verbose:
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'
@@ -1700,13 +1700,13 @@
# another get-query first.
if bot:
while pywikibot.get_throttle.waittime() + 2.0 < pywikibot.put_throttle.waittime():
- if not globalvar.quiet:
+ if not globalvar.quiet or pywikibot.verbose:
pywikibot.output(u"NOTE: Performing a recursive query first to save time....")
qdone = bot.oneQuery()
if not qdone:
# Nothing more to do
break
- if not globalvar.quiet:
+ if not globalvar.quiet or pywikibot.verbose:
pywikibot.output(u"NOTE: Updating live wiki...")
timeout=60
while 1:
@@ -1842,7 +1842,7 @@
list of subjects becomes too small, but only if there is a
PageGenerator"""
fs = self.firstSubject()
- if fs and not globalvar.quiet:
+ if fs and (not globalvar.quiet or pywikibot.verbose):
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):
@@ -2163,7 +2163,6 @@
ns = 'all'
hintlessPageGen = pagegenerators.NewpagesPageGenerator(newPages, namespace=ns)
-
elif optRestore or optContinue or globalvar.restoreAll:
site = pywikibot.getSite()
if globalvar.restoreAll: