Add cacheV to the controller
[viewsvn.git] / includes / cachev.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * File container for cacheV class
24 *
25 * @package ViewSVN
26 */
27
28 /**
29 * cacheV
30 *
31 * This class is responsible for interacting with a given cacheV table.
32 * It controls rebuilding from scratch, updates, and querying the cache.
33 *
34 * @author Iris Studios, Inc.
35 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
36 * @version $Revision$
37 * @package ViewSVN
38 *
39 */
40 class cacheV
41 {
42 /**
43 * Controller
44 * @var object
45 * @access private
46 */
47 var $controller = null;
48
49 /**
50 * cacheV hash
51 * @var string
52 */
53 var $hash;
54
55 /**
56 * Record count - the number of records in cacheV
57 * @var integer
58 */
59 var $count;
60
61 /**
62 * Memcache for all fetched revisions so we don't have to query-dupe
63 * @var array
64 */
65 var $memcache = array('revs' => array(), 'nodes' => array());
66
67 // ###################################################################
68 /**
69 * Constructor: initialies the registry
70 *
71 * @param object Controller
72 */
73 function cacheV(&$controller)
74 {
75 $this->controller =& $controller;
76 }
77
78 // ###################################################################
79 /**
80 * Sets the hash so we know what table we're dealing with
81 *
82 * @access public
83 */
84 function set_hash()
85 {
86 $this->hash = md5($this->controller->registry->repos->fetch_path($this->controller->registry->paths->repos));
87 }
88
89 // ###################################################################
90 /**
91 * Returns a node string that has the beginning and ending slashes
92 * removed to allow it to match to the _nodes cacheV table
93 *
94 * @access public
95 *
96 * @param string Original string
97 *
98 * @return string Matchable string
99 */
100 function fetch_node_string($node)
101 {
102 return preg_replace('#(^/|/$)#', '', $node);
103 }
104
105 // ###################################################################
106 /**
107 * Returns a specific log entry
108 *
109 * @access public
110 *
111 * @param integer Revision number
112 *
113 * @return array Complete revision/commit entry
114 */
115 function fetch_revision($revision)
116 {
117 $revision = $this->controller->registry->clean($revision, TYPE_UINT);
118
119 if (!isset($this->memcache['revs']["$revision"]))
120 {
121 $this->memcache['revs']["$revision"] = $this->controller->registry->db->query_first("SELECT * FROM {$this->hash}_revs " . ($revision == 0 ? " ORDER BY revision DESC LIMIT 1" : "WHERE revision = $revision"));
122 $this->memcache['revs']["$revision"]['files'] = unserialize($this->memcache['revs']["$revision"]['files']);
123 }
124
125 return $this->memcache['revs']["$revision"];
126 }
127
128 // ###################################################################
129 /**
130 * Returns the revision entry before the specified one
131 *
132 * @access public
133 *
134 * @param string Node path
135 * @param integer Revision number
136 *
137 * @return array Complete revision/commit entry
138 */
139 function fetch_prev_revision($node, $revision)
140 {
141 $data = $this->fetch_node($node);
142 $data = $data['history'];
143
144 if (count($data['history']) < 1)
145 {
146 return $this->fetch_revision(0);
147 }
148
149 unset($data[ max(array_keys($data)) ]);
150
151 return $this->fetch_revision(max(array_keys($data)));
152 }
153
154 // ###################################################################
155 /**
156 * Fetches the latest revision for a given path
157 *
158 * @access public
159 *
160 * @param string Node path
161 *
162 * @return integer Latest revision; FALSE if none (not in HEAD)
163 */
164 function fetch_node($node)
165 {
166 $node = $this->fetch_node_string($node);
167 if (!isset($this->memcache['nodes']["$node"]))
168 {
169 $result = $this->controller->registry->db->query_first("SELECT * FROM {$this->hash}_nodes WHERE name = '" . $this->controller->registry->escape($node) . "'");
170 if ($result == false)
171 {
172 return false;
173 }
174
175 $this->memcache['nodes']["$node"] = $result;
176 $this->memcache['nodes']["$node"]['history'] = unserialize($this->memcache['nodes']["$node"]['history']);
177 }
178
179 return $this->memcache['nodes']["$node"];
180 }
181
182 // ###################################################################
183 /**
184 * Checks to see if a given node is a directory. Returns TRUE if so.
185 *
186 * @access public
187 *
188 * @param string Node path
189 *
190 * @return bool TRUE if directory, FALSE if not
191 */
192 function isdir($node)
193 {
194 $node = $this->fetch_node($node);
195 if ($node['node'] == 'dir')
196 {
197 return true;
198 }
199
200 return false;
201 }
202
203 // ###################################################################
204 /**
205 * Checks to see if it's necessary to rebuild the cacheV table for the
206 * current repository. This is done by making sure $count > 0. If not,
207 * then rebuild() is run. This also checks against the cacheV table
208 * to make sure that it's up-to-date against the root repository.
209 *
210 * @access public
211 */
212 function exec_build()
213 {
214 $result = $this->controller->registry->db->query_first("SELECT MAX(revision) AS max FROM {$this->hash}_revs");
215 $this->count = $result['max'];
216
217 // time to go from the start
218 if ($this->count == 0)
219 {
220 $this->build(null);
221 }
222 else
223 {
224 // send an Xquery to SVN to see if we need to update
225 $query = $this->controller->registry->svn->svn('info --xml ' . $this->controller->registry->repos->fetch_path($this->controller->registry->paths->repos));
226 $query = implode("\n", $query);
227
228 $tree = $this->controller->registry->xml->parse($query);
229
230 if ($tree['info']['entry']['revision'] != $this->count)
231 {
232 $this->build($this->count);
233 }
234 }
235 }
236
237 // ###################################################################
238 /**
239 * Builds the cacheV table. This can be used to build only part of the
240 * cache or the entire thing, if the revision is set to NULL.
241 *
242 * @access public
243 *
244 * @param integer Lower (current) revision
245 */
246 function build($revision)
247 {
248 $start = microtime();
249
250 // get _revs
251 $output = $this->controller->registry->svn->svn('log --xml -v ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->controller->registry->repos->fetch_path($this->controller->registry->paths->repos));
252 $output = implode("\n", $output);
253 $tree = $this->controller->registry->xml->parse($output);
254
255 // get _nodes
256 $output = $this->controller->registry->svn->svn('info --xml -R ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->controller->registry->repos->fetch_path($this->controller->registry->paths->repos));
257 $output = implode("\n", $output);
258 $infolist = $this->controller->registry->xml->parse($output);
259
260 // other part of _nodes: properties
261 $output = $this->controller->registry->svn->svn('proplist -v -R ' . ($revision !== null ? ' -r' . $revision . ':HEAD ' : '') . $this->controller->registry->repos->fetch_path($this->controller->registry->paths->repos));
262 foreach ($output AS $line)
263 {
264 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
265 {
266 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
267 $index = str_replace($this->controller->registry->repos->fetch_path($this->controller->registry->paths->repos), '', $bits[1]);
268 $capture = false;
269 }
270 else
271 {
272 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
273 {
274 $curprop = $matches[1];
275 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
276 $capture = true;
277 }
278 else if ($capture == true)
279 {
280 $proplist["$index"]["$curprop"] .= $line . "\n";
281 }
282 }
283 }
284
285 // construct _revs inserts and the list of add revisions
286 foreach ($tree['log']['logentry'] AS $log)
287 {
288 $this->controller->registry->xml->unify_node($log['paths']['path']);
289
290 $inserts['revs'][] = "($log[revision], '{$log['author']['value']}', '{$log['date']['value']}', '" . $this->controller->registry->escape($log['msg']['value']) . "', '" . $this->controller->registry->escape(serialize($log['paths']['path'])) . "')";
291
292 foreach ($log['paths']['path'] AS $path)
293 {
294 if (trim($path['action']) == 'A')
295 {
296 $path['value'] = preg_replace('#^/#', '', $path['value']);
297 $addlist["$path[value]"] = $log['revision'];
298 }
299 }
300 }
301
302 // construct list of HEAD nodes for _nodes
303 foreach ($infolist['info']['entry'] AS $node)
304 {
305 $history = $this->controller->registry->svn->svn('log --xml ' . $node['url']['value']);
306 $history = implode("\n", $history);
307 $history = $this->controller->registry->xml->parse($history);
308
309 $loglist = array();
310 $latestrev = -1;
311 foreach ($history['log']['logentry'] AS $log)
312 {
313 $loglist["$log[revision]"] = array(
314 'revision' => $log['revision'],
315 'author' => $log['author']['value'],
316 'date' => $log['date']['value'],
317 'message' => $log['msg']['value']
318 );
319 }
320
321 $inserts['nodes'][] = "('$node[path]', '" . $node['kind'] . "', " . $node['commit']['revision'] . ", '" . $this->controller->registry->escape(serialize($loglist)) . "', '" . $this->controller->registry->escape(serialize($proplist["$node[path]"])) . "')";
322 }
323
324 // insert _revs
325 $this->controller->registry->db->query("
326 REPLACE INTO {$this->hash}_revs
327 (revision, author, dateline, message, files)
328 VALUES
329 " . implode(",\n", $inserts['revs'])
330 );
331
332 // insert _nodes
333 $this->controller->registry->db->query("
334 REPLACE INTO {$this->hash}_nodes
335 (name, node, revision, history, properties)
336 VALUES
337 " . implode(",\n", $inserts['nodes'])
338 );
339
340 $this->controller->registry->debug("TIME TO (RE)BUILD: " . $this->controller->registry->funct->fetch_microtime_diff($start));
341 }
342 }
343
344 /*=====================================================================*\
345 || ###################################################################
346 || # $HeadURL$
347 || # $Id$
348 || ###################################################################
349 \*=====================================================================*/
350 ?>