Skip to content

Commit

Permalink
Merge pull request #92 from xxyzz/cite_book
Browse files Browse the repository at this point in the history
Fix infinite recursion caused by "Template:cite-book"
  • Loading branch information
kristian-clausal authored Aug 29, 2023
2 parents bbac485 + 070ee9f commit 276d4ab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
9 changes: 9 additions & 0 deletions tests/test_wikiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -4155,6 +4155,15 @@ def test_analyze_used_pre_expand_template(self) -> None:
page = self.ctx.get_page("Template:En-nm", 10)
self.assertTrue(page.need_pre_expand)

def test_get_page_resolve_redirect_infinite_recursion(self):
self.ctx.add_page("Template:cite-book", 10, body="cite-book")
self.ctx.add_page(
"Template:Cite-book", 10, redirect_to="Template:cite-book"
)
self.ctx.db_conn.commit()
page = self.ctx.get_page_resolve_redirect("Template:cite-book", 10)
self.assertEqual(page.body, "cite-book")


# XXX Test template_fn

Expand Down
11 changes: 7 additions & 4 deletions wikitextprocessor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1676,7 +1676,10 @@ def magic_repl(m: re.Match) -> str:

@lru_cache(maxsize=1000)
def get_page(
self, title: str, namespace_id: Optional[int] = None
self,
title: str,
namespace_id: Optional[int] = None,
no_redirect: bool = False,
) -> Optional[Page]:
# " " in Lua Module name is replaced by "_" in Wiktionary Lua code
# when call `require`
Expand Down Expand Up @@ -1720,6 +1723,8 @@ def get_page(
if namespace_id is not None:
query_str += " AND namespace_id = ?"
query_values.append(namespace_id)
if no_redirect:
query_str += " AND redirect_to IS NULL"
query_str += " LIMIT 1"
try:
for result in self.db_conn.execute(query_str, tuple(query_values)):
Expand Down Expand Up @@ -1811,9 +1816,7 @@ def get_page_resolve_redirect(
if page is None:
return None
if page.redirect_to is not None:
return self.get_page_resolve_redirect(
page.redirect_to, namespace_id
)
return self.get_page(page.redirect_to, namespace_id, True)
return page

def parse(
Expand Down

0 comments on commit 276d4ab

Please sign in to comment.