Skip to content

Commit

Permalink
Use title text in database to create template parent data
Browse files Browse the repository at this point in the history
Previous code adds namespace prefix to the called template name in
wikitext, this usually creates a lower case title like "Template:val".
But Lua code could call "frame:getParent():getTitle()" and compare it
with the actually page title, which might be upper case in Chinese
Wiktionary and English Wikipedia.

Example: https://en.wikipedia.org/wiki/Module:Arguments
  • Loading branch information
xxyzz committed Aug 28, 2023
1 parent 4bc34c4 commit e3614fd
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions wikitextprocessor/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,10 +1535,11 @@ def expander(arg: str):
# print("TEMPLATE_FN {}: {} {} -> {}"
# .format(template_fn, name, ht, repr(t)))
if t is None:
body: Optional[str] = self.read_by_title(
template_page = self.get_page_resolve_redirect(
name, self.NAMESPACE_DATA["Template"]["id"]
)
if body is not None:
if template_page is not None:
body = template_page.body
# XXX optimize by pre-encoding bodies during
# preprocessing
# (Each template is typically used many times)
Expand All @@ -1551,17 +1552,7 @@ def expander(arg: str):
encoded_body = expand_args(encoded_body, ht)
# Expand the body using the calling template/page
# as the parent frame for any parserfn calls
new_title = tname.strip()
for prefix in self.NAMESPACE_DATA:
if tname.startswith(prefix + ":"):
break
else:
new_title = (
self.NAMESPACE_DATA["Template"]["name"]
+ ":"
+ new_title
)
new_parent = (new_title, ht)
new_parent = (template_page.title, ht)
# print("expanding template body for {} {}"
# .format(name, ht))
# XXX no real need to expand here, it will expanded
Expand Down Expand Up @@ -1810,12 +1801,20 @@ def read_by_title(
) -> Optional[str]:
"""Reads the contents of the page. Returns None if the page does
not exist."""
page = self.get_page_resolve_redirect(title, namespace_id)
return page.body if page is not None else None

def get_page_resolve_redirect(
self, title: str, namespace_id: Optional[int] = None
) -> Optional[Page]:
page = self.get_page(title, namespace_id)
if page is None:
return None
if page.redirect_to is not None:
return self.read_by_title(page.redirect_to, namespace_id)
return page.body if page is not None else None
return self.get_page_resolve_redirect(
page.redirect_to, namespace_id
)
return page

def parse(
self,
Expand Down

0 comments on commit e3614fd

Please sign in to comment.