Making some fields private
[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 {$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 return max(array_keys($data));
184 }
185
186 // ###################################################################
187 /**
188 * Returns the revision entry that it's in context with the node. For
189 * instance, if the version 50 was passed and this node only has 48 as
190 * it's max, 48 will be returned.
191 *
192 * @access public
193 *
194 * @param integer Target revision
195 *
196 * @return integer Contextual revision
197 */
198 function fetch_revision_context($target)
199 {
200 $data = $this->fetch_node();
201 $data = $data['history'];
202
203 if (isset($data["$target"]))
204 {
205 return $this->fetch_revision($target);
206 }
207
208 $keys = array_keys($data);
209
210 $prev = 0;
211 foreach ($keys AS $id => $revnum)
212 {
213 if ($target > $revnum)
214 {
215 $prev = $revnum;
216 if ($prev > $rev)
217 {
218 $rev = $prev;
219 }
220 }
221 else if ($target < $revnum)
222 {
223 $rev = $prev;
224 }
225 else
226 {
227 $rev = $keys[0];
228 }
229 }
230
231 return $this->fetch_revision($rev);
232 }
233
234 // ###################################################################
235 /**
236 * Fetches the latest revision for a given path
237 *
238 * @access public
239 *
240 * @return integer Latest revision; FALSE if none (not in HEAD)
241 */
242 function fetch_node()
243 {
244 $node = $this->controller->path;
245 if (!isset($this->memcache['nodes']["$node"]))
246 {
247 $result = $this->controller->registry->db->query_first("SELECT * FROM {$this->hash}_nodes WHERE name = '" . $this->controller->registry->escape($node) . "'");
248 if ($result == false)
249 {
250 return false;
251 }
252
253 $this->memcache['nodes']["$node"] = $result;
254 $this->memcache['nodes']["$node"]['history'] = unserialize($this->memcache['nodes']["$node"]['history']);
255 }
256
257 return $this->memcache['nodes']["$node"];
258 }
259
260 // ###################################################################
261 /**
262 * Checks to see if a given node is a directory. Returns TRUE if so.
263 *
264 * @access public
265 *
266 * @return bool TRUE if directory, FALSE if not
267 */
268 function isdir()
269 {
270 $node = $this->fetch_node();
271 if ($node['node'] == 'dir')
272 {
273 return true;
274 }
275
276 return false;
277 }
278
279 // ###################################################################
280 /**
281 * Checks to see if it's necessary to rebuild the cacheV table for the
282 * current repository. This is done by making sure $count > 0. If not,
283 * then rebuild() is run. This also checks against the cacheV table
284 * to make sure that it's up-to-date against the root repository.
285 *
286 * @access public
287 */
288 function exec_build()
289 {
290 $result = $this->controller->registry->db->query_first("SELECT MAX(revision) AS max FROM {$this->hash}_revs");
291 $this->count = $result['max'];
292
293 // time to go from the start
294 if ($this->count == 0)
295 {
296 $this->build(null);
297 }
298 else
299 {
300 // send an Xquery to SVN to see if we need to update
301 $query = $this->controller->library->svn('info --xml ' . $this->controller->repospath);
302 $query = implode("\n", $query);
303
304 $tree = $this->controller->registry->xml->parse($query);
305
306 if ($tree['info']['entry']['revision'] != $this->count)
307 {
308 $this->build($this->count);
309 }
310 }
311 }
312
313 // ###################################################################
314 /**
315 * Builds the cacheV table. This can be used to build only part of the
316 * cache or the entire thing, if the revision is set to NULL.
317 *
318 * @access public
319 *
320 * @param integer Lower (current) revision
321 */
322 function build($revision)
323 {
324 $start = microtime();
325
326 // get _revs
327 $output = $this->controller->library->svn('log --xml -v ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->controller->repospath);
328 $output = implode("\n", $output);
329 $tree = $this->controller->registry->xml->parse($output);
330
331 // get _nodes
332 $output = $this->controller->library->svn('info --xml -R ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->controller->repospath);
333 $output = implode("\n", $output);
334 $infolist = $this->controller->registry->xml->parse($output);
335
336 // other part of _nodes: properties
337 $output = $this->controller->library->svn('proplist -v -R ' . ($revision !== null ? ' -r' . $revision . ':HEAD ' : '') . $this->controller->repospath);
338 foreach ($output AS $line)
339 {
340 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
341 {
342 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
343 $index = str_replace($this->controller->repospath, '', $bits[1]);
344 $capture = false;
345 }
346 else
347 {
348 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
349 {
350 $curprop = $matches[1];
351 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
352 $capture = true;
353 }
354 else if ($capture == true)
355 {
356 $proplist["$index"]["$curprop"] .= $line . "\n";
357 }
358 }
359 }
360
361 // construct _revs inserts and the list of add revisions
362 foreach ($tree['log']['logentry'] AS $log)
363 {
364 $this->controller->registry->xml->unify_node($log['paths']['path']);
365
366 $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'])) . "')";
367
368 foreach ($log['paths']['path'] AS $path)
369 {
370 if (trim($path['action']) == 'A')
371 {
372 $path['value'] = preg_replace('#^/#', '', $path['value']);
373 $addlist["$path[value]"] = $log['revision'];
374 }
375 }
376 }
377
378 // construct list of HEAD nodes for _nodes
379 foreach ($infolist['info']['entry'] AS $node)
380 {
381 $history = $this->controller->library->svn('log --xml ' . $node['url']['value']);
382 $history = implode("\n", $history);
383 $history = $this->controller->registry->xml->parse($history);
384
385 $loglist = array();
386 $latestrev = -1;
387 foreach ($history['log']['logentry'] AS $log)
388 {
389 // WHY THE HELL DOES THIS GET HIT ON REBUILDS?
390 if (!is_array($log))
391 {
392 print_r($node);
393 var_dump($log);
394 continue;
395 }
396 $loglist["$log[revision]"] = array(
397 'revision' => $log['revision'],
398 'author' => $log['author']['value'], // why does PHP5 hate this?
399 'date' => $log['date']['value'],
400 'message' => $log['msg']['value']
401 );
402 }
403
404 $path = str_replace($this->controller->repospath, '', $node['url']['value']);
405 $path = ($path == '' ? '/' : $path);
406
407 $inserts['nodes'][] = "('$path', '" . $node['kind'] . "', " . $node['commit']['revision'] . ", '" . $this->controller->registry->escape(serialize($loglist)) . "', '" . $this->controller->registry->escape(serialize($proplist["$path"])) . "')";
408 }
409
410 // insert _revs
411 $this->controller->registry->db->query("
412 REPLACE INTO {$this->hash}_revs
413 (revision, author, dateline, message, files)
414 VALUES
415 " . implode(",\n", $inserts['revs'])
416 );
417
418 // insert _nodes
419 $this->controller->registry->db->query("
420 REPLACE INTO {$this->hash}_nodes
421 (name, node, revision, history, properties)
422 VALUES
423 " . implode(",\n", $inserts['nodes'])
424 );
425
426 $this->controller->registry->debug("TIME TO (RE)BUILD: " . $this->controller->registry->funct->fetch_microtime_diff($start));
427 }
428 }
429
430 /*=====================================================================*\
431 || ###################################################################
432 || # $HeadURL$
433 || # $Id$
434 || ###################################################################
435 \*=====================================================================*/
436 ?>