Xqt has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1240880?usp=email )
Change subject: pagegenerators: Support namespaces parameter for all category
generators
......................................................................
pagegenerators: Support namespaces parameter for all category generators
- Add namespaces parameter to SubCategoriesPageGenerator
- use namespaces parameter in getCategoryGen which either calls
CategorizedPageGenerator or SubCategoriesPageGenerator.
This ensures that namespace filtering is applied consistently when using
any category option.
Inspired by T417961: using -catr together with -ns:0 now correctly
limits results to main namespace pages and avoids yielding FilePage
objects.
Bug: T417961
Change-Id: I54f9b1f67dbfa81cd7a6194add8686b37a492fa4
---
M pywikibot/pagegenerators/_factory.py
M pywikibot/pagegenerators/_generators.py
2 files changed, 44 insertions(+), 13 deletions(-)
Approvals:
Xqt: Verified; Looks good to me, approved
diff --git a/pywikibot/pagegenerators/_factory.py
b/pywikibot/pagegenerators/_factory.py
index c9ed4b3..caacf4e 100644
--- a/pywikibot/pagegenerators/_factory.py
+++ b/pywikibot/pagegenerators/_factory.py
@@ -196,6 +196,11 @@
self.site.namespaces.resolve(self._namespaces))
return self._namespaces
+ @namespaces.deleter
+ def namespaces(self) -> None:
+ """Deleter of namespaces property."""
+ self._namespaces = frozenset()
+
def getCombinedGenerator(self, # noqa: N802
gen: OPT_GENERATOR_TYPE = None,
preload: bool = False) -> OPT_GENERATOR_TYPE:
@@ -346,6 +351,11 @@
gen_func: Callable | None = None) -> Any:
"""Return generator based on Category defined by category and gen_func.
+ .. versionchanged::11.1
+ *gen_func* is now called with the ``namespaces`` parameter
+ using the value from :attr:`namespaces`, because the namespace
+ option is prioritized in :meth:`handle_args`.
+
:param category: Category name with start parameter
:param recurse: If not False or 0, also iterate articles in
subcategories. If an int, limit recursion to this number of
@@ -359,10 +369,17 @@
cat, startfrom = self.getCategory(category)
- return gen_func(cat,
- start=startfrom,
- recurse=recurse,
- content=content)
+ ns = self.namespaces or None
+ # reset namespaces property to avoid filtering by getCombinedGenerator
+ del self.namespaces
+
+ return gen_func(
+ cat,
+ start=startfrom,
+ recurse=recurse,
+ content=content,
+ namespaces=ns
+ )
@staticmethod
def _parse_log_events(
diff --git a/pywikibot/pagegenerators/_generators.py
b/pywikibot/pagegenerators/_generators.py
index 8ef461b..6edc8ed 100644
--- a/pywikibot/pagegenerators/_generators.py
+++ b/pywikibot/pagegenerators/_generators.py
@@ -338,6 +338,7 @@
) -> Generator[pywikibot.page.Page]:
"""Yield all pages in a specific category.
+ :param category: The Category object to generate subcategories from
:param recurse: If not False or 0, also iterate articles in
subcategories. If an int, limit recursion to this number of
levels, e.g. recurse=1 will iterate articles in first-level
@@ -348,6 +349,8 @@
all levels)
:param content: If True, retrieve the content of the current version
of each page (default False)
+ :param namespaces: List of namespaces to search in (default is None,
+ meaning all namespaces)
"""
yield from category.articles(
content=content,
@@ -358,14 +361,19 @@
)
-def SubCategoriesPageGenerator(category: pywikibot.page.Category,
- recurse: int | bool = False,
- start: str | None = None,
- total: int | None = None,
- content: bool = False,
- ) -> Generator[pywikibot.page.Page]:
+def SubCategoriesPageGenerator(
+ category: pywikibot.page.Category,
+ recurse: int | bool = False,
+ start: str | None = None,
+ total: int | None = None,
+ content: bool = False,
+ namespaces: NamespaceArgType = None,
+) -> Generator[pywikibot.page.Page]:
"""Yield all subcategories in a specific category.
+ .. versionchanged:: 11.1
+ *namespaces* parameter was added
+
:param category: The Category object to generate subcategories from
:param recurse: If not False or 0, also iterate articles in
subcategories. If an int, limit recursion to this number of
@@ -377,10 +385,16 @@
all levels)
:param content: If True, retrieve the content of the current version
of each page (default False)
+ :param namespaces: List of namespaces to search in (default is None,
+ meaning all namespaces)
"""
- return category.subcategories(recurse=recurse,
- total=total, content=content,
- startprefix=start)
+ return category.subcategories(
+ recurse=recurse,
+ total=total,
+ content=content,
+ startprefix=start,
+ namespaces=namespaces
+ )
def LinkedPageGenerator(
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1240880?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I54f9b1f67dbfa81cd7a6194add8686b37a492fa4
Gerrit-Change-Number: 1240880
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Matěj Suchánek <[email protected]>
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]