Repository: LeMyst/WikibaseIntegrator

Issue:
0.12.3: Wikidata URL for lexemes is not supported as id
![image](https://user-images.githubusercontent.com/68460690/215345516-7daf2814-bea9-4f86-b315-83c55bc69077.png)


Discussion:

Patch:
diff --git a/wikibaseintegrator/datatypes/item.py b/wikibaseintegrator/datatypes/item.py
index bf6959f..a35b57e 100644
--- a/wikibaseintegrator/datatypes/item.py
+++ b/wikibaseintegrator/datatypes/item.py
@@ -31,7 +31,7 @@ class Item(BaseDataType):
 
         if value:
             if isinstance(value, str):
-                pattern = re.compile(r'^(?:[a-zA-Z]+:)?Q?([0-9]+)$')
+                pattern = re.compile(r'^(?:[a-zA-Z]+:|.+\/entity\/)?Q?([0-9]+)$')
                 matches = pattern.match(value)
 
                 if not matches:
diff --git a/wikibaseintegrator/datatypes/lexeme.py b/wikibaseintegrator/datatypes/lexeme.py
index e1cbbde..2ad0c64 100644
--- a/wikibaseintegrator/datatypes/lexeme.py
+++ b/wikibaseintegrator/datatypes/lexeme.py
@@ -31,7 +31,7 @@ class Lexeme(BaseDataType):
 
         if value:
             if isinstance(value, str):
-                pattern = re.compile(r'^(?:[a-zA-Z]+:)?L?([0-9]+)$')
+                pattern = re.compile(r'^(?:[a-zA-Z]+:|.+\/entity\/)?L?([0-9]+)$')
                 matches = pattern.match(value)
 
                 if not matches:
diff --git a/wikibaseintegrator/datatypes/property.py b/wikibaseintegrator/datatypes/property.py
index 884a5ed..d68962f 100644
--- a/wikibaseintegrator/datatypes/property.py
+++ b/wikibaseintegrator/datatypes/property.py
@@ -32,7 +32,7 @@ class Property(BaseDataType):
 
         if value:
             if isinstance(value, str):
-                pattern = re.compile(r'^(?:[a-zA-Z]+:)?P?([0-9]+)$')
+                pattern = re.compile(r'^(?:[a-zA-Z]+:|.+\/entity\/)?P?([0-9]+)$')
                 matches = pattern.match(value)
 
                 if not matches:
diff --git a/wikibaseintegrator/wbi_helpers.py b/wikibaseintegrator/wbi_helpers.py
index 1d21ff0..4920459 100644
--- a/wikibaseintegrator/wbi_helpers.py
+++ b/wikibaseintegrator/wbi_helpers.py
@@ -5,6 +5,7 @@ from __future__ import annotations
 
 import datetime
 import logging
+import re
 from time import sleep
 from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union
 from urllib.parse import urlparse
@@ -534,7 +535,7 @@ def lexeme_add_form(lexeme_id, data, baserevid: Optional[int] = None, tags: Opti
     """
     Adds Form to Lexeme
 
-    :param lexeme_id: ID of the Lexeme
+    :param lexeme_id: ID of the Lexeme, e.g. L10
     :param data: The serialized object that is used as the data source.
     :param baserevid: Base Revision ID of the Lexeme, if edit conflict check is wanted.
     :param tags: Change tags to apply to the revision.
@@ -563,11 +564,11 @@ def lexeme_add_form(lexeme_id, data, baserevid: Optional[int] = None, tags: Opti
     return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs)
 
 
-def lexeme_edit_form(form_id, data, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict:
+def lexeme_edit_form(form_id: str, data, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict:
     """
     Edits representations and grammatical features of a Form
 
-    :param form_id: ID of the Form, e.g. L10-F2
+    :param form_id: ID of the Form or the concept URI, e.g. L10-F2
     :param data: The serialized object that is used as the data source.
     :param baserevid: Base Revision ID of the Lexeme, if edit conflict check is wanted.
     :param tags: Change tags to apply to the revision.
@@ -577,6 +578,14 @@ def lexeme_edit_form(form_id, data, baserevid: Optional[int] = None, tags: Optio
 
     """
 
+    pattern = re.compile(r'^(?:.+\/entity\/)?(L[0-9]+-F[0-9]+)$')
+    matches = pattern.match(form_id)
+
+    if not matches:
+        raise ValueError(f"Invalid Form ID ({form_id}), format must be 'L[0-9]+-F[0-9]+'")
+
+    form_id = matches.group(1)
+
     params = {
         'action': 'wbleditformelements',
         'formId': form_id,
@@ -596,11 +605,11 @@ def lexeme_edit_form(form_id, data, baserevid: Optional[int] = None, tags: Optio
     return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs)
 
 
-def lexeme_remove_form(form_id, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict:
+def lexeme_remove_form(form_id: str, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict:
     """
     Removes Form from Lexeme
 
-    :param form_id: ID of the Form, e.g. L10-F2
+    :param form_id: ID of the Form or the concept URI, e.g. L10-F2
     :param baserevid: Base Revision ID of the Lexeme, if edit conflict check is wanted.
     :param tags: Change tags to apply to the revision.
     :param is_bot: Mark this edit as bot.
@@ -609,6 +618,14 @@ def lexeme_remove_form(form_id, baserevid: Optional[int] = None, tags: Optional[
 
     """
 
+    pattern = re.compile(r'^(?:.+\/entity\/)?(L[0-9]+-F[0-9]+)$')
+    matches = pattern.match(form_id)
+
+    if not matches:
+        raise ValueError(f"Invalid Form ID ({form_id}), format must be 'L[0-9]+-F[0-9]+'")
+
+    form_id = matches.group(1)
+
     params = {
         'action': 'wblremoveform',
         'id': form_id,
@@ -631,8 +648,8 @@ def lexeme_add_sense(lexeme_id, data, baserevid: Optional[int] = None, tags: Opt
     """
     Adds a Sense to a Lexeme
 
-    :param lexeme_id: ID of the Lexeme
-    :param data: The serialized object that is used as the data source.
+    :param lexeme_id: ID of the Lexeme, e.g. L10
+    :param data: JSON-encoded data for the Sense, i.e. its glosses
     :param baserevid: Base Revision ID of the Lexeme, if edit conflict check is wanted.
     :param tags: Change tags to apply to the revision.
     :param is_bot: Mark this edit as bot.
@@ -660,11 +677,11 @@ def lexeme_add_sense(lexeme_id, data, baserevid: Optional[int] = None, tags: Opt
     return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs)
 
 
-def lexeme_edit_sense(sense_id, data, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict:
+def lexeme_edit_sense(sense_id: str, data, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict:
     """
     Edits glosses of a Sense
 
-    :param sense_id: ID of the Sense, e.g. L10-S2
+    :param sense_id: ID of the Sense or the concept URI, e.g. L10-S2
     :param data: The serialized object that is used as the data source.
     :param baserevid: Base Revision ID of the Lexeme, if edit conflict check is wanted.
     :param tags: Change tags to apply to the revision.
@@ -674,6 +691,14 @@ def lexeme_edit_sense(sense_id, data, baserevid: Optional[int] = None, tags: Opt
 
     """
 
+    pattern = re.compile(r'^(?:.+\/entity\/)?(L[0-9]+-S[0-9]+)$')
+    matches = pattern.match(sense_id)
+
+    if not matches:
+        raise ValueError(f"Invalid Sense ID ({sense_id}), format must be 'L[0-9]+-S[0-9]+'")
+
+    sense_id = matches.group(1)
+
     params = {
         'action': 'wbleditsenseelements',
         'formId': sense_id,
@@ -693,11 +718,11 @@ def lexeme_edit_sense(sense_id, data, baserevid: Optional[int] = None, tags: Opt
     return mediawiki_api_call_helper(data=params, is_bot=is_bot, **kwargs)
 
 
-def lexeme_remove_sense(sense_id, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict:
+def lexeme_remove_sense(sense_id: str, baserevid: Optional[int] = None, tags: Optional[List[str]] = None, is_bot: bool = False, **kwargs: Any) -> Dict:
     """
     Adds Form to Lexeme
 
-    :param sense_id: ID of the Sense, e.g. L10-S2
+    :param sense_id: ID of the Sense, e.g. L10-S20
     :param baserevid: Base Revision ID of the Lexeme, if edit conflict check is wanted.
     :param tags: Change tags to apply to the revision.
     :param is_bot: Mark this edit as bot.
@@ -706,6 +731,14 @@ def lexeme_remove_sense(sense_id, baserevid: Optional[int] = None, tags: Optiona
 
     """
 
+    pattern = re.compile(r'^(?:.+\/entity\/)?(L[0-9]+-S[0-9]+)$')
+    matches = pattern.match(sense_id)
+
+    if not matches:
+        raise ValueError(f"Invalid Sense ID ({sense_id}), format must be 'L[0-9]+-S[0-9]+'")
+
+    sense_id = matches.group(1)
+
     params = {
         'action': 'wblremovesense',
         'id': sense_id,


Test patch:
diff --git a/test/test_wbi_core.py b/test/test_wbi_core.py
index 0cd7ae6..a9c0e67 100644
--- a/test/test_wbi_core.py
+++ b/test/test_wbi_core.py
@@ -224,6 +224,7 @@ class TestWbiCore(unittest.TestCase):
             Item(value='123', prop_nr='P4'),
             Item(value=123, prop_nr='P4'),
             Item(value='Item:Q123', prop_nr='P4'),
+            Item(value='http://www.wikidata.org/entity/Q123', prop_nr='P4'),
             Time(time='-0458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
             Time(time='+458-01-01T00:00:00Z', before=1, after=2, precision=WikibaseDatePrecision.MILLION_YEARS, timezone=4, prop_nr='P5'),
             Time(time='+2021-01-01T00:00:00Z', before=1, after=2, precision=3, timezone=4, prop_nr='P5'),
@@ -243,12 +244,14 @@ class TestWbiCore(unittest.TestCase):
             Property(value='123', prop_nr='P12'),
             Property(value=123, prop_nr='P12'),
             Property(value='Property:P123', prop_nr='P12'),
+            Property(value='http://www.wikidata.org/entity/P123', prop_nr='P12'),
             TabularData(value="Data:Taipei+Population.tab", prop_nr='P13'),
             MusicalNotation(value="\relative c' { c d e f | g2 g | a4 a a a | g1 |}", prop_nr='P14'),
             Lexeme(value='L123', prop_nr='P15'),
             Lexeme(value='123', prop_nr='P15'),
             Lexeme(value=123, prop_nr='P15'),
             Lexeme(value='Lexeme:L123', prop_nr='P15'),
+            Lexeme(value='http://www.wikidata.org/entity/L123', prop_nr='P15'),
             Form(value='L123-F123', prop_nr='P16'),
             Sense(value='L123-S123', prop_nr='P17')
         ]