PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/confluence/contentImportUtilities/importWithPerl/restExample.pl

https://bitbucket.org/SaiPatel/keysight-plugins-for-atlassian-products
Perl | 182 lines | 132 code | 29 blank | 21 comment | 26 complexity | 851184a3823521cb72a59c2632c03f32 MD5 | raw file
Possible License(s): Apache-2.0
  1. #!/usr/bin/perl
  2. use strict;
  3. use FindBin;
  4. use lib "$FindBin::Bin/../perllib.custom";
  5. use Getopt::Std;
  6. use File::Basename;
  7. use MIME::Base64;
  8. use Confluence;
  9. use Confluence::Utilities;
  10. $| = 1;
  11. my %options;
  12. my $usage = "This is an example script for using Perl to interact with the Confluence REST API.\n"
  13. . "Two of the methods - set page author and set page creation date - require the Entity \n"
  14. . "Authorship plugin as the default Confluence REST Interface does not support those operations.\n"
  15. . "\n"
  16. . "https://bitbucket.org/selberg/entity-authorship\n"
  17. . "\n"
  18. . "By default, the script will look to find the user credentials in a file called \"credentials.txt\"\n"
  19. . "located in the current directory. The file can be created using the script \"makeCredentials.pl\"\n"
  20. . "The credentials can also be passed in directly using the switches \"-U username -P password\" or\n"
  21. . "explicitly pointing to a credentials file with \"-C credentialsFile.txt\"\n"
  22. . "\n"
  23. . "-U [username]\n"
  24. . "-P [password]\n"
  25. . "-C [credentialsFile]\n"
  26. . "\n"
  27. . "-u [confluence url] => such as http://localhost:1990/confluence\n"
  28. . "-s [spaceKey]\n"
  29. . "-p [pageTitle] sets the page to get, attach a file to or the parent page for page creation\n"
  30. . "\n"
  31. . "-g => get the page\n"
  32. . "-c [filename] => create a new page with the title\n"
  33. . "-n [filename] => update an existing page with New content\n"
  34. . "-a [attachment] => attach the specified file to the page\n"
  35. . "-t [title for new page] => Set the title for a new or updated page. Defaults to the filename or the existing page name.\n"
  36. . "-d => debug mode (same as verbose mode)\n"
  37. . "-v => verbose mode\n"
  38. . "\n"
  39. . "Examples (expecting to upload to the demo space of an SDK instance of Confluence on the localhost)\n"
  40. . "\n"
  41. . "Get Page ID\n"
  42. . "$0 -g\n"
  43. . "\n"
  44. . "Upload HTML to a New Page\n"
  45. . "$0 -c new.html -t \"New Page\"\n"
  46. . "\n"
  47. . "Update a Page with new content\n"
  48. . "$0 -n update.html -t \"Updated Page\"\n"
  49. . "\n"
  50. . "Upload Attachment to an existing page\n"
  51. . "$0 -a BlueCircle.png -p \"Updated Page\"\n";
  52. my $confluence = 'Confluence'->new();
  53. my $credentialsFile = 'credentials.txt';
  54. my $confluenceUrl = 'http://localhost:1990/confluence';
  55. my $spaceKey = 'ds';
  56. my $pageTitle = 'Welcome to the Confluence Demonstration Space';
  57. my $pageId;
  58. my $pageContents;
  59. my $action;
  60. my $bDebug;
  61. my $newPageTitle;
  62. my $content;
  63. my %updateOptions;
  64. getopts( "U:P:C:u:s:p:gc:n:a:t:dv", \%options );
  65. if( $options{'g'} ){
  66. $action = "GET_PAGE";
  67. } elsif( $options{'c'} and -f $options{ 'c' } ){
  68. $action = "CREATE_PAGE";
  69. } elsif( $options{'n'} and -f $options{ 'n' } ){
  70. $action = "UPDATE_PAGE";
  71. } elsif( $options{'a'} and -f $options{ 'a' } ){
  72. $action = "ATTACH_TO_PAGE";
  73. } else {
  74. die $usage;
  75. }
  76. # -------------------------------------------------------------------------------------- #
  77. # Override defaults with command line pass parameters
  78. # -------------------------------------------------------------------------------------- #
  79. $confluenceUrl = $options{'u'} if $options{'u'};
  80. $spaceKey = $options{'s'} if $options{'s'};
  81. $pageTitle = $options{'p'} if $options{'p'};
  82. $bDebug = 1 if $options{'d'};
  83. $bDebug = 1 if $options{'v'};
  84. # -------------------------------------------------------------------------------------- #
  85. # Initialize the Confluence Object. Under the hood, this is just a
  86. # hash with associated methods to keep track of things like the user
  87. # credentials and confluence URL.
  88. # -------------------------------------------------------------------------------------- #
  89. $confluence->confluenceUrl( $confluenceUrl );
  90. if( $options{'U'} and $options{'P'} ){
  91. $confluence->credentials( encode_base64( $options{'U'} . ":" . $options{'P'} ));
  92. } else {
  93. $credentialsFile = $options{'C'} if $options{'C'} and -e $options{'C'};
  94. $confluence->credentials( sReadFile( $credentialsFile ) );
  95. }
  96. $confluence->bDebug( 1 ) if $bDebug;
  97. # -------------------------------------------------------------------------------------- #
  98. # Get a Page's ID
  99. # -------------------------------------------------------------------------------------- #
  100. if( $action =~ /GET_PAGE/ ){
  101. print "Getting Page\n\n";
  102. $pageId = $confluence->getPageId( $spaceKey, $pageTitle );
  103. if( $pageId and $pageId =~ /\w/ ){
  104. print( "Page Found: $pageId\n" );
  105. $pageContents = $confluence->getPageContents( $pageId );
  106. if( $pageContents and $pageContents =~ /\w/ ){
  107. print( "Page Contents;\n$pageContents\n" );
  108. } else {
  109. print( "No Page Contents Found\n" );
  110. }
  111. } else {
  112. print( "Page Not Found\n" );
  113. }
  114. } elsif( $action =~ /CREATE_PAGE/ ){
  115. print "Creating Page\n\n";
  116. # Uncomment the below to use the Entity Authorship Rest methods to set the author and creation date
  117. # $confluence->author( $author ); # Needs to be a known userid in Confluence
  118. # $confluence->creationDate( $date ); # Date::Manip object or parseable string
  119. if( $options{ 't' } ){
  120. $newPageTitle = $options{'t'};
  121. } else {
  122. $newPageTitle = $options{'c'};
  123. }
  124. $content = sReadFile( $options{ 'c' } );
  125. $pageId = $confluence->createPage( $spaceKey, $newPageTitle, $content, { 'PARENT_PAGE_TITLE' => $pageTitle } );
  126. if( $pageId and $pageId =~ /\w/ ){
  127. print( "Created Page: $pageId\n" );
  128. }
  129. } elsif( $action =~ /UPDATE_PAGE/ ){
  130. print "Updating Page\n\n";
  131. # Uncomment the below to use the Entity Authorship Rest methods to set the author and creation date
  132. # $confluence->author( $author ); # Needs to be a known userid in Confluence
  133. # $confluence->creationDate( $date ); # Date::Manip object or parseable string
  134. if( $options{ 't' } ){
  135. $updateOptions{ 'TITLE' } = $options{'t'};
  136. }
  137. $content = sReadFile( $options{ 'n' } );
  138. $pageId = $confluence->updatePage( $spaceKey, $pageTitle, $content, \%updateOptions );
  139. if( $pageId and $pageId =~ /\w/ ){
  140. print( "Created Page: $pageId\n" );
  141. }
  142. } elsif( $action =~ /ATTACH_TO_PAGE/ ){
  143. # Uncomment the below to use the Entity Authorship Rest methods to set the author and creation date
  144. # $confluence->author( $author ); # Needs to be a known userid in Confluence
  145. # $confluence->creationDate( $date ); # Date::Manip object or parseable string
  146. print "Getting Page Id\n\n";
  147. $pageId = $confluence->getPageId( $spaceKey, $pageTitle );
  148. print "Attaching To Page\n\n";
  149. $pageId = $confluence->addAttachment( $pageId, $options{ 'a' } );
  150. if( $pageId and $pageId =~ /\w/ ){
  151. print( "Attachment Added: $pageId\n" );
  152. }
  153. }