Skip to content

Examples

fastily edited this page Jul 30, 2018 · 9 revisions

GETTING STARTED

  1. Clone the starter pack with git clone https://github.com/fastily/jwiki-starter.git
  2. Make changes to JwikiExample.java under src/main/java/jwikistarter/
  3. Run your code with ./gradlew run

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") );

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) );

MULTI QUERIES

Check if multiple pages exist

// 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")) );

Get text of multiple pages at once

// Gets page text of GitHub, Foobar, and Apple Inc.
System.out.println( MQuery.getPageText(wiki, FL.toSAL("GitHub", "Foobar", "Apple Inc.")) );