/
caching_functions.php
335 lines (299 loc) · 9.44 KB
/
caching_functions.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php if(!defined('IN_GS')){ die('you cannot load this page directly.'); }
/****************************************************
*
* @File: caching_functions.php
* @Package: GetSimple
* @since 3.1
* @Action: Plugin to create pages.xml and new functions
*
*****************************************************/
$pagesArray = array();
add_action('index-header','getPagesXmlValues',array(false)); // make $pagesArray available to the front
add_action('header', 'getPagesXmlValues',array(get_filename_id() != 'pages')); // make $pagesArray available to the back
add_action('page-delete', 'create_pagesxml',array(true)); // Create pages.array if page deleted
add_action('page-restored', 'create_pagesxml',array(true)); // Create pages.array if page undo
add_action('changedata-aftersave', 'create_pagesxml',array(true)); // Create pages.array if page is updated
/**
* Get Page Content
*
* Retrieve and display the content of the requested page.
* As the Content is not cahed the file is read in.
*
* @since 2.0
* @param $page - slug of the page to retrieve content
*
*/
function getPageContent($page,$field='content'){
$thisfile = file_get_contents(GSDATAPAGESPATH.$page.'.xml');
$data = simplexml_load_string($thisfile);
$content = stripslashes(htmlspecialchars_decode($data->$field, ENT_QUOTES));
if ($field=='content'){
$content = exec_filter('content',$content);
}
echo $content;
}
/**
* Get Page Field
*
* Retrieve and display the requested field from the given page.
*
* @since 3.1
* @param $page - slug of the page to retrieve content
* @param $field - the Field to display
*
*/
function getPageField($page,$field){
global $pagesArray;
if(!$pagesArray) getPagesXmlValues();
if ($field=="content"){
getPageContent($page);
} else {
if (array_key_exists($field, $pagesArray[(string)$page])){
echo strip_decode($pagesArray[(string)$page][(string)$field]);
} else {
getPageContent($page,$field);
}
}
}
/**
* Echo Page Field
*
* Retrieve and display the requested field from the given page.
*
* @since 3.1
* @param $page - slug of the page to retrieve content
* @param $field - the Field to display
*
*/
function echoPageField($page,$field){
getPageField($page,$field);
}
/**
* Return Page Content
*
* Return the content of the requested page.
* As the Content is not cahed the file is read in.
*
* @since 3.1
* @param $page - slug of the page to retrieve content
* @param $raw false - if true return raw xml
* @param $nofilter false - if true skip content filter execution
*
*/
function returnPageContent($page, $field='content', $raw = false, $nofilter = false){
$thisfile = file_get_contents(GSDATAPAGESPATH.$page.'.xml');
$data = simplexml_load_string($thisfile);
if(!$data) return;
$content = $data->$field;
if(!$raw) $content = stripslashes(htmlspecialchars_decode($content, ENT_QUOTES));
if ($field=='content' and !$nofilter){
$content = exec_filter('content',$content);
}
return $content;
}
/**
* Get Page Field
*
* Retrieve and display the requested field from the given page.
* If the field is "content" it will call returnPageContent()
*
* @since 3.1
* @param $page - slug of the page to retrieve content
* @param $field - the Field to display
*
*/
function returnPageField($page,$field){
global $pagesArray;
if(!$pagesArray) getPagesXmlValues();
if ($field=="content"){
$ret=returnPageContent($page);
} else {
if (isset($pagesArray[(string)$page]) && array_key_exists($field, $pagesArray[(string)$page])){
$ret=strip_decode(@$pagesArray[(string)$page][(string)$field]);
} else {
$ret = returnPageContent($page,$field);
}
}
return $ret;
}
/**
* Get Page Children
*
* Return an Array of pages that are children of the requested page/slug
*
* @since 3.1
* @param $page - slug of the page to retrieve content
*
* @returns - Array of slug names
*
*/
function getChildren($page){
global $pagesArray;
if(!$pagesArray) getPagesXmlValues();
$returnArray = array();
foreach ($pagesArray as $key => $value) {
if ($pagesArray[$key]['parent']==$page){
$returnArray[]=$key;
}
}
return $returnArray;
}
/**
* Get Page Children - returns multi fields
*
* Return an Array of pages that are children of the requested page/slug with optional fields.
*
* @since 3.1
* @param $page - slug of the page to retrieve content
* @param options - array of optional fields to return
*
* @returns - Array of slug names and optional fields.
*
*/
function getChildrenMulti($page,$options=array()){
global $pagesArray;
if(!$pagesArray) getPagesXmlValues();
$count=0;
$returnArray = array();
foreach ($pagesArray as $key => $value) {
if ($pagesArray[$key]['parent']==$page){
$returnArray[$count]=array();
$returnArray[$count]['url']=$key;
foreach ($options as $option){
$returnArray[$count][$option]=returnPageField($key,$option);
}
$count++;
}
}
return $returnArray;
}
/**
* Get Cached Pages XML Values
*
* Loads the Cached XML data into the Array $pagesArray
* If the file does not exist it is created the first time.
*
* @since 3.1
*
*/
function getPagesXmlValues($chkcount=false){
global $pagesArray;
// debugLog(__FUNCTION__.": chkcount - " .(int)$chkcount);
// if page cache not load load it
if(!$pagesArray){
$pagesArray=array();
$file=GSDATAOTHERPATH."pages.xml";
if (file_exists($file)){
// load the xml file and setup the array.
// debugLog(__FUNCTION__.": load pages.xml");
$thisfile = file_get_contents($file);
$data = simplexml_load_string($thisfile);
$pages = $data->item;
foreach ($pages as $page) {
$key=$page->url;
$pagesArray[(string)$key]=array();
foreach ($page->children() as $opt=>$val) {
$pagesArray[(string)$key][(string)$opt]=(string)$val;
}
}
}
else {
// no page cache, regen and then load it
// debugLog(__FUNCTION__.": pages.xml not exist");
if(create_pagesxml(true)) getPagesXmlValues(false);
return;
}
}
// if checking cache sync, regen cache if pages differ.
if ($chkcount==true){
$path = GSDATAPAGESPATH;
$dir_handle = @opendir($path) or die("getPageXmlValues: Unable to open $path");
$filenames = array();
while ($filename = readdir($dir_handle)) {
$ext = substr($filename, strrpos($filename, '.') + 1);
if ($ext=="xml"){
$filenames[] = $filename;
}
}
if (count($pagesArray)!=count($filenames)) {
// debugLog(__FUNCTION__.": count differs regen pages.xml");
if(create_pagesxml(true)) getPagesXmlValues(false);
}
}
}
/**
* Create the Cached Pages XML file
*
* Reads in each page of the site and creates a single XML file called
* data/pages/pages.array
*
* @since 3.1
*
*/
function create_pagesxml($flag){
global $pagesArray;
$success = '';
// debugLog("create_pagesxml: " . $flag);
if ((isset($_GET['upd']) && $_GET['upd']=="edit-success") || $flag===true || $flag=='true'){
$pagesArray = array();
// debugLog("create_pagesxml proceeding");
$menu = '';
$filem=GSDATAOTHERPATH."pages.xml";
$path = GSDATAPAGESPATH;
$dir_handle = @opendir($path) or die("create_pagesxml: Unable to open $path");
$filenames = array();
while ($filename = readdir($dir_handle)) {
$ext = substr($filename, strrpos($filename, '.') + 1);
if ($ext=="xml"){
$filenames[] = $filename;
}
}
$count=0;
$xml = new SimpleXMLExtended('<?xml version="1.0" encoding="UTF-8"?><channel></channel>');
if (count($filenames) != 0) {
foreach ($filenames as $file) {
if ($file == "." || $file == ".." || is_dir(GSDATAPAGESPATH.$file) || $file == ".htaccess" ) {
// not a page data file
} else {
$thisfile = file_get_contents($path.$file);
$data = simplexml_load_string($thisfile);
if(!$data){
// handle corrupt page xml
debugLog("page $file is corrupt");
continue;
}
$count++;
$id=$data->url;
$pages = $xml->addChild('item');
// $pages->addChild('url', $id);
// $pagesArray[(string)$id]['url']=(string)$id;
foreach ($data->children() as $item => $itemdata) {
if ($item!="content"){
$note = $pages->addChild($item);
$note->addCData($itemdata);
$pagesArray[(string)$id][$item]=(string)$itemdata;
}
}
$note = $pages->addChild('slug');
$note->addCData($id);
$pagesArray[(string)$id]['slug']=(string)$id;
$pagesArray[(string)$id]['filename']=$file;
$note = $pages->addChild('filename');
$note->addCData($file);
} // else
} // end foreach
} // endif
if ($flag===true || $flag == 'true'){
// Plugin Authors should add custom fields etc.. here
$xml = exec_filter('pagecache',$xml);
// sanity check in case the filter does not come back properly or returns null
if($xml){
$success = XMLsave($xml,$filem);
}
// debugLog("create_pagesxml saved: ". $success);
exec_action('pagecache-aftersave');
return $success;
}
}
}
?>