-
Notifications
You must be signed in to change notification settings - Fork 111
/
caching_functions.php
345 lines (311 loc) · 8.65 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
336
337
338
339
340
341
342
343
344
345
<?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(true)); // make $pagesArray available to the back
add_action('page-delete', 'create_pagesxml',array(true)); // Create pages.array if page deleted
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'){
echo returnPageContent($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, no strip, no filter
* @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);
$content = $data->$field;
if($raw) return $content; // return without any processing
$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.
*
* @since 3.1
* @param $page - slug of the page to retrieve content
* @param $field - the Field to display
*
*/
function getPageField($page,$field){
returnPageField($page,$field);
}
/**
* alias for getPageField()
*/
function echoPageField($page,$field){
getPageField($page,$field);
}
/**
* Return Page Field
*
* Retrieve 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){
$pagesArray = getPagesXmlValues();
if ($field=="content"){
$ret=returnPageContent($page);
} else {
if (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){
$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()){
$pagesArray = getPagesXmlValues();
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;
}
/**
* Return true if pagecache differs from pages
* Uses very basic filecount checks
* @todo make more complex checking
*
* @since 3.3.0
* @return bool
*/
function pageCacheCountDiffers(){
GLOBAL $pagesArray;
$path = GSDATAPAGESPATH;
$filenames = getXmlFiles($path);
return count($pagesArray)!=count($filenames);
}
/**
* LEGACY
* Get Cached Pages XML File Values
*
* Populates $pagesArray from page cache file
* If the file does not exist it is created
*
* @since 3.1
* @param bool $refresh check cache for pages changes and regen
*
*/
function getPagesXmlValues($refresh=false){
debugLog('getPagesXmlValues '.$refresh);
GLOBAL $pagesArray;
if(!isset($pagesArray)) init_pageCache($refresh);
}
/**
* LEGACY
* Create the Cached Pages XML file
*
* @since 3.1
* @param bool $flag true saves pages.xml
* @return null
*/
function create_pagesxml($save=false){
global $pagesArray;
debugLog('create_pagesxml '.$save);
$pageCacheXml = generate_pageCacheXml();
if((bool)$save){
save_pageCacheXml($pageCacheXml);
}
pageCacheXMLtoArray($pageCacheXml);
}
/**
* Initialize pagecache
*
* @param bool $refresh regenerate cache from pages files
*/
function init_pageCache($refresh = false)
{
$file=GSDATAOTHERPATH."pages.xml";
if (file_exists($file) and !$refresh){
// if exists load it
load_pageCache();
} else {
// else generate,save it,set global pagecache array
$pageCacheXml = generate_pageCacheXml();
save_pageCacheXml($pageCacheXml);
pageCacheXMLtoArray($pageCacheXml);
return;
}
}
/**
* Loads in pagescache file xml to pagecache array
*/
function load_pageCache(){
GLOBAL $pagesArray;
$file=GSDATAOTHERPATH."pages.xml";
$pagesArray=array(); // wipe array
$data = getXml($file);
pageCacheXMLtoArray($data); // create array from xml
}
/**
* Save pagecache xml file
* @param simpleXmlObj
* @return sucess
*/
function save_pageCacheXml($xml){
$file=GSDATAOTHERPATH."pages.xml";
// Plugin Authors should add custome fields etc.. here
$xml = exec_filter('pagecache',$xml);
if(!empty($xml)) return $xml->asXML($file);
exec_action('pagecache-aftersave');
}
/**
* Generates pagecachexml obj from pages xml
* @return simpleXmlobj pagecache xml
*/
function generate_pageCacheXml(){
// read in each pages xml file
$path = GSDATAPAGESPATH;
$filenames = getXmlFiles($path);
$xml = @new SimpleXMLExtended('<channel></channel>');
if (count($filenames) != 0) {
foreach ($filenames as $file) {
$data = getXml($path.$file);
$id=$data->url;
$pages = $xml->addChild('item');
$pages->addChild('url', $id);
$children = $data->children();
foreach ($children as $item => $itemdata) {
if ($item!="content"){
$note = $pages->addChild($item);
$note->addCData($itemdata);
}
}
// removed from xml , redundant
# $note = $pages->addChild('slug');
# $note->addCData($id);
# $note = $pages->addChild('filename');
# $note->addCData($file);
}
}
return $xml;
}
/**
* creates pagecache array from pagescache xml
*
* @since 3.3.0
* @uses $pagesArray
* @param simpleXmlObj $xml xml object of page cache
*/
function pageCacheXMLtoArray($xml){
GLOBAL $pagesArray;
debugLog('pageCacheXMLtoArray');
$data = $xml;
$pages = $data->item;
foreach ($pages as $page) {
$key=(string)$page->url;
$pagesArray[$key]=array();
$children = $page->children();
foreach ($children as $opt=>$val) {
$pagesArray[$key][(string)$opt]=(string)$val;
}
$pagesArray[$key]['slug']=$key; // legacy
$pagesArray[$key]['filename']=$key.'.xml'; // legacy
}
// debugLog(var_export($pagesArray,true));
}
/**
* Adds a single page to pagecache array from page xml node
*
* @since 3.3.0
* @uses $pagesArray
* @param simpleXmlObj $xml xml node of single page
*/
function pageXMLtoArray($xml){
GLOBAL $pagesArray;
$data = $xml;
$key=(string)$data->url;
// debugLog('pageXMLtoArray ' . $key);
$pagesArray[$key]['url']=$key;
$children = $data->children();
foreach ($children as $item => $itemdata) {
if ($item!="content"){
$pagesArray[$key][$item]=(string)$itemdata;
}
}
$pagesArray[$key]['slug']=$key; // legacy
$pagesArray[$key]['filename']=$key.'.xml'; // legacy
// debugLog(var_export($pagesArray[$key],true));
// _debugLog('pageXMLtoArray ' . $key,$pagesArray[$key]);
}
?>