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.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

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.

Environment

Confluence Server or Data Center


Solution

The steps outlined on this article are provided AS-IS. This means we've had reports of them working for some customers — under certain circumstances — yet are not officially supported, nor can we guarantee they'll work for your specific scenario.

You may follow through and validate them on your own non-prod environments prior to production or fall back to supported alternatives if they don't work out.

We also invite you to reach out to our Community for matters that fall beyond Atlassian's scope of support!

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 May 24, 2024

Was this helpful?

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