def test_pageprops(self, mock_site): # Check that variouse page props are read correctly from API response title = 'Some page' mock_site.get.return_value = { 'query': { 'pages': { '728': { 'contentmodel': 'wikitext', 'counter': '', 'lastrevid': 13355471, 'length': 58487, 'ns': 0, 'pageid': 728, 'pagelanguage': 'nb', 'protection': [], 'title': title, 'touched': '2014-09-14T21:11:52Z' } } } } page = Page(mock_site, title) assert page.exists is True assert page.redirect is False assert page.revision == 13355471 assert page.length == 58487 assert page.namespace == 0 assert page.name == title assert page.page_title == title
def test_captcha(self, mock_site): # Check that Captcha results in EditError mock_site.blocked = False mock_site.rights = ['read', 'edit'] title = 'Norge' mock_site.get.return_value = { 'query': { 'pages': { '728': { 'protection': [] } } } } page = Page(mock_site, title) mock_site.post.return_value = { 'edit': { 'result': 'Failure', 'captcha': { 'type': 'math', 'mime': 'text/tex', 'id': '509895952', 'question': '36 + 4 = ' } } } # For now, mwclient will just raise an EditError. # <https://github.com/mwclient/mwclient/issues/33> with pytest.raises(mwclient.errors.EditError): page.save('Some text')
def test_redirect(self, mock_site): # Check that page.redirect is set correctly title = 'Some redirect page' mock_site.get.return_value = { "query": { "pages": { "796917": { "contentmodel": "wikitext", "counter": "", "lastrevid": 9342494, "length": 70, "ns": 0, "pageid": 796917, "pagelanguage": "nb", "protection": [], "redirect": "", "title": title, "touched": "2014-08-29T22:25:15Z" } } } } page = Page(mock_site, title) assert page.exists is True assert page.redirect is True
def test_existing_page(self, mock_site): # Check that API response results in page.exists being set to True title = 'Norge' mock_site.get.return_value = {'query': {'pages': {'728': {}}}} page = Page(mock_site, title) assert page.exists is True
def process(self, article): page = Page(self.client, article) wikitext = page.text() matches = re.search( r"\[\[((?:[^\]|$]+)(?=.png|.jpg|.jpeg|.gif|.svg).png|.jpg|.jpeg|.gif|svg)(?:|[^\]$]+)\]\]", wikitext) if matches: for match in matches.groups(): self.file.write(match + "\n")
def process(self, article): page = Page(self.client, article) if not page.redirect: return self.logger.info(self.lang.t("scripts.cleanupdoubleredirects.processing").format(name=article)) redirection_chain = [] while page.redirect: if page.name in redirection_chain: self.logger.warning(self.lang.t("cleanupdoubleredirects.circular_reference").format(name=article)) return redirection_chain.append(page.name) page = Page(self.client, page.redirects_to()) if len(redirection_chain) > 1: for redirect in redirection_chain[:-1]: self.logger.info(self.lang.t("resolving").format(redirect=redirect, target=page.name)) redir = Page(self.client, redirect) redir.edit("#REDIRECT [[" + page.name + "]]", self.lang.t("scripts.cleanupdoubleredirects.reason") .format(target=page.name))
def test_nonexisting_page(self, mock_site): # Check that API response results in page.exists being set to False title = 'Some nonexisting page' mock_site.api.return_value = { 'query': {'pages': {'-1': {'missing': ''}}} } page = Page(mock_site, title) assert page.exists is False
def process(self, page_name): page = Page(self.client, page_name) wikitext = page.text() if BEGINNING: wikitext = TEMPLATE + "\n" + wikitext else: wikitext = wikitext + "\n" + TEMPLATE page.edit(wikitext, summary=self.lang.t("scripts.addtemplate.summary").format( template=TEMPLATE))
def test_api_call_on_page_init(self, mock_site): # Check that site.api() is called once on Page init title = 'Some page' mock_site.api.return_value = { 'query': {'pages': {'1': {}}} } page = Page(mock_site, title) # test that Page called site.api with the right parameters mock_site.api.assert_called_once_with('query', inprop='protection', titles=title, prop='info')
def setUp(self): title = 'Some page' self.page_text = 'Hello world' MockSite = mock.patch('mwclient.client.Site').start() self.site = MockSite() self.site.api.return_value = {'query': {'pages': {'1': {'title': title}}}} self.site.rights = ['read'] self.page = Page(self.site, title) self.site.api.return_value = {'query': {'pages': {'2': { 'ns': 0, 'pageid': 2, 'revisions': [{'*': 'Hello world', 'timestamp': '2014-08-29T22:25:15Z'}], 'title': title }}}}
def save_changes(self): if self.edited: self.logger.info( self.lang.t("interwiki.saving").format(name=self.page.name)) self.page.edit(self.wikitext, summary=self.generate_summary()) self.created = [] self.deleted = [] self.edited = False self.page = Page(self.page.site, self.page.name) self.wikitext = self.page.text() self.interwiki_links = dict() for lang, link in self.page.langlinks(): if lang not in self.interwiki_links: self.interwiki_links[lang] = [link] else: self.interwiki_links[lang].append(link)
def test_invalid_title(self, mock_site): # Check that API page.exists is False for invalid title title = '[Test]' mock_site.get.return_value = { "query": { "pages": { "-1": { "title": "[Test]", "invalidreason": "The requested page title contains invalid characters: \"[\".", "invalid": "" } } } } with pytest.raises(InvalidPageTitle): page = Page(mock_site, title)
def test_protection_levels(self, mock_site): # If page is protected, check that protection is parsed correctly title = 'Some page' mock_site.get.return_value = { 'query': { 'pages': { '728': { 'protection': [{ 'expiry': 'infinity', 'level': 'autoconfirmed', 'type': 'edit' }, { 'expiry': 'infinity', 'level': 'sysop', 'type': 'move' }] } } } } mock_site.rights = ['read', 'edit', 'move'] page = Page(mock_site, title) assert page.protection == { 'edit': ('autoconfirmed', 'infinity'), 'move': ('sysop', 'infinity') } assert page.can('read') is True assert page.can( 'edit') is False # User does not have 'autoconfirmed' right assert page.can('move') is False # User does not have 'sysop' right mock_site.rights = ['read', 'edit', 'move', 'autoconfirmed'] assert page.can('edit') is True # User has 'autoconfirmed' right assert page.can('move') is False # User doesn't have 'sysop' right mock_site.rights = [ 'read', 'edit', 'move', 'autoconfirmed', 'editprotected' ] assert page.can('edit') is True # User has 'autoconfirmed' right assert page.can('move') is True # User has 'sysop' right
def remap_interwikis(self, name): if not self.interwikis: raise ValueError("call bootstrap first!") page = Page(self.client, name) if page.redirect: return self.remap_interwikis(page.resolve_redirect().name) self.logger.info(self.lang.t("interwiki.remapping").format(name=name)) task = InterwikiTask(page, self.client, self.interwikis, self.logger, self.lang) task.clean_broken_interwikis() missing = list(task.missing_interwikis()) for language in missing: self.logger.debug( self.lang.t("interwiki.locating").format(lang=language)) for strategy in self.strategies: self.logger.debug( self.lang.t("interwiki.attempting_strategy").format( strategy=strategy.get_name())) resultado = strategy.run(task, language) if resultado is not None: break task.save_changes()
def find_article_in_wikipedia(self, nombre) -> Optional[Page]: wp_article = Page(self.wp_client, nombre) if wp_article.redirect: wp_article = wp_article.resolve_redirect() return wp_article if wp_article.exists else None
def process(self, article): page = Page(self.client, article) page.purge() self.logger.info(self.lang.t("common.processed").format(name=article))