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.
55 * Record count - the number of records in cacheV
61 * Memcache for all fetched revisions so we don't have to query-dupe
64 var $memcache = array();
66 // ###################################################################
68 * Constructor: initialies the registry
73 $this->registry
=& $viewsvn;
76 // ###################################################################
78 * Sets the hash so we know what table we're dealing with
84 $this->hash
= md5($this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
87 // ###################################################################
89 * Returns a node string that has the beginning and ending slashes
90 * removed to allow it to match to the _nodes cacheV table
94 * @param string Original string
96 * @return string Matchable string
98 function fetch_node_string($node)
100 return preg_replace('#(^/|/$)#', '', $node);
103 // ###################################################################
105 * Returns a specific log entry
109 * @param integer Revision number
111 * @return array Complete revision/commit entry
113 function fetch_revision($revision)
115 $revision = $this->registry
->clean($revision, TYPE_UINT
);
117 if (!isset($this->memcache
['revs']["$revision"]))
119 $this->memcache
['revs']["$revision"] = $this->registry
->db
->query_first("SELECT * FROM {$this->hash}_revs " . ($revision == 0 ?
" ORDER BY revision DESC LIMIT 1" : "WHERE revision = $revision"));
120 $this->memcache
['revs']["$revision"]['files'] = unserialize($this->memcache
['revs']["$revision"]['files']);
123 return $this->memcache
['revs']["$revision"];
126 // ###################################################################
128 * Returns the revision entry before the specified one
132 * @param integer Revision number
134 * @return array Complete revision/commit entry
136 function fetch_prev_revision($revision)
140 $revision = $this->registry
->clean($revision, TYPE_UINT
);
142 if (!isset($prevrevs["$revision"]))
144 $result = $this->registry
->db
->query_first("SELECT * FROM {$this->hash}_revs ORDER BY revision DESC LIMIT 1 WHERE revision < $revision");
145 $revision = $result['revision'];
146 $this->memcache
['revs']["$revision"] = $result;
147 $this->memcache
['revs']["$revision"]['files'] = unserialize($this->memcache
['revs']["$revision"]['files']);
151 $revision = $prevrevs["$revision"];
154 return $this->memcache
['revs']["$revision"];
157 // ###################################################################
159 * Fetches the latest revision for a given path
163 * @param string Node path
165 * @return integer Latest revision; FALSE if none (not in HEAD)
167 function fetch_node($node)
169 $node = $this->fetch_node_string($node);
170 if (!isset($this->memcache
['nodes']["$node"]))
172 $this->memcache
['nodes']["$node"] = $this->registry
->db
->query_first("SELECT * FROM {$this->hash}_nodes WHERE name = '" . $this->registry
->escape($node) . "'");
175 return $this->memcache
['nodes']["$node"];
178 // ###################################################################
180 * Checks to see if a given node is a directory. Returns TRUE if so.
184 * @param string Node path
186 * @return bool TRUE if directory, FALSE if not
188 function isdir($node)
190 $node = $this->fetch_node($node);
191 if ($node['node'] == 'dir')
199 // ###################################################################
201 * Checks to see if it's necessary to rebuild the cacheV table for the
202 * current repository. This is done by making sure $count > 0. If not,
203 * then rebuild() is run. This also checks against the cacheV table
204 * to make sure that it's up-to-date against the root repository.
208 function exec_build()
210 $result = $this->registry
->db
->query_first("SELECT MAX(revision) AS max FROM {$this->hash}_revs");
211 $this->count
= $result['max'];
213 // time to go from the start
214 if ($this->count
== 0)
220 // send an Xquery to SVN to see if we need to update
221 $query = $this->registry
->svn
->svn('info --xml ' . $this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
222 $query = implode("\n", $query);
224 $tree = $this->registry
->xml
->parse($query);
226 if ($tree['info']['entry']['revision'] != $this->count
)
228 $this->build($this->count
);
233 // ###################################################################
235 * Builds the cacheV table. This can be used to build only part of the
236 * cache or the entire thing, if the revision is set to NULL.
240 * @param integer Lower (current) revision
242 function build($revision)
244 $start = microtime();
247 $output = $this->registry
->svn
->svn('log --xml -v ' . ($revision !== null ?
'-r' . $revision . ':HEAD ' : '') . $this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
248 $output = implode("\n", $output);
249 $tree = $this->registry
->xml
->parse($output);
252 $output = $this->registry
->svn
->svn('info --xml -R ' . ($revision !== null ?
'-r' . $revision . ':HEAD ' : '') . $this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
253 $output = implode("\n", $output);
254 $infolist = $this->registry
->xml
->parse($output);
256 // other part of _nodes: properties
257 $output = $this->registry
->svn
->svn('proplist -v -R ' . ($revision !== null ?
' -r' . $revision . ':HEAD ' : '') . $this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
258 foreach ($output AS $line)
260 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
262 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
263 $index = str_replace($this->registry
->repos
->fetch_path($this->registry
->paths
->repos
), '', $bits[1]);
268 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
270 $curprop = $matches[1];
271 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
274 else if ($capture == true
)
276 $proplist["$index"]["$curprop"] .= $line . "\n";
281 // construct _revs inserts and the list of add revisions
282 foreach ($tree['log']['logentry'] AS $log)
284 $this->registry
->xml
->unify_node($log['paths']['path']);
286 $inserts['revs'][] = "($log[revision], '{$log['author']['value']}', '{$log['date']['value']}', '" . $this->registry
->escape($log['msg']['value']) . "', '" . $this->registry
->escape(serialize($log['paths']['path'])) . "')";
288 foreach ($log['paths']['path'] AS $path)
290 if (trim($path['action']) == 'A')
292 $path['value'] = preg_replace('#^/#', '', $path['value']);
293 $addlist["$path[value]"] = $log['revision'];
298 // construct list of HEAD nodes for _nodes
299 foreach ($infolist['info']['entry'] AS $node)
301 $history = $this->registry
->svn
->svn('log --xml ' . $node['url']['value']);
302 $history = implode("\n", $history);
303 $history = $this->registry
->xml
->parse($history);
307 foreach ($history['log']['logentry'] AS $log)
309 $latestrev = ($log['revision'] > $latestrev ?
$log['revision'] : $latestrev);
310 $loglist["$log[revision]"] = array(
311 'revision' => $log['revision'],
312 'author' => $log['author']['value'],
313 'date' => $log['date']['value'],
314 'message' => $log['msg']['value']
318 $inserts['nodes'][] = "('$node[path]', '" . $node['kind'] . "', $latestrev, '" . $this->registry
->escape(serialize($loglist)) . "', '" . $this->registry
->escape(serialize($proplist["$node[path]"])) . "')";
322 $this->registry
->db
->query("
323 REPLACE INTO {$this->hash}_revs
324 (revision, author, dateline, message, files)
326 " . implode(",\n", $inserts['revs'])
330 $this->registry
->db
->query("
331 REPLACE INTO {$this->hash}_nodes
332 (name, node, revision, history, properties)
334 " . implode(",\n", $inserts['nodes'])
337 $this->registry
->debug("TIME TO (RE)BUILD: " . $this->registry
->funct
->fetch_microtime_diff($start));
341 /*=====================================================================*\
342 || ###################################################################
345 || ###################################################################
346 \*=====================================================================*/