def fix_allpages(self): pageids = self.find_broken() if not pageids: logger.info("All pages are categorized under correct language.") return for chunk in ws.utils.iter_chunks(pageids, self.api.max_ids_per_query): pageids = "|".join(str(pageid) for pageid in chunk) result = self.api.call_api(action="query", pageids=pageids, prop="revisions", rvprop="content|timestamp") pages = result["pages"] for page in pages.values(): logger.info( "Fixing language of categories on page [[{}]]...".format( page["title"])) timestamp = page["revisions"][0]["timestamp"] text_old = page["revisions"][0]["*"] text_new = self.fix_page(page["title"], text_old) if text_old != text_new: try: edit_interactive(self.api, page["title"], page["pageid"], text_old, text_new, timestamp, self.edit_summary, bot="") # self.api.edit(page["title"], page["pageid"], text_new, timestamp, self.edit_summary, bot="") except APIError: pass
def _edit(self, title, pageid, text_new, text_old, timestamp, edit_summary): if text_old != text_new: if self.dry_run: diff = diff_highlighted(text_old, text_new, title + ".old", title + ".new", timestamp, "<utcnow>") print(diff) print("Edit summary: " + edit_summary) print("(edit discarded due to --dry-run)") else: try: if self.interactive is False: self.api.edit(title, pageid, text_new, timestamp, edit_summary, bot="") else: edit_interactive(self.api, title, pageid, text_old, text_new, timestamp, edit_summary, bot="") except APIError as e: pass
def check_allpages(self): if not self.finder.refresh(): raise Exception("Failed to refresh package information.") # ensure that we are authenticated require_login(self.api) namespaces = [0, 4, 14, 3000] for ns in namespaces: for page in self.api.generator(generator="allpages", gaplimit="100", gapfilterredir="nonredirects", gapnamespace=ns, prop="revisions", rvprop="content|timestamp", rvslots="main"): title = page["title"] if title in self.blacklist_pages: logger.info("skipping blacklisted page [[{}]]".format(title)) continue timestamp = page["revisions"][0]["timestamp"] text_old = page["revisions"][0]["slots"]["main"]["*"] text_new = self.update_page(title, text_old) if text_old != text_new: try: if self.interactive: edit_interactive(self.api, title, page["pageid"], text_old, text_new, timestamp, self.edit_summary, bot="") else: self.api.edit(title, page["pageid"], text_new, timestamp, self.edit_summary, bot="") except APIError: pass
def _edit(self, title, pageid, text_new, text_old, timestamp, edit_summary): if text_old != text_new: # print the info message print("\nSuggested edit for page [[{}]]. Please double-check all changes before accepting!".format(title)) try: # TODO: set bot="" only when the logged-in user is a bot edit_interactive(self.api, title, pageid, text_old, text_new, timestamp, edit_summary, bot="") except APIError as e: pass
def _edit(self, title, pageid, text_new, text_old, timestamp, edit_summary): if text_old == text_new: return if self.dry_run: diff = diff_highlighted(text_old, text_new, title + ".old", title + ".new", timestamp, "<utcnow>") print(diff) print("Edit summary: " + edit_summary) print("(edit discarded due to --dry-run)") return interactive = self.interactive # override interactive mode for edits which are very frequent and "always" correct if "bot" in self.api.user.rights and edit_summary == "update http to https": interactive = False try: if interactive is False: self.api.edit(title, pageid, text_new, timestamp, edit_summary, bot="") else: # print the info message print( "\nSuggested edit for page [[{}]]. Please double-check all changes before accepting!" .format(title)) if "bot" in self.api.user.rights: edit_interactive(self.api, title, pageid, text_old, text_new, timestamp, edit_summary, bot="") else: edit_interactive(self.api, title, pageid, text_old, text_new, timestamp, edit_summary) except APIError: pass
def fix_allpages(self): pageids = self.find_categorized() if not pageids: logger.info("All pages are categorized under correct language.") return for chunk in ws.utils.iter_chunks(pageids, self.api.max_ids_per_query): pageids = "|".join(str(pageid) for pageid in chunk) result = self.api.call_api(action="query", pageids=pageids, prop="revisions", rvprop="content|timestamp") pages = result["pages"] for page in pages.values(): logger.info("Decategorizing page [[{}]]...".format( page["title"])) timestamp = page["revisions"][0]["timestamp"] text_old = page["revisions"][0]["*"] text_new = self.decategorize(page["title"], text_old) if text_old != text_new: title = self.api.Title(page["title"]) if title.namespace == "User": edit_summary = "user pages shall not be categorized, see [[Help:Style#User pages]]" else: edit_summary = "talk pages should not be categorized" try: edit_interactive(self.api, page["title"], page["pageid"], text_old, text_new, timestamp, edit_summary, bot="") # self.api.edit(page["title"], page["pageid"], text_new, timestamp, edit_summary, bot="") except APIError: pass else: logger.error("Failed to decategorize page [[{}]]".format( page["title"]))
def fix_allpages(self): pageids = self.find_broken() if not pageids: logger.info("All pages are categorized under correct language.") return for chunk in ws.utils.iter_chunks(pageids, self.api.max_ids_per_query): pageids = "|".join(str(pageid) for pageid in chunk) result = self.api.call_api(action="query", pageids=pageids, prop="revisions", rvprop="content|timestamp") pages = result["pages"] for page in pages.values(): logger.info("Fixing language of categories on page [[{}]]...".format(page["title"])) timestamp = page["revisions"][0]["timestamp"] text_old = page["revisions"][0]["*"] text_new = self.fix_page(page["title"], text_old) if text_old != text_new: try: edit_interactive(self.api, page["title"], page["pageid"], text_old, text_new, timestamp, self.edit_summary, bot="") # self.api.edit(page["title"], page["pageid"], text_new, timestamp, self.edit_summary, bot="") except APIError: pass
def save(self, edit_summary, interactive=False, **kwargs): """ Saves the updated wikicode of the page to the wiki. :param str edit_summary: Summary of the change. :param bool interactive: If ``True``, calls :py:func:`ws.interactive.edit_interactive` to ask the user before making the change; otherwise calls :py:meth:`API.edit <ws.client.api.API.edit>` directly. :param kwargs: Additional keyword arguments passed to the API query. """ text_new = str(self.wikicode) if self.contents[self.title] != text_new: # use bot=1 iff it makes sense kwargs["bot"] = "1" if "bot" not in self.api.user.rights: del kwargs["bot"] if interactive is True: edit_interactive(self.api, self.title, self.pageids[self.title], self.contents[self.title], text_new, self.timestamps[self.title], edit_summary, **kwargs) else: self.api.edit(self.title, self.pageids[self.title], text_new, self.timestamps[self.title], edit_summary, **kwargs) else: logger.info("Page [[{}]] is already up to date.".format(self.title))