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.edit('Some text')
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 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 do_recategorize(self, article: Page, additions, removals, replaces): if self.query == "": return print("Guardando...") wikitext = article.text() wikitext = wikitext.replace("[[Category:", "[[" + self.category_name) for removal in removals: wikitext = re.sub(r'\[\[' + removal + r'(?:|.*)*\]\]', '', wikitext) for addition in additions: wikitext = wikitext + "\n[[{}]]".format(addition) for replace in replaces: search, rep = replace.split("%") wikitext = re.sub(r'\[\[' + self.category_name + search + r'(?:|.*)*\]\]', '[[' + self.category_name + rep + ']]', wikitext) plainadditions = ",".join(map(lambda x: "+" + ",".join(x.split(':')[1:]), additions)) plainremovals = ",".join(map(lambda x: "-" + ",".join(x.split(':')[1:]), removals)) plainreplaces = ",".join(map(lambda x: "=>".join(x.split('%')), replaces)) query = plainadditions + "; " + plainremovals + "; " + plainreplaces article.edit(wikitext, summary=self.lang.t("scripts.recategorize.reason") + query, minor=True, bot=True)
def save(self, page: Page, text, summary=u'', minor=False, bot=True, section=None, **kwargs): """ Performs a page edit, retrying the login once if the edit fails due to the user being logged out This function hopefully makes it easy to workaround the lag and frequent login timeouts experienced on the Fandom UCP platform compared to Gamepedia Hydra. :param page: mwclient Page object :param text: as in mwclient.Page.edit :param summary: as in mwclient.Page.edit :param minor: as in mwclient.Page.edit :param bot: as in mwclient.Page.edit :param section: as in mwclient.Page.edit :param kwargs: as in mwclient.Page.edit :return: nothing """ try: page.site = self.client page.edit(text, summary=summary, minor=minor, bot=bot, section=section, **kwargs) except self.write_errors: self._retry_login_action(self._retry_save, 'edit', page=page, text=text, summary=summary, minor=minor, bot=bot, section=section, **kwargs)
class TestPageApiArgs(unittest.TestCase): def setUp(self): title = 'Some page' self.page_text = 'Hello world' MockSite = mock.patch('mwclient.client.Site').start() self.site = MockSite() self.site.get.return_value = { 'query': { 'pages': { '1': { 'title': title } } } } self.site.rights = ['read'] self.site.api_limit = 500 self.site.version = (1, 32, 0) self.page = Page(self.site, title) self.site.get.return_value = { 'query': { 'pages': { '2': { 'ns': 0, 'pageid': 2, 'revisions': [{ '*': 'Hello world', 'timestamp': '2014-08-29T22:25:15Z' }], 'title': title } } } } def get_last_api_call_args(self, http_method='POST'): if http_method == 'GET': args, kwargs = self.site.get.call_args else: args, kwargs = self.site.post.call_args action = args[0] args = args[1:] kwargs.update(args) return kwargs def tearDown(self): mock.patch.stopall() def test_get_page_text(self): # Check that page.text() works, and that a correct API call is made text = self.page.text() args = self.get_last_api_call_args(http_method='GET') assert text == self.page_text assert args == { 'prop': 'revisions', 'rvdir': 'older', 'titles': self.page.page_title, 'uselang': None, 'rvprop': 'content|timestamp', 'rvlimit': '1', 'rvslots': 'main', } def test_get_page_text_cached(self): # Check page.text() caching self.page.revisions = mock.Mock(return_value=iter([])) self.page.text() self.page.text() # When cache is hit, revisions is not, so call_count should be 1 assert self.page.revisions.call_count == 1 self.page.text(cache=False) # With cache explicitly disabled, we should hit revisions assert self.page.revisions.call_count == 2 def test_get_section_text(self): # Check that the 'rvsection' parameter is sent to the API text = self.page.text(section=0) args = self.get_last_api_call_args(http_method='GET') assert args['rvsection'] == '0' def test_get_text_expanded(self): # Check that the 'rvexpandtemplates' parameter is sent to the API text = self.page.text(expandtemplates=True) args = self.get_last_api_call_args(http_method='GET') assert self.site.expandtemplates.call_count == 1 assert args.get('rvexpandtemplates') is None def test_assertuser_true(self): # Check that assert=user is sent when force_login=True self.site.blocked = False self.site.rights = ['read', 'edit'] self.site.logged_in = True self.site.force_login = True self.site.api.return_value = {'edit': {'result': 'Ok'}} self.page.edit('Some text') args = self.get_last_api_call_args() assert args['assert'] == 'user' def test_assertuser_false(self): # Check that assert=user is not sent when force_login=False self.site.blocked = False self.site.rights = ['read', 'edit'] self.site.logged_in = False self.site.force_login = False self.site.api.return_value = {'edit': {'result': 'Ok'}} self.page.edit('Some text') args = self.get_last_api_call_args() assert 'assert' not in args def test_handle_edit_error_assertuserfailed(self): # Check that AssertUserFailedError is triggered api_error = APIError( 'assertuserfailed', 'Assertion that the user is logged in failed', 'See https://en.wikipedia.org/w/api.php for API usage') with pytest.raises(AssertUserFailedError): self.page.handle_edit_error(api_error, 'n/a') def test_handle_edit_error_protected(self): # Check that ProtectedPageError is triggered api_error = APIError( 'protectedpage', 'The "editprotected" right is required to edit this page', 'See https://en.wikipedia.org/w/api.php for API usage') with pytest.raises(ProtectedPageError) as pp_error: self.page.handle_edit_error(api_error, 'n/a') assert pp_error.value.code == 'protectedpage' assert str( pp_error.value ) == 'The "editprotected" right is required to edit this page' def test_get_page_categories(self): # Check that page.categories() works, and that a correct API call is made self.site.get.return_value = { "batchcomplete": "", "query": { "pages": { "1009371": { "pageid": 1009371, "ns": 14, "title": "Category:1879 births", }, "1005547": { "pageid": 1005547, "ns": 14, "title": "Category:1955 deaths", } } } } cats = list(self.page.categories()) args = self.get_last_api_call_args(http_method='GET') assert { 'generator': 'categories', 'titles': self.page.page_title, 'iiprop': 'timestamp|user|comment|url|size|sha1|metadata|archivename', 'inprop': 'protection', 'prop': 'info|imageinfo', 'gcllimit': repr(self.page.site.api_limit), } == args assert set([c.name for c in cats]) == set([ 'Category:1879 births', 'Category:1955 deaths', ])