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 specific log entry
93 * @param integer Revision number
95 * @return array Complete revision/commit entry
97 function fetch_revision($revision)
99 $revision = $this->registry
->clean($revision, TYPE_UINT
);
101 if (!isset($this->memcache
['revs']["$revision"]))
103 $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"));
104 $this->memcache['revs']["$revision"]['files'] = unserialize($this->memcache
['revs']["$revision"]['files']);
107 return $this->memcache['revs']["$revision"];
110 // ###################################################################
112 * Returns the revision entry before the specified one
116 * @param integer Revision number
118 * @return array Complete revision/commit entry
120 function fetch_prev_revision($revision)
124 $revision = $this->registry
->clean($revision, TYPE_UINT
);
126 if (!isset($prevrevs["$revision"]))
128 $result = $this->registry->db->query_first("SELECT
* FROM {$this
->hash
}_revs ORDER BY revision DESC LIMIT
1 WHERE revision
< $revision");
129 $revision = $result['revision'];
130 $this->memcache
['revs']["$revision"] = $result;
131 $this->memcache['revs']["$revision"]['files'] = unserialize($this->memcache
['revs']["$revision"]['files']);
135 $revision = $prevrevs["$revision"];
138 return $this->memcache
['revs']["$revision"];
141 // ###################################################################
143 * Fetches the latest revision for a given path
147 * @param string Node path
149 * @return integer Latest revision; FALSE if none (not in HEAD)
151 function fetch_latest_revision($node)
153 if (!isset($this->memcache['nodes']["$node"]))
155 $this->memcache
['nodes']["$node"] = $this->registry->db->query_first("SELECT
* FROM {$this
->hash
}_nodes WHERE name
= '" . $this->registry->escape($node) . "'");
158 return $this->memcache['nodes']["$node"];
161 // ###################################################################
163 * Checks to see if it's necessary to rebuild the cacheV table for the
164 * current repository. This is done by making sure $count > 0. If not,
165 * then rebuild() is run. This also checks against the cacheV table
166 * to make sure that it's up-to-date against the root repository.
170 function exec_build()
172 $result = $this->registry
->db
->query_first("SELECT MAX(revision) AS max FROM {$this->hash}_revs");
173 $this->count
= $result['max'];
175 // time to go from the start
176 if ($this->count
== 0)
182 // send an Xquery to SVN to see if we need to update
183 $query = $this->registry
->svn
->svn('info --xml ' . $this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
184 $query = implode("\n", $query);
186 $tree = $this->registry
->xml
->parse($query);
188 if ($tree['info']['entry']['revision'] != $this->count
)
190 $this->build($this->count
);
195 // ###################################################################
197 * Builds the cacheV table. This can be used to build only part of the
198 * cache or the entire thing, if the revision is set to NULL.
202 * @param integer Lower (current) revision
204 function build($revision)
206 $start = microtime();
209 $output = $this->registry
->svn
->svn('log --xml -v ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
210 $output = implode("\n", $output);
211 $tree = $this->registry
->xml
->parse($output);
214 $output = $this->registry
->svn
->svn('info --xml -R ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
215 $output = implode("\n", $output);
216 $infolist = $this->registry
->xml
->parse($output);
218 // other part of _nodes: properties
219 $output = $this->registry
->svn
->svn('proplist -v -R ' . ($revision !== null ? ' -r' . $revision . ':HEAD ' : '') . $this->registry
->repos
->fetch_path($this->registry
->paths
->repos
));
220 foreach ($output AS $line)
222 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
224 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
225 $index = str_replace($this->registry
->repos
->fetch_path($this->registry
->paths
->repos
), '', $bits[1]);
230 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
232 $curprop = $matches[1];
233 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
236 else if ($capture == true)
238 $proplist["$index"]["$curprop"] .= $line . "\n";
243 // construct _revs inserts and the list of add revisions
244 foreach ($tree['log']['logentry'] AS $log)
246 $this->registry
->xml
->unify_node($log['paths']['path']);
248 $inserts['revs'][] = "($log[revision], '{$log['author']['value']}', '{$log['date']['value']}', '" . $this->registry
->escape($log['msg']['value']) . "', '" . $this->registry
->escape(serialize($log['paths']['path'])) . "')";
250 foreach ($log['paths']['path'] AS $path)
252 if (trim($path['action']) == 'A')
254 $path['value'] = preg_replace('#^/#', '', $path['value']);
255 $addlist["$path[value]"] = $log['revision'];
260 // construct list of HEAD nodes for _nodes
261 foreach ($infolist['info']['entry'] AS $node)
263 $history = $this->registry
->svn
->svn('log --xml ' . $node['url']['value']);
264 $history = implode("\n", $history);
265 $history = $this->registry
->xml
->parse($history);
269 foreach ($history['log']['logentry'] AS $log)
271 $latestrev = ($log['revision'] > $latestrev ? $log['revision'] : $latestrev);
272 $loglist["$log[revision]"] = array(
273 'revision' => $log['revision'],
274 'author' => $log['author']['value'],
275 'date' => $log['date']['value'],
276 'message' => $log['msg']['value']
280 $inserts['nodes'][] = "('$node[path]', '" . $node['kind'] . "', $latestrev, '" . $this->registry
->escape(serialize($loglist)) . "', '" . $this->registry
->escape(serialize($proplist["$node[path]"])) . "')";
284 $this->registry
->db
->query("
285 REPLACE INTO {$this->hash}_revs
286 (revision, author, dateline, message, files)
288 " . implode(",\n", $inserts['revs'])
292 $this->registry
->db
->query("
293 REPLACE INTO {$this->hash}_nodes
294 (name, node, revision, history, properties)
296 " . implode(",\n", $inserts['nodes'])
299 $this->registry
->debug("TIME TO (RE)BUILD: " . $this->registry
->funct
->fetch_microtime_diff($start));
303 /*=====================================================================*\
304 || ###################################################################
307 || ###################################################################
308 \*=====================================================================*/