-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPageCachingPlugin.php
199 lines (181 loc) · 5.1 KB
/
PageCachingPlugin.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
<?php
/**
* Page Caching
*
* @version $Id$
* @copyright Center for History and New Media, 2009
* @license http://www.gnu.org/licenses/gpl-3.0.txt
* @package PageCaching
*/
define('PAGE_CACHING_PLUGIN_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR);
define('PAGE_CACHING_CACHE_DIR_PATH', PAGE_CACHING_PLUGIN_DIR . 'cache' . DIRECTORY_SEPARATOR);
/**
* The Page Caching plugin.
* @package Omeka\Plugins\PageCaching
*/
class PageCachingPlugin extends Omeka_Plugin_AbstractPlugin
{
/**
* @var array Hooks for the plugin.
*/
protected $_hooks = array(
'initialize',
'install',
'uninstall',
'config_form',
'config',
'after_save_record',
'after_delete_record',
);
/**
* @var array Filters for the plugin.
*/
protected $_filters = array(
'admin_dashboard_panels',
);
/**
* @var array Options and their default values.
*/
protected $_options = array(
);
/**
* Initialize this plugin.
*/
public function hookInitialize()
{
Zend_Controller_Front::getInstance()->registerPlugin(new PageCachingControllerPlugin);
}
/**
* Install the plugin.
*/
public function hookInstall()
{
$pageCacher = page_caching_get_page_cacher(false);
$pageCacher->saveOptions();
}
/**
* Uninstall the plugin.
*/
public function hookUninstall()
{
$pageCacher = page_caching_get_page_cacher();
$pageCacher->cleanCache();
$pageCacher->deleteOptions();
}
/**
* Shows plugin configuration page.
*/
public function hookConfigForm($args)
{
$view = get_view();
$pageCacher = page_caching_get_page_cacher();
$form = $pageCacher->buildConfigForm();
echo $form->render();
}
/**
* Processes the configuration form.
*/
public function hookConfig($args)
{
$pageCacher = page_caching_get_page_cacher();
$form = $pageCacher->buildConfigForm();
if (!$form->isValid($_POST)) {
throw new Exception('Not valid!');
}
$pageCacher->setOptions($_POST);
$pageCacher->saveOptions();
$pageCacher = page_caching_get_page_cacher(true, true);
$pageCache = $pageCacher->getCache();
if ($pageCache) {
$pageCacher->cleanCache();
} else {
$errorExceptions = $pageCacher->getErrorExceptions();
if ($errorExceptions) {
throw $errorExceptions[0];
}
}
}
/**
* Hook called after saving a record.
*/
public function hookAfterSaveRecord($args)
{
$record = $args['record'];
if ($args['insert']) {
$this->_after_record_changed($record, 'insert');
}
else {
$this->_after_record_changed($record, 'update');
}
}
/**
* Hook called after deleting a record.
*/
public function hookAfterDeleteRecord($args)
{
$record = $args['record'];
$this->_after_record_changed($record, 'delete');
}
/**
* Filter used to display a panel on the admin dashboad.
*/
public function filterAdminDashboardPanels($panels)
{
ob_start();
?>
<div id="page-caching" class="info-panel">
<h2><?php echo __('Page Caching'); ?></h2>
<p><a class="button" href="<?php echo html_escape(url('page-caching/index/clear-cache')); ?>"><?php echo __('Clear Page Cache'); ?></a></p>
</div>
<?php
$panels[] = ob_get_clean();
return $panels;
}
protected function _after_record_changed($record, $action='insert')
{
$pageCacher = page_caching_get_page_cacher();
$pageCacher->blacklistForRecord($record, $action);
if ($pageCacher->getOption('automatically_clear_cache_after_record_change')) {
$pageCacher->cleanCache();
}
}
}
function page_caching_get_page_cacher($loadOptions = true, $forceReload = false)
{
if (!$loadOptions) {
$pageCacher = new PageCaching_PageCacher($loadOptions);
}
else {
$regError = false;
try {
$pageCacher = Zend_Registry::get('page_cacher');
} catch (Exception $e) {
$regError = true;
}
if ($forceReload || $regError) {
$pageCacher = new PageCaching_PageCacher;
$pageCacher->initializeCache();
Zend_Registry::set('page_cacher', $pageCacher);
}
}
return $pageCacher;
}
class PageCachingControllerPlugin extends Zend_Controller_Plugin_Abstract
{
/**
* Called before Zend_Controller_Front begins evaluating the
* request against its routes.
*
* @param Zend_Controller_Request_Abstract $request
* @return void
*/
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$pageCacher = page_caching_get_page_cacher();
$pageCache = $pageCacher->getCache();
if ($pageCache) {
$pageCacher->cleanCache(Zend_Cache::CLEANING_MODE_OLD);
$pageCache->start();
}
}
}