File tree Expand file tree Collapse file tree 2 files changed +13
-8
lines changed Expand file tree Collapse file tree 2 files changed +13
-8
lines changed Original file line number Diff line number Diff line change 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.
2936foreach ($ books as $ book ) {
3037 $ id = $ book ['id ' ];
3138 $ extension = $ extensionByFormat [$ exportFormat ] ?? $ exportFormat ;
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 ;
Original file line number Diff line number Diff line change 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
3636php 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```
You can’t perform that action at this time.
0 commit comments