]> src.bluestatic.org Git - viewsvn.git/blob - includes/cachev.php
Make fetch_prev_rev() work if there is no history
[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 * The registry
44 * @var object
45 */
46 var $registry = null;
47
48 /**
49 * cacheV hash
50 * @var string
51 */
52 var $hash;
53
54 /**
55 * Record count - the number of records in cacheV
56 * @var integer
57 */
58 var $count;
59
60 /**
61 * Memcache for all fetched revisions so we don't have to query-dupe
62 * @var array
63 */
64 var $memcache = array('revs' => array(), 'nodes' => array());
65
66 // ###################################################################
67 /**
68 * Constructor: initialies the registry
69 */
70 function cacheV()
71 {
72 global $viewsvn;
73 $this->registry =& $viewsvn;
74 }
75
76 // ###################################################################
77 /**
78 * Sets the hash so we know what table we're dealing with
79 *
80 * @access public
81 */
82 function set_hash()
83 {
84 $this->hash = md5($this->registry->repos->fetch_path($this->registry->paths->repos));
85 }
86
87 // ###################################################################
88 /**
89 * Returns a node string that has the beginning and ending slashes
90 * removed to allow it to match to the _nodes cacheV table
91 *
92 * @access public
93 *
94 * @param string Original string
95 *
96 * @return string Matchable string
97 */
98 function fetch_node_string($node)
99 {
100 return preg_replace('#(^/|/$)#', '', $node);
101 }
102
103 // ###################################################################
104 /**
105 * Returns a specific log entry
106 *
107 * @access public
108 *
109 * @param integer Revision number
110 *
111 * @return array Complete revision/commit entry
112 */
113 function fetch_revision($revision)
114 {
115 $revision = $this->registry->clean($revision, TYPE_UINT);
116
117 if (!isset($this->memcache['revs']["$revision"]))
118 {
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']);
121 }
122
123 return $this->memcache['revs']["$revision"];
124 }
125
126 // ###################################################################
127 /**
128 * Returns the revision entry before the specified one
129 *
130 * @access public
131 *
132 * @param string Node path
133 * @param integer Revision number
134 *
135 * @return array Complete revision/commit entry
136 */
137 function fetch_prev_revision($node, $revision)
138 {
139 $data = $this->fetch_node($node);
140 $data = $data['history'];
141
142 if (count($data['history']) < 1)
143 {
144 return $this->fetch_revision(0);
145 }
146
147 unset($data[ max(array_keys($data)) ]);
148
149 return $this->fetch_revision(max(array_keys($data)));
150 }
151
152 // ###################################################################
153 /**
154 * Fetches the latest revision for a given path
155 *
156 * @access public
157 *
158 * @param string Node path
159 *
160 * @return integer Latest revision; FALSE if none (not in HEAD)
161 */
162 function fetch_node($node)
163 {
164 $node = $this->fetch_node_string($node);
165 if (!isset($this->memcache['nodes']["$node"]))
166 {
167 $result = $this->registry->db->query_first("SELECT * FROM {$this->hash}_nodes WHERE name = '" . $this->registry->escape($node) . "'");
168 if ($result == false)
169 {
170 return false;
171 }
172
173 $this->memcache['nodes']["$node"] = $result;
174 $this->memcache['nodes']["$node"]['history'] = unserialize($this->memcache['nodes']["$node"]['history']);
175 }
176
177 return $this->memcache['nodes']["$node"];
178 }
179
180 // ###################################################################
181 /**
182 * Checks to see if a given node is a directory. Returns TRUE if so.
183 *
184 * @access public
185 *
186 * @param string Node path
187 *
188 * @return bool TRUE if directory, FALSE if not
189 */
190 function isdir($node)
191 {
192 $node = $this->fetch_node($node);
193 if ($node['node'] == 'dir')
194 {
195 return true;
196 }
197
198 return false;
199 }
200
201 // ###################################################################
202 /**
203 * Checks to see if it's necessary to rebuild the cacheV table for the
204 * current repository. This is done by making sure $count > 0. If not,
205 * then rebuild() is run. This also checks against the cacheV table
206 * to make sure that it's up-to-date against the root repository.
207 *
208 * @access public
209 */
210 function exec_build()
211 {
212 $result = $this->registry->db->query_first("SELECT MAX(revision) AS max FROM {$this->hash}_revs");
213 $this->count = $result['max'];
214
215 // time to go from the start
216 if ($this->count == 0)
217 {
218 $this->build(null);
219 }
220 else
221 {
222 // send an Xquery to SVN to see if we need to update
223 $query = $this->registry->svn->svn('info --xml ' . $this->registry->repos->fetch_path($this->registry->paths->repos));
224 $query = implode("\n", $query);
225
226 $tree = $this->registry->xml->parse($query);
227
228 if ($tree['info']['entry']['revision'] != $this->count)
229 {
230 $this->build($this->count);
231 }
232 }
233 }
234
235 // ###################################################################
236 /**
237 * Builds the cacheV table. This can be used to build only part of the
238 * cache or the entire thing, if the revision is set to NULL.
239 *
240 * @access public
241 *
242 * @param integer Lower (current) revision
243 */
244 function build($revision)
245 {
246 $start = microtime();
247
248 // get _revs
249 $output = $this->registry->svn->svn('log --xml -v ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->registry->repos->fetch_path($this->registry->paths->repos));
250 $output = implode("\n", $output);
251 $tree = $this->registry->xml->parse($output);
252
253 // get _nodes
254 $output = $this->registry->svn->svn('info --xml -R ' . ($revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this->registry->repos->fetch_path($this->registry->paths->repos));
255 $output = implode("\n", $output);
256 $infolist = $this->registry->xml->parse($output);
257
258 // other part of _nodes: properties
259 $output = $this->registry->svn->svn('proplist -v -R ' . ($revision !== null ? ' -r' . $revision . ':HEAD ' : '') . $this->registry->repos->fetch_path($this->registry->paths->repos));
260 $index = null;
261 foreach ($output AS $line)
262 {
263 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
264 {
265 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
266 $index = str_replace($this->registry->repos->fetch_path($this->registry->paths->repos), '', $bits[1]);
267 $capture = false;
268 }
269 else
270 {
271 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
272 {
273 $curprop = $matches[1];
274 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
275 $capture = true;
276 }
277 else if ($capture == true)
278 {
279 $proplist["$index"]["$curprop"] .= $line . "\n";
280 }
281 }
282 }
283
284 // construct _revs inserts and the list of add revisions
285 foreach ($tree['log']['logentry'] AS $log)
286 {
287 $this->registry->xml->unify_node($log['paths']['path']);
288
289 $inserts['revs'][] = "($log[revision], '{$log['author']['value']}', '{$log['date']['value']}', '" . $this->registry->escape($log['msg']['value']) . "', '" . $this->registry->escape(serialize($log['paths']['path'])) . "')";
290
291 foreach ($log['paths']['path'] AS $path)
292 {
293 if (trim($path['action']) == 'A')
294 {
295 $path['value'] = preg_replace('#^/#', '', $path['value']);
296 $addlist["$path[value]"] = $log['revision'];
297 }
298 }
299 }
300
301 // construct list of HEAD nodes for _nodes
302 foreach ($infolist['info']['entry'] AS $node)
303 {
304 $history = $this->registry->svn->svn('log --xml ' . $node['url']['value']);
305 $history = implode("\n", $history);
306 $history = $this->registry->xml->parse($history);
307
308 $loglist = array();
309 $latestrev = -1;
310 foreach ($history['log']['logentry'] AS $log)
311 {
312 $latestrev = ($log['revision'] > $latestrev ? $log['revision'] : $latestrev);
313 $loglist["$log[revision]"] = array(
314 'revision' => $log['revision'],
315 'author' => $log['author']['value'],
316 'date' => $log['date']['value'],
317 'message' => $log['msg']['value']
318 );
319 }
320
321 $inserts['nodes'][] = "('$node[path]', '" . $node['kind'] . "', $latestrev, '" . $this->registry->escape(serialize($loglist)) . "', '" . $this->registry->escape(serialize($proplist["$node[path]"])) . "')";
322 }
323
324 // insert _revs
325 $this->registry->db->query("
326 REPLACE INTO {$this->hash}_revs
327 (revision, author, dateline, message, files)
328 VALUES
329 " . implode(",\n", $inserts['revs'])
330 );
331
332 // insert _nodes
333 $this->registry->db->query("
334 REPLACE INTO {$this->hash}_nodes
335 (name, node, revision, history, properties)
336 VALUES
337 " . implode(",\n", $inserts['nodes'])
338 );
339
340 $this->registry->debug("TIME TO (RE)BUILD: " . $this->registry->funct->fetch_microtime_diff($start));
341 }
342 }
343
344 /*=====================================================================*\
345 || ###################################################################
346 || # $HeadURL$
347 || # $Id$
348 || ###################################################################
349 \*=====================================================================*/
350 ?>