http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11589
Revision: 11589
Author: drtrigon
Date: 2013-05-25 21:19:21 +0000 (Sat, 25 May 2013)
Log Message:
-----------
bug fix; svn:keywords missing
Modified Paths:
--------------
trunk/pywikipedia/replicate_wiki.py
Property Changed:
----------------
trunk/pywikipedia/replicate_wiki.py
Modified: trunk/pywikipedia/replicate_wiki.py
===================================================================
--- trunk/pywikipedia/replicate_wiki.py 2013-05-25 21:14:50 UTC (rev 11588)
+++ trunk/pywikipedia/replicate_wiki.py 2013-05-25 21:19:21 UTC (rev 11589)
@@ -1,12 +1,5 @@
#!/usr/bin/env python
-#
# -*- coding: utf-8 -*-
-#
-# (C) Kasper Souren 2012-2013
-#
-# Distributed under the terms of the MIT license.
-#
-
'''
This bot replicates all pages (from specific namespaces) in a wiki to a second wiki within one family.
@@ -25,6 +18,14 @@
to replace all occurences of 'Hoofdpagina' with 'Veurblaad' when writing to liwiki. Note that this does
not take the origin wiki into account.
'''
+#
+# (C) Kasper Souren 2012-2013
+# (C) 2013 Pywikipediabot team
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+#
import sys
import re
Property changes on: trunk/pywikipedia/replicate_wiki.py
___________________________________________________________________
Added: svn:keywords
+ Id
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11587
Revision: 11587
Author: xqt
Date: 2013-05-25 16:34:50 +0000 (Sat, 25 May 2013)
Log Message:
-----------
fix bug #3604180 for wikisource, patch submitted by betacommand.
some PEP8 changes.
update from trunk r11586
Modified Paths:
--------------
branches/rewrite/scripts/redirect.py
Modified: branches/rewrite/scripts/redirect.py
===================================================================
--- branches/rewrite/scripts/redirect.py 2013-05-25 16:20:36 UTC (rev 11586)
+++ branches/rewrite/scripts/redirect.py 2013-05-25 16:34:50 UTC (rev 11587)
@@ -52,13 +52,14 @@
# (C) Daniel Herding, 2004.
# (C) Purodha Blissenbach, 2009.
# (C) xqt, 2009-2012
-# (C) Pywikipedia bot team, 2004-2012
+# (C) Pywikipedia bot team, 2004-2013
#
# Distributed under the terms of the MIT license.
#
__version__='$Id: redirect.py 7789 2009-12-17 19:20:12Z xqt $'
#
-import re, sys, datetime
+import re
+import datetime
import pywikibot
from pywikibot import i18n
from pywikibot import config
@@ -73,8 +74,8 @@
self.site.login()
self.xmlFilename = xmlFilename
self.namespaces = namespaces
- if use_api and self.namespaces == []:
- self.namespaces = [ 0 ]
+ if use_api and not self.namespaces:
+ self.namespaces = [0]
self.offset = offset
self.use_move_log = use_move_log
self.use_api = use_api
@@ -145,7 +146,7 @@
u'HINT: %s is a redirect with a pipelink.'
% entry.title)
target = target[:target.index('|')].rstrip("_")
- if target: # in case preceding steps left nothing
+ if target: # in case preceding steps left nothing
redict[source] = target
if alsoGetPageTitles:
return redict, pageTitles
@@ -203,10 +204,11 @@
(currently, the API seems not to return sufficient
data to allow this return values, but that may
change)
- None - start of a redirect chain of unknown length, or loop
+ None - start of a redirect chain of unknown length, or
+ loop
2 - target page title of the redirect, or chain (may not exist)
- 3 - target page of the redirect, or end of chain, or page title where
- chain or loop detecton was halted, or None if unknown
+ 3 - target page of the redirect, or end of chain, or page title
+ where chain or loop detecton was halted, or None if unknown
"""
for apiQ in self._next_redirect_group():
gen = pywikibot.data.api.Request(action="query", redirects="",
@@ -235,8 +237,8 @@
final = target
try:
while result <= maxlen:
- result += 1
- final = redirects[final]
+ result += 1
+ final = redirects[final]
# result = None
except KeyError:
pass
@@ -321,8 +323,8 @@
if self.offset <= 0:
self.offset = 1
- start = datetime.datetime.utcnow() \
- - datetime.timedelta(0, self.offset*3600)
+ start = datetime.datetime.utcnow() - \
+ datetime.timedelta(0, self.offset * 3600)
# self.offset hours ago
offset_time = start.strftime("%Y%m%d%H%M%S")
pywikibot.output(u'Retrieving %s moved pages via API...'
@@ -386,7 +388,7 @@
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
- % redir_page.title())
+ % redir_page.title())
try:
targetPage = redir_page.getRedirectTarget()
except pywikibot.IsNotRedirectPage:
@@ -397,20 +399,21 @@
try:
targetPage.get()
except pywikibot.NoPage:
- if self.prompt(
- u'Redirect target %s does not exist. Do you want to delete %s?'
+ if self.prompt(u'Redirect target %s does not exist. '
+ u'Do you want to delete %s?'
% (targetPage.title(asLink=True),
redir_page.title(asLink=True))):
try:
- redir_page.delete(reason, prompt = False)
+ redir_page.delete(reason, prompt=False)
except pywikibot.NoUsername:
- if i18n.twhas_key(
+ if (i18n.twhas_key(
targetPage.site.lang,
- 'redirect-broken-redirect-template') and \
+ 'redirect-broken-redirect-template') and
i18n.twhas_key(targetPage.site.lang,
- 'redirect-remove-broken'):
- pywikibot.output(
- u"No sysop in user-config.py, put page to speedy deletion.")
+ 'redirect-remove-broken')) or \
+ targetPage.site.lang == '-':
+ pywikibot.output(u"No sysop in user-config.py, "
+ u"put page to speedy deletion.")
content = redir_page.get(get_redirect=True)
### TODO: Add bot's signature if needed
### Not supported via TW yet
@@ -420,9 +423,9 @@
) + "\n" + content
redir_page.put(content, reason)
except pywikibot.IsRedirectPage:
- pywikibot.output(
- u'Redirect target %s is also a redirect! Won\'t delete anything.'
- % targetPage.title(asLink=True))
+ pywikibot.output(u"Redirect target %s is also a redirect! "
+ u"Won't delete anything."
+ % targetPage.title(asLink=True))
else:
#we successfully get the target page, meaning that
#it exists and is not a redirect: no reason to touch it.
@@ -442,7 +445,7 @@
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
- % redir.title())
+ % redir.title())
newRedir = redir
redirList = [] # bookkeeping to detect loops
while True:
@@ -454,18 +457,18 @@
if len(redirList) == 1:
pywikibot.output(u'Skipping: Page %s is not a redirect.'
% redir.title(asLink=True))
- break #do nothing
+ break # do nothing
elif len(redirList) == 2:
pywikibot.output(
u'Skipping: Redirect target %s is not a redirect.'
% newRedir.title(asLink=True))
break # do nothing
else:
- pass # target found
+ pass # target found
except pywikibot.SectionError:
- pywikibot.output(
- u'Warning: Redirect target section %s doesn\'t exist.'
- % newRedir.title(asLink=True))
+ pywikibot.warning(
+ u"Redirect target section %s doesn't exist."
+ % newRedir.title(asLink=True))
except pywikibot.CircularRedirect, e:
try:
pywikibot.warning(u"Skipping circular redirect: [[%s]]"
@@ -475,14 +478,14 @@
break
except pywikibot.BadTitle, e:
# str(e) is in the format 'BadTitle: [[Foo]]'
- pywikibot.output(
- u'Warning: Redirect target %s is not a valid page title.'
- % str(e)[10:])
+ pywikibot.warning(
+ u'Redirect target %s is not a valid page title.'
+ % str(e)[10:])
break
except pywikibot.NoPage:
if len(redirList) == 1:
pywikibot.output(u'Skipping: Page %s does not exist.'
- % redir.title(asLink=True))
+ % redir.title(asLink=True))
break
else:
if self.always:
@@ -491,11 +494,12 @@
% newRedir.title(asLink=True))
break # skip if automatic
else:
- pywikibot.output(
- u"Warning: Redirect target %s doesn't exist."
+ pywikibot.warning(
+ u"Redirect target %s doesn't exist."
% newRedir.title(asLink=True))
except pywikibot.ServerError:
- pywikibot.output(u'Skipping: Server Error')
+ pywikibot.output(u'Skipping due to server error: '
+ u'No textarea found')
break
else:
pywikibot.output(
@@ -503,26 +507,26 @@
% targetPage.title(asLink=True))
if targetPage.site.sitename() == 'wikipedia:en':
mw_msg = targetPage.site.mediawiki_message(
- 'wikieditor-toolbar-tool-redirect-example')
+ 'wikieditor-toolbar-tool-redirect-example')
if targetPage.title() == mw_msg:
pywikibot.output(
- u"Skipping toolbar example: Redirect source is potentially vandalized.")
+ u"Skipping toolbar example: Redirect source is "
+ u"potentially vandalized.")
break
if targetPage.site != self.site:
- pywikibot.output(
- u'Warning: redirect target (%s) is on a different site.'
+ pywikibot.warning(
+ u'redirect target (%s) is on a different site.'
% targetPage.title(asLink=True))
if self.always:
break # skip if automatic
# watch out for redirect loops
if redirList.count(u'%s:%s'
% (targetPage.site.lang,
- targetPage.title(withSection=False))
- ) > 0:
- pywikibot.output(
- u'Warning: Redirect target %s forms a redirect loop.'
+ targetPage.title(withSection=False))):
+ pywikibot.warning(
+ u'Redirect target %s forms a redirect loop.'
% targetPage.title(asLink=True))
- break ### doesn't work. edits twice!
+ break ### doesn't work. edits twice!
## try:
## content = targetPage.get(get_redirect=True)
## except pywikibot.SectionError:
@@ -547,7 +551,7 @@
## targetPage.put(content, summ)
## redir.put(content, summ)
## break # TODO Better implement loop redirect
- else: # redirect target found
+ else: # redirect target found
if targetPage.isStaticRedirect():
pywikibot.output(
u" Redirect target is STATICREDIRECT.")
@@ -595,7 +599,8 @@
break
def fix_double_or_delete_broken_redirects(self):
- # TODO: part of this should be moved to generator, the rest merged into self.run()
+ # TODO: part of this should be moved to generator, the rest merged into
+ # self.run()
# get reason for deletion text
delete_reason = i18n.twtranslate(self.site, 'redirect-remove-broken')
count = 0
@@ -668,14 +673,14 @@
ns = i18n.input('pywikibot-enter-namespace-number')
# TODO! at least for some generators enter a namespace by its name or number
if ns == '':
- ns = '0'
+ ns = '0'
try:
ns = int(ns)
except ValueError:
#-namespace:all Process all namespaces. Works only with the API read interface.
- pass
+ pass
if not ns in namespaces:
- namespaces.append(ns)
+ namespaces.append(ns)
elif arg.startswith('-offset:'):
offset = int(arg[8:])
elif arg.startswith('-start:'):
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11586
Revision: 11586
Author: xqt
Date: 2013-05-25 16:20:36 +0000 (Sat, 25 May 2013)
Log Message:
-----------
fix bug #3604180 for wikisource, patch submitted by betacommand. Thanks a lot.
some PEP8 changes.
Modified Paths:
--------------
trunk/pywikipedia/redirect.py
Modified: trunk/pywikipedia/redirect.py
===================================================================
--- trunk/pywikipedia/redirect.py 2013-05-25 15:22:10 UTC (rev 11585)
+++ trunk/pywikipedia/redirect.py 2013-05-25 16:20:36 UTC (rev 11586)
@@ -55,13 +55,14 @@
# (C) Daniel Herding, 2004.
# (C) Purodha Blissenbach, 2009.
# (C) xqt, 2009-2012
-# (C) Pywikipedia bot team, 2004-2012
+# (C) Pywikipedia bot team, 2004-2013
#
# Distributed under the terms of the MIT license.
#
-__version__='$Id$'
+__version__ = '$Id$'
#
-import re, sys, datetime
+import re
+import datetime
import wikipedia as pywikibot
from pywikibot import i18n
import config
@@ -76,8 +77,8 @@
self.site = pywikibot.getSite()
self.xmlFilename = xmlFilename
self.namespaces = namespaces
- if use_api and self.namespaces == []:
- self.namespaces = [ 0 ]
+ if use_api and not self.namespaces:
+ self.namespaces = [0]
self.offset = offset
self.use_move_log = use_move_log
self.use_api = use_api
@@ -153,7 +154,7 @@
u'HINT: %s is a redirect with a pipelink.'
% entry.title)
target = target[:target.index('|')].rstrip("_")
- if target: # in case preceding steps left nothing
+ if target: # in case preceding steps left nothing
redict[source] = target
if alsoGetPageTitles:
return redict, pageTitles
@@ -182,7 +183,7 @@
data = query.GetData(params, self.site)
if 'error' in data:
raise RuntimeError("API query error: %s" % data['error'])
- if "limits" in data: # process aplimit = max
+ if "limits" in data: # process aplimit = max
params['aplimit'] = int(data['limits']['allpages'])
for x in data['query']['allpages']:
done = self.api_until and x['title'] >= self.api_until
@@ -223,16 +224,17 @@
(currently, the API seems not to return sufficient
data to allow this return values, but that may
change)
- None - start of a redirect chain of unknown length, or loop
+ None - start of a redirect chain of unknown length, or
+ loop
2 - target page title of the redirect, or chain (may not exist)
- 3 - target page of the redirect, or end of chain, or page title where
- chain or loop detecton was halted, or None if unknown
+ 3 - target page of the redirect, or end of chain, or page title
+ where chain or loop detecton was halted, or None if unknown
"""
import urllib
params = {
- 'action':'query',
- 'redirects':1,
- #'':'',
+ 'action': 'query',
+ 'redirects': 1,
+ #'': '',
}
for apiQ in self._next_redirect_group():
params['pageids'] = apiQ
@@ -261,8 +263,8 @@
final = target
try:
while result <= maxlen:
- result += 1
- final = redirects[final]
+ result += 1
+ final = redirects[final]
# result = None
except KeyError:
pass
@@ -283,7 +285,7 @@
if count >= self.api_number:
break
- elif self.xmlFilename == None:
+ elif not self.xmlFilename:
# retrieve information from the live wiki's maintenance page
# broken redirect maintenance page's URL
path = self.site.broken_redirects_address(default_limit=False)
@@ -304,7 +306,7 @@
pywikibot.output(
u'Getting a list of all redirects and of all page titles...')
redirs, pageTitles = self.get_redirects_from_dump(
- alsoGetPageTitles=True)
+ alsoGetPageTitles=True)
for (key, value) in redirs.iteritems():
if value not in pageTitles:
yield key
@@ -321,7 +323,7 @@
if count >= self.api_number:
break
- elif self.xmlFilename == None:
+ elif not self.xmlFilename:
if self.use_move_log:
if self.use_api:
gen = self.get_moved_pages_redirects()
@@ -333,7 +335,7 @@
# retrieve information from the live wiki's maintenance page
# double redirect maintenance page's URL
# pywikibot.config.special_page_limit = 1000
- path = self.site.double_redirects_address(default_limit = False)
+ path = self.site.double_redirects_address(default_limit=False)
pywikibot.output(u'Retrieving special page...')
maintenance_txt = self.site.getUrl(path)
@@ -363,8 +365,8 @@
if self.offset <= 0:
self.offset = 1
- start = datetime.datetime.utcnow() \
- - datetime.timedelta(0, self.offset*3600)
+ start = datetime.datetime.utcnow() - \
+ datetime.timedelta(0, self.offset * 3600)
# self.offset hours ago
offset_time = start.strftime("%Y%m%d%H%M%S")
pywikibot.output(u'Retrieving %s moved pages via API...'
@@ -403,8 +405,8 @@
r"""\(<a href="/w/index\.php\?title=Special:Log&offset=(\d+)"""
r"""&limit=500&type=move" title="Special:Log" rel="next">"""
r"""older 500</a>\)""")
- start = datetime.datetime.utcnow() \
- - datetime.timedelta(0, self.offset*3600)
+ start = datetime.datetime.utcnow() - \
+ datetime.timedelta(0, self.offset * 3600)
# self.offset hours ago
offset_time = start.strftime("%Y%m%d%H%M%S")
while True:
@@ -482,7 +484,7 @@
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
- % redir_page.title())
+ % redir_page.title())
try:
targetPage = redir_page.getRedirectTarget()
except pywikibot.IsNotRedirectPage:
@@ -493,20 +495,21 @@
try:
targetPage.get()
except pywikibot.NoPage:
- if self.prompt(
- u'Redirect target %s does not exist. Do you want to delete %s?'
+ if self.prompt(u'Redirect target %s does not exist. '
+ u'Do you want to delete %s?'
% (targetPage.title(asLink=True),
redir_page.title(asLink=True))):
try:
- redir_page.delete(reason, prompt = False)
+ redir_page.delete(reason, prompt=False)
except pywikibot.NoUsername:
- if i18n.twhas_key(
+ if (i18n.twhas_key(
targetPage.site.lang,
- 'redirect-broken-redirect-template') and \
+ 'redirect-broken-redirect-template') and
i18n.twhas_key(targetPage.site.lang,
- 'redirect-remove-broken'):
- pywikibot.output(
- u"No sysop in user-config.py, put page to speedy deletion.")
+ 'redirect-remove-broken')) or \
+ targetPage.site.lang == '-':
+ pywikibot.output(u"No sysop in user-config.py, "
+ u"put page to speedy deletion.")
content = redir_page.get(get_redirect=True)
### TODO: Add bot's signature if needed
### Not supported via TW yet
@@ -516,9 +519,9 @@
) + "\n" + content
redir_page.put(content, reason)
except pywikibot.IsRedirectPage:
- pywikibot.output(
- u'Redirect target %s is also a redirect! Won\'t delete anything.'
- % targetPage.title(asLink=True))
+ pywikibot.output(u"Redirect target %s is also a redirect! "
+ u"Won't delete anything."
+ % targetPage.title(asLink=True))
else:
#we successfully get the target page, meaning that
#it exists and is not a redirect: no reason to touch it.
@@ -538,7 +541,7 @@
# Show the title of the page we're working on.
# Highlight the title in purple.
pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
- % redir.title())
+ % redir.title())
newRedir = redir
redirList = [] # bookkeeping to detect loops
while True:
@@ -550,31 +553,31 @@
if len(redirList) == 1:
pywikibot.output(u'Skipping: Page %s is not a redirect.'
% redir.title(asLink=True))
- break #do nothing
+ break # do nothing
elif len(redirList) == 2:
pywikibot.output(
u'Skipping: Redirect target %s is not a redirect.'
% newRedir.title(asLink=True))
break # do nothing
else:
- pass # target found
+ pass # target found
except pywikibot.SectionError:
pywikibot.warning(
- u'Redirect target section %s doesn\'t exist.'
- % newRedir.title(asLink=True))
+ u"Redirect target section %s doesn't exist."
+ % newRedir.title(asLink=True))
except pywikibot.BadTitle, e:
# str(e) is in the format 'BadTitle: [[Foo]]'
pywikibot.warning(
u'Redirect target %s is not a valid page title.'
- % str(e)[10:])
- #sometimes this error occures. Invalid Title starting with a '#'
+ % str(e)[10:])
+ # sometimes this error occures. Invalid Title starting with a '#'
except pywikibot.InvalidTitle, err:
pywikibot.warning(u'%s' % err)
break
except pywikibot.NoPage:
if len(redirList) == 1:
pywikibot.output(u'Skipping: Page %s does not exist.'
- % redir.title(asLink=True))
+ % redir.title(asLink=True))
break
else:
if self.always:
@@ -596,10 +599,11 @@
% targetPage.title(asLink=True))
if targetPage.site.sitename() == 'wikipedia:en':
mw_msg = targetPage.site.mediawiki_message(
- 'wikieditor-toolbar-tool-redirect-example')
+ 'wikieditor-toolbar-tool-redirect-example')
if targetPage.title() == mw_msg:
pywikibot.output(
- u"Skipping toolbar example: Redirect source is potentially vandalized.")
+ u"Skipping toolbar example: Redirect source is "
+ u"potentially vandalized.")
break
if targetPage.site != self.site:
pywikibot.warning(
@@ -610,12 +614,11 @@
# watch out for redirect loops
if redirList.count(u'%s:%s'
% (targetPage.site.lang,
- targetPage.sectionFreeTitle())
- ) > 0:
+ targetPage.sectionFreeTitle())):
pywikibot.warning(
u'Redirect target %s forms a redirect loop.'
% targetPage.title(asLink=True))
- break ### doesn't work. edits twice!
+ break ### doesn't work. edits twice!
## try:
## content = targetPage.get(get_redirect=True)
## except pywikibot.SectionError:
@@ -640,7 +643,7 @@
## targetPage.put(content, summ)
## redir.put(content, summ)
## break # TODO Better implement loop redirect
- else: # redirect target found
+ else: # redirect target found
if targetPage.isStaticRedirect():
pywikibot.output(
u" Redirect target is STATICREDIRECT.")
@@ -688,7 +691,8 @@
break
def fix_double_or_delete_broken_redirects(self):
- # TODO: part of this should be moved to generator, the rest merged into self.run()
+ # TODO: part of this should be moved to generator, the rest merged into
+ # self.run()
# get reason for deletion text
delete_reason = i18n.twtranslate(self.site, 'redirect-remove-broken')
count = 0
@@ -760,14 +764,14 @@
ns = i18n.input('pywikibot-enter-namespace-number')
# TODO! at least for some generators enter a namespace by its name or number
if ns == '':
- ns = '0'
+ ns = '0'
try:
ns = int(ns)
except ValueError:
#-namespace:all Process all namespaces. Works only with the API read interface.
- pass
+ pass
if not ns in namespaces:
- namespaces.append(ns)
+ namespaces.append(ns)
elif arg.startswith('-offset:'):
offset = int(arg[8:])
elif arg.startswith('-start:'):
@@ -786,8 +790,7 @@
else:
pywikibot.output(u'Unknown argument: %s' % arg)
- if not action or (xmlFilename and moved_pages)\
- or (api and xmlFilename):
+ if not action or (xmlFilename and moved_pages) or (api and xmlFilename):
pywikibot.showHelp('redirect')
else:
gen = RedirectGenerator(xmlFilename, namespaces, offset, moved_pages,