-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
functions.inc.php
240 lines (221 loc) · 6.69 KB
/
functions.inc.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Functions
*
* @package WikiDocs
* @repository https://github.com/Zavy86/wikidocs
*/
/**
* Dump a variable into a debug box (only if debug is enabled)
*
* @param mixed $variable Dump variable
* @param ?string $label Dump label
* @param ?string $class Dump class
* @param bool $force Force dump also if debug is disabled
*/
function wdf_dump($variable,?string $label=null,?string $class=null,bool $force=false):void{
if(!DEBUG && !$force){return;}
echo "\n<!-- dump -->\n";
echo "<pre class='debug ".$class."'>\n";
if($label<>null){echo "<b>".$label."</b>\n";}
if(is_string($variable)){$variable=str_replace(array("<",">"),array("<",">"),$variable);}
print_r($variable);
echo "</pre>\n<!-- /dump -->\n";
}
/**
* Redirect (if debug is enabled show a redirect link)
*
* @param string $location Location URL
*/
function wdf_redirect(string $location):void{
if(DEBUG){die("<a href=\"".$location."\">".$location."</a>");}
exit(header("location: ".$location));
}
/**
* Alert (if debug is enabled show a debug message)
*
* @param string $message Alert message
* @param string $class Alert class (success|info|warning|danger)
* @return bool
*/
function wdf_alert(string $message,string $class="info"):bool{
// checks
if(!$message){return false;}
// build alert object
$alert=new stdClass();
$alert->timestamp=time();
$alert->message=$message;
$alert->class=$class;
// check for debug
if(!DEBUG){
// add alert to session alerts
$_SESSION['wikidocs']['alerts'][]=$alert;
}else{
// swicth class
switch($class){
case "success":$message="(!) ".$message;break;
case "warning":$message="/!\\ ".$message;break;
case "danger":$message="<!> ".$message;break;
default:$message="(?) ".$message;
}
// dump alert
wdf_dump($message,"ALERT");
}
// return
return true;
}
/**
* Timestamp Format
*
* @param ?int $timestamp Unix timestamp
* @param string $format Date Time format (see php.net/manual/en/function.date.php)
* @return string|boolean Formatted timestamp or false
* @throws Exception
*/
function wdf_timestamp_format(?int $timestamp,string $format="Y-m-d H:i:s"){
if(!is_numeric($timestamp) || $timestamp==0){return false;}
// build date time object
$datetime=new DateTime("@".$timestamp);
if(defined('TIMEZONE') && TIMEZONE != 'default'){
$datetime->setTimezone(new DateTimeZone(TIMEZONE));
}
// return date time formatted
return $datetime->format($format);
}
/**
* Regenerate Sitemap
*/
function wdf_regenerate_sitemap(){
$baseURL=URL;
$lastMod=date('Y-m-d\TH:i:sP',Document::getUpdateDate("/homepage"));
// open sitemap
$sitemap=<<<EOS
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>$baseURL</loc>
<lastmod>$lastMod</lastmod>
</url>
EOS;
// add documents from root
$sitemap.=wdf_regenerate_sitemap_documents();
// close sitemap
$sitemap.=<<<EOS
</urlset>
EOS;
// write sitemap to file
file_put_contents(DIR."sitemap.xml",$sitemap);
}
function wdf_regenerate_sitemap_documents(?string $parent=null){
$sitemap='';
$documents=Document::index($parent);
// cycle all documents
foreach($documents as $document){
// set variables
$url=URL.$document->url;
$lastMod=date('Y-m-d\TH:i:sP',Document::getUpdateDate($document->url));
// add to sitemap
$sitemap.=<<<EOS
<url>
<loc>$url</loc>
<lastmod>$lastMod</lastmod>
</url>
EOS;
// add sub documents recursively
$sitemap.= wdf_regenerate_sitemap_documents($document->url);
}
return $sitemap;
}
/**
* Parse inline text for custom tags while ignoring code blocks and inline code
*
* @param string $text The text to parse
* @param array $tags The tags to process
* @return string Parsed text
*/
function parseCustomTags($text, $tags) {
// Regular expression to match inline code (`code`) and code blocks (```code```)
$codeBlockPattern = '/(```.*?```|`[^`]*`)/s';
// Split text by code blocks
$segments = preg_split($codeBlockPattern, $text, -1, PREG_SPLIT_DELIM_CAPTURE);
// Process only non-code segments
foreach ($segments as &$segment) {
// If the segment is not a code block, process for custom tags
if (!preg_match($codeBlockPattern, $segment)) {
foreach ($tags as $tag => $callback) {
if (preg_match('/\[' . $tag . '(?::(\d+))?\]/', $segment, $matches)) {
$param = isset($matches[1]) ? (int)$matches[1] : 7; // Default limit is 7
$html = call_user_func($callback, $param);
$segment = str_replace($matches[0], $html, $segment);
}
}
}
}
// Join the segments back together
return implode('', $segments);
}
/**
* Render the recent edits
*
* @param int $limit Number of recent edits to show
* @return string HTML of the recent edits
*/
function renderRecentEdits($limit = 7) {
$docs = Document::getLastEditedDocs($limit);
$html = "<ul>\n";
foreach ($docs as $doc) {
$path = rtrim($doc['path'], '/');
$title = getDocumentTitle($path);
$html .= '<li><a href="' . URL . $path . '">' . $title . '</a> - ' . date('Y-m-d H:i', $doc['timestamp']) . "</li>\n";
}
$html .= "</ul>\n";
return $html;
}
/**
* Get the title of a document from the first line of its content.md file
*
* @param string $path The path to the document
* @return string The title of the document
*/
function getDocumentTitle($path) {
// Construct the full path to the content.md file
$fullPath = realpath(__DIR__ . '/../public_html/datasets/documents/' . $path . '/content.md');
if ($fullPath && file_exists($fullPath)) {
$file = fopen($fullPath, 'r');
if ($file) {
$firstLine = fgets($file);
fclose($file);
if ($firstLine !== false && strpos($firstLine, '# ') === 0) {
return trim(substr($firstLine, 2));
}
}
}
return $path; // fallback to the path if title not found
}
/**
* Render the total number of content.md files
*
* @return string The total number of content.md files
*/
function renderTotalContent() {
$total = Document::getTotalContentCount();
return (string)$total;
}
/**
* Parse inline text for custom tags like [wd-recentedits] and [wd-total]
*
* @param string $text The text to parse
* @return string Parsed text
*/
function parseInlineText($text) {
// Define the tags and their respective callbacks
$tags = [
'wd-recentedits' => function($limit = 7) {
return renderRecentEdits($limit);
},
'wd-total' => function() {
return renderTotalContent();
}
];
return parseCustomTags($text, $tags);
}