WolfgangFahl / py-3rdparty-mediawiki

Wrapper for pywikibot and mwclient MediaWiki API librarties with improvements for 3rd party wikis
Apache License 2.0
4 stars 5 forks source link

refactor to have a separate edit_page_content function #106

Closed WolfgangFahl closed 1 month ago

WolfgangFahl commented 1 month ago
def edit_page_content(self, page_title: str, modify: typing.Callable[[str], str], force: bool, context: int) -> str:
        """
        Edit the content of a single page.

        Args:
        page_title (str): The title of the page to be edited
        modify (Callable[[str], str]): Function to modify the page content
        force (bool): If True, actually edit the page; if False, perform a dry run
        context (int): The number of context lines for diff

        Returns:
        str: Status of the edit operation
        """
        page_to_edit = self.toWiki.getPage(page_title)
        if not force and not page_to_edit.exists:
            return "👎"

        text = page_to_edit.text()
        new_text = modify(text)

        if new_text == text:
            return "↔"

        if force:
            comment = "edited by wikiedit"
            page_to_edit.edit(new_text, comment)
            return "✅"
        else:
            diff_str = self.getDiff(text, new_text, n=context)
            return f"👍{diff_str}"