Revision: 8057
Author: xqt
Date: 2010-04-06 09:02:45 +0000 (Tue, 06 Apr 2010)
Log Message:
-----------
ption to automatically change pages. Not to wait for operators reaction (request #2981327)
Modified Paths:
--------------
trunk/pywikipedia/selflink.py
Modified: trunk/pywikipedia/selflink.py
===================================================================
--- trunk/pywikipedia/selflink.py 2010-04-06 07:55:06 UTC (rev 8056)
+++ trunk/pywikipedia/selflink.py 2010-04-06 09:02:45 UTC (rev 8057)
@@ -13,6 +13,9 @@
or pages-meta-current, see http://download.wikimedia.org).
Argument can also be given as "-xml:filename".
+ -always Unlink always but don't prompt you for each replacement.
+ ATTENTION: Use this with care!
+
-namespace:n Number of namespace to process. The parameter can be used
multiple times. It works in combination with all other
parameters, except for the -start parameter. If you e.g.
@@ -90,7 +93,7 @@
class SelflinkBot:
- def __init__(self, generator):
+ def __init__(self, generator, always=False):
self.generator = generator
linktrail = wikipedia.getSite().linktrail()
# The regular expression which finds links. Results consist of four groups:
@@ -100,6 +103,8 @@
# group linktrail is the link trail, that's letters after ]] which are part of the word.
# note that the definition of 'letter' varies from language to language.
self.linkR = re.compile(r'\[\[(?P<title>[^\]\|#]*)(?P<section>#[^\]\|]*)?(\|(?P<label>[^\]]*))?\]\](?P<linktrail>' + linktrail + ')')
+ self.always = always
+ self.done = False
def handleNextLink(self, page, text, match, context = 100):
"""
@@ -128,32 +133,46 @@
else:
# 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, match.start() - context) : match.start()] + '\03{lightred}' + text[match.start() : match.end()] + '\03{default}' + text[match.end() : match.end() + context])
- choice = wikipedia.inputChoice(u'\nWhat shall be done with this selflink?', ['unlink', 'make bold', 'skip', 'edit', 'more context'], ['U', 'b', 's', 'e', 'm'], 'u')
- wikipedia.output(u'')
+ if self.always:
+ choice = 'a'
+ else:
+ wikipedia.output(
+ text[max(0, match.start() - context) : match.start()] \
+ + '\03{lightred}' + text[match.start() : match.end()] \
+ + '\03{default}' + text[match.end() : match.end() + context])
+ choice = wikipedia.inputChoice(
+ u'\nWhat shall be done with this selflink?\n',
+ ['unlink', 'make bold', 'skip', 'edit', 'more context',
+ 'unlink all', 'quit'],
+ ['U', 'b', 's', 'e', 'm', 'a', 'q'], 'u')
+ wikipedia.output(u'')
- if choice == 's':
- # skip this link
- return text, False
- elif choice == 'e':
- editor = editarticle.TextEditor()
- newText = editor.edit(text, jumpIndex = match.start())
- # if user didn't press Cancel
- if newText:
- return newText, True
- else:
- return text, True
- elif choice == 'm':
- # show more context by recursive self-call
- return self.handleNextLink(page, text, match, context = context + 100)
+ if choice == 's':
+ # skip this link
+ return text, False
+ elif choice == 'e':
+ editor = editarticle.TextEditor()
+ newText = editor.edit(text, jumpIndex = match.start())
+ # if user didn't press Cancel
+ if newText:
+ return newText, True
+ else:
+ return text, True
+ elif choice == 'm':
+ # show more context by recursive self-call
+ return self.handleNextLink(page, text, match, context = context + 100)
+ elif choice == 'a':
+ self.always = True
+ elif choice == 'q':
+ self.done = True
+ return text, False
+ new = match.group('label') or match.group('title')
+ new += match.group('linktrail')
+ if choice == 'b':
+ # make bold
+ return text[:match.start()] + "'''" + new + "'''" + text[match.end():], False
else:
- new = match.group('label') or match.group('title')
- new += match.group('linktrail')
- if choice == 'u':
- return text[:match.start()] + new + text[match.end():], False
- else:
- # make bold
- return text[:match.start()] + "'''" + new + "'''" + text[match.end():], False
+ return text[:match.start()] + new + text[match.end():], False
def treat(self, page):
# Show the title of the page we're working on.
@@ -196,6 +215,7 @@
wikipedia.setAction(comment)
for page in self.generator:
+ if self.done: break
self.treat(page)
def main():
@@ -211,6 +231,7 @@
# that are also used by other scripts and that determine on which pages
# to work on.
genFactory = pagegenerators.GeneratorFactory()
+ always = False
for arg in wikipedia.handleArgs():
if arg.startswith('-xml'):
@@ -236,6 +257,8 @@
namespaces.append(int(arg[11:]))
except ValueError:
namespaces.append(arg[11:])
+ elif arg == '-always':
+ always = True
else:
if not genFactory.handleArg(arg):
pageTitle.append(arg)
@@ -251,7 +274,7 @@
if namespaces != []:
gen = pagegenerators.NamespaceFilterPageGenerator(gen, namespaces)
preloadingGen = pagegenerators.PreloadingGenerator(gen)
- bot = SelflinkBot(preloadingGen)
+ bot = SelflinkBot(preloadingGen, always)
bot.run()
if __name__ == "__main__":
Revision: 8056
Author: xqt
Date: 2010-04-06 07:55:06 +0000 (Tue, 06 Apr 2010)
Log Message:
-----------
Ignore invalid title error (bugfix #2981290)
Modified Paths:
--------------
trunk/pywikipedia/selflink.py
Modified: trunk/pywikipedia/selflink.py
===================================================================
--- trunk/pywikipedia/selflink.py 2010-04-06 07:35:40 UTC (rev 8055)
+++ trunk/pywikipedia/selflink.py 2010-04-06 07:55:06 UTC (rev 8056)
@@ -109,11 +109,18 @@
should be reset to 0. This is required after the user has edited the
article.
"""
- # ignore interwiki links and links to sections of the same page as well as section links
- if not match.group('title') or page.site().isInterwikiLink(match.group('title')) or match.group('section'):
+ # ignore interwiki links and links to sections of the same page as well
+ # as section links
+ if not match.group('title') \
+ or page.site().isInterwikiLink(match.group('title')) \
+ or match.group('section'):
return text, False
-
- linkedPage = wikipedia.Page(page.site(), match.group('title'))
+ try:
+ linkedPage = wikipedia.Page(page.site(), match.group('title'))
+ except wikipedia.InvalidTitle, err:
+ wikipedia.output(u'Warning: %s' % err)
+ return text, False
+
# Check whether the link found is to the current page itself.
if linkedPage != page:
# not a self-link
Revision: 8055
Author: xqt
Date: 2010-04-06 07:35:40 +0000 (Tue, 06 Apr 2010)
Log Message:
-----------
change bot status page to Wikipedia:Bot/Status (bugfix #2982162)
Modified Paths:
--------------
branches/rewrite/pywikibot/login.py
trunk/pywikipedia/login.py
Modified: branches/rewrite/pywikibot/login.py
===================================================================
--- branches/rewrite/pywikibot/login.py 2010-04-05 20:45:17 UTC (rev 8054)
+++ branches/rewrite/pywikibot/login.py 2010-04-06 07:35:40 UTC (rev 8055)
@@ -58,7 +58,7 @@
# the bot's user page in a specific list.
botList = {
'wikipedia': {
- 'en': u'Wikipedia:Registered bots',
+ 'en': u'Wikipedia:Bot/Status',
# Disabled because they are now using a template system which
# we can't check with our current code.
#'simple': u'Wikipedia:Bots',
Modified: trunk/pywikipedia/login.py
===================================================================
--- trunk/pywikipedia/login.py 2010-04-05 20:45:17 UTC (rev 8054)
+++ trunk/pywikipedia/login.py 2010-04-06 07:35:40 UTC (rev 8055)
@@ -39,7 +39,7 @@
If not given as parameter, the script will ask for your username and
password (password entry will be hidden), log in to your home wiki using
this combination, and store the resulting cookies (containing your password
-so keep it secured!) in a file in the login-data subdirectory.
+hash, so keep it secured!) in a file in the login-data subdirectory.
All scripts in this library will be looking for this cookie file and will
use the login information if it is present.
@@ -57,13 +57,14 @@
import re, os, query
import urllib2
-import wikipedia, config
+import config
+import wikipedia
# On some wikis you are only allowed to run a bot if there is a link to
# the bot's user page in a specific list.
botList = {
'wikipedia': {
- 'en': u'Wikipedia:Registered bots',
+ 'en': u'Wikipedia:Bot/Status',
# Disabled because they are now using a template system which
# we can't check with our current code.
#'simple': u'Wikipedia:Bots',