Skip to content

Commit

Permalink
Clarify behavior of listPages, removeNamespace and every method that has
Browse files Browse the repository at this point in the history
removes namespaces optionally to only remove the desired namespace. See
#163.
  • Loading branch information
MER-C committed Oct 20, 2018
1 parent 4390b4d commit a5a15ef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
42 changes: 26 additions & 16 deletions src/org/wikipedia/Wiki.java
Original file line number Diff line number Diff line change
Expand Up @@ -1554,18 +1554,26 @@ public String getPageUrl(String page)
* <kbd>{{PAGENAME}}</kbd>.
*
* @param page a page
* @param ns limit removal to only these namespaces. If empty, remove any
* namespace.
* @return (see above)
* @throws UncheckedIOException if the namespace cache has not been
* populated, and a network error occurs when populating it
* @see #namespace(String)
* @see #namespaceIdentifier(int)
* @since 0.35
*/
public String removeNamespace(String page)
public String removeNamespace(String page, int... ns)
{
if (namespace(page) == 0)
int namespace = namespace(page);
if (namespace == 0)
return page;
return page.substring(page.indexOf(':') + 1);
if (ns.length == 0)
return page.substring(page.indexOf(':') + 1);
for (int nstest : ns)
if (nstest == namespace)
return page.substring(page.indexOf(':') + 1);
return page;
}

/**
Expand Down Expand Up @@ -3980,7 +3988,7 @@ public boolean getImage(String title, int width, int height, File file) throws F
getparams.put("action", "query");
getparams.put("prop", "imageinfo");
getparams.put("iiprop", "url");
getparams.put("titles", "File:" + removeNamespace(normalize(title)));
getparams.put("titles", "File:" + removeNamespace(normalize(title), FILE_NAMESPACE));
getparams.put("iiurlwidth", String.valueOf(width));
getparams.put("iiurlheight", String.valueOf(height));
String line = makeApiCall(getparams, null, "getImage");
Expand Down Expand Up @@ -4066,7 +4074,7 @@ public String[] getDuplicates(String file) throws IOException
{
Map<String, String> getparams = new HashMap<>();
getparams.put("prop", "duplicatefiles");
getparams.put("titles", "File:" + removeNamespace(normalize(file)));
getparams.put("titles", "File:" + removeNamespace(normalize(file), FILE_NAMESPACE));

List<String> duplicates = makeListQuery("df", getparams, null, "getDuplicates", -1, (line, results) ->
{
Expand Down Expand Up @@ -4099,7 +4107,7 @@ public LogEntry[] getImageHistory(String title) throws IOException
Map<String, String> getparams = new HashMap<>();
getparams.put("prop", "imageinfo");
getparams.put("iiprop", "timestamp|user|comment|parsedcomment");
getparams.put("titles", "File:" + removeNamespace(normalize(title)));
getparams.put("titles", "File:" + removeNamespace(normalize(title), FILE_NAMESPACE));

String prefixtitle = namespaceIdentifier(FILE_NAMESPACE) + ":" + title;
List<LogEntry> history = makeListQuery("ii", getparams, null, "getImageHistory", -1, (line, results) ->
Expand Down Expand Up @@ -4262,7 +4270,7 @@ public synchronized void upload(File file, String filename, String contents, Str
// check for log in
if (user == null || !user.isAllowedTo("upload"))
throw new SecurityException("Permission denied: cannot upload files.");
filename = removeNamespace(filename);
filename = removeNamespace(filename, FILE_NAMESPACE);
throttle();

// protection
Expand Down Expand Up @@ -4385,7 +4393,7 @@ public synchronized void upload(URL url, String filename, String contents, Strin
// check for log in
if (user == null || !user.isAllowedTo("upload_by_url"))
throw new SecurityException("Permission denied: cannot upload files via URL.");
filename = removeNamespace(filename);
filename = removeNamespace(filename, FILE_NAMESPACE);
throttle();

// protection
Expand Down Expand Up @@ -5433,7 +5441,7 @@ public String[] imageUsage(String image, int... ns) throws IOException
{
Map<String, String> getparams = new HashMap<>();
getparams.put("list", "imageusage");
getparams.put("iutitle", "File:" + removeNamespace(normalize(image)));
getparams.put("iutitle", "File:" + removeNamespace(normalize(image), FILE_NAMESPACE));
if (ns.length > 0)
getparams.put("iunamespace", constructNamespaceString(ns));

Expand Down Expand Up @@ -5661,11 +5669,11 @@ public String[] getCategoryMembers(String name, int maxdepth, boolean sorttimest
protected String[] getCategoryMembers(String name, int maxdepth, List<String> visitedcategories,
boolean sorttimestamp, int... ns) throws IOException
{
name = removeNamespace(name);
name = removeNamespace(normalize(name), CATEGORY_NAMESPACE);
Map<String, String> getparams = new HashMap<>();
getparams.put("list", "categorymembers");
getparams.put("cmprop", "title");
getparams.put("cmtitle", "Category:" + removeNamespace(normalize(name)));
getparams.put("cmtitle", "Category:" + name);
if (sorttimestamp)
getparams.put("cmsort", "timestamp");
boolean nocat = ns.length != 0;
Expand Down Expand Up @@ -6158,7 +6166,7 @@ public String[] listPages(String prefix, Map<String, Object> protectionstate, in
* @param protectionstate a {@link #protect protection state}, use null
* to not specify one
* @param namespace a namespace. ALL_NAMESPACES is not suppported, an
* UnsupportedOperationException will be thrown.
* UnsupportedOperationException will be thrown unless a prefix is specified.
* @param minimum the minimum size in bytes these pages can be. Use -1 to
* not specify one.
* @param maximum the maximum size in bytes these pages can be. Use -1 to
Expand All @@ -6176,11 +6184,13 @@ public String[] listPages(String prefix, Map<String, Object> protectionstate, in
// for this module.
Map<String, String> getparams = new HashMap<>();
getparams.put("list", "allpages");
if (!prefix.isEmpty()) // prefix
if (!prefix.isEmpty())
{
// cull the namespace prefix
namespace = namespace(prefix);
prefix = removeNamespace(prefix);
if (namespace == ALL_NAMESPACES)
{
namespace = namespace(prefix);
prefix = removeNamespace(prefix);
}
getparams.put("apprefix", normalize(prefix));
}
else if (namespace == ALL_NAMESPACES) // check for namespace
Expand Down
2 changes: 2 additions & 0 deletions test/org/wikipedia/WikiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ public void removeNamespace() throws Exception
assertEquals("removeNamespace: main namespace", "Hello World", enWiki.removeNamespace("Hello World"));
assertEquals("removeNamespace: pretend namespace", "Hello:World", enWiki.removeNamespace("Hello:World"));
assertEquals("removeNamespace: custom namespace", "Hello", enWiki.removeNamespace("Portal:Hello"));
assertEquals("removeNamespace: partial", "Category:Blah", enWiki.removeNamespace("Category:Blah", Wiki.FILE_NAMESPACE));
assertEquals("removeNamespace: partial", "Blah", enWiki.removeNamespace("Blah", Wiki.CATEGORY_NAMESPACE));
// something funky going on with RTL, can't tell whether it's Netbeans, Firefox or me failing at copy/paste
// assertEquals("removeNamespace: rtl fail", "صفحات للحذف السريع", arWiki.removeNamespace("تصنيف:صفحات_للحذف_السريع"));
}
Expand Down

0 comments on commit a5a15ef

Please sign in to comment.