]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DiskCache/DiskCachePlugin.php
[VersionBump] 1.19.0, fairly late
[quix0rs-gnu-social.git] / plugins / DiskCache / DiskCachePlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
6  * Plugin to implement cache interface with disk files
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  Cache
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2009 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * A plugin to cache data on local disk
39  *
40  * @category  Cache
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2009 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link      http://status.net/
46  */
47 class DiskCachePlugin extends Plugin
48 {
49     const PLUGIN_VERSION = '2.0.0';
50
51     var $root = '/tmp';
52
53     function keyToFilename($key)
54     {
55         return $this->root . '/' . str_replace(':', '/', $key);
56     }
57
58     /**
59      * Get a value associated with a key
60      *
61      * The value should have been set previously.
62      *
63      * @param string &$key   in; Lookup key
64      * @param mixed  &$value out; value associated with key
65      *
66      * @return boolean hook success
67      */
68     function onStartCacheGet(&$key, &$value)
69     {
70         $filename = $this->keyToFilename($key);
71
72         if (file_exists($filename)) {
73             $data = file_get_contents($filename);
74             if ($data !== false) {
75                 $value = unserialize($data);
76             }
77         }
78
79         Event::handle('EndCacheGet', array($key, &$value));
80         return false;
81     }
82
83     /**
84      * Associate a value with a key
85      *
86      * @param string  &$key     in; Key to use for lookups
87      * @param mixed   &$value   in; Value to associate
88      * @param integer &$flag    in; Flag (passed through to Memcache)
89      * @param integer &$expiry  in; Expiry (passed through to Memcache)
90      * @param boolean &$success out; Whether the set was successful
91      *
92      * @return boolean hook success
93      */
94     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
95     {
96         $filename = $this->keyToFilename($key);
97         $parent = dirname($filename);
98
99         $sofar = '';
100
101         foreach (explode('/', $parent) as $part) {
102             if (empty($part)) {
103                 continue;
104             }
105             $sofar .= '/' . $part;
106             if (!is_dir($sofar)) {
107                 $this->debug("Creating new directory '$sofar'");
108                 $success = mkdir($sofar, 0750);
109                 if (!$success) {
110                     $this->log(LOG_ERR, "Can't create directory '$sofar'");
111                     return false;
112                 }
113             }
114         }
115
116         if (is_dir($filename)) {
117             $success = false;
118             return false;
119         }
120
121         // Write to a temp file and move to destination
122
123         $tempname = tempnam(null, 'statusnetdiskcache');
124
125         $result = file_put_contents($tempname, serialize($value));
126
127         if ($result === false) {
128             $this->log(LOG_ERR, "Couldn't write '$key' to temp file '$tempname'");
129             return false;
130         }
131
132         $result = rename($tempname, $filename);
133
134         if (!$result) {
135             $this->log(LOG_ERR, "Couldn't move temp file '$tempname' to path '$filename' for key '$key'");
136             @unlink($tempname);
137             return false;
138         }
139
140         Event::handle('EndCacheSet', array($key, $value, $flag,
141                                            $expiry));
142
143         return false;
144     }
145
146     /**
147      * Delete a value associated with a key
148      *
149      * @param string  &$key     in; Key to lookup
150      * @param boolean &$success out; whether it worked
151      *
152      * @return boolean hook success
153      */
154     function onStartCacheDelete(&$key, &$success)
155     {
156         $filename = $this->keyToFilename($key);
157
158         if (file_exists($filename) && !is_dir($filename)) {
159             unlink($filename);
160         }
161
162         Event::handle('EndCacheDelete', array($key));
163         return false;
164     }
165
166     function onPluginVersion(array &$versions)
167     {
168         $versions[] = array('name' => 'DiskCache',
169                             'version' => self::PLUGIN_VERSION,
170                             'author' => 'Evan Prodromou',
171                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/DiskCache',
172                             'rawdescription' =>
173                             // TRANS: Plugin description.
174                             _m('Plugin to implement cache interface with disk files.'));
175         return true;
176     }
177 }