WikibaseDataEditor { public ItemDocument updateTermsStatements(ItemIdValue itemIdValue, List<MonolingualTextValue> addLabels, List<MonolingualTextValue> addDescriptions, List<MonolingualTextValue> addAliases, List<MonolingualTextValue> deleteAliases, List<Statement> addStatements, List<Statement> deleteStatements, String summary, List<String> tags) throws MediaWikiApiErrorException, IOException { ItemDocument currentDocument = (ItemDocument) this.wikibaseDataFetcher .getEntityDocument(itemIdValue.getId()); return updateTermsStatements(currentDocument, addLabels, addDescriptions, addAliases, deleteAliases, addStatements, deleteStatements, summary, tags); } WikibaseDataEditor(ApiConnection connection, String siteUri);  WikibaseDataEditor(ApiConnection connection, String siteUri, GuidGenerator generator); boolean editAsBot(); void setEditAsBot(boolean editAsBot); int getMaxLag(); void setMaxLag(int maxLag); int getMaxLagMaxRetries(); void setMaxLagMaxRetries(int retries); int getMaxLagFirstWaitTime(); void setMaxLagFirstWaitTime(int time); double getMaxLagBackOffFactor(); void setMaxLagBackOffFactor(double value); int getAverageTimePerEdit(); void setAverageTimePerEdit(int milliseconds); int getRemainingEdits(); void setRemainingEdits(int remainingEdits); void disableEditing(); ItemDocument createItemDocument(ItemDocument itemDocument,
			String summary, List<String> tags); PropertyDocument createPropertyDocument(
			PropertyDocument propertyDocument, String summary, List<String> tags); ItemDocument editItemDocument(ItemDocument itemDocument,
			boolean clear, String summary, List<String> tags); PropertyDocument editPropertyDocument(
			PropertyDocument propertyDocument, boolean clear, String summary,
			List<String> tags); MediaInfoDocument editMediaInfoDocument(
			MediaInfoDocument mediaInfoDocument, boolean clear, String summary,
			List<String> tags); ItemDocument updateStatements(ItemIdValue itemIdValue,
			List<Statement> addStatements, List<Statement> deleteStatements,
			String summary, List<String> tags); ItemDocument updateTermsStatements(ItemIdValue itemIdValue,
			List<MonolingualTextValue> addLabels,
			List<MonolingualTextValue> addDescriptions,
			List<MonolingualTextValue> addAliases,
			List<MonolingualTextValue> deleteAliases,
			List<Statement> addStatements,
			List<Statement> deleteStatements,
			String summary,
			List<String> tags); PropertyDocument updateStatements(PropertyIdValue propertyIdValue,
			List<Statement> addStatements, List<Statement> deleteStatements,
			String summary, List<String> tags); @SuppressWarnings("unchecked") T updateStatements(T currentDocument,
			List<Statement> addStatements, List<Statement> deleteStatements,
			String summary, List<String> tags); @SuppressWarnings("unchecked") T updateTermsStatements(T currentDocument,
			List<MonolingualTextValue> addLabels,
			List<MonolingualTextValue> addDescriptions,
			List<MonolingualTextValue> addAliases,
			List<MonolingualTextValue> deleteAliases,
			List<Statement> addStatements, List<Statement> deleteStatements,
			String summary, List<String> tags); void nullEdit(ItemIdValue itemId); void nullEdit(PropertyIdValue propertyId); @SuppressWarnings("unchecked") T nullEdit(T currentDocument);  }
@Test public void testLabelEdit() throws MediaWikiApiErrorException, IOException { WikibaseDataEditor wde = new WikibaseDataEditor(this.con, Datamodel.SITE_WIKIDATA); ItemIdValue id = Datamodel.makeWikidataItemIdValue("Q1234"); Statement s1 = StatementBuilder.forSubjectAndProperty(id, P31) .withValue(Q5).withId("ID-s1").build(); MonolingualTextValue label = Datamodel.makeMonolingualTextValue("My label", "en"); ItemDocument itemDocument = ItemDocumentBuilder.forItemId(id) .withStatement(s1) .withRevisionId(1234) .build(); ItemDocument expectedDocument = ItemDocumentBuilder.forItemId(id) .withLabel(label) .withStatement(s1) .withRevisionId(1235) .build(); Map<String, String> params = new HashMap<>(); params.put("action", "wbsetlabel"); params.put("id", "Q1234"); params.put("summary", "Adding a label"); params.put("token", "42307b93c79b0cb558d2dfb4c3c92e0955e06041+\\"); params.put("format", "json"); params.put("baserevid", "1234"); params.put("maxlag", "5"); params.put("language", "en"); params.put("value", "My label"); String expectedResult = "{\"entity\":{\"labels\":{\"en\":{\"language\":\"en\",\"value\":\"My label\"}},"+ "\"id\":\"Q1234\",\"type\":\"item\",\"lastrevid\":1235},\"success\":1}"; con.setWebResource(params, expectedResult); ItemDocument editedDocument = wde.updateTermsStatements(itemDocument, Collections.singletonList(label), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), "Adding a label", Collections.emptyList()); assertEquals(expectedDocument, editedDocument); }
@Test public void testDescriptionEdit() throws MediaWikiApiErrorException, IOException { WikibaseDataEditor wde = new WikibaseDataEditor(this.con, Datamodel.SITE_WIKIDATA); ItemIdValue id = Datamodel.makeWikidataItemIdValue("Q1234"); Statement s1 = StatementBuilder.forSubjectAndProperty(id, P31) .withValue(Q5).withId("ID-s1").build(); MonolingualTextValue description = Datamodel.makeMonolingualTextValue("My description", "en"); ItemDocument itemDocument = ItemDocumentBuilder.forItemId(id) .withStatement(s1) .withRevisionId(1234) .build(); ItemDocument expectedDocument = ItemDocumentBuilder.forItemId(id) .withDescription(description) .withStatement(s1) .withRevisionId(1235L) .build(); Map<String, String> params = new HashMap<>(); params.put("action", "wbsetdescription"); params.put("id", "Q1234"); params.put("summary", "Adding a description"); params.put("token", "42307b93c79b0cb558d2dfb4c3c92e0955e06041+\\"); params.put("format", "json"); params.put("baserevid", "1234"); params.put("maxlag", "5"); params.put("language", "en"); params.put("value", "My description"); String expectedResult = "{\"entity\":{\"descriptions\":{\"en\":{\"language\":\"en\",\"value\":\"My description\"}},"+ "\"id\":\"Q1234\",\"type\":\"item\",\"lastrevid\":1235},\"success\":1}"; con.setWebResource(params, expectedResult); ItemDocument editedDocument = wde.updateTermsStatements(itemDocument, Collections.emptyList(), Collections.singletonList(description), Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.<Statement>emptyList(), Collections.<Statement>emptyList(), "Adding a description", null); assertEquals(expectedDocument, editedDocument); }
@Test public void testAliasEdit() throws MediaWikiApiErrorException, IOException { WikibaseDataEditor wde = new WikibaseDataEditor(this.con, Datamodel.SITE_WIKIDATA); ItemIdValue id = Datamodel.makeWikidataItemIdValue("Q1234"); Statement s1 = StatementBuilder.forSubjectAndProperty(id, P31) .withValue(Q5).withId("ID-s1").build(); MonolingualTextValue label = Datamodel.makeMonolingualTextValue("My label", "en"); MonolingualTextValue addedAlias = Datamodel.makeMonolingualTextValue("My added alias", "en"); MonolingualTextValue removedAlias = Datamodel.makeMonolingualTextValue("My removed alias", "en"); ItemDocument itemDocument = ItemDocumentBuilder.forItemId(id) .withStatement(s1) .withLabel(label) .withAlias(removedAlias) .withRevisionId(1234) .build(); ItemDocument expectedDocument = ItemDocumentBuilder.forItemId(id) .withStatement(s1) .withLabel(label) .withAlias(addedAlias) .withRevisionId(1235) .build(); Map<String, String> params = new HashMap<>(); params.put("action", "wbsetaliases"); params.put("id", "Q1234"); params.put("summary", "Changing aliases"); params.put("token", "42307b93c79b0cb558d2dfb4c3c92e0955e06041+\\"); params.put("format", "json"); params.put("baserevid", "1234"); params.put("maxlag", "5"); params.put("language", "en"); params.put("add", "My added alias"); params.put("remove", "My removed alias"); String expectedResult = "{\"entity\":{\"aliases\":{\"en\":[{\"language\":\"en\",\"value\":\"My added alias\"}]},"+ "\"id\":\"Q1234\",\"type\":\"item\",\"lastrevid\":1235},\"success\":1}"; con.setWebResource(params, expectedResult); ItemDocument editedDocument = wde.updateTermsStatements(itemDocument, Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.singletonList(addedAlias), Collections.singletonList(removedAlias), Collections.<Statement>emptyList(), Collections.<Statement>emptyList(), "Changing aliases", null); assertEquals(expectedDocument, editedDocument); }
@Test public void testNewSingleStatement() throws MediaWikiApiErrorException, IOException { String guid = "8372EF7A-B72C-7DE2-98D0-DFB4-8EC8392AC28E"; WikibaseDataEditor wde = new WikibaseDataEditor(this.con, Datamodel.SITE_WIKIDATA, new MockGuidGenerator(guid)); ItemIdValue id = Datamodel.makeWikidataItemIdValue("Q1234"); Statement s1 = StatementBuilder.forSubjectAndProperty(id, P31) .withValue(Q5).build(); Statement s2 = StatementBuilder.forSubjectAndProperty(id, P31) .withValue(Q5).withId("Q1234$"+guid).build(); ItemDocument itemDocument = ItemDocumentBuilder.forItemId(id) .withRevisionId(1234) .build(); ItemDocument expectedDocument = ItemDocumentBuilder.forItemId(id) .withStatement(s2) .withRevisionId(1235) .build(); String statementJson = JsonSerializer.getJsonString(s2); Map<String, String> params = new HashMap<>(); params.put("action", "wbsetclaim"); params.put("summary", "Adding a claim"); params.put("tags", "statement-creation"); params.put("token", "42307b93c79b0cb558d2dfb4c3c92e0955e06041+\\"); params.put("format", "json"); params.put("baserevid", "1234"); params.put("maxlag", "5"); params.put("claim", statementJson); String expectedResult = "{\"pageinfo\":{\"lastrevid\":1235},\"success\":1,\"claim\":"+statementJson+"}"; con.setWebResource(params, expectedResult); ItemDocument editedDocument = wde.updateTermsStatements(itemDocument, Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.singletonList(s1), Collections.<Statement>emptyList(), "Adding a claim", Collections.singletonList("statement-creation")); assertEquals(expectedDocument, editedDocument); }
@Test public void testDeleteStatements() throws MediaWikiApiErrorException, IOException { String guid1 = "8372EF7A-B72C-7DE2-98D0-DFB4-8EC8392AC28E"; String guid2 = "4311895D-9091-4BC9-9B34-DFB4-1B00EE8CFA62"; WikibaseDataEditor wde = new WikibaseDataEditor(this.con, Datamodel.SITE_WIKIDATA); ItemIdValue id = Datamodel.makeWikidataItemIdValue("Q1234"); Statement s1 = StatementBuilder.forSubjectAndProperty(id, P31) .withValue(Q5).withId("Q1234$"+guid1).build(); Statement s2 = StatementBuilder.forSubjectAndProperty(id, P31) .withValue(Q5).withId("Q1234$"+guid2).build(); ItemDocument itemDocument = ItemDocumentBuilder.forItemId(id) .withRevisionId(1234) .withStatement(s1) .withStatement(s2) .build(); ItemDocument expectedDocument = ItemDocumentBuilder.forItemId(id) .withRevisionId(1235) .build(); List<String> statementIds = Arrays.asList("Q1234$"+guid1, "Q1234$"+guid2); String statementsList = String.join("|", statementIds); Map<String, String> params = new HashMap<>(); params.put("action", "wbremoveclaims"); params.put("summary", "Removing claims"); params.put("token", "42307b93c79b0cb558d2dfb4c3c92e0955e06041+\\"); params.put("format", "json"); params.put("baserevid", "1234"); params.put("maxlag", "5"); params.put("claim", statementsList); String expectedResult = "{\"pageinfo\":{\"lastrevid\":1235},\"success\":1,\"claims\":[\""+statementIds.get(0)+"\",\""+statementIds.get(1)+"\"]}"; con.setWebResource(params, expectedResult); ItemDocument editedDocument = wde.updateTermsStatements(itemDocument, Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.<Statement>emptyList(), Arrays.asList(s1,s2), "Removing claims", null); assertEquals(expectedDocument, editedDocument); }
@Test(expected = TagsApplyNotAllowedException.class) public void testApplyInvalidTag() throws MediaWikiApiErrorException, IOException { WikibaseDataEditor wde = new WikibaseDataEditor(this.con, Datamodel.SITE_WIKIDATA); ItemIdValue id = Datamodel.makeWikidataItemIdValue("Q1234"); Statement s1 = StatementBuilder.forSubjectAndProperty(id, P31) .withValue(Q5).withId("ID-s1").build(); MonolingualTextValue description = Datamodel.makeMonolingualTextValue("My description", "en"); ItemDocument itemDocument = ItemDocumentBuilder.forItemId(id) .withStatement(s1) .withRevisionId(1234) .build(); Map<String, String> params = new HashMap<>(); params.put("action", "wbsetdescription"); params.put("id", "Q1234"); params.put("summary", "testing tags"); params.put("tags", "tag_which_does_not_exist"); params.put("token", "42307b93c79b0cb558d2dfb4c3c92e0955e06041+\\"); params.put("format", "json"); params.put("baserevid", "1234"); params.put("maxlag", "5"); params.put("language", "en"); params.put("value", "My description"); String expectedResult = "{\"error\":" + "{\"code\":\"tags-apply-not-allowed-one\"," + "\"info\":\"The tag \\\"tag_which_does_not_exist\\\" is not allowed to be manually applied.\"," + "\"*\":\"See https: + "\"servedby\":\"mw1276\"}"; con.setWebResource(params, expectedResult); wde.updateTermsStatements(itemDocument, Collections.<MonolingualTextValue>emptyList(), Collections.singletonList(description), Collections.<MonolingualTextValue>emptyList(), Collections.<MonolingualTextValue>emptyList(), Collections.<Statement>emptyList(), Collections.<Statement>emptyList(), "testing tags", Collections.singletonList("tag_which_does_not_exist")); }