Skip to content

Commit

Permalink
added ability to query special pages to Wiki, closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
fastily committed Apr 17, 2018
1 parent 7e22913 commit cb60c45
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/fastily/jwiki/core/MQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -483,5 +483,5 @@ public static HashMap<String, String> getTextExtracts(Wiki wiki, Collection<Stri
getNoContProp(wiki, titles, WQuery.TEXTEXTRACTS, null, "extract").forEach((k, v) -> l.put(k, v == null ? null : v.getAsString()));

return l;
}
}
}
15 changes: 10 additions & 5 deletions src/main/java/fastily/jwiki/core/WQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ class WQuery
public static final QTemplate PROTECTEDTITLES = new QTemplate(
FL.pMap("list", "protectedtitles", "ptprop", "timestamp|level|user|comment"), "ptlimit", "protectedtitles");

/**
* Default parameters for listing the results of querying Special pages.
*/
public static final QTemplate QUERYPAGES = new QTemplate(FL.pMap("list", "querypage", "qppage", null), "qplimit", "querypage");

/**
* Default parameters for listing random pages
*/
Expand Down Expand Up @@ -182,7 +187,7 @@ class WQuery
* Default parameters for getting a user's username and id.
*/
public static final QTemplate USERINFO = new QTemplate(FL.pMap("meta", "userinfo"), null);

/**
* Default parameters for listing users and their rights.
*/
Expand All @@ -199,7 +204,7 @@ class WQuery
*/
private static Type strMapT = new TypeToken<HashMap<String, String>>() {
}.getType();

/**
* The master parameter list. Tracks current query status.
*/
Expand Down Expand Up @@ -444,9 +449,9 @@ protected static class QReply
private QReply(JsonObject input)
{
this.input = input;
if(GSONP.nestedHas(input, FL.toSAL("query", "normalized")))
normalized = GSONP.pairOff(GSONP.getJAofJO(GSONP.getNestedJA(input, FL.toSAL("query", "normalized"))), "from", "to");

if (GSONP.nestedHas(input, FL.toSAL("query", "normalized")))
normalized = GSONP.pairOff(GSONP.getJAofJO(GSONP.getNestedJA(input, FL.toSAL("query", "normalized"))), "from", "to");
}

/**
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/fastily/jwiki/core/Wiki.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,12 @@ public String talkPageOf(String title)
public String talkPageBelongsTo(String title)
{
NS ns = whichNS(title);
if(ns.v < 0 || ns.v % 2 == 0 )

if (ns.v < 0 || ns.v % 2 == 0)
return null;
else if(ns.equals(NS.TALK))
else if (ns.equals(NS.TALK))
return nss(title);

return (String) nsl.nsM.get(ns.v - 1) + ":" + nss(title);
}

Expand Down Expand Up @@ -1070,6 +1070,35 @@ public ArrayList<String> prefixIndex(NS namespace, String prefix)
return allPages(prefix, false, false, -1, namespace);
}

/**
* Queries a special page.
*
* @param title The special page to query, without the {@code Special:} prefix. CAVEAT: this is CASE-sensitive, so be
* sure to use the exact title (e.g. {@code UnusedFiles}, {@code BrokenRedirects}). For a full list of
* titles, see <a href="https://www.mediawiki.org/w/api.php?action=help&modules=query+querypage">the
* official documentation</a>.
* @param cap The maximum number of elements to return. Use {@code -1} to get everything, but be careful because some
* pages can have 10k+ entries.
* @return A List of titles returned by this special page.
*/
public ArrayList<String> querySpecialPage(String title, int cap)
{
WQuery wq = new WQuery(this, cap, WQuery.QUERYPAGES).set("qppage", nss(title));

ArrayList<String> l = new ArrayList<>();
while (wq.has())
try
{
l.addAll(FL.toAL(FL.streamFrom(GSONP.getNestedJA(wq.next().input, FL.toSAL("query", "querypage", "results")))
.map(e -> GSONP.getStr(e.getAsJsonObject(), "title"))));
}
catch (Throwable e)
{
e.printStackTrace();
}
return l;
}

/**
* Attempts to resolve title redirects on a Wiki.
*
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/fastily/jwiki/test/MockQueryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,19 @@ public void testGetLogs()
assertEquals("FastilyBot", l.get(2).user);
assertEquals("Test", l.get(2).title);
}

/**
* Tests querying of special pages.
*/
@Test
public void testQuerySpecialPage()
{
addResponse("mockQuerySpecialPage");

ArrayList<String> l = wiki.querySpecialPage("Deadendpages", 10);

assertTrue(l.contains("TestPage"));
assertTrue(l.contains("File:Example.jpg"));
assertTrue(l.contains("Talk:Main page"));
}
}
27 changes: 27 additions & 0 deletions src/test/resources/fastily/jwiki/test/mockQuerySpecialPage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"query": {
"querypage": {
"name": "Deadendpages",
"cached": "",
"cachedtimestamp": "2017-01-01T01:17:04Z",
"maxresults": 5000,
"results": [
{
"value": "0",
"ns": 0,
"title": "TestPage"
},
{
"value": "0",
"ns": 6,
"title": "File:Example.jpg"
},
{
"value": "0",
"ns": 1,
"title": "Talk:Main page"
}
]
}
}
}

0 comments on commit cb60c45

Please sign in to comment.