Skip to content

Examples

fastily edited this page Mar 25, 2018 · 9 revisions

GETTING STARTED

  1. Clone the jwiki starter pack with git clone https://github.com/fastily/jwiki-starter.git
  2. Make changes to JwikiExample.java under src/main/java/jwikistarter/
  3. Have fun!

CONTENTS

ACTIONS

Login

// Logs in to the English Wikipedia (you must have an account for this to work).
wiki.login("<YOUR_USERNAME>", "<YOUR_PASSWORD>")

Edit

// Test edits Wikipedia:Sandbox.
wiki.edit("Wikipedia:Sandbox", "Some test text", "Edit Summary");

Upload a file

// Uploads a file.  You need to have an autoconfirmed/confirmed account and be logged in.
wiki.upload(Paths.get("<PATH_TO_YOUR_TEST_FILE>"), "<TITLE_TO_UPLOAD_FILE_TO>", "This is a test", "test summary");

QUERIES

Get Page Text

// Gets the text of the main page and prints it.
System.out.println(wiki.getPageText("Main Page"));

Get Page Author

// Gets the author of the Main Page and prints it
System.out.println(wiki.getPageCreator("Main Page"));

Get categories of a page

// Gets the categories on the Main Page and prints them
System.out.println(wiki.getCategoriesOnPage("Main Page"));

Get the links on a page

// Gets all wiki-links on a page (GitHub) and prints them
System.out.println(wiki.getLinksOnPage("GitHub"));

Get links pointing to a page

// Gets all wiki-links pointing to the the Stack Overflow page and prints them
System.out.println(wiki.whatLinksHere("Stack Overflow"));

Check if one page exists

// Check if a page (GitHub) exists and print true/false.
System.out.println(wiki.exists("GitHub"));

Check if multiple pages exist

// Uses an efficient multi-query to check if the following pages exist, and then prints the ones that do.
System.out.println(MQuery.exists(wiki, true, FL.toSAL("Stack Overflow", "BlahBlahBlahDoesNotExist", "Main Page"));

Gets the first paragraph of a page

// Gets the first paragraph of the Stack Overflow page and prints it.
System.out.println(wiki.getTextExtract("Stack Overflow"));

Get 5 random articles

// Gets 5 random articles (main namespace) on Wikipedia and print the result.
System.out.println(wiki.getRandomPages(5, NS.MAIN));
Clone this wiki locally