Changes for page XML-RPC Java Examples

Last modified by Thomas Mortagne on 2023/10/10 14:31

From version 1.4
edited by Vincent Massol
on 2010/01/27 00:08
Change comment: fix header levels
To version 1.5
edited by dilipkumarj
on 2010/01/30 04:17
Change comment: Added code to get version history for a Page

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -xwiki:XWiki.VincentMassol
1 +xwiki:XWiki.dilipkumarj
Content
... ... @@ -332,3 +332,79 @@
332 332  }
333 333  
334 334  {{/code}}
335 +
336 +
337 +=== Page: Get Page History ===
338 +
339 +Now, that we are adding content to Pages, there are going to be versions of the same document. These versions are called History of the Page. The XWiki API has methods available to access the history of the Page. The class that would help us in this case is the XWikiPageHistorySummary class.
340 +
341 +{{info}}
342 +Please note that the methods of XWikiPageHistorySummary class only help you access the various historical revisions of the document & not the content of the document itself. Eg. You would get to know that a particular Page called Main.WebHome has highest version as 4.1 or maybe 10.9. In order to access the content of the 4.1 revision of the document, you would have to use the [[Page retrieval code provided here>>http://platform.xwiki.org/xwiki/bin/view/Features/XMLRPCJavaExamples2#HRetrievePageByMajorMinorVersion]] by passing the appropriate version of the document.
343 +{{/info}}
344 +
345 +{{code language="java"}}
346 +import java.net.MalformedURLException;
347 +import java.util.List;
348 +import org.apache.xmlrpc.XmlRpcException;
349 +import org.xwiki.xmlrpc.XWikiXmlRpcClient;
350 +import org.xwiki.xmlrpc.model.XWikiPageHistorySummary;
351 +
352 +
353 +public class PageHistory {
354 +
355 + public static void main(String[] args) throws MalformedURLException, XmlRpcException {
356 +
357 + //URL of the xwiki instance
358 + String url = "http://localhost:8080/xwiki/xmlrpc/confluence";
359 +
360 + //Replace user & pass with desired xwiki username & password
361 + String user = "Admin";
362 + String pass = "admin";
363 +
364 +
365 + XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(url);
366 + try {
367 +
368 + //Perform Login & Authentication
369 + rpc.login(user, pass);
370 +
371 + //Create a XWikiPageHistorySummary object to hold all the revisions/history
372 + //of the Main.WebHome Page
373 + List<XWikiPageHistorySummary> hist = rpc.getPageHistory("Main.WebHome");
374 +
375 + //Iterate through all the available versions for Main.WebHome
376 + for(XWikiPageHistorySummary xphs:hist){
377 +
378 + //Print the fully qualified name of the Page i.e. SpaceName.PageName
379 + //In our example this would be Main.WebHome
380 + System.out.println(xphs.getBasePageId());
381 +
382 + //Print the historical page ID or name of the Main.WebHome page
383 + //This would print something like:
384 + //Main.WebHome?minorVersion=1&language=&version=2
385 + //It means page=Main.WebHome, version=2, minor version=1 & language=default
386 + System.out.println(xphs.getId());
387 +
388 + //Printing the version of Main.WebHome in the majorversion.minorversion format
389 + System.out.println("Version: "+xphs.getVersion()+"."+xphs.getMinorVersion());
390 +
391 + //Date when the Page was last modified
392 + System.out.println(xphs.getModified());
393 +
394 + //User who last modified the Page
395 + System.out.println(xphs.getModifier());
396 +
397 + //Just a seperator between various versions of the document when printing
398 + //to console
399 + System.out.println("------------------------------");
400 + }
401 +
402 + } catch (XmlRpcException e) {
403 + System.out.println("invalid username/password was specified or communication problem or ");
404 + System.out.println(e);
405 + } finally {
406 + rpc.logout();
407 + }
408 + }
409 +}
410 +{{/code}}

Get Connected