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