tuupola / frog_funky_cache

Funky cache plugin for Frog CMS

This URL has Read+Write access

frog_funky_cache / index.php
100644 131 lines (110 sloc) 4.669 kb
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
<?php
 
/*
* Funky Cache - Frog CMS caching plugin
*
* Copyright (c) 2008-2009 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* http://www.appelsiini.net/projects/funky_cache
*
*/
 
require_once 'models/FunkyCachePage.php';
 
Plugin::setInfos(array(
    'id' => 'funky_cache',
    'title' => 'Funky Cache',
    'description' => 'Enables funky caching which makes your site ultra fast.',
    'version' => '0.3.6-dev',
    'license' => 'MIT',
    'author' => 'Mika Tuupola',
    'require_frog_version' => '0.9.3',
    'update_url' => 'http://www.appelsiini.net/download/frog-plugins.xml',
    'website' => 'http://www.appelsiini.net/'
));
 
/* Stuff for backend. */
if ('/admin/index.php' == $_SERVER['PHP_SELF']) {
    
    AutoLoader::addFolder(dirname(__FILE__) . '/lib');
    
    Plugin::addController('funky_cache', 'Cache');
    
    #Observer::observe('page_edit_after_save', 'funky_cache_delete_one');
    Observer::observe('page_edit_after_save', 'funky_cache_delete_all');
    Observer::observe('page_add_after_save', 'funky_cache_delete_all');
    Observer::observe('page_delete', 'funky_cache_delete_all');
    Observer::observe('view_page_edit_plugins', 'funky_cache_show_select');
    
    /* These currently only work in MIT fork of Frog. */
    Observer::observe('layout_after_edit', 'funky_cache_delete_all');
    Observer::observe('snippet_after_edit', 'funky_cache_delete_all');
    
    /* TODO Fix this to work with configurable cache folder. */
    function funky_cache_delete_one($page) {
        $data['url'] = '/' . $page->getUri() . URL_SUFFIX;
        if (($cache = Record::findOneFrom('FunkyCachePage', 'url=?', array($data['url'])))) {
            $cache->delete();
        }
    }
 
    function funky_cache_delete_all() {
        $cache = Record::findAllFrom('FunkyCachePage');
        foreach ($cache as $page) {
            $page->delete();
        }
        $message = sprintf('Cache was automatically cleared.');
        Observer::notify('log_event', $message, 7, 'funky_cache');
    }
    
    function funky_cache_show_select($page) {
        $enabled = isset($page->funky_cache_enabled) ?
                         $page->funky_cache_enabled : funky_cache_by_default();
        print '
<p><label for="page_funky_cache_enabled">'.__('Should cache').'</label>
<select id="page_funky_cache_enabled" name="page[funky_cache_enabled]">
<option value="0"'.($enabled == 0 ? ' selected="selected"': '').'>'.__('No').'</option>
<option value="1"'.($enabled == 1 ? ' selected="selected"': '').'>'.__('Yes').'</option>
</select>
</p>';
    }
        
} else {
/* Stuff for frontend. */
 
    global $__FROG_CONN__;
    Record::connection($__FROG_CONN__);
    
    Observer::observe('page_found', 'funky_cache_create');
    Observer::observe('page_requested', 'funky_cache_debug');
 
    function funky_cache_debug($page) {
       //print "-" . $_SERVER['QUERY_STRING'] . "-";
    }
 
    function funky_cache_create($page) {
        if ($page->funky_cache_enabled) {
            funky_cache_suffix();
            #$data['url'] = "/" . $_SERVER['QUERY_STRING'];
            $data['url'] = "/" . CURRENT_URI . URL_SUFFIX;
            
            /* Frontpage should become index.html */
            if ('/' . URL_SUFFIX == $data['url']) {
                $data['url'] = '/index' . funky_cache_suffix();
            /* If Frog suffix is not used, use suffix from cache settings */
            /* For example /articles becomes /articles.html */
            } elseif (!strlen(URL_SUFFIX)) {
                $data['url'] .= funky_cache_suffix();
            }
            $data['url'] = funky_cache_folder() . $data['url'];
            $data['url'] = preg_replace('#//#', '/', $data['url']);
            $data['page'] = $page;
            if (!($cache = Record::findOneFrom('FunkyCachePage', 'url=?', array($data['url'])))) {
                $cache = new FunkyCachePage($data);
            }
            $cache->page = $page;
            $cache->save();
        }
    }
}
 
function funky_cache_suffix() {
    return Setting::get('funky_cache_suffix');
}
 
function funky_cache_by_default() {
    return Setting::get('funky_cache_by_default');
}
 
function funky_cache_folder() {
$folder = '/' . Setting::get('funky_cache_folder') . '/';
$folder = preg_replace('#//*#', '/', $folder);
    return $folder;
}
 
function funky_cache_folder_is_root() {
    return '/' == funky_cache_folder();
}