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