- Making it so the last component in ConstructNavbar() doesn't work as a link
[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 * @access private
53 */
54 var $hash;
55
56 /**
57 * Record count - the number of records in cacheV
58 * @var integer
59 * @access private
60 */
61 var $count;
62
63 /**
64 * Memcache for all fetched revisions so we don't have to query-dupe
65 * @var array
66 * @access private
67 */
68 var $memcache = array('revs' => array(), 'nodes' => array());
69
70 // ###################################################################
71 /**
72 * Constructor: initialies the registry
73 *
74 * @param object Controller
75 */
76 function cacheV(&$controller)
77 {
78 $this->controller =& $controller;
79 $this->set_hash();
80 }
81
82 // ###################################################################
83 /**
84 * Sets the hash so we know what table we're dealing with
85 *
86 * @access public
87 */
88 function set_hash()
89 {
90 $this->hash = md5($this->controller->repospath);
91 $this->controller->registry->debug("hash: $this->hash");
92 }
93
94 // ###################################################################
95 /**
96 * Returns a node string that has the beginning and ending slashes
97 * removed to allow it to match to the _nodes cacheV table
98 *
99 * @access public
100 *
101 * @param string Original string
102 *
103 * @return string Matchable string
104 */
105 function fetch_node_string($node)
106 {
107 trigger_error('You shouldn\'t be calling fetch_node_string. It\'s evil.', E_USER_WARNING);
108 return preg_replace('#(^/|/$)#', '', $node);
109 }
110
111 // ###################################################################
112 /**
113 * Returns a specific log entry
114 *
115 * @access public
116 *
117 * @param integer Revision number
118 *
119 * @return array Complete revision/commit entry
120 */
121 function fetch_revision($revision)
122 {
123 $revision = $this->controller->registry->clean($revision, TYPE_UINT);
124
125 if (!isset($this->memcache['revs']["$revision"]))
126 {
127 $this->memcache['revs']["$revision"] = $this->controller->registry->db->query_first("SELECT * FROM c{$this->hash}_revs " . ($revision == 0 ? " ORDER BY revision DESC LIMIT 1" : "WHERE revision = $revision"));
128 $this->memcache['revs']["$revision"]['files'] = unserialize($this->memcache['revs']["$revision"]['files']);
129 }
130
131 return $this->memcache['revs']["$revision"];
132 }
133
134 // ###################################################################
135 /**
136 * Returns the revision entry before the specified one
137 *
138 * @access public
139 *
140 * @param integer Revision number
141 *
142 * @return array Complete revision/commit entry
143 */
144 function fetch_prev_revision($revision)
145 {
146 $data = $this->fetch_node();
147 $data = $data['history'];
148 if (sizeof($data) < 1)
149 {
150 return $this->fetch_revision(0);
151 }
152
153 $list = array_keys($data);
154 $key = array_search($revision, $list);
155
156 if ($revision == 'HEAD')
157 {
158 $key = 0;
159 }
160
161 $key++; // go to the next earliest revision
162 if (!isset($list["$key"]))
163 {
164 return -1;
165 }
166
167 return $this->fetch_revision($list["$key"]);
168 }
169
170 // ###################################################################
171 /**
172 * Returns the latest revision that the file is at
173 *
174 * @access public
175 *
176 * @return integer HEAD revision
177 */
178 function fetch_head_revision()
179 {
180 $data = $this->fetch_node();
181 $data = $data['history'];
182
183 if (is_array($data))
184 {
185 return max(array_keys($data));
186 }
187 else
188 {
189 return 0;
190 }
191 }
192
193 // ###################################################################
194 /**
195 * Returns the revision entry that it's in context with the node. For
196 * instance, if the version 50 was passed and this node only has 48 as
197 * it's max, 48 will be returned.
198 *
199 * @access public
200 *
201 * @param integer Target revision
202 *
203 * @return integer Contextual revision
204 */
205 function fetch_revision_context($target)
206 {
207 $data = $this->fetch_node();
208 $data = $data['history'];
209
210 if ($target == 'HEAD')
211 {
212 $target = $this->fetch_head_revision();
213 }
214
215 if (isset($data["$target"]))
216 {
217 return $this->fetch_revision($target);
218 }
219
220 if (is_array($data))
221 {
222 $keys = array_keys($data);
223 }
224 else
225 {
226 return 0;
227 }
228
229 $prev = 0;
230 foreach ($keys AS $id => $revnum)
231 {
232 if ($target > $revnum)
233 {
234 $prev = $revnum;
235 if ($prev > $rev)
236 {
237 $rev = $prev;
238 }
239 }
240 else if ($target < $revnum)
241 {
242 $rev = $prev;
243 }
244 else
245 {
246 $rev = $keys[0];
247 }
248 }
249
250 return $this->fetch_revision($rev);
251 }
252
253 // ###################################################################
254 /**
255 * Fetches the latest revision for a given path
256 *
257 * @access public
258 *
259 * @return integer Latest revision; FALSE if none (not in HEAD)
260 */
261 function fetch_node()
262 {
263 $node = $this->controller->path;
264 if (!isset($this->memcache['nodes']["$node"]))
265 {
266 $result = $this->controller->registry->db->query_first("SELECT * FROM c{$this->hash}_nodes WHERE name = '" . $this->controller->registry->escape($node) . "'");
267 if ($result == false)
268 {
269 return false;
270 }
271
272 $this->memcache['nodes']["$node"] = $result;
273 $this->memcache['nodes']["$node"]['history'] = unserialize($this->memcache['nodes']["$node"]['history']);
274 $this->memcache['nodes']["$node"]['properties'] = unserialize($this->memcache['nodes']["$node"]['properties']);
275 }
276
277 return $this->memcache['nodes']["$node"];
278 }
279
280 // ###################################################################
281 /**
282 * Checks to see if a given node is a directory. Returns TRUE if so.
283 *
284 * @access public
285 *
286 * @return bool TRUE if directory, FALSE if not
287 */
288 function isdir()
289 {
290 $node = $this->fetch_node();
291 if ($node['node'] == 'dir')
292 {
293 return true;
294 }
295
296 return false;
297 }
298
299 // ###################################################################
300 /**
301 * Checks to see if it's necessary to rebuild the cacheV table for the
302 * current repository. This is done by making sure $count > 0. If not,
303 * then rebuild() is run. This also checks against the cacheV table
304 * to make sure that it's up-to-date against the root repository.
305 *
306 * @access public
307 *
308 * @return bool Whether or not any part of the cache was (re-)built
309 */
310 function exec_build()
311 {
312 $result = $this->controller->registry->db->query_first("SELECT MAX(revision) AS max FROM c{$this->hash}_revs");
313 $this->count = $result['max'];
314
315 // time to go from the start
316 if ($this->count == 0)
317 {
318 $this->build(null);
319 }
320 else
321 {
322 // send an Xquery to SVN to see if we need to update
323 $query = $this->controller->library->svn('info --xml ' . $this->controller->repospath);
324 $query = implode("\n", $query);
325
326 $tree = $this->controller->registry->xml->parse($query);
327
328 if ($tree['info']['entry']['revision'] != $this->count)
329 {
330 $this->build($this->count);
331 return true;
332 }
333 }
334
335 return false;
336 }
337
338 // ###################################################################
339 /**
340 * Builds the cacheV table. This can be used to build only part of the
341 * cache or the entire thing, if the revision is set to NULL.
342 *
343 * @access public
344 *
345 * @param integer Lower (current) revision
346 * @param bool Use separate and individual queries for inserting?
347 */
348 function build($revision, $seps = false)
349 {
350 $start = microtime();
351
352 // get _revs
353 $output = $this->controller->library->svn('log --xml -v ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->controller->repospath);
354 $output = implode("\n", $output);
355 $tree = $this->controller->registry->xml->parse($output);
356
357 // construct _revs inserts and the list of add revisions
358 foreach ($tree['log']['logentry'] AS $log)
359 {
360 XML::unify_node($log['paths']['path']);
361
362 $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'])) . "')";
363
364 foreach ($log['paths']['path'] AS $path)
365 {
366 if (trim($path['action']) == 'A')
367 {
368 $path['value'] = preg_replace('#^/#', '', $path['value']);
369 $addlist["$path[value]"] = $log['revision'];
370 }
371
372 $filelist[] = $this->controller->repospath . $path['value'] . '@' . $log['revision'];
373 }
374 }
375
376 $newfilelist = array();
377 foreach ($filelist AS $item)
378 {
379
380 }
381
382 // get _nodes
383 $output = $this->controller->library->svn('info --xml ' . implode(' ', $filelist));
384 $output = implode("\n", $output);
385 $infolist = $this->controller->registry->xml->parse($output);
386
387 // other part of _nodes: properties
388 $output = $this->controller->library->svn('proplist -v ' . implode(' ', $filelist));
389 $init = false;
390 foreach ($output AS $line)
391 {
392 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
393 {
394 if ($init == true)
395 {
396 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
397 }
398 $init = true;
399
400 $index = str_replace($this->controller->repospath, '', $bits[1]);
401 $index = ($index == '' ? '/' : $index);
402 $capture = false;
403 }
404 else
405 {
406 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
407 {
408 $curprop = $matches[1];
409 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
410 $capture = true;
411 }
412 else if ($capture == true)
413 {
414 $proplist["$index"]["$curprop"] .= $line . "\n";
415 }
416 }
417 }
418
419 XML::unify_node($infolist['info']['entry']);
420
421 $nodeindex = 0;
422 $nodesat = array();
423 // construct list of HEAD nodes for _nodes
424 foreach ($infolist['info']['entry'] AS $node)
425 {
426 $history = $this->controller->library->svn('log --xml ' . $node['url']['value']);
427 $history = implode("\n", $history);
428 $history = $this->controller->registry->xml->parse($history);
429
430 $loglist = array();
431 $latestrev = -1;
432 foreach ($history['log']['logentry'] AS $log)
433 {
434 // WHY THE HELL DOES THIS GET HIT ON REBUILDS?
435 if (!is_array($log))
436 {
437 /*print_r($node);
438 var_dump($log);*/
439 continue;
440 }
441 $loglist["$log[revision]"] = array(
442 'revision' => $log['revision'],
443 'author' => $log['author']['value'], // why does PHP5 hate this?
444 'date' => $log['date']['value'],
445 'message' => $log['msg']['value']
446 );
447 }
448
449 $path = str_replace($this->controller->repospath, '', $node['url']['value']);
450 $path = ($path == '' ? '/' : $path);
451
452 $nodesat["$path"][ $node['commit']['revision'] ] = $nodeindex;
453 $max = max(array_keys($nodesat["$path"]));
454 if ($max < $node['commit']['revision'])
455 {
456 continue;
457 }
458 else if ($max >= $node['commit']['revision'])
459 {
460 foreach ($nodesat["$path"] AS $rev => $ind)
461 {
462 if ($rev != $node['commit']['revision'])
463 {
464 unset($inserts['nodes'][ $nodesat["$path"]["$rev"] ]);
465 }
466 }
467 }
468
469 $inserts['nodes']["$nodeindex"] = "('$path', '" . $node['kind'] . "', " . $node['commit']['revision'] . ", '" . $this->controller->registry->escape(serialize($loglist)) . "', '" . $this->controller->registry->escape(serialize($proplist["$path"])) . "')";
470
471 $nodeindex++;
472 }
473
474 if ($seps == false)
475 {
476 // insert _revs
477 var_dump("
478 REPLACE INTO c{$this->hash}_revs
479 (revision, author, dateline, message, files)
480 VALUES
481 " . implode(",\n", $inserts['revs'])
482 );
483
484 // insert _nodes
485 var_dump("
486 REPLACE INTO c{$this->hash}_nodes
487 (name, node, revision, history, properties)
488 VALUES
489 " . implode(",\n", $inserts['nodes'])
490 );
491 }
492 else
493 {
494 // _revs
495 foreach ($inserts['revs'] AS $insert)
496 {
497 $this->controller->registry->db->query("
498 REPLACE INTO c{$this->hash}_revs
499 (revision, author, dateline, message, files)
500 VALUES
501 $insert"
502 );
503 }
504
505 // _nodes
506 foreach ($inserts['nodes'] AS $insert)
507 {
508 $this->controller->registry->db->query("
509 REPLACE INTO c{$this->hash}_nodes
510 (name, node, revision, history, properties)
511 VALUES
512 $insert"
513 );
514 }
515 }
516
517 $this->controller->registry->debug("TIME TO (RE)BUILD: " . $this->controller->registry->funct->fetch_microtime_diff($start));
518 }
519 }
520
521 /*=====================================================================*\
522 || ###################################################################
523 || # $HeadURL$
524 || # $Id$
525 || ###################################################################
526 \*=====================================================================*/
527 ?>