]>
src.bluestatic.org Git - viewsvn.git/blob - includes/cachev.php
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.
47 var $controller = null ;
56 * Record count - the number of records in cacheV
62 * Memcache for all fetched revisions so we don't have to query-dupe
65 var $memcache = array ( 'revs' => array (), 'nodes' => array ());
67 // ###################################################################
69 * Constructor: initialies the registry
71 * @param object Controller
73 function cacheV (& $controller )
75 $this- > controller
=& $controller ;
79 // ###################################################################
81 * Sets the hash so we know what table we're dealing with
87 $this- > hash
= md5 ( $this- > controller
-> repospath
);
88 $this- > controller
-> registry
-> debug ( "hash: $this- >hash" );
91 // ###################################################################
93 * Returns a node string that has the beginning and ending slashes
94 * removed to allow it to match to the _nodes cacheV table
98 * @param string Original string
100 * @return string Matchable string
102 function fetch_node_string ( $node )
104 trigger_error ( 'You shouldn \' t be calling fetch_node_string. It \' s evil.' , E_USER_WARNING
);
105 return preg_replace ( '#(^/|/$)#' , '' , $node );
108 // ###################################################################
110 * Returns a specific log entry
114 * @param integer Revision number
116 * @return array Complete revision/commit entry
118 function fetch_revision ( $revision )
120 $revision = $this- > controller
-> registry
-> clean ( $revision , TYPE_UINT
);
122 if (! isset ( $this- > memcache
[ 'revs' ][ " $revision" ]))
124 $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" ));
125 $this- >memcache['revs'][" $revision" ][ 'files' ] = unserialize ( $this- > memcache
[ 'revs' ][ " $revision" ]['files']);
128 return $this- >memcache['revs'][" $revision" ];
131 // ###################################################################
133 * Returns the revision entry before the specified one
137 * @param integer Revision number
139 * @return array Complete revision/commit entry
141 function fetch_prev_revision ( $revision )
143 $data = $this- > fetch_node ();
144 $data = $data [ 'history' ];
145 if ( sizeof ( $data ) < 1 )
147 return $this- > fetch_revision ( 0 );
150 $list = array_keys ( $data );
151 $key = array_search ( $revision , $list );
153 if ( $revision == 'HEAD' )
158 $key ++
; // go to the next earliest revision
159 if (! isset ( $list [ " $key" ]))
164 return $this- >fetch_revision( $list [" $key" ]);
167 // ###################################################################
169 * Returns the latest revision that the file is at
173 * @return integer HEAD revision
175 function fetch_head_revision ()
177 $data = $this- > fetch_node ();
178 $data = $data [ 'history' ];
180 return max ( array_keys ( $data ));
183 // ###################################################################
185 * Fetches the latest revision for a given path
189 * @return integer Latest revision; FALSE if none (not in HEAD)
191 function fetch_node ()
193 $node = $this- > controller
-> path
;
194 if (! isset ( $this- > memcache
[ 'nodes' ][ " $node" ]))
196 $result = $this- >controller->registry->db->query_first(" SELECT
* FROM {$this
-> hash
} _nodes WHERE name
= '" . $this- >controller->registry->escape( $node ) . "' ");
197 if ( $result == false)
202 $this- >memcache['nodes'][" $node" ] = $result ;
203 $this- > memcache
[ 'nodes' ][ " $node" ]['history'] = unserialize( $this- >memcache['nodes'][" $node" ][ 'history' ]);
206 return $this- > memcache
[ 'nodes' ][ " $node" ];
209 // ###################################################################
211 * Checks to see if a given node is a directory. Returns TRUE if so.
215 * @return bool TRUE if directory, FALSE if not
219 $node = $this- >fetch_node();
220 if ( $node ['node'] == 'dir')
228 // ###################################################################
230 * Checks to see if it's necessary to rebuild the cacheV table for the
231 * current repository. This is done by making sure $count > 0. If not,
232 * then rebuild() is run. This also checks against the cacheV table
233 * to make sure that it's up-to-date against the root repository.
237 function exec_build()
239 $result = $this- >controller->registry->db->query_first(" SELECT
MAX ( revision
) AS max FROM {$this
-> hash
} _revs
");
240 $this- >count = $result ['max'];
242 // time to go from the start
243 if ( $this- >count == 0)
249 // send an Xquery to SVN to see if we need to update
250 $query = $this- >controller->library->svn('info --xml ' . $this- >controller->repospath);
251 $query = implode(" \n
", $query );
253 $tree = $this- >controller->registry->xml->parse( $query );
255 if ( $tree ['info']['entry']['revision'] != $this- >count)
257 $this- >build( $this- >count);
262 // ###################################################################
264 * Builds the cacheV table. This can be used to build only part of the
265 * cache or the entire thing, if the revision is set to NULL.
269 * @param integer Lower (current) revision
271 function build( $revision )
273 $start = microtime();
276 $output = $this- >controller->library->svn('log --xml -v ' . ( $revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this- >controller->repospath);
277 $output = implode(" \n
", $output );
278 $tree = $this- >controller->registry->xml->parse( $output );
281 $output = $this- >controller->library->svn('info --xml -R ' . ( $revision !== null ? '-r' . $revision . ':HEAD ' : '') . $this- >controller->repospath);
282 $output = implode(" \n
", $output );
283 $infolist = $this- >controller->registry->xml->parse( $output );
285 // other part of _nodes: properties
286 $output = $this- >controller->library->svn('proplist -v -R ' . ( $revision !== null ? ' -r' . $revision . ':HEAD ' : '') . $this- >controller->repospath);
287 foreach ( $output AS $line )
289 if (preg_match('#^Properties on \' (.*?) \' :$#', $line , $bits ))
291 $proplist [" $index" ][ " $curprop" ] = trim( $proplist [" $index" ][ " $curprop" ]);
292 $index = str_replace( $this- >controller->repospath, '', $bits [1]);
297 if (preg_match('#^\s+(.*)\s:\s(.*)#', $line , $matches ))
299 $curprop = $matches [1];
300 $proplist [" $index" ][ " $curprop" ] = $matches [2] . " \n
";
303 else if ( $capture == true)
305 $proplist [" $index" ][ " $curprop" ] .= $line . " \n
";
310 // construct _revs inserts and the list of add revisions
311 foreach ( $tree ['log']['logentry'] AS $log )
313 $this- >controller->registry->xml->unify_node( $log ['paths']['path']);
315 $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
'])) . "' ) ";
317 foreach ( $log ['paths']['path'] AS $path )
319 if (trim( $path ['action']) == 'A')
321 $path ['value'] = preg_replace('#^/#', '', $path ['value']);
322 $addlist [" $path [ value
] "] = $log ['revision'];
327 // construct list of HEAD nodes for _nodes
328 foreach ( $infolist ['info']['entry'] AS $node )
330 $history = $this- >controller->library->svn('log --xml ' . $node ['url']['value']);
331 $history = implode(" \n
", $history );
332 $history = $this- >controller->registry->xml->parse( $history );
336 foreach ( $history ['log']['logentry'] AS $log )
338 // WHY THE HELL DOES THIS GET HIT ON REBUILDS?
345 $loglist [" $log [ revision
] "] = array(
346 'revision' => $log ['revision'],
347 'author' => $log ['author']['value'], // why does PHP5 hate this?
348 'date' => $log ['date']['value'],
349 'message' => $log ['msg']['value']
353 $path = str_replace( $this- >controller->repospath, '', $node ['url']['value']);
354 $path = ( $path == '' ? '/' : $path );
356 $inserts ['nodes'][] = " ( ' $path' , ' " . $node ['kind'] . " ', " . $node [' commit
'][' revision
'] . ", ' " . $this- >controller->registry->escape(serialize( $loglist )) . " ', ' " . $this- >controller->registry->escape(serialize( $proplist [" $path" ])) . "')" ;
360 $this- > controller
-> registry
-> db
-> query ( "
361 REPLACE INTO {$this->hash} _revs
362 (revision, author, dateline, message, files)
364 " . implode ( ", \n " , $inserts [ 'revs' ])
368 $this- > controller
-> registry
-> db
-> query ( "
369 REPLACE INTO {$this->hash} _nodes
370 (name, node, revision, history, properties)
372 " . implode ( ", \n " , $inserts [ 'nodes' ])
375 $this- > controller
-> registry
-> debug ( "TIME TO (RE)BUILD: " . $this- > controller
-> registry
-> funct
-> fetch_microtime_diff ( $start ));
379 /*=====================================================================*\
380 || ###################################################################
383 || ###################################################################
384 \*=====================================================================*/