http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10082
Revision: 10082
Author: binbot
Date: 2012-04-04 15:21:23 +0000 (Wed, 04 Apr 2012)
Log Message:
-----------
With -nosub will not process subpages. Useful in template or portal namespace.
Modified Paths:
--------------
trunk/pywikipedia/ndashredir.py
Modified: trunk/pywikipedia/ndashredir.py
===================================================================
--- trunk/pywikipedia/ndashredir.py 2012-04-04 12:44:52 UTC (rev …
[View More]10081)
+++ trunk/pywikipedia/ndashredir.py 2012-04-04 15:21:23 UTC (rev 10082)
@@ -16,6 +16,9 @@
-namespace Works in the given namespace (only one at a time). Parameter
-ns may be given as "-ns:<number>" or "-namespace:<number>".
Defaults to 0 (main namespace).
+-nosub Will not process subpages. Useful in template or portal
+ namespace. (Not recommended for main namespace that has no
+ real subpages.)
-save Saves the title of existing hyphenated articles whose content
is _other_ than a redirect to the corresponding article with
n dash or m dash in the title and thus may need manual
@@ -56,6 +59,8 @@
ns = pywikibot.input(u'Which namespace should we process?')
elif arg.startswith('-ns:') or arg.startswith('-namespace:'):
ns = arg[arg.find(':')+1:]
+ elif arg == '-nosub':
+ regex = ur'[^/]*[–—][^/]*$'
elif arg == '-save':
filename = pywikibot.input('Please enter the filename:')
elif arg.startswith('-save:'):
[View Less]
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10081
Revision: 10081
Author: binbot
Date: 2012-04-04 12:44:52 +0000 (Wed, 04 Apr 2012)
Log Message:
-----------
ndashredir.py saves the titles that may need manual treatment + adding ndashredir.py to CONTENTS.
Modified Paths:
--------------
trunk/pywikipedia/CONTENTS
trunk/pywikipedia/ndashredir.py
Modified: trunk/pywikipedia/CONTENTS
===================================================================
--- trunk/pywikipedia/…
[View More]CONTENTS 2012-04-04 03:32:55 UTC (rev 10080)
+++ trunk/pywikipedia/CONTENTS 2012-04-04 12:44:52 UTC (rev 10081)
@@ -125,6 +125,8 @@
category.
movepages.py : Bot page moves to another title.
nowcommons.py : This bot can delete images with NowCommons template.
+ndashredir.py : Creates hyphenated redirects to articles with n dash
+ or m dash in their title.
pagefromfile.py : This bot takes its input from a file that contains a
number of pages to be put on the wiki.
piper.py : Pipes article text through external program(s) on
Modified: trunk/pywikipedia/ndashredir.py
===================================================================
--- trunk/pywikipedia/ndashredir.py 2012-04-04 03:32:55 UTC (rev 10080)
+++ trunk/pywikipedia/ndashredir.py 2012-04-04 12:44:52 UTC (rev 10081)
@@ -16,12 +16,16 @@
-namespace Works in the given namespace (only one at a time). Parameter
-ns may be given as "-ns:<number>" or "-namespace:<number>".
Defaults to 0 (main namespace).
+-save Saves the title of existing hyphenated articles whose content
+ is _other_ than a redirect to the corresponding article with
+ n dash or m dash in the title and thus may need manual
+ treatment. If omitted, these titles will be written only to
+ the screen (or the log if logging is on). The file is in the
+ form you may upload it to a wikipage.
+ May be given as "-save:<filename>". If it exists, titles
+ will be appended.
+"""
-"""
-"""
-TODO:
-- listing existing hyphenated titles to a file/wikipage instead of just skipping
-"""
#
# (C) Bináris, 2012
#
@@ -29,6 +33,7 @@
#
__version__='$Id$'
+import codecs
import wikipedia as pywikibot
from pagegenerators import RegexFilterPageGenerator as RPG
from pywikibot import i18n
@@ -37,10 +42,11 @@
regex = ur'.*[–—]' # Alt 0150 (n dash), alt 0151 (m dash), respectively.
ns = 0
start = '!'
+ filename = None # The name of the file to save titles
+ titlefile = None # The file object itself
# Handling parameters:
for arg in pywikibot.handleArgs(*args):
- pass
if arg == '-start':
start = pywikibot.input(
u'From which title do you want to continue?')
@@ -50,6 +56,19 @@
ns = pywikibot.input(u'Which namespace should we process?')
elif arg.startswith('-ns:') or arg.startswith('-namespace:'):
ns = arg[arg.find(':')+1:]
+ elif arg == '-save':
+ filename = pywikibot.input('Please enter the filename:')
+ elif arg.startswith('-save:'):
+ filename = arg[6:]
+ if filename:
+ try:
+ # This opens in strict error mode, that means bot will stop
+ # on encoding errors with ValueError.
+ # See http://docs.python.org/library/codecs.html#codecs.open
+ titlefile = codecs.open(filename, encoding='utf-8', mode='a')
+ except IOError:
+ pywikibot.output("%s cannot be opened for writing." % filename)
+ return
site = pywikibot.getSite()
redirword = site.redirect()
gen = RPG(site.allpages(
@@ -71,14 +90,25 @@
% (newtitle, title))
else:
pywikibot.output(
- (u'Skipping [[%s]] beacuse it exists already with a ' +
- u'different content.') % newtitle)
- # TODO: list it for further examination to a file or wikipage
+ (u'\03{lightyellow}Skipping [[%s]] beacuse it exists ' +
+ u'already with a different content.\03{default}')
+ % newtitle)
+ if titlefile:
+ s = u'\n#%s does not redirect to %s.' %\
+ (redirpage.title(asLink=True, textlink=True),
+ page.title(asLink=True, textlink=True))
+ # For the unlikely case if someone wants to run it in
+ # file namespace.
+ titlefile.write(s)
+ titlefile.flush()
else:
text = u'#%s[[%s]]' % (redirword, title)
redirpage.put(text, editSummary)
# Todo: output the title upon Ctrl C? (KeyboardInterrupt always hits
- # RegexFilterPageGenerator and cannot be catched in this loop.)
+ # RegexFilterPageGenerator or throttle.py or anything else and cannot
+ # be catched in this loop.)
+ if titlefile:
+ titlefile.close() # For the spirit of programming (it was flushed)
if __name__ == "__main__":
try:
[View Less]
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10079
Revision: 10079
Author: binbot
Date: 2012-04-03 12:46:06 +0000 (Tue, 03 Apr 2012)
Log Message:
-----------
Handling global arguments, introducing parameters for namespace and start title.
Modified Paths:
--------------
trunk/pywikipedia/ndashredir.py
Modified: trunk/pywikipedia/ndashredir.py
===================================================================
--- trunk/pywikipedia/ndashredir.py 2012-04-02 13:11:04 UTC (rev …
[View More]10078)
+++ trunk/pywikipedia/ndashredir.py 2012-04-03 12:46:06 UTC (rev 10079)
@@ -1,14 +1,27 @@
# -*- coding: utf-8 -*-
"""
-This script will collect articles (currently only from main namespace) that
-have n dash or m dash character in their title, and create a redirect to them
-from the corresponding hyphenated title.
+This script will collect articles that have n dash or m dash character in their
+title, and create a redirect to them automatically from the corresponding
+hyphenated title. If the target exists, will be skipped.
+It may take several hours. You may quit by Ctrl C at any time and continue
+later. Type the first few characters of the last shown title after -start.
+The script is primarily designed for work in article namespace, but can be used
+in any other one. Use in accordance with the rules of your community.
+
+Known parameters:
+-start Will start from the given title (it does not have to exist).
+ Parameter may be given as "-start" or "-start:tile"
+ Defaults to '!'.
+-namespace Works in the given namespace (only one at a time). Parameter
+-ns may be given as "-ns:<number>" or "-namespace:<number>".
+ Defaults to 0 (main namespace).
+
+"""
+"""
TODO:
-- prompting for other namespaces and start
- listing existing hyphenated titles to a file/wikipage instead of just skipping
"""
-
#
# (C) Bináris, 2012
#
@@ -20,28 +33,55 @@
from pagegenerators import RegexFilterPageGenerator as RPG
from pywikibot import i18n
-site = pywikibot.getSite()
-redirword = site.redirect()
-regex = ur'.*[–—]' # Alt 0150 (n dash), alt 0151 (m dash), respectively.
-gen = RPG(site.allpages(namespace=0, includeredirects=False), [regex])
-for page in gen:
- title = page.title()
- editSummary = i18n.twtranslate(site, 'ndashredir-create',
- {'title': title})
- newtitle = title.replace(u'–','-').replace(u'—','-')
- # n dash -> hyphen, m dash -> hyphen, respectively
- redirpage = pywikibot.Page(site, newtitle)
- if redirpage.exists():
- if redirpage.isRedirectPage() and \
- redirpage.getRedirectTarget() == page:
- pywikibot.output(
- u'[[%s]] already redirects to [[%s]], nothing to do with it.'
- % (newtitle, title))
+def main(*args):
+ regex = ur'.*[–—]' # Alt 0150 (n dash), alt 0151 (m dash), respectively.
+ ns = 0
+ start = '!'
+
+ # Handling parameters:
+ for arg in pywikibot.handleArgs(*args):
+ pass
+ if arg == '-start':
+ start = pywikibot.input(
+ u'From which title do you want to continue?')
+ elif arg.startswith('-start:'):
+ start = arg[7:]
+ elif arg in ['-ns', '-namespace']:
+ ns = pywikibot.input(u'Which namespace should we process?')
+ elif arg.startswith('-ns:') or arg.startswith('-namespace:'):
+ ns = arg[arg.find(':')+1:]
+ site = pywikibot.getSite()
+ redirword = site.redirect()
+ gen = RPG(site.allpages(
+ start=start, namespace=ns, includeredirects=False), [regex])
+
+ # Processing:
+ for page in gen:
+ title = page.title()
+ editSummary = i18n.twtranslate(site, 'ndashredir-create',
+ {'title': title})
+ newtitle = title.replace(u'–','-').replace(u'—','-')
+ # n dash -> hyphen, m dash -> hyphen, respectively
+ redirpage = pywikibot.Page(site, newtitle)
+ if redirpage.exists():
+ if redirpage.isRedirectPage() and \
+ redirpage.getRedirectTarget() == page:
+ pywikibot.output(
+ u'[[%s]] already redirects to [[%s]], nothing to do with it.'
+ % (newtitle, title))
+ else:
+ pywikibot.output(
+ (u'Skipping [[%s]] beacuse it exists already with a ' +
+ u'different content.') % newtitle)
+ # TODO: list it for further examination to a file or wikipage
else:
- pywikibot.output(
- (u'Skipping [[%s]] beacuse it exists already with a ' +
- u'different content.') % newtitle)
- # TODO: list it for further examination to a file or wikipage
- else:
- text = u'#%s[[%s]]' % (redirword, title)
- redirpage.put(text, editSummary)
+ text = u'#%s[[%s]]' % (redirword, title)
+ redirpage.put(text, editSummary)
+ # Todo: output the title upon Ctrl C? (KeyboardInterrupt always hits
+ # RegexFilterPageGenerator and cannot be catched in this loop.)
+
+if __name__ == "__main__":
+ try:
+ main()
+ finally:
+ pywikibot.stopme()
[View Less]
http://www.mediawiki.org/wiki/Special:Code/pywikipedia/10076
Revision: 10076
Author: binbot
Date: 2012-04-02 12:06:30 +0000 (Mon, 02 Apr 2012)
Log Message:
-----------
A new script to create redirects to articles with n dash/m dash in their title. i18n file will be sent to pywikipedia-l due to an access error.
Added Paths:
-----------
trunk/pywikipedia/ndashredir.py
Added: trunk/pywikipedia/ndashredir.py
===================================================================
--- trunk/…
[View More]pywikipedia/ndashredir.py (rev 0)
+++ trunk/pywikipedia/ndashredir.py 2012-04-02 12:06:30 UTC (rev 10076)
@@ -0,0 +1,47 @@
+# -*- coding: utf-8 -*-
+"""
+This script will collect articles (currently only from main namespace) that
+have n dash or m dash character in their title, and create a redirect to them
+from the corresponding hyphenated title.
+
+TODO:
+- prompting for other namespaces and start
+- listing existing hyphenated titles to a file/wikipage instead of just skipping
+"""
+
+#
+# (C) Bináris, 2012
+#
+# Distributed under the terms of the MIT license.
+#
+__version__='$Id$'
+
+import wikipedia as pywikibot
+from pagegenerators import RegexFilterPageGenerator as RPG
+from pywikibot import i18n
+
+site = pywikibot.getSite()
+redirword = site.redirect()
+regex = ur'.*[–—]' # Alt 0150 (n dash), alt 0151 (m dash), respectively.
+gen = RPG(site.allpages(namespace=0, includeredirects=False), [regex])
+for page in gen:
+ title = page.title()
+ editSummary = i18n.twtranslate(site, 'ndashredir-create',
+ {'title': title})
+ newtitle = title.replace(u'–','-').replace(u'—','-')
+ # n dash -> hyphen, m dash -> hyphen, respectively
+ redirpage = pywikibot.Page(site, newtitle)
+ if redirpage.exists():
+ if redirpage.isRedirectPage() and \
+ redirpage.getRedirectTarget() == page:
+ pywikibot.output(
+ u'[[%s]] already redirects to [[%s]], nothing to do with it.'
+ % (newtitle, title))
+ else:
+ pywikibot.output(
+ (u'Skipping [[%s]] beacuse it exists already with a ' +
+ u'different content.') % newtitle)
+ # TODO: list it for further examination to a file or wikipage
+ else:
+ text = u'#%s[[%s]]' % (redirword, title)
+ redirpage.put(text, editSummary)
Property changes on: trunk/pywikipedia/ndashredir.py
___________________________________________________________________
Added: svn:eol-style
+ native
[View Less]