]> BookStack Code Mirror - api-scripts/blob - php-export-all-books/export-books.php
Updated php-export-all-books script
[api-scripts] / php-export-all-books / export-books.php
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 // Get all list of all books in the system
21 $books = getAllBooks();
22 // Get a reference to our output location
23 $outDir = realpath($exportLocation);
24
25 // Mapping for export formats to the resulting export file extensions
26 $extensionByFormat = [
27     'pdf' => 'pdf',
28     'html' => 'html',
29     'plaintext' => 'txt',
30     'markdown' => 'md',
31 ];
32
33 // Loop over each book, exporting each one-by-one and saving its
34 // contents into the output location, using the books slug as
35 // the file name.
36 foreach ($books as $book) {
37     $id = $book['id'];
38     $extension = $extensionByFormat[$exportFormat] ?? $exportFormat;
39     $content = apiGet("api/books/{$id}/export/{$exportFormat}");
40     $outPath = $outDir  . "/{$book['slug']}.{$extension}";
41     file_put_contents($outPath, $content);
42 }
43
44 /**
45  * Get all books from the system API.
46  */
47 function getAllBooks(): array {
48     $count = 100;
49     $offset = 0;
50     $total = 0;
51     $allBooks = [];
52
53     do {
54         $endpoint = 'api/books?' . http_build_query(['count' => $count, 'offset' => $offset]);
55         $resp = apiGetJson($endpoint);
56
57         $total = $resp['total'] ?? 0;
58         $newBooks = $resp['data'] ?? [];
59         array_push($allBooks, ...$newBooks);
60         $offset += $count;
61     } while ($offset < $total);
62
63     return $allBooks;
64 }
65
66 /**
67  * Make a simple GET HTTP request to the API.
68  */
69 function apiGet(string $endpoint): string {
70     global $apiUrl, $clientId, $clientSecret;
71     $url = rtrim($apiUrl, '/') . '/' . ltrim($endpoint, '/');
72     $opts = ['http' => ['header' => "Authorization: Token {$clientId}:{$clientSecret}"]];
73     $context = stream_context_create($opts);
74     return file_get_contents($url, false, $context);
75 }
76
77 /**
78  * Make a simple GET HTTP request to the API &
79  * decode the JSON response to an array.
80  */
81 function apiGetJson(string $endpoint): array {
82     $data = apiGet($endpoint);
83     return json_decode($data, true);
84 }
85
86 /**
87  * DEBUG: Dump out the given variables and exit.
88  */
89 function dd(...$args) {
90     foreach ($args as $arg) {
91         var_dump($arg);
92     }
93     exit(1);
94 }