From: Dan Brown Date: Sat, 9 Sep 2023 11:26:22 +0000 (+0100) Subject: Updated php-export-all-books script X-Git-Url: http://source.bookstackapp.com/api-scripts/commitdiff_plain/0dc645ff7bcd46e21f2806697d440e648b69bd08?ds=sidebyside Updated php-export-all-books script - Added better support for markdown via extension. - Removed old fix for old 2020 bookstack bug. Related to #11 --- diff --git a/php-export-all-books/export-books.php b/php-export-all-books/export-books.php index a0f9ff9..d2561b7 100644 --- a/php-export-all-books/export-books.php +++ b/php-export-all-books/export-books.php @@ -17,15 +17,22 @@ $exportLocation = $argv[2] ?? './'; // Script logic //////////////// +// Get all list of all books in the system $books = getAllBooks(); +// Get a reference to our output location $outDir = realpath($exportLocation); +// Mapping for export formats to the resulting export file extensions $extensionByFormat = [ 'pdf' => 'pdf', 'html' => 'html', 'plaintext' => 'txt', + 'markdown' => 'md', ]; +// Loop over each book, exporting each one-by-one and saving its +// contents into the output location, using the books slug as +// the file name. foreach ($books as $book) { $id = $book['id']; $extension = $extensionByFormat[$exportFormat] ?? $exportFormat; @@ -37,7 +44,7 @@ foreach ($books as $book) { /** * Get all books from the system API. */ -function getAllBooks() { +function getAllBooks(): array { $count = 100; $offset = 0; $total = 0; @@ -47,12 +54,7 @@ function getAllBooks() { $endpoint = 'api/books?' . http_build_query(['count' => $count, 'offset' => $offset]); $resp = apiGetJson($endpoint); - // Only set total on first request, due to API bug: - // https://github.com/BookStackApp/BookStack/issues/2043 - if ($offset == 0) { - $total = $resp['total'] ?? 0; - } - + $total = $resp['total'] ?? 0; $newBooks = $resp['data'] ?? []; array_push($allBooks, ...$newBooks); $offset += $count; diff --git a/php-export-all-books/readme.md b/php-export-all-books/readme.md index b076a39..fbb0cf5 100644 --- a/php-export-all-books/readme.md +++ b/php-export-all-books/readme.md @@ -1,6 +1,6 @@ # Export All Books -This script will export all books in your preferred format (PDF, HTML or TXT). +This script will export all books in your preferred format (PDF, HTML, Markdown or TXT). ## Requirements @@ -34,4 +34,7 @@ php export-books.php pdf ./ # Export as HTML to an existing "html" directory php export-books.php html ./html + +# Export as Markdown to an existing "md-files" directory +php export-books.php markdown ./md-files ``` \ No newline at end of file