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
;
56 * Record count - the number of records in cacheV
62 * Memcache for all fetched revisions so we don't have to query-dupe
65 var $memcache = array('revs' => array(), 'nodes' => array());
67 // ###################################################################
69 * Constructor: initialies the registry
71 * @param object Controller
73 function cacheV(&$controller)
75 $this->controller
=& $controller;
78 // ###################################################################
80 * Sets the hash so we know what table we're dealing with
86 $this->hash
= md5($this->controller
->registry
->repos
->fetch_path($this->controller
->registry
->paths
->repos
));
89 // ###################################################################
91 * Returns a node string that has the beginning and ending slashes
92 * removed to allow it to match to the _nodes cacheV table
96 * @param string Original string
98 * @return string Matchable string
100 function fetch_node_string($node)
102 return preg_replace('#(^/|/$)#', '', $node);
105 // ###################################################################
107 * Returns a specific log entry
111 * @param integer Revision number
113 * @return array Complete revision/commit entry
115 function fetch_revision($revision)
117 $revision = $this->controller
->registry
->clean($revision, TYPE_UINT
);
119 if (!isset($this->memcache
['revs']["$revision"]))
121 $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"));
122 $this->memcache
['revs']["$revision"]['files'] = unserialize($this->memcache
['revs']["$revision"]['files']);
125 return $this->memcache
['revs']["$revision"];
128 // ###################################################################
130 * Returns the revision entry before the specified one
134 * @param string Node path
135 * @param integer Revision number
137 * @return array Complete revision/commit entry
139 function fetch_prev_revision($node, $revision)
141 $data = $this->fetch_node($node);
142 $data = $data['history'];
144 if (count($data['history']) < 1)
146 return $this->fetch_revision(0);
149 unset($data[ max(array_keys($data)) ]);
151 return $this->fetch_revision(max(array_keys($data)));
154 // ###################################################################
156 * Fetches the latest revision for a given path
160 * @param string Node path
162 * @return integer Latest revision; FALSE if none (not in HEAD)
164 function fetch_node($node)
166 $node = $this->fetch_node_string($node);
167 if (!isset($this->memcache
['nodes']["$node"]))
169 $result = $this->controller
->registry
->db
->query_first("SELECT * FROM {$this->hash}_nodes WHERE name = '" . $this->controller
->registry
->escape($node) . "'");
170 if ($result == false
)
175 $this->memcache
['nodes']["$node"] = $result;
176 $this->memcache
['nodes']["$node"]['history'] = unserialize($this->memcache
['nodes']["$node"]['history']);
179 return $this->memcache
['nodes']["$node"];
182 // ###################################################################
184 * Checks to see if a given node is a directory. Returns TRUE if so.
188 * @param string Node path
190 * @return bool TRUE if directory, FALSE if not
192 function isdir($node)
194 $node = $this->fetch_node($node);
195 if ($node['node'] == 'dir')
203 // ###################################################################
205 * Checks to see if it's necessary to rebuild the cacheV table for the
206 * current repository. This is done by making sure $count > 0. If not,
207 * then rebuild() is run. This also checks against the cacheV table
208 * to make sure that it's up-to-date against the root repository.
212 function exec_build()
214 $result = $this->controller
->registry
->db
->query_first("SELECT MAX(revision) AS max FROM {$this->hash}_revs");
215 $this->count
= $result['max'];
217 // time to go from the start
218 if ($this->count
== 0)
224 // send an Xquery to SVN to see if we need to update
225 $query = $this->controller
->registry
->svn
->svn('info --xml ' . $this->controller
->registry
->repos
->fetch_path($this->controller
->registry
->paths
->repos
));
226 $query = implode("\n", $query);
228 $tree = $this->controller
->registry
->xml
->parse($query);
230 if ($tree['info']['entry']['revision'] != $this->count
)
232 $this->build($this->count
);
237 // ###################################################################
239 * Builds the cacheV table. This can be used to build only part of the
240 * cache or the entire thing, if the revision is set to NULL.
244 * @param integer Lower (current) revision
246 function build($revision)
248 $start = microtime();
251 $output = $this->controller
->registry
->svn
->svn('log --xml -v ' . ($revision !== null ?
'-r' . $revision . ':HEAD ' : '') . $this->controller
->registry
->repos
->fetch_path($this->controller
->registry
->paths
->repos
));
252 $output = implode("\n", $output);
253 $tree = $this->controller
->registry
->xml
->parse($output);
256 $output = $this->controller
->registry
->svn
->svn('info --xml -R ' . ($revision !== null ?
'-r' . $revision . ':HEAD ' : '') . $this->controller
->registry
->repos
->fetch_path($this->controller
->registry
->paths
->repos
));
257 $output = implode("\n", $output);
258 $infolist = $this->controller
->registry
->xml
->parse($output);
260 // other part of _nodes: properties
261 $output = $this->controller
->registry
->svn
->svn('proplist -v -R ' . ($revision !== null ?
' -r' . $revision . ':HEAD ' : '') . $this->controller
->registry
->repos
->fetch_path($this->controller
->registry
->paths
->repos
));
262 foreach ($output AS $line)
264 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
266 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
267 $index = str_replace($this->controller
->registry
->repos
->fetch_path($this->controller
->registry
->paths
->repos
), '', $bits[1]);
272 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
274 $curprop = $matches[1];
275 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
278 else if ($capture == true
)
280 $proplist["$index"]["$curprop"] .= $line . "\n";
285 // construct _revs inserts and the list of add revisions
286 foreach ($tree['log']['logentry'] AS $log)
288 $this->controller
->registry
->xml
->unify_node($log['paths']['path']);
290 $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'])) . "')";
292 foreach ($log['paths']['path'] AS $path)
294 if (trim($path['action']) == 'A')
296 $path['value'] = preg_replace('#^/#', '', $path['value']);
297 $addlist["$path[value]"] = $log['revision'];
302 // construct list of HEAD nodes for _nodes
303 foreach ($infolist['info']['entry'] AS $node)
305 $history = $this->controller
->registry
->svn
->svn('log --xml ' . $node['url']['value']);
306 $history = implode("\n", $history);
307 $history = $this->controller
->registry
->xml
->parse($history);
311 foreach ($history['log']['logentry'] AS $log)
313 $loglist["$log[revision]"] = array(
314 'revision' => $log['revision'],
315 'author' => $log['author']['value'],
316 'date' => $log['date']['value'],
317 'message' => $log['msg']['value']
321 $inserts['nodes'][] = "('$node[path]', '" . $node['kind'] . "', " . $node['commit']['revision'] . ", '" . $this->controller
->registry
->escape(serialize($loglist)) . "', '" . $this->controller
->registry
->escape(serialize($proplist["$node[path]"])) . "')";
325 $this->controller
->registry
->db
->query("
326 REPLACE INTO {$this->hash}_revs
327 (revision, author, dateline, message, files)
329 " . implode(",\n", $inserts['revs'])
333 $this->controller
->registry
->db
->query("
334 REPLACE INTO {$this->hash}_nodes
335 (name, node, revision, history, properties)
337 " . implode(",\n", $inserts['nodes'])
340 $this->controller
->registry
->debug("TIME TO (RE)BUILD: " . $this->controller
->registry
->funct
->fetch_microtime_diff($start));
344 /*=====================================================================*\
345 || ###################################################################
348 || ###################################################################
349 \*=====================================================================*/