Difference between revisions of "Examples"
Jump to navigation
Jump to search
(→Edit) |
|||
(13 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | == Intro == | ||
+ | Depending on your version of Mediawiki you'll get different API details and help pages: | ||
+ | |||
+ | * [http://mediawiki-japi.bitplan.com/mw1_23/api.php Version 1.23 API help] | ||
+ | * [https://en.wikipedia.org/w/api.php current API help] | ||
+ | |||
+ | == API usage examples == | ||
+ | You might have to change the DEFAULT_SCRIPTPATH depending on your wiki configuration see [https://github.com/WolfgangFahl/Mediawiki-Japi/issues/26 Issue26] | ||
+ | |||
=== Sample query:get a single page === | === Sample query:get a single page === | ||
http://www.mediawiki.org/wiki/API:Query#Sample_query | http://www.mediawiki.org/wiki/API:Query#Sample_query | ||
Line 6: | Line 15: | ||
String content=wiki.getPageContent("Main Page"); | String content=wiki.getPageContent("Main Page"); | ||
</source> | </source> | ||
− | + | [https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/TestUsage.java JUnit Test] | |
<source lang='java'> | <source lang='java'> | ||
/** | /** | ||
Line 20: | Line 29: | ||
} | } | ||
</source> | </source> | ||
+ | |||
=== login/logout === | === login/logout === | ||
http://www.mediawiki.org/wiki/API:Login | http://www.mediawiki.org/wiki/API:Login | ||
Line 26: | Line 36: | ||
wiki.logout(); | wiki.logout(); | ||
</source> | </source> | ||
+ | [https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/TestAPI_Login.java Junit Test] | ||
− | |||
<source lang='java'> | <source lang='java'> | ||
/** | /** | ||
Line 45: | Line 55: | ||
} | } | ||
</source> | </source> | ||
+ | |||
+ | === query === | ||
+ | [https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/TestAPI_Query.java Junit Test] | ||
+ | ==== Allpages ==== | ||
+ | <source lang='java'> | ||
+ | /** | ||
+ | * test the Allpages API call http://www.mediawiki.org/wiki/API:Allpages | ||
+ | * | ||
+ | * @throws Exception | ||
+ | */ | ||
+ | @Test | ||
+ | public void testGetAllPages() throws Exception { | ||
+ | for (ExampleWiki lwiki : getWikis()) { | ||
+ | Api api = getQueryResult(lwiki, "&list=allpages&apfrom=Kre&aplimit=3"); | ||
+ | List<P> pageRefList = api.getQuery().getAllpages(); | ||
+ | assertEquals(lwiki.getSiteurl(),3, pageRefList.size()); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
=== Edit === | === Edit === | ||
https://www.mediawiki.org/wiki/API:Edit | https://www.mediawiki.org/wiki/API:Edit | ||
Line 51: | Line 81: | ||
wiki.edit("Test Page 1","new content of test page 1","Summary for this edit"); | wiki.edit("Test Page 1","new content of test page 1","Summary for this edit"); | ||
</source> | </source> | ||
− | * [https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/ | + | [https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/TestAPI_Edit.java TestAPI_Edit.java JUnit test] |
+ | === Multiple Implementations === | ||
+ | {{CurrentVersion}} supports to use different implementations via the common MediawikiApi Java Interface. E.g. you can use the one-class implementation org.wikipedia.Mediawiki. | ||
+ | <source lang='java'> | ||
+ | @Test | ||
+ | /** | ||
+ | * Test for issue #3 | ||
+ | * @throws Exception | ||
+ | */ | ||
+ | public void testMultipleImplementations() throws Exception { | ||
+ | MediawikiApi[] wikiapis = { new Mediawiki(), new org.wikipedia.Mediawiki() }; | ||
+ | for (MediawikiApi wikiapi : wikiapis) { | ||
+ | wikiapi.setSiteurl("http://en.wikipedia.org"); | ||
+ | String content = wikiapi.getPageContent("Main Page"); | ||
+ | assertTrue(content.contains("Wikipedia")); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | [https://github.com/WolfgangFahl/Mediawiki-Japi/blob/master/src/test/java/com/bitplan/mediawiki/japi/TestUsage.java TestUsage.java JUnit test] |
Latest revision as of 15:23, 20 August 2018
Intro
Depending on your version of Mediawiki you'll get different API details and help pages:
API usage examples
You might have to change the DEFAULT_SCRIPTPATH depending on your wiki configuration see Issue26
Sample query:get a single page
http://www.mediawiki.org/wiki/API:Query#Sample_query
Mediawiki wiki=new Mediawiki("http://en.wikipedia.org");
String content=wiki.getPageContent("Main Page");
/**
* http://www.mediawiki.org/wiki/API:Query#Sample_query
* http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&titles=Main%20Page&format=xml
* @throws Exception
*/
@Test
public void testSampleQuery() throws Exception {
Mediawiki wiki=new Mediawiki("http://en.wikipedia.org");
String content=wiki.getPageContent("Main Page");
assertTrue(content.contains("Wikipedia"));
}
login/logout
http://www.mediawiki.org/wiki/API:Login
Login login=wiki.login("scott","tiger");
wiki.logout();
/**
* test Login and logout
* see <a href='http://www.mediawiki.org/wiki/API:Login'>API:Login</a>
* @throws Exception
*/
@Test
public void testLogin() throws Exception {
WikiUser wuser=WikiUser.getUser("mediawiki_org");
Login login=wiki.login(wuser.getUsername(),wuser.getPassword());
assertEquals("Success",login.getResult());
assertNotNull(login.getLguserid());
assertEquals(wuser.getUsername(),login.getLgusername());
assertNotNull(login.getLgtoken());
wiki.logout();
}
query
Allpages
/**
* test the Allpages API call http://www.mediawiki.org/wiki/API:Allpages
*
* @throws Exception
*/
@Test
public void testGetAllPages() throws Exception {
for (ExampleWiki lwiki : getWikis()) {
Api api = getQueryResult(lwiki, "&list=allpages&apfrom=Kre&aplimit=3");
List<P> pageRefList = api.getQuery().getAllpages();
assertEquals(lwiki.getSiteurl(),3, pageRefList.size());
}
}
Edit
https://www.mediawiki.org/wiki/API:Edit
wiki.login("scott","tiger");
wiki.edit("Test Page 1","new content of test page 1","Summary for this edit");
Multiple Implementations
0.1.06 supports to use different implementations via the common MediawikiApi Java Interface. E.g. you can use the one-class implementation org.wikipedia.Mediawiki.
@Test
/**
* Test for issue #3
* @throws Exception
*/
public void testMultipleImplementations() throws Exception {
MediawikiApi[] wikiapis = { new Mediawiki(), new org.wikipedia.Mediawiki() };
for (MediawikiApi wikiapi : wikiapis) {
wikiapi.setSiteurl("http://en.wikipedia.org");
String content = wikiapi.getPageContent("Main Page");
assertTrue(content.contains("Wikipedia"));
}
}