1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class MenuCacheStatic extends DataExtension |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
private static $class_names_to_cache = array(); |
6
|
|
|
public static function add_class_name_to_cache($className) |
7
|
|
|
{ |
8
|
|
|
self::$class_names_to_cache[$className] = $className; |
9
|
|
|
} |
10
|
|
|
|
11
|
|
|
private static $class_names_NOT_to_cache = array(); |
12
|
|
|
public static function remove_class_name_from_cache($className) |
13
|
|
|
{ |
14
|
|
|
self::$class_names_NOT_to_cache[$className] = $className; |
15
|
|
|
} |
16
|
|
|
public static function get_class_names_NOT_to_cache($className) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
self::$class_names_NOT_to_cache["UserDefinedForm"] = "UserDefinedForm"; |
19
|
|
|
return self::$class_names_NOT_to_cache; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
private static $custom_urls_to_add = array(); |
23
|
|
|
public static function add_custom_url_to_cache($url) |
24
|
|
|
{ |
25
|
|
|
self::$custom_urls_to_add[$url] = $url; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function updateCMSFields(FieldList $fields) |
29
|
|
|
{ |
30
|
|
|
$js = "window.open(this.href, 'publisher', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=600,height=600'); return false;"; |
31
|
|
|
$fields->addFieldToTab("Root.Caching", new LiteralField("PublishAllPages", '<p>This website runs a static cache to make it faster. You may be required to manually publish the pages to the cache. You can <a href="/dev/buildcache/?flush=1" onclick="'.$js.'">publish all pages to cache</a> now...</p>')); |
32
|
|
|
return $fields; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
//------------------ static publisher ------------------ ------------------ ------------------ ------------------ |
36
|
|
|
/** |
37
|
|
|
* Return a list of all the pages to cache |
38
|
|
|
*/ |
39
|
|
|
public function allPagesToCache() |
40
|
|
|
{ |
41
|
|
|
// Get each page type to define its sub-urls |
42
|
|
|
$urls = array(); |
43
|
|
|
// memory intensive depending on number of pages |
44
|
|
|
foreach (self::$class_names_to_cache as $className) { |
45
|
|
|
$pages = $className::get() |
46
|
|
|
->exclude(array("ClassName" => self::$class_names_NOT_to_cache)); |
47
|
|
|
if ($pages->count()) { |
48
|
|
|
foreach ($pages as $page) { |
49
|
|
|
$urls = array_merge($urls, (array)$page->subPagesToCache()); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
if (is_array(self::$custom_urls_to_add)) { |
54
|
|
|
if (count(self::$custom_urls_to_add)) { |
55
|
|
|
foreach (self::$custom_urls_to_add as $url) { |
56
|
|
|
$urls[] = $url; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return $urls; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a list of URLs to cache related to this page |
65
|
|
|
*/ |
66
|
|
|
public function subPagesToCache() |
67
|
|
|
{ |
68
|
|
|
$urls = array(); |
69
|
|
|
|
70
|
|
|
// add current page |
71
|
|
|
$urls[] = $this->owner->Link(); |
72
|
|
|
|
73
|
|
|
// cache the RSS feed if comments are enabled |
74
|
|
|
if ($this->owner->ProvideComments) { |
75
|
|
|
$urls[] = Director::absoluteBaseURL() . "pagecomment/rss/" . $this->owner->ID; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $urls; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function pagesAffectedByChanges() |
82
|
|
|
{ |
83
|
|
|
$urls = $this->subPagesToCache(); |
84
|
|
|
if ($parent = SiteTree::get()->byID($this->owner->ParentID)) { |
85
|
|
|
$urls = array_merge((array)$urls, (array)$parent->subPagesToCache()); |
86
|
|
|
} |
87
|
|
|
return $urls; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.