BookStack HTML Importer

descriptionStandard

This project was put together to aid in migrating content from Atlassian Confluence to BookStack.

It requires that the Confluence Space in question is first exported to HTML (built-in). Then that HTML can be imported to BookStack using this API helper library.

My own requirements were simplistic for the import task; in its default state it will create a Book using the name you provide and import HTML from the path provided (See examples/importer.php). Each HTML document will create a new page within the book using the file name as the page name.

Feel free to adapt this to your own requirements.

Usage Instructions

See examples/importer.php for a working example.

Essentially you want to do the following:

  • Exported a space to HTML.
  • Extract the space and copy the path to the location.
  • Define a Book name and include the book name and imported path for the HTML in the file: importer.php lines: 13-14.
// Set the Book and HTML import path here
$book_title = "bookname";
$read_path = "path-to-html";
  • Run the import task; which will create a Book and imported each HTML page as a page with the Book.

To test you could create a credentials file: credentials.php and include your Rest API credentials from BookStack:

$credentials = array(
	'url' => "<bookstack-path>",
	'id' => "<id>",
	'secret' => "<api-token>"
);

Current functions

These are provided by: bookstack_client.php

  • get_shelves – List all shelves
  • get_books – List all books
  • create_book – Create a book; supports checks for existing books to prevent duplicates.
  • create_page – Create a page within a book.

The Importer Script

// Create book
$book_id = $europa->create_book($book_title);

// Create page in book
if (isset($book_id) && is_integer($book_id)) {
	// Load files from directory
	$iterator = new DirectoryIterator($read_path);

	// Iterate over files
	foreach ($iterator as $fileinfo) {
		if (!$fileinfo->isDot()) {
			$file_name = $fileinfo->getFilename();

			// Only handle files that end with .html extension
			if ($europa->ends_with($file_name, ".html")) {
				$title = str_replace(array("-", "_"), " ", $file_name);
				$title = str_replace(array(".html"), "", $title);
				var_dump(
					array($title, $book_id)
				);

				// Build a payload to createa a page
				$payload = array(
					"book_id" => $book_id,
					"name" => $title,
					"html" => file_get_contents("{$read_path}\\{$file_name}"),
					//"tags" => array()
				);

				$page = $europa->create_page($payload);
			}
		}
	}
}

One thought on “BookStack HTML Importer

  1. avatar Rea Bancroft

    Everything is very open with a precise clarification of the issues. It was definitely informative. Your website is very useful. Thank you for sharing!

Comments are closed.