Skip to content

Commit 58a7be0

Browse files
authored
REMOVE_REF_BLOCK (#446)
* feat: REMOVE_REF_BLOCK with one pair * translations: remove-reference-block
1 parent deacf8c commit 58a7be0

6 files changed

Lines changed: 64 additions & 8 deletions

File tree

src/core/models.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,10 @@ class Operation(models.TextChoices):
881881
"remove_reference",
882882
pgettext_lazy("batchcommand-py-operation-remove-reference", "Remove reference"),
883883
)
884+
REMOVE_REFERENCE_BLOCK = (
885+
"remove_reference_block",
886+
pgettext_lazy("batchcommand-py-operation-remove-reference-block", "Remove reference block"),
887+
)
884888
#
885889
SET_SITELINK = (
886890
"set_sitelink",
@@ -1535,6 +1539,7 @@ def operation_is_combinable(self):
15351539
self.Operation.REMOVE_STATEMENT_BY_VALUE,
15361540
self.Operation.REMOVE_QUALIFIER,
15371541
self.Operation.REMOVE_REFERENCE,
1542+
self.Operation.REMOVE_REFERENCE_BLOCK,
15381543
self.Operation.ADD_ALIAS,
15391544
self.Operation.SET_LABEL,
15401545
self.Operation.SET_DESCRIPTION,
@@ -1686,6 +1691,7 @@ def update_entity_json(self, entity: dict):
16861691
elif self.operation in (
16871692
self.Operation.REMOVE_QUALIFIER,
16881693
self.Operation.REMOVE_REFERENCE,
1694+
self.Operation.REMOVE_REFERENCE_BLOCK,
16891695
):
16901696
self._remove_qualifier_or_reference(entity)
16911697
elif self.operation == self.Operation.SET_SITELINK:
@@ -1747,7 +1753,7 @@ def _update_entity_statements(self, entity: dict):
17471753

17481754
def _remove_qualifier_or_reference(self, entity: dict):
17491755
"""
1750-
Removes a qualifier or a reference from the entity.
1756+
Removes a qualifier, a reference part or a reference block from the entity.
17511757
"""
17521758
statements = self._get_statements(entity)
17531759
found_qualifier = False
@@ -1768,9 +1774,9 @@ def _remove_qualifier_or_reference(self, entity: dict):
17681774
found_ref_part = True
17691775
break
17701776
if found_ref_part:
1777+
if self.operation == self.Operation.REMOVE_REFERENCE_BLOCK:
1778+
statement["references"].pop(i)
17711779
break
1772-
# any better way to do this? :P
1773-
# (without refactoring into a different function...)
17741780
if found_ref_part:
17751781
break
17761782
if not found_qualifier and len(self.qualifiers_for_api()) > 0:

src/core/parsers/v1.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,19 @@ def parse_remove_qualifier(self, elements):
9494
return data
9595

9696
def parse_remove_reference(self, elements):
97+
oprt = str(elements[0])
98+
is_block = oprt == "REMOVE_REF_BLOCK"
9799
llen = len(elements)
98100
if llen != 6:
99-
raise ParserException("REMOVE_REF command must be Qid|Pid|value|Sid|value")
101+
raise ParserException(f"{oprt} command must be Qid|Pid|value|Sid|value")
100102
elements.pop(0)
101103
data = self.parse_statement(elements, elements[0].upper())
102104
data["action"] = "remove"
103-
data["what"] = "reference"
105+
data["what"] = "reference_block" if is_block else "reference"
104106
if len(data.get("references", [])) != 1:
105-
raise ParserException("REMOVE_REF command must have 1 reference")
107+
raise ParserException(f"{oprt} command must have 1 reference")
106108
if len(data.get("qualifiers", [])) != 0:
107-
raise ParserException("REMOVE_REF command must have no qualifiers")
109+
raise ParserException(f"{oprt} command must have no qualifiers")
108110
return data
109111

110112
def parse_statement_by_id(self, elements):
@@ -354,7 +356,7 @@ def parse_command(self, raw_command):
354356
elif first_command == "REMOVE_QUAL":
355357
logger.debug(f"parsing remove qualifier: {elements}")
356358
data = self.parse_remove_qualifier(elements)
357-
elif first_command == "REMOVE_REF":
359+
elif first_command in ("REMOVE_REF", "REMOVE_REF_BLOCK"):
358360
logger.debug(f"parsing remove reference: {elements}")
359361
data = self.parse_remove_reference(elements)
360362
elif first_command == "SWITCH_VALUE":
@@ -447,6 +449,8 @@ def restore_placeholders(command):
447449
bc.operation = bc.Operation.REMOVE_QUALIFIER
448450
elif what == "reference":
449451
bc.operation = bc.Operation.REMOVE_REFERENCE
452+
elif what == "reference_block":
453+
bc.operation = bc.Operation.REMOVE_REFERENCE_BLOCK
450454
elif command["action"] == "create":
451455
bc.action = BatchCommand.ACTION_CREATE
452456
what_or_type = command.get("type", command.get("what"))

src/core/tests/test_entity_patching.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,49 @@ def test_remove_reference(self):
427427
prop = entity["statements"]["P31"][0]["references"][1]["parts"][0]["property"]
428428
self.assertEqual(prop["id"], "P74")
429429

430+
def test_remove_reference_block(self):
431+
text = """
432+
REMOVE_REF_BLOCK|Q12345678|P65|42|S31|somevalue
433+
REMOVE_REF_BLOCK|Q12345678|P31|somevalue|S93|"https://www.mediawiki.org/"
434+
REMOVE_REF_BLOCK|Q12345678|P31|somevalue|S84267|42
435+
REMOVE_REF_BLOCK|Q12345678|P31|somevalue|S84267|42
436+
"""
437+
batch = self.parse(text)
438+
entity = copy.deepcopy(self.INITIAL)
439+
# -----
440+
remove_nothing = batch.commands()[0]
441+
self.assertRefCount(entity, "P65", 0)
442+
with self.assertRaises(NoReferenceParts):
443+
remove_nothing.update_entity_json(entity)
444+
self.assertRefCount(entity, "P65", 0)
445+
# ---
446+
prop = entity["statements"]["P31"][0]["references"][0]["parts"][0]["property"]
447+
self.assertEqual(prop["id"], "P93")
448+
prop = entity["statements"]["P31"][0]["references"][1]["parts"][0]["property"]
449+
self.assertEqual(prop["id"], "P93")
450+
# -----
451+
remove_part_mediawiki = batch.commands()[1]
452+
self.assertRefCount(entity, "P31", 2)
453+
self.assertRefPartsCount(entity, "P31", 2, ipart=0)
454+
self.assertRefPartsCount(entity, "P31", 3, ipart=1)
455+
remove_part_mediawiki.update_entity_json(entity)
456+
self.assertRefCount(entity, "P31", 1)
457+
self.assertRefPartsCount(entity, "P31", 2, ipart=0)
458+
with self.assertRaises(NoReferenceParts):
459+
# try to remove it again
460+
remove_part_mediawiki.update_entity_json(entity)
461+
# -----
462+
remove_42 = batch.commands()[2]
463+
self.assertRefCount(entity, "P31", 1)
464+
self.assertRefPartsCount(entity, "P31", 2, ipart=0)
465+
remove_42.update_entity_json(entity)
466+
self.assertRefCount(entity, "P31", 0)
467+
# -----
468+
remove_42_again = batch.commands()[3]
469+
with self.assertRaises(NoReferenceParts):
470+
# try to remove it again
471+
remove_42_again.update_entity_json(entity)
472+
430473
def test_add_reference(self):
431474
text = """
432475
Q12345678|P31|somevalue|S93|"https://example.com/"

translations/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"batchcommand-py-operation-remove-statement-by-value": "Remove statement by value",
2727
"batchcommand-py-operation-remove-qualifier": "Remove qualifier",
2828
"batchcommand-py-operation-remove-reference": "Remove reference",
29+
"batchcommand-py-operation-remove-reference-block": "Remove reference block",
2930
"batchcommand-py-operation-set-sitelink": "Set sitelink",
3031
"batchcommand-py-operation-set-label": "Set label",
3132
"batchcommand-py-operation-set-description": "Set description",

translations/pt-br.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"batchcommand-py-operation-remove-statement-by-value": "Remover declaração por valor",
3131
"batchcommand-py-operation-remove-qualifier": "Remover qualificador",
3232
"batchcommand-py-operation-remove-reference": "Remover referência",
33+
"batchcommand-py-operation-remove-reference-block": "Remover bloco de referência",
3334
"batchcommand-py-operation-set-sitelink": "Definir hiperligação de site",
3435
"batchcommand-py-operation-set-label": "Definir rótulo",
3536
"batchcommand-py-operation-set-description": "Definir descrição",

translations/qqq.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"batchcommand-py-operation-remove-statement-by-value": "Individual command operation type displayed in command list",
3232
"batchcommand-py-operation-remove-qualifier": "Individual command operation type displayed in command list",
3333
"batchcommand-py-operation-remove-reference": "Individual command operation type displayed in command list",
34+
"batchcommand-py-operation-remove-reference-block": "Individual command operation type displayed in command list",
3435
"batchcommand-py-operation-set-sitelink": "Individual command operation type displayed in command list",
3536
"batchcommand-py-operation-set-label": "Individual command operation type displayed in command list",
3637
"batchcommand-py-operation-set-description": "Individual command operation type displayed in command list",

0 commit comments

Comments
 (0)