jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/336886 )
Change subject: Update internals after redirecting items
......................................................................
Update internals after redirecting items
Change-Id: I9c458363b976e987a0e9b60e192b84e51b4999ea
---
M pywikibot/page.py
1 file changed, 7 insertions(+), 2 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py
index e44f393..a091956 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -4466,6 +4466,7 @@
item.latest_revision_id = data['to']['lastrevid']
if data.get('redirected', 0):
self._isredir = True
+ self._redirtarget = item
def set_redirect_target(self, target_page, create=False, force=False,
keep_section=False, save=True, **kwargs):
@@ -4475,7 +4476,7 @@
You need to define an extra argument to make this work, like save=True
@param target_page: target of the redirect, this argument is required.
- @type target_page: pywikibot.Item or string
+ @type target_page: ItemPage or string
@param force: if true, it sets the redirect target even the page
is not redirect.
@type force: bool
@@ -4488,8 +4489,12 @@
raise pywikibot.IsNotRedirectPage(self)
if not save or keep_section or create:
raise NotImplementedError
- self.repo.set_redirect_target(
+ data = self.repo.set_redirect_target(
from_item=self, to_item=target_page)
+ if data.get('success', 0):
+ del self.latest_revision_id
+ self._isredir = True
+ self._redirtarget = target_page
def isRedirectPage(self):
"""Return True if item is a redirect, False if not or not existing."""
--
To view, visit https://gerrit.wikimedia.org/r/336886
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I9c458363b976e987a0e9b60e192b84e51b4999ea
Gerrit-Change-Number: 336886
Gerrit-PatchSet: 7
Gerrit-Owner: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Multichill <maarten(a)mdammers.nl>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/407186 )
Change subject: [IMPR] Make page.py skip pages with in use templates
......................................................................
[IMPR] Make page.py skip pages with in use templates
Bug: T186170
Change-Id: I2316b56ee7922a34754fab6d2d7b34730e2b3a1c
---
M pywikibot/families/wikipedia_family.py
M pywikibot/family.py
M pywikibot/page.py
M tests/page_tests.py
4 files changed, 34 insertions(+), 5 deletions(-)
Approvals:
Framawiki: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/families/wikipedia_family.py b/pywikibot/families/wikipedia_family.py
index a3c17df..f6707e4 100644
--- a/pywikibot/families/wikipedia_family.py
+++ b/pywikibot/families/wikipedia_family.py
@@ -198,6 +198,19 @@
'uk': (u'/Документація', ),
}
+ # Templates that indicate an edit should be avoided
+ self.edit_restricted_templates = {
+ 'ar': ('تحرر',),
+ 'bs': ('Izmjena u toku',),
+ 'cs': ('Pracuje se',),
+ 'de': ('Inuse', 'In use', 'In bearbeitung', 'Inbearbeitung',),
+ 'en': ('Inuse', 'In use'),
+ 'fa': ('ویرایش',),
+ 'hr': ('Radovi',),
+ 'sr': ('Радови у току', 'Рут',),
+ 'zh': ('Inuse',),
+ }
+
def get_known_families(self, site):
"""Override the family interwiki prefixes for each site."""
# In Swedish Wikipedia 's:' is part of page title not a family
diff --git a/pywikibot/family.py b/pywikibot/family.py
index a4c49f0..4be15e5 100644
--- a/pywikibot/family.py
+++ b/pywikibot/family.py
@@ -728,6 +728,10 @@
'_default': []
}
+ # A dict of tuples for different sites with names of templates
+ # that indicate an edit should be avoided
+ self.edit_restricted_templates = {}
+
# A list of projects that share cross-project sessions.
if not hasattr(self, 'cross_projects'):
self.cross_projects = []
diff --git a/pywikibot/page.py b/pywikibot/page.py
index b2f1888..68b71b5 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -1145,9 +1145,10 @@
"""
Determine whether the active bot is allowed to edit the page.
- This will be True if the page doesn't contain {{bots}} or
- {{nobots}}, or it contains them and the active bot is allowed to
- edit this page. (This method is only useful on those sites that
+ This will be True if the page doesn't contain {{bots}} or {{nobots}}
+ or any other template from edit_restricted_templates list
+ in x_family.py file, or it contains them and the active bot is allowed
+ to edit this page. (This method is only useful on those sites that
recognize the bot-exclusion protocol; on other sites, it will always
return True.)
@@ -1175,8 +1176,13 @@
# go through all templates and look for any restriction
# multiple bots/nobots templates are allowed
+ restrictions = self.family.edit_restricted_templates.get(
+ self.site.code)
for template, params in templates:
title = template.title(withNamespace=False)
+ if restrictions:
+ if title in restrictions:
+ return False
if title == 'Nobots':
if not params:
return False
@@ -1258,7 +1264,8 @@
watch = 'unwatch'
if not force and not self.botMayEdit():
raise pywikibot.OtherPageSaveError(
- self, "Editing restricted by {{bots}} template")
+ self, 'Editing restricted by {{bots}}, {{nobots}} '
+ 'or site\'s equivalent of {{in use}} template')
self._save(summary=summary, watch=watch, minor=minor, botflag=botflag,
asynchronous=asynchronous, callback=callback,
cc=apply_cosmetic_changes, quiet=quiet, **kwargs)
diff --git a/tests/page_tests.py b/tests/page_tests.py
index 8fd2a08..b404028 100644
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Tests for the page module."""
#
-# (C) Pywikibot team, 2008-2017
+# (C) Pywikibot team, 2008-2018
#
# Distributed under the terms of the MIT license.
#
@@ -817,6 +817,11 @@
u'%s: %s but user=%s'
% (page.text, page.botMayEdit(), user))
+ # Ban all users including bots.
+ page.text = '{{in use}}'
+ page._templates = [pywikibot.Page(site, 'Template:In use')]
+ self.assertFalse(page.botMayEdit())
+
class TestPageHistory(DefaultSiteTestCase):
--
To view, visit https://gerrit.wikimedia.org/r/407186
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I2316b56ee7922a34754fab6d2d7b34730e2b3a1c
Gerrit-Change-Number: 407186
Gerrit-PatchSet: 14
Gerrit-Owner: Dvorapa <dvorapa(a)seznam.cz>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Dvorapa <dvorapa(a)seznam.cz>
Gerrit-Reviewer: Framawiki <framawiki(a)tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>
jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/342588 )
Change subject: [IMPR] Make preloading generators work with arbitrary entity types
......................................................................
[IMPR] Make preloading generators work with arbitrary entity types
Bug: T160397
Change-Id: Ie068ca3427063ff13ba4545544a1b3965ab7d88d
---
M pywikibot/pagegenerators.py
M pywikibot/site.py
M tests/site_tests.py
3 files changed, 47 insertions(+), 32 deletions(-)
Approvals:
Xqt: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index ab4ed51..429336a 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -14,7 +14,7 @@
¶ms;
"""
#
-# (C) Pywikibot team, 2008-2017
+# (C) Pywikibot team, 2008-2018
#
# Distributed under the terms of the MIT license.
#
@@ -2073,11 +2073,11 @@
@deprecated_args(step='groupsize')
-def PreloadingItemGenerator(generator, groupsize=50):
+def PreloadingEntityGenerator(generator, groupsize=50):
"""
Yield preloaded pages taken from another generator.
- Function basically is copied from above, but for ItemPage's
+ Function basically is copied from above, but for Wikibase entities.
@param generator: pages to iterate over
@param groupsize: how many pages to preload at once
@@ -2085,26 +2085,16 @@
"""
sites = {}
for page in generator:
- if not isinstance(page, pywikibot.page.WikibasePage):
- datasite = page.site.data_repository()
- if page.namespace() != datasite.item_namespace:
- pywikibot.output(
- u'PreloadingItemGenerator skipping %s as it is not in %s'
- % (page, datasite.item_namespace))
- continue
-
- page = pywikibot.ItemPage(datasite, page.title())
-
site = page.site
sites.setdefault(site, []).append(page)
if len(sites[site]) >= groupsize:
# if this site is at the groupsize, process it
group = sites.pop(site)
- for i in site.preloaditempages(group, groupsize):
+ for i in site.preload_entities(group, groupsize):
yield i
for site, pages in sites.items():
# process any leftover sites that never reached the groupsize
- for i in site.preloaditempages(pages, groupsize):
+ for i in site.preload_entities(pages, groupsize):
yield i
@@ -2982,6 +2972,8 @@
yield page
+PreloadingItemGenerator = redirect_func(PreloadingEntityGenerator,
+ old_name='PreloadingItemGenerator')
# Deprecated old names available for compatibility with compat.
ImageGenerator = redirect_func(PageClassGenerator, old_name='ImageGenerator')
FileGenerator = redirect_func(PageClassGenerator, old_name='FileGenerator')
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 1eff176..152da7c 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -6,7 +6,7 @@
groups of wikis on the same topic in different languages.
"""
#
-# (C) Pywikibot team, 2008-2017
+# (C) Pywikibot team, 2008-2018
#
# Distributed under the terms of the MIT license.
#
@@ -7351,6 +7351,10 @@
super(DataSite, self).__init__(*args, **kwargs)
self._item_namespace = None
self._property_namespace = None
+ self._type_to_class = {
+ 'item': pywikibot.ItemPage,
+ 'property': pywikibot.PropertyPage,
+ }
def _cache_entity_namespaces(self):
"""Find namespaces for each known wikibase entity type."""
@@ -7587,9 +7591,9 @@
raise api.APIError(data['errors'])
return data['entities']
- def preloaditempages(self, pagelist, groupsize=50):
+ def preload_entities(self, pagelist, groupsize=50):
"""
- Yield ItemPages with content prefilled.
+ Yield subclasses of WikibasePage's with content prefilled.
Note that pages will be iterated in a different order
than in the underlying pagelist.
@@ -7607,23 +7611,34 @@
for key in ident:
req[key].append(ident[key])
else:
- assert p.site.has_data_repository, \
- 'Site must have a data repository'
- if (p.site == p.site.data_repository() and
- p.namespace() == p.data_repository.item_namespace):
+ if p.site == self and p.namespace() in (
+ self.item_namespace, self.property_namespace):
req['ids'].append(p.title(withNamespace=False))
else:
+ assert p.site.has_data_repository, \
+ 'Site must have a data repository'
req['sites'].append(p.site.dbName())
req['titles'].append(p._link._text)
req = self._simple_request(action='wbgetentities', **req)
data = req.submit()
- for qid in data['entities']:
- item = pywikibot.ItemPage(self, qid)
- item._content = data['entities'][qid]
+ for entity in data['entities']:
+ if 'missing' in data['entities'][entity]:
+ continue
+ cls = self._type_to_class[data['entities'][entity]['type']]
+ page = cls(self, entity)
# No api call is made because item._content is given
- item.get(get_redirect=True)
- yield item
+ page._content = data['entities'][entity]
+ try:
+ page.get() # cannot provide get_redirect=True (T145971)
+ except pywikibot.IsRedirectPage:
+ pass
+ yield page
+
+ @deprecated('DataSite.preload_entities')
+ def preloaditempages(self, pagelist, groupsize=50):
+ """DEPRECATED."""
+ return self.preload_entities(pagelist, groupsize)
def getPropertyType(self, prop):
"""
diff --git a/tests/site_tests.py b/tests/site_tests.py
index f2dd8ef..81700ca 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -3007,7 +3007,7 @@
class TestDataSitePreloading(WikidataTestCase):
- """Test DataSite.preloaditempages for repo pages."""
+ """Test DataSite.preload_entities for repo pages."""
def test_item(self):
"""Test that ItemPage preloading works for Item objects."""
@@ -3016,7 +3016,7 @@
for num in range(1, 6)]
seen = []
- for item in datasite.preloaditempages(items):
+ for item in datasite.preload_entities(items):
self.assertIsInstance(item, pywikibot.ItemPage)
self.assertTrue(hasattr(item, '_content'))
self.assertNotIn(item, seen)
@@ -3031,24 +3031,32 @@
for num in range(1, 6)]
seen = []
- for item in datasite.preloaditempages(pages):
+ for item in datasite.preload_entities(pages):
self.assertIsInstance(item, pywikibot.ItemPage)
self.assertTrue(hasattr(item, '_content'))
self.assertNotIn(item, seen)
seen.append(item)
self.assertEqual(len(seen), 5)
+ def test_property(self):
+ """Test that preloading works for properties."""
+ datasite = self.get_repo()
+ page = pywikibot.Page(datasite, 'P6')
+ property_page = next(datasite.preload_entities([page]))
+ self.assertIsInstance(property_page, pywikibot.PropertyPage)
+ self.assertTrue(hasattr(property_page, '_content'))
+
class TestDataSiteClientPreloading(DefaultWikidataClientTestCase):
- """Test DataSite.preloaditempages for client pages."""
+ """Test DataSite.preload_entities for client pages."""
def test_non_item(self):
"""Test that ItemPage preloading works with Page generator."""
mainpage = self.get_mainpage()
datasite = self.get_repo()
- item = next(datasite.preloaditempages([mainpage]))
+ item = next(datasite.preload_entities([mainpage]))
self.assertIsInstance(item, pywikibot.ItemPage)
self.assertTrue(hasattr(item, '_content'))
self.assertEqual(item.id, 'Q5296')
--
To view, visit https://gerrit.wikimedia.org/r/342588
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie068ca3427063ff13ba4545544a1b3965ab7d88d
Gerrit-Change-Number: 342588
Gerrit-PatchSet: 9
Gerrit-Owner: Matěj Suchánek <matejsuchanek97(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Multichill <maarten(a)mdammers.nl>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>