Wikidata:Requests for permissions/Bot/TolBot 14
From Wikidata
Jump to navigation
Jump to search
- The following discussion is closed. Please do not modify it. Subsequent comments should be made in a new section. A summary of the conclusions reached follows.
- Approved--Ymblanter (talk) 18:45, 18 May 2022 (UTC)[reply]
TolBot (talk • contribs • new items • new lexemes • SUL • Block log • User rights log • User rights • xtools)
Operator: Tol (talk • contribs • logs)
Task/s: Archives Wikidata:Requests for deletions
Code: Using MisterSynergy's code below:
Code |
---|
from time import strftime
import re
from typing import Optional
import mwparserfromhell
import pywikibot as pwb
### configuration ###
ARCHIVE_TEMPLATES = [ 'deleted', 'not deleted', 'merged', 'done', 'not done', 'notdone' ]
CURRENT_RFD_PAGE = 'Wikidata:Requests for deletions'
ARCHIVE_PAGE = f'Wikidata:Requests for deletions/Archive/{strftime("%Y/%m/%d")}'
def eval_body_parts(body:list) -> tuple:
part_headline = re.compile(r'^==[^=]') # second-level headline
kept_parts = []
archived_parts = []
for i, part in enumerate(body, start=1):
keep = True
wrong_part_level = False
has_child_parts = False
# always keep parts that are not second-level headlines (i.e. not "== part ==")
if part_headline.search(part.title) is None:
wrong_part_level = True
# always keep parts that have child parts (i.e. with third-level parts as childs)
if i < len(body):
if part_headline.search(part.title) is not None and body[i].title[:3]=='===':
has_child_parts = True
# check whether one of the archive_templates is found in the part
for template in mwparserfromhell.parse(part.content).filter_templates():
if template.name.lower() in ARCHIVE_TEMPLATES:
keep = False
break
if keep is True or wrong_part_level is True or has_child_parts is True:
kept_parts.append(part)
else:
archived_parts.append(part)
return kept_parts, archived_parts
def new_rfd_text(header:str, footer:str, parts:list) -> str:
wikitext = header
for part in parts:
wikitext += part.title
wikitext += part.content
wikitext += footer
return wikitext
def new_rfd_archive_text(archive:pwb.Page, parts:list) -> str:
if archive.exists() is False:
wikitext = '{{Archive|category=Archived requests for deletion}}\n\n'
else:
wikitext = archive.text + '\n\n'
for part in parts:
wikitext += part.title
wikitext += part.content
return wikitext
def print_log(page:pwb.page.BasePage, err:Optional[Exception]):
if err is None:
print(f'Made edit to page "{page.title()}"')
else:
print(f'An exception occurred while saving page "{page.title()}": {err}')
def print_output(body:list, kept_parts:list, \
archived_parts:list, archive_exists:bool) -> None:
print(f'== {strftime("%Y-%m-%d, %H:%M:%S")} ==')
print(f'Archive: "{ARCHIVE_PAGE}"')
print(f'Templates that trigger archiving: {", ".join(ARCHIVE_TEMPLATES)}\n')
print(f'Total parts: {len(body)}')
print(f'Kept parts: {len(kept_parts)}')
print(f'Archived parts: {len(archived_parts)}\n')
if archive_exists is False:
print(f'Created new archive page "{ARCHIVE_PAGE}"\n')
def main() -> None:
### basic variables ###
site = pwb.Site('wikidata', 'wikidata')
site.login()
site.throttle.setDelays(0, 0, True)
### actual processing ###
rfd = pwb.Page(site, CURRENT_RFD_PAGE)
rfd_archive = pwb.Page(site, ARCHIVE_PAGE)
sections = pwb.textlib.extract_sections(rfd.get(), site)
header:str = sections[0]
body:list[pwb.textlib._Section] = sections[1] # list of namedtuples
footer:str = sections[2]
kept_parts, archived_parts = eval_body_parts(body)
rfd.text = new_rfd_text(header, footer, kept_parts)
rfd_archive.text = new_rfd_archive_text(rfd_archive, archived_parts)
### some output ###
print_output(body, kept_parts, archived_parts, rfd_archive.exists())
### save or show changes ###
if len(archived_parts) == 0:
print('Nothing to do.')
return
rfd_archive.save(
summary=f'Archived {len(archived_parts)} sections from [[{CURRENT_RFD_PAGE}]]',
minor=False,
quiet=True,
callback=print_log
)
rfd.save(
summary=f'Archive {len(archived_parts)} sections to [[{ARCHIVE_PAGE}]]',
minor=False,
quiet=True,
callback=print_log
)
if __name__=='__main__':
main()
|
Function details: Parses sections using mwparserfromhell
, uses pywikibot
to remove the ones with a template indicating they are resolved (configured in ARCHIVE_TEMPLATES
) and adds them to the daily archive subpage.
Tol (talk | contribs) @ 14:09, 25 April 2022 (UTC)[reply]
- This request is because MsynBot's task 6 (RfP) has recently started acting up. There's been some discussion at this topic on MisterSynergy's talk page. Tol (talk | contribs) @ 14:13, 25 April 2022 (UTC)[reply]
- Please make some test edits Ymblanter (talk) 19:19, 5 May 2022 (UTC)[reply]
- Edits: to RfD and to archive. Tol (talk | contribs) @ 02:13, 12 May 2022 (UTC)[reply]
- Please make some test edits Ymblanter (talk) 19:19, 5 May 2022 (UTC)[reply]