John Vandenberg has uploaded a new change for review.
https://gerrit.wikimedia.org/r/135385
Change subject: Determine entity namespaces for site using API
......................................................................
Determine entity namespaces for site using API
Remove hardcoded namespace 0 and 120, replacing with
values obtained via existing siprop=namespaces query.
Change-Id: If7ca06adbb4b9932bba0abffc7588afcb320e934
---
M pywikibot/page.py
M pywikibot/site.py
2 files changed, 35 insertions(+), 2 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/85/135385/1
diff --git a/pywikibot/page.py b/pywikibot/page.py
index 935331b..483b552 100644
--- a/pywikibot/page.py
+++ b/pywikibot/page.py
@@ -2398,6 +2398,10 @@
def __init__(self, site, title=u"", **kwargs):
if not isinstance(site, pywikibot.site.DataSite):
raise TypeError("site must be a pywikibot.site.DataSite object")
+ if 'entitytype' in kwargs:
+ entitytype = kwargs.pop('entitytype')
+ if 'ns' not in kwargs:
+ kwargs["ns"] = site.entity_namespace(entitytype)
Page.__init__(self, site, title, **kwargs)
self.repo = self.site
self._isredir = False # Wikibase pages cannot be a redirect
@@ -2601,7 +2605,7 @@
@type site: pywikibot.site.DataSite
@param title: id number of item, "Q###"
"""
- super(ItemPage, self).__init__(site, title, ns=0)
+ super(ItemPage, self).__init__(site, title, entitytype='item')
self.id = title.upper() # This might cause issues if not ns0?
@classmethod
@@ -2777,7 +2781,7 @@
@type source: pywikibot.site.DataSite
@param title: page name of property, like "Property:P##"
"""
- WikibasePage.__init__(self, source, title, ns=120)
+ WikibasePage.__init__(self, source, title, entitytype='property')
self.id = self.title(withNamespace=False).upper()
if not self.id.startswith(u'P'):
raise ValueError(u"'%s' is not a property page!" % self.title())
diff --git a/pywikibot/site.py b/pywikibot/site.py
index 9599d4c..8e69d1b 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -649,6 +649,9 @@
14: [u"Category"],
15: [u"Category talk"],
}
+
+ self._namespace_content_model_defaults = {}
+
if LV(self.family.version(self.code)) >= LV("1.14"):
self._namespaces[6] = [u"File"]
self._namespaces[7] = [u"File talk"]
@@ -1092,6 +1095,9 @@
self._namespaces[ns].insert(0, nsdata[nskey]["*"])
else:
self._namespaces[ns] = [nsdata[nskey]["*"]]
+ if "defaultcontentmodel" in nsdata[nskey]:
+ content_model = nsdata[nskey]["defaultcontentmodel"]
+ self._namespace_content_model_defaults[ns] = content_model
if 'namespacealiases' in sidata:
aliasdata = sidata['namespacealiases']
for item in aliasdata:
@@ -3549,6 +3555,13 @@
class DataSite (APISite):
+ def __init__(self, code, fam=None, user=None, sysop=None):
+ APISite.__init__(self, code, fam, user, sysop)
+ self._namespace_content_model_defaults = {
+ 0: 'wikibase-item',
+ 120: 'wikibase-property'
+ }
+
def __getattr__(self, attr):
"""Calls to methods get_info, get_sitelinks, get_aliases, get_labels,
get_descriptions, get_urls
@@ -3648,6 +3661,22 @@
item._content = data['entities'][qid]
yield item
+ def entity_namespace(self, entitytype):
+ """Provide the namespace number for an entity-type.
+
+ @param entitytype: the type of generator to construct (must be
+ a subclass of pywikibot.data.api.QueryGenerator)
+ @type entitytype: string ('item' or 'property')
+
+ """
+ if not hasattr(self, "_siteinfo"):
+ self._getsiteinfo()
+ if 'wikibase-' not in entitytype:
+ entitytype = 'wikibase-' + entitytype
+ for ns in self._namespace_content_model_defaults:
+ if self._namespace_content_model_defaults[ns] == entitytype:
+ return ns
+
def getPropertyType(self, prop):
"""
This is used specifically because we can cache
--
To view, visit https://gerrit.wikimedia.org/r/135385
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: If7ca06adbb4b9932bba0abffc7588afcb320e934
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits