Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit a62c7f1

Browse files
committed
Added export-all-books example
1 parent 251489e commit a62c7f1

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
# api-scripts
2-
Examples of BookStack API scripts
1+
# BookStack API Scripts
2+
3+
This repository contains different examples of BookStack API scripts that you might find useful to use or modify.
4+
5+
6+
Each folder within this repo is a different script. Each script has it's own readme. Click into a folder to see the readme for detail about the script.
7+
8+
9+
These scripts are not part an officially supported part of the BookStack project itself and therefore may be outdated or more likely to have bugs.

php-export-all-books/export-books.php

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
// API Credentials
5+
// You can either provide them as environment variables
6+
// or hard-code them in the empty strings below.
7+
$apiUrl = getenv('BS_URL') ?: ''; // http://bookstack.local/
8+
$clientId = getenv('BS_TOKEN_ID') ?: '';
9+
$clientSecret = getenv('BS_TOKEN_SECRET') ?: '';
10+
11+
// Export Format & Location
12+
// Can be provided as a arguments when calling the script
13+
// or be hard-coded as strings below.
14+
$exportFormat = $argv[1] ?? 'pdf';
15+
$exportLocation = $argv[2] ?? './';
16+
17+
// Script logic
18+
////////////////
19+
20+
$books = getAllBooks();
21+
$outDir = realpath($exportLocation);
22+
23+
$extensionByFormat = [
24+
'pdf' => 'pdf',
25+
'html' => 'html',
26+
'plaintext' => 'txt',
27+
];
28+
29+
foreach ($books as $book) {
30+
$id = $book['id'];
31+
$extension = $extensionByFormat[$exportFormat] ?? $exportFormat;
32+
$content = apiGet("api/books/{$id}/export/{$exportFormat}");
33+
$outPath = $outDir . "/{$book['slug']}.{$extension}";
34+
file_put_contents($outPath, $content);
35+
}
36+
37+
/**
38+
* Get all books from the system API.
39+
*/
40+
function getAllBooks() {
41+
$count = 100;
42+
$offset = 0;
43+
$total = 0;
44+
$allBooks = [];
45+
46+
do {
47+
$endpoint = 'api/books?' . http_build_query(['count' => $count, 'offset' => $offset]);
48+
$resp = apiGetJson($endpoint);
49+
50+
// Only set total on first request, due to API bug:
51+
// https://github.com/BookStackApp/BookStack/issues/2043
52+
if ($offset == 0) {
53+
$total = $resp['total'] ?? 0;
54+
}
55+
56+
$newBooks = $resp['data'] ?? [];
57+
array_push($allBooks, ...$newBooks);
58+
$offset += $count;
59+
} while ($offset < $total);
60+
61+
return $allBooks;
62+
}
63+
64+
/**
65+
* Make a simple GET HTTP request to the API.
66+
*/
67+
function apiGet(string $endpoint): string {
68+
global $apiUrl, $clientId, $clientSecret;
69+
$url = rtrim($apiUrl, '/') . '/' . ltrim($endpoint, '/');
70+
$opts = ['http' => ['header' => "Authorization: Token {$clientId}:{$clientSecret}"]];
71+
$context = stream_context_create($opts);
72+
return file_get_contents($url, false, $context);
73+
}
74+
75+
/**
76+
* Make a simple GET HTTP request to the API &
77+
* decode the JSON response to an array.
78+
*/
79+
function apiGetJson(string $endpoint): array {
80+
$data = apiGet($endpoint);
81+
return json_decode($data, true);
82+
}
83+
84+
/**
85+
* DEBUG: Dump out the given variables and exit.
86+
*/
87+
function dd(...$args) {
88+
foreach ($args as $arg) {
89+
var_dump($arg);
90+
}
91+
exit(1);
92+
}

php-export-all-books/readme.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Export All Books
2+
3+
This script will export all books in your preferred format (PDF, HTML or TXT).
4+
5+
## Requirements
6+
7+
You will need php (~7.1+) installed on the machine you want to run this script on.
8+
You will also need BookStack API credentials (TOKEN_ID & TOKEN_SECRET) at the ready.
9+
10+
## Running
11+
12+
```bash
13+
# Downloading the script
14+
curl https://raw.githubusercontent.com/BookStackApp/api-scripts/main/php-export-all-books/export-books.php > export-books.php
15+
16+
# Setup
17+
# ALTERNATIVELY: Open the script and edit the variables at the top.
18+
export BS_URL=https://bookstack.example.com # Set to be your BookStack base URL
19+
export BS_TOKEN_ID=abc123 # Set to be your API token_id
20+
export BS_TOKEN_SECRET=123abc # Set to be your API token_secret
21+
22+
# Running the script
23+
php export-books.php <format> <output_dir>
24+
```
25+
26+
## Examples
27+
28+
```bash
29+
# Export as plaintext to an existing "out" directory
30+
php export-books.php plaintext ./out
31+
32+
# Export as pdf to the current directory
33+
php export-books.php pdf ./
34+
35+
# Export as HTML to an existing "html" directory
36+
php export-books.php html ./html
37+
```

0 commit comments

Comments
 (0)