jenkins-bot merged this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
site.categorymembers: Support cmstartsortkeyprefix API parameter

site.py:
- Fix the docstring for `startsort` and `endsort`. They are in binary
string format and cannot be compared with a title directly. Also mention
that they are deprecated in MW 1.24.
- Add two new parameters, `startprefix` and `endprefix`, to be passed to
API as `cmstartsortkeyprefix` and `cmendsortkeyprefix`.
The old `startsort` is passed to API as `cmstartsortkey` which is
deprecated and is not compatible with `cmstartsortkeyprefix`. Also
working with `startsort` is difficult as it should be a "binary string"
usually obtained via API or directly from the database. (See T74101)

page.py:
- Add the new parameters to `Category.articles`.

Bug: T170265
Change-Id: I08f4a7a4bb1bbe5c10fe678cc7a951a273f61d07
---
M pywikibot/page.py
M pywikibot/site.py
2 files changed, 64 insertions(+), 15 deletions(-)

diff --git a/pywikibot/page.py b/pywikibot/page.py
index 6b4d8cf..97f03e1 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -2802,7 +2802,9 @@
def articles(self, recurse=False, total=None,
content=False, namespaces=None, sortby=None,
reverse=False, starttime=None, endtime=None,
- startsort=None, endsort=None):
+ startsort=None, endsort=None,
+ startprefix=None, endprefix=None,
+ ):
"""
Yield all articles in the current category.

@@ -2833,12 +2835,22 @@
@param endtime: if provided, only generate pages added before this
time; not valid unless sortby="timestamp"
@type endtime: pywikibot.Timestamp
- @param startsort: if provided, only generate pages >= this title
- lexically; not valid if sortby="timestamp"
+ @param startsort: if provided, only generate pages that have a
+ sortkey >= startsort; not valid if sortby="timestamp"
+ (Deprecated in MW 1.24)
@type startsort: str
- @param endsort: if provided, only generate pages <= this title
- lexically; not valid if sortby="timestamp"
+ @param endsort: if provided, only generate pages that have a
+ sortkey <= endsort; not valid if sortby="timestamp"
+ (Deprecated in MW 1.24)
@type endsort: str
+ @param startprefix: if provided, only generate pages >= this title
+ lexically; not valid if sortby="timestamp"; overrides "startsort"
+ (requires MW 1.18+)
+ @type startprefix: str
+ @param endprefix: if provided, only generate pages < this title
+ lexically; not valid if sortby="timestamp"; overrides "endsort"
+ (requires MW 1.18+)
+ @type endprefix: str
"""
for member in self.site.categorymembers(self,
namespaces=namespaces,
@@ -2849,6 +2861,8 @@
endtime=endtime,
startsort=startsort,
endsort=endsort,
+ startprefix=startprefix,
+ endprefix=endprefix,
member_type=['page', 'file']
):
yield member
@@ -2868,7 +2882,9 @@
starttime=starttime,
endtime=endtime,
startsort=startsort,
- endsort=endsort
+ endsort=endsort,
+ startprefix=startprefix,
+ endprefix=endprefix,
):
yield article
if total is not None:
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 88f48eb..e5bc29a 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -3811,7 +3811,9 @@
def categorymembers(self, category, namespaces=None, sortby=None,
reverse=False, starttime=None, endtime=None,
startsort=None, endsort=None, total=None,
- content=False, member_type=None):
+ content=False, member_type=None,
+ startprefix=None, endprefix=None,
+ ):
"""Iterate members of specified category.

@param category: The Category to iterate.
@@ -3833,13 +3835,22 @@
@type starttime: pywikibot.Timestamp
@param endtime: if provided, only generate pages added before this
time; not valid unless sortby="timestamp"
- @type endtime: pywikibot.Timestamp
- @param startsort: if provided, only generate pages >= this title
- lexically; not valid if sortby="timestamp"
+ @param startsort: if provided, only generate pages that have a
+ sortkey >= startsort; not valid if sortby="timestamp"
+ (Deprecated in MW 1.24)
@type startsort: str
- @param endsort: if provided, only generate pages <= this title
- lexically; not valid if sortby="timestamp"
+ @param endsort: if provided, only generate pages that have a
+ sortkey <= endsort; not valid if sortby="timestamp"
+ (Deprecated in MW 1.24)
@type endsort: str
+ @param startprefix: if provided, only generate pages >= this title
+ lexically; not valid if sortby="timestamp"; overrides "startsort"
+ (requires MW 1.18+)
+ @type startprefix: str
+ @param endprefix: if provided, only generate pages < this title
+ lexically; not valid if sortby="timestamp"; overrides "endsort"
+ (requires MW 1.18+)
+ @type endprefix: str
@param content: if True, load the current content of each iterated page
(default False)
@type content: bool
@@ -3849,6 +3860,8 @@
@type member_type: str or iterable of str; values: page, subcat, file

@raises KeyError: a namespace identifier was not resolved
+ @raises NotImplementedError: startprefix or endprefix parameters are
+ given but site.version is less than 1.18.
@raises TypeError: a namespace identifier has an inappropriate
type such as NoneType or bool
"""
@@ -3868,7 +3881,10 @@
if starttime and endtime and starttime > endtime:
raise ValueError(
"categorymembers: starttime must be before endtime")
- if startsort and endsort and startsort > endsort:
+ if startprefix and endprefix and startprefix > endprefix:
+ raise ValueError(
+ 'categorymembers: startprefix must be less than endprefix')
+ elif startsort and endsort and startsort > endsort:
raise ValueError(
"categorymembers: startsort must be less than endsort")

@@ -3923,6 +3939,7 @@
# sort; we take care of this reversal for the user
(starttime, endtime) = (endtime, starttime)
(startsort, endsort) = (endsort, startsort)
+ (startprefix, endprefix) = (endprefix, startprefix)
if starttime and sortby == "timestamp":
cmargs["gcmstart"] = starttime
elif starttime:
@@ -3933,12 +3950,28 @@
elif endtime:
raise ValueError("categorymembers: "
"invalid combination of 'sortby' and 'endtime'")
- if startsort and sortby != "timestamp":
+ if startprefix and sortby != 'timestamp':
+ if self.version() < MediaWikiVersion('1.18'):
+ raise NotImplementedError(
+ 'categorymembers: "startprefix" requires MW 1.18+')
+ cmargs['gcmstartsortkeyprefix'] = startprefix
+ elif startprefix:
+ raise ValueError('categorymembers: invalid combination of '
+ "'sortby' and 'startprefix'")
+ elif startsort and sortby != 'timestamp':
cmargs["gcmstartsortkey"] = startsort
elif startsort:
raise ValueError("categorymembers: "
"invalid combination of 'sortby' and 'startsort'")
- if endsort and sortby != "timestamp":
+ if endprefix and sortby != 'timestamp':
+ if self.version() < MediaWikiVersion('1.18'):
+ raise NotImplementedError(
+ 'categorymembers: "endprefix" requires MW 1.18+')
+ cmargs['cmendsortkeyprefix'] = endprefix
+ elif endprefix:
+ raise ValueError('categorymembers: '
+ "invalid combination of 'sortby' and 'endprefix'")
+ elif endsort and sortby != 'timestamp':
cmargs["gcmendsortkey"] = endsort
elif endsort:
raise ValueError("categorymembers: "

To view, visit change 364423. To unsubscribe, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I08f4a7a4bb1bbe5c10fe678cc7a951a273f61d07
Gerrit-Change-Number: 364423
Gerrit-PatchSet: 12
Gerrit-Owner: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki@gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb@gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski@gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot <>