diff --git a/wikibaseintegrator/entities/baseentity.py b/wikibaseintegrator/entities/baseentity.py index b7857b55..eaa2be4e 100644 --- a/wikibaseintegrator/entities/baseentity.py +++ b/wikibaseintegrator/entities/baseentity.py @@ -7,7 +7,7 @@ from wikibaseintegrator import wbi_fastrun from wikibaseintegrator.datatypes import BaseDataType from wikibaseintegrator.models.claims import Claim, Claims -from wikibaseintegrator.wbi_enums import ActionIfExists +from wikibaseintegrator.wbi_enums import ActionIfExists, EntityField from wikibaseintegrator.wbi_exceptions import MissingEntityException from wikibaseintegrator.wbi_helpers import delete_page, edit_entity, mediawiki_api_call_helper from wikibaseintegrator.wbi_login import _Login @@ -197,7 +197,7 @@ def clear(self, **kwargs: Any) -> dict[str, Any]: return self._write(data={}, clear=True, **kwargs) def _write(self, data: dict | None = None, summary: str | None = None, login: _Login | None = None, allow_anonymous: bool = False, limit_claims: list[str | int] | None = None, - clear: bool = False, as_new: bool = False, is_bot: bool | None = None, **kwargs: Any) -> dict[str, Any]: + clear: bool = False, as_new: bool = False, is_bot: bool | None = None, fields_to_update: list | None | EntityField = None, **kwargs: Any) -> dict[str, Any]: """ Writes the entity JSON to the Wikibase instance and after successful write, returns the "entity" part of the response. @@ -209,13 +209,49 @@ def _write(self, data: dict | None = None, summary: str | None = None, login: _L :param clear: If set, the complete entity is emptied before proceeding. The entity will not be saved before it is filled with the "data", possibly with parts excluded. :param as_new: Write the entity as a new one :param is_bot: Add the bot flag to the query + :param field_to_update: A list or a single EntityField to update. If not set, all fields will be updated. :param kwargs: More arguments for Python requests :return: A dictionary representation of the edited Entity """ data = data or {} - if limit_claims: + if fields_to_update is not None: + if not isinstance(fields_to_update, list): + fields_to_update = [fields_to_update] + + if EntityField.ALIASES not in fields_to_update and 'aliases' in data: + del data['aliases'] + + if EntityField.CLAIMS not in fields_to_update and 'claims' in data: + del data['claims'] + + if EntityField.DESCRIPTIONS not in fields_to_update and 'descriptions' in data: + del data['descriptions'] + + if EntityField.LABELS not in fields_to_update and 'labels' in data: + del data['labels'] + + if EntityField.SITELINKS not in fields_to_update and 'sitelinks' in data: + del data['sitelinks'] + + # Lexeme-specific fields + if EntityField.LEMMAS not in fields_to_update and 'lemmas' in data: + del data['lemmas'] + + if EntityField.LEXICAL_CATEGORY not in fields_to_update and 'lexicalCategory' in data: + del data['lexicalCategory'] + + if EntityField.LANGUAGE not in fields_to_update and 'language' in data: + del data['language'] + + if EntityField.FORMS not in fields_to_update and 'forms' in data: + del data['forms'] + + if EntityField.SENSES not in fields_to_update and 'senses' in data: + del data['senses'] + + if limit_claims and 'claims' in data: new_claims = {} if not isinstance(limit_claims, list): diff --git a/wikibaseintegrator/wbi_enums.py b/wikibaseintegrator/wbi_enums.py index b238da46..d47735a9 100644 --- a/wikibaseintegrator/wbi_enums.py +++ b/wikibaseintegrator/wbi_enums.py @@ -74,3 +74,24 @@ class WikibaseTimePrecision(Enum): TEN_MILLION_YEARS = 2 HUNDRED_MILLION_YEARS = 1 BILLION_YEARS = 0 + +class EntityField(Enum): + """ + The different fields of an entity. + Used to specify which field to update when updating an entity. + """ + # BaseEntity field + CLAIMS = auto() + + # Item fields (and partly MediaInfo and Property fields) + ALIASES = auto() + DESCRIPTIONS = auto() + LABELS = auto() + SITELINKS = auto() + + # Lexeme fields + LEMMAS = auto() + LEXICAL_CATEGORY = auto() + LANGUAGE = auto() + FORMS = auto() + SENSES = auto()