Repository: Wikidata/Wikidata-Toolkit
Pull request #733 (2022-10-12 14:54:44)

239: add uselang as optional parameter for wbsearchentities to specif…
…y required response language

Lhaaits: Resolves #239 

Patch:
diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
index 32619203..48319b52 100644
--- a/.github/workflows/deploy.yml
+++ b/.github/workflows/deploy.yml
@@ -31,7 +31,7 @@ jobs:
       - name: Generate javadocs
         run: mvn compile javadoc:aggregate
       - name: Publish javadocs
-        uses: JamesIves/github-pages-deploy-action@v4.4.0
+        uses: JamesIves/github-pages-deploy-action@v4.4.1
         with:
           branch: gh-pages
           folder: target/site/apidocs
diff --git a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesSearchData.java b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesSearchData.java
index edb02352..7d06ed91 100644
--- a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesSearchData.java
+++ b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbGetEntitiesSearchData.java
@@ -32,21 +32,21 @@ public class WbGetEntitiesSearchData {
 
 	/**
 	 * search for this text. See
-	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long)}
+	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long, String)}
 	 * for details.
 	 */
 	public String search = null;
 
 	/**
 	 * search in this language. See
-	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long)}
+	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long, String)}
 	 * for details.
 	 */
 	public String language = null;
 
 	/**
 	 * whether to disable language fallback. See
-	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long)}
+	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long, String)}
 	 * for details.
 	 */
 	public Boolean strictlanguage = null;
@@ -54,7 +54,7 @@ public class WbGetEntitiesSearchData {
 	/**
 	 * search for this type of entity
 	 * One of the following values: item, property. See
-	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long)}
+	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long, String)}
 	 * for details.
 	 */
 	public String type = null;
@@ -62,7 +62,7 @@ public class WbGetEntitiesSearchData {
 	/**
 	 * maximal number of results
 	 * no more than 50 (500 for bots) allowed. See
-	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long)}
+	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long, String)}
 	 * for details.
 	 */
 	public Long limit = null;
@@ -70,8 +70,15 @@ public class WbGetEntitiesSearchData {
 	/**
 	 * offset where to continue a search
 	 * this parameter is called "continue" in the API (which is a Java keyword). See
-	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long)}
+	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long, String)}
 	 * for details.
 	 */
 	public Long offset = null;
+
+	/**
+	 * get response in this language. See
+	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long, String)}
+	 * for details.
+	 */
+	public String uselang = null;
 }
diff --git a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbSearchEntitiesAction.java b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbSearchEntitiesAction.java
index bab63c08..49acfef5 100644
--- a/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbSearchEntitiesAction.java
+++ b/wdtk-wikibaseapi/src/main/java/org/wikidata/wdtk/wikibaseapi/WbSearchEntitiesAction.java
@@ -69,7 +69,17 @@ public class WbSearchEntitiesAction {
 	public List<WbSearchEntitiesResult> wbSearchEntities(WbGetEntitiesSearchData properties)
 			throws MediaWikiApiErrorException, IOException {
 		return wbSearchEntities(properties.search, properties.language,
-				properties.strictlanguage, properties.type, properties.limit, properties.offset);
+				properties.strictlanguage, properties.type, properties.limit, properties.offset, properties.uselang);
+	}
+
+	/**
+	 * Keeping this for backwards compatibility, the real action happens in 
+	 * {@link WbSearchEntitiesAction#wbSearchEntities(String, String, Boolean, String, Long, Long, String)}
+	 */
+	public List<WbSearchEntitiesResult> wbSearchEntities(String search, String language,
+			Boolean strictLanguage, String type, Long limit, Long offset)
+					throws MediaWikiApiErrorException, IOException {
+		return wbSearchEntities(search, language, strictLanguage, type, limit, offset, null);
 	}
 
 	/**
@@ -103,7 +113,8 @@ public class WbSearchEntitiesAction {
 	 *            (optional) offset where to continue a search
 	 *            Default: 0
 	 *            this parameter is called "continue" in the API (which is a Java keyword)
-	 *
+	 * @param uselang        
+	 *            (optional) the response should have this language, default en
 	 * @return list of matching entities retrieved via the API URL
 	 * @throws MediaWikiApiErrorException
 	 *             if the API returns an error
@@ -112,8 +123,8 @@ public class WbSearchEntitiesAction {
 	 * @throws MalformedResponseException
 	 *             if response JSON cannot be parsed
 	 */
-	public List<WbSearchEntitiesResult> wbSearchEntities(String search, String language,
-			Boolean strictLanguage, String type, Long limit, Long offset)
+	public List<WbSearchEntitiesResult> wbSearchEntities(String search, String language, 
+			Boolean strictLanguage, String type, Long limit, Long offset, String uselang)
 					throws MediaWikiApiErrorException, IOException {
 
 		Map<String, String> parameters = new HashMap<>();
@@ -148,6 +159,10 @@ public class WbSearchEntitiesAction {
 			parameters.put("continue", Long.toString(offset));
 		}
 
+		if (uselang != null) {
+			parameters.put("uselang", uselang);
+		}
+
 		List<WbSearchEntitiesResult> results = new ArrayList<>();
 
 		JsonNode root = this.connection.sendJsonRequest("POST", parameters);


Test patch:
diff --git a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WbSearchEntitiesActionTest.java b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WbSearchEntitiesActionTest.java
index 6ceb00d8..434b7ae5 100644
--- a/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WbSearchEntitiesActionTest.java
+++ b/wdtk-wikibaseapi/src/test/java/org/wikidata/wdtk/wikibaseapi/WbSearchEntitiesActionTest.java
@@ -63,7 +63,7 @@ public class WbSearchEntitiesActionTest {
     @Test
     public void testWbSearchEntities() throws MediaWikiApiErrorException, IOException {
         List<WbSearchEntitiesResult> results = action.wbSearchEntities("abc",
-                "en", null, null, null, null);
+                "en", null, null, null, null, null);
 
         assertEquals(7, results.size());
 
@@ -89,18 +89,18 @@ public class WbSearchEntitiesActionTest {
     public void testWbSearchEntitiesEmpty() throws MediaWikiApiErrorException, IOException {
         List<WbSearchEntitiesResult> results = action.wbSearchEntities(
                 "some search string with no results", "en", null, null, null,
-                null);
+                null, null);
 
         assertTrue(results.isEmpty());
     }
 
     @Test(expected = IllegalArgumentException.class)
     public void testIdsAndTitles() throws MediaWikiApiErrorException, IOException {
-        action.wbSearchEntities(null, "en", null, null, null, null);
+        action.wbSearchEntities(null, "en", null, null, null, null, null);
     }
 
     @Test(expected = IllegalArgumentException.class)
     public void testIdsAndSites() throws MediaWikiApiErrorException, IOException {
-        action.wbSearchEntities("abc", null, null, null, null, null);
+        action.wbSearchEntities("abc", null, null, null, null, null, null);
     }
 }