commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / ctools / includes / object-cache.inc
1 <?php
2
3 /**
4 * @file
5 * The non-volatile object cache is used to store an object while it is
6 * being edited, so that we don't have to save until we're completely
7 * done. The cache should be 'cleaned' on a regular basis, meaning to
8 * remove old objects from the cache, but otherwise the data in this
9 * cache must remain stable, as it includes unsaved changes.
10 */
11
12 /**
13 * Get an object from the non-volatile ctools cache.
14 *
15 * This function caches in memory as well, so that multiple calls to this
16 * will not result in multiple database reads.
17 *
18 * @param $obj
19 * A 128 character or less string to define what kind of object is being
20 * stored; primarily this is used to prevent collisions.
21 * @param $name
22 * The name of the object being stored.
23 * @param $skip_cache
24 * Skip the memory cache, meaning this must be read from the db again.
25 * @param $sid
26 * The session id, allowing someone to use Session API or their own solution;
27 * defaults to session_id().
28 *
29 * @deprecated $skip_cache is deprecated in favor of drupal_static*
30 * @return
31 * The data that was cached.
32 */
33 function ctools_object_cache_get($obj, $name, $skip_cache = FALSE, $sid = NULL) {
34 $cache = &drupal_static(__FUNCTION__, array());
35 $key = "$obj:$name";
36 if ($skip_cache) {
37 unset($cache[$key]);
38 }
39
40 if (!$sid) {
41 $sid = session_id();
42 }
43
44 if (!array_key_exists($key, $cache)) {
45 $data = db_query('SELECT * FROM {ctools_object_cache} WHERE sid = :session_id AND obj = :object AND name = :name', array(':session_id' => $sid, ':object' => $obj, ':name' => $name))
46 ->fetchObject();
47 if ($data) {
48 $cache[$key] = unserialize($data->data);
49 }
50 }
51 return isset($cache[$key]) ? $cache[$key] : NULL;
52 }
53
54 /**
55 * Store an object in the non-volatile ctools cache.
56 *
57 * @param $obj
58 * A 128 character or less string to define what kind of object is being
59 * stored; primarily this is used to prevent collisions.
60 * @param $name
61 * The name of the object being stored.
62 * @param $cache
63 * The object to be cached. This will be serialized prior to writing.
64 * @param $sid
65 * The session id, allowing someone to use Session API or their own solution;
66 * defaults to session_id().
67 */
68 function ctools_object_cache_set($obj, $name, $cache, $sid = NULL) {
69 // Store the CTools session id in the user session to force a
70 // session for anonymous users in Drupal 7 and Drupal 6 Pressflow.
71 // see http://drupal.org/node/562374, http://drupal.org/node/861778
72 if (empty($GLOBALS['user']->uid) && empty($_SESSION['ctools_session_id'])) {
73 $_SESSION['ctools_hold_session'] = TRUE;
74 }
75
76 ctools_object_cache_clear($obj, $name, $sid);
77
78 if (!$sid) {
79 $sid = session_id();
80 }
81
82 db_insert('ctools_object_cache')
83 ->fields(array(
84 'sid' => $sid,
85 'obj' => $obj,
86 'name' => $name,
87 'data' => serialize($cache),
88 'updated' => REQUEST_TIME,
89 ))
90 ->execute();
91 }
92
93 /**
94 * Remove an object from the non-volatile ctools cache
95 *
96 * @param $obj
97 * A 128 character or less string to define what kind of object is being
98 * stored; primarily this is used to prevent collisions.
99 * @param $name
100 * The name of the object being removed.
101 * @param $sid
102 * The session id, allowing someone to use Session API or their own solution;
103 * defaults to session_id().
104 */
105 function ctools_object_cache_clear($obj, $name, $sid = NULL) {
106
107 if (!$sid) {
108 $sid = session_id();
109 }
110
111 db_delete('ctools_object_cache')
112 ->condition('sid', $sid)
113 ->condition('obj', $obj)
114 ->condition('name', $name)
115 ->execute();
116 // Ensure the static cache is emptied of this obj:name set.
117 drupal_static_reset('ctools_object_cache_get');
118 }
119
120
121 /**
122 * Determine if another user has a given object cached.
123 *
124 * This is very useful for 'locking' objects so that only one user can
125 * modify them.
126 *
127 * @param $obj
128 * A 128 character or less string to define what kind of object is being
129 * stored; primarily this is used to prevent collisions.
130 * @param $name
131 * The name of the object being removed.
132 * @param $sid
133 * The session id, allowing someone to use Session API or their own solution;
134 * defaults to session_id().
135 *
136 * @return
137 * An object containing the UID and updated date if found; NULL if not.
138 */
139 function ctools_object_cache_test($obj, $name, $sid = NULL) {
140
141 if (!$sid) {
142 $sid = session_id();
143 }
144
145 return db_query('SELECT s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions} s ON c.sid = s.sid WHERE s.sid <> :session_id AND c.obj = :obj AND c.name = :name ORDER BY c.updated ASC', array(':session_id' => $sid, ':obj' => $obj, ':name' => $name))
146 ->fetchObject();
147 }
148
149 /**
150 * Get the cache status of a group of objects.
151 *
152 * This is useful for displaying lock status when listing a number of objects
153 * an an administration UI.
154 *
155 * @param $obj
156 * A 128 character or less string to define what kind of object is being
157 * stored; primarily this is used to prevent collisions.
158 * @param $names
159 * An array of names of objects
160 *
161 * @return
162 * An array of objects containing the UID and updated date for each name found.
163 */
164 function ctools_object_cache_test_objects($obj, $names) {
165 return db_query("SELECT c.name, s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions} s ON c.sid = s.sid WHERE c.obj = :obj AND c.name IN (:names) ORDER BY c.updated ASC", array(':obj' => $obj, ':names' => $names))
166 ->fetchAllAssoc('name');
167 }
168
169 /**
170 * Remove an object from the non-volatile ctools cache for all session IDs.
171 *
172 * This is useful for clearing a lock.
173 *
174 * @param $obj
175 * A 128 character or less string to define what kind of object is being
176 * stored; primarily this is used to prevent collisions.
177 * @param $name
178 * The name of the object being removed.
179 */
180 function ctools_object_cache_clear_all($obj, $name) {
181 db_delete('ctools_object_cache')
182 ->condition('obj', $obj)
183 ->condition('name', $name)
184 ->execute();
185 // Ensure the static cache is emptied of this obj:name set.
186 $cache = &drupal_static('ctools_object_cache_get', array());
187 unset($cache["$obj:$name"]);
188 }
189
190 /**
191 * Remove all objects in the object cache that are older than the
192 * specified age.
193 *
194 * @param $age
195 * The minimum age of objects to remove, in seconds. For example, 86400 is
196 * one day. Defaults to 7 days.
197 */
198 function ctools_object_cache_clean($age = NULL) {
199 if (empty($age)) {
200 $age = 86400 * 7; // 7 days
201 }
202 db_delete('ctools_object_cache')
203 ->condition('updated', REQUEST_TIME - $age, '<')
204 ->execute();
205 }