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

Commit 0dc645f

Browse files
committed
Updated php-export-all-books script
- Added better support for markdown via extension. - Removed old fix for old 2020 bookstack bug. Related to #11
1 parent fffd11d commit 0dc645f

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

Diff for: php-export-all-books/export-books.php

+9-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@
1717
// Script logic
1818
////////////////
1919

20+
// Get all list of all books in the system
2021
$books = getAllBooks();
22+
// Get a reference to our output location
2123
$outDir = realpath($exportLocation);
2224

25+
// Mapping for export formats to the resulting export file extensions
2326
$extensionByFormat = [
2427
'pdf' => 'pdf',
2528
'html' => 'html',
2629
'plaintext' => 'txt',
30+
'markdown' => 'md',
2731
];
2832

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.
2936
foreach ($books as $book) {
3037
$id = $book['id'];
3138
$extension = $extensionByFormat[$exportFormat] ?? $exportFormat;
@@ -37,7 +44,7 @@
3744
/**
3845
* Get all books from the system API.
3946
*/
40-
function getAllBooks() {
47+
function getAllBooks(): array {
4148
$count = 100;
4249
$offset = 0;
4350
$total = 0;
@@ -47,12 +54,7 @@ function getAllBooks() {
4754
$endpoint = 'api/books?' . http_build_query(['count' => $count, 'offset' => $offset]);
4855
$resp = apiGetJson($endpoint);
4956

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-
57+
$total = $resp['total'] ?? 0;
5658
$newBooks = $resp['data'] ?? [];
5759
array_push($allBooks, ...$newBooks);
5860
$offset += $count;

Diff for: php-export-all-books/readme.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Export All Books
22

3-
This script will export all books in your preferred format (PDF, HTML or TXT).
3+
This script will export all books in your preferred format (PDF, HTML, Markdown or TXT).
44

55
## Requirements
66

@@ -34,4 +34,7 @@ php export-books.php pdf ./
3434

3535
# Export as HTML to an existing "html" directory
3636
php export-books.php html ./html
37+
38+
# Export as Markdown to an existing "md-files" directory
39+
php export-books.php markdown ./md-files
3740
```

0 commit comments

Comments
 (0)