-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkirby_publisher.php
executable file
·224 lines (198 loc) · 10.5 KB
/
kirby_publisher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/*
+-----------------------------------------------------------------+
|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
|## Kirby Publisher+++++++++++++++++++++++++++++++++++++++++++++++|
|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
|PHP class to publish Kirby articles automatically based on YAML +|
|directive.+++++++++++++++++++++++++++++++++++++++++++++++++++++++|
|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++|
+-----------------------------------------------------------------+
*/
class kirby_publisher{
/*
+-----------------------------------------------------------------+
|Kirby Publisher Configuration Parameters: |
| |
|$CONTENT_PATH ▶▶ The Kirby content directory (default = |
|/var/www/content) |
| |
|$TOOLKIT_PATH ▷▷ The Kirby toolkit directory (default = |
|/var/www/kirby/toolkit) |
| |
|$DIRECTORY_DIGITS ▶▶ Number of digits used in preceding directory|
|names (default = 4) |
| |
|$YAML_DIRECTIVE ▷▷ The YAML variable name to look for Publish |
|status (default = published) |
| |
|$PUBLIHSED_VALUE ▶▶ The value used with the $YAML_DIRECTIVE to |
|indicate the post should be published (default = Publish) |
|e.g. |
|---- |
|published: Publish |
|---- |
| |
|$DRAFT_VALUE ▷▷ The value used with the $YAML_DIRECTIVE to |
|indicate the post should be drafted (default = Draft) |
|e.g. |
|---- |
|published: Draft |
|---- |
+-----------------------------------------------------------------+
*/
private $CONTENT_PATH;
private $TOOLKIT_PATH;
private $DIRECTORY_DIGITS;
private $YAML_DIRECTIVE;
private $PUBLIHSED_VALUE;
private $DRAFT_VALUE;
/*
+-----------------------------------------------------------------+
|-----------------------------------------------------------------|
|kirby_publisher()------------------------------------------------|
|-----------------------------------------------------------------|
|Class constructor------------------------------------------------|
|-----------------------------------------------------------------|
|arguments--------------------------------------------------------|
|(----------------------------------------------------------------|
|$content="/var/www/public_html/content"--------------------------|
|$toolkit="/var/www/public_html/kirby/toolkit"--------------------|
|$digits=4--------------------------------------------------------|
|$yaml="published"------------------------------------------------|
|$publish="Publish"-----------------------------------------------|
|$draft="Draft"---------------------------------------------------|
|)----------------------------------------------------------------|
|-----------------------------------------------------------------|
|returns (kirby_publisher)----------------------------------------|
|-----------------------------------------------------------------|
+-----------------------------------------------------------------+
*/
function kirby_publisher($content="/var/www/public_html/content",$toolkit="/var/www/public_html/kirby/toolkit",$digits=4,$yaml="published",$publish="Publish",$draft="Draft"){
$this->CONTENT_PATH=$content;
$this->TOOLKIT_PATH=$toolkit;
$this->DIRECTORY_DIGITS=$digits;
$this->YAML_DIRECTIVE=$yaml;
$this->PUBLIHSED_VALUE=$publish;
$this->DRAFT_VALUE=$draft;
}
/*
+-----------------------------------------------------------------+
|-----------------------------------------------------------------|
|get_last_id()----------------------------------------------------|
|-----------------------------------------------------------------|
|returns the next id to be used for the upcoming post.------------|
|-----------------------------------------------------------------|
|arguments (void)-------------------------------------------------|
|-----------------------------------------------------------------|
|returns (string)-------------------------------------------------|
|-----------------------------------------------------------------|
+-----------------------------------------------------------------+
*/
function get_last_id(){
$last_id=0;
$directories = scandir($this->CONTENT_PATH,1);
//Check published posts
$regex_pattern = "/^(\d{".$this->DIRECTORY_DIGITS."})-/";
$published_array = preg_grep($regex_pattern, $directories);
$last_id = substr(array_values($published_array)[0],0,$this->DIRECTORY_DIGITS);
//Check draft posts
$draft_array = array_diff($directories, $published_array);
foreach ($draft_array as $directory){
if (file_exists($this->CONTENT_PATH."/".$directory."/".".file_id")){
$id_file=fopen($this->CONTENT_PATH."/".$directory."/".".file_id","r");
$directory_id=fgets($id_file);
fclose($id_file);
$last_id=($last_id>$directory_id?$last_id:$directory_id);
}
}
$last_id++;
return str_pad($last_id,$this->DIRECTORY_DIGITS,"0",STR_PAD_LEFT);
}
/*
+-----------------------------------------------------------------+
|-----------------------------------------------------------------|
|is_published()---------------------------------------------------|
|-----------------------------------------------------------------|
|returns the publishing status true or false for specific article |
|directory.-------------------------------------------------------|
|-----------------------------------------------------------------|
|arguments (void)-------------------------------------------------|
|-----------------------------------------------------------------|
|returns (bool)---------------------------------------------------|
|-----------------------------------------------------------------|
+-----------------------------------------------------------------+
*/
function is_published($directory){
$regex_pattern = "/^(\d{".$this->DIRECTORY_DIGITS."})-/";
return preg_match($regex_pattern, $directory);
}
/*
+-------------------------------------------------------------------------+
|-------------------------------------------------------------------------|
|publish_toggle()---------------------------------------------------------|
|-------------------------------------------------------------------------|
|Changes the publish status for article directory based on ---------------|
|$published_status.-------------------------------------------------------|
|-------------------------------------------------------------------------|
|arguments (--------------------------------------------------------------|
|$directory //directory name inside content directory---------------------|
|$published_status // 0 ▶▶ currently draft || 1 ▶▶ currently published----|
|)------------------------------------------------------------------------|
|-------------------------------------------------------------------------|
|returns (void)-----------------------------------------------------------|
|-------------------------------------------------------------------------|
+-------------------------------------------------------------------------+
*/
function publish_toggle($directory, $published_status){
if(!$published_status){
if (file_exists($this->CONTENT_PATH."/".$directory."/".".file_id")){
$id_file=fopen($this->CONTENT_PATH."/".$directory."/".".file_id","r");
$directory_id=fgets($id_file);
fclose($id_file);
rename($this->CONTENT_PATH."/".$directory,$this->CONTENT_PATH."/".$directory_id."-".$directory);
unlink($this->CONTENT_PATH."/".$directory_id."-".$directory."/".".file_id");
}else{
$directory_id=$this->get_last_id();
rename($this->CONTENT_PATH."/".$directory,$this->CONTENT_PATH."/".$directory_id."-".$directory);
}
}else{
$directory_id=substr($directory, 0,$this->DIRECTORY_DIGITS);
$article_name=substr($directory,$this->DIRECTORY_DIGITS + 1);
$id_file=fopen($this->CONTENT_PATH."/".$directory."/".".file_id", "w");
fwrite($id_file, $directory_id);
fclose($id_file);
rename($this->CONTENT_PATH."/".$directory,$this->CONTENT_PATH."/".$article_name);
}
}
/*
+-----------------------------------------------------------------+
|-----------------------------------------------------------------|
|update_published_status()----------------------------------------|
|-----------------------------------------------------------------|
|Scans content directory and changes the published status for each|
|article based on the YAML variable in each blogarticle.txt file.-|
|-----------------------------------------------------------------|
|arguments (void)-------------------------------------------------|
|-----------------------------------------------------------------|
|returns (void)---------------------------------------------------|
|-----------------------------------------------------------------|
+-----------------------------------------------------------------+
*/
function update_published_status(){
require_once($this->TOOLKIT_PATH."/lib/yaml.php");
require_once($this->TOOLKIT_PATH."/vendors/yaml/yaml.php");
$directories = scandir($this->CONTENT_PATH);
foreach ($directories as $directory){
$published_status = $this->is_published($directory);
if (file_exists($this->CONTENT_PATH."/".$directory."/"."blogarticle.txt")){
$yaml = Yaml::read($this->CONTENT_PATH."/".$directory."/"."blogarticle.txt");
if(array_key_exists($this->YAML_DIRECTIVE,$yaml) && $yaml[$this->YAML_DIRECTIVE]===$this->PUBLIHSED_VALUE && $published_status==0){
$this->publish_toggle($directory,0);
}elseif (array_key_exists($this->YAML_DIRECTIVE,$yaml) && $yaml[$this->YAML_DIRECTIVE]==$this->DRAFT_VALUE && $published_status==1) {
$this->publish_toggle($directory,1);
}
}
}
}
}