How to programmatically generate the tiny link of a Confluence page

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform notice: Server and Data Center only. This article only applies to Atlassian products on the server and data center platforms.

Purpose

Tiny links to Confluence pages are a good way to share content with other users. The tiny link is shown when you share a page using the share button and can be retrieved when you view the page information. They are generated based on the PageId after converting it to a byte array and encoding it in base64.

Sometimes you may need to programmatically generate the tiny link for one or multiple pages from your Confluence instance. This document will guide you on this task.

Solution

We’ll generate the list of tiny links in two steps, getting all the pages, and generating each link.

Step 1: Get All Pages

First, get all pages in the instance with the following SQL query. This query can be adjusted to only return pages from a given space or user as well.

SELECT S.LOWERSPACEKEY, S.SPACENAME, TITLE, CONTENTID
FROM CONTENT
JOIN SPACES S on CONTENT.SPACEID = S.SPACEID
WHERE CONTENTTYPE = 'PAGE'
    AND PREVVER IS NULL
    AND CONTENT_STATUS = 'current';

Export the results of the query above to a CSV format. It should resemble the following:

ds,Demonstration Space,Welcome to Confluence,98319
ds,Demonstration Space,What is Confluence? (step 1 of 9),98320
ds,Demonstration Space,A quick look at the editor (step 2 of 9),98322
ds,Demonstration Space,Let's edit this page (step 3 of 9),98317
ds,Demonstration Space,Prettify the page with an image (step 4 of 9),98318
ds,Demonstration Space,Get serious with a table (step 5 of 9),98332
ds,Demonstration Space,Lay out your page (step 6 of 9),98321
ds,Demonstration Space,Learn the wonders of autoconvert (step 7 of 9),98314
ds,Demonstration Space,Tell people what you think in a comment (step 8 of 9),98305
ds,Demonstration Space,Share your page with a team member (step 9 of 9),98306

The solution below is based on a Perl script. You may change it according to your need and also port it to your preferred coding language.

The main steps of the algorithm are:

  1. Convert the page ID to its byte array.
  2. Transform the byte array using the base64 encoding.
  3. Perform additional treatment on the resulting encoded string.

Make sure to replace the connieBaseUrl variable with the base URL for your instance. Place the CSV from step one next to the script, and rename it to pages.csv.

Perl script to create the tiny link of a Confluence page
use MIME::Base64 qw(encode_base64);

my $connieBaseUrl = 'http://example.com/confluence'; ### Confluence Base URL
my $file = 'pages.csv';

open my $info, $file or die "Could not open $file: $!";

while( my $line = <$info>)  {   
    my @page = split(",",$line);
	my $pageID = @page[3];
	$pageID =~ s/^\s+|\s+$//g;


	my $tinyString = encode_base64(pack("L", $pageID)); ### the page ID must be encoded after converting it to a byte array
	my $actualTinyString = '';
	my $padding = 0;
	foreach my $c (split //, $tinyString)
	{
		if ($c eq '=')
		{ next; }
		if ($padding == 1 && $c eq 'A')
		{ next; }

		$padding = 0;
		if ($c eq '/')
		{
			$actualTinyString .= '-';
		}
		elsif ($c eq '+')
		{
			$actualTinyString .= '_';
		}
		elsif ($c eq "\n")
		{
			$actualTinyString .= '/';
		}
		else
		{
			$actualTinyString .= $c;
		}
	}

	my $tinyUrl = $connieBaseUrl . '/x/' . $actualTinyString;

	$line =~ s/^\s+|\s+$//g;
	print $line.",".$tinyUrl . "\n";
}

close $info;






Last modified on Nov 16, 2022

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.