Making all classes that are subservient to Node_Controller not have to pass info...
[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 (count($data['history']) < 1)
145 {
146 return $this->fetch_revision(0);
147 }
148
149 unset($data[ max(array_keys($data)) ]);
150
151 return $this->fetch_revision(max(array_keys($data)));
152 }
153
154 // ###################################################################
155 /**
156 * Returns the latest revision that the file is at
157 *
158 * @access public
159 *
160 * @return integer HEAD revision
161 */
162 function fetch_head_revision()
163 {
164 $data = $this->fetch_node();
165 $data = $data['history'];
166
167 return max(array_keys($data));
168 }
169
170 // ###################################################################
171 /**
172 * Fetches the latest revision for a given path
173 *
174 * @access public
175 *
176 * @return integer Latest revision; FALSE if none (not in HEAD)
177 */
178 function fetch_node()
179 {
180 $node = $this->fetch_node_string($this->controller->path);
181 if (!isset($this->memcache['nodes']["$node"]))
182 {
183 $result = $this->controller->registry->db->query_first("SELECT * FROM {$this->hash}_nodes WHERE name = '" . $this->controller->registry->escape($node) . "'");
184 if ($result == false)
185 {
186 return false;
187 }
188
189 $this->memcache['nodes']["$node"] = $result;
190 $this->memcache['nodes']["$node"]['history'] = unserialize($this->memcache['nodes']["$node"]['history']);
191 }
192
193 return $this->memcache['nodes']["$node"];
194 }
195
196 // ###################################################################
197 /**
198 * Checks to see if a given node is a directory. Returns TRUE if so.
199 *
200 * @access public
201 *
202 * @param string Node path
203 *
204 * @return bool TRUE if directory, FALSE if not
205 */
206 function isdir($node)
207 {
208 $node = $this->fetch_node($node);
209 if ($node['node'] == 'dir')
210 {
211 return true;
212 }
213
214 return false;
215 }
216
217 // ###################################################################
218 /**
219 * Checks to see if it's necessary to rebuild the cacheV table for the
220 * current repository. This is done by making sure $count > 0. If not,
221 * then rebuild() is run. This also checks against the cacheV table
222 * to make sure that it's up-to-date against the root repository.
223 *
224 * @access public
225 */
226 function exec_build()
227 {
228 $result = $this->controller->registry->db->query_first("SELECT MAX(revision) AS max FROM {$this->hash}_revs");
229 $this->count = $result['max'];
230
231 // time to go from the start
232 if ($this->count == 0)
233 {
234 $this->build(null);
235 }
236 else
237 {
238 // send an Xquery to SVN to see if we need to update
239 $query = $this->controller->library->svn('info --xml ' . $this->controller->repospath);
240 $query = implode("\n", $query);
241
242 $tree = $this->controller->registry->xml->parse($query);
243
244 if ($tree['info']['entry']['revision'] != $this->count)
245 {
246 $this->build($this->count);
247 }
248 }
249 }
250
251 // ###################################################################
252 /**
253 * Builds the cacheV table. This can be used to build only part of the
254 * cache or the entire thing, if the revision is set to NULL.
255 *
256 * @access public
257 *
258 * @param integer Lower (current) revision
259 */
260 function build($revision)
261 {
262 $start = microtime();
263
264 // get _revs
265 $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));
266 $output = implode("\n", $output);
267 $tree = $this->controller->registry->xml->parse($output);
268
269 // get _nodes
270 $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));
271 $output = implode("\n", $output);
272 $infolist = $this->controller->registry->xml->parse($output);
273
274 // other part of _nodes: properties
275 $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));
276 foreach ($output AS $line)
277 {
278 if (preg_match('#^Properties on \'(.*?)\':$#', $line, $bits))
279 {
280 $proplist["$index"]["$curprop"] = trim($proplist["$index"]["$curprop"]);
281 $index = str_replace($this->controller->registry->repos->fetch_path($this->controller->registry->paths->repos), '', $bits[1]);
282 $capture = false;
283 }
284 else
285 {
286 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line, $matches))
287 {
288 $curprop = $matches[1];
289 $proplist["$index"]["$curprop"] = $matches[2] . "\n";
290 $capture = true;
291 }
292 else if ($capture == true)
293 {
294 $proplist["$index"]["$curprop"] .= $line . "\n";
295 }
296 }
297 }
298
299 // construct _revs inserts and the list of add revisions
300 foreach ($tree['log']['logentry'] AS $log)
301 {
302 $this->controller->registry->xml->unify_node($log['paths']['path']);
303
304 $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'])) . "')";
305
306 foreach ($log['paths']['path'] AS $path)
307 {
308 if (trim($path['action']) == 'A')
309 {
310 $path['value'] = preg_replace('#^/#', '', $path['value']);
311 $addlist["$path[value]"] = $log['revision'];
312 }
313 }
314 }
315
316 // construct list of HEAD nodes for _nodes
317 foreach ($infolist['info']['entry'] AS $node)
318 {
319 $history = $this->controller->registry->svn->svn('log --xml ' . $node['url']['value']);
320 $history = implode("\n", $history);
321 $history = $this->controller->registry->xml->parse($history);
322
323 $loglist = array();
324 $latestrev = -1;
325 foreach ($history['log']['logentry'] AS $log)
326 {
327 $loglist["$log[revision]"] = array(
328 'revision' => $log['revision'],
329 'author' => $log['author']['value'],
330 'date' => $log['date']['value'],
331 'message' => $log['msg']['value']
332 );
333 }
334
335 $inserts['nodes'][] = "('$node[path]', '" . $node['kind'] . "', " . $node['commit']['revision'] . ", '" . $this->controller->registry->escape(serialize($loglist)) . "', '" . $this->controller->registry->escape(serialize($proplist["$node[path]"])) . "')";
336 }
337
338 // insert _revs
339 $this->controller->registry->db->query("
340 REPLACE INTO {$this->hash}_revs
341 (revision, author, dateline, message, files)
342 VALUES
343 " . implode(",\n", $inserts['revs'])
344 );
345
346 // insert _nodes
347 $this->controller->registry->db->query("
348 REPLACE INTO {$this->hash}_nodes
349 (name, node, revision, history, properties)
350 VALUES
351 " . implode(",\n", $inserts['nodes'])
352 );
353
354 $this->controller->registry->debug("TIME TO (RE)BUILD: " . $this->controller->registry->funct->fetch_microtime_diff($start));
355 }
356 }
357
358 /*=====================================================================*\
359 || ###################################################################
360 || # $HeadURL$
361 || # $Id$
362 || ###################################################################
363 \*=====================================================================*/
364 ?>