]> BookStack Code Mirror - api-scripts/commitdiff
Updated php-export-all-books script
authorDan Brown <redacted>
Sat, 9 Sep 2023 11:26:22 +0000 (12:26 +0100)
committerDan Brown <redacted>
Sat, 9 Sep 2023 11:26:22 +0000 (12:26 +0100)
- Added better support for markdown via extension.
- Removed old fix for old 2020 bookstack bug.

Related to #11

php-export-all-books/export-books.php
php-export-all-books/readme.md

index a0f9ff954be6c1251fa6751fb4323bd14a32004c..d2561b79fec3bcee13e3d08e8d0c0b666142a87e 100644 (file)
@@ -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;
index b076a395c4b3e53f3283f5d37d2be5df6a841749..fbb0cf587624ca0aface32044753f29956010a79 100644 (file)
@@ -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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.