jenkins-bot has submitted this change and it was merged.
Change subject: add_text.py porting to core. The -untagged option is not yet supported. Started work on bug 56774.
......................................................................
add_text.py porting to core.
The -untagged option is not yet supported.
Started work on bug 56774.
Change-Id: I2cf7a4ede53ebf3479748b31590c88b8c9aff4f3
---
A scripts/add_text.py
1 file changed, 377 insertions(+), 0 deletions(-)
Approvals:
Merlijn van Deen: Looks good to me, approved
jenkins-bot: Verified
diff --git a/scripts/add_text.py b/scripts/add_text.py
new file mode 100644
index 0000000..528b82f
--- /dev/null
+++ b/scripts/add_text.py
@@ -0,0 +1,377 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+This is a Bot written by Filnik to add a text at the end of the page but above
+categories, interwiki and template for the stars of the interwiki (default).
+
+Alternatively it may also add a text at the top of the page.
+These command line parameters can be used to specify which pages to work on:
+
+¶ms;
+
+Furthermore, the following command line parameters are supported:
+
+-page Use a page as generator
+
+-talkpage Put the text onto the talk page instead the generated on
+-talk
+
+-text Define which text to add. "\n" are interpreted as newlines.
+
+-textfile Define a texfile name which contains the text to add
+
+-summary Define the summary to use
+
+-except Use a regex to check if the text is already in the page
+
+-excepturl Use the html page as text where you want to see if there's
+ the text, not the wiki-page.
+
+-newimages Add text in the new images
+
+(Not yet supported: -untagged Add text in the images that don't have any license template)
+
+-always If used, the bot won't ask if it should add the text
+ specified
+
+-up If used, put the text at the top of the page
+
+-noreorder Avoid to reorder cats and interwiki
+
+--- Example ---
+1.
+# This is a script to add a template to the top of the pages with
+# category:catname
+# Warning! Put it in one line, otherwise it won't work correctly.
+
+python add_text.py -cat:catname -summary:"Bot: Adding a template"
+-text:"{{Something}}" -except:"\{\{([Tt]emplate:|)[Ss]omething" -up
+
+2.
+# Command used on it.wikipedia to put the template in the page without any
+# category.
+# Warning! Put it in one line, otherwise it won't work correctly.
+
+python add_text.py -excepturl:"class='catlinks'>" -uncat
+-text:"{{Categorizzare}}" -except:"\{\{([Tt]emplate:|)[Cc]ategorizzare"
+-summary:"Bot: Aggiungo template Categorizzare"
+
+--- Credits and Help ---
+This script has been written by Botwiki's staff, if you want to help us
+or you need some help regarding this script, you can find us here:
+
+* http://botwiki.sno.cc
+
+"""
+
+#
+# (C) Filnik, 2007-2010
+# (C) Pywikipedia bot team, 2007-2013
+#
+# Distributed under the terms of the MIT license.
+#
+__version__ = '$Id$'
+#
+
+import re
+import webbrowser
+import codecs
+import pywikibot
+from pywikibot import config
+from pywikibot import i18n
+from pywikibot import pagegenerators
+
+docuReplacements = {
+ '¶ms;': pagegenerators.parameterHelp,
+}
+
+
+class NoEnoughData(pywikibot.Error):
+ """ Error class for when the user doesn't specified all the data needed """
+
+starsList = [
+ u'bueno',
+ u'bom interwiki',
+ u'cyswllt[ _]erthygl[ _]ddethol', u'dolen[ _]ed',
+ u'destacado', u'destaca[tu]',
+ u'enllaç[ _]ad',
+ u'enllaz[ _]ad',
+ u'leam[ _]vdc',
+ u'legătură[ _]a[bcf]',
+ u'liamm[ _]pub',
+ u'lien[ _]adq',
+ u'lien[ _]ba',
+ u'liên[ _]kết[ _]bài[ _]chất[ _]lượng[ _]tốt',
+ u'liên[ _]kết[ _]chọn[ _]lọc',
+ u'ligam[ _]adq',
+ u'ligoelstara',
+ u'ligoleginda',
+ u'link[ _][afgu]a', u'link[ _]adq', u'link[ _]f[lm]', u'link[ _]km',
+ u'link[ _]sm', u'linkfa',
+ u'na[ _]lotura',
+ u'nasc[ _]ar',
+ u'tengill[ _][úg]g',
+ u'ua',
+ u'yüm yg',
+ u'רא',
+ u'وصلة مقالة جيدة',
+ u'وصلة مقالة مختارة',
+]
+
+
+def add_text(page=None, addText=None, summary=None, regexSkip=None,
+ regexSkipUrl=None, always=False, up=False, putText=True,
+ oldTextGiven=None, reorderEnabled=True, create=False):
+ if not addText:
+ raise NoEnoughData('You have to specify what text you want to add!')
+ if not summary:
+ summary = i18n.twtranslate(pywikibot.getSite(), 'add_text-adding',
+ {'adding': addText[:200]})
+
+ # When a page is tagged as "really well written" it has a star in the
+ # interwiki links. This is a list of all the templates used (in regex
+ # format) to make the stars appear.
+
+ errorCount = 0
+ site = pywikibot.getSite()
+ pathWiki = site.family.nicepath(site.lang)
+
+ if putText:
+ pywikibot.output(u'Loading %s...' % page.title())
+ if oldTextGiven is None:
+ try:
+ text = page.get()
+ except pywikibot.NoPage:
+ if create:
+ pywikibot.output(u"%s doesn't exist, creating it!"
+ % page.title())
+ text = u''
+ else:
+ pywikibot.output(u"%s doesn't exist, skip!" % page.title())
+ return (False, False, always)
+ except pywikibot.IsRedirectPage:
+ pywikibot.output(u"%s is a redirect, skip!" % page.title())
+ return (False, False, always)
+ else:
+ text = oldTextGiven
+ # Understand if the bot has to skip the page or not
+ # In this way you can use both -except and -excepturl
+ if regexSkipUrl is not None:
+ url = '%s%s' % (pathWiki, page.urlname())
+ result = re.findall(regexSkipUrl, site.getUrl(url))
+ if result != []:
+ pywikibot.output(
+u'''Exception! regex (or word) used with -exceptUrl is in the page. Skip!
+Match was: %s''' % result)
+ return (False, False, always)
+ if regexSkip is not None:
+ result = re.findall(regexSkip, text)
+ if result != []:
+ pywikibot.output(
+u'''Exception! regex (or word) used with -except is in the page. Skip!
+Match was: %s''' % result)
+ return (False, False, always)
+ # If not up, text put below
+ if not up:
+ newtext = text
+ # Translating the \\n into binary \n
+ addText = addText.replace('\\n', '\n')
+ if (reorderEnabled):
+ # Getting the categories
+ categoriesInside = pywikibot.getCategoryLinks(newtext, site)
+ # Deleting the categories
+ newtext = pywikibot.removeCategoryLinks(newtext, site)
+ # Getting the interwiki
+ interwikiInside = pywikibot.getLanguageLinks(newtext, site)
+ # Removing the interwiki
+ newtext = pywikibot.removeLanguageLinks(newtext, site)
+
+ # Adding the text
+ newtext += u"\n%s" % addText
+ # Reputting the categories
+ newtext = pywikibot.replaceCategoryLinks(newtext,
+ categoriesInside, site,
+ True)
+ # Dealing the stars' issue
+ allstars = []
+ starstext = pywikibot.removeDisabledParts(text)
+ for star in starsList:
+ regex = re.compile('(\{\{(?:template:|)%s\|.*?\}\}[\s]*)'
+ % star, re.I)
+ found = regex.findall(starstext)
+ if found != []:
+ newtext = regex.sub('', newtext)
+ allstars += found
+ if allstars != []:
+ newtext = newtext.strip() + '\r\n\r\n'
+ allstars.sort()
+ for element in allstars:
+ newtext += '%s\r\n' % element.strip()
+ # Adding the interwiki
+ newtext = pywikibot.replaceLanguageLinks(newtext, interwikiInside,
+ site)
+ else:
+ newtext += u"\n%s" % addText
+ else:
+ newtext = addText + '\n' + text
+ if putText and text != newtext:
+ pywikibot.output(u"\n\n>>> \03{lightpurple}%s\03{default} <<<"
+ % page.title())
+ pywikibot.showDiff(text, newtext)
+ # Let's put the changes.
+ while True:
+ # If someone load it as module, maybe it's not so useful to put the
+ # text in the page
+ if putText:
+ if not always:
+ choice = pywikibot.inputChoice(
+ u'Do you want to accept these changes?',
+ ['Yes', 'No', 'All', 'open in Browser'],
+ ['y', 'n', 'a', 'b'], 'n')
+ if choice == 'a':
+ always = True
+ elif choice == 'n':
+ return (False, False, always)
+ elif choice == 'b':
+ webbrowser.open("http://%s%s" % (
+ page.site().hostname(),
+ page.site().nice_get_address(page.title())
+ ))
+ pywikibot.input("Press Enter when finished in browser.")
+ if always or choice == 'y':
+ try:
+ if always:
+ page.put(newtext, summary,
+ minorEdit=page.namespace() != 3)
+ else:
+ page.put_async(newtext, summary,
+ minorEdit=page.namespace() != 3)
+ except pywikibot.EditConflict:
+ pywikibot.output(u'Edit conflict! skip!')
+ return (False, False, always)
+ except pywikibot.ServerError:
+ errorCount += 1
+ if errorCount < 5:
+ pywikibot.output(u'Server Error! Wait..')
+ time.sleep(5)
+ continue
+ else:
+ raise pywikibot.ServerError(u'Fifth Server Error!')
+ except pywikibot.SpamfilterError, e:
+ pywikibot.output(
+ u'Cannot change %s because of blacklist entry %s'
+ % (page.title(), e.url))
+ return (False, False, always)
+ except pywikibot.PageNotSaved, error:
+ pywikibot.output(u'Error putting page: %s' % error.args)
+ return (False, False, always)
+ except pywikibot.LockedPage:
+ pywikibot.output(u'Skipping %s (locked page)'
+ % page.title())
+ return (False, False, always)
+ else:
+ # Break only if the errors are one after the other...
+ errorCount = 0
+ return (True, True, always)
+ else:
+ return (text, newtext, always)
+
+
+def main():
+ # If none, the var is setted only for check purpose.
+ summary = None
+ addText = None
+ regexSkip = None
+ regexSkipUrl = None
+ generator = None
+ always = False
+ textfile = None
+ talkPage = False
+ reorderEnabled = True
+ namespaces = []
+ # Load a lot of default generators
+ genFactory = pagegenerators.GeneratorFactory()
+ # Put the text above or below the text?
+ up = False
+ # Loading the arguments
+ for arg in pywikibot.handleArgs():
+ if arg.startswith('-textfile'):
+ if len(arg) == 9:
+ textfile = pywikibot.input(
+ u'Which textfile do you want to add?')
+ else:
+ textfile = arg[10:]
+ elif arg.startswith('-text'):
+ if len(arg) == 5:
+ addText = pywikibot.input(u'What text do you want to add?')
+ else:
+ addText = arg[6:]
+ elif arg.startswith('-summary'):
+ if len(arg) == 8:
+ summary = pywikibot.input(u'What summary do you want to use?')
+ else:
+ summary = arg[9:]
+ elif arg.startswith('-page'):
+ if len(arg) == 5:
+ generator = [pywikibot.Page(
+ pywikibot.getSite(),
+ pywikibot.input(u'What page do you want to use?'))]
+ else:
+ generator = [pywikibot.Page(pywikibot.getSite(), arg[6:])]
+ elif arg.startswith('-excepturl'):
+ if len(arg) == 10:
+ regexSkipUrl = pywikibot.input(u'What text should I skip?')
+ else:
+ regexSkipUrl = arg[11:]
+ elif arg.startswith('-except'):
+ if len(arg) == 7:
+ regexSkip = pywikibot.input(u'What text should I skip?')
+ else:
+ regexSkip = arg[8:]
+## *** NOT YET SUPPORTED ***
+## elif arg.startswith('-untagged'):
+## if len(arg) == 9:
+## untaggedProject = pywikibot.input(
+## u'What project do you want to use?')
+## else:
+## untaggedProject = arg[10:]
+## generator = untaggedGenerator(untaggedProject)
+ elif arg == '-up':
+ up = True
+ elif arg == '-noreorder':
+ reorderEnabled = False
+ elif arg == '-always':
+ always = True
+ elif arg == '-talk' or arg == '-talkpage':
+ talkPage = True
+ else:
+ genFactory.handleArg(arg)
+ if textfile and not addText:
+ f = codecs.open(textfile, 'r', config.textfile_encoding)
+ addText = f.read()
+ f.close()
+ if not generator:
+ generator = genFactory.getCombinedGenerator()
+ if not generator:
+ raise NoEnoughData(
+ 'You have to specify the generator you want to use for the script!')
+ if talkPage:
+ generator = pagegenerators.PageWithTalkPageGenerator(generator)
+ site = pywikibot.getSite()
+ for namespace in site.namespaces():
+ index = site.getNamespaceIndex(namespace)
+ if index % 2 == 1 and index > 0:
+ namespaces += [index]
+ generator = pagegenerators.NamespaceFilterPageGenerator(
+ generator, namespaces)
+ for page in generator:
+ (text, newtext, always) = add_text(page, addText, summary, regexSkip,
+ regexSkipUrl, always, up, True,
+ reorderEnabled=reorderEnabled,
+ create=talkPage)
+
+if __name__ == "__main__":
+ try:
+ main()
+ finally:
+ pywikibot.stopme()
--
To view, visit https://gerrit.wikimedia.org/r/94623
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I2cf7a4ede53ebf3479748b31590c88b8c9aff4f3
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Pyfisch <pyfisch(a)googlemail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change and it was merged.
Change subject: [BUGFIX] do not add separator if interwiki links are on one line
......................................................................
[BUGFIX] do not add separator if interwiki links are on one line
added missing check for
site.language() in site.family.interwiki_on_one_line
follow-up for f706e8b89885ad8510fe17f8c87bed2c7f6e0db0
Change-Id: Idd5b80a1c7046dc4a299b948246e40a3eb682b72
---
M pywikibot/textlib.py
1 file changed, 4 insertions(+), 2 deletions(-)
Approvals:
Merlijn van Deen: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index f1df622..3ebb0d0 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -516,9 +516,11 @@
if s:
if site.language() in site.family.interwiki_attop or \
u'<!-- interwiki at top -->' in oldtext:
- #do not add separator if interiki links are on one line
+ #do not add separator if interwiki links are on one line
newtext = (s +
- [u'' if site.language() else separator] + \
+ [u''
+ if site.language() in site.family.interwiki_on_one_line
+ else separator] +
s2.replace(marker, '').strip())
else:
# calculate what was after the language links on the page
--
To view, visit https://gerrit.wikimedia.org/r/94701
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Idd5b80a1c7046dc4a299b948246e40a3eb682b72
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change and it was merged.
Change subject: [SYNC] Synchronize compat with core
......................................................................
[SYNC] Synchronize compat with core
follow-up for https://gerrit.wikimedia.org/r/#/c/94701/
Change-Id: If92c948f0184dc8e84528d807d53f25d417c82d0
---
M pywikibot/textlib.py
1 file changed, 4 insertions(+), 3 deletions(-)
Approvals:
Merlijn van Deen: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index 80b55e5..ee84235 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -514,10 +514,11 @@
if s:
if site.language() in site.family.interwiki_attop or \
u'<!-- interwiki at top -->' in oldtext:
- #do not add separator if interiki links are on one line
+ #do not add separator if interwiki links are on one line
newtext = (s +
- [separator, u''][site.language() in
- site.family.interwiki_on_one_line] +
+ [u''
+ if site.language() in site.family.interwiki_on_one_line
+ else separator] +
s2.replace(marker, '').strip())
else:
# calculate what was after the language links on the page
--
To view, visit https://gerrit.wikimedia.org/r/94702
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: If92c948f0184dc8e84528d807d53f25d417c82d0
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/compat
Gerrit-Branch: master
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change and it was merged.
Change subject: Changing name.
......................................................................
Changing name.
Old name: "Pywikipedia bot"
New name: "Pywikibot"
Change-Id: I860d8ca0771f2cf021837d702d34ec220af11441
---
M INSTALL
M LICENSE
M README-conversion.txt
M generate_user_files.py
M pwb.py
M setup.py
6 files changed, 16 insertions(+), 17 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/INSTALL b/INSTALL
index 3d5166d..86a5a28 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,4 +1,4 @@
-To install the Pywikipediabot framework:
+To install the Pywikibot framework:
1) Extract/unzip this package to a directory on your computer.
2) Open a command prompt in that directory, and run the command:
@@ -12,7 +12,7 @@
python setup.py install --home=/path/to/location
-where "/path/to/location" is the directory that you want the Pywikipediabot
+where "/path/to/location" is the directory that you want the Pywikibot
files installed to.
Note: You will need to have an active Internet connection when you run the
diff --git a/LICENSE b/LICENSE
index 976c237..1b1e522 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2004-2013 Pywikipedia bot team
+Copyright (c) 2004-2013 Pywikibot team
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
@@ -22,6 +22,6 @@
OTHER DEALINGS IN THE SOFTWARE.
Everything in the 'externals' directory is general released under seperate
-licenses. Some parts may come from Pywikipedia bot team and share the same
+licenses. Some parts may come from Pywikibot team and share the same
permissions, but in general they have to be checked for any external package
seperately.
diff --git a/README-conversion.txt b/README-conversion.txt
index 58078da..c3c69cb 100644
--- a/README-conversion.txt
+++ b/README-conversion.txt
@@ -1,5 +1,5 @@
This is a guide to converting bot scripts from version 1 of the
-Pywikipediabot framework to version 2.
+Pywikibot framework to version 2.
Most importantly, note that the version 2 framework *only* supports wikis
using MediaWiki v.1.14 or higher software. If you need to access a wiki that
diff --git a/generate_user_files.py b/generate_user_files.py
index b28f4df..f1363fd 100644
--- a/generate_user_files.py
+++ b/generate_user_files.py
@@ -1,12 +1,12 @@
# -*- coding: utf-8 -*-
""" Script to create user files (user-config.py, user-fixes.py) """
#
-# (C) Pywikipedia team, 2010-2013
-#
-__version__ = '$Id$'
+# (C) Pywikibot team, 2010-2013
#
# Distributed under the terms of the MIT license.
#
+__version__ = '$Id$'
+#
import os
import sys
diff --git a/pwb.py b/pwb.py
index 9977fe0..4aecdee 100644
--- a/pwb.py
+++ b/pwb.py
@@ -5,13 +5,11 @@
and it will use the package directory to store all user files, will fix up
search paths so the package does not need to be installed, etc.
"""
-# (C) Pywikipedia team, 2013
-#
-__version__ = '$Id$'
+# (C) Pywikibot team, 2013
#
# Distributed under the terms of the MIT license.
#
-
+__version__ = '$Id$'
# The following snippet was developed by Ned Batchelder (and others)
# for coverage.py [1], and is available under the BSD license (see [2])
diff --git a/setup.py b/setup.py
index 620ee2f..c61b298 100644
--- a/setup.py
+++ b/setup.py
@@ -1,13 +1,14 @@
# -*- coding: utf-8 -*-
"""
Installer script for Pywikibot 2.0 framework
-
-(C) Pywikipedia team, 2009-2013
-
-Distributed under the terms of the MIT license.
-
"""
+#
+# (C) Pywikibot team, 2009-2013
+#
+# Distributed under the terms of the MIT license.
+#
__version__ = '$Id$'
+#
import sys
--
To view, visit https://gerrit.wikimedia.org/r/94781
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I860d8ca0771f2cf021837d702d34ec220af11441
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Pyfisch <pyfisch(a)googlemail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change and it was merged.
Change subject: Code improvements
......................................................................
Code improvements
- remove obsolete threading
- call getVersionHistory() as method not as function
because we do not need the result here and use the internal
variable "_revisions"
- simplify result for isEmpty()
- change "return None" to "return" which returns None per default
- PEP8 changes
Change-Id: I8eb71790f3c762feee1ff7d86fa91eed3be72b32
---
M pywikibot/page.py
1 file changed, 98 insertions(+), 78 deletions(-)
Approvals:
Xqt: Looks good to me, approved
Pyfisch: Looks good to me, but someone else must approve
jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py
index ca159fd..89574f7 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -19,7 +19,6 @@
import htmlentitydefs
import logging
import re
-import threading
import unicodedata
import urllib
import collections
@@ -83,7 +82,8 @@
self.__dict__ = source.__dict__
if title:
# overwrite title
- self._link = Link(title, source=source.site, defaultNamespace=ns)
+ self._link = Link(title, source=source.site,
+ defaultNamespace=ns)
elif isinstance(source, Link):
self._link = source
self._revisions = {}
@@ -141,11 +141,11 @@
title = title + "#" + self._link.section
if asLink:
if forceInterwiki or \
- (allowInterwiki and \
- (self.site.family.name != config.family
- or self.site.code != config.mylang)):
+ (allowInterwiki and
+ (self.site.family.name != config.family
+ or self.site.code != config.mylang)):
if self.site.family.name != config.family \
- and self.site.family.name != self.site.code:
+ and self.site.family.name != self.site.code:
return u'[[%s:%s:%s]]' % (self.site.family.name,
self.site.code,
title)
@@ -391,13 +391,12 @@
def expand_text(self, refresh=False):
"""Return the page text with all templates expanded."""
- if not hasattr(self, "_expanded_text") or (self._expanded_text is None) or refresh:
- req = pywikibot.data.api.Request(action="expandtemplates",
- text=self.text,
- title=self.title(withSection=False),
- site=self.site)
+ if not hasattr(self, "_expanded_text") or (
+ self._expanded_text is None) or refresh:
+ req = pywikibot.data.api.Request(
+ action="expandtemplates", text=self.text,
+ title=self.title(withSection=False), site=self.site)
self._expanded_text = req.submit()["expandtemplates"]["*"]
-
return self._expanded_text
def userName(self):
@@ -429,7 +428,7 @@
def previousRevision(self):
"""Return the revision id for the previous revision of this Page."""
- vh = self.getVersionHistory(total=2)
+ self.getVersionHistory(total=2)
revkey = sorted(self._revisions, reverse=True)[1]
return revkey
@@ -473,7 +472,8 @@
if template.title(withNamespace=False) in catredirs:
# Get target (first template argument)
try:
- self._catredirect = self.site.namespace(14) + ":" + args[0].strip()
+ self._catredirect = "%s:%s" % (self.site.namespace(14),
+ args[0].strip())
break
except IndexError:
pywikibot.warning(
@@ -501,10 +501,7 @@
txt = self.get()
txt = pywikibot.removeLanguageLinks(txt, site=self.site)
txt = pywikibot.removeCategoryLinks(txt, site=self.site)
- if len(txt) < 4:
- return True
- else:
- return False
+ return len(txt) < 4
def isTalkPage(self):
"""Return True if this page is in any talk namespace."""
@@ -523,20 +520,18 @@
"""
ns = self.namespace()
if ns < 0: # Special page
- return None
+ return
if self.isTalkPage():
if self.namespace() == 1:
return Page(self.site, self.title(withNamespace=False))
else:
- return Page(
- self.site,
- self.site.namespace(ns - 1) + ':' + self.title(withNamespace=False)
- )
+ return Page(self.site,
+ "%s:%s" % (self.site.namespace(ns - 1),
+ self.title(withNamespace=False)))
else:
- return Page(
- self.site,
- self.site.namespace(ns + 1) + ':' + self.title(withNamespace=False)
- )
+ return Page(self.site,
+ "%s:%s" % (self.site.namespace(ns + 1),
+ self.title(withNamespace=False)))
def isCategory(self):
"""Return True if the page is a Category, False otherwise."""
@@ -820,7 +815,8 @@
botflag=botflag, async=async, callback=callback,
**kwargs)
- def _save(self, comment, minor, watchval, botflag, async, callback, **kwargs):
+ def _save(self, comment, minor, watchval, botflag, async, callback,
+ **kwargs):
err = None
link = self.title(asLink=True)
if config.cosmetic_changes:
@@ -854,15 +850,15 @@
family = self.site.family.name
config.cosmetic_changes_disable.update({'wikidata': ('repo', )})
if config.cosmetic_changes_mylang_only:
- cc = (family == config.family and \
- self.site.lang == config.mylang) or \
- family in config.cosmetic_changes_enable.keys() and \
- self.site.lang in config.cosmetic_changes_enable[family]
+ cc = ((family == config.family and
+ self.site.lang == config.mylang) or
+ family in config.cosmetic_changes_enable.keys() and
+ self.site.lang in config.cosmetic_changes_enable[family])
else:
cc = True
- cc = cc and not \
- (family in config.cosmetic_changes_disable.keys() and \
- self.site.lang in config.cosmetic_changes_disable[family])
+ cc = (cc and not
+ (family in config.cosmetic_changes_disable.keys() and
+ self.site.lang in config.cosmetic_changes_disable[family]))
if not cc:
return
old = self.text
@@ -876,7 +872,8 @@
pageTitle=self.title())
self.text = ccToolkit.change(old)
if comment and \
- old.strip().replace('\r\n', '\n') != self.text.strip().replace('\r\n', '\n'):
+ old.strip().replace('\r\n',
+ '\n') != self.text.strip().replace('\r\n', '\n'):
comment += i18n.twtranslate(self.site, 'cosmetic_changes-append')
return comment
@@ -1277,7 +1274,8 @@
answer = u'y'
if prompt and not hasattr(self.site, '_noDeletePrompt'):
answer = pywikibot.inputChoice(
- u'Do you want to delete %s?' % self.title(asLink=True, forceInterwiki=True),
+ u'Do you want to delete %s?' % self.title(asLink=True,
+ forceInterwiki=True),
['Yes', 'No', 'All'],
['Y', 'N', 'A'],
'N')
@@ -1342,8 +1340,8 @@
if not hasattr(self, "_deletedRevs"):
self.loadDeletedRevisions()
if timestamp not in self._deletedRevs:
- #TODO: Throw an exception?
- return None
+ # TODO: Throw an exception?
+ return
self._deletedRevs[timestamp][4] = undelete
self._deletedRevsModified = True
@@ -1404,7 +1402,8 @@
answer = 'y'
if prompt and not hasattr(self.site, '_noProtectPrompt'):
answer = pywikibot.inputChoice(
- u'Do you want to change the protection level of %s?' % self.title(asLink=True, forceInterwiki=True),
+ u'Do you want to change the protection level of %s?'
+ % self.title(asLink=True, forceInterwiki=True),
['Yes', 'No', 'All'],
['Y', 'N', 'A'],
'N')
@@ -1431,7 +1430,8 @@
rearranging them.
"""
- #get list of Category objects the article is in and remove possible duplicates
+ # get list of Category objects the article is in and remove possible
+ # duplicates
cats = []
for cat in self.categories(get_redirect=True):
if cat not in cats:
@@ -1457,7 +1457,8 @@
newtext = pywikibot.replaceCategoryInPlace(oldtext, oldCat, newCat)
else:
if newCat:
- cats[cats.index(oldCat)] = Category(site, newCat.title(), sortKey=sortKey)
+ cats[cats.index(oldCat)] = Category(site, newCat.title(),
+ sortKey=sortKey)
else:
cats.pop(cats.index(oldCat))
oldtext = self.get(get_redirect=True)
@@ -1466,8 +1467,8 @@
except ValueError:
# Make sure that the only way replaceCategoryLinks() can return
# a ValueError is in the case of interwiki links to self.
- pywikibot.output(u'Skipping %s because of interwiki link to self'
- % self.title())
+ pywikibot.output(u'Skipping %s because of interwiki link to '
+ u'self' % self.title())
if oldtext != newtext:
try:
@@ -1482,8 +1483,8 @@
pywikibot.output(u'Skipping %s because page is locked'
% self.title())
except pywikibot.NoUsername:
- pywikibot.output(u'Page %s not saved; sysop privileges required.'
- % self.title(asLink=True))
+ pywikibot.output(u'Page %s not saved; sysop privileges '
+ u'required.' % self.title(asLink=True))
except pywikibot.PageNotSaved, error:
pywikibot.output(u'Saving page %s failed: %s'
% (self.title(asLink=True), error.message))
@@ -1496,11 +1497,11 @@
"""
if not self.isCategory():
- return None # should this raise an exception??
+ return # should this raise an exception??
try:
return self.site.categoryinfo(self)
except NotImplementedError:
- return None
+ return
######## DEPRECATED METHODS ########
@@ -1808,8 +1809,10 @@
namespaces=namespaces,
step=step, total=total,
content=content, sortby=sortby,
- starttime=starttime, endtime=endtime,
- startsort=startsort, endsort=endsort,
+ starttime=starttime,
+ endtime=endtime,
+ startsort=startsort,
+ endsort=endsort
):
yield member
if total is not None:
@@ -1822,9 +1825,12 @@
for subcat in self.subcategories(step=step):
for article in subcat.articles(recurse, step=step, total=total,
content=content,
- namespaces=namespaces, sortby=sortby,
- starttime=starttime, endtime=endtime,
- startsort=startsort, endsort=endsort,
+ namespaces=namespaces,
+ sortby=sortby,
+ starttime=starttime,
+ endtime=endtime,
+ startsort=startsort,
+ endsort=endsort
):
yield article
if total is not None:
@@ -2135,7 +2141,8 @@
if self._isAutoblock:
#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.")
+ 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))
@@ -2151,7 +2158,8 @@
if self._isAutoblock:
#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.")
+ 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,
@@ -2199,8 +2207,8 @@
return True
return False
- def block(self, expiry, reason, anononly=True, nocreate=True, autoblock=True,
- noemail=False, reblock=False):
+ def block(self, expiry, reason, anononly=True, nocreate=True,
+ autoblock=True, noemail=False, reblock=False):
"""
Blocks a user
@param expiry: When the block should expire
@@ -2220,7 +2228,8 @@
@return: None
"""
try:
- self.site.blockuser(self, expiry, reason, anononly, nocreate, autoblock, noemail, reblock)
+ self.site.blockuser(self, expiry, reason, anononly, nocreate,
+ autoblock, noemail, reblock)
except pywikibot.data.api.APIError, err:
if err.code == 'invalidrange':
raise ValueError("%s is not a valid IP range." % self.username)
@@ -2258,7 +2267,7 @@
for contrib in self.site.usercontribs(
user=self.username, namespaces=namespaces, total=total):
ts = pywikibot.Timestamp.fromISOformat(contrib['timestamp'])
- yield (Page(self.site, contrib['title'], contrib['ns']), \
+ yield (Page(self.site, contrib['title'], contrib['ns']),
contrib['revid'],
ts,
contrib.get('comment', None)
@@ -2278,7 +2287,7 @@
raise StopIteration
for item in self.site.logevents(
logtype='upload', user=self.username, total=total):
- yield (ImagePage(self.site, item.title().title()), \
+ yield (ImagePage(self.site, item.title().title()),
unicode(item.timestamp()),
item.comment(),
item.pageid() > 0
@@ -2401,7 +2410,8 @@
self.descriptions = {}
if 'descriptions' in self._content:
for lang in self._content['descriptions']:
- self.descriptions[lang] = self._content['descriptions'][lang]['value']
+ self.descriptions[lang] = self._content[
+ 'descriptions'][lang]['value']
return {'aliases': self.aliases,
'labels': self.labels,
@@ -2525,7 +2535,8 @@
@return: ItemPage
"""
repo = page.site.data_repository()
- if hasattr(page, '_pageprops') and page.properties().get('wikibase_item'):
+ if hasattr(page,
+ '_pageprops') and page.properties().get('wikibase_item'):
# If we have already fetched the pageprops for something else,
# we already have the id, so use it
return cls(repo, page.properties().get('wikibase_item'))
@@ -2566,11 +2577,13 @@
self.sitelinks = {}
if 'sitelinks' in self._content:
for dbname in self._content['sitelinks']:
- #Due to issues with locked/obsolete sites
- #this part is commented out
- #site = self.__make_site(dbname)
- #self.sitelinks[site] = pywikibot.Page(site, self._content['sitelinks'][dbname]['title'])
- self.sitelinks[dbname] = self._content['sitelinks'][dbname]['title']
+ # Due to issues with locked/obsolete sites
+ # this part is commented out
+## site = self.__make_site(dbname)
+## self.sitelinks[site] = pywikibot.Page(
+## site, self._content['sitelinks'][dbname]['title'])
+ self.sitelinks[dbname] = self._content[
+ 'sitelinks'][dbname]['title']
return {'aliases': self.aliases,
'labels': self.labels,
@@ -2589,10 +2602,12 @@
"""
if not hasattr(self, 'sitelinks'):
self.get()
- if family is not None and not isinstance(family, pywikibot.family.Family):
+ if family is not None and not isinstance(family,
+ pywikibot.family.Family):
family = pywikibot.site.Family(family)
for dbname in self.sitelinks:
- pg = Page(pywikibot.site.APISite.fromDBName(dbname), self.sitelinks[dbname])
+ pg = Page(pywikibot.site.APISite.fromDBName(dbname),
+ self.sitelinks[dbname])
if family is None or family == pg.site.family:
yield pg
@@ -2779,14 +2794,16 @@
if claim.getSnakType() == 'value':
if claim.getType() == 'wikibase-item':
claim.target = ItemPage(site, 'Q' +
- str(data['mainsnak']['datavalue']['value']['numeric-id']))
+ str(data['mainsnak']['datavalue']
+ ['value']['numeric-id']))
elif claim.getType() == 'commonsMedia':
claim.target = ImagePage(site.image_repository(), 'File:' +
- data['mainsnak']['datavalue']['value'])
+ data['mainsnak']['datavalue']['value'])
elif claim.getType() == 'globecoordinate':
- claim.target = pywikibot.Coordinate.fromWikibase(data['mainsnak']['datavalue']['value'], site)
+ claim.target = pywikibot.Coordinate.fromWikibase(
+ data['mainsnak']['datavalue']['value'], site)
else:
- #This covers string, url types
+ # This covers string, url types
claim.target = data['mainsnak']['datavalue']['value']
if 'rank' in data: # References/Qualifiers don't have ranks
claim.rank = data['rank']
@@ -2798,7 +2815,6 @@
for qualifier in data['qualifiers'][prop]:
qual = Claim.qualifierFromJSON(site, qualifier)
claim.qualifiers[prop].append(qual)
-
return claim
@staticmethod
@@ -2811,7 +2827,8 @@
source = collections.defaultdict(list)
for prop in data['snaks'].values():
for claimsnak in prop:
- claim = Claim.fromJSON(site, {'mainsnak': claimsnak, 'hash': data['hash']})
+ claim = Claim.fromJSON(site, {'mainsnak': claimsnak,
+ 'hash': data['hash']})
source[claim.getID()].append(claim)
return source
@@ -2872,7 +2889,8 @@
if value in ['value', 'somevalue', 'novalue']:
self.snaktype = value
else:
- raise ValueError("snaktype must be 'value', 'somevalue', or 'novalue'.")
+ raise ValueError(
+ "snaktype must be 'value', 'somevalue', or 'novalue'.")
def getRank(self):
return self.rank
@@ -2945,7 +2963,8 @@
elif self.getType() == 'globecoordinate':
value = self.getTarget().toWikibase()
else:
- raise NotImplementedError('%s datatype is not supported yet.' % self.getType())
+ raise NotImplementedError('%s datatype is not supported yet.'
+ % self.getType())
return value
@@ -3053,7 +3072,8 @@
# This code was adapted from Title.php : secureAndSplit()
#
if u'\ufffd' in t:
- raise pywikibot.Error("Title contains illegal char (\\uFFFD 'REPLACEMENT CHARACTER')")
+ raise pywikibot.Error(
+ "Title contains illegal char (\\uFFFD 'REPLACEMENT CHARACTER')")
# Replace underscores by spaces
t = t.replace(u"_", u" ")
--
To view, visit https://gerrit.wikimedia.org/r/94599
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8eb71790f3c762feee1ff7d86fa91eed3be72b32
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Pyfisch <pyfisch(a)googlemail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot
jenkins-bot has submitted this change and it was merged.
Change subject: (Bug 56347) Enable RandomPageGenerator() and site.randompages returning more than 10 pages
......................................................................
(Bug 56347) Enable RandomPageGenerator() and site.randompages returning more than 10 pages
API random list does not return query-continue.
We loop the query until we have our total limit exceeded.
It is on programmers side to ensure that
duplicate pages are filtered
(e.g. via DuplicateFilterPageGenerator())
Change-Id: I496e661486afebf5dc681036c73a454c1b4ff367
---
M pywikibot/data/api.py
1 file changed, 4 insertions(+), 0 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 5f36019..9e549c4 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -707,6 +707,10 @@
count += 1
if self.limit > 0 and count >= self.limit:
return
+ if self.module == "random" and self.limit:
+ # "random" module does not return "query-continue"
+ # now we loop for a new random query
+ continue
if not "query-continue" in self.data:
return
if not self.continuekey in self.data["query-continue"]:
--
To view, visit https://gerrit.wikimedia.org/r/93167
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I496e661486afebf5dc681036c73a454c1b4ff367
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot