]> BookStack Code Mirror - devops/blob - meta-scripts/bookstack-new-release-blogpost
Debian 13 script: Fixed mysql use for mariadb, removed composer use
[devops] / meta-scripts / bookstack-new-release-blogpost
1 #!/usr/bin/env php
2 <?php
3
4 // Script to create a new blogpost, with a cover image created at the right size
5 // and attribution ready set.
6
7 $siteDir = '/home/dan/web/bs-site';
8
9 $key = getenv('UNSPLASH_ACCESS_KEY');
10 if (!$key) {
11     error("Unsplash access key needs to be set on [UNSPLASH_ACCESS_KEY] environment variable to run this script");
12 }
13
14 $unsplashId = read("Enter the unsplash image ID:");
15 $imageDesc = read("Enter a short one/two word description of the image:");
16 $version = read("Enter the BookStack version (eg. v21.03 or v21.12.3):");
17
18 // Fetch and write out image
19 $image = unsplashGet($unsplashId, $key);
20 $imageContent = getImageContent($image, 2100, 800, 60);
21 $imageOutName = str_replace(' ', '-', strtolower($imageDesc)) . "-" . $image->user->username . '.jpg';
22 $imageOutPath = implode(DIRECTORY_SEPARATOR, [$siteDir, "static/images/blog-cover-images", $imageOutName]);
23 file_put_contents($imageOutPath, $imageContent);
24
25 // Write out blogpost
26 $hyphenVersion = str_replace('.', '-', trim($version));
27 $postMdName = "bookstack-release-{$hyphenVersion}.md";
28 $postMdOutPath = implode(DIRECTORY_SEPARATOR, [$siteDir, "content/blog", $postMdName]);
29 $postMdContent = getPostMarkdown();
30 file_put_contents($postMdOutPath, $postMdContent);
31
32 output("Done, Post created at {$postMdOutPath}");
33
34 function unsplashGet(string $id, string $unsplashKey) {
35     $json = file_get_contents("https://api.unsplash.com/photos/{$id}?client_id={$unsplashKey}");
36     return json_decode($json);
37 }
38
39 function getImageContent($unsplashImage, int $width, int $height, int $quality) {
40     $url = $unsplashImage->urls->raw;
41     $url .= "&fm=jpg&w={$width}&h={$height}&fit=crop&q={$quality}&crop=focalpoint,center";
42     return file_get_contents($url);
43 }
44
45 function getPostMarkdown() {
46     global $image, $imageContent, $version, $hyphenVersion, $imageOutName;
47     $date = str_replace('+00:00', 'Z', date('c'));
48     $content = <<<POSTCONTENT
49 +++
50 categories = ["Releases"]
51 tags = ["Releases"]
52 title = "BookStack Release $version"
53 date = $date
54 author = "Dan Brown"
55 image = "/images/blog-cover-images/$imageOutName"
56 slug = "bookstack-release-$hyphenVersion"
57 draft = false
58 +++
59
60 Intro
61
62 * [Update instructions](https://www.bookstackapp.com/docs/admin/updates)
63 * [GitHub release page](https://github.com/BookStackApp/BookStack/releases/tag/$version)
64
65 **Upgrade Notices**
66
67 TODO
68
69 - **Title** - Detail
70
71 TODO - Video
72
73 ### New Feature A
74
75 TODO
76
77 ### Translations
78
79 TODO
80
81 - Username - *Language*
82
83 ### Next Steps
84
85 TODO
86
87 ### Full List of Changes
88
89 **Released in $version**
90
91 TODO
92
93 ----
94
95 <span style="font-size: 0.8em;opacity:0.9;">Header Image Credits: <span>Photo by <a href="https://unsplash.com/@{$image->user->username}?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">{$image->user->name}</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></span></span>
96 POSTCONTENT;
97     return $content;
98 }
99
100 function error($text) {
101         echo $text . "\n";
102         exit(1);
103 }
104
105 function output($text) {
106     echo $text . "\n";
107 }
108
109 function read($question): string {
110     $response = readline($question);
111     if (!$response) {
112         error("Failed to respond to question (" . $question . ")");
113     }
114
115     return $response;
116 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.