]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/DiskCache/DiskCachePlugin.php
Snapshot of the Transifex translation project - February 2015
[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     var $root = '/tmp';
50
51     function keyToFilename($key)
52     {
53         return $this->root . '/' . str_replace(':', '/', $key);
54     }
55
56     /**
57      * Get a value associated with a key
58      *
59      * The value should have been set previously.
60      *
61      * @param string &$key   in; Lookup key
62      * @param mixed  &$value out; value associated with key
63      *
64      * @return boolean hook success
65      */
66     function onStartCacheGet(&$key, &$value)
67     {
68         $filename = $this->keyToFilename($key);
69
70         if (file_exists($filename)) {
71             $data = file_get_contents($filename);
72             if ($data !== false) {
73                 $value = unserialize($data);
74             }
75         }
76
77         Event::handle('EndCacheGet', array($key, &$value));
78         return false;
79     }
80
81     /**
82      * Associate a value with a key
83      *
84      * @param string  &$key     in; Key to use for lookups
85      * @param mixed   &$value   in; Value to associate
86      * @param integer &$flag    in; Flag (passed through to Memcache)
87      * @param integer &$expiry  in; Expiry (passed through to Memcache)
88      * @param boolean &$success out; Whether the set was successful
89      *
90      * @return boolean hook success
91      */
92     function onStartCacheSet(&$key, &$value, &$flag, &$expiry, &$success)
93     {
94         $filename = $this->keyToFilename($key);
95         $parent = dirname($filename);
96
97         $sofar = '';
98
99         foreach (explode('/', $parent) as $part) {
100             if (empty($part)) {
101                 continue;
102             }
103             $sofar .= '/' . $part;
104             if (!is_dir($sofar)) {
105                 $this->debug("Creating new directory '$sofar'");
106                 $success = mkdir($sofar, 0750);
107                 if (!$success) {
108                     $this->log(LOG_ERR, "Can't create directory '$sofar'");
109                     return false;
110                 }
111             }
112         }
113
114         if (is_dir($filename)) {
115             $success = false;
116             return false;
117         }
118
119         // Write to a temp file and move to destination
120
121         $tempname = tempnam(null, 'statusnetdiskcache');
122
123         $result = file_put_contents($tempname, serialize($value));
124
125         if ($result === false) {
126             $this->log(LOG_ERR, "Couldn't write '$key' to temp file '$tempname'");
127             return false;
128         }
129
130         $result = rename($tempname, $filename);
131
132         if (!$result) {
133             $this->log(LOG_ERR, "Couldn't move temp file '$tempname' to path '$filename' for key '$key'");
134             @unlink($tempname);
135             return false;
136         }
137
138         Event::handle('EndCacheSet', array($key, $value, $flag,
139                                            $expiry));
140
141         return false;
142     }
143
144     /**
145      * Delete a value associated with a key
146      *
147      * @param string  &$key     in; Key to lookup
148      * @param boolean &$success out; whether it worked
149      *
150      * @return boolean hook success
151      */
152     function onStartCacheDelete(&$key, &$success)
153     {
154         $filename = $this->keyToFilename($key);
155
156         if (file_exists($filename) && !is_dir($filename)) {
157             unlink($filename);
158         }
159
160         Event::handle('EndCacheDelete', array($key));
161         return false;
162     }
163
164     function onPluginVersion(&$versions)
165     {
166         $versions[] = array('name' => 'DiskCache',
167                             'version' => GNUSOCIAL_VERSION,
168                             'author' => 'Evan Prodromou',
169                             'homepage' => 'http://status.net/wiki/Plugin:DiskCache',
170                             'rawdescription' =>
171                             // TRANS: Plugin description.
172                             _m('Plugin to implement cache interface with disk files.'));
173         return true;
174     }
175 }