Page MenuHomePhabricator
Paste P92102

(An Untitled Masterwork)
ActivePublic

Authored by Ladsgroup on Apr 30 2026, 4:09 PM.
Project Tags
None
Referenced Files
F78894604: raw-paste-data.txt
Apr 30 2026, 4:09 PM
Subscribers
None
import pywikibot
import re
from bs4 import BeautifulSoup
from pywikibot import pagegenerators
def convert_html_links_to_wikitext(html_content):
"""Parses HTML and converts <a> tags to [[Wikitext]] links."""
soup = BeautifulSoup(html_content, 'html.parser')
for a in soup.find_all('a'):
# Extract the page title from the 'title' attribute or 'href'
# MediaWiki links usually have a title attribute with the page name
link_target = a.get('title')
link_text = a.get_text()
if link_target:
# Format as [[Target|Text]] or just [[Target]] if they match
new_link = f"[[{link_target}|{link_text}]]" if link_target != link_text else f"[[{link_target}]]"
a.replace_with(new_link)
else:
# Fallback for links without titles (like external links)
href = a.get('href')
if href:
a.replace_with(f"[{href} {link_text}]")
# Get the text back. Use 'decode_contents' to avoid adding <html><body> wrappers
return soup.get_text()
def process_category():
site = pywikibot.Site('it', 'wikinews')
gen = pagegenerators.SearchPageGenerator(
query='insource:"DynamicPageList"',
site=site,
namespaces=list(site.namespaces.keys()),
total=None
)
# Regex to find DynamicPageList tags (case insensitive)
dpl_pattern = re.compile(r'<DynamicPageList>(.*?)</DynamicPageList>', re.DOTALL | re.IGNORECASE)
for page in gen:
if 'Main page' in page.title().replace('_', ' '):
continue
if 'Newsroom' in page.title():
continue
if 'Water cooler' in page.title().replace('_', ' '):
continue
text = page.text
matches = dpl_pattern.findall(text)
if not matches:
continue
pywikibot.output(f"\n>>> Processing {page.title()} <<<")
new_text = text
for dpl_content in matches:
if '{' in dpl_content:
continue
pywikibot.output("Found DPL content. Sending to API for parsing...")
# 1. Send DPL content to the Parsing API
params = {
'action': 'parse',
'text': f'<DynamicPageList>{dpl_content}</DynamicPageList>',
'contentmodel': 'wikitext',
'disablelimitreport': True,
}
try:
request = site.simple_request(**params)
data = request.submit()
html_output = data['parse']['text']['*']
# 2. Convert HTML links to Wikitext
static_wikitext = convert_html_links_to_wikitext(html_output).strip()
if static_wikitext.count('\n'):
static_wikitext = '*' + '\n*'.join(static_wikitext.split('\n'))
if static_wikitext.count('[[') == 1:
static_wikitext = '*' + static_wikitext
print(static_wikitext)
# 3. Replace the original tag with the new content
# We use a literal replace to avoid regex collision with the parsed content
full_tag = f"<DynamicPageList>{dpl_content}</DynamicPageList>"
new_text = new_text.replace(full_tag, static_wikitext.strip())
except Exception as e:
pywikibot.error(f"Failed to parse DPL on {page.title()}: {e}")
#if new_text != text:
page.text = new_text
try:
page.save(summary="Replacing DynamicPageList with static wikitext ([[phab:T421796]]).")
except:
continue
if __name__ == "__main__":
process_category()