cacheV now stores the right paths... this makes me very happy
[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 $this->set_hash();
77 }
78
79 // ###################################################################
80 /**
81 * Sets the hash so we know what table we're dealing with
82 *
83 * @access public
84 */
85 function set_hash()
86 {
87 $this->hash = md5($this->controller->repospath);
88 $this->controller->registry->debug("hash: $this->hash");
89 }
90
91 // ###################################################################
92 /**
93 * Returns a node string that has the beginning and ending slashes
94 * removed to allow it to match to the _nodes cacheV table
95 *
96 * @access public
97 *
98 * @param string Original string
99 *
100 * @return string Matchable string
101 */
102 function fetch_node_string($node)
103 {
104 return preg_replace('#(^/|/$)#', '', $node);
105 }
106
107 // ###################################################################
108 /**
109 * Returns a specific log entry
110 *
111 * @access public
112 *
113 * @param integer Revision number
114 *
115 * @return array Complete revision/commit entry
116 */
117 function fetch_revision($revision)
118 {
119 $revision = $this->controller->registry->clean($revision, TYPE_UINT);
120
121 if (!isset($this->memcache['revs']["$revision"]))
122 {
123 $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"));
124 $this->memcache['revs']["$revision"]['files'] = unserialize($this->memcache['revs']["$revision"]['files']);
125 }
126
127 return $this->memcache['revs']["$revision"];
128 }
129
130 // ###################################################################
131 /**
132 * Returns the revision entry before the specified one
133 *
134 * @access public
135 *
136 * @param integer Revision number
137 *
138 * @return array Complete revision/commit entry
139 */
140 function fetch_prev_revision($revision)
141 {
142 $data = $this->fetch_node(($this->controller->path == '/' ? $this->controller->repos : $this->controller->path));
143 $data = $data['history'];
144 if (sizeof($data) < 1)
145 {
146 return $this->fetch_revision(0);
147 }
148
149 $list = array_keys($data);
150 $key = array_search($revision, $list);
151
152 if ($revision == 'HEAD')
153 {
154 $key = 0;
155 }
156
157 $key++; // go to the next earliest revision
158 if (!isset($list["$key"]))
159 {
160 return -1;
161 }
162
163 return $this->fetch_revision($list["$key"]);
164 }
165
166 // ###################################################################
167 /**
168 * Returns the latest revision that the file is at
169 *
170 * @access public
171 *
172 * @return integer HEAD revision
173 */
174 function fetch_head_revision()
175 {
176 $data = $this->fetch_node();
177 $data = $data['history'];
178
179 return max(array_keys($data));
180 }
181
182 // ###################################################################
183 /**
184 * Fetches the latest revision for a given path
185 *
186 * @access public
187 *
188 * @param string Node override
189 *
190 * @return integer Latest revision; FALSE if none (not in HEAD)
191 */
192 function fetch_node($node = false)
193 {
194 $node = $this->fetch_node_string(($node === false ? $this->controller->path : $node));
195 if (!isset($this->memcache['nodes']["$node"]))
196 {
197 $result = $this->controller->registry->db->query_first("SELECT * FROM {$this->hash}_nodes WHERE name = '" . $this->controller->registry->escape($node) . "'");
198 if ($result == false)
199 {
200 return false;
201 }
202
203 $this->memcache['nodes']["$node"] = $result;
204 $this->memcache['nodes']["$node"]['history'] = unserialize($this->memcache['nodes']["$node"]['history']);
205 }
206
207 return $this->memcache['nodes']["$node"];
208 }
209
210 // ###################################################################
211 /**
212 * Checks to see if a given node is a directory. Returns TRUE if so.
213 *
214 * @access public
215 *
216 * @return bool TRUE if directory, FALSE if not
217 */
218 function isdir()
219 {
220 $node = $this->fetch_node();
221 if ($node['node'] == 'dir')
222 {
223 return true;
224 }
225
226 return false;
227 }
228
229 // ###################################################################
230 /**
231 * Checks to see if it's necessary to rebuild the cacheV table for the
232 * current repository. This is done by making sure $count > 0. If not,
233 * then rebuild() is run. This also checks against the cacheV table
234 * to make sure that it's up-to-date against the root repository.
235 *
236 * @access public
237 */
238 function exec_build()
239 {
240 $result = $this->controller->registry->db->query_first("SELECT MAX(revision) AS max FROM {$this->hash}_revs");
241 $this->count = $result['max'];
242
243 // time to go from the start
244 if ($this->count == 0)
245 {
246 $this->build(null);
247 }
248 else
249 {
250 // send an Xquery to SVN to see if we need to update
251 $query = $this->controller->library->svn('info --xml ' . $this->controller->repospath);
252 $query = implode("\n", $query);
253
254 $tree = $this->controller->registry->xml->parse($query);
255
256 if ($tree['info']['entry']['revision'] != $this->count)
257 {
258 $this->build($this->count);
259 }
260 }
261 }
262
263 // ###################################################################
264 /**
265 * Builds the cacheV table. This can be used to build only part of the
266 * cache or the entire thing, if the revision is set to NULL.
267 *
268 * @access public
269 *
270 * @param integer Lower (current) revision
271 */
272 function build($revision)
273 {
274 $start = microtime();
275
276 // get _revs
277 $output = $this->controller->library->svn('log --xml -v ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->controller->repospath);
278 $output = implode("\n", $output);
279 $tree = $this->controller->registry->xml->parse($output);
280
281 // get _nodes
282 $output = $this->controller->library->svn('info --xml -R ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->controller->repospath);
283 $output = implode("\n", $output);
284 $infolist = $this->controller->registry->xml->parse($output);
285
286 // other part of _nodes: properties
287 $output = $this->controller->library->svn('proplist -v -R ' . ($revision !== null ? ' -r' . $revision . ':HEAD ' : '') . $this->controller->repospath);
288 foreach ($output AS $line)
289 {
290 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
291 {
292 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
293 $index = str_replace($this->controller->repospath, '', $bits[1]);
294 $capture = false;
295 }
296 else
297 {
298 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
299 {
300 $curprop = $matches[1];
301 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
302 $capture = true;
303 }
304 else if ($capture == true)
305 {
306 $proplist["$index"]["$curprop"] .= $line . "\n";
307 }
308 }
309 }
310
311 // construct _revs inserts and the list of add revisions
312 foreach ($tree['log']['logentry'] AS $log)
313 {
314 $this->controller->registry->xml->unify_node($log['paths']['path']);
315
316 $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'])) . "')";
317
318 foreach ($log['paths']['path'] AS $path)
319 {
320 if (trim($path['action']) == 'A')
321 {
322 $path['value'] = preg_replace('#^/#', '', $path['value']);
323 $addlist["$path[value]"] = $log['revision'];
324 }
325 }
326 }
327
328 // construct list of HEAD nodes for _nodes
329 foreach ($infolist['info']['entry'] AS $node)
330 {
331 $history = $this->controller->library->svn('log --xml ' . $node['url']['value']);
332 $history = implode("\n", $history);
333 $history = $this->controller->registry->xml->parse($history);
334
335 $loglist = array();
336 $latestrev = -1;
337 foreach ($history['log']['logentry'] AS $log)
338 {
339 // WHY THE HELL DOES THIS GET HIT ON REBUILDS?
340 if (!is_array($log))
341 {
342 print_r($node);
343 var_dump($log);
344 continue;
345 }
346 $loglist["$log[revision]"] = array(
347 'revision' => $log['revision'],
348 'author' => $log['author']['value'], // why does PHP5 hate this?
349 'date' => $log['date']['value'],
350 'message' => $log['msg']['value']
351 );
352 }
353
354 $path = str_replace($this->controller->repospath, '', $node['url']['value']);
355
356 $inserts['nodes'][] = "('$path', '" . $node['kind'] . "', " . $node['commit']['revision'] . ", '" . $this->controller->registry->escape(serialize($loglist)) . "', '" . $this->controller->registry->escape(serialize($proplist["$path"])) . "')";
357 }
358
359 // insert _revs
360 $this->controller->registry->db->query("
361 REPLACE INTO {$this->hash}_revs
362 (revision, author, dateline, message, files)
363 VALUES
364 " . implode(",\n", $inserts['revs'])
365 );
366
367 // insert _nodes
368 $this->controller->registry->db->query("
369 REPLACE INTO {$this->hash}_nodes
370 (name, node, revision, history, properties)
371 VALUES
372 " . implode(",\n", $inserts['nodes'])
373 );
374
375 $this->controller->registry->debug("TIME TO (RE)BUILD: " . $this->controller->registry->funct->fetch_microtime_diff($start));
376 }
377 }
378
379 /*=====================================================================*\
380 || ###################################################################
381 || # $HeadURL$
382 || # $Id$
383 || ###################################################################
384 \*=====================================================================*/
385 ?>