2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
23 * File container for cacheV class
31 * This class is responsible for interacting with a given cacheV table.
32 * It controls rebuilding from scratch, updates, and querying the cache.
34 * @author Iris Studios, Inc.
35 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
47 var $controller = null
;
57 * Record count - the number of records in cacheV
64 * Memcache for all fetched revisions so we don't have to query-dupe
68 var $memcache = array('revs' => array(), 'nodes' => array());
70 // ###################################################################
72 * Constructor: initialies the registry
74 * @param object Controller
76 function cacheV(&$controller)
78 $this->controller
=& $controller;
82 // ###################################################################
84 * Sets the hash so we know what table we're dealing with
90 $this->hash
= md5($this->controller
->repospath
);
91 $this->controller
->registry
->debug("hash: $this->hash");
94 // ###################################################################
96 * Returns a node string that has the beginning and ending slashes
97 * removed to allow it to match to the _nodes cacheV table
101 * @param string Original string
103 * @return string Matchable string
105 function fetch_node_string($node)
107 trigger_error('You shouldn\'t be calling fetch_node_string. It\'s evil.', E_USER_WARNING
);
108 return preg_replace('#(^/|/$)#', '', $node);
111 // ###################################################################
113 * Returns a specific log entry
117 * @param integer Revision number
119 * @return array Complete revision/commit entry
121 function fetch_revision($revision)
123 $revision = $this->controller
->registry
->clean($revision, TYPE_UINT
);
125 if (!isset($this->memcache
['revs']["$revision"]))
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']);
131 return $this->memcache
['revs']["$revision"];
134 // ###################################################################
136 * Returns the revision entry before the specified one
140 * @param integer Revision number
142 * @return array Complete revision/commit entry
144 function fetch_prev_revision($revision)
146 $data = $this->fetch_node();
147 $data = $data['history'];
148 if (sizeof($data) < 1)
150 return $this->fetch_revision(0);
153 $list = array_keys($data);
154 $key = array_search($revision, $list);
156 if ($revision == 'HEAD')
161 $key++
; // go to the next earliest revision
162 if (!isset($list["$key"]))
167 return $this->fetch_revision($list["$key"]);
170 // ###################################################################
172 * Returns the latest revision that the file is at
176 * @return integer HEAD revision
178 function fetch_head_revision()
180 $data = $this->fetch_node();
181 $data = $data['history'];
183 return max(array_keys($data));
186 // ###################################################################
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.
194 * @param integer Target revision
196 * @return integer Contextual revision
198 function fetch_revision_context($target)
200 $data = $this->fetch_node();
201 $data = $data['history'];
203 if ($target == 'HEAD')
205 $target = $this->fetch_head_revision();
208 if (isset($data["$target"]))
210 return $this->fetch_revision($target);
213 $keys = array_keys($data);
216 foreach ($keys AS $id => $revnum)
218 if ($target > $revnum)
226 else if ($target < $revnum)
236 return $this->fetch_revision($rev);
239 // ###################################################################
241 * Fetches the latest revision for a given path
245 * @return integer Latest revision; FALSE if none (not in HEAD)
247 function fetch_node()
249 $node = $this->controller
->path
;
250 if (!isset($this->memcache
['nodes']["$node"]))
252 $result = $this->controller
->registry
->db
->query_first("SELECT * FROM c{$this->hash}_nodes WHERE name = '" . $this->controller
->registry
->escape($node) . "'");
253 if ($result == false
)
258 $this->memcache
['nodes']["$node"] = $result;
259 $this->memcache
['nodes']["$node"]['history'] = unserialize($this->memcache
['nodes']["$node"]['history']);
260 $this->memcache
['nodes']["$node"]['properties'] = unserialize($this->memcache
['nodes']["$node"]['properties']);
263 return $this->memcache
['nodes']["$node"];
266 // ###################################################################
268 * Checks to see if a given node is a directory. Returns TRUE if so.
272 * @return bool TRUE if directory, FALSE if not
276 $node = $this->fetch_node();
277 if ($node['node'] == 'dir')
285 // ###################################################################
287 * Checks to see if it's necessary to rebuild the cacheV table for the
288 * current repository. This is done by making sure $count > 0. If not,
289 * then rebuild() is run. This also checks against the cacheV table
290 * to make sure that it's up-to-date against the root repository.
294 * @return bool Whether or not any part of the cache was (re-)built
296 function exec_build()
298 $result = $this->controller
->registry
->db
->query_first("SELECT MAX(revision) AS max FROM c{$this->hash}_revs");
299 $this->count
= $result['max'];
301 // time to go from the start
302 if ($this->count
== 0)
308 // send an Xquery to SVN to see if we need to update
309 $query = $this->controller
->library
->svn('info --xml ' . $this->controller
->repospath
);
310 $query = implode("\n", $query);
312 $tree = $this->controller
->registry
->xml
->parse($query);
314 if ($tree['info']['entry']['revision'] != $this->count
)
316 $this->build($this->count
);
324 // ###################################################################
326 * Builds the cacheV table. This can be used to build only part of the
327 * cache or the entire thing, if the revision is set to NULL.
331 * @param integer Lower (current) revision
332 * @param bool Use separate and individual queries for inserting?
334 function build($revision, $seps = false
)
336 $start = microtime();
339 $output = $this->controller
->library
->svn('log --xml -v ' . ($revision !== null ?
'-r' . $revision . ':HEAD ' : '') . $this->controller
->repospath
);
340 $output = implode("\n", $output);
341 $tree = $this->controller
->registry
->xml
->parse($output);
344 $output = $this->controller
->library
->svn('info --xml -R ' . $this->controller
->repospath
);
345 $output = implode("\n", $output);
346 $infolist = $this->controller
->registry
->xml
->parse($output);
348 // other part of _nodes: properties
349 $output = $this->controller
->library
->svn('proplist -v -R ' . $this->controller
->repospath
);
351 foreach ($output AS $line)
353 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
357 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
361 $index = str_replace($this->controller
->repospath
, '', $bits[1]);
362 $index = ($index == '' ?
'/' : $index);
367 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
369 $curprop = $matches[1];
370 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
373 else if ($capture == true
)
375 $proplist["$index"]["$curprop"] .= $line . "\n";
380 // construct _revs inserts and the list of add revisions
381 foreach ($tree['log']['logentry'] AS $log)
383 $this->controller
->registry
->xml
->unify_node($log['paths']['path']);
385 $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'])) . "')";
387 foreach ($log['paths']['path'] AS $path)
389 if (trim($path['action']) == 'A')
391 $path['value'] = preg_replace('#^/#', '', $path['value']);
392 $addlist["$path[value]"] = $log['revision'];
397 // construct list of HEAD nodes for _nodes
398 foreach ($infolist['info']['entry'] AS $node)
400 $history = $this->controller
->library
->svn('log --xml ' . $node['url']['value']);
401 $history = implode("\n", $history);
402 $history = $this->controller
->registry
->xml
->parse($history);
406 foreach ($history['log']['logentry'] AS $log)
408 // WHY THE HELL DOES THIS GET HIT ON REBUILDS?
415 $loglist["$log[revision]"] = array(
416 'revision' => $log['revision'],
417 'author' => $log['author']['value'], // why does PHP5 hate this?
418 'date' => $log['date']['value'],
419 'message' => $log['msg']['value']
423 $path = str_replace($this->controller
->repospath
, '', $node['url']['value']);
424 $path = ($path == '' ?
'/' : $path);
426 $inserts['nodes'][] = "('$path', '" . $node['kind'] . "', " . $node['commit']['revision'] . ", '" . $this->controller
->registry
->escape(serialize($loglist)) . "', '" . $this->controller
->registry
->escape(serialize($proplist["$path"])) . "')";
432 $this->controller
->registry
->db
->query("
433 REPLACE INTO c{$this->hash}_revs
434 (revision, author, dateline, message, files)
436 " . implode(",\n", $inserts['revs'])
440 $this->controller
->registry
->db
->query("
441 REPLACE INTO c{$this->hash}_nodes
442 (name, node, revision, history, properties)
444 " . implode(",\n", $inserts['nodes'])
450 foreach ($inserts['revs'] AS $insert)
452 $this->controller
->registry
->db
->query("
453 REPLACE INTO c{$this->hash}_revs
454 (revision, author, dateline, message, files)
461 foreach ($inserts['nodes'] AS $insert)
463 $this->controller
->registry
->db
->query("
464 REPLACE INTO c{$this->hash}_nodes
465 (name, node, revision, history, properties)
472 $this->controller
->registry
->debug("TIME TO (RE)BUILD: " . $this->controller
->registry
->funct
->fetch_microtime_diff($start));
476 /*=====================================================================*\
477 || ###################################################################
480 || ###################################################################
481 \*=====================================================================*/