def getTemplateTitlesFromPage(siteSrc, nbPages, iTitles): (i, title) = iTitles p = Page(siteSrc, title) if (p.is_filepage()): # check if the file is in a specific file repository f = FilePage(siteSrc.image_repository(), title) if (f.exists()): # get template on page in specific file repository tplt = f.templates() else: # get templates from original site tplt = p.templates() else: # get templates tplt = p.templates() nbTplt = len(tplt) if (nbTplt > 0): log("%i/%i Process %s : %i templates found " % (i + 1, nbPages, title, nbTplt)) else: log("%i/%i Process %s :no templates found " % (i + 1, nbPages, title)) return mapTitle(tplt)
def find_sub_templates( lookingfor: str, page: Page, wholeword: bool, matchcase: bool ): found_templates = [] if page.isRedirectPage(): page = page.getRedirectTarget() pagetext = page.text if not matchcase: pagetext = pagetext.lower() lookingfor = lookingfor.lower() if wholeword: pattern = re.compile(r'\b' + re.escape(lookingfor) + r'\b') if pattern.search(pagetext): found_templates.append(page) elif lookingfor in pagetext: found_templates.append(page) for sub_template in page.templates(content=True): if sub_template.isRedirectPage(): sub_template = sub_template.getRedirectTarget() text = sub_template.text if matchcase else sub_template.text.lower() if wholeword: # noinspection PyUnboundLocalVariable if pattern.search(text): found_templates.append(sub_template) elif lookingfor in text: found_templates.append(sub_template) # Remove duplicate templates return {f.title(): f for f in found_templates}.values()
def has_template( page: pywikibot.Page, templates: Union[str, Iterable[Union[pywikibot.Page, str]]], ) -> bool: """ Return True if the page has one of the templates. False otherwise. @param page: page to check @param templates: templates to check """ if isinstance(templates, str): templates = [templates] templates = get_redirects( tpl if isinstance(tpl, pywikibot.Page ) else pywikibot.Page(page.site, tpl, ns=10) for tpl in templates) return bool(templates & set(page.templates()))